From 64f0303332958207eee767839ed395332230c771 Mon Sep 17 00:00:00 2001
From: Jean-Paul Smets <jp@nexedi.com>
Date: Sun, 14 Dec 2003 22:06:13 +0000
Subject: [PATCH] updatePrice method added

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@105 20353a03-c40f-0410-a6d1-a30d3c3de9de
---
 product/ERP5/Document/Delivery.py     |  6 +++
 product/ERP5/Document/DeliveryCell.py | 47 ++++++++++++----------
 product/ERP5/Document/DeliveryLine.py | 57 +++++++++++++++------------
 3 files changed, 63 insertions(+), 47 deletions(-)

diff --git a/product/ERP5/Document/Delivery.py b/product/ERP5/Document/Delivery.py
index ed16725769..fd42fb67c5 100755
--- a/product/ERP5/Document/Delivery.py
+++ b/product/ERP5/Document/Delivery.py
@@ -410,6 +410,12 @@ une liste de mouvements..."""
       return self._getDestinationTotalPrice(self.asContext(context=context, REQUEST=REQUEST, **kw))
 
     # Pricing
+    security.declareProtected( Permissions.ModifyPortalContent, 'updatePrice' )
+    def updatePrice(self):
+      for c in self.objectValues():
+        if hasattr(aq_base(c), 'updatePrice'):
+          c.updatePrice()
+
     security.declareProtected(Permissions.AccessContentsInformation, 'getTotalPrice')
     def getTotalPrice(self):
       """
diff --git a/product/ERP5/Document/DeliveryCell.py b/product/ERP5/Document/DeliveryCell.py
index dc355f4601..64d6eaf14c 100755
--- a/product/ERP5/Document/DeliveryCell.py
+++ b/product/ERP5/Document/DeliveryCell.py
@@ -180,6 +180,32 @@ Une ligne tarifaire."""
         result = None
       return result
 
+    security.declareProtected( Permissions.ModifyPortalContent, 'updatePrice' )
+    def updatePrice(self):
+      if 'price' in self.getMappedValuePropertyList([]):
+        # Try to compute an average price by accessing simulation movements
+        # This should always return 0 in the case of OrderCell
+        total_quantity = 0.0
+        total_price = 0.0
+        for m in self.getDeliveryRelatedValueList(portal_type="Simulation Movement"):
+          order = m.getOrderValue()
+          if order is not None:
+            # Price is defined in an order
+            price = m.getPrice()
+            quantity = m.getQuantity()
+            try:
+              price = float(price)
+              quantity = float(quantity)
+            except:
+              price = 0.0
+              quantity = 0.0
+            total_quantity += quantity
+            total_price += quantity * price
+        if total_quantity:
+          # Update local price
+          # self._setPrice(total_price / total_quantity)
+          self.setPrice( total_price / total_quantity )
+
     security.declareProtected( Permissions.AccessContentsInformation, 'getPrice' )
     def getPrice(self, context=None, REQUEST=None, **kw):
       """
@@ -188,27 +214,6 @@ Une ligne tarifaire."""
       """
       # Call a script on the context
       if 'price' in self.getMappedValuePropertyList([]):
-        # Price is defined in an order
-        # First try to compute an average price by accessing simulation movements
-        # this should always return 0 in the case of OrderCell
-        total_quantity = 0.0
-        total_price = 0.0
-        for m in self.getDeliveryRelatedValueList(portal_type="Simulation Movement"):
-          price = m.getPrice()
-          quantity = m.getQuantity()
-          try:
-            price = float(price)
-            quantity = float(quantity)
-          except:
-            price = 0.0
-            quantity = 0.0
-          total_quantity += quantity
-          total_price += quantity * price
-        if total_quantity:
-          # Update local price
-          # self._setPrice(total_price / total_quantity)
-          return total_price / total_quantity
-        # Either this is an order cell or it is a delivery with no relation in the simulation
         if getattr(aq_base(self), 'price', None) is not None:
           return getattr(self, 'price') # default returns a price defined by the mapped value
         else:
diff --git a/product/ERP5/Document/DeliveryLine.py b/product/ERP5/Document/DeliveryLine.py
index 4b28312cb1..b170e90f48 100755
--- a/product/ERP5/Document/DeliveryLine.py
+++ b/product/ERP5/Document/DeliveryLine.py
@@ -28,6 +28,7 @@
 
 from Globals import InitializeClass, PersistentMapping
 from AccessControl import ClassSecurityInfo
+from Acquisition import aq_base
 
 from Products.CMFCore.WorkflowCore import WorkflowAction
 from Products.ERP5Type import Permissions, PropertySheet, Constraint, Interface
@@ -153,34 +154,38 @@ Une ligne tarifaire."""
       return self.aq_parent.isAccountable() and (not self.hasCellContent())
 
     # Pricing
-    security.declareProtected(Permissions.AccessContentsInformation, 'getPrice')
-    def getPrice(self, context=None, REQUEST=None, **kw):
+    security.declareProtected(Permissions.ModifyPortalContent, 'updatePrice')
+    def updatePrice(self):
       """
-        Returns the price if defined on the cell
-        or acquire it
+        Tries to find out a price for this movement
       """
-      # Price is defined in an order
-      # First try to compute an average price by accessing simulation movements
-      # this should always return 0 in the case of OrderCell
-      total_quantity = 0.0
-      total_price = 0.0
-      for m in self.getDeliveryRelatedValueList(portal_type="Simulation Movement"):
-          price = m.getPrice()
-          quantity = m.getQuantity()
-          try:
-            price = float(price)
-            quantity = float(quantity)
-          except:
-            price = 0.0
-            quantity = 0.0
-          total_quantity += quantity
-          total_price += quantity * price
-      if total_quantity:
-        # Update local price
-        # self._setPrice(total_price / total_quantity)
-        return total_price / total_quantity
-      # Either this is an order cell or it is a delivery with no relation in the simulation
-      return Movement.getPrice(self, context=context, REQUEST=REQUEST, **kw)
+      if not self.hasCellContent():
+        # Try to compute an average price by accessing simulation movements
+        # This should always return 0 in the case of OrderCell
+        total_quantity = 0.0
+        total_price = 0.0
+        for m in self.getDeliveryRelatedValueList(portal_type="Simulation Movement"):
+          order = m.getOrderValue()
+          if order is not None:
+            # Price is defined in an order
+            price = m.getPrice()
+            quantity = m.getQuantity()
+            try:
+              price = float(price)
+              quantity = float(quantity)
+            except:
+              price = 0.0
+              quantity = 0.0
+            total_quantity += quantity
+            total_price += quantity * price
+        if total_quantity:
+          # Update local price
+          # self._setPrice(total_price / total_quantity)
+          self.setPrice( total_price / total_quantity )
+      else:
+        for c in self.objectValues():
+          if hasattr(aq_base(c), 'updatePrice'):
+            c.updatePrice()
 
     def _getTotalPrice(self, context):
       if not self.hasCellContent():
-- 
2.30.9