Cameron Wolfe
Cameron Wolfe

Reputation: 23

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

Our company pays commissions based on the discount given on a line by line basis. I am trying to show this commission amount on the invoice copy we send to the sales rep.

I am using Odoo studio in v12.0 and created a field x_studio_field_dakHb labeled "Commission Amount" that appears in account.invoice.line model.

I have checked the "Readonly" and "Stored" boxes. In "Dependencies" field I have "discount, price_subtotal".

In the "Advanced Properties" section I have:

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)

I am not getting any errors, but the field is not not calculating as I anticipated.

An example of what I am expecting would be as follows:

Invoice Table

Is there something I am missing in my code to get it to calculate properly?

Upvotes: 2

Views: 717

Answers (2)

CZoellner
CZoellner

Reputation: 14801

You have to use record to assign your values. On a field you will find following hint on compute methods:

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)

So your code should be like:

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)

Edit: and there was another mistake for price_subtotal which should be get from record. Edit2: and the same with discount

Upvotes: 1

Rinaldi
Rinaldi

Reputation: 260

Actually you need to use @api.depends('fields1','fields2',..) every fields1 or fields2 changed, your commision value will changed. for the sample code you can find in how system change sub total in line with callculate product price, quantity, discount, and tax.

Upvotes: 0

Related Questions