# HG changeset patch
# User Adrián Romero <adrianromero@openbravo.com>
# Date 1420813341 -3600
# Branch bomtaxes
# Node ID 430198b3953a08264ff6e681236ba699c4be4933
# Parent  132fb37a6c25edf69060c5ab2840ee882a7b9bd0
Fixes issue 0028514: Problem when calculating cascade taxes in price including taxes
Rounding adjustment is applied on greater tax amount independently on the taxbase, and it is ajusted the tax amount of the greater tax amount and the net of son taxes.
PL/SQL implementation

diff --git a/src-db/database/model/functions/C_INVOICELINETAX_ROUNDING.xml b/src-db/database/model/functions/C_INVOICELINETAX_ROUNDING.xml
--- a/src-db/database/model/functions/C_INVOICELINETAX_ROUNDING.xml
+++ b/src-db/database/model/functions/C_INVOICELINETAX_ROUNDING.xml
@@ -44,15 +44,15 @@
   WHERE c_invoiceline_id = p_invoiceline_id and line > p_linefrom;
   IF (v_expected_tax_amt <> v_current_tax_amt) THEN
     FOR cur_invoicelinetax IN (
-      SELECT c_invoicelinetax_id
+      SELECT c_invoicelinetax_id, c_tax_id
       FROM c_invoicelinetax
       WHERE c_invoiceline_id = p_invoiceline_id and line > p_linefrom
-      and not exists (select tb.c_tax_id from c_tax tb where tb.c_taxbase_id = c_invoicelinetax.c_tax_id)
       ORDER BY taxamt desc
     ) LOOP
       UPDATE c_invoicelinetax
       SET taxamt = taxamt - (v_current_tax_amt - v_expected_tax_amt)
       WHERE c_invoicelinetax_id  = cur_invoicelinetax.c_invoicelinetax_id ;
+      C_INVOICELINETAX_ROUNDING_AMT(p_invoiceline_id, cur_invoicelinetax.c_tax_id, v_expected_tax_amt - v_current_tax_amt, p_linefrom);
       EXIT;
     END LOOP;
   END IF;
diff --git a/src-db/database/model/functions/C_INVOICELINETAX_ROUNDING_AMT.xml b/src-db/database/model/functions/C_INVOICELINETAX_ROUNDING_AMT.xml
new file mode 100644
--- /dev/null
+++ b/src-db/database/model/functions/C_INVOICELINETAX_ROUNDING_AMT.xml
@@ -0,0 +1,57 @@
+<?xml version="1.0"?>
+  <database name="FUNCTION C_INVOICELINETAX_ROUNDING_AMT">
+    <function name="C_INVOICELINETAX_ROUNDING_AMT" type="NULL">
+      <parameter name="p_invoiceline_id" type="VARCHAR" mode="in">
+        <default/>
+      </parameter>
+      <parameter name="p_tax_id" type="VARCHAR" mode="in">
+        <default/>
+      </parameter>
+      <parameter name="p_diff_amount" type="NUMERIC" mode="in">
+        <default/>
+      </parameter>
+      <parameter name="p_linefrom" type="NUMERIC" mode="in">
+        <default/>
+      </parameter>
+      <body><![CDATA[/*************************************************************************
+* The contents of this file are subject to the Openbravo  Public  License
+* Version  1.1  (the  "License"),  being   the  Mozilla   Public  License
+* Version 1.1  with a permitted attribution clause; you may not  use this
+* file except in compliance with the License. You  may  obtain  a copy of
+* the License at http://www.openbravo.com/legal/license.html
+* Software distributed under the License  is  distributed  on  an "AS IS"
+* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
+* License for the specific  language  governing  rights  and  limitations
+* under the License.
+* The Original Code is Openbravo ERP.
+* The Initial Developer of the Original Code is Openbravo SLU
+* All portions are Copyright (C) 2015 Openbravo SLU
+* All Rights Reserved.
+* Contributor(s):  ______________________________________.
+************************************************************************/
+-- Variables
+  v_ResultStr VARCHAR(2000):='';
+  TYPE RECORD IS REF CURSOR;
+  cur_invoicelinetax RECORD;
+  
+BEGIN --BODY
+  FOR cur_invoicelinetax IN (
+    SELECT c_invoicelinetax_id, c_tax_id
+    FROM c_invoicelinetax
+    WHERE  c_invoiceline_id = p_invoiceline_id and line > p_linefrom
+    AND c_tax_id = (SELECT tb.c_tax_id FROM c_tax tb WHERE tb.c_taxbase_id = p_tax_id)
+  ) LOOP
+      UPDATE c_invoicelinetax
+      SET taxbaseamt = taxbaseamt + p_diff_amount
+      WHERE c_invoicelinetax_id  = cur_invoicelinetax.c_invoicelinetax_id;
+      C_INVOICELINETAX_ROUNDING_AMT(p_invoiceline_id, cur_invoicelinetax.c_tax_id, p_diff_amount, p_linefrom);
+  END LOOP;
+EXCEPTION
+  WHEN OTHERS THEN
+  v_ResultStr:= '@ERROR=' || SQLERRM;
+  DBMS_OUTPUT.PUT_LINE(v_ResultStr) ;
+  RAISE;
+END C_INVOICELINETAX_ROUNDING_AMT
+]]></body>
+    </function>
+  </database>
diff --git a/src-db/database/model/functions/C_ORDERLINETAX_ROUNDING.xml b/src-db/database/model/functions/C_ORDERLINETAX_ROUNDING.xml
--- a/src-db/database/model/functions/C_ORDERLINETAX_ROUNDING.xml
+++ b/src-db/database/model/functions/C_ORDERLINETAX_ROUNDING.xml
@@ -44,15 +44,15 @@
   WHERE c_orderline_id = p_orderline_id and line > p_linefrom;
   IF (v_expected_tax_amt <> v_current_tax_amt) THEN
     FOR cur_orderlinetax IN (
-      SELECT c_orderlinetax_id
+      SELECT c_orderlinetax_id, c_tax_id
       FROM c_orderlinetax
       WHERE c_orderline_id = p_orderline_id and line > p_linefrom
-      and not exists (select tb.c_tax_id from c_tax tb where tb.c_taxbase_id = c_orderlinetax.c_tax_id)
       ORDER BY taxamt desc
     ) LOOP
       UPDATE c_orderlinetax
       SET taxamt = taxamt - (v_current_tax_amt - v_expected_tax_amt)
       WHERE c_orderlinetax_id  = cur_orderlinetax.c_orderlinetax_id ;
