Fixed issue 27580 fixed issue to cash vat invoices
authorSandra Huguet <sandra.huguet@openbravo.com>
Tue, 23 Dec 2014 18:53:13 +0100
changeset 25602 fbc674d56f04
parent 25601 263028744507
child 25603 73cd159d83fb
Fixed issue 27580 fixed issue to cash vat invoices

Fixed wrong journal entries when you void a cash vat sales invoice
and fixed total prepayment amount
src-db/database/model/triggers/C_INVOICELINE_TRG2.xml
src/org/openbravo/erpCommon/ad_actionButton/CreateFrom.java
src/org/openbravo/erpCommon/ad_actionButton/CreateFrom_Invoice_data.xsql
src/org/openbravo/erpCommon/ad_forms/DocInvoice.java
--- a/src-db/database/model/triggers/C_INVOICELINE_TRG2.xml	Tue Dec 16 23:22:21 2014 +0530
+++ b/src-db/database/model/triggers/C_INVOICELINE_TRG2.xml	Tue Dec 23 18:53:13 2014 +0100
@@ -149,7 +149,7 @@
      GrandTotal = CASE v_istaxincluded
                        WHEN 'Y' THEN grandtotal - v_oldgrossamt
                        ELSE TotalLines - v_oldLine + v_newLineNetAmt + COALESCE(v_taxAmt, 0)
-                  END,  prepaymentamt=prepaymentamt - v_oldLine + v_newLineNetAmt
+                  END
    --  Updated = SysDate -- Don't update as otherwise it does not save changes
    WHERE C_Invoice_ID=:old.C_Invoice_ID;
   END IF;
--- a/src/org/openbravo/erpCommon/ad_actionButton/CreateFrom.java	Tue Dec 16 23:22:21 2014 +0530
+++ b/src/org/openbravo/erpCommon/ad_actionButton/CreateFrom.java	Tue Dec 23 18:53:13 2014 +0100
@@ -1582,8 +1582,13 @@
               grossAmt = grossAmt.setScale(curPrecision, BigDecimal.ROUND_HALF_UP);
             }
             if (!strPO.equals("")) {
+              String invoiceprepaymentamt = CreateFromInvoiceData.selectInvoicePrepaymentAmt(this,
+                  strKey);
               String prepaymentamt = CreateFromInvoiceData.selectPrepaymentAmt(this, strPO);
-              CreateFromInvoiceData.updatePrepaymentAmt(conn, this, prepaymentamt, strKey);
+              BigDecimal totalprepayment = new BigDecimal(invoiceprepaymentamt).add(new BigDecimal(
+                  prepaymentamt));
+              CreateFromInvoiceData.updatePrepaymentAmt(conn, this, totalprepayment.toString(),
+                  strKey);
             }
             String strTaxRate = CreateFromInvoiceData.selectTaxRate(this, C_Tax_ID);
             BigDecimal taxRate = (strTaxRate.equals("") ? new BigDecimal(1) : new BigDecimal(
--- a/src/org/openbravo/erpCommon/ad_actionButton/CreateFrom_Invoice_data.xsql	Tue Dec 16 23:22:21 2014 +0530
+++ b/src/org/openbravo/erpCommon/ad_actionButton/CreateFrom_Invoice_data.xsql	Tue Dec 23 18:53:13 2014 +0100
@@ -911,10 +911,18 @@
     </Sql>
     <Parameter name="cOrderId"/>
   </SqlMethod>
+    <SqlMethod name="selectInvoicePrepaymentAmt" type="preparedStatement" return="String" default="">
+    <SqlMethodComment></SqlMethodComment>
+    <Sql>
+     select prepaymentamt from c_invoice 
+     where c_invoice_id=?
+    </Sql>
+    <Parameter name="cOrderId"/>
+  </SqlMethod>
   <SqlMethod name="updatePrepaymentAmt" type="preparedStatement" connection="true" return="rowCount">
     <SqlMethodComment></SqlMethodComment>
     <Sql><![CDATA[
-      UPDATE C_Invoice SET prepaymentamt = prepaymentamt + TO_NUMBER(?)
+      UPDATE C_Invoice SET prepaymentamt =  TO_NUMBER(?)
       WHERE C_Invoice_ID = ?
     ]]></Sql>
     <Parameter name="prepaymentamt"/>
--- a/src/org/openbravo/erpCommon/ad_forms/DocInvoice.java	Tue Dec 16 23:22:21 2014 +0530
+++ b/src/org/openbravo/erpCommon/ad_forms/DocInvoice.java	Tue Dec 23 18:53:13 2014 +0100
@@ -409,6 +409,9 @@
           conn);
       // TaxDue CR
       log4jDocInvoice.debug("m_taxes.length: " + m_taxes);
+      BigDecimal grossamt = new BigDecimal(Amounts[AMTTYPE_Gross]);
+      BigDecimal prepayment = new BigDecimal(prepaymentamt);
+
       for (int i = 0; m_taxes != null && i < m_taxes.length; i++) {
         // New docLine created to assign C_Tax_ID value to the entry
         DocLine docLine = new DocLine(DocumentType, Record_ID, "");
@@ -418,12 +421,24 @@
         final BigDecimal taxesAmountTotal = new BigDecimal(
             StringUtils.isBlank(m_taxes[i].m_amount) ? "0" : m_taxes[i].m_amount);
         BigDecimal taxToTransAccount = BigDecimal.ZERO;
+        final Currency currency = OBDal.getInstance().get(Currency.class, C_Currency_ID);
+        int precission = currency.getStandardPrecision().intValue();
         if (IsReversal.equals("Y")) {
           if (isCashVAT && m_taxes[i].m_isCashVAT) {
-            percentageFinalAccount = CashVATUtil.calculatePrepaidPercentageForCashVATTax(
-                m_taxes[i].m_C_Tax_ID, Record_ID);
-            taxToTransAccount = CashVATUtil.calculatePercentageAmount(
-                CashVATUtil._100.subtract(percentageFinalAccount), taxesAmountTotal, C_Currency_ID);
+            if ((m_payments == null || m_payments.length == 0)
+                && (m_debt_payments == null || m_debt_payments.length == 0)
+                && (!prepaymentamt.equals("0"))) {
+              percentageFinalAccount = ((prepayment.multiply(new BigDecimal(100))).divide(
+                  grossamt.abs(), precission, RoundingMode.HALF_UP));
+              taxToTransAccount = percentageFinalAccount.multiply(taxesAmountTotal).divide(
+                  new BigDecimal(100), precission, RoundingMode.HALF_UP);
+            } else {
+              percentageFinalAccount = CashVATUtil.calculatePrepaidPercentageForCashVATTax(
+                  m_taxes[i].m_C_Tax_ID, Record_ID);
+              taxToTransAccount = CashVATUtil.calculatePercentageAmount(
+                  CashVATUtil._100.subtract(percentageFinalAccount), taxesAmountTotal,
+                  C_Currency_ID);
+            }
             fact.createLine(docLine, m_taxes[i].getAccount(DocTax.ACCTTYPE_TaxDue_Trans, as, conn),
                 C_Currency_ID, taxToTransAccount.toString(), "", Fact_Acct_Group_ID,
                 nextSeqNo(SeqNo), DocumentType, conn);
@@ -434,10 +449,20 @@
               nextSeqNo(SeqNo), DocumentType, conn);
         } else {
           if (isCashVAT && m_taxes[i].m_isCashVAT) {
-            percentageFinalAccount = CashVATUtil.calculatePrepaidPercentageForCashVATTax(
-                m_taxes[i].m_C_Tax_ID, Record_ID);
-            taxToTransAccount = CashVATUtil.calculatePercentageAmount(
-                CashVATUtil._100.subtract(percentageFinalAccount), taxesAmountTotal, C_Currency_ID);
+            if ((m_payments == null || m_payments.length == 0)
+                && (m_debt_payments == null || m_debt_payments.length == 0)
+                && (!prepaymentamt.equals("0"))) {
+              percentageFinalAccount = ((prepayment.multiply(new BigDecimal(100))).divide(
+                  grossamt.abs(), precission, RoundingMode.HALF_UP));
+              taxToTransAccount = percentageFinalAccount.multiply(taxesAmountTotal).divide(
+                  new BigDecimal(100), precission, RoundingMode.HALF_UP);
+            } else {
+              percentageFinalAccount = CashVATUtil.calculatePrepaidPercentageForCashVATTax(
+                  m_taxes[i].m_C_Tax_ID, Record_ID);
+              taxToTransAccount = CashVATUtil.calculatePercentageAmount(
+                  CashVATUtil._100.subtract(percentageFinalAccount), taxesAmountTotal,
+                  C_Currency_ID);
+            }
             fact.createLine(docLine, m_taxes[i].getAccount(DocTax.ACCTTYPE_TaxDue_Trans, as, conn),
                 C_Currency_ID, "", taxToTransAccount.toString(), Fact_Acct_Group_ID,
                 nextSeqNo(SeqNo), DocumentType, conn);