Mohamed Fouad
Mohamed Fouad

Reputation: 526

how use odoo onchange method on current non saved order lines?

I'm working on update the sale order lines with discount value, and there is some product has an option no discount, that means ignoring then while distributing the discount values on the lines

here is the method

@api.onchange('discount_type', 'discount_rate', 'order_line', 'discount_durar')
def set_lines_discount(self):
    total = discount = 0.0
    for line in self.order_line:
        if self.discount_type == 'percentage':
            if line.no_discount_field:
                line.discount_durar = 0
            else:
                line.discount_durar = self.discount_rate
        else:
            if line.no_discount_field == True:
                line.discount_durar = 0
            else:
                total += (line.product_uom_qty * line.price_unit)
                if self.discount_rate != 0:
                    discount = (self.discount_rate / total) * 100
                else:
                    discount = self.discount_rate
                for line in self.order_line.search([('no_discount_field', '=', False)]):
                    line.discount_durar = discount

it's worked well in all conditions except the last one which I used search on it, it needs the order to be saved to be calculated and to loop over the lines.

how can in loop over the lines with a condition before I save the order?

Upvotes: 0

Views: 311

Answers (2)

Mohamed Fouad
Mohamed Fouad

Reputation: 526

i changed the search on sale.order.line object with filter

for line in self.order_line.filtered(lambda l: not l.no_discount_field):

then its worked well

Upvotes: 0

Lilit Zakaryan
Lilit Zakaryan

Reputation: 89

You can use python one-line list comprehension with condition. In your case you can have something like this.

[line for line in self.order_line if condition]

Upvotes: 1

Related Questions