From 9aa34428244709f0ed99d1cadf6303ff951346f0 Mon Sep 17 00:00:00 2001
From: Guillaume Michon <guillaume@nexedi.com>
Date: Mon, 18 Apr 2005 05:53:45 +0000
Subject: [PATCH] Massive update to adapt to the target properties removal

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@2878 20353a03-c40f-0410-a6d1-a30d3c3de9de
---
 product/ERP5/DeliverySolver/Distribute.py | 80 ++++++++++++++++-------
 1 file changed, 57 insertions(+), 23 deletions(-)

diff --git a/product/ERP5/DeliverySolver/Distribute.py b/product/ERP5/DeliverySolver/Distribute.py
index 4571a87bee..486f1fccf1 100755
--- a/product/ERP5/DeliverySolver/Distribute.py
+++ b/product/ERP5/DeliverySolver/Distribute.py
@@ -29,6 +29,7 @@
 
 from Products.ERP5.Tool.SimulationTool import registerDeliverySolver
 from DeliverySolver import DeliverySolver
+from zLOG import LOG
 
 class Distribute(DeliverySolver):
   """
@@ -40,30 +41,63 @@ class Distribute(DeliverySolver):
       Solve a delivery by reducing / increasing each simulation movement
       it relates to
     """
-    delivery_line_simulation_quantity = float(movement.getSimulationQuantity())
+    # Determine the amount to distribute
+    d_source = movement.getSource()
+    d_destination = movement.getDestination()
+    d_source_section = movement.getSourceSection()
+    d_destination_section = movement.getDestinationSection()
+    d_start_date = movement.getStartDate()
+    d_stop_date = movement.getStopDate()
+    d_resource = movement.getResource()
+    
+    simulation_movement_list = movement.getDeliveryRelatedValueList()
+    new_simulation_movement_list = []
+    to_aggregate_list = []
+    delivery_line_simulation_quantity = 0
+    for simulation_movement in simulation_movement_list:
+      m_source = simulation_movement.getSource()
+      m_destination = simulation_movement.getDestination()
+      m_source_section = simulation_movement.getSourceSection()
+      m_destination_section = simulation_movement.getDestinationSection()
+      m_start_date = simulation_movement.getStartDate()
+      m_stop_date = simulation_movement.getStopDate()
+      m_resource = simulation_movement.getResource()
+      if m_source != d_source or \
+         m_destination != d_destination or \
+         m_source_section != d_source_section or \
+         m_destination_section != d_destination_section or \
+         m_start_date != d_start_date or \
+         m_stop_date != d_stop_date or \
+         m_resource != d_resource:
+          # Disconnect the movement if anything else the quantity is changed
+          to_aggregate_list.append(simulation_movement)
+          simulation_movement.setDelivery('')
+          simulation_movement.setProfitQuantity(simulation_movement.getQuantity())
+          simulation_movement.setDeliveryError(0.)
+          simulation_movement.immediateReindexObject()
+      else:
+        delivery_line_simulation_quantity += float(simulation_movement.getCorrectedQuantity())
+        new_simulation_movement_list.append(simulation_movement)
+        
     delivery_line_quantity = float(movement.getQuantity())
-    if delivery_line_simulation_quantity != delivery_line_quantity:
-      if delivery_line_simulation_quantity != 0 :
-      # XXXXXXXXXXXXXXXXXXXXXXXXX something special should be done if delivery_line_simulation_quantity == 0 !
-        distribute_ratio = delivery_line_quantity  / delivery_line_simulation_quantity
-        for s in movement.getDeliveryRelatedValueList():
-          # Reduce quantity
-          s.setQuantity(s.getQuantity() * distribute_ratio)
-          # Change dates
-          s.setStartDate(movement.getStartDate())
-          s.setStopDate(movement.getStopDate())
-          s.diverge() # Make sure everyone knows this simulation movement is inconsistent
+    to_distribute = delivery_line_quantity - delivery_line_simulation_quantity
+    if to_distribute != 0:
+      if delivery_line_simulation_quantity != 0:
+        for m in new_simulation_movement_list:
+          m_corrected_quantity = m.getCorrectedQuantity()
+          m_quantity = m.getQuantity()
+          m._v_previous_quantity = m_quantity
+          distribute_ratio = m_corrected_quantity / delivery_line_simulation_quantity
+          m.setQuantity(m_quantity + to_distribute * distribute_ratio)
+          m.setDeliveryError(0.)
+          m.immediateReindexObject()
       else:
-        delivery_related_value_list = movement.getDeliveryRelatedValueList()
-        distribute_ratio = float(len(delivery_related_value_list))
-        target_quantity = movement.getTargetQuantity()
-        for s in delivery_related_value_list:
-          # Define new quantity
-          s.setQuantity(target_quantity / distribute_ratio)
-          # Change dates
-          s.setStartDate(movement.getStartDate())
-          s.setStopDate(movement.getStopDate())
-          s.diverge() # Make sure everyone knows this simulation movement is inconsistent
-    # No need to touch date since it should be defined at the upper level.
+        if len(new_simulation_movement_list) > 0:
+          to_add_quantity = to_distribute / len(new_simulation_movement_list)
+          for m in new_simulation_movement_list:
+            m._v_previous_quantity = m.getQuantity()
+            m.setQuantity(m.getQuantity() + to_add_quantity)
+            m.setDeliveryError(0.)
+            m.immediateReindexObject()
 
 registerDeliverySolver(Distribute)
-- 
2.30.9