Odoo 14中的计算字段

2024-05-29 01:47:39 发布

您现在位置:Python中文网/ 问答频道 /正文

在pos.order.line中,我添加了一个布尔字段;我添加了两个字段来计算; 我想根据布尔值将行的总和分成两个字段:

在'amount1'中布尔值为False的行的总和(数量*价格单位)
“amount2”中布尔值为真的行(数量*价格单位)之和

请问我怎么做

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)

Tags: posselffalsetruefields数量line单位
1条回答
网友
1楼 · 发布于 2024-05-29 01:47:39

您需要先迭代self,然后按照公式求和。然后将其分配回pos.order字段。比如说,

@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

相关问题 更多 >

    热门问题