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

How do I create a computed field on an invoice in Odoo 11 using Python?

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

我在 v12.0 中使用 Odoo studio,并创建了一个字段 x_studio_field_dakHb,标签为 "Commission Amount",出现在 account.invoice.line 模型中。

我已经勾选了 "Readonly" 和 "Stored" 框。在 "Dependencies" 字段中我有 "discount, price_subtotal".

在 "Advanced Properties" 部分我有:

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

我的代码中是否遗漏了一些无法正确计算的内容?

实际上你需要使用@api.depends('fields1','fields2',..) 每改变 fields1 或 fields2,您的佣金值就会改变。 对于示例代码,您可以在系统如何根据 callculate 产品价格、数量、折扣和税费更改小计中找到。

您必须使用 record 来分配您的值。在一个字段上,您会发现以下有关计算方法的提示:

The field Compute is the Python code to compute the value of the field on a set of records. The value of the field must be assigned to each record with a dictionary-like assignment.

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

相同