From 7a9c9690141a041ff8eff252665b0f3503ae5807 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?J=C3=A9rome=20Perrin?= <jerome@nexedi.com>
Date: Thu, 17 Apr 2008 18:18:20 +0000
Subject: [PATCH] Order_applyTradeCondition: * copy payment condition
 recursivly from all trade conditions * initialize discount line * immediatly
 calculate discount / tax line if the order already contains some movements
 when the trade condition is applied

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@20635 20353a03-c40f-0410-a6d1-a30d3c3de9de
---
 .../erp5_trade/Order_applyTradeCondition.xml  | 68 ++++++++++++++-----
 bt5/erp5_trade/bt/revision                    |  2 +-
 2 files changed, 53 insertions(+), 17 deletions(-)

diff --git a/bt5/erp5_trade/SkinTemplateItem/portal_skins/erp5_trade/Order_applyTradeCondition.xml b/bt5/erp5_trade/SkinTemplateItem/portal_skins/erp5_trade/Order_applyTradeCondition.xml
index f8cbfe918d..8d2b8974ae 100644
--- a/bt5/erp5_trade/SkinTemplateItem/portal_skins/erp5_trade/Order_applyTradeCondition.xml
+++ b/bt5/erp5_trade/SkinTemplateItem/portal_skins/erp5_trade/Order_applyTradeCondition.xml
@@ -100,20 +100,50 @@ for category in category_list:\n
     if v:\n
       new_category_dict[category] = v\n
 \n
-def copyObjects(portal_type):\n
-  # TODO: iterate on specialise tree\n
-  filter_dict = {\'portal_type\': portal_type}\n
-  if len(order.contentIds(filter=filter_dict)) == 0:\n
-    to_copy = trade_condition.contentIds(filter=filter_dict)\n
-    if len(to_copy) > 0 :\n
-      copy_data = trade_condition.manage_copyObjects(ids=to_copy)\n
-      order.manage_pasteObjects(copy_data)\n
 \n
-# copy payments if none exist\n
-copyObjects(\'Payment Condition\')\n
+def copyPaymentCondition(order, trade_condition):\n
+  filter_dict = {\'portal_type\': \'Payment Condition\'}\n
+  to_copy = trade_condition.contentIds(filter=filter_dict)\n
+  if len(to_copy) > 0 :\n
+    copy_data = trade_condition.manage_copyObjects(ids=to_copy)\n
+    order.manage_pasteObjects(copy_data)\n
+  for other_trade_condition in trade_condition.getSpecialiseValueList():\n
+    copyPaymentCondition(order, other_trade_condition)\n
+  \n
+filter_dict = {\'portal_type\': \'Payment Condition\'}\n
+if len(order.contentIds(filter=filter_dict)) == 0:\n
+  copyPaymentCondition(order, trade_condition)\n
+\n
+# initialise discount lines\n
+def initialiseDiscountLineList(order, trade_condition):\n
+  for discount_model_line in trade_condition.contentValues(\n
+                                             portal_type=\'Discount Model Line\'):\n
+    discount_model_line_reference = tax_model_line.getReference()\n
+    if not discount_model_line_reference or discount_model_line_reference not in [\n
+            x.getProperty(\'reference\') for x in\n
+            order.contentValues(portal_type=\'Discount Line\')]:\n
+      discount_line = order.newContent(\n
+            portal_type=\'Discount Line\',\n
+            resource=discount_model_line.getResource(),\n
+            reference=discount_model_line_reference,\n
+            float_index=discount_model_line.getFloatIndex(),\n
+            base_application_list=discount_model_line.getBaseApplicationList(),\n
+            base_contribution_list=discount_model_line.getBaseContributionList(),\n
+            quantity=0,\n
+            price=0,)\n
+      # calculation_script\n
+      if discount_model_line.getProperty(\'calculation_script_id\'):\n
+        raise NotImplementedError\n
+      # fixed_amount\n
+      if discount_model_line.getQuantity():\n
+        raise NotImplementedError\n
+      # ratio\n
+      discount_line.setPrice(discount_model_line.getEfficiency())\n
+  \n
+  # recurse on other trade conditions\n
+  for specialised_trade_condition in trade_condition.getSpecialiseValueList():\n
+    initialiseDiscountLineList(order, specialised_trade_condition)\n
 \n
-# copy discounts if none exist\n
-copyObjects(\'Discount\')\n
 \n
 # initialise tax lines\n
 def initialiseTaxLineList(order, trade_condition):\n
@@ -128,7 +158,6 @@ def initialiseTaxLineList(order, trade_condition):\n
             reference=tax_model_line_reference,\n
             float_index=tax_model_line.getFloatIndex(),\n
             base_application_list=tax_model_line.getBaseApplicationList(),\n
-            # XXX tax model line or tax model line resource ?\n
             base_contribution_list=tax_model_line.getBaseContributionList(),\n
             quantity=0,\n
             price=0,)\n
@@ -145,8 +174,13 @@ def initialiseTaxLineList(order, trade_condition):\n
   for specialised_trade_condition in trade_condition.getSpecialiseValueList():\n
     initialiseTaxLineList(order, specialised_trade_condition)\n
 \n
-if not len(context.contentValues(portal_type=\'Tax Line\')):\n
+if not len(context.contentValues(portal_type=(\'Discount Line\', \'Tax Line\'))):\n
   initialiseTaxLineList(context, trade_condition)\n
+  initialiseDiscountLineList(context, trade_condition)\n
+ \n
+# if the order already contain lines, immediately update tax line\n
+if order.getMovementList():\n
+  order.Delivery_updateTaxLineList()\n
 \n
 # set specialise\n
 new_category_dict[\'specialise\'] = trade_condition.getRelativeUrl()\n
@@ -214,9 +248,11 @@ order.edit(**new_category_dict)\n
                             <string>_getattr_</string>
                             <string>v</string>
                             <string>_write_</string>
-                            <string>copyObjects</string>
-                            <string>initialiseTaxLineList</string>
+                            <string>copyPaymentCondition</string>
+                            <string>filter_dict</string>
                             <string>len</string>
+                            <string>initialiseDiscountLineList</string>
+                            <string>initialiseTaxLineList</string>
                             <string>_apply_</string>
                           </tuple>
                         </value>
diff --git a/bt5/erp5_trade/bt/revision b/bt5/erp5_trade/bt/revision
index 059361f246..ab760c939a 100644
--- a/bt5/erp5_trade/bt/revision
+++ b/bt5/erp5_trade/bt/revision
@@ -1 +1 @@
-317
\ No newline at end of file
+319
\ No newline at end of file
-- 
2.30.9