This question has been flagged
3 Replies
5252 Views

Hi,


Sorry, I can't insert a picture. You can look in the link below.

https://s18.postimg.org/onx8z4de1/test.png


Can i totally records with the same product_id in one2many field after click "save" ?


Example In the picture, I have 2 product_id named "test0001" qty 11, 22 and a price is 456.


I want to totally it. When i click save,... So the result should be only one record with "33" qty ,not 11 and 22.

Like this

0 test0001 test01 test01 33 456.00 0.00


Thank you...

Avatar
Discard

Try dictionary.of dictionaries

sumup={

'item_id': {'qty': 0, 'prise': 0, 'sum': 0]

}

or dictionary of lists or list of lists

The code just the idea.

sumup={}

for item in items:

sumup[item.description]['qty']+=item.qty

sumup[item.description]['sum']+=item.sum

sumup[item.description]['prise']=sumup[item.description]['sum']/sumup[item.description]['qty']

delete items

write sumup instead of items

Author

Thank you, this idea should execute after Add an item right ?

Best Answer

I get records in record.department_id.id. These records have record.price_subtotal. I need to add record.price_subtotal if they have the same record.department_id.id. How do I do this with the .get() method And output it to the xml

At the moment the records CPA =50 and CPA =500. and I need one record 'CPA' and total_amount_interim = 100

if not reconciliation_act_id:
self.env["alfaleads.accounts.receivable"].create({"reconciliation_act_id": self.id})

for record in self.reconciliation_act_line_ids:
if record.department_id.id:
vals = {
"department_id": record.department_id.id,
"total_amount_interim": record.price_subtotal,
"account_receivable_id": self.accounts_receivable_id.id,
}
self.env["alfaleads.accounts.interim.total"].create(vals)
else:
for record in self.reconciliation_act_line_ids:
if record.department_id.id:
reconciliation_act_id.write({'alfaleads_accounts_department_total_ids': [(0, 0, {
"department_id": record.department_id.id,
"total_amount_interim": record.price_subtotal,
"account_receivable_id": self.accounts_receivable_id.id})]})


Avatar
Discard
Best Answer

Hi StCrownClown,

Step 1: You create a function when click on the save button  

https://www.odoo.com/forum/help-1/question/how-to-recall-some-certain-function-when-click-save-button-in-creating-form-105059#answer-105183

Step 2: Create a loop of one2many field inside the function

Step 3: Seperate the list of unique and duplicate sale order line ids to the variable

ids= []
duplicate_ids = []
for line_id in self.order_line:
    if line_id.product_id.id in ids:
        duplicate_ids.append(line_id.id)
    else:
        ids.append(line_id.id)

Now you can get the all sale order line unique ids in ids and all duplicate ids in duplicate_ids.

Step 4: You add the Qty of duplicate_ids to the corresponding line in ids.

self.env['sale.order.line'].search([('id', '=', id_in_ids)]).write({'product_uom_qty': qty_new_value,})

Step 5:

     Delete sale order lines in duplicate_ids

for duplicate_id in duplicate_ids:
     self.env['sale.order.line'].search([('id', '=', id_in_ids)]).unlink()

Note : My code is not tested, I wrote it for help

All the best !


Avatar
Discard
Author

Thank you, shameem@aχoncoms :)

Author Best Answer

any idea ?

:)

Avatar
Discard