From f6a968535d0400503c8631b760f9d9ca09cbccef Mon Sep 17 00:00:00 2001 From: cosmos Date: Thu, 31 Aug 2023 13:27:25 -0500 Subject: [PATCH] Add fields total_discuont and total_discount_cache --- sale.py | 61 ++++++++++++++++++++++++++++++++++++++++++++++ view/sale_form.xml | 4 +++ 2 files changed, 65 insertions(+) diff --git a/sale.py b/sale.py index aacb941..3497c86 100644 --- a/sale.py +++ b/sale.py @@ -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 diff --git a/view/sale_form.xml b/view/sale_form.xml index e29bdd4..dff9d0b 100644 --- a/view/sale_form.xml +++ b/view/sale_form.xml @@ -12,4 +12,8 @@ this repository contains the full copyright notices and license terms. --> + +