From 09985de2255a93064d450dd3416e7012546524b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Thu, 13 Jun 2024 12:32:08 +0200 Subject: [PATCH] Always clear cached amount before storing it This is safer as if for some reason the cache is filled with wrong data, the values will be fixed instead of keep propagating it. --- modules/account_invoice/invoice.py | 10 ++++++---- modules/purchase/purchase.py | 6 ++++++ modules/purchase_amendment/purchase.py | 4 ---- modules/purchase_requisition/purchase.py | 4 ++++ modules/sale/sale.py | 6 ++++++ modules/sale_amendment/sale.py | 4 ---- 6 files changed, 22 insertions(+), 12 deletions(-) diff --git a/modules/account_invoice/invoice.py b/modules/account_invoice/invoice.py index 0562b7f500..618f18c4dd 100644 --- a/modules/account_invoice/invoice.py +++ b/modules/account_invoice/invoice.py @@ -1762,11 +1762,13 @@ class Invoice(Workflow, ModelSQL, ModelView, TaxableMixin, InvoiceReportMixin): @classmethod def _store_cache(cls, invoices): + invoices = list(invoices) + cls.write(invoices, { + 'untaxed_amount_cache': None, + 'tax_amount_cache': None, + 'total_amount_cache': None, + }) for invoice in invoices: - if (invoice.untaxed_amount == invoice.untaxed_amount_cache - and invoice.tax_amount == invoice.tax_amount_cache - and invoice.total_amount == invoice.total_amount_cache): - continue invoice.untaxed_amount_cache = invoice.untaxed_amount invoice.tax_amount_cache = invoice.tax_amount invoice.total_amount_cache = invoice.total_amount diff --git a/modules/purchase/purchase.py b/modules/purchase/purchase.py index 9db98fb862..60c4593aaf 100644 --- a/modules/purchase/purchase.py +++ b/modules/purchase/purchase.py @@ -766,6 +766,12 @@ class Purchase( @classmethod def store_cache(cls, purchases): + purchases = list(purchases) + cls.write(purchases, { + 'untaxed_amount_cache': None, + 'tax_amount_cache': None, + 'total_amount_cache': None, + }) for purchase in purchases: purchase.untaxed_amount_cache = purchase.untaxed_amount purchase.tax_amount_cache = purchase.tax_amount diff --git a/modules/purchase_amendment/purchase.py b/modules/purchase_amendment/purchase.py index 6c1b9c241f..f973ff24e2 100644 --- a/modules/purchase_amendment/purchase.py +++ b/modules/purchase_amendment/purchase.py @@ -119,10 +119,6 @@ class Amendment(Workflow, ModelSQL, ModelView): line.apply(purchase) # Force saved lines purchase.lines = purchase.lines - # Force recompute cache - purchase.untaxed_amount_cache = None - purchase.tax_amount_cache = None - purchase.total_amount_cache = None Purchase.save(purchases) Purchase.store_cache(purchases) diff --git a/modules/purchase_requisition/purchase.py b/modules/purchase_requisition/purchase.py index b479c0bd87..32c2cd97c7 100644 --- a/modules/purchase_requisition/purchase.py +++ b/modules/purchase_requisition/purchase.py @@ -247,6 +247,10 @@ class PurchaseRequisition(Workflow, ModelSQL, ModelView): @classmethod def store_cache(cls, requisitions): + requisitions = list(requisitions) + cls.write(requisitions, { + 'total_amount_cache': None, + }) for requisition in requisitions: requisition.total_amount_cache = requisition.total_amount cls.save(requisitions) diff --git a/modules/sale/sale.py b/modules/sale/sale.py index 949a76a6d5..155fdca49f 100644 --- a/modules/sale/sale.py +++ b/modules/sale/sale.py @@ -866,6 +866,12 @@ class Sale( @classmethod def store_cache(cls, sales): + sales = list(sales) + cls.write(sales, { + 'untaxed_amount_cache': None, + 'tax_amount_cache': None, + 'total_amount_cache': None, + }) for sale in sales: sale.untaxed_amount_cache = sale.untaxed_amount sale.tax_amount_cache = sale.tax_amount diff --git a/modules/sale_amendment/sale.py b/modules/sale_amendment/sale.py index c5c516fb1a..7d031929a7 100644 --- a/modules/sale_amendment/sale.py +++ b/modules/sale_amendment/sale.py @@ -119,10 +119,6 @@ class Amendment(Workflow, ModelSQL, ModelView): line.apply(sale) # Force saved lines sale.lines = sale.lines - # Force recompute cache - sale.untaxed_amount_cache = None - sale.tax_amount_cache = None - sale.total_amount_cache = None Sale.save(sales) Sale.store_cache(sales)