From 1d49a0b74ae8335f7853a1be6219179eeb38100f Mon Sep 17 00:00:00 2001
From: Vincent Pelletier <vincent@nexedi.com>
Date: Tue, 5 Dec 2017 14:52:47 +0900
Subject: [PATCH] erp5_accounting: Factorise grouping query generation.

Allows overriding it on instances where the invariant
  grouping_reference IS NULL == grouping_date IS NULL
is applicable, allowing simpler query execution plans.
---
 ...odule_getGeneralLedgerReportSectionList.py |  7 +--
 ..._getNotGroupedAccountingTransactionList.py |  6 +-
 ...ransactionModule_getAgedBalanceLineList.py |  7 +--
 .../ERP5Site_getNotGroupedAtDateSQLQuery.py   |  6 ++
 .../ERP5Site_getNotGroupedAtDateSQLQuery.xml  | 62 +++++++++++++++++++
 .../Node_getAccountingTransactionList.py      |  7 +--
 6 files changed, 74 insertions(+), 21 deletions(-)
 create mode 100644 bt5/erp5_accounting/SkinTemplateItem/portal_skins/erp5_accounting/ERP5Site_getNotGroupedAtDateSQLQuery.py
 create mode 100644 bt5/erp5_accounting/SkinTemplateItem/portal_skins/erp5_accounting/ERP5Site_getNotGroupedAtDateSQLQuery.xml

diff --git a/bt5/erp5_accounting/SkinTemplateItem/portal_skins/erp5_accounting/AccountModule_getGeneralLedgerReportSectionList.py b/bt5/erp5_accounting/SkinTemplateItem/portal_skins/erp5_accounting/AccountModule_getGeneralLedgerReportSectionList.py
index 3b4a5ae6b3..ed7c6a2350 100644
--- a/bt5/erp5_accounting/SkinTemplateItem/portal_skins/erp5_accounting/AccountModule_getGeneralLedgerReportSectionList.py
+++ b/bt5/erp5_accounting/SkinTemplateItem/portal_skins/erp5_accounting/AccountModule_getGeneralLedgerReportSectionList.py
@@ -1,6 +1,6 @@
 """Get the report sections for general ledger
 """
-from Products.ZSQLCatalog.SQLCatalog import SimpleQuery, ComplexQuery
+from Products.ZSQLCatalog.SQLCatalog import SimpleQuery
 from Products.ERP5Form.Report import ReportSection
 portal   = context.portal_url.getPortalObject()
 request  = portal.REQUEST
@@ -98,10 +98,7 @@ default_selection_params['no_mirror_section_uid_cache'] = 1
 # if user request report without grouping reference, don't show accounts that only have grouped lines in the period.
 if request.get('omit_grouping_reference', False):
   if at_date:
-    params['grouping_query'] = ComplexQuery(
-      SimpleQuery(grouping_reference=None),
-      SimpleQuery(grouping_date=at_date, comparison_operator=">="),
-      logical_operator="OR")
+    params['grouping_query'] = portal.ERP5Site_getNotGroupedAtDateSQLQuery(at_date)
   else:
     params['grouping_reference'] = None
 
diff --git a/bt5/erp5_accounting/SkinTemplateItem/portal_skins/erp5_accounting/Account_getNotGroupedAccountingTransactionList.py b/bt5/erp5_accounting/SkinTemplateItem/portal_skins/erp5_accounting/Account_getNotGroupedAccountingTransactionList.py
index 6d4d7e89b4..f59a0d5824 100644
--- a/bt5/erp5_accounting/SkinTemplateItem/portal_skins/erp5_accounting/Account_getNotGroupedAccountingTransactionList.py
+++ b/bt5/erp5_accounting/SkinTemplateItem/portal_skins/erp5_accounting/Account_getNotGroupedAccountingTransactionList.py
@@ -1,4 +1,3 @@
-from Products.ZSQLCatalog.SQLCatalog import SimpleQuery, ComplexQuery
 from Products.PythonScripts.standard import Object
 portal = context.getPortalObject()
 params = portal.ERP5Site_getAccountingSelectionParameterDict(selection_name)
