From be68d7ad69cd2e32c90b379dd462a158262e700e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Sat, 27 Jul 2024 10:16:51 +0200 Subject: [PATCH] Use a separate browse list to compute amounts from non cached states The browse record list loops over the ids of the list to complete the read batch. So move the record without cache at the end does not prevent to read the lines of the first records with cache. A separate browse list must be used for the records without cache to prevent to read lines from record with cache. Closes #13396 --- modules/purchase/purchase.py | 11 +++++++---- modules/purchase_requisition/purchase.py | 11 +++++++---- modules/sale/sale.py | 11 +++++++---- 3 files changed, 21 insertions(+), 12 deletions(-) diff --git a/modules/purchase/purchase.py b/modules/purchase/purchase.py index 50a354e541..c8f63bc538 100644 --- a/modules/purchase/purchase.py +++ b/modules/purchase/purchase.py @@ -532,11 +532,14 @@ class Purchase( compute_taxes = True else: compute_taxes = False - # Sort cached first and re-instanciate to optimize cache management - purchases = sorted(purchases, - key=lambda p: p.state in cls._states_cached, reverse=True) - purchases = cls.browse(purchases) + # Browse separately not cached to limit number of lines read + cached, not_cached = [], [] for purchase in purchases: + if purchase.state in cls._states_cached: + cached.append(purchase) + else: + not_cached.append(purchase) + for purchase in chain(cached, cls.browse(not_cached)): if (purchase.state in cls._states_cached and purchase.untaxed_amount_cache is not None and purchase.tax_amount_cache is not None diff --git a/modules/purchase_requisition/purchase.py b/modules/purchase_requisition/purchase.py index 95b7954906..9c03dc2aaf 100644 --- a/modules/purchase_requisition/purchase.py +++ b/modules/purchase_requisition/purchase.py @@ -259,11 +259,14 @@ class PurchaseRequisition(Workflow, ModelSQL, ModelView): def get_amount(cls, requisitions, name): total_amount = {} - # Sort cached first and re-instantiate to optimize cache management - requisitions = sorted(requisitions, - key=lambda r: r.state in cls._states_cached, reverse=True) - requisitions = cls.browse(requisitions) + # Browse separately not cached to limit number of lines read + cached, not_cached = [], [] for requisition in requisitions: + if requisition.state in cls._states_cached: + cached.append(requisition) + else: + not_cached.append(requisition) + for requisition in chain(cached, cls.browse(not_cached)): if (requisition.state in cls._states_cached and requisition.total_amount_cache is not None): total_amount[requisition.id] = requisition.total_amount_cache diff --git a/modules/sale/sale.py b/modules/sale/sale.py index 97669e820e..268cd3ee75 100644 --- a/modules/sale/sale.py +++ b/modules/sale/sale.py @@ -587,11 +587,14 @@ class Sale( 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) + # Browse separately not cached to limit number of lines read + cached, not_cached = [], [] for sale in sales: + if sale.state in cls._states_cached: + cached.append(sale) + else: + not_cached.append(sale) + for sale in chain(cached, cls.browse(not_cached)): if (sale.state in cls._states_cached and sale.untaxed_amount_cache is not None and sale.tax_amount_cache is not None