From c564c658c65356d1f85abc5376c13c6da1e834e4 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?J=C3=A9rome=20Perrin?= <jerome@nexedi.com>
Date: Fri, 2 Dec 2016 03:09:03 +0000
Subject: [PATCH] accounting: fix "Total Selected Amount" not being updated
 after searching in grouping reference fast input

---
 ...ngReferenceFastInputTotalSelectedAmount.py |  21 +++
 ...gReferenceFastInputTotalSelectedAmount.xml |  62 +++++++
 .../your_total_selected_amount.xml            |  12 +-
 ...ted_amount_updated_when_searching.html.xml |  58 +++++++
 ...ted_amount_updated_when_searching.html.zpt | 164 ++++++++++++++++++
 5 files changed, 315 insertions(+), 2 deletions(-)
 create mode 100644 bt5/erp5_accounting/SkinTemplateItem/portal_skins/erp5_accounting/AccountingTransactionModule_getGroupingReferenceFastInputTotalSelectedAmount.py
 create mode 100644 bt5/erp5_accounting/SkinTemplateItem/portal_skins/erp5_accounting/AccountingTransactionModule_getGroupingReferenceFastInputTotalSelectedAmount.xml
 create mode 100644 bt5/erp5_accounting_ui_test/PathTemplateItem/portal_tests/accounting_zuite/test_grouping_reference/test_group_selected_amount_updated_when_searching.html.xml
 create mode 100644 bt5/erp5_accounting_ui_test/PathTemplateItem/portal_tests/accounting_zuite/test_grouping_reference/test_group_selected_amount_updated_when_searching.html.zpt

diff --git a/bt5/erp5_accounting/SkinTemplateItem/portal_skins/erp5_accounting/AccountingTransactionModule_getGroupingReferenceFastInputTotalSelectedAmount.py b/bt5/erp5_accounting/SkinTemplateItem/portal_skins/erp5_accounting/AccountingTransactionModule_getGroupingReferenceFastInputTotalSelectedAmount.py
new file mode 100644
index 0000000000..352c93e2e6
--- /dev/null
+++ b/bt5/erp5_accounting/SkinTemplateItem/portal_skins/erp5_accounting/AccountingTransactionModule_getGroupingReferenceFastInputTotalSelectedAmount.py
@@ -0,0 +1,21 @@
+"""Selected amount in grouping fastinput is set in request by the update scripts,
+but this update script will not be called when searching the listbox.
+
+In that case, we can still calculcate.
+"""
+portal = context.getPortalObject()
+selection_name = \
+  context.AccountingTransactionModule_viewGroupingFastInputDialog.listbox.get_value('selection_name')
+
+getobject = portal.portal_catalog.getobject
+
+selected_uid_list = portal.portal_selections.getSelectionCheckedUidsFor(selection_name)
+total_selected_amount = 0
+# calculate total selected amount
+for uid in selected_uid_list or []:
+  line = getobject(uid)
+  if line.AccountingTransaction_isSourceView():
+    total_selected_amount += (line.getSourceInventoriatedTotalAssetPrice() or 0)
+  else:
+    total_selected_amount += (line.getDestinationInventoriatedTotalAssetPrice() or 0)
+return total_selected_amount
diff --git a/bt5/erp5_accounting/SkinTemplateItem/portal_skins/erp5_accounting/AccountingTransactionModule_getGroupingReferenceFastInputTotalSelectedAmount.xml b/bt5/erp5_accounting/SkinTemplateItem/portal_skins/erp5_accounting/AccountingTransactionModule_getGroupingReferenceFastInputTotalSelectedAmount.xml
new file mode 100644
index 0000000000..a99602bc2e
--- /dev/null
+++ b/bt5/erp5_accounting/SkinTemplateItem/portal_skins/erp5_accounting/AccountingTransactionModule_getGroupingReferenceFastInputTotalSelectedAmount.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></string> </value>
+        </item>
+        <item>
+            <key> <string>id</string> </key>
+            <value> <string>AccountingTransactionModule_getGroupingReferenceFastInputTotalSelectedAmount</string> </value>
+        </item>
+      </dictionary>
+    </pickle>
+  </record>
+</ZopeData>
diff --git a/bt5/erp5_accounting/SkinTemplateItem/portal_skins/erp5_accounting/AccountingTransactionModule_viewGroupingFastInputDialog/your_total_selected_amount.xml b/bt5/erp5_accounting/SkinTemplateItem/portal_skins/erp5_accounting/AccountingTransactionModule_viewGroupingFastInputDialog/your_total_selected_amount.xml
index 4133a4c74f..c3e706c7c2 100644
--- a/bt5/erp5_accounting/SkinTemplateItem/portal_skins/erp5_accounting/AccountingTransactionModule_viewGroupingFastInputDialog/your_total_selected_amount.xml
+++ b/bt5/erp5_accounting/SkinTemplateItem/portal_skins/erp5_accounting/AccountingTransactionModule_viewGroupingFastInputDialog/your_total_selected_amount.xml
@@ -10,9 +10,9 @@
             <key> <string>delegated_list</string> </key>
             <value>
               <list>
