From 74838d442ef739cd15135c40f6c6659f8491f5c0 Mon Sep 17 00:00:00 2001
From: Nicolas Dumazet <nicolas.dumazet@nexedi.com>
Date: Thu, 18 Mar 2010 03:15:52 +0000
Subject: [PATCH] Fix durably getAggregatedAmount issues: * both Transformation
 and TransformedResource should support a context   passing a multiple
 quantity. The proper way to do so is to deleguate   quantity computation to
 Transformation Lines, and then to pull the result   up to aggregate it in
 Transformations. * Fix a quirk in TransformationRule that was induced by the
 wrong   behaviour of TransformedResource.getAggregatedAmountList: noone
 should   need to multiply the amounts returned by getAggregatedAmountList, as
   the method should already include the computed quantities.

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@33830 20353a03-c40f-0410-a6d1-a30d3c3de9de
---
 product/ERP5/Document/Transformation.py       | 11 ++++-------
 product/ERP5/Document/TransformedResource.py  | 19 +++++++++++++++++++
 .../ERP5Legacy/Document/TransformationRule.py |  2 +-
 3 files changed, 24 insertions(+), 8 deletions(-)

diff --git a/product/ERP5/Document/Transformation.py b/product/ERP5/Document/Transformation.py
index a6ab0775cb..3c17804e43 100644
--- a/product/ERP5/Document/Transformation.py
+++ b/product/ERP5/Document/Transformation.py
@@ -241,14 +241,11 @@ class Transformation(XMLObject, Predicate, Variated):
                                 # obsolete, use trade_phase_list instead
                                 ind_phase_url_list=None,
                                 rejected_resource_uid_list=None,
-                                context_quantity=0,**kw):
+                                **kw):
       """
         getAggregatedAmountList returns an AggregatedAmountList which
         can be used either to do some calculation (ex. price, BOM)
         or to display a detailed view of a transformation.
-
-        context_quantity : if set to one, multiply all quantities
-                           with the quantity of the context
       """
       context = self.asContext(context=context, REQUEST=REQUEST, **kw)
 
@@ -304,13 +301,13 @@ class Transformation(XMLObject, Predicate, Variated):
           # transformation
           if line_is_included(transformation_line):
             try:
-              result.extend(transformation_line.getAggregatedAmountList(context))
+              line_result = transformation_line.getAggregatedAmountList(context)
             except KeyError:
               # KeyError is raised by TransformedResource.getAggregatedAmountList
               # in case of misconfiguration of a Cell.
               # Just ignore the line
               pass
+            else:
+              result.extend(line_result)
 
-      if context_quantity:
-        result.multiplyQuantity(context=context)
       return result
diff --git a/product/ERP5/Document/TransformedResource.py b/product/ERP5/Document/TransformedResource.py
index c59731d5ba..d5fad98c47 100644
--- a/product/ERP5/Document/TransformedResource.py
+++ b/product/ERP5/Document/TransformedResource.py
@@ -264,6 +264,25 @@ class TransformedResource(Predicate, XMLObject, XMLMatrix, Amount):
         except ValueError:
           error_string += 'Quantity is not a float.'
 
+        # If IAmount specifies that 4 resources are needed, all quantities
+        # need to be multiplicated by 4...
+        context_quantity = None
+        quantity_getter = getattr(context, "getQuantity", None)
+        if quantity_getter is not None:
+          _marker = object()
+          context_quantity = quantity_getter(_marker)
+          if context_quantity is _marker:
+            # XXX Backwards compatibility:
+            # previously, quantity property of the Amount was completely
+            # ignored, and was assumed to be 1.0 . Re-enact this old
+            # behavior (quantity default value is 0.0) to avoid breakages
+            warn("No quantity was defined on the Amount passed to " \
+                 "getAggregatedAmountList, 1.0 was assumed", DeprecationWarning)
+            context_quantity = 1.0
+        else:
+          raise KeyError("No quantity defined on context")
+        quantity *= float(context_quantity)
+
         # Get the variation category list
         variation_category_list_defined_by = None
         variation_category_list = None
diff --git a/product/ERP5Legacy/Document/TransformationRule.py b/product/ERP5Legacy/Document/TransformationRule.py
index e964836119..7960685a1a 100644
--- a/product/ERP5Legacy/Document/TransformationRule.py
+++ b/product/ERP5Legacy/Document/TransformationRule.py
@@ -248,7 +248,7 @@ class TransformationRule(TransformationSourcingRuleMixin, Rule):
                         amount.getVariationCategoryList(),
           "variation_property_dict": \
                         amount.getVariationPropertyDict(),
-          "quantity": amount.getNetQuantity() * parent_movement.getQuantity(), # getNetQuantity to support efficency from transformation
+          "quantity": amount.getNetQuantity(), # getNetQuantity to support efficency from transformation
           "price": price,
           "quantity_unit": amount.getQuantityUnit(),
           "destination_list": (),
-- 
2.30.9