From f3791e4fee028c8bb09f3455db477f0b5a2b50b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Wed, 13 Jan 2016 21:05:55 +0100 Subject: [PATCH] Don't set missing attribute on line in _on_change_lines_taxes because it is sent back to the client as changed values, instead improve taxable_lines to work with missing values issue5103 review22691002 --- invoice.py | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/invoice.py b/invoice.py index 5d2be25345..7afdd63b2f 100644 --- a/invoice.py +++ b/invoice.py @@ -452,13 +452,6 @@ class Invoice(Workflow, ModelSQL, ModelView, TaxableMixin): if self.lines: for line in self.lines: self.untaxed_amount += getattr(line, 'amount', None) or 0 - for attribute, default_value in ( - ('taxes', []), - ('unit_price', Decimal(0)), - ('quantity', 0.), - ): - if not getattr(line, attribute, None): - setattr(line, attribute, default_value) computed_taxes = self._get_taxes() def is_zero(amount): @@ -733,8 +726,22 @@ class Invoice(Workflow, ModelSQL, ModelView, TaxableMixin): @property def taxable_lines(self): - return [(l.taxes, l.unit_price, l.quantity) for l in self.lines - if l.type == 'line'] + taxable_lines = [] + # In case we're called from an on_change we have to use some sensible + # defaults + for line in self.lines: + if getattr(line, 'type', None) != 'line': + continue + taxable_lines.append(tuple()) + for attribute, default_value in [ + ('taxes', []), + ('unit_price', Decimal(0)), + ('quantity', 0.), + ]: + value = getattr(line, attribute, None) + taxable_lines[-1] += ( + value if value is not None else default_value,) + return taxable_lines @property def tax_type(self):