From 688607335f6536a5c966ae464f5ac2f552a19453 Mon Sep 17 00:00:00 2001
From: Fabien Morin <fabien@nexedi.com>
Date: Wed, 8 Jul 2009 13:33:36 +0000
Subject: [PATCH] fix some mistakes, improve code : - cartesianProduct is not
 needed : getCellKeyList can do the same thing in a better and cleaner way -
 if movements already exists, we don't want to take only the first one, but
 all movements are needed - improve error message to display the Line (title
 and relative_url) and the coordinates of the not found cell. This will make
 debugging much more easier - to set quantity on new created movements, we
 search on movement_list if movements contribute to the current movement
 applied on, but we need to look also on already processed movements
 (current_aggregated_amount_list) - the condition to check if the quantity of
 the current movement should be updated was wrong, fix it - in TradeCondition,
 now one loop turn is enought to make all calculation - change some variables
 names that where already used previously to avoid mistakes

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@28024 20353a03-c40f-0410-a6d1-a30d3c3de9de
---
 product/ERP5/Document/TradeCondition.py | 21 +++++++--------
 product/ERP5/Document/TradeModelLine.py | 36 ++++++++++++-------------
 2 files changed, 27 insertions(+), 30 deletions(-)

diff --git a/product/ERP5/Document/TradeCondition.py b/product/ERP5/Document/TradeCondition.py
index 21579443e9..2d293c1bd5 100644
--- a/product/ERP5/Document/TradeCondition.py
+++ b/product/ERP5/Document/TradeCondition.py
@@ -222,23 +222,22 @@ class TradeCondition(Path, Transformation, XMLMatrix):
 
       # initialise run then rerun only once, as trade_model_line_composed_list
       # is sorted in good way to have simple algorithm
-      for pass_type in ['initialise', 'rerun']:
-        for model_line in trade_model_line_composed_list:
-          result.extend(model_line.getAggregatedAmountList(context,
-            movement_list=movement_list,
-            current_aggregated_amount_list=result,
-            **kw))
-        movement_list = result # apply model again on generated movements
+      for model_line in trade_model_line_composed_list:
+        result.extend(model_line.getAggregatedAmountList(context,
+          movement_list=movement_list,
+          current_aggregated_amount_list=result,
+          **kw))
+      movement_list = result # apply model again on generated movements
 
       # remove movement that should not be created
-      movement_list = []
-      for movement in result:
+      final_movement_list = []
+      for movement in movement_list:
         movement_ref = movement.getReference()
         for model_line in trade_model_line_composed_list:
           if model_line.getReference() == movement_ref and\
               model_line.isCreateLine():
-            movement_list.append(movement)
-      return movement_list
+            final_movement_list.append(movement)
+      return final_movement_list
 
     security.declareProtected( Permissions.AccessContentsInformation, 'getCell')
     def getCell(self, *kw , **kwd):
diff --git a/product/ERP5/Document/TradeModelLine.py b/product/ERP5/Document/TradeModelLine.py
index 63d9694915..2177f0a372 100644
--- a/product/ERP5/Document/TradeModelLine.py
+++ b/product/ERP5/Document/TradeModelLine.py
@@ -33,7 +33,6 @@ from Products.ERP5Type import Permissions, PropertySheet, interfaces
 from Products.ERP5Type.XMLMatrix import XMLMatrix
 from Products.ERP5.Document.Amount import Amount
 from Products.ERP5.Document.Predicate import Predicate
-from Products.ERP5Type.Utils import cartesianProduct
 from Products.ERP5.AggregatedAmountList import AggregatedAmountList
 import zope.interface
 
@@ -129,8 +128,6 @@ class TradeModelLine(Predicate, XMLMatrix, Amount):
     tmp_movement_list = [q for q in current_aggregated_amount_list \
         if q.getReference() == self.getReference()]
     if len(tmp_movement_list) > 0:
-      tmp_movement_list = tmp_movement_list[:1] # list is needed in case of
-                                                # having cells
       update = 1
     else:
       # get source and destination using Business Process
@@ -195,19 +192,17 @@ class TradeModelLine(Predicate, XMLMatrix, Amount):
 
       update = 0
       base_category_list = self.getVariationBaseCategoryList()
-      category_list_list = []
-      for base_cat in base_category_list:
-        category_list = self.getVariationCategoryList(
-                                        base_category_list=base_cat)
-        category_list_list.append(category_list)
-      cartesian_product = cartesianProduct(category_list_list)
-      # look for cells if categories are used
-      if len(category_list_list) > 0:
-        for cell_coordinates in cartesian_product:
+      # get cells categories cartesian product
+      cell_key_list = self.getCellKeyList(base_id='movement')
+      if len(cell_key_list) > 0:
+        # look for cells
+        for cell_coordinates in cell_key_list:
           cell = self.getCell(base_id=base_id, *cell_coordinates)
           if cell is None:
-            raise ValueError("Can't find the cell corresponding to those "+\
-                "cells coordinates : %s" % cell_coordinates)
+            raise ValueError("Line '%s' (%s) can't find the cell corresponding"+\
+                " to those cells coordinates : %s" % (self.getTitle(),
+                                                      self.getRelativeUrl(),
+                                                      cell_coordinates))
           tmp_movement = newTempSimulationMovement(self.getPortalObject(),
               self_id)
           tmp_movement.edit(
@@ -232,14 +227,17 @@ class TradeModelLine(Predicate, XMLMatrix, Amount):
           self.getQuantity(None) is None or \
           len(self.getVariationCategoryList()) and \
           tmp_movement.getQuantity(None) is None:
-        # if the quantity is not defined, take it by searching all movements
-        # that used this base_amount
-        for movement in movement_list:
+        for movement in movement_list + current_aggregated_amount_list:
+          # here we need to look on movement_list and also on already processed
+          # movements (current_aggregated_amount_list).
+          # if the quantity is not defined, take it by searching all movements
+          # that used this base_amount
           if set(base_application_list)\
               .intersection(set(movement.getBaseContributionList())) and \
-              len(movement.getVariationCategoryList()) == 0 or \
+              (len(movement.getVariationCategoryList()) == 0 or \
+               len(tmp_movement.getVariationCategoryList()) == 0 or \
               set(movement.getVariationCategoryList()).intersection( \
-              set(tmp_movement.getVariationCategoryList())):
+              set(tmp_movement.getVariationCategoryList()))):
             # at least one base application is in base contributions and
             # if the movement have no variation category, it's the same as
             # if he have all variation categories
-- 
2.30.9