@@ -14,10 +13,7 @@ total_credit_price = 0
 at_date = (from_date - 1).latestTime()
 inventory_query = {
   'at_date': at_date, # this is not to_date
-  'grouping_query': ComplexQuery(
-    SimpleQuery(grouping_reference=None),
-    SimpleQuery(grouping_date=at_date, comparison_operator=">="),
-    logical_operator="OR"),
+  'grouping_query': portal.ERP5Site_getNotGroupedAtDateSQLQuery(at_date),
   'simulation_state': params['simulation_state'],
   'node_uid': kw['node_uid'],
   'portal_type': portal.getPortalAccountingMovementTypeList(),
diff --git a/bt5/erp5_accounting/SkinTemplateItem/portal_skins/erp5_accounting/AccountingTransactionModule_getAgedBalanceLineList.py b/bt5/erp5_accounting/SkinTemplateItem/portal_skins/erp5_accounting/AccountingTransactionModule_getAgedBalanceLineList.py
index a60c904043..fe2ade2739 100644
--- a/bt5/erp5_accounting/SkinTemplateItem/portal_skins/erp5_accounting/AccountingTransactionModule_getAgedBalanceLineList.py
+++ b/bt5/erp5_accounting/SkinTemplateItem/portal_skins/erp5_accounting/AccountingTransactionModule_getAgedBalanceLineList.py
@@ -38,7 +38,6 @@ reportCallback ((line_list) -> list of dict)
   line_list.
 """
 from collections import defaultdict
-from Products.ZSQLCatalog.SQLCatalog import SimpleQuery, ComplexQuery
 from Products.PythonScripts.standard import Object
 if reportCallback is None:
   reportCallback = lambda x: x
@@ -86,11 +85,7 @@ for brain in portal.portal_simulation.getMovementHistoryList(
     section_category,
     section_category_strict,
   ),
-  grouping_query=ComplexQuery(
-    SimpleQuery(grouping_reference=None),
-    SimpleQuery(grouping_date=at_date, comparison_operator=">="),
-    logical_operator="OR",
-  ),
+  grouping_query=portal.ERP5Site_getNotGroupedAtDateSQLQuery(at_date),
   **extra_kw
 ):
   total_price = brain.total_price or 0
diff --git a/bt5/erp5_accounting/SkinTemplateItem/portal_skins/erp5_accounting/ERP5Site_getNotGroupedAtDateSQLQuery.py b/bt5/erp5_accounting/SkinTemplateItem/portal_skins/erp5_accounting/ERP5Site_getNotGroupedAtDateSQLQuery.py
new file mode 100644
index 0000000000..e9c0c9b590
--- /dev/null
+++ b/bt5/erp5_accounting/SkinTemplateItem/portal_skins/erp5_accounting/ERP5Site_getNotGroupedAtDateSQLQuery.py
@@ -0,0 +1,6 @@
+from Products.ZSQLCatalog.SQLCatalog import SimpleQuery, ComplexQuery
+return ComplexQuery(
+  SimpleQuery(grouping_reference=None),
+  SimpleQuery(grouping_date=grouping_date, comparison_operator=">="),
+  logical_operator="OR",
+)
diff --git a/bt5/erp5_accounting/SkinTemplateItem/portal_skins/erp5_accounting/ERP5Site_getNotGroupedAtDateSQLQuery.xml b/bt5/erp5_accounting/SkinTemplateItem/portal_skins/erp5_accounting/ERP5Site_getNotGroupedAtDateSQLQuery.xml
new file mode 100644
index 0000000000..55b5492f40
--- /dev/null
+++ b/bt5/erp5_accounting/SkinTemplateItem/portal_skins/erp5_accounting/ERP5Site_getNotGroupedAtDateSQLQuery.xml
@@ -0,0 +1,62 @@
+<?xml version="1.0"?>
+<ZopeData>
+  <record id="1" aka="AAAAAAAAAAE=">
+    <pickle>
+      <global name="PythonScript" module="Products.PythonScripts.PythonScript"/>
+    </pickle>
+    <pickle>
+      <dictionary>
+        <item>
+            <key> <string>Script_magic</string> </key>
+            <value> <int>3</int> </value>
+        </item>
+        <item>
+            <key> <string>_bind_names</string> </key>
+            <value>
+              <object>
+                <klass>
+                  <global name="NameAssignments" module="Shared.DC.Scripts.Bindings"/>
+                </klass>
+                <tuple/>
+                <state>
+                  <dictionary>
+                    <item>
+                        <key> <string>_asgns</string> </key>
+                        <value>
+                          <dictionary>
+                            <item>
+                                <key> <string>name_container</string> </key>
+                                <value> <string>container</string> </value>
+                            </item>
+                            <item>
+                                <key> <string>name_context</string> </key>
+                                <value> <string>context</string> </value>
+                            </item>
+                            <item>
+                                <key> <string>name_m_self</string> </key>
+                                <value> <string>script</string> </value>
+                            </item>
+                            <item>
+                                <key> <string>name_subpath</string> </key>
+                                <value> <string>traverse_subpath</string> </value>
+                            </item>
+                          </dictionary>
+                        </value>
+                    </item>
+                  </dictionary>
+                </state>
+              </object>
+            </value>
+        </item>
+        <item>
+            <key> <string>_params</string> </key>
+            <value> <string>grouping_date</string> </value>
+        </item>
+        <item>
+            <key> <string>id</string> </key>
+            <value> <string>ERP5Site_getNotGroupedAtDateSQLQuery</string> </value>
+        </item>
+      </dictionary>
+    </pickle>
+  </record>
+</ZopeData>
diff --git a/bt5/erp5_accounting/SkinTemplateItem/portal_skins/erp5_accounting/Node_getAccountingTransactionList.py b/bt5/erp5_accounting/SkinTemplateItem/portal_skins/erp5_accounting/Node_getAccountingTransactionList.py
index a03b2ba891..39b78c3e90 100644
--- a/bt5/erp5_accounting/SkinTemplateItem/portal_skins/erp5_accounting/Node_getAccountingTransactionList.py
+++ b/bt5/erp5_accounting/SkinTemplateItem/portal_skins/erp5_accounting/Node_getAccountingTransactionList.py
@@ -1,5 +1,5 @@
 from Products.ERP5Type.Document import newTempBase
-from Products.ZSQLCatalog.SQLCatalog import SimpleQuery, ComplexQuery
+from Products.ZSQLCatalog.SQLCatalog import SimpleQuery
 from Products.ERP5Type.Message import translateString
 from Products.ERP5Type.Log import log
 portal = context.getPortalObject()
@@ -89,10 +89,7 @@ if is_pl_account and not from_date:
 
 if portal.portal_selections.getSelectionParamsFor(selection_name).get('omit_grouping_reference'):
   if params.get('at_date'):
-    params['grouping_query'] = ComplexQuery(
-      SimpleQuery(grouping_reference=None),
-      SimpleQuery(grouping_date=params['at_date'], comparison_operator=">="),
-      logical_operator="OR")
+    params['grouping_query'] = portal.ERP5Site_getNotGroupedAtDateSQLQuery(params['at_date'])
   else:
     params['grouping_reference'] = None
 
-- 
2.30.9