Apply tax roundings on invoice process.
authorGorka Ion Damián <gorkaion.damian@openbravo.com>
Tue, 19 Jun 2012 16:04:26 +0200
changeset 17350 09a2cab4bfac
parent 17349 b4cee0eb216a
child 17351 5679c5a27f26
Apply tax roundings on invoice process.
src-db/database/model/functions/C_INVOICE_POST.xml
--- a/src-db/database/model/functions/C_INVOICE_POST.xml	Tue Jun 19 16:04:08 2012 +0200
+++ b/src-db/database/model/functions/C_INVOICE_POST.xml	Tue Jun 19 16:04:26 2012 +0200
@@ -82,6 +82,7 @@
   v_BPartner_User_ID VARCHAR2(32);
   v_IsSOTrx C_INVOICE.IsSOTrx%TYPE;
   v_Posted C_INVOICE.Posted%TYPE;
+  v_istaxincluded CHAR(1);
   --Added by P.SAROBE
   v_documentno_Settlement VARCHAR2(40);
   v_dateSettlement DATE;
@@ -211,15 +212,19 @@
       i.AD_Client_ID, i.AD_Org_ID, i.UpdatedBy, i.DocumentNo,
       i.C_Order_ID, i.IsSOTrx, i.C_BPartner_ID, i.AD_User_ID,
       i.C_Currency_ID, i.POReference, i.Posted,
-      i.c_Project_Id, i.C_WithHolding_ID, dt.isreturn
+      i.c_Project_Id, i.C_WithHolding_ID, dt.isreturn,
+      pl.istaxincluded
   INTO v_Processing, v_Processed, v_DocAction, v_DocStatus,
       v_DocType_ID, v_DocTypeTarget_ID,
       v_PaymentRule, v_PaymentTerm, v_DateAcct, v_DateInvoiced,
       v_Client_ID, v_Org_ID, v_UpdatedBy, v_DocumentNo,
       v_Order_ID, v_IsSOTrx, v_BPartner_ID, v_BPartner_User_ID,
       v_Currency_ID, v_POReference, v_Posted,
-      v_C_Project_Id, cWithHoldID, v_isreturndoctype
-  FROM C_INVOICE i JOIN c_doctype dt ON i.c_doctypetarget_id = dt.c_doctype_id
+      v_C_Project_Id, cWithHoldID, v_isreturndoctype,
+      v_istaxincluded
+  FROM C_INVOICE i
+        JOIN c_doctype dt ON i.c_doctypetarget_id = dt.c_doctype_id
+        JOIN m_pricelist pl ON i.m_pricelist_id = pl.m_pricelist_id
   WHERE i.C_Invoice_ID=v_Record_ID FOR UPDATE;
   DBMS_OUTPUT.PUT_LINE('Invoice_ID=' || v_Record_ID ||', DocAction=' || v_DocAction || ', DocStatus=' || v_DocStatus || ', DocType_ID=' || v_DocType_ID || ', DocTypeTarget_ID=' || v_DocTypeTarget_ID) ;
   /**
@@ -1205,14 +1210,15 @@
               COALESCE(SUM(l.LineNetAmt),0) + COALESCE(SUM(l.ChargeAmt), 0) AS LineNetAmt,
               COALESCE(SUM(l.TaxBaseAmt), 0) + COALESCE(SUM(l.ChargeAmt), 0) AS TaxBaseAmt,
               COALESCE(SUM(i.ChargeAmt), 0) AS HeaderNet,
-              t.Rate, t.IsSummary, c.StdPrecision, t.Cascade, t.BaseAmount
+              t.Rate, t.IsSummary, c.StdPrecision, t.Cascade, t.BaseAmount,
+              l.line_gross_amount
          FROM C_INVOICE i, C_INVOICELINE l, C_TAX t, C_CURRENCY c
          WHERE i.C_Invoice_ID=l.C_Invoice_ID
            AND i.C_Invoice_ID=v_Record_ID -- Parameter
            AND l.C_Tax_ID=t.C_Tax_ID
            AND i.C_Currency_ID=c.C_Currency_ID
          GROUP BY l.C_InvoiceLine_ID, l.C_Tax_ID,i.IsTaxIncluded, t.Rate, t.IsSummary,
-              c.StdPrecision, t.Cascade, t.BaseAmount
+              c.StdPrecision, t.Cascade, t.BaseAmount, l.line_gross_amount
          ORDER BY 4 DESC
         )
       LOOP
@@ -1223,6 +1229,10 @@
           xTaxBaseAmt:=xTaxBaseAmt + Cur_Tax.HeaderNet;
         END IF;
         C_INVOICELINETAX_INSERT(v_Org_ID, v_Record_ID, Cur_Tax.C_InvoiceLine_ID, v_UpdatedBy, Cur_Tax.C_Tax_ID, Cur_Tax.C_Tax_ID, Cur_Tax.LineNetAmt, Cur_Tax.TaxBaseAmt, Cur_Tax.StdPrecision);
+        IF (v_istaxincluded = 'Y') THEN
+          C_INVOICELINETAX_ROUNDING(cur_tax.c_invoiceline_id, cur_tax.line_gross_amount, cur_tax.linenetamt);
+        END IF;
+
       END LOOP; -- Insert New Taxes
       -- Update Header
       SELECT COALESCE(SUM(TaxAmt),0) INTO v_TaxNoRecalculable
@@ -1236,15 +1246,22 @@
       END IF;
 
       v_GrandTotal:=C_Currency_Round(v_GrandTotal+v_TaxNoRecalculable, v_Currency_ID, NULL) ;
-      SELECT COALESCE(SUM(TAXAMT), 0) INTO v_GrandTotal
-      FROM C_INVOICETAX
-      WHERE C_INVOICE_ID = v_Record_ID;
-      v_GrandTotal:=v_GrandTotal+ v_TotalLines;
+      IF (v_istaxincluded = 'Y') THEN
+        SELECT COALESCE(SUM(line_gross_amount), 0) INTO v_grandtotal
+        FROM c_invoiceline
+        WHERE c_invoice_id = v_record_id;
+      ELSE
+        SELECT COALESCE(SUM(TAXAMT), 0) INTO v_GrandTotal
+        FROM C_INVOICETAX
+        WHERE C_INVOICE_ID = v_Record_ID;
+        v_GrandTotal:=v_GrandTotal+ v_TotalLines;
+      END IF;
       UPDATE C_INVOICE
       SET TotalLines=v_TotalLines,
           GrandTotal=v_GrandTotal,
           withholdingamount = withholdamount
       WHERE C_Invoice_ID=v_Record_ID;
+      C_INVOICETAX_ROUNDING(v_Record_ID, v_GrandTotal, v_TotalLines);
       DBMS_OUTPUT.PUT_LINE('withholdingamount=' || withholdamount);
       DBMS_OUTPUT.PUT_LINE('GrandTotal=' || v_GrandTotal) ;
     END; -- Calculate Tax and Totals