From c3fb98cd41e0f3b22581f308558ef7bc28cd739c Mon Sep 17 00:00:00 2001
From: Yusei Tahara <yusei@nexedi.com>
Date: Wed, 6 Feb 2008 15:48:22 +0000
Subject: [PATCH] Optimize query for calculation of debit and credit. Fix
 sorting bug of operation accounting date column.

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@19107 20353a03-c40f-0410-a6d1-a30d3c3de9de
---
 ...nModule_countAccountingTransactionList.xml | 19 +++++++-
 ...ionModule_getAccountingTransactionList.xml | 24 ++++++++--
 ...tingTransactionModule_statSourceCredit.xml | 21 ++++++++-
 ...ntingTransactionModule_statSourceDebit.xml | 21 ++++++++-
 .../listbox_credit.xml                        | 22 ++-------
 .../listbox_debit.xml                         | 22 ++-------
 .../listbox_operation_date.xml                |  6 ---
 ...onModule_zGetAccountingTransactionList.xml | 46 +++++++++++++++++--
 bt5/erp5_accounting/bt/revision               |  2 +-
 9 files changed, 127 insertions(+), 56 deletions(-)

diff --git a/bt5/erp5_accounting/SkinTemplateItem/portal_skins/erp5_accounting/AccountingTransactionModule_countAccountingTransactionList.xml b/bt5/erp5_accounting/SkinTemplateItem/portal_skins/erp5_accounting/AccountingTransactionModule_countAccountingTransactionList.xml
index 75a2f755c9..b44e9e020b 100644
--- a/bt5/erp5_accounting/SkinTemplateItem/portal_skins/erp5_accounting/AccountingTransactionModule_countAccountingTransactionList.xml
+++ b/bt5/erp5_accounting/SkinTemplateItem/portal_skins/erp5_accounting/AccountingTransactionModule_countAccountingTransactionList.xml
@@ -71,8 +71,18 @@
 kw[\'omit_input\'] = 0\n
 kw[\'omit_output\'] = 0\n
 \n
-if kw.get(\'delivery_start_date\'):\n
-  kw[\'delivery.start_date\'] = kw[\'delivery_start_date\']\n
+from Products.ZSQLCatalog.SQLCatalog import Query\n
+if kw.get(\'operation_date\', {}).get(\'query\'):\n
+  operation_date_dict = kw[\'operation_date\']\n
+  ss_q = Query(format=operation_date_dict[\'format\'],\n
+               type=operation_date_dict[\'type\'],\n
+               **{\'delivery.start_date\':operation_date_dict[\'query\']})\n
+  kw[\'source_section_where_expression\'] = ss_q()[\'where_expression\']\n
+  ds_q = Query(format=operation_date_dict[\'format\'],\n
+               type=operation_date_dict[\'type\'],\n
+               **{\'delivery.stop_date\':operation_date_dict[\'query\']})\n
+  kw[\'destination_section_where_expression\'] = ds_q()[\'where_expression\']\n
+  del kw[\'operation_date\']\n
 \n
 return context.AccountingTransactionModule_zGetAccountingTransactionList( selection=None,\n
                                                                           selection_params=kw,\n
@@ -126,8 +136,13 @@ return context.AccountingTransactionModule_zGetAccountingTransactionList( select
                             <string>kw</string>
                             <string>_getattr_</string>
                             <string>_write_</string>
+                            <string>Products.ZSQLCatalog.SQLCatalog</string>
+                            <string>Query</string>
                             <string>_getitem_</string>
+                            <string>operation_date_dict</string>
                             <string>_apply_</string>
+                            <string>ss_q</string>
+                            <string>ds_q</string>
                             <string>context</string>
                             <string>None</string>
                           </tuple>
diff --git a/bt5/erp5_accounting/SkinTemplateItem/portal_skins/erp5_accounting/AccountingTransactionModule_getAccountingTransactionList.xml b/bt5/erp5_accounting/SkinTemplateItem/portal_skins/erp5_accounting/AccountingTransactionModule_getAccountingTransactionList.xml
index a40cbda9b1..dbc3edc756 100644
--- a/bt5/erp5_accounting/SkinTemplateItem/portal_skins/erp5_accounting/AccountingTransactionModule_getAccountingTransactionList.xml
+++ b/bt5/erp5_accounting/SkinTemplateItem/portal_skins/erp5_accounting/AccountingTransactionModule_getAccountingTransactionList.xml
@@ -70,6 +70,9 @@
 # XXX workarounds for DTML limitation\n
 # Because DTML cannot do <dtml-if node or resource or...>\n
 # If Python is used, it does not check the existence of a variable automatically\n
+\n
+from Products.ZSQLCatalog.SQLCatalog import Query\n
+\n
 if \'node\' not in params:\n
   params[\'node\'] = []\n
 if \'resource\' not in params:\n
@@ -88,9 +91,17 @@ if params.get(\'count\', 0):\n
 params[\'omit_input\'] = 0\n
 params[\'omit_output\'] = 0\n
 \n
-# XXX this workaround is probably no longer needed\n
-if params.get(\'delivery_start_date\'):\n
-  params[\'delivery.start_date\'] = params[\'delivery_start_date\']\n
+if params.get(\'operation_date\', {}).get(\'query\'):\n
+  operation_date_dict = params[\'operation_date\']\n
+  ss_q = Query(format=operation_date_dict[\'format\'],\n
+               type=operation_date_dict[\'type\'],\n
+               **{\'delivery.start_date\':operation_date_dict[\'query\']})\n
+  params[\'source_section_where_expression\'] = ss_q()[\'where_expression\']\n
+  ds_q = Query(format=operation_date_dict[\'format\'],\n
+               type=operation_date_dict[\'type\'],\n
+               **{\'delivery.stop_date\':operation_date_dict[\'query\']})\n
+  params[\'destination_section_where_expression\'] = ds_q()[\'where_expression\']\n
+  del params[\'operation_date\']\n
 \n
 # this method can be used to build another complex query, for example\n
 # as a catalog.parent_uid filter with SimulationTool.getMovementList.\n
@@ -150,15 +161,20 @@ return context.AccountingTransactionModule_zGetAccountingTransactionList( \n
                         <value>
                           <tuple>
                             <string>params</string>
+                            <string>Products.ZSQLCatalog.SQLCatalog</string>
+                            <string>Query</string>
                             <string>_write_</string>
                             <string>_getattr_</string>
                             <string>_getitem_</string>
+                            <string>operation_date_dict</string>
+                            <string>_apply_</string>
+                            <string>ss_q</string>
+                            <string>ds_q</string>
                             <string>context</string>
                             <string>src__</string>
                             <string>no_limit</string>
                             <string>None</string>
                             <string>search_result_keys</string>
-                            <string>_apply_</string>
                           </tuple>
                         </value>
                     </item>
diff --git a/bt5/erp5_accounting/SkinTemplateItem/portal_skins/erp5_accounting/AccountingTransactionModule_statSourceCredit.xml b/bt5/erp5_accounting/SkinTemplateItem/portal_skins/erp5_accounting/AccountingTransactionModule_statSourceCredit.xml
index 8f6dcb2fe7..f591a4174d 100644
--- a/bt5/erp5_accounting/SkinTemplateItem/portal_skins/erp5_accounting/AccountingTransactionModule_statSourceCredit.xml
+++ b/bt5/erp5_accounting/SkinTemplateItem/portal_skins/erp5_accounting/AccountingTransactionModule_statSourceCredit.xml
@@ -70,8 +70,20 @@
 params[\'stat\'] = 1\n
 params[\'omit_input\'] = 1\n
 params[\'omit_output\'] = 0\n
-if params.get(\'delivery_start_date\'):\n
-  params[\'delivery.start_date\'] = params[\'delivery_start_date\']\n
+\n
+from Products.ZSQLCatalog.SQLCatalog import Query\n
+\n
+if params.get(\'operation_date\', {}).get(\'query\'):\n
+  operation_date_dict = params[\'operation_date\']\n
+  ss_q = Query(format=operation_date_dict[\'format\'],\n
+               type=operation_date_dict[\'type\'],\n
+               **{\'delivery.start_date\':operation_date_dict[\'query\']})\n
+  params[\'source_section_where_expression\'] = ss_q()[\'where_expression\']\n
+  ds_q = Query(format=operation_date_dict[\'format\'],\n
+               type=operation_date_dict[\'type\'],\n
+               **{\'delivery.stop_date\':operation_date_dict[\'query\']})\n
+  params[\'destination_section_where_expression\'] = ds_q()[\'where_expression\']\n
+  del params[\'operation_date\']\n
 \n
 result = context.AccountingTransactionModule_zGetAccountingTransactionList(\n
               selection=selection,\n
@@ -128,8 +140,13 @@ return float(\'%.02f\' % (row.total_price and - row.total_price or 0.0))\n
                             <string>context</string>
                             <string>params</string>
                             <string>_write_</string>
+                            <string>Products.ZSQLCatalog.SQLCatalog</string>
+                            <string>Query</string>
                             <string>_getitem_</string>
+                            <string>operation_date_dict</string>
                             <string>_apply_</string>
+                            <string>ss_q</string>
+                            <string>ds_q</string>
                             <string>result</string>
                             <string>row</string>
                             <string>float</string>
diff --git a/bt5/erp5_accounting/SkinTemplateItem/portal_skins/erp5_accounting/AccountingTransactionModule_statSourceDebit.xml b/bt5/erp5_accounting/SkinTemplateItem/portal_skins/erp5_accounting/AccountingTransactionModule_statSourceDebit.xml
index 9c9647f774..a6d7e5ac1f 100644
--- a/bt5/erp5_accounting/SkinTemplateItem/portal_skins/erp5_accounting/AccountingTransactionModule_statSourceDebit.xml
+++ b/bt5/erp5_accounting/SkinTemplateItem/portal_skins/erp5_accounting/AccountingTransactionModule_statSourceDebit.xml
@@ -70,8 +70,20 @@
 params[\'stat\'] = 1\n
 params[\'omit_output\'] = 1\n
 params[\'omit_input\'] = 0\n
-if params.get(\'delivery_start_date\'):\n
-  params[\'delivery.start_date\'] = params[\'delivery_start_date\']\n
+\n
+from Products.ZSQLCatalog.SQLCatalog import Query\n
+\n
+if params.get(\'operation_date\', {}).get(\'query\'):\n
+  operation_date_dict = params[\'operation_date\']\n
+  ss_q = Query(format=operation_date_dict[\'format\'],\n
+               type=operation_date_dict[\'type\'],\n
+               **{\'delivery.start_date\':operation_date_dict[\'query\']})\n
+  params[\'source_section_where_expression\'] = ss_q()[\'where_expression\']\n
+  ds_q = Query(format=operation_date_dict[\'format\'],\n
+               type=operation_date_dict[\'type\'],\n
+               **{\'delivery.stop_date\':operation_date_dict[\'query\']})\n
+  params[\'destination_section_where_expression\'] = ds_q()[\'where_expression\']\n
+  del params[\'operation_date\']\n
 \n
 result = context.AccountingTransactionModule_zGetAccountingTransactionList(\n
                 selection=selection, selection_params = params, **params)\n
@@ -127,8 +139,13 @@ return float(\'%.02f\' % (row.total_price or 0.0))\n
                             <string>context</string>
                             <string>params</string>
                             <string>_write_</string>
+                            <string>Products.ZSQLCatalog.SQLCatalog</string>
+                            <string>Query</string>
                             <string>_getitem_</string>
+                            <string>operation_date_dict</string>
                             <string>_apply_</string>
+                            <string>ss_q</string>
+                            <string>ds_q</string>
                             <string>result</string>
                             <string>row</string>
                             <string>float</string>
diff --git a/bt5/erp5_accounting/SkinTemplateItem/portal_skins/erp5_accounting/AccountingTransactionModule_viewAccountingTransactionList/listbox_credit.xml b/bt5/erp5_accounting/SkinTemplateItem/portal_skins/erp5_accounting/AccountingTransactionModule_viewAccountingTransactionList/listbox_credit.xml
index d685ddb618..c2c81c693e 100644
--- a/bt5/erp5_accounting/SkinTemplateItem/portal_skins/erp5_accounting/AccountingTransactionModule_viewAccountingTransactionList/listbox_credit.xml
+++ b/bt5/erp5_accounting/SkinTemplateItem/portal_skins/erp5_accounting/AccountingTransactionModule_viewAccountingTransactionList/listbox_credit.xml
@@ -9,12 +9,6 @@
     </pickle>
     <pickle>
       <dictionary>
-        <item>
-            <key> <string>_owner</string> </key>
-            <value>
-              <none/>
-            </value>
-        </item>
         <item>
             <key> <string>id</string> </key>
             <value> <string>listbox_credit</string> </value>
@@ -261,18 +255,15 @@
   <record id="2" aka="AAAAAAAAAAI=">
     <pickle>
       <tuple>
-        <tuple>
-          <string>Products.Formulator.TALESField</string>
-          <string>TALESMethod</string>
-        </tuple>
-        <none/>
+        <global name="TALESMethod" module="Products.Formulator.TALESField"/>
+        <tuple/>
       </tuple>
     </pickle>
     <pickle>
       <dictionary>
         <item>
             <key> <string>_text</string> </key>
-            <value> <string>cell/AccountingTransactionModule_getSourceCredit</string> </value>
+            <value> <string>cell/credit|cell/AccountingTransactionModule_getSourceCredit</string> </value>
         </item>
       </dictionary>
     </pickle>
@@ -280,11 +271,8 @@
   <record id="3" aka="AAAAAAAAAAM=">
     <pickle>
       <tuple>
-        <tuple>
-          <string>Products.Formulator.TALESField</string>
-          <string>TALESMethod</string>
-        </tuple>
-        <none/>
+        <global name="TALESMethod" module="Products.Formulator.TALESField"/>
+        <tuple/>
       </tuple>
     </pickle>
     <pickle>
diff --git a/bt5/erp5_accounting/SkinTemplateItem/portal_skins/erp5_accounting/AccountingTransactionModule_viewAccountingTransactionList/listbox_debit.xml b/bt5/erp5_accounting/SkinTemplateItem/portal_skins/erp5_accounting/AccountingTransactionModule_viewAccountingTransactionList/listbox_debit.xml
index 69fbef7d01..8ff70bd92e 100644
--- a/bt5/erp5_accounting/SkinTemplateItem/portal_skins/erp5_accounting/AccountingTransactionModule_viewAccountingTransactionList/listbox_debit.xml
+++ b/bt5/erp5_accounting/SkinTemplateItem/portal_skins/erp5_accounting/AccountingTransactionModule_viewAccountingTransactionList/listbox_debit.xml
@@ -9,12 +9,6 @@
     </pickle>
     <pickle>
       <dictionary>
-        <item>
-            <key> <string>_owner</string> </key>
-            <value>
-              <none/>
-            </value>
-        </item>
         <item>
             <key> <string>id</string> </key>
             <value> <string>listbox_debit</string> </value>
@@ -261,18 +255,15 @@
   <record id="2" aka="AAAAAAAAAAI=">
     <pickle>
       <tuple>
-        <tuple>
-          <string>Products.Formulator.TALESField</string>
-          <string>TALESMethod</string>
-        </tuple>
-        <none/>
+        <global name="TALESMethod" module="Products.Formulator.TALESField"/>
+        <tuple/>
       </tuple>
     </pickle>
     <pickle>
       <dictionary>
         <item>
             <key> <string>_text</string> </key>
-            <value> <string>cell/AccountingTransactionModule_getSourceDebit</string> </value>
+            <value> <string>cell/debit|cell/AccountingTransactionModule_getSourceDebit</string> </value>
         </item>
       </dictionary>
     </pickle>
@@ -280,11 +271,8 @@
   <record id="3" aka="AAAAAAAAAAM=">
     <pickle>
       <tuple>
-        <tuple>
-          <string>Products.Formulator.TALESField</string>
-          <string>TALESMethod</string>
-        </tuple>
-        <none/>
+        <global name="TALESMethod" module="Products.Formulator.TALESField"/>
+        <tuple/>
       </tuple>
     </pickle>
     <pickle>
diff --git a/bt5/erp5_accounting/SkinTemplateItem/portal_skins/erp5_accounting/AccountingTransactionModule_viewAccountingTransactionList/listbox_operation_date.xml b/bt5/erp5_accounting/SkinTemplateItem/portal_skins/erp5_accounting/AccountingTransactionModule_viewAccountingTransactionList/listbox_operation_date.xml
index f949733fef..54acc643c4 100644
--- a/bt5/erp5_accounting/SkinTemplateItem/portal_skins/erp5_accounting/AccountingTransactionModule_viewAccountingTransactionList/listbox_operation_date.xml
+++ b/bt5/erp5_accounting/SkinTemplateItem/portal_skins/erp5_accounting/AccountingTransactionModule_viewAccountingTransactionList/listbox_operation_date.xml
@@ -9,12 +9,6 @@
     </pickle>
     <pickle>
       <dictionary>
-        <item>
-            <key> <string>_owner</string> </key>
-            <value>
-              <none/>
-            </value>
-        </item>
         <item>
             <key> <string>id</string> </key>
             <value> <string>listbox_operation_date</string> </value>
diff --git a/bt5/erp5_accounting/SkinTemplateItem/portal_skins/erp5_accounting/AccountingTransactionModule_zGetAccountingTransactionList.xml b/bt5/erp5_accounting/SkinTemplateItem/portal_skins/erp5_accounting/AccountingTransactionModule_zGetAccountingTransactionList.xml
index 1f0729307b..0b78930b33 100644
--- a/bt5/erp5_accounting/SkinTemplateItem/portal_skins/erp5_accounting/AccountingTransactionModule_zGetAccountingTransactionList.xml
+++ b/bt5/erp5_accounting/SkinTemplateItem/portal_skins/erp5_accounting/AccountingTransactionModule_zGetAccountingTransactionList.xml
@@ -125,6 +125,12 @@
                                   </dictionary>
                                 </value>
                             </item>
+                            <item>
+                                <key> <string>destination_section_where_expression</string> </key>
+                                <value>
+                                  <dictionary/>
+                                </value>
+                            </item>
                             <item>
                                 <key> <string>entity</string> </key>
                                 <value>
@@ -265,6 +271,12 @@
                                   <dictionary/>
                                 </value>
                             </item>
+                            <item>
+                                <key> <string>source_section_where_expression</string> </key>
+                                <value>
+                                  <dictionary/>
+                                </value>
+                            </item>
                             <item>
                                 <key> <string>specific_reference</string> </key>
                                 <value>
@@ -338,6 +350,8 @@
 <string>no_limit</string>
 <string>search_result_keys</string>
 <string>delivery_mirror_section_title</string>
+<string>source_section_where_expression</string>
+<string>destination_section_where_expression</string>
                           </list>
                         </value>
                     </item>
@@ -380,7 +394,9 @@ transaction_uid=""\r\n
 specific_reference\r\n
 no_limit\r\n
 search_result_keys=\'\'\r\n
-delivery_mirror_section_title=""</string> </value>
+delivery_mirror_section_title=""\r\n
+source_section_where_expression\r\n
+destination_section_where_expression</string> </value>
         </item>
         <item>
             <key> <string>cache_time_</string> </key>
@@ -618,10 +634,14 @@ delivery_mirror_section_title=""</string> </value>
         <dtml-elif count>\n
           SELECT COUNT(DISTINCT uid) AS count FROM (\n
         <dtml-else>\n
-          SELECT * from (\n
+          SELECT catalog.*,\n
+          <dtml-let accounting_movement_list="portal_url.getPortalObject().getPortalAccountingMovementTypeList()">\n
+          ( SELECT SUM(stock.total_price) FROM stock, catalog AS child WHERE child.uid = stock.uid AND child.parent_uid = catalog.uid AND stock.total_price > 0 AND <dtml-sqltest accounting_movement_list type=string column=child.portal_type op=eq multiple>) AS debit,\n
+          ( SELECT -SUM(stock.total_price) FROM stock, catalog AS child WHERE child.uid = stock.uid AND child.parent_uid = catalog.uid AND stock.total_price < 0 AND <dtml-sqltest accounting_movement_list type=string column=child.portal_type op=eq multiple>) AS credit\n
+          FROM (\n
+          </dtml-let>\n
         </dtml-if>\n
 \n
-\n
 <dtml-comment>\n
          ##################\n
          #                #\n
@@ -681,6 +701,9 @@ delivery_mirror_section_title=""</string> </value>
           <dtml-if "query[\'where_expression\']">\n
             AND <dtml-var "query[\'where_expression\']">\n
           </dtml-if>\n
+          <dtml-if source_section_where_expression>\n
+            AND <dtml-var source_section_where_expression>\n
+          </dtml-if>\n
           <dtml-if selection_domain>\n
             AND <dtml-var "portal_selections.buildSQLExpressionFromDomainSelection(selection_domain)">\n
           </dtml-if>\n
@@ -860,6 +883,9 @@ UNION\n
           <dtml-if "query[\'where_expression\']">\n
             AND <dtml-var "query[\'where_expression\']">\n
           </dtml-if>\n
+          <dtml-if destination_section_where_expression>\n
+            AND <dtml-var destination_section_where_expression>\n
+          </dtml-if>\n
           <dtml-if selection_domain>\n
             AND <dtml-var "portal_selections.buildSQLExpressionFromDomainSelection(selection_domain)">\n
           </dtml-if>\n
@@ -1250,10 +1276,14 @@ UNION\n
         <dtml-elif count>\n
           SELECT COUNT(DISTINCT uid) AS count FROM (\n
         <dtml-else>\n
-          SELECT * from (\n
+          SELECT catalog.*,\n
+          <dtml-let accounting_movement_list="portal_url.getPortalObject().getPortalAccountingMovementTypeList()">\n
+          ( SELECT SUM(stock.total_price) FROM stock, catalog AS child WHERE child.uid = stock.uid AND child.parent_uid = catalog.uid AND stock.total_price > 0 AND <dtml-sqltest accounting_movement_list type=string column=child.portal_type op=eq multiple>) AS debit,\n
+          ( SELECT -SUM(stock.total_price) FROM stock, catalog AS child WHERE child.uid = stock.uid AND child.parent_uid = catalog.uid AND stock.total_price < 0 AND <dtml-sqltest accounting_movement_list type=string column=child.portal_type op=eq multiple>) AS credit\n
+          FROM (\n
+          </dtml-let>\n
         </dtml-if>\n
 \n
-\n
 <dtml-comment>\n
          ##################\n
          #                #\n
@@ -1313,6 +1343,9 @@ UNION\n
           <dtml-if "query[\'where_expression\']">\n
             AND <dtml-var "query[\'where_expression\']">\n
           </dtml-if>\n
+          <dtml-if source_section_where_expression>\n
+            AND <dtml-var source_section_where_expression>\n
+          </dtml-if>\n
           <dtml-if selection_domain>\n
             AND <dtml-var "portal_selections.buildSQLExpressionFromDomainSelection(selection_domain)">\n
           </dtml-if>\n
@@ -1492,6 +1525,9 @@ UNION\n
           <dtml-if "query[\'where_expression\']">\n
             AND <dtml-var "query[\'where_expression\']">\n
           </dtml-if>\n
+          <dtml-if destination_section_where_expression>\n
+            AND <dtml-var destination_section_where_expression>\n
+          </dtml-if>\n
           <dtml-if selection_domain>\n
             AND <dtml-var "portal_selections.buildSQLExpressionFromDomainSelection(selection_domain)">\n
           </dtml-if>\n
diff --git a/bt5/erp5_accounting/bt/revision b/bt5/erp5_accounting/bt/revision
index 5ae48373ad..ec28ec146d 100644
--- a/bt5/erp5_accounting/bt/revision
+++ b/bt5/erp5_accounting/bt/revision
@@ -1 +1 @@
-585
\ No newline at end of file
+588
\ No newline at end of file
-- 
2.30.9