Fixes Issue 26764:Amt posted to 'Tax Transitory' acct should be recalculated
authorAtul Gaware <atul.gaware@openbravo.com>
Tue, 29 Jul 2014 23:00:41 +0530
changeset 24133 6c1baf6523cb
parent 24132 e16f466cacf3
child 24134 7ad630b1df1f
Fixes Issue 26764:Amt posted to 'Tax Transitory' acct should be recalculated
when posting payment/transaction/reconciliation
src/org/openbravo/erpCommon/ad_forms/Fact.java
src/org/openbravo/erpCommon/utility/CashVATUtil.java
--- a/src/org/openbravo/erpCommon/ad_forms/Fact.java	Tue Jul 29 18:42:40 2014 +0200
+++ b/src/org/openbravo/erpCommon/ad_forms/Fact.java	Tue Jul 29 23:00:41 2014 +0530
@@ -11,7 +11,7 @@
  * Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke, parts
  * created by ComPiere are Copyright (C) ComPiere, Inc.;   All Rights Reserved.
  * Contributor(s): Openbravo SLU
- * Contributions are Copyright (C) 2001-2011 Openbravo S.L.U.
+ * Contributions are Copyright (C) 2001-2014 Openbravo S.L.U.
  ******************************************************************************
  */
 package org.openbravo.erpCommon.ad_forms;
@@ -34,6 +34,10 @@
   /** Document */
   private AcctServer m_doc = null;
 
+  public AcctServer getM_doc() {
+    return m_doc;
+  }
+
   /** Accounting Schema */
   private AcctSchema m_acctSchema = null;
 