-                <string>title</string>
                 <string>default</string>
                 <string>editable</string>
+                <string>title</string>
               </list>
             </value>
         </item>
@@ -60,6 +60,10 @@
                       <persistent> <string encoding="base64">AAAAAAAAAAI=</string> </persistent>
                     </value>
                 </item>
+                <item>
+                    <key> <string>editable</string> </key>
+                    <value> <string></string> </value>
+                </item>
                 <item>
                     <key> <string>field_id</string> </key>
                     <value> <string></string> </value>
@@ -72,6 +76,10 @@
                     <key> <string>target</string> </key>
                     <value> <string></string> </value>
                 </item>
+                <item>
+                    <key> <string>title</string> </key>
+                    <value> <string></string> </value>
+                </item>
               </dictionary>
             </value>
         </item>
@@ -117,7 +125,7 @@
       <dictionary>
         <item>
             <key> <string>_text</string> </key>
-            <value> <string>request/total_selected_amount | python: 0</string> </value>
+            <value> <string>request/total_selected_amount | context/AccountingTransactionModule_getGroupingReferenceFastInputTotalSelectedAmount</string> </value>
         </item>
       </dictionary>
     </pickle>
diff --git a/bt5/erp5_accounting_ui_test/PathTemplateItem/portal_tests/accounting_zuite/test_grouping_reference/test_group_selected_amount_updated_when_searching.html.xml b/bt5/erp5_accounting_ui_test/PathTemplateItem/portal_tests/accounting_zuite/test_grouping_reference/test_group_selected_amount_updated_when_searching.html.xml
new file mode 100644
index 0000000000..47a33ae99c
--- /dev/null
+++ b/bt5/erp5_accounting_ui_test/PathTemplateItem/portal_tests/accounting_zuite/test_grouping_reference/test_group_selected_amount_updated_when_searching.html.xml
@@ -0,0 +1,58 @@
+<?xml version="1.0"?>
+<ZopeData>
+  <record id="1" aka="AAAAAAAAAAE=">
+    <pickle>
+      <global name="ZopePageTemplate" module="Products.PageTemplates.ZopePageTemplate"/>
+    </pickle>
+    <pickle>
+      <dictionary>
+        <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_subpath</string> </key>
+                                <value> <string>traverse_subpath</string> </value>
+                            </item>
+                          </dictionary>
+                        </value>
+                    </item>
+                  </dictionary>
+                </state>
+              </object>
+            </value>
+        </item>
+        <item>
+            <key> <string>content_type</string> </key>
+            <value> <string>text/html</string> </value>
+        </item>
+        <item>
+            <key> <string>expand</string> </key>
+            <value> <int>0</int> </value>
+        </item>
+        <item>
+            <key> <string>id</string> </key>
+            <value> <string>test_group_selected_amount_updated_when_searching.html</string> </value>
+        </item>
+        <item>
+            <key> <string>output_encoding</string> </key>
+            <value> <string>utf-8</string> </value>
+        </item>
+        <item>
+            <key> <string>title</string> </key>
+            <value> <unicode></unicode> </value>
+        </item>
+      </dictionary>
+    </pickle>
+  </record>
+</ZopeData>
diff --git a/bt5/erp5_accounting_ui_test/PathTemplateItem/portal_tests/accounting_zuite/test_grouping_reference/test_group_selected_amount_updated_when_searching.html.zpt b/bt5/erp5_accounting_ui_test/PathTemplateItem/portal_tests/accounting_zuite/test_grouping_reference/test_group_selected_amount_updated_when_searching.html.zpt
new file mode 100644
index 0000000000..33274f85ca
--- /dev/null
+++ b/bt5/erp5_accounting_ui_test/PathTemplateItem/portal_tests/accounting_zuite/test_grouping_reference/test_group_selected_amount_updated_when_searching.html.zpt
@@ -0,0 +1,164 @@
+<html>
+<head><title>Selected amount updated when searching</title></head>
+<body>
+<table cellpadding="1" cellspacing="1" border="1">
+<thead>
+<tr><td rowspan="1" colspan="4">
+Selected amount updated when searching
+</td></tr>
+</thead><tbody tal:define="init_method string:AccountingZuite_createAccountingTransactionList?add_draft_transactions:int=0&month_count:int=6&add_related_payments:int=1">
+
+<!-- Setup {{{ -->
+<tal:block metal:use-macro="here/AccountingZuite_CommonTemplate/macros/init"/>
+<tr>
+  <td>selectAndWait</td>
+  <td>select_module</td>
+  <td>Organisations</td>
+</tr>
+<tr>
+  <td>clickAndWait</td>
+  <td>Folder_show:method</td>
+  <td></td>
+</tr>
+<tr>
+  <td>type</td>
+  <!-- title -->
+  <td>//tr[@class='listbox-search-line']/th[2]/input</td>
+  <td>Client 1</td>
+</tr>
+<tr>
+  <td>clickAndWait</td>
+  <td>//input[@class="listbox-select-action"]</td>
+  <td></td>
+</tr>
+<!-- }}} -->
+
+<!-- {{{ Select Client 1 Organisation and use grouping fast input -->
+<tr>
+  <td>clickAndWait</td>
+  <td>link=Client 1</td>
+  <td></td>
+</tr>
+<tr>
+  <td>clickAndWait</td>
+  <td>//img[@alt='Grouping Reference Fast Input']</td>
+  <td></td>
+</tr>
+
+<!-- The mode should be 'grouping' by default -->
+<tr>
+  <td>verifyValue</td>
+  <td>//input[@name='field_your_grouping' and @value='grouping']</td>
+  <td>on</td>
+</tr>
+<tr>
+  <td>verifyText</td>
+  <td>//label[contains(text(),"Total Selected Amount")]/../div[1]/</td>
+  <td>0.00</td>
+</tr>
+<tr>
+  <td>verifySelectedLabel</td>
+  <td>field_your_node</td>
+  <td></td>
+</tr>
+<tr>
+  <td>select</td>
+  <td>field_your_node</td>
+  <td>label=41 - Receivable</td>
+</tr>
+<tr>
+  <td>clickAndWait</td>
+  <td>//button[@id="dialog_update_button"]</td>
+  <td></td>
+</tr>
+<tr>
+  <td>assertPortalStatusMessage</td>
+  <td>Updated</td>
+  <td></td>
+</tr>
+
+<tr>
+  <td>assertText</td>
+  <td>//span[@class="listbox-current-page-total-number"]</td>
+  <td>12 records</td>
+</tr>
+
+<!-- select one line -->
+<tr>
+  <td>click</td>
+  <td>//tr[@class='listbox-data-line-0 DataA']/td[1]/input</td>
+  <td></td>
+</tr>
+<tr>
+  <td>clickAndWait</td>
+  <td>//button[@id="dialog_update_button"]</td>
+  <td></td>
+</tr>
+<tr>
+  <td>verifyText</td>
+  <td>//span[@class='listbox-current-item-number']</td>
+  <td>- 1 items selected</td>
+</tr>
+<tr>
+  <td>verifyText</td>
+  <td>//label[contains(text(),"Total Selected Amount")]/../div[1]/</td>
+  <td>2 200.00</td>
+</tr>
+
+
+<!-- search -->
+<tr>
+  <td>type</td>
+  <td>//tr[@class='listbox-search-line']/th[5]/input</td>
+  <td></td>
+</tr>
+<tr>
+  <td>type</td>
+  <!-- date -->
+  <td>//tr[@class='listbox-search-line']/th[6]/input</td>
+  <td>2005/01/02</td>
+</tr>
+<tr>
+  <td>clickAndWait</td>
+  <td>//input[@class="listbox-select-action"]</td>
+  <td></td>
+</tr>
+<tr>
+  <td>verifyText</td>
+  <td>//span[@class="listbox-current-page-total-number"]</td>
+  <td>2 records</td>
+</tr>
+
+<!-- The selected amount is still displayed properly directly after search -->
+<tr>
+  <td>verifyText</td>
+  <td>//span[@class='listbox-current-item-number']</td>
+  <td>- 1 items selected</td>
+</tr>
+<tr>
+  <td>verifyText</td>
+  <td>//label[contains(text(),"Total Selected Amount")]/../div[1]/</td>
+  <td>2 200.00</td>
+</tr>
+
+<!-- Click "update" button, the selected amount is still displayed properly -->
+<tr>
+  <td>clickAndWait</td>
+  <td>//button[@id="dialog_update_button"]</td>
+  <td></td>
+</tr>
+<tr>
+  <td>verifyText</td>
+  <td>//span[@class='listbox-current-item-number']</td>
+  <td>- 1 items selected</td>
+</tr>
+<tr>
+  <td>verifyText</td>
+  <td>//label[contains(text(),"Total Selected Amount")]/../div[1]/</td>
+  <td>2 200.00</td>
+</tr>
+
+</tbody></table>
+</body>
+</html> <!-- vim: syntax=html foldmethod=marker
+-->
\ No newline at end of file
-- 
2.30.9