Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

1

Sum records with the same product_id in one2many field after click "save" ?

By
StCrownClown
on 10/31/16, 6:31 AM 309 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...

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

Artem
on 10/31/16, 9:06 AM

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

StCrownClown
on 11/1/16, 12:42 AM
0
shameem@aχoncoms
On 11/1/16, 1:45 AM

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 !


Thank you, shameem@aχoncoms :)

StCrownClown
on 11/1/16, 10:15 PM
0
StCrownClown
On 11/1/16, 12:39 AM

any idea ?

:)

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 10/31/16, 6:31 AM
Seen: 309 times
Last updated: 11/1/16, 2:29 AM