@@ -205,8 +209,8 @@
       // If this is a manual entry then we need to recompute Amounts which were set in loadLines for
       // GL Journal Document
       if ("GLJ".equals(DocBaseType)) {
-        docLine.setConvertedAmt(docLine.m_C_AcctSchema_ID, DebitAmt.toString(),
-            CreditAmt.toString());
+        docLine.setConvertedAmt(docLine.m_C_AcctSchema_ID, DebitAmt.toString(), CreditAmt
+            .toString());
       }
       if (strNegate.equals("N") && (DebitAmt.compareTo(ZERO) < 0 || CreditAmt.compareTo(ZERO) < 0)) {
         return createLine(docLine, account, C_Currency_ID, CreditAmt.abs().toString(), DebitAmt
@@ -249,8 +253,8 @@
     if (conversionRate != null) {
       line.convertByRate(m_acctSchema.getC_Currency_ID(), conversionRate);
     } else {
-      line.convert(m_acctSchema.getC_Currency_ID(), conversionDate,
-          m_acctSchema.getCurrencyRateType(), conn);
+      line.convert(m_acctSchema.getC_Currency_ID(), conversionDate, m_acctSchema
+          .getCurrencyRateType(), conn);
     }
     // Optionally overwrite Acct Amount
     if (docLine != null && !docLine.m_AmtAcctDr.equals("") && !docLine.m_AmtAcctCr.equals(""))
@@ -409,8 +413,8 @@
       // positive balance => CR
       line.setAmtSource(m_doc.C_Currency_ID, ZERO.toString(), diff.toString());
     // Convert
-    line.convert(m_acctSchema.getC_Currency_ID(), m_doc.DateAcct,
-        m_acctSchema.getCurrencyRateType(), conn);
+    line.convert(m_acctSchema.getC_Currency_ID(), m_doc.DateAcct, m_acctSchema
+        .getCurrencyRateType(), conn);
     line.setAccount(m_acctSchema, m_acctSchema.getSuspenseBalancing_Acct());
     //
     log4jFact.debug("balanceSource - ");
@@ -584,8 +588,8 @@
             line.setAmtSource(m_doc.C_Currency_ID, ZERO.toString(), diff.abs().toString());
             line.setAccount(m_acctSchema, m_acctSchema.m_DueTo_Acct);
           }
-          line.convert(m_acctSchema.getC_Currency_ID(), m_doc.DateAcct,
-              m_acctSchema.getCurrencyRateType(), conn);
+          line.convert(m_acctSchema.getC_Currency_ID(), m_doc.DateAcct, m_acctSchema
+              .getCurrencyRateType(), conn);
           line.setAD_Org_ID(key);
           log4jFact.debug("balanceSegment (" + segmentType + ") - ");
           log4jFact.debug("************* fact - balanceSegment - m_lines.size() - "
@@ -657,8 +661,8 @@
 
       // Amount
       line.setAmtSource(m_doc.C_Currency_ID, ZERO.toString(), ZERO.toString());
-      line.convert(m_acctSchema.getC_Currency_ID(), m_doc.DateAcct,
-          m_acctSchema.getCurrencyRateType(), conn);
+      line.convert(m_acctSchema.getC_Currency_ID(), m_doc.DateAcct, m_acctSchema
+          .getCurrencyRateType(), conn);
       if (diff.compareTo(ZERO) < 0)
         line.setAmtAcct(diff.abs().toString(), ZERO.toString());
       else
--- a/src/org/openbravo/erpCommon/utility/CashVATUtil.java	Tue Jul 29 18:42:40 2014 +0200
+++ b/src/org/openbravo/erpCommon/utility/CashVATUtil.java	Tue Jul 29 23:00:41 2014 +0530
@@ -21,16 +21,20 @@
 
 import java.math.BigDecimal;
 import java.math.RoundingMode;
+import java.text.SimpleDateFormat;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
+import javax.servlet.ServletException;
+
 import org.apache.commons.lang.StringUtils;
 import org.apache.log4j.Logger;
 import org.hibernate.Query;
 import org.hibernate.Session;
 import org.hibernate.criterion.Restrictions;
 import org.openbravo.base.provider.OBProvider;
+import org.openbravo.base.session.OBPropertiesProvider;
 import org.openbravo.dal.core.OBContext;
 import org.openbravo.dal.service.OBDal;
 import org.openbravo.dal.service.OBDao;
@@ -443,83 +447,97 @@
   public static String createFactCashVAT(AcctSchema as, ConnectionProvider conn, Fact fact,
       String Fact_Acct_Group_ID, DocLineCashVATReady_PaymentTransactionReconciliation line,
       Invoice invoice, final String documentType, final String cCurrencyID, final String SeqNo) {
-    if (invoice.isCashVAT() && !line.getInvoiceTaxCashVAT_V().isEmpty()) {
-      FactLine factLine2 = null;
-      for (final InvoiceTaxCashVAT_V itcv : line.getInvoiceTaxCashVAT_V()) {
-        final TaxRate tax = itcv.getInvoiceTax().getTax();
-        if (tax.isCashVAT()) {
-          final BigDecimal taxAmt = itcv.getTaxAmount();
-          if (taxAmt.compareTo(BigDecimal.ZERO) != 0) {
-            final DocTax m_tax = new DocTax(tax.getId(), tax.getName(), tax.getRate().toString(),
-                itcv.getInvoiceTax().getTaxableAmount().toString(), itcv.getTaxAmount().toString(),
-                tax.isNotTaxdeductable(), tax.isTaxdeductable());
-            final String invoicedocumentType = invoice.getDocumentType().getDocumentCategory();
-            final boolean isReversal = invoice.getDocumentType().isReversal();
-            // ARI, ARF, ARI_RM
-            if (invoicedocumentType.equals(AcctServer.DOCTYPE_ARInvoice)
-                || invoicedocumentType.equals(AcctServer.DOCTYPE_ARProForma)
-                || invoicedocumentType.equals(AcctServer.DOCTYPE_RMSalesInvoice)) {
-              if (isReversal) {
+    try {
+      if (invoice.isCashVAT() && !line.getInvoiceTaxCashVAT_V().isEmpty()) {
+        FactLine factLine2 = null;
+        for (final InvoiceTaxCashVAT_V itcv : line.getInvoiceTaxCashVAT_V()) {
+          final TaxRate tax = itcv.getInvoiceTax().getTax();
+          if (tax.isCashVAT()) {
+            final BigDecimal taxAmt = itcv.getTaxAmount();
+            if (taxAmt.compareTo(BigDecimal.ZERO) != 0) {
+              final DocTax m_tax = new DocTax(tax.getId(), tax.getName(), tax.getRate().toString(),
+                  itcv.getInvoiceTax().getTaxableAmount().toString(), itcv.getTaxAmount()
+                      .toString(), tax.isNotTaxdeductable(), tax.isTaxdeductable());
+              final String invoicedocumentType = invoice.getDocumentType().getDocumentCategory();
+              final boolean isReversal = invoice.getDocumentType().isReversal();
+              String dateFormatString = OBPropertiesProvider.getInstance().getOpenbravoProperties()
+                  .getProperty("dateFormat.java");
+              SimpleDateFormat dateFormat = new SimpleDateFormat(dateFormatString);
+              String taxAmountConverted = fact
+                  .getM_doc()
+                  .convertAmount(taxAmt, invoice.isSalesTransaction(),
+                      dateFormat.format(invoice.getAccountingDate()), AcctServer.TABLEID_Invoice,
+                      invoice.getId(), invoice.getCurrency().getId(), as.m_C_Currency_ID, line, as,
+                      fact, Fact_Acct_Group_ID, nextSeqNo(SeqNo), conn).toString();
+              // ARI, ARF, ARI_RM
+              if (invoicedocumentType.equals(AcctServer.DOCTYPE_ARInvoice)
+                  || invoicedocumentType.equals(AcctServer.DOCTYPE_ARProForma)
+                  || invoicedocumentType.equals(AcctServer.DOCTYPE_RMSalesInvoice)) {
+                if (isReversal) {
+                  final FactLine factLine1 = fact.createLine(line, m_tax.getAccount(
+                      DocTax.ACCTTYPE_TaxDue_Trans, as, conn), invoice.getCurrency().getId(), "",
+                      taxAmountConverted, Fact_Acct_Group_ID, nextSeqNo(SeqNo), documentType, conn);
+                  factLine2 = fact.createLine(line, m_tax.getAccount(DocTax.ACCTTYPE_TaxDue, as,
+                      conn), invoice.getCurrency().getId(), taxAmt.toString(), "",
+                      Fact_Acct_Group_ID, nextSeqNo(factLine1.m_SeqNo), documentType, conn);
+                } else {
+                  final FactLine factLine1 = fact.createLine(line, m_tax.getAccount(
+                      DocTax.ACCTTYPE_TaxDue_Trans, as, conn), invoice.getCurrency().getId(),
+                      taxAmountConverted, "", Fact_Acct_Group_ID, nextSeqNo(SeqNo), documentType,
+                      conn);
+                  factLine2 = fact.createLine(line, m_tax.getAccount(DocTax.ACCTTYPE_TaxDue, as,
+                      conn), invoice.getCurrency().getId(), "", taxAmt.toString(),
+                      Fact_Acct_Group_ID, nextSeqNo(factLine1.m_SeqNo), documentType, conn);
+                }
+              }// ARC
+              else if (invoicedocumentType.equals(AcctServer.DOCTYPE_ARCredit)) {
                 final FactLine factLine1 = fact.createLine(line, m_tax.getAccount(
                     DocTax.ACCTTYPE_TaxDue_Trans, as, conn), invoice.getCurrency().getId(), "",
-                    taxAmt.toString(), Fact_Acct_Group_ID, nextSeqNo(SeqNo), documentType, conn);
+                    taxAmountConverted, Fact_Acct_Group_ID, nextSeqNo(SeqNo), documentType, conn);
                 factLine2 = fact.createLine(line, m_tax
                     .getAccount(DocTax.ACCTTYPE_TaxDue, as, conn), invoice.getCurrency().getId(),
                     taxAmt.toString(), "", Fact_Acct_Group_ID, nextSeqNo(factLine1.m_SeqNo),
                     documentType, conn);
-              } else {
+              }
+              // API
+              else if (invoicedocumentType.equals(AcctServer.DOCTYPE_APInvoice)) {
+                if (isReversal) {
+                  final FactLine factLine1 = fact.createLine(line, m_tax.getAccount(
+                      DocTax.ACCTTYPE_TaxCredit_Trans, as, conn), invoice.getCurrency().getId(),
+                      taxAmountConverted, "", Fact_Acct_Group_ID, nextSeqNo(SeqNo), documentType,
+                      conn);
+                  factLine2 = fact.createLine(line, m_tax.getAccount(DocTax.ACCTTYPE_TaxCredit, as,
+                      conn), invoice.getCurrency().getId(), "", taxAmt.toString(),
+                      Fact_Acct_Group_ID, nextSeqNo(factLine1.m_SeqNo), documentType, conn);
+                } else {
+                  final FactLine factLine1 = fact.createLine(line,
+                      m_tax.getAccount(DocTax.ACCTTYPE_TaxCredit_Trans, as, conn), cCurrencyID, "",
+                      taxAmountConverted, Fact_Acct_Group_ID, nextSeqNo(SeqNo), documentType, conn);
+                  factLine2 = fact.createLine(line,
+                      m_tax.getAccount(DocTax.ACCTTYPE_TaxCredit, as, conn), cCurrencyID,
+                      taxAmt.toString(), "", Fact_Acct_Group_ID, nextSeqNo(factLine1.m_SeqNo),
+                      documentType, conn);
+                }
+              }
+              // APC
+              else if (invoicedocumentType.equals(AcctServer.DOCTYPE_APCredit)) {
                 final FactLine factLine1 = fact.createLine(line, m_tax.getAccount(
-                    DocTax.ACCTTYPE_TaxDue_Trans, as, conn), invoice.getCurrency().getId(), taxAmt
-                    .toString(), "", Fact_Acct_Group_ID, nextSeqNo(SeqNo), documentType, conn);
-                factLine2 = fact.createLine(line, m_tax
-                    .getAccount(DocTax.ACCTTYPE_TaxDue, as, conn), invoice.getCurrency().getId(),
-                    "", taxAmt.toString(), Fact_Acct_Group_ID, nextSeqNo(factLine1.m_SeqNo),
-                    documentType, conn);
-              }
-            }// ARC
-            else if (invoicedocumentType.equals(AcctServer.DOCTYPE_ARCredit)) {
-              final FactLine factLine1 = fact.createLine(line, m_tax.getAccount(
-                  DocTax.ACCTTYPE_TaxDue_Trans, as, conn), invoice.getCurrency().getId(), "",
-                  taxAmt.toString(), Fact_Acct_Group_ID, nextSeqNo(SeqNo), documentType, conn);
-              factLine2 = fact.createLine(line, m_tax.getAccount(DocTax.ACCTTYPE_TaxDue, as, conn),
-                  invoice.getCurrency().getId(), taxAmt.toString(), "", Fact_Acct_Group_ID,
-                  nextSeqNo(factLine1.m_SeqNo), documentType, conn);
-            }
-            // API
-            else if (invoicedocumentType.equals(AcctServer.DOCTYPE_APInvoice)) {
-              if (isReversal) {
-                final FactLine factLine1 = fact
-                    .createLine(line, m_tax.getAccount(DocTax.ACCTTYPE_TaxCredit_Trans, as, conn),
-                        invoice.getCurrency().getId(), taxAmt.toString(), "", Fact_Acct_Group_ID,
-                        nextSeqNo(SeqNo), documentType, conn);
+                    DocTax.ACCTTYPE_TaxCredit_Trans, as, conn), invoice.getCurrency().getId(),
+                    taxAmountConverted, "", Fact_Acct_Group_ID, nextSeqNo(SeqNo), documentType,
+                    conn);
                 factLine2 = fact.createLine(line, m_tax.getAccount(DocTax.ACCTTYPE_TaxCredit, as,
                     conn), invoice.getCurrency().getId(), "", taxAmt.toString(),
                     Fact_Acct_Group_ID, nextSeqNo(factLine1.m_SeqNo), documentType, conn);
-              } else {
-                final FactLine factLine1 = fact.createLine(line,
-                    m_tax.getAccount(DocTax.ACCTTYPE_TaxCredit_Trans, as, conn), cCurrencyID, "",
-                    taxAmt.toString(), Fact_Acct_Group_ID, nextSeqNo(SeqNo), documentType, conn);
-                factLine2 = fact.createLine(line,
-                    m_tax.getAccount(DocTax.ACCTTYPE_TaxCredit, as, conn), cCurrencyID,
-                    taxAmt.toString(), "", Fact_Acct_Group_ID, nextSeqNo(factLine1.m_SeqNo),
-                    documentType, conn);
               }
             }
-            // APC
-            else if (invoicedocumentType.equals(AcctServer.DOCTYPE_APCredit)) {
-              final FactLine factLine1 = fact.createLine(line, m_tax.getAccount(
-                  DocTax.ACCTTYPE_TaxCredit_Trans, as, conn), invoice.getCurrency().getId(), taxAmt
-                  .toString(), "", Fact_Acct_Group_ID, nextSeqNo(SeqNo), documentType, conn);
-              factLine2 = fact.createLine(line, m_tax.getAccount(DocTax.ACCTTYPE_TaxCredit, as,
-                  conn), invoice.getCurrency().getId(), "", taxAmt.toString(), Fact_Acct_Group_ID,
-                  nextSeqNo(factLine1.m_SeqNo), documentType, conn);
-            }
           }
         }
+        if (factLine2 != null) {
+          return factLine2.m_SeqNo;
+        }
       }
-      if (factLine2 != null) {
-        return factLine2.m_SeqNo;
-      }
+    } catch (ServletException e) {
+      log4j.error("Error ocurring posting cashVAT", e);
     }
     return SeqNo;
   }