From 1541c540eac94d03e36a12ec0f6c63924cf25bed Mon Sep 17 00:00:00 2001
From: Jean-Paul Smets <jp@nexedi.com>
Date: Thu, 18 Dec 2003 00:22:35 +0000
Subject: [PATCH] Improved isSimulated

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@132 20353a03-c40f-0410-a6d1-a30d3c3de9de
---
 product/ERP5/Document/Delivery.py     |  4 ++++
 product/ERP5/Document/DeliveryLine.py |  4 +---
 product/ERP5/Document/PackingList.py  | 14 +++++++++++++-
 3 files changed, 18 insertions(+), 4 deletions(-)

diff --git a/product/ERP5/Document/Delivery.py b/product/ERP5/Document/Delivery.py
index 5f726bdfd9..e60c2af3ad 100755
--- a/product/ERP5/Document/Delivery.py
+++ b/product/ERP5/Document/Delivery.py
@@ -492,6 +492,10 @@ une liste de mouvements..."""
 
     security.declareProtected(Permissions.View, 'isSimulated')
     def isSimulated(self):
+      """
+        Returns 1 if all movements have a delivery or order counterpart
+        in the simulation
+      """
       for m in self.getMovementList():
         if not m.isSimulated():
           return 0
diff --git a/product/ERP5/Document/DeliveryLine.py b/product/ERP5/Document/DeliveryLine.py
index db4ff9f2d7..2bd62ff0e4 100755
--- a/product/ERP5/Document/DeliveryLine.py
+++ b/product/ERP5/Document/DeliveryLine.py
@@ -309,10 +309,8 @@ Une ligne tarifaire."""
         for cell in self.contentValues(filter={'portal_type': 'Delivery Cell'}):
           if cell.isDivergent():
             return 1
-          if not cell.isSimulated():
-            return 1
       else:
-         return Movement.isDivergent(self) or (not self.isSimulated())
+         return Movement.isDivergent(self)
 
     security.declareProtected(Permissions.ModifyPortalContent, 'applyTargetSolver')
     def applyTargetSolver(self, solver):
diff --git a/product/ERP5/Document/PackingList.py b/product/ERP5/Document/PackingList.py
index 2b984ea6e0..e2b74fed5b 100755
--- a/product/ERP5/Document/PackingList.py
+++ b/product/ERP5/Document/PackingList.py
@@ -29,6 +29,7 @@
 from Globals import InitializeClass, PersistentMapping
 from AccessControl import ClassSecurityInfo
 from Products.ERP5Type import Permissions, PropertySheet, Constraint, Interface
+from Products.ERP5.ERP5Globals import movement_type_list, draft_order_state
 
 from Delivery import Delivery
 
@@ -132,10 +133,20 @@ An order..."""
         )
       }
 
+    security.declareProtected(Permissions.View, 'isDivergent')
+    def isDivergent(self):
+      """
+        Returns 1 if not simulated or inconsistent target and values
+      """
+      if self.getSimulationState() not in draft_order_state:
+        if not self.isSimulated():
+          return 1
+      return Delivery.isDivergent(self)
+
     security.declareProtected(Permissions.ModifyPortalContent, 'updateAppliedRule')
     def updateAppliedRule(self):
       if self.getSimulationState() not in draft_order_state:
-        # Nothing to do
+        # Nothing to do if we are already simulated
         self._createDeliveryRule()
 
     security.declareProtected(Permissions.ModifyPortalContent, '_createDeliveryRule')
@@ -149,6 +160,7 @@ An order..."""
       # Look up if existing applied rule
       my_applied_rule_list = self.getCausalityRelatedValueList(portal_type='Applied Rule')
       if len(my_applied_rule_list) == 0:
+        if self.isSimulated(): return # No need to create a DeliveryRule if we are already in the simulation process
         # Create a new applied order rule (portal_rules.order_rule)
         portal_rules = getToolByName(self, 'portal_rules')
         portal_simulation = getToolByName(self, 'portal_simulation')
-- 
2.30.9