From b6159852615e465d89d375ff074e0622dd08470f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?J=C3=A9rome=20Perrin?= <jerome@nexedi.com>
Date: Wed, 2 Sep 2009 15:06:29 +0000
Subject: [PATCH] Use a more robust way than getDeliveryRelatedValue list to
 check if we have to create a new simulation branch for a given delivery
 movement.

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@28754 20353a03-c40f-0410-a6d1-a30d3c3de9de
---
 product/ERP5/Document/DeliveryRule.py | 26 +++++++++++++++++++++++++-
 1 file changed, 25 insertions(+), 1 deletion(-)

diff --git a/product/ERP5/Document/DeliveryRule.py b/product/ERP5/Document/DeliveryRule.py
index ff4b394982..03da074798 100644
--- a/product/ERP5/Document/DeliveryRule.py
+++ b/product/ERP5/Document/DeliveryRule.py
@@ -93,7 +93,7 @@ class DeliveryRule(Rule):
 
       # Create or modify movements
       for deliv_mvt in delivery_movement_list:
-        sim_mvt = deliv_mvt.getDeliveryRelatedValue()
+        sim_mvt = self._getDeliveryRelatedSimulationMovement(deliv_mvt)
         if sim_mvt is None:
           # create a new deliv_mvt
           if deliv_mvt.getParentUid() == deliv_mvt.getExplanationUid():
@@ -177,6 +177,30 @@ class DeliveryRule(Rule):
     # Pass to base class
     Rule.expand(self, applied_rule, **kw)
 
+  def _getDeliveryRelatedSimulationMovement(self, delivery_movement):
+    """Helper method to get the delivery related simulation movement.
+    This method is more robust than simply calling getDeliveryRelatedValue
+    which will not work if simulation movements are not indexed.
+    """
+    simulation_movement = delivery_movement.getDeliveryRelatedValue()
+    if simulation_movement is not None:
+      return simulation_movement
+    # simulation movement was not found, maybe simply because it's not indexed
+    # yet. We'll look in the simulation tree and try to find it anyway before
+    # creating another simulation movement.
+    # Try to find the one from trade model rule, which is the most common case
+    # where we may expand again before indexation of simulation movements is
+    # finished.
+    delivery = delivery_movement.getExplanationValue()
+    for movement in delivery.getMovementList():
+      related_simulation_movement = movement.getDeliveryRelatedValue()
+      if related_simulation_movement is not None:
+        for applied_rule in related_simulation_movement.contentValues():
+          for simulation_movement in applied_rule.contentValues():
+            if simulation_movement.getDeliveryValue() == delivery_movement:
+              return simulation_movement
+    return None
+
   security.declareProtected(Permissions.ModifyPortalContent, 'solve')
   def solve(self, applied_rule, solution_list):
     """
-- 
2.30.9