From fb5d44b35d0ccfd57efefde99a80bc8f1a0c8746 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?J=C3=A9rome=20Perrin?= <jerome@nexedi.com>
Date: Wed, 14 Jun 2006 08:17:17 +0000
Subject: [PATCH] fix some problems when having lines from the same resource

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@7713 20353a03-c40f-0410-a6d1-a30d3c3de9de
---
 .../SaleInvoiceTransaction_getVAT.xml         | 63 +++++++++++--------
 1 file changed, 38 insertions(+), 25 deletions(-)

diff --git a/bt5/erp5_accounting/SkinTemplateItem/portal_skins/erp5_accounting/SaleInvoiceTransaction_getVAT.xml b/bt5/erp5_accounting/SkinTemplateItem/portal_skins/erp5_accounting/SaleInvoiceTransaction_getVAT.xml
index a9a3394942..a8c2d4c79d 100644
--- a/bt5/erp5_accounting/SkinTemplateItem/portal_skins/erp5_accounting/SaleInvoiceTransaction_getVAT.xml
+++ b/bt5/erp5_accounting/SkinTemplateItem/portal_skins/erp5_accounting/SaleInvoiceTransaction_getVAT.xml
@@ -70,10 +70,23 @@
             <key> <string>_body</string> </key>
             <value> <string encoding="cdata"><![CDATA[
 
-""" Calculates the VAT for this invoice. \n
-FIXME: it should support :\n
-    o multiple VAT lines (with differents ratio)\n
-    o other taxes that VAT\n
+"""Calculates the VAT for this invoice, returning a dict with keys:\n
+\n
+ * total: The amount of VAT\n
+ * ratio: The ratio of the VAT\n
+ * title: The title of this VAT, actually this is the title of the\n
+   account used in the invoice transaction rule.\n
+\n
+This scripts first look in the accounting lines related to this invoice, and\n
+uses the sum of all accounts of type collected_vat. If nothing is found, it\n
+will look in the simulation tree to find all movements which uses a\n
+collected_vat account as source.\n
+\n
+TODO: It\'s now clear that this script now needs a complete rewrite, because it\n
+doesn\'t support different VAT rates for different lines and retrieving the VAT\n
+rate associated with an invoice line.\n
+\n
+This API will probably change.\n
 """\n
 \n
 vat_infos = {\n
@@ -82,27 +95,26 @@ vat_infos = {\n
 \n
 invoice = context\n
 portal = context.getPortalObject()\n
-accounting_movement_type_list =\\\n
-          portal.getPortalAccountingMovementTypeList()\n
 accounting_movement_list = invoice.getMovementList(\n
-          portal_type=accounting_movement_type_list)\n
+          portal_type=portal.getPortalAccountingMovementTypeList())\n
 \n
 # 1) the invoice contains accounting lines.\n
-if len(accounting_movement_list) :\n
-  for movement in accounting_movement_list :\n
-    account = movement.getSourceValue()\n
+if len(accounting_movement_list):\n
+  for movement in accounting_movement_list:\n
+    account = movement.getSourceValue(portal_type=\'Account\')\n
     if account is None : continue\n
     if account.getAccountTypeId() == \'collected_vat\' :\n
       vat_infos.setdefault(\'title\', account.getTitle())\n
-      vat_infos.setdefault(\'ratio\', round(movement.getQuantity() /\n
+      vat_infos.update({\'total\': vat_infos[\'total\'] + movement.getQuantity()})\n
+  vat_infos[\'ratio\'] = vat_infos[\'total\'] /\\\n
         (float(invoice.getTotalPrice(\n
-                  portal_type=portal.getPortalInvoiceMovementTypeList())) or 1),\n
-        2)) # XXX this calls catalog => bad & this does not suport multiple VAT lines either\n
-      vat_infos.update( { \'total\': vat_infos[\'total\'] + movement.getQuantity() } )\n
+                  portal_type=portal.getPortalInvoiceMovementTypeList(),\n
+                  fast=0)) or 1)\n
 \n
 # 2) no accounting lines => we must look in the simulation\n
 if not vat_infos.has_key(\'title\') :\n
-  for invoice_movement in invoice.getMovementList(portal_type=portal.getPortalInvoiceMovementTypeList() ) :\n
+  for invoice_movement in invoice.getMovementList(\n
+                      portal_type=portal.getPortalInvoiceMovementTypeList() ) :\n
     aggregated_simulation_movements = {}\n
     for simulation_movement in ( invoice_movement.getDeliveryRelatedValueList(\n
                                       portal_type = \'Simulation Movement\')  +\n
@@ -116,15 +128,18 @@ if not vat_infos.has_key(\'title\') :\n
       for applied_rule in simulation_movement.objectValues():\n
         # inside we got accounting movements\n
         for movement in applied_rule.objectValues() :\n
-          account = movement.getSourceValue()\n
+          account = movement.getSourceValue(portal_type=\'Account\')\n
           if account is None : continue\n
           if account.getAccountTypeId() == \'collected_vat\':\n
             vat_infos.setdefault(\'title\', account.getTitle())\n
-            vat_infos.setdefault(\'ratio\', round(movement.getQuantity() / \n
-                                               (float(invoice_movement.getTotalPrice()) or 1),\n
-                                          2)) # XXX this does not suport multiple VAT lines\n
-            vat_infos.update( { \'total\': vat_infos[\'total\'] + movement.getQuantity() } )\n
-\n
+            vat_infos.update( { \'total\':\n
+                    vat_infos[\'total\'] + movement.getQuantity() } )\n
+  \n
+  vat_infos[\'ratio\'] = vat_infos[\'total\'] /\\\n
+        (float(invoice.getTotalPrice(\n
+                  portal_type=portal.getPortalInvoiceMovementTypeList(),\n
+                  fast=0)) or 1)\n
+        \n
 vat_infos.setdefault(\'title\', "")\n
 vat_infos.setdefault(\'ratio\', 0)\n
 return vat_infos\n
@@ -175,20 +190,18 @@ return vat_infos\n
                             <string>invoice</string>
                             <string>_getattr_</string>
                             <string>portal</string>
-                            <string>accounting_movement_type_list</string>
                             <string>accounting_movement_list</string>
                             <string>len</string>
                             <string>_getiter_</string>
                             <string>movement</string>
                             <string>account</string>
                             <string>None</string>
-                            <string>round</string>
-                            <string>float</string>
                             <string>_getitem_</string>
+                            <string>float</string>
+                            <string>_write_</string>
                             <string>invoice_movement</string>
                             <string>aggregated_simulation_movements</string>
                             <string>simulation_movement</string>
-                            <string>_write_</string>
                             <string>applied_rule</string>
                           </tuple>
                         </value>
-- 
2.30.9