Use one getter for all amounts

issue2936
review897002
This commit is contained in:
Cédric Krier committed 2013-06-01 00:01:39 +02:00
1 parent 007f299220
commit 257aa9c21d
1 file changed
+38 -23
+38 -23
View File
@@ -22,6 +22,8 @@ __all__ = ['Sale', 'SaleInvoice', 'SaleIgnoredInvoice', 'SaleRecreatedInvoice',
'ReturnSale']
__metaclass__ = PoolMeta
_ZERO = Decimal(0)
class Sale(Workflow, ModelSQL, ModelView):
'Sale'
@@ -97,21 +99,21 @@ class Sale(Workflow, ModelSQL, ModelView):
comment = fields.Text('Comment')
untaxed_amount = fields.Function(fields.Numeric('Untaxed',
digits=(16, Eval('currency_digits', 2)),
depends=['currency_digits']), 'get_untaxed_amount')
depends=['currency_digits']), 'get_amount')
untaxed_amount_cache = fields.Numeric('Untaxed Cache',
digits=(16, Eval('currency_digits', 2)),
readonly=True,
depends=['currency_digits'])
tax_amount = fields.Function(fields.Numeric('Tax',
digits=(16, Eval('currency_digits', 2)),
depends=['currency_digits']), 'get_tax_amount')
depends=['currency_digits']), 'get_amount')
tax_amount_cache = fields.Numeric('Tax Cache',
digits=(16, Eval('currency_digits', 2)),
readonly=True,
depends=['currency_digits'])
total_amount = fields.Function(fields.Numeric('Total',
digits=(16, Eval('currency_digits', 2)),
depends=['currency_digits']), 'get_total_amount')
depends=['currency_digits']), 'get_amount')
total_amount_cache = fields.Numeric('Total Tax',
digits=(16, Eval('currency_digits', 2)),
readonly=True,
@@ -427,22 +429,11 @@ class Sale(Workflow, ModelSQL, ModelView):
res['total_amount'] = self.currency.round(res['total_amount'])
return res
def get_untaxed_amount(self, name):
if (self.state in self._states_cached
and self.untaxed_amount_cache is not None):
return self.untaxed_amount_cache
amount = sum((l.amount for l in self.lines if l.type == 'line'),
Decimal(0))
return self.currency.round(amount)
def get_tax_amount(self, name):
def get_tax_amount(self):
pool = Pool()
Tax = pool.get('account.tax')
Invoice = pool.get('account.invoice')
if (self.state in self._states_cached
and self.tax_amount_cache is not None):
return self.tax_amount_cache
context = self.get_tax_context()
taxes = {}
for line in self.lines:
@@ -458,15 +449,39 @@ class Sale(Workflow, ModelSQL, ModelView):
taxes[key] = val['amount']
else:
taxes[key] += val['amount']
amount = sum((self.currency.round(taxes[key]) for key in taxes),
Decimal(0))
return self.currency.round(amount)
return sum((self.currency.round(tax) for tax in taxes.values()), _ZERO)
def get_total_amount(self, name):
if (self.state in self._states_cached
and self.total_amount_cache is not None):
return self.total_amount_cache
return self.currency.round(self.untaxed_amount + self.tax_amount)
@classmethod
def get_amount(cls, sales, names):
untaxed_amount = {}
tax_amount = {}
total_amount = {}
for sale in sales:
if (sale.state in cls._states_cached
and sale.untaxed_amount_cache is not None
and sale.tax_amount_cache is not None
and sale.total_amount_cache is not None):
untaxed_amount[sale.id] = sale.untaxed_amount_cache
tax_amount[sale.id] = sale.tax_amount_cache
total_amount[sale.id] = sale.total_amount_cache
else:
untaxed_amount[sale.id] = sum(
(line.amount for line in sale.lines
if line.type == 'line'), _ZERO)
tax_amount[sale.id] = sale.get_tax_amount()
total_amount[sale.id] = (
untaxed_amount[sale.id] + tax_amount[sale.id])
result = {
'untaxed_amount': untaxed_amount,
'tax_amount': tax_amount,
'total_amount': total_amount,
}
for key in result.keys():
if key not in names:
del result[key]
return result
def get_invoice_state(self):
'''