简体   繁体   English

Odoo 14 中的计算场

[英]Computed Fields in Odoo 14

In pos.order.line, I added a boolean field;在 pos.order.line 中,我添加了一个 boolean 字段; I added 2 fields compute;我添加了 2 个字段计算; I want to get the sum of lines into 2 fields according to boolean value:我想根据 boolean 值将行总和分为 2 个字段:

sum of (qty * price_unit) of lines with boolean False in 'amount1' 'amount1' 中带有 boolean False 的行的总和(数量 * price_unit)
sum of (qty * price_unit) of lines with boolean True in 'amount2' 'amount2' 中具有 boolean 的行的总和(数量 * price_unit)

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)

You need to iterate self first and then do sum as per your formula.您需要先迭代 self ,然后根据您的公式求和。 And then assign it back to pos.order fields.然后将其分配回 pos.order 字段。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM