From 3e3958605db142ca4eac9938180ec01fc4b881d5 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Klaus=20W=C3=B6lfel?= <klaus@nexedi.com>
Date: Tue, 27 Jan 2015 03:30:38 +0000
Subject: [PATCH] erp5_trade: Order Report improvements

- Optionally group by function
- Give higher priority to Order Report compared to Workflow Report
- Optionally filter by Quantity Unit and enable Stat Columns for Quantity

TODO: Quantity also for Thirs Party if filtered by quantity Unit
---
 .../Sale%20Order%20Module/workflow_report.xml |   2 +-
 ...OrderModule_getOrderReportParameterDict.py |  17 ++-
 .../OrderModule_getOrderReportSectionList.py  |   4 -
 .../OrderModule_getOrderStatList.py           |  40 ++++---
 .../OrderModule_viewOrderReport.xml           |   1 +
 .../your_quantity_unit.xml                    |  96 +++++++++++++++++
 .../OrderModule_viewOrderReportDialog.xml     |   1 +
 .../your_quantity_unit.xml                    | 100 ++++++++++++++++++
 8 files changed, 232 insertions(+), 29 deletions(-)
 create mode 100644 bt5/erp5_trade/SkinTemplateItem/portal_skins/erp5_trade/OrderModule_viewOrderReport/your_quantity_unit.xml
 create mode 100644 bt5/erp5_trade/SkinTemplateItem/portal_skins/erp5_trade/OrderModule_viewOrderReportDialog/your_quantity_unit.xml

diff --git a/bt5/erp5_trade/ActionTemplateItem/portal_types/Sale%20Order%20Module/workflow_report.xml b/bt5/erp5_trade/ActionTemplateItem/portal_types/Sale%20Order%20Module/workflow_report.xml
index fc0d00c1b0..8a89fe6b80 100644
--- a/bt5/erp5_trade/ActionTemplateItem/portal_types/Sale%20Order%20Module/workflow_report.xml
+++ b/bt5/erp5_trade/ActionTemplateItem/portal_types/Sale%20Order%20Module/workflow_report.xml
@@ -52,7 +52,7 @@
         </item>
         <item>
             <key> <string>priority</string> </key>
-            <value> <float>8.0</float> </value>
+            <value> <float>12.0</float> </value>
         </item>
         <item>
             <key> <string>title</string> </key>
diff --git a/bt5/erp5_trade/SkinTemplateItem/portal_skins/erp5_trade/OrderModule_getOrderReportParameterDict.py b/bt5/erp5_trade/SkinTemplateItem/portal_skins/erp5_trade/OrderModule_getOrderReportParameterDict.py
index b492f6d1b7..e4c8641344 100644
--- a/bt5/erp5_trade/SkinTemplateItem/portal_skins/erp5_trade/OrderModule_getOrderReportParameterDict.py
+++ b/bt5/erp5_trade/SkinTemplateItem/portal_skins/erp5_trade/OrderModule_getOrderReportParameterDict.py
@@ -1,14 +1,13 @@
 from Products.ERP5Type.DateUtils import getIntervalListBetweenDates
 from DateTime import DateTime
-
 request = container.REQUEST
 # list only if user has a login defined
 aggregation_level = request.get('aggregation_level')
 from_date = request.get('from_date')
 to_date = request.get('at_date')
 group_by = request.get('group_by')
+quantity_unit = request.get('quantity_unit')
 simulation_state = request.get('simulation_state', ())
-
 # define some parameter dependings on module
 if "Sale" in context.getPortalType():
   report_type = "sale"
@@ -30,7 +29,6 @@ elif request.get('order_report_document_portal_type'):
     raise ValueError("unknown document portal type for report %s" % doc_portal_type)
 else:
   raise ValueError("unknown type for report")
-
 selection_columns = [('group_by', "Group by")]
 if from_date is None:
   # get the minimum start date in catalog
@@ -46,7 +44,6 @@ if from_date is None:
                                      limit=1)
   if result_list:
     from_date = DateTime(result_list[0].start_date)
-
  
 # get period list between given date
 interval_list_dict = getIntervalListBetweenDates(from_date=from_date, to_date=to_date,
@@ -55,7 +52,6 @@ interval_list_dict = getIntervalListBetweenDates(from_date=from_date, to_date=to
                                                   'week' : aggregation_level=="week",
                                                   'day':aggregation_level=="day"})
 interval_list = interval_list_dict[aggregation_level]
-
 # FIXME: translate column names
 # list columns of the listbox
 interval_column_list = []
@@ -69,18 +65,21 @@ else:
   if group_by == "product":
     selection_columns = [('product', "Product")]
     stat_columns = [('product', "product")]
+  elif group_by == "function":
+    function_title = context.AccountingTransactionLine_getFunctionBaseCategoryTitle()
+    selection_columns = [('product', function_title)]
+    stat_columns = [('product', "product")]
   else:
     selection_columns = [('client', "Client"), ('product', "Product")]
     stat_columns = [('client', "client"), ('product', "product")]
   for x in interval_list:
-    interval_column_list.extend([("Amount %s" %x,"Amount %s" %x), ("Quantity %s" %x,"Quantity %s" %x),
-                                 ("Quantity Unit %s" %x,"Quantity Unit %s" %x)])
+    interval_column_list.extend([("Amount %s" %x,"Amount %s" %x), ("Quantity %s" %x,"Quantity %s" %x)])
+    if not quantity_unit:
+      interval_column_list.extend([("Quantity Unit %s" %x,"Quantity Unit %s" %x)])
   total_column_list = [('total amount', 'Total Amount'),('total quantity', 'Total Quantity')]
   total_stat_list = [('total amount', 'total amount'),('total quantity', 'total quantity')]
-
 selection_columns.extend(interval_column_list)
 selection_columns.extend(total_column_list)
-
 params=dict(period_list=interval_list, report_type=report_type,
             doc_portal_type=doc_portal_type, line_portal_type=line_portal_type,
             simulation_state=simulation_state)
diff --git a/bt5/erp5_trade/SkinTemplateItem/portal_skins/erp5_trade/OrderModule_getOrderReportSectionList.py b/bt5/erp5_trade/SkinTemplateItem/portal_skins/erp5_trade/OrderModule_getOrderReportSectionList.py
index ef3227185d..db611a4060 100644
--- a/bt5/erp5_trade/SkinTemplateItem/portal_skins/erp5_trade/OrderModule_getOrderReportSectionList.py
+++ b/bt5/erp5_trade/SkinTemplateItem/portal_skins/erp5_trade/OrderModule_getOrderReportSectionList.py
@@ -1,8 +1,6 @@
 from Products.ERP5Form.Report import ReportSection
 from DateTime import DateTime
-
 params, stat_columns, selection_columns = context.OrderModule_getOrderReportParameterDict()
-
 context.REQUEST.set('stat_columns', stat_columns)
 result=[]
 result.append(ReportSection(
@@ -11,6 +9,4 @@ result.append(ReportSection(
               listbox_display_mode='FlatListMode',
               selection_params=params,
               form_id='OrderModule_viewOrderStatList'))
-
-
 return result
diff --git a/bt5/erp5_trade/SkinTemplateItem/portal_skins/erp5_trade/OrderModule_getOrderStatList.py b/bt5/erp5_trade/SkinTemplateItem/portal_skins/erp5_trade/OrderModule_getOrderStatList.py
index 2650fdd985..d2f25aec9e 100644
--- a/bt5/erp5_trade/SkinTemplateItem/portal_skins/erp5_trade/OrderModule_getOrderStatList.py
+++ b/bt5/erp5_trade/SkinTemplateItem/portal_skins/erp5_trade/OrderModule_getOrderStatList.py
@@ -1,12 +1,10 @@
 from Products.PythonScripts.standard import Object
 from json import loads
-
 portal = context.getPortalObject()
-
 request = container.REQUEST
 report_group_by = request.get('group_by', None)
+quantity_unit = request.get('quantity_unit', None)
 active_process_path = request.get('active_process')
-
 # We have to sum product_dict and client_dict from the results of active process
 def _addDict(global_dict, local_dict, only_amount=False):
   if report_group_by == "both" and not only_amount:
@@ -30,7 +28,6 @@ def _addDict(global_dict, local_dict, only_amount=False):
         if not only_amount:
           amount_dict['quantity'] = amount_dict['quantity'] + local_amount_dict['quantity']
           amount_dict['quantity_unit'] = local_amount_dict['quantity_unit']
-
 product_dict = {}
 client_dict = {}
 if active_process_path:
@@ -55,10 +52,8 @@ if active_process_path:
       _addDict(product_dict, result_product_dict)
 else:
   raise ValueError("No active process found to process report")
-
 def sortProduct(a, b):
   return cmp(a['product'], b['product'])
-
 period_counter_dict = {}
 line_list = []
 append = line_list.append
@@ -89,7 +84,6 @@ if len(client_dict):
         period_counter_dict['total amount'] = period_counter_dict['total amount'] + line_total_amount
       else:
         period_counter_dict['total amount'] = line_total_amount
-
     append(obj)
     if report_group_by == "both":
       product_lines_list = []
@@ -115,12 +109,18 @@ if len(client_dict):
                                                              line_product_dict[product_title][period]['amount']
               else:
                 period_counter_dict['Amount %s' %(period)] = line_product_dict[product_title][period]['amount']
-              
+
+              if quantity_unit:
+                if period_counter_dict.has_key('Quantity %s' %(period)):
+                  period_counter_dict['Quantity %s' %(period)] = period_counter_dict['Quantity %s' %(period)] + \
+                                                               line_product_dict[product_title][period]['quantity']
+                else:
+                  period_counter_dict['Quantity %s' %(period)] = line_product_dict[product_title][period]['quantity']
             else:
               obj['Amount %s' %(period)] = 0
               obj['Quantity %s' %(period)] = 0
               obj['Quantity Unit %s' %(period)] = ""
-          
+
           obj['total quantity'] = line_total_quantity
           obj['total amount'] = round(line_total_amount, 2)
           # total for stat line
@@ -128,6 +128,11 @@ if len(client_dict):
             period_counter_dict['total amount'] = period_counter_dict['total amount'] + line_total_amount
           else:
             period_counter_dict['total amount'] = line_total_amount
+          if quantity_unit:
+            if period_counter_dict.has_key('total quantity'):
+              period_counter_dict['total quantity'] = period_counter_dict['total quantity'] + line_total_quantity
+            else:
+              period_counter_dict['total quantity'] = line_total_quantity
 
           product_lines_list.append(obj)
       # sort product list
@@ -135,7 +140,7 @@ if len(client_dict):
       extend(product_lines_list)
 else:
   # products
-  if report_group_by == "product":
+  if report_group_by in ("product", "function"):
     for product_title in product_dict.keys():
       obj = Object(uid="new_")
       obj['product'] = product_title
@@ -154,11 +159,15 @@ else:
             period_counter_dict['Amount %s' %(period)] = period_counter_dict['Amount %s' %(period)] + product_dict[product_title][period]['amount']
           else:
             period_counter_dict['Amount %s' %(period)] = product_dict[product_title][period]['amount']
+          if quantity_unit:
+            if period_counter_dict.has_key('Quantity %s' %(period)):
+              period_counter_dict['Quantity %s' %(period)] = period_counter_dict['Quantity %s' %(period)] + product_dict[product_title][period]['quantity']
+            else:
+              period_counter_dict['Quantity %s' %(period)] = product_dict[product_title][period]['quantity']
         else:
           obj['Amount %s' %(period)] = 0
           obj['Quantity %s' %(period)] = 0
           obj['Quantity Unit %s' %(period)] = ""
-
       obj['total quantity'] = line_total_quantity
       obj['total amount'] = round(line_total_amount,2)
       # total for stat line
@@ -166,17 +175,18 @@ else:
         period_counter_dict['total amount'] = period_counter_dict['total amount'] + line_total_amount
       else:
         period_counter_dict['total amount'] = line_total_amount
+      if quantity_unit:
+        if period_counter_dict.has_key('total quantity'):
+          period_counter_dict['total quantity'] = period_counter_dict['total quantity'] + line_total_quantity
+        else:
+          period_counter_dict['total quantity'] = line_total_quantity
       append(obj)
-
     line_list.sort(sortProduct)
-
 obj = Object(uid="new_")
 obj["client"] = 'Total'
 for k,v in period_counter_dict.items():
   if "mount" in k:
     v = round(v, 2)
   obj[k] = v
-
 request.set('stat_line', [obj,])
-
 return line_list
diff --git a/bt5/erp5_trade/SkinTemplateItem/portal_skins/erp5_trade/OrderModule_viewOrderReport.xml b/bt5/erp5_trade/SkinTemplateItem/portal_skins/erp5_trade/OrderModule_viewOrderReport.xml
index 5a87e68365..389856fbe2 100644
--- a/bt5/erp5_trade/SkinTemplateItem/portal_skins/erp5_trade/OrderModule_viewOrderReport.xml
+++ b/bt5/erp5_trade/SkinTemplateItem/portal_skins/erp5_trade/OrderModule_viewOrderReport.xml
@@ -94,6 +94,7 @@
                         <string>your_delivery_mode</string>
                         <string>your_from_date</string>
                         <string>your_group_by</string>
+                        <string>your_quantity_unit</string>
                         <string>your_incoterm</string>
                         <string>your_order</string>
                         <string>your_section_category</string>
diff --git a/bt5/erp5_trade/SkinTemplateItem/portal_skins/erp5_trade/OrderModule_viewOrderReport/your_quantity_unit.xml b/bt5/erp5_trade/SkinTemplateItem/portal_skins/erp5_trade/OrderModule_viewOrderReport/your_quantity_unit.xml
new file mode 100644
index 0000000000..1f02638f58
--- /dev/null
+++ b/bt5/erp5_trade/SkinTemplateItem/portal_skins/erp5_trade/OrderModule_viewOrderReport/your_quantity_unit.xml
@@ -0,0 +1,96 @@
+<?xml version="1.0"?>
+<ZopeData>
+  <record id="1" aka="AAAAAAAAAAE=">
+    <pickle>
+      <global name="ProxyField" module="Products.ERP5Form.ProxyField"/>
+    </pickle>
+    <pickle>
+      <dictionary>
+        <item>
+            <key> <string>delegated_list</string> </key>
+            <value>
+              <list>
+                <string>title</string>
+              </list>
+            </value>
+        </item>
+        <item>
+            <key> <string>id</string> </key>
+            <value> <string>your_quantity_unit</string> </value>
+        </item>
+        <item>
+            <key> <string>message_values</string> </key>
+            <value>
+              <dictionary>
+                <item>
+                    <key> <string>external_validator_failed</string> </key>
+                    <value> <string>The input failed the external validator.</string> </value>
+                </item>
+              </dictionary>
+            </value>
+        </item>
+        <item>
+            <key> <string>overrides</string> </key>
+            <value>
+              <dictionary>
+                <item>
+                    <key> <string>field_id</string> </key>
+                    <value> <string></string> </value>
+                </item>
+                <item>
+                    <key> <string>form_id</string> </key>
+                    <value> <string></string> </value>
+                </item>
+                <item>
+                    <key> <string>target</string> </key>
+                    <value> <string></string> </value>
+                </item>
+              </dictionary>
+            </value>
+        </item>
+        <item>
+            <key> <string>tales</string> </key>
+            <value>
+              <dictionary>
+                <item>
+                    <key> <string>field_id</string> </key>
+                    <value> <string></string> </value>
+                </item>
+                <item>
+                    <key> <string>form_id</string> </key>
+                    <value> <string></string> </value>
+                </item>
+                <item>
+                    <key> <string>target</string> </key>
+                    <value> <string></string> </value>
+                </item>
+              </dictionary>
+            </value>
+        </item>
+        <item>
+            <key> <string>values</string> </key>
+            <value>
+              <dictionary>
+                <item>
+                    <key> <string>field_id</string> </key>
+                    <value> <string>my_dialog_mode_category</string> </value>
+                </item>
+                <item>
+                    <key> <string>form_id</string> </key>
+                    <value> <string>Base_viewFieldLibrary</string> </value>
+                </item>
+                <item>
+                    <key> <string>target</string> </key>
+                    <value> <string>Click to edit the target</string> </value>
+                </item>
+                <item>
+                    <key> <string>title</string> </key>
+                    <value> <string>Quantity Unit</string> </value>
+                </item>
+              </dictionary>
+            </value>
+        </item>
+      </dictionary>
+    </pickle>
+  </record>
+</ZopeData>
diff --git a/bt5/erp5_trade/SkinTemplateItem/portal_skins/erp5_trade/OrderModule_viewOrderReportDialog.xml b/bt5/erp5_trade/SkinTemplateItem/portal_skins/erp5_trade/OrderModule_viewOrderReportDialog.xml
index 2d2e4c944c..e8039934b4 100644
--- a/bt5/erp5_trade/SkinTemplateItem/portal_skins/erp5_trade/OrderModule_viewOrderReportDialog.xml
+++ b/bt5/erp5_trade/SkinTemplateItem/portal_skins/erp5_trade/OrderModule_viewOrderReportDialog.xml
@@ -112,6 +112,7 @@
                       <list>
                         <string>your_aggregation_level</string>
                         <string>your_group_by</string>
+                        <string>your_quantity_unit</string>
                         <string>your_simulation_state</string>
                       </list>
                     </value>
diff --git a/bt5/erp5_trade/SkinTemplateItem/portal_skins/erp5_trade/OrderModule_viewOrderReportDialog/your_quantity_unit.xml b/bt5/erp5_trade/SkinTemplateItem/portal_skins/erp5_trade/OrderModule_viewOrderReportDialog/your_quantity_unit.xml
new file mode 100644
index 0000000000..877d749d96
--- /dev/null
+++ b/bt5/erp5_trade/SkinTemplateItem/portal_skins/erp5_trade/OrderModule_viewOrderReportDialog/your_quantity_unit.xml
@@ -0,0 +1,100 @@
+<?xml version="1.0"?>
+<ZopeData>
+  <record id="1" aka="AAAAAAAAAAE=">
+    <pickle>
+      <global name="ProxyField" module="Products.ERP5Form.ProxyField"/>
+    </pickle>
+    <pickle>
+      <dictionary>
+        <item>
+            <key> <string>delegated_list</string> </key>
+            <value>
+              <list>
+                <string>title</string>
+              </list>
+            </value>
+        </item>
+        <item>
+            <key> <string>id</string> </key>
+            <value> <string>your_quantity_unit</string> </value>
+        </item>
+        <item>
+            <key> <string>message_values</string> </key>
+            <value>
+              <dictionary>
+                <item>
+                    <key> <string>external_validator_failed</string> </key>
+                    <value> <string>The input failed the external validator.</string> </value>
+                </item>
+              </dictionary>
+            </value>
+        </item>
+        <item>
+            <key> <string>overrides</string> </key>
+            <value>
+              <dictionary>
+                <item>
+                    <key> <string>field_id</string> </key>
+                    <value> <string></string> </value>
+                </item>
+                <item>
+                    <key> <string>form_id</string> </key>
+                    <value> <string></string> </value>
+                </item>
+                <item>
+                    <key> <string>target</string> </key>
+                    <value> <string></string> </value>
+                </item>
+              </dictionary>
+            </value>
+        </item>
+        <item>
+            <key> <string>tales</string> </key>
+            <value>
+              <dictionary>
+                <item>
+                    <key> <string>field_id</string> </key>
+                    <value> <string></string> </value>
+                </item>
+                <item>
+                    <key> <string>form_id</string> </key>
+                    <value> <string></string> </value>
+                </item>
+                <item>
+                    <key> <string>target</string> </key>
+                    <value> <string></string> </value>
+                </item>
+                <item>
+                    <key> <string>title</string> </key>
+                    <value> <string></string> </value>
+                </item>
+              </dictionary>
+            </value>
+        </item>
+        <item>
+            <key> <string>values</string> </key>
+            <value>
+              <dictionary>
+                <item>
+                    <key> <string>field_id</string> </key>
+                    <value> <string>my_dialog_mode_category</string> </value>
+                </item>
+                <item>
+                    <key> <string>form_id</string> </key>
+                    <value> <string>Base_viewFieldLibrary</string> </value>
+                </item>
+                <item>
+                    <key> <string>target</string> </key>
+                    <value> <string>Click to edit the target</string> </value>
+                </item>
+                <item>
+                    <key> <string>title</string> </key>
+                    <value> <string>Quantity Unit</string> </value>
+                </item>
+              </dictionary>
+            </value>
+        </item>
+      </dictionary>
+    </pickle>
+  </record>
+</ZopeData>
-- 
2.30.9