[Erp5-report] r41891 jm - /erp5/trunk/products/ERP5/MovementGroup.py
nobody at svn.erp5.org
nobody at svn.erp5.org
Thu Dec 30 20:31:15 CET 2010
Author: jm
Date: Thu Dec 30 20:31:15 2010
New Revision: 41891
URL: http://svn.erp5.org?rev=41891&view=rev
Log:
MovementGroup.getAveragePrice: avoid useless calculations when all prices are equal
What could happen otherwise:
>>> a = 0.4 * 6 * 5; a
12.000000000000002
>>> b = 1.6 * 3; b
4.8000000000000007
>>> c = 0.05; c
0.050000000000000003
>>> (a * c + b * c) / (a + b)
0.049999999999999989
Modified:
erp5/trunk/products/ERP5/MovementGroup.py
Modified: erp5/trunk/products/ERP5/MovementGroup.py
URL: http://svn.erp5.org/erp5/trunk/products/ERP5/MovementGroup.py?rev=41891&r1=41890&r2=41891&view=diff
==============================================================================
--- erp5/trunk/products/ERP5/MovementGroup.py [utf8] (original)
+++ erp5/trunk/products/ERP5/MovementGroup.py [utf8] Thu Dec 30 20:31:15 2010
@@ -401,10 +401,11 @@ class FakeMovement:
"""
Return average price
"""
- total_quantity = self.getAddQuantity()
- if total_quantity != 0:
- return (self.getAddPrice() / total_quantity)
- return 0.0
+ price_dict = self._getPriceDict()
+ if len(price_dict) == 1:
+ return price_dict.keys()[0]
+ return sum(price * quantity for price, quantity in price_dict.items()) / \
+ float(sum(price_dict.values()))
def getAddQuantity(self):
"""
@@ -412,28 +413,35 @@ class FakeMovement:
"""
total_quantity = 0
for movement in self.getMovementList():
- if getattr(movement, 'getMappedProperty', None) is not None:
- quantity = movement.getMappedProperty('quantity')
- else:
+ getMappedProperty = getattr(movement, 'getMappedProperty', None)
+ if getMappedProperty is None:
quantity = movement.getQuantity()
- if quantity != None:
+ else:
+ quantity = getMappedProperty('quantity')
+ if quantity:
total_quantity += quantity
return total_quantity
+ def _getPriceDict(self):
+ price_dict = {}
+ for movement in self.getMovementList():
+ getMappedProperty = getattr(movement, 'getMappedProperty', None)
+ if getMappedProperty is None:
+ quantity = movement.getQuantity()
+ else:
+ quantity = getMappedProperty('quantity')
+ if quantity:
+ price = movement.getPrice() or 0
+ quantity += price_dict.setdefault(price, 0)
+ price_dict[price] = quantity
+ return price_dict
+
def getAddPrice(self):
"""
Return total price
"""
- total_price = 0
- for movement in self.getMovementList():
- if getattr(movement, 'getMappedProperty', None) is not None:
- quantity = movement.getMappedProperty('quantity')
- else:
- quantity = movement.getQuantity()
- price = movement.getPrice()
- if (quantity is not None) and (price is not None):
- total_price += (quantity * price)
- return total_price
+ price_dict = self._getPriceDict()
+ return sum(price * quantity for price, quantity in price_dict.items())
def recursiveReindexObject(self):
"""
More information about the Erp5-report
mailing list