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.
This commit is contained in:
Cédric Krier
2024-06-13 12:32:08 +02:00
parent 347a161353
commit 09985de225
6 changed files with 22 additions and 12 deletions
+6 -4
View File
@@ -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
+6
View File
@@ -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
-4
View File
@@ -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)
+4
View File
@@ -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)
+6
View File
@@ -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
-4
View File
@@ -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)