Skip to Content
Menu
This question has been flagged
4 Replies
6180 Views

I created a new record in a one2many field in a view, e.g. the order.line in a sales order screen. I need after each new line added in the detail, the one2many field, to sum the total amount of the order via an onchange method in the qty or price fields. I don't need to go to the 'update' button and click it each time I need to update the total amount. How to do that?

----

My idea is to access the vals array that is sent to the 'create' method when saving a record, I need to access it before saving.

Avatar
Discard
Best Answer

The one2many fields store the values in a format described on the write method of the model. For this task you need to pass the one2many field for the on_change method and check the state of their records in order to properly sum your total, something like this:

 def onchange_line_ids(self, cr, uid, ids, order_lines, context=None):  
    total = 0
    line_pool = self.pool.get('sale.order.line')
   for line in order_lines: 
if line[0] == 0:
             total += line[2].get('qty', 0)
        elif line[0] == 4:
            total += line_pool.browse(cr, uid, line[1], context=context).qty
        elif line[0] == 1:
             if line[2].get('qty', False):
                 total += line[2].get('qty', 0)
            else:
                 total += line_pool.browse(cr, uid, line[1], context=context).qty  
   return {'value': {'total': total}}

Avatar
Discard
Author

in the onchange method of the price I passed the 'order_id.order_line' and it didn't recognize it. I then passed only the 'order_id' on hope to access 'order_line' in the method, but it gave me what is called 'Newid' and was not able to accomplish my task

You cannot do that, you need to put the onchange on the main form level to be able to pass order_line field, you could put the onchange to the order_line field itself because it's the main source of changes that you need to monitor, and pass only order_line instead of order.order_line, in the view you cannot move very easily over relation fields

Author

thx Axel for answer, it has worked. I accepted the answer and gave you an upvote

Best Answer

Hi Tarek, 

If i am wright, you want to sum of total price/qty in your one2many field. you can simply add an attribute "sum" at the qty/price field under tree tag, like:

<tree> [your one2many field tree view]

<field name="[your field]" sum="Total" />

........

</tree>

it will show sum of your fields data at the end of tree view.....

Hope it helps!!    

Avatar
Discard
Author

I've already did that and I can see the sum of the amounts every time I do a change in qty or price in any line. My case is different, that is, I have a field that defines the upper limit of a Sales Order amount, this field is per customer, and I read this field in the sale_order class via a function and display it on the order header. When saving the order, the create/write method checks if the sum of the line amounts exceeded this limit, then it raises an exception and stops the save action. The user then have to go to lines and reduce qtys until the amounts sum go below the upper limit allowed. I need this be done automatically via an onchange method. I checked the last reply from Axel and still didn't try it. I think it would work

Related Posts Replies Views Activity
0
Sep 17
3035
1
Aug 16
9154
0
Apr 24
564
1
Dec 22
5259
0
Dec 19
3497