From 2bcdd046ad0b977332d65ba41a58e09e56b7bb95 Mon Sep 17 00:00:00 2001
From: Julien Muchembled <jm@nexedi.com>
Date: Thu, 12 Aug 2010 18:19:43 +0000
Subject: [PATCH] Add reference and causality links to generated amounts

git-svn-id: https://svn.erp5.org/repos/public/erp5/sandbox/amount_generator@37780 20353a03-c40f-0410-a6d1-a30d3c3de9de
---
 product/ERP5/Document/Amount.py          |  1 +
 product/ERP5/Document/BusinessProcess.py |  8 +++++---
 product/ERP5/Document/Movement.py        |  1 +
 product/ERP5/mixin/amount_generator.py   | 26 +++++++++++++++---------
 4 files changed, 23 insertions(+), 13 deletions(-)

diff --git a/product/ERP5/Document/Amount.py b/product/ERP5/Document/Amount.py
index 17debe476a..681214e5ee 100644
--- a/product/ERP5/Document/Amount.py
+++ b/product/ERP5/Document/Amount.py
@@ -67,6 +67,7 @@ class Amount(Base, Variated):
                     , PropertySheet.SimpleItem
                     , PropertySheet.Amount
                     , PropertySheet.Price
+                    , PropertySheet.Reference
                     )
 
   # A few more mix-in methods which should be relocated
diff --git a/product/ERP5/Document/BusinessProcess.py b/product/ERP5/Document/BusinessProcess.py
index fe491e6f26..edecf98814 100644
--- a/product/ERP5/Document/BusinessProcess.py
+++ b/product/ERP5/Document/BusinessProcess.py
@@ -711,9 +711,11 @@ class BusinessProcess(Path, XMLObject):
         pass
       kw.update(update_property_dict)
       movement._edit(**kw)
-      business_link = self.getBusinessLinkValueList(trade_phase=trade_phase, context=movement)
-      business_link = map(lambda x: x.getRelativeUrl(), business_link)
-      movement._setCausalityList(business_link + movement.getCausalityList())
+      business_link = self.getBusinessLinkValueList(trade_phase=trade_phase,
+                                                    context=movement)
+      business_link = [x.getRelativeUrl() for x in business_link]
+      movement._setCausalityList(business_link
+        + movement.getCausalityList() + amount.getCausalityList())
       result.append(movement)
 
     # result can not be empty
diff --git a/product/ERP5/Document/Movement.py b/product/ERP5/Document/Movement.py
index 523719e3fd..7a19bf5595 100644
--- a/product/ERP5/Document/Movement.py
+++ b/product/ERP5/Document/Movement.py
@@ -196,6 +196,7 @@ class Movement(XMLObject, Amount, CompositionMixin, AmountGeneratorMixin):
                     , PropertySheet.SimpleItem
                     , PropertySheet.CategoryCore
                     , PropertySheet.Amount
+                    , PropertySheet.Reference
                     , PropertySheet.Task
                     , PropertySheet.Arrow
                     , PropertySheet.Movement
diff --git a/product/ERP5/mixin/amount_generator.py b/product/ERP5/mixin/amount_generator.py
index 9b0f6b5e3c..76bfd82d6a 100644
--- a/product/ERP5/mixin/amount_generator.py
+++ b/product/ERP5/mixin/amount_generator.py
@@ -220,13 +220,8 @@ class AmountGeneratorMixin:
             property_dict.setdefault('base_application_set',
                                      set()).add(base_application)
             #property_dict['trade_phase_list'] = amount_generator_cell.getTradePhaseList() # Required moved to MappedValue
-            property_dict['reference'] = (amount_generator_cell.getReference()
-                                          or self.getReference()) # XXX
-            property_dict['id'] = amount_generator_cell.getRelativeUrl().replace('/', '_')
-            property_dict['title'] = self.getTitle()
-            property_dict['int_index'] = self.getIntIndex()
-            property_dict['description'] = self.getDescription()
-
+            property_dict.setdefault('causality_value_list',
+                                     []).append(amount_generator_cell)
           # Case 2: the cell defines a temporary calculation line
           if base_contribution_list:
             # Define a key in order to aggregate amounts in cells
@@ -254,6 +249,7 @@ class AmountGeneratorMixin:
             # base_contribution_list MUST be defined
             property_dict['base_contribution_list'] = base_contribution_list
       for property_dict in resource_amount_aggregate.itervalues():
+        causality_value_list = property_dict.pop('causality_value_list')
         base_application_set = property_dict['base_application_set']
         # property_dict should include
         #   resource - VAT service or a Component in MRP
@@ -283,9 +279,19 @@ class AmountGeneratorMixin:
           continue
         # Create an Amount object
         # XXX-JPS Could we use a movement for safety ?
-        amount = newTempAmount(portal, property_dict.pop('id'))
+        amount = newTempAmount(portal,
+          # we only want the id to be unique
+          causality_value_list[0].getRelativeUrl().replace('/', '_'))
         amount._setCategoryList(property_dict.pop('category_list', ()))
-        amount._edit(**property_dict)
+        amount._edit(
+          # XXX If they are several cells, we may have duplicate references.
+          reference=self.getReference(),
+          # XXX Are title, int_index and description useful ??
+          title=self.getTitle(),
+          int_index=self.getIntIndex(),
+          description=self.getDescription(),
+          **property_dict)
+        amount._setValueList('causality', causality_value_list)
         if rounding:
           # We hope here that rounding is sufficient at line level
           amount = getRoundingProxy(amount, context=self)
@@ -357,7 +363,7 @@ class AmountGeneratorMixin:
     result_list = []
     for amount in generated_amount_list:
       key = (amount.getPrice(), amount.getEfficiency(),
-             amount.reference, amount.categories)
+             amount.getReference(), amount.categories)
       aggregated_amount = aggregated_amount_dict.get(key)
       if aggregated_amount is None:
         aggregated_amount_dict[key] = amount
-- 
2.30.9