Add fields total_discuont and total_discount_cache

This commit is contained in:
cosmos 2023-08-31 13:27:25 -05:00
parent 03d8711ab3
commit f6a968535d
2 changed files with 65 additions and 0 deletions

61
sale.py
View File

@ -11,6 +11,9 @@ class Sale(metaclass=PoolMeta):
__name__ = 'sale.sale'
pizza_number = fields.Integer("Number pizza")
total_discount = fields.Function(Monetary("Total Discount", currency='currency', digits='currency'),
'get_amount')
total_discount_cache = Monetary("Total Discount cache", currency='currency', digits='currency')
@classmethod
def __setup__(cls):
@ -27,6 +30,64 @@ class Sale(metaclass=PoolMeta):
def default_pizza_number(cls):
return 0
@classmethod
def store_cache(cls, sales):
for sale in sales:
sale.untaxed_amount_cache = sale.untaxed_amount
sale.tax_amount_cache = sale.tax_amount
sale.total_amount_cache = sale.total_amount
sale.total_discount_cache = sale.total_discount
cls.save(sales)
@classmethod
def get_amount(cls, sales, names):
untaxed_amount = {}
tax_amount = {}
total_amount = {}
total_discount = {}
if {'tax_amount', 'total_amount'} & set(names):
compute_taxes = True
else:
compute_taxes = False
# Sort cached first and re-instanciate to optimize cache management
sales = sorted(sales, key=lambda s: s.state in cls._states_cached,
reverse=True)
sales = cls.browse(sales)
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
and sale.total_discount is not None):
untaxed_amount[sale.id] = sale.untaxed_amount_cache
total_discount[sale.id] = sale.total_discount_cache
if compute_taxes:
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'), Decimal(0))
total_discount[sale.id] = sum(
(line.discount_amount for line in sale.lines
if line.type == 'line'), Decimal(0))
if compute_taxes:
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,
'total_discount': total_discount,
}
for key in list(result.keys()):
if key not in names:
del result[key]
return result
def get_invoice_resolution(subtype):
if subtype:
resolution = subtype.sequence.invoice_resolution

View File

@ -12,4 +12,8 @@ this repository contains the full copyright notices and license terms. -->
</group>
<newline/>
</xpath>
<xpath expr="/form/notebook/page[@id='purchase']/group[@id='amount']/field[@name='tax_amount']" position="after">
<label name="total_discount"/>
<field name="total_discount"/>
</xpath>
</data>