+      C_ORDERLINETAX_ROUNDING_AMT(p_orderline_id, cur_orderlinetax.c_tax_id, v_expected_tax_amt - v_current_tax_amt, p_linefrom);
       EXIT;
     END LOOP;
   END IF;
diff --git a/src-db/database/model/functions/C_ORDERLINETAX_ROUNDING_AMT.xml b/src-db/database/model/functions/C_ORDERLINETAX_ROUNDING_AMT.xml
new file mode 100644
--- /dev/null
+++ b/src-db/database/model/functions/C_ORDERLINETAX_ROUNDING_AMT.xml
@@ -0,0 +1,57 @@
+<?xml version="1.0"?>
+  <database name="FUNCTION C_ORDERLINETAX_ROUNDING_AMT">
+    <function name="C_ORDERLINETAX_ROUNDING_AMT" type="NULL">
+      <parameter name="p_orderline_id" type="VARCHAR" mode="in">
+        <default/>
+      </parameter>
+      <parameter name="p_tax_id" type="VARCHAR" mode="in">
+        <default/>
+      </parameter>
+      <parameter name="p_diff_amount" type="NUMERIC" mode="in">
+        <default/>
+      </parameter>
+      <parameter name="p_linefrom" type="NUMERIC" mode="in">
+        <default/>
+      </parameter>
+      <body><![CDATA[/*************************************************************************
+* The contents of this file are subject to the Openbravo  Public  License
+* Version  1.1  (the  "License"),  being   the  Mozilla   Public  License
+* Version 1.1  with a permitted attribution clause; you may not  use this
+* file except in compliance with the License. You  may  obtain  a copy of
+* the License at http://www.openbravo.com/legal/license.html
+* Software distributed under the License  is  distributed  on  an "AS IS"
+* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
+* License for the specific  language  governing  rights  and  limitations
+* under the License.
+* The Original Code is Openbravo ERP.
+* The Initial Developer of the Original Code is Openbravo SLU
+* All portions are Copyright (C) 2015 Openbravo SLU
+* All Rights Reserved.
+* Contributor(s):  ______________________________________.
+************************************************************************/
+-- Variables
+  v_ResultStr VARCHAR(2000):='';
+  TYPE RECORD IS REF CURSOR;
+  cur_orderlinetax RECORD;
+  
+BEGIN --BODY
+  FOR cur_orderlinetax IN (
+    SELECT c_orderlinetax_id, c_tax_id
+    FROM c_orderlinetax
+    WHERE  c_orderline_id = p_orderline_id and line > p_linefrom
+    AND c_tax_id = (SELECT tb.c_tax_id FROM c_tax tb WHERE tb.c_taxbase_id = p_tax_id)
+  ) LOOP
+      UPDATE c_orderlinetax
+      SET taxbaseamt = taxbaseamt + p_diff_amount
+      WHERE c_orderlinetax_id  = cur_orderlinetax.c_orderlinetax_id;
+      C_ORDERLINETAX_ROUNDING_AMT(p_orderline_id, cur_orderlinetax.c_tax_id, p_diff_amount, p_linefrom);
+  END LOOP;
+EXCEPTION
+  WHEN OTHERS THEN
+  v_ResultStr:= '@ERROR=' || SQLERRM;
+  DBMS_OUTPUT.PUT_LINE(v_ResultStr) ;
+  RAISE;
+END C_ORDERLINETAX_ROUNDING_AMT
+]]></body>
+    </function>
+  </database>
