From 85350657a70096920131fe655c515a86472199f9 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?J=C3=A9rome=20Perrin?= <jerome@nexedi.com>
Date: Wed, 28 Oct 2020 10:46:50 +0100
Subject: [PATCH] trade: test Inventory Valuation in Stock Report

This change to unconditionally create products, to make it more clear
that it's OK for test methods to change the products configuration because
they are reset at the beginning of each tests (they have always been removed
during tear down)
---
 .../test.erp5.testTradeReports.py             | 129 ++++++++++++++----
 1 file changed, 100 insertions(+), 29 deletions(-)

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 01dc69fd65..e1197a5381 100644
--- a/bt5/erp5_trade/TestTemplateItem/portal_components/test.erp5.testTradeReports.py
+++ b/bt5/erp5_trade/TestTemplateItem/portal_components/test.erp5.testTradeReports.py
@@ -170,35 +170,31 @@ class TestTradeReports(ERP5ReportTestCase):
       self.tic()
 
     # Create resources
-    module = self.portal.product_module
-    if not module.has_key('product_B'):
-      module.newContent(
-          portal_type='Product',
-          id='product_B',
-          title='product_B',
-          reference='ref 1',
-          quantity_unit='mass/kg'
-          )
-    if not module.has_key('product_A'):
-      module.newContent(
-          portal_type='Product',
-          id='product_A',
-          title='product_A',
-          reference='ref 2',
-          quantity_unit_list=('mass/g', 'mass/kg'),
-          default_purchase_supply_line_base_price=3,
-          default_internal_supply_line_base_price=5,
-          default_sale_supply_line_base_price=7,
-          )
-    if not module.has_key('product_C'):
-      module.newContent(
-          portal_type='Product',
-          id='product_C',
-          title='variated product',
-          reference='ref 3',
-          variation_base_category_list=['colour'],
-          colour_list=['colour1', 'colour2'],
-          )
+    self.portal.product_module.newContent(
+        portal_type='Product',
+        id='product_A',
+        title='product_A',
+        reference='ref 2',
+        quantity_unit_list=('mass/g', 'mass/kg'),
+        default_purchase_supply_line_base_price=3,
+        default_internal_supply_line_base_price=5,
+        default_sale_supply_line_base_price=7,
+    )
+    self.portal.product_module.newContent(
+        portal_type='Product',
+        id='product_B',
+        title='product_B',
+        reference='ref 1',
+        quantity_unit='mass/kg'
+    )
+    self.portal.product_module.newContent(
+        portal_type='Product',
+        id='product_C',
+        title='variated product',
+        reference='ref 3',
+        variation_base_category_list=['colour'],
+        colour_list=['colour1', 'colour2'],
+    )
     if not self.portal.service_module.has_key('service_a'):
       self.portal.service_module.newContent(
           portal_type='Service',
@@ -1546,6 +1542,81 @@ class TestTradeReports(ERP5ReportTestCase):
         total_price=7,
     )
 
+  def testStockReport_valuation_and_quantity_unit_conversion(self):
+    self._createConfirmedSalePackingListForStockReportTest(
+        quantity=0.5,
+        quantity_unit_value=self.portal.portal_categories.quantity_unit.mass.kg,
+    )
+    request = self.portal.REQUEST
+    request.form['at_date'] = DateTime(2007, 3, 3)
+    request.form['node_category'] = 'site/demo_site_A'
+    request.form['simulation_period'] = 'future'
+    request.form['inventory_valuation_method'] = 'default_purchase_price'
+
+    line_list = self.portal.inventory_module.Base_viewStockReportBySite.listbox.\
+        get_value('default',
+                  render_format='list', REQUEST=self.portal.REQUEST)
+
+    data_line_list = [l for l in line_list if l.isDataLine()]
+    self.assertEqual(1, len(data_line_list))
+    data_line = data_line_list[0]
+    self.assertEqual(
+        data_line.column_id_list,
+        ['resource_title', 'resource_reference', 'variation_category_item_list', 'inventory', 'quantity_unit', 'total_price'])
+
+    self.checkLineProperties(
+        data_line_list[0],
+        resource_title='product_A',
+        resource_reference='ref 2',
+        variation_category_item_list=[],
+        inventory=500,
+        quantity_unit='G',
+        # price for 1g is 3, we have 0.5Kg=500g
+        total_price=1500,
+    )
+    # listbox_total_price is an editable field using this for precision
+    self.assertEqual(self.portal.REQUEST.get('precision'), 2)
+
+  def testStockReport_valuation_and_quantity_price_in_different_unit(self):
+    # edit the product to set a default purchase price of 3300 per Kg, price
+    # for one gram will be 3.3
+    purchase_supply_line = self.portal.product_module.product_A.getDefaultPurchaseSupplyLineValue()
+    purchase_supply_line.edit(
+        quantity_unit_value=self.portal.portal_categories.quantity_unit.mass.kg,
+        base_price=3300
+    )
+    self._createConfirmedSalePackingListForStockReportTest()
+
+    request = self.portal.REQUEST
+    request.form['at_date'] = DateTime(2007, 3, 3)
+    request.form['node_category'] = 'site/demo_site_A'
+    request.form['simulation_period'] = 'future'
+    request.form['inventory_valuation_method'] = 'default_purchase_price'
+
+    line_list = self.portal.inventory_module.Base_viewStockReportBySite.listbox.\
+        get_value('default',
+                  render_format='list', REQUEST=self.portal.REQUEST)
+
+    data_line_list = [l for l in line_list if l.isDataLine()]
+    self.assertEqual(1, len(data_line_list))
+    data_line = data_line_list[0]
+    self.assertEqual(
+        data_line.column_id_list,
+        ['resource_title', 'resource_reference', 'variation_category_item_list', 'inventory', 'quantity_unit', 'total_price'])
+
+    self.checkLineProperties(
+        data_line_list[0],
+        resource_title='product_A',
+        resource_reference='ref 2',
+        variation_category_item_list=[],
+        inventory=1,
+        quantity_unit='G',
+        total_price=3.3,
+    )
+    # listbox_total_price is an editable field using this for precision
+    self.assertEqual(self.portal.REQUEST.get('precision'), 2)
+
+
   def test_Folder_generateWorkflowReport(self):
     # Create sales orders
     self._makeOneSaleOrder(
-- 
2.30.9