From a4c9e4d6e588d7fd51c426ff03d9da4565bed113 Mon Sep 17 00:00:00 2001
From: Guillaume Michon <guillaume@nexedi.com>
Date: Mon, 18 Apr 2005 06:03:45 +0000
Subject: [PATCH] Adapted to target properties removal

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@2885 20353a03-c40f-0410-a6d1-a30d3c3de9de
---
 product/ERP5/TargetSolver/ProfitAndLoss.py | 24 +++++++++++++---------
 1 file changed, 14 insertions(+), 10 deletions(-)

diff --git a/product/ERP5/TargetSolver/ProfitAndLoss.py b/product/ERP5/TargetSolver/ProfitAndLoss.py
index 386260b5a4..f5944dff94 100755
--- a/product/ERP5/TargetSolver/ProfitAndLoss.py
+++ b/product/ERP5/TargetSolver/ProfitAndLoss.py
@@ -29,6 +29,7 @@
 
 from Products.ERP5.Tool.SimulationTool import registerTargetSolver
 from CopyToTarget import CopyToTarget
+from zLOG import LOG
 
 class ProfitAndLoss(CopyToTarget):
   """
@@ -44,15 +45,18 @@ class ProfitAndLoss(CopyToTarget):
       Movement difference as a profit (ie. a quantity coming from nowhere)
       Accumulate into delivered movement
     """
-    target_quantity = movement.getTargetQuantity()
-    new_target_quantity = new_target.target_quantity
-    if target_quantity != new_target_quantity:
-      previous_profit_quantity = movement.getProfitQuantity()
-      movement.setProfitQuantity(new_target_quantity - target_quantity)
-      new_profit_quantity = movement.getProfitQuantity()
-      delivery_value = movement.getDeliveryValue()
-      if delivery_value is not None:
-        delivery_value.setProfitQuantity(delivery_value.getProfitQuantity() + new_profit_quantity -  previous_profit_quantity)
-    CopyToTarget.solve(self, movement, new_target)
+    LOG('profit and loss called on movement', 0, repr(movement))
+    previous_quantity = getattr(movement, '_v_previous_quantity', None)
+    if previous_quantity is None:
+      return
+    added_quantity = movement.getQuantity() - previous_quantity
+    profit_quantity = movement.getProfitQuantity()
+    if profit_quantity is None:
+      profit_quantity = 0.
+    movement.setQuantity(previous_quantity)
+    movement.setProfitQuantity(profit_quantity - added_quantity)
+
+   # The calling method must call Delivery.updateSimulationDeliveryProperties once
+   # all the movements have been solved
 
 registerTargetSolver(ProfitAndLoss)
-- 
2.30.9