如何使用Python在Odoo 11中的发票上创建计算字段?

问题描述 投票:2回答:2

我们公司根据逐行给予的折扣支付佣金。我试图在我们发送给销售代表的发票副本上显示此佣金金额。

我在v12.0中使用Odoo studio,并在account.invoice.line模型中创建了一个标记为“Commission Amount”的字段x_studio_field_dakHb。

我检查了“Readonly”和“Stored”框。在“依赖关系”字段中,我有“discount,price_subtotal”。

在“高级属性”部分中,我有:

def compute_commission_amount(self):
  for record in self:
    if (discount >= 55.0):
      x_studio_field_dakHb = (price_subtotal * .05)
    elif (discount >= 45.0):
      x_studio_field_dakHb = (price_subtotal * .10)
    elif (discount >= 30.0):
      x_studio_field_dakHb = (price_subtotal * .15)
    elif (discount >= 25.0):
      x_studio_field_dakHb = (price_subtotal * .20)
    elif (discount >= 0.0):
      x_studio_field_dakHb = (price_subtotal * .25)

我没有收到任何错误,但是这个领域并没有像我预期的那样计算。

我期待的一个例子如下:

Invoice Table

我的代码中是否有一些东西可以让它正确计算?

python odoo odoo-11 computed-field
2个回答
1
投票

您必须使用record来分配您的值。在字段上,您将找到以下有关计算方法的提示:

Compute字段是用于计算一组记录中字段值的Python代码。必须使用类似字典的分配将字段的值分配给每个记录。

for record in self:
    record['size'] = len(record.name)

所以你的代码应该像:

def compute_commission_amount(self):
    for record in self:
        if (record.discount >= 55.0):
            record['x_studio_field_dakHb'] = (record.price_subtotal * .05)
        elif (record.discount >= 45.0):
            record['x_studio_field_dakHb'] = (record.price_subtotal * .10)
        elif (record.discount >= 30.0):
            record['x_studio_field_dakHb'] = (record.price_subtotal * .15)
        elif (record.discount >= 25.0):
            record['x_studio_field_dakHb'] = (record.price_subtotal * .20)
        elif (record.discount >= 0.0):
            record['x_studio_field_dakHb'] = (record.price_subtotal * .25)

编辑:还有price_subtotal的另一个错误,应该从record获得。 Edit2:和discount一样


0
投票

实际上你需要使用@ api.depends('fields1','fields2',..)更改每个fields1或fields2,你的commision值会改变。对于示例代码,您可以在系统更改子总计中找到符合计算产品价格,数量,折扣和税收的信息。

© www.soinside.com 2019 - 2024. All rights reserved.