From ffbe5a5f9b47bdb1b88e3ef2fb2c554faaf6a35b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?J=C3=A9rome=20Perrin?= <jerome@nexedi.com>
Date: Mon, 25 Jan 2021 11:01:39 +0100
Subject: [PATCH] trade: support packing lists in order report

Even though it was enabled on packing list, this report was showing orders.
---
 ...OrderModule_getOrderReportParameterDict.py |   6 +
 .../test.erp5.testTradeReports.py             | 111 +++++++++++++++++-
 2 files changed, 116 insertions(+), 1 deletion(-)

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 7ae8d2200c..20e06bbced 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
@@ -14,10 +14,16 @@ if "Sale" in context.getPortalType():
   report_type = "sale"
   line_portal_type = "Sale Order Line"
   doc_portal_type = "Sale Order"
+  if "Packing List" in context.getPortalType():
+    line_portal_type = "Sale Packing List Line"
+    doc_portal_type = "Sale Packing List"
 elif "Purchase" in context.getPortalType():
   report_type = "purchase"
   line_portal_type = "Purchase Order Line"
   doc_portal_type = "Purchase Order"
+  if "Packing List" in context.getPortalType():
+    line_portal_type = "Purchase Packing List Line"
+    doc_portal_type = "Purchase Packing List"
 elif request.get('order_report_document_portal_type'):
   doc_portal_type = request.get('order_report_document_portal_type')
   if doc_portal_type == 'Purchase Invoice Transaction':
diff --git a/bt5/erp5_trade/TestTemplateItem/portal_components/test.erp5.testTradeReports.py b/bt5/erp5_trade/TestTemplateItem/portal_components/test.erp5.testTradeReports.py
index e1197a5381..ca4aa4934f 100644
--- a/bt5/erp5_trade/TestTemplateItem/portal_components/test.erp5.testTradeReports.py
+++ b/bt5/erp5_trade/TestTemplateItem/portal_components/test.erp5.testTradeReports.py
@@ -267,6 +267,22 @@ class TestTradeReports(ERP5ReportTestCase):
 
     return sale_order
 
+  @reindex
+  def _makeOneSalePackingList(self, resource_dict=None, **kw):
+    """
+    Create a sale packing list
+    """
+    if resource_dict is None:
+      resource_dict = {}
+    sale_packing_list = self.portal.sale_packing_list_module.newContent(portal_type="Sale Packing List", **kw)
+    for product, values in resource_dict.iteritems():
+      sale_packing_list.newContent(
+          portal_type="Sale Packing List Line",
+          resource=product,
+          quantity=values["quantity"],
+          price=values["price"])
+    return sale_packing_list
+
   def _createSaleOrdersForSaleOrderReportTest(self):
     # Create sales orders to be used in testSaleOrderReportXXX tests
     self._makeOneSaleOrder(
@@ -318,6 +334,19 @@ class TestTradeReports(ERP5ReportTestCase):
               cancel=True
               )
 
+    # create also packing lists
+    self._makeOneSalePackingList(
+              title='SPL 1 (same as SO 3, but delivered state)',
+              destination_value=self.organisation_module.Organisation_2,
+              destination_section_value=self.organisation_module.Organisation_2,
+              destination_decision_value=self.organisation_module.Organisation_2,
+              source_value=self.organisation_module.Organisation_1,
+              source_section_value=self.organisation_module.Organisation_1,
+              source_decision_value=self.organisation_module.Organisation_1,
+              start_date=DateTime(2006, 2, 22),
+              resource_dict = {'product_module/product_A':{"quantity":5, "price":3},
+                               'product_module/product_B':{"quantity":1, "price":6},}
+              )
     self.tic()
 
   def testSaleOrderReportBefore2006(self):
@@ -889,8 +918,88 @@ class TestTradeReports(ERP5ReportTestCase):
                  'total quantity': None}
     self.checkLineProperties(stat_line_list[0],**d)
 
+  def testSalePackingListReport(self):
+    """Using Sale Packing Lists, not sales Order
+    """
+    self._createSaleOrdersForSaleOrderReportTest()
+    request = self.portal.REQUEST
+
+    request['from_date'] = DateTime(2006, 1, 1)
+    request['at_date'] = DateTime(2006, 12, 31)
+    request['simulation_state'] = ['draft',]
+    request['aggregation_level'] = "year"
+    request['group_by'] = "both"
+    request['section_category'] = 'group/g1'
+
+    parameter_dict, _, _ = self.portal.sale_packing_list_module.OrderModule_getOrderReportParameterDict()
+    active_process = self.portal.sale_packing_list_module.OrderModule_activateGetOrderStatList(tag="unit_test", **parameter_dict)
+    request['active_process'] = active_process.getPath()
+    self.tic()
+    report_section_list = self.getReportSectionList(self.portal.sale_packing_list_module,
+                                                    'OrderModule_viewOrderReport')
+    self.assertEqual(1, len(report_section_list))
+
+    line_list = self.getListBoxLineList(report_section_list[0])
+    data_line_list = [l for l in line_list if l.isDataLine()]
+    stat_line_list = [l for l in line_list if l.isStatLine()]
+    self.assertEqual(3, len(data_line_list))
+
+    # test columns values
+    line = data_line_list[0]
+    self.assertEqual(line.column_id_list, ['client',
+                    'product',
+                    'Amount 2006',
+                    'Quantity 2006',
+                    'Quantity Unit 2006',
+                    'total amount',
+                    'total quantity'])
+
+    d = {
+        'Amount 2006': 21.0,
+        'Quantity 2006': None,
+        'Quantity Unit 2006': None,
+        'client': 'Organisation_2',
+        'product': None,
+        'total amount': 21.0,
+        'total quantity': None,
+    }
+    self.checkLineProperties(data_line_list[0],**d)
+
+    d = {
+      'Amount 2006': 15.0,
+      'Quantity 2006': 5.0,
+      'Quantity Unit 2006': 'G',
+      'client': None,
+      'product': 'product_A',
+      'total amount': 15.0,
+      'total quantity': 5.0,
+    }
+    self.checkLineProperties(data_line_list[1],**d)
+
+    d = {
+        'Amount 2006': 6.0,
+        'Quantity 2006': 1.0,
+        'Quantity Unit 2006': 'Kg',
+        'client': None,
+        'product': 'product_B',
+        'total amount': 6.0,
+        'total quantity': 1.0,
+    }
+    self.checkLineProperties(data_line_list[2],**d)
+
+    # stat line
+    d = {
+      'Amount 2006': 21.0,
+      'Quantity 2006': None,
+      'Quantity Unit 2006': None,
+      'client': 'Total',
+      'product': None,
+      'total amount': 21.0,
+      'total quantity': None,
+    }
+    self.checkLineProperties(stat_line_list[0],**d)
+
   def _createInventoryForStockReportTest(self):
-    # Create inventories
     # Create inventories
     self._makeOneInventory(
               title='Inventory 1',
-- 
2.30.9