Dear Tryton team.
I’m working on a discount functionally for invoice:
- User has to input the % of discount
- 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)
- 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