On_change is taking the old field value

Dear Tryton team.
I’m working on a discount functionally for invoice:

  1. User has to input the % of discount
  2. Tryton will calculate the amount of this particular discount ( from payable amount and I need this field on db , so I can’t use function field)
  3. I’ll show a total-discount (using a function field)

Everything seems good, but I’m doing something wrong with the on_change method, when I put 10% and then let’s say I change for 20% the invoice_discount_amount field is calculated with 10%, like calculated with previous value of % in all cases.

Any help will be appreciated:

class Invoice(metaclass=PoolMeta):
    __name__ = 'account.invoice'

    invoice_discount = fields.Numeric('Descuento %')
    invoice_discount_amount= fields.Numeric('Monto del descuento')
    invoice_with_discount = fields.Function(fields.Numeric('Total con descuento',depends=['invoice_discount_amount'
        ,'total_amount']),'amount_with_discount')

    def amount_with_discount(self, name):
        try:
            amount_with_discount = self.total_amount - self.invoice_discount_amount
        except:
            amount_with_discount = self.total_amount
        return amount_with_discount

    @fields.depends('untaxed_amount','tax_amount','invoice_discount')
    def on_change_invoice_discount(self, name=None):
        if self.untaxed_amount:
            amount = self.untaxed_amount + self.tax_amount
            discount_amount = Decimal(self.invoice_discount/100) * amount
            self.invoice_discount_amount = discount_amount

Well, It’s working using the on_change_with on destination field, here’s the working method in case someone has a similar issue :wink: :

@fields.depends('untaxed_amount','tax_amount','invoice_discount')
def on_change_with_invoice_discount_amount(self, name=None):
    if self.untaxed_amount:
        amount = self.untaxed_amount + self.tax_amount
        discount_amount = Decimal(self.invoice_discount/100) * amount
        return discount_amount
    else:
        return 0

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.