From 0179a72f4d7acc36df7528b4bd8fb4ebf071f557 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=C5=81ukasz=20Nowak?= <luke@nexedi.com>
Date: Fri, 15 May 2009 12:18:06 +0000
Subject: [PATCH]  - check for Delivery Rule and Invoice Rule simulation impact
 in various simulation states on typical documents related to those rules
 (Sale & Purchase Packing Lists and Invoices)

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@27004 20353a03-c40f-0410-a6d1-a30d3c3de9de
---
 product/ERP5/tests/testInvoice.py     | 141 ++++++++++-------
 product/ERP5/tests/testPackingList.py | 212 +++++++++++++++++++++++++-
 2 files changed, 300 insertions(+), 53 deletions(-)

diff --git a/product/ERP5/tests/testInvoice.py b/product/ERP5/tests/testInvoice.py
index a1e8654cdd..b11aa87e69 100644
--- a/product/ERP5/tests/testInvoice.py
+++ b/product/ERP5/tests/testInvoice.py
@@ -41,7 +41,7 @@ from DateTime import DateTime
 from Acquisition import aq_parent
 from zLOG import LOG
 from Products.ERP5Type.tests.Sequence import SequenceList
-from testPackingList import TestPackingListMixin
+from testPackingList import TestPackingListMixin, TestDeliveryRule
 from testAccountingRules import TestAccountingRulesMixin
 
 class TestInvoiceMixin(TestPackingListMixin,
@@ -2121,24 +2121,6 @@ class TestSaleInvoiceMixin(TestInvoiceMixin,
       self.assertEquals(self.default_quantity + last_delta,
           line.getQuantity())
 
-  def stepAddInvoiceLinesManyTransactions(self, sequence=None, sequence_list=[]):
-    """
-    add some invoice and accounting lines to the invoice
-    """
-    invoice = sequence.get('invoice')
-    invoice_line = invoice.newContent(portal_type='Invoice Line')
-    transaction_line_1 = invoice.newContent(portal_type='Sale Invoice Transaction Line')
-    transaction_line_2 = invoice.newContent(portal_type='Sale Invoice Transaction Line')
-    transaction.commit()
-    self.tic()
-    invoice_line.edit(resource_value=sequence.get('resource'), quantity=3,
-        price=555)
-    transaction_line_1.edit(id ='receivable', source='account_module/customer',
-        destination='account_module/supplier', quantity=-1665)
-    transaction_line_2.edit(
-        id='income', source='account_module/sale',
-        destination='account_module/purchase', quantity=1665)
-
   def stepAddInvoiceLines(self, sequence=None, sequence_list=[]):
     """
     add some invoice and accounting lines to the invoice
@@ -2668,6 +2650,58 @@ class TestSaleInvoice(TestSaleInvoiceMixin, TestInvoice, ERP5TypeTestCase):
       sequence_list.addSequenceString(sequence)
     sequence_list.play(self, quiet=quiet)
 
+  def stepAddInvoiceLinesManyTransactions(self, sequence=None, sequence_list=[]):
+    """
+    add some invoice and accounting lines to the invoice
+    """
+    invoice = sequence.get('invoice')
+    invoice_line = invoice.newContent(portal_type='Invoice Line')
+    transaction_line_1 = invoice.newContent(portal_type='Sale Invoice Transaction Line')
+    transaction_line_2 = invoice.newContent(portal_type='Sale Invoice Transaction Line')
+    transaction.commit()
+    self.tic()
+    invoice_line.edit(resource_value=sequence.get('resource'), quantity=3,
+        price=555)
+    transaction_line_1.edit(id ='receivable', source='account_module/customer',
+        destination='account_module/supplier', quantity=-1665)
+    transaction_line_2.edit(
+        id='income', source='account_module/sale',
+        destination='account_module/purchase', quantity=1665)
+
+
+  def test_16a_ManuallyAddedMovementsManyTransactions(self, quiet=quiet):
+    """
+    Checks that adding invoice lines and accounting lines to one invoice
+    generates correct simulation
+
+    In this case checks what is happening, where movements are added in
+    one transaction and edited in another
+    """
+    if not quiet:
+      self.logMessage('Invoice with Manually Added Movements in separate transactions')
+    sequence_list = SequenceList()
+    for base_sequence in (self.PACKING_LIST_DEFAULT_SEQUENCE, ) :
+      sequence_list.addSequenceString(
+          base_sequence +
+          """
+          stepSetReadyPackingList
+          stepTic
+          stepStartPackingList
+          stepCheckInvoicingRule
+          stepTic
+          stepCheckInvoiceBuilding
+          stepRebuildAndCheckNothingIsCreated
+          stepCheckInvoicesConsistency
+          stepAddInvoiceLinesManyTransactions
+          stepTic
+          stepCheckInvoiceIsSolved
+          stepStartInvoice
+          stepTic
+          stepCheckSimulationTrees
+          """)
+    sequence_list.play(self, quiet=quiet)
+
+
   def test_11_AcceptDecisionOnPackingListAndInvoice(self, quiet=quiet,
       run=RUN_ALL_TESTS):
     """
@@ -3059,38 +3093,6 @@ class TestSaleInvoice(TestSaleInvoiceMixin, TestInvoice, ERP5TypeTestCase):
           """)
     sequence_list.play(self, quiet=quiet)
 
-  def test_16a_ManuallyAddedMovementsManyTransactions(self, quiet=quiet):
-    """
-    Checks that adding invoice lines and accounting lines to one invoice
-    generates correct simulation
-
-    In this case checks what is happening, where movements are added in
-    one transaction and edited in another
-    """
-    if not quiet:
-      self.logMessage('Invoice with Manually Added Movements in separate transactions')
-    sequence_list = SequenceList()
-    for base_sequence in (self.PACKING_LIST_DEFAULT_SEQUENCE, ) :
-      sequence_list.addSequenceString(
-          base_sequence +
-          """
-          stepSetReadyPackingList
-          stepTic
-          stepStartPackingList
-          stepCheckInvoicingRule
-          stepTic
-          stepCheckInvoiceBuilding
-          stepRebuildAndCheckNothingIsCreated
-          stepCheckInvoicesConsistency
-          stepAddInvoiceLinesManyTransactions
-          stepTic
-          stepCheckInvoiceIsSolved
-          stepStartInvoice
-          stepTic
-          stepCheckSimulationTrees
-          """)
-    sequence_list.play(self, quiet=quiet)
-
   def test_17_ManuallyAddedWrongMovements(self, quiet=quiet, run=RUN_ALL_TESTS):
     """
     Checks that adding invoice lines and accounting lines to one invoice
@@ -3256,9 +3258,46 @@ class TestPurchaseInvoice(TestInvoice, ERP5TypeTestCase):
       stepTic
     """
 
+class TestInvoiceRule(TestDeliveryRule):
+  rule_portal_type = 'Invoice Rule'
+  delivery_line_portal_type = 'Invoice Line'
+
+  def test_delivery_rule_simulation_ready(self):
+    # not available on invoice
+    return
+
+  def test_delivery_rule_simulation_confirm(self):
+    self.createDocuments()
+    self.delivery.plan()
+    self.assertEqual('planned', self.delivery.getSimulationState())
+    transaction.commit() ; self.tic()
+
+    self.checkDeliverySimulation()
+
+    self.modifyDelivery()
+    
+    transaction.commit() ; self.tic()
+
+    self.checkDeliverySimulation()
+
+
+class TestInvoiceRuleSaleInvoiceTransaction(TestInvoiceRule):
+  delivery_portal_type = 'Sale Invoice Transaction'
+
+  def getTitle(self):
+    return "Sale Invoice Transaction Invoice Rule"
+
+class TestInvoiceRulePurchaseInvoiceTransaction(TestInvoiceRule):
+  delivery_portal_type = 'Purchase Invoice Transaction'
+
+  def getTitle(self):
+    return "Purchase Invoice Transaction Invoice Rule"
+
 import unittest
 def test_suite():
   suite = unittest.TestSuite()
   suite.addTest(unittest.makeSuite(TestSaleInvoice))
   suite.addTest(unittest.makeSuite(TestPurchaseInvoice))
+  suite.addTest(unittest.makeSuite(TestInvoiceRuleSaleInvoiceTransaction))
+  suite.addTest(unittest.makeSuite(TestInvoiceRulePurchaseInvoiceTransaction))
   return suite
diff --git a/product/ERP5/tests/testPackingList.py b/product/ERP5/tests/testPackingList.py
index 5fe50ca19c..bf77df837d 100644
--- a/product/ERP5/tests/testPackingList.py
+++ b/product/ERP5/tests/testPackingList.py
@@ -27,7 +27,6 @@
 ##############################################################################
 
 import unittest
-
 import transaction
 
 from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
@@ -42,7 +41,6 @@ class TestPackingListMixin(TestOrderMixin):
   container_portal_type = 'Container'
   container_line_portal_type = 'Container Line'
   container_cell_portal_type = 'Container Cell'
-
   default_order_sequence = '\
                       stepCreateOrganisation1 \
                       stepCreateOrganisation2 \
@@ -1415,9 +1413,219 @@ class TestPurchasePackingList(TestPurchasePackingListMixin, TestPackingList):
   """Tests for purchase packing list.
   """
 
+class TestDeliveryRule(TestPackingListMixin, ERP5TypeTestCase):
+  applied_rule_portal_type = 'Applied Rule'
+
+  def createResource(self, **kw):
+    module = self.portal.getDefaultModule(portal_type=self.resource_portal_type)
+    resource = module.newContent(portal_type=self.resource_portal_type, **kw)
+    return resource
+
+  def createOrganisation(self, **kw):
+    organisation_portal_type = 'Organisation'
+    module = self.portal.getDefaultModule(portal_type=organisation_portal_type)
+    organisation = module.newContent(portal_type=organisation_portal_type, **kw)
+    return organisation
+
+  def createDelivery(self, **kw):
+    module = self.portal.getDefaultModule(portal_type=self.delivery_portal_type)
+    delivery = module.newContent(portal_type=self.delivery_portal_type, **kw)
+    return delivery
+
+  def checkDeliverySimulation(self):
+    delivery = self.delivery
+    delivery_line = self.delivery_line
+    applied_rule_list = delivery.getCausalityRelatedValueList(
+        portal_type=self.applied_rule_portal_type)
+
+    # delivery shall have one applied rule
+    self.assertEqual(1, len(applied_rule_list))
+    applied_rule = applied_rule_list[0]
+
+    self.assertEqual(self.rule_portal_type, applied_rule.getSpecialiseValue()
+        .getPortalType())
+
+    simulation_movement_list = applied_rule.contentValues()
+
+    # there is one delivery line - so one simulation movement
+    self.assertEqual(1, len(simulation_movement_list))
+
+    simulation_movement = simulation_movement_list[0]
+
+    # now check if simulation movement has all properties set correctly
+
+    self.assertEqual( self.expected_resource, simulation_movement.getResourceValue())
+    self.assertEqual( self.expected_quantity, simulation_movement.getQuantity())
+    self.assertEqual( self.expected_source, simulation_movement.getSourceValue())
+    self.assertEqual( self.expected_source_section, simulation_movement.getSourceSectionValue())
+    self.assertEqual( self.expected_destination, simulation_movement.getDestinationValue())
+    self.assertEqual( self.expected_destination_section,
+        simulation_movement.getDestinationSectionValue())
+    self.assertEqual( self.expected_start_date, simulation_movement.getStartDate())
+    self.assertEqual( self.expected_stop_date, simulation_movement.getStopDate()
+    )
+
+  def createDocuments(self):
+    self.resource = self.createResource()
+    self.source = self.createOrganisation()
+    self.source_section = self.createOrganisation()
+    self.destination = self.createOrganisation()
+    self.destination_section = self.createOrganisation()
+    self.delivery = self.createDelivery(
+      source_value = self.source,
+      source_section_value = self.source_section,
+      destination_value = self.destination,
+      destination_section_value = self.destination_section,
+      start_date = self.datetime,
+      stop_date = self.datetime,
+    )
+
+    self.delivery_line = self.delivery.newContent(
+        portal_type=self.delivery_line_portal_type)
+    self.delivery_line.edit(
+      resource_value = self.resource,
+      quantity = self.default_quantity
+    )
+
+    self.expected_resource = self.resource
+    self.expected_quantity = self.default_quantity
+    self.expected_start_date = self.datetime
+    self.expected_stop_date = self.datetime
+    self.expected_source = self.source
+    self.expected_source_section = self.source_section
+    self.expected_destination = self.destination
+    self.expected_destination_section = self.destination_section
+
+  def modifyDelivery(self, change_expected=True):
+    # for now only quantity
+    new_quantity = self.delivery_line.getQuantity() - 1
+    self.delivery_line.edit(quantity = new_quantity)
+    if change_expected:
+      self.expected_quantity = new_quantity
+
+  def test_delivery_rule_simulation_confirm(self):
+    self.createDocuments()
+    self.delivery.confirm()
+    self.assertEqual('confirmed', self.delivery.getSimulationState())
+    transaction.commit() ; self.tic()
+
+    self.checkDeliverySimulation()
+
+    self.modifyDelivery()
+
+    transaction.commit() ; self.tic()
+
+    self.checkDeliverySimulation()
+
+  def test_delivery_rule_simulation_ready(self):
+    self.createDocuments()
+    self.delivery.confirm()
+    self.delivery.setReady()
+    self.assertEqual('ready', self.delivery.getSimulationState())
+
+    transaction.commit() ; self.tic()
+
+    self.checkDeliverySimulation()
+
+    self.modifyDelivery()
+
+    transaction.commit() ; self.tic()
+
+    self.checkDeliverySimulation()
+
+  def test_delivery_rule_simulation_start(self):
+    self.createDocuments()
+    self.delivery.confirm()
+    self.delivery.setReady()
+    self.delivery.start()
+    self.assertEqual('started', self.delivery.getSimulationState())
+
+    transaction.commit() ; self.tic()
+
+    self.checkDeliverySimulation()
+
+    self.modifyDelivery()
+
+    transaction.commit() ; self.tic()
+
+    self.checkDeliverySimulation()
+
+  def test_delivery_rule_simulation_stop(self):
+    self.createDocuments()
+    self.delivery.confirm()
+    self.delivery.setReady()
+    self.delivery.start()
+    self.delivery.stop()
+    self.assertEqual('stopped', self.delivery.getSimulationState())
+
+    transaction.commit() ; self.tic()
+
+    self.checkDeliverySimulation()
+
+    # stopped state freezes simulation
+    self.modifyDelivery(False)
+
+    transaction.commit() ; self.tic()
+
+    self.checkDeliverySimulation()
+
+  def test_delivery_rule_simulation_deliver(self):
+    self.createDocuments()
+    self.delivery.confirm()
+    self.delivery.setReady()
+    self.delivery.start()
+    self.delivery.stop()
+    self.delivery.deliver()
+    self.assertEqual('delivered', self.delivery.getSimulationState())
+
+    transaction.commit() ; self.tic()
+
+    self.checkDeliverySimulation()
+
+    # delivered state freezes simulation
+    self.modifyDelivery(False)
+
+    transaction.commit() ; self.tic()
+
+    self.checkDeliverySimulation()
+
+  def test_delivery_rule_simulation_cancel(self):
+    self.createDocuments()
+    self.delivery.confirm()
+    self.delivery.cancel()
+    self.assertEqual('cancelled', self.delivery.getSimulationState())
+
+    transaction.commit() ; self.tic()
+
+    self.checkDeliverySimulation()
+
+    # cancelled state freezes simulation
+    self.modifyDelivery(False)
+
+    transaction.commit() ; self.tic()
+
+    self.checkDeliverySimulation()
+
+class TestDeliveryRuleSalePackingList(TestDeliveryRule):
+  delivery_portal_type = 'Sale Packing List'
+  delivery_line_portal_type = 'Sale Packing List Line'
+  rule_portal_type = 'Delivery Rule'
+
+  def getTitle(self):
+    return "Sale Packing List Delivery Rule"
+
+class TestDeliveryRulePurchasePackingList(TestDeliveryRule):
+  delivery_portal_type = 'Purchase Packing List'
+  delivery_line_portal_type = 'Purchase Packing List Line'
+  rule_portal_type = 'Delivery Rule'
+
+  def getTitle(self):
+    return "Purchase Packing List Delivery Rule"
 
 def test_suite():
   suite = unittest.TestSuite()
   suite.addTest(unittest.makeSuite(TestPackingList))
   suite.addTest(unittest.makeSuite(TestPurchasePackingList))
+  suite.addTest(unittest.makeSuite(TestDeliveryRuleSalePackingList))
+  suite.addTest(unittest.makeSuite(TestDeliveryRulePurchasePackingList))
   return suite
-- 
2.30.9