Computed Fields in Odoo 14

Viewed 615

In pos.order.line , I added a boolean field ; I added 2 fields compute; I want to get the sum of lines into 2 fields according to boolean value:

sum of (qty * price_unit) of lines with boolean False in 'amount1'
sum of (qty * price_unit) of lines with boolean True in 'amount2'

How can I do it please?

class PosOrder(models.Model):
    _inherit = "pos.order"


    amount1 = fields.Monetary(compute='_compute_amount1', readonly=True)
    amount2 = fields.Monetary(compute='_compute_amount2', readonly=True)

    @api.depends('payment_ids', 'lines')
    def _compute_amount_ht(self):
        for line in self.lines:
            if line.is_false == True:
                self.amount1 += line.qty * line.price_unit
            else:
                self.amount2 += line.qty * line.price_unit



class PosOrderLine(models.Model):
    _inherit = "pos.order.line"
    is_false = fields.Boolean(default=False)
1 Answers

You need to iterate self first and then do sum as per your formula. And then assign it back to pos.order fields. For example,

@api.depends('payment_ids', 'lines')
def _compute_amount_ht(self):
    for order in self:
        amount1 = 0.0
        amount2 = 0.0
        for line in order.lines:
            if line.is_false == True:
                amount1 += line.qty * line.price_unit
            else:
                amount2 += line.qty * line.price_unit
        order.amount1 = amount1
        order.amount2 = amount2
Related