From 8ddc1e987271d7c1d6dcee20653eff89463a5b8c Mon Sep 17 00:00:00 2001
From: Romain Courteaud <romain@nexedi.com>
Date: Thu, 6 Mar 2025 16:15:03 +0000
Subject: [PATCH] slapos_accounting: speed up Sale Supply retrieval by
 explicitely filtering with SQL

---
 ...Movement_getPriceCalculationOperandDict.py | 23 +++++++++----------
 1 file changed, 11 insertions(+), 12 deletions(-)

diff --git a/master/bt5/slapos_accounting/SkinTemplateItem/portal_skins/slapos_accounting/Movement_getPriceCalculationOperandDict.py b/master/bt5/slapos_accounting/SkinTemplateItem/portal_skins/slapos_accounting/Movement_getPriceCalculationOperandDict.py
index 3e4ea0f3a..4a11f4417 100644
--- a/master/bt5/slapos_accounting/SkinTemplateItem/portal_skins/slapos_accounting/Movement_getPriceCalculationOperandDict.py
+++ b/master/bt5/slapos_accounting/SkinTemplateItem/portal_skins/slapos_accounting/Movement_getPriceCalculationOperandDict.py
@@ -14,7 +14,7 @@ def sort_key_method(e):
     minus = 0
   return 0 - 1 * int(parent.hasDestination()) - minus
 
-def filter_method(currency, destination_project, group):
+def filter_method(currency, destination_project):
   def filter_by_source_function_and_group(l):
     ret = []
     for i in l:
@@ -43,22 +43,21 @@ def filter_method(currency, destination_project, group):
 
     return ret
   return filter_by_source_function_and_group
-source = context.getSourceValue()
-if source is None:
-  group = None
-else:
-  group = source.getGroup()
-kw['filter_method'] = filter_method(context.getPriceCurrency(), context.getDestinationProject(), group)
 
+kw['filter_method'] = filter_method(context.getPriceCurrency(), context.getDestinationProject())
 kw['sort_key_method'] = sort_key_method
 
 resource = context.getResourceValue()
+price_currency = context.getPriceCurrencyValue()
 
-if resource is not None:
-  product_line = resource.getProductLine()
-  if product_line:
-    kw['categories'] = kw.get('categories', []) + ['product_line/%s' % product_line]
-
+if (resource is not None) and (price_currency is not None):
+  # Explicitely filter with SQL, to reduce the number of predicates to check
+  # Currently, only Sales are handled
+  # See also SaleSupplyLine_asPredicate
+  kw['resource__uid'] = resource.getUid()
+  kw['price_currency__uid'] = price_currency.getUid()
+  kw['validation_state'] = 'validated'
+  kw['portal_type'] = ['Sale Supply Line', 'Sale Supply Cell']
   return resource.getPriceCalculationOperandDict(
     default=default, context=context, **kw)
 
-- 
2.30.9