Fixes Issue 24848: Closed period verification should not be performed
authorPandeeswari Ramakrishnan <pandeeswari.ramakrishnan@openbravo.com>
Wed, 02 Apr 2014 17:57:02 +0530
changeset 22863 0a17f1fe4f0c
parent 22862 511a09bd0e13
child 22864 a5cfb741acf8
Fixes Issue 24848: Closed period verification should not be performed
when trying to process a document not configured to generate accounting, closed period verification should not be performed.
modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/ad_actionbutton/AddPaymentFromInvoice.java
modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/ad_actionbutton/AddTransaction.html
modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/ad_actionbutton/Reconciliation.java
modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/process/FIN_PaymentProcess.java
modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/process/FIN_TransactionProcess.java
--- a/modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/ad_actionbutton/AddPaymentFromInvoice.java	Wed Apr 02 13:53:38 2014 +0200
+++ b/modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/ad_actionbutton/AddPaymentFromInvoice.java	Wed Apr 02 17:57:02 2014 +0530
@@ -46,6 +46,7 @@
 import org.openbravo.dal.service.OBCriteria;
 import org.openbravo.dal.service.OBDal;
 import org.openbravo.data.FieldProvider;
+import org.openbravo.database.ConnectionProvider;
 import org.openbravo.erpCommon.utility.ComboTableData;
 import org.openbravo.erpCommon.utility.DateTimeData;
 import org.openbravo.erpCommon.utility.FieldProviderFactory;
@@ -58,6 +59,7 @@
 import org.openbravo.model.common.enterprise.DocumentType;
 import org.openbravo.model.common.enterprise.Organization;
 import org.openbravo.model.common.invoice.Invoice;
+import org.openbravo.model.financialmgmt.accounting.FIN_FinancialAccountAccounting;
 import org.openbravo.model.financialmgmt.payment.FIN_FinancialAccount;
 import org.openbravo.model.financialmgmt.payment.FIN_Payment;
 import org.openbravo.model.financialmgmt.payment.FIN_PaymentMethod;
@@ -201,6 +203,12 @@
           strReceivedFromId);
       PriceList priceList = isReceipt ? businessPartner.getPriceList() : businessPartner
           .getPurchasePricelist();
+      FIN_FinancialAccount finAccount = OBDal.getInstance().get(FIN_FinancialAccount.class,
+          strFinancialAccountId);
+      FIN_PaymentMethod finPaymentMethod = OBDal.getInstance().get(FIN_PaymentMethod.class,
+          strPaymentMethodId);
+      boolean paymentDocumentEnabled = getDocumentConfirmation(this, finAccount, finPaymentMethod,
+          isReceipt, strPaymentAmount, true);
       OBError message = null;
       // FIXME: added to access the FIN_PaymentSchedule and FIN_PaymentScheduleDetail tables to be
       // removed when new security implementation is done
@@ -231,12 +239,24 @@
           // parameters.add(null);
           String strDocTypeId = (String) CallStoredProcedure.getInstance().call("AD_GET_DOCTYPE",
               parameters, null);
+          boolean documentEnabled = true;
           String strDocBaseType = parameters.get(2).toString();
           boolean orgLegalWithAccounting = FIN_Utility.periodControlOpened(Invoice.TABLE_NAME,
               strInvoiceId, Invoice.TABLE_NAME + "_ID", "LE");
 
-          if (!FIN_Utility.isPeriodOpen(vars.getClient(), strDocBaseType, strOrgId, strPaymentDate)
-              && orgLegalWithAccounting) {
+          if ((strAction.equals("PRD") || strAction.equals("PPW") || FIN_Utility
+              .isAutomaticDepositWithdrawn(finAccount, finPaymentMethod, isReceipt))
+              && new BigDecimal(strPaymentAmount).compareTo(BigDecimal.ZERO) != 0) {
+            documentEnabled = paymentDocumentEnabled
+                || getDocumentConfirmation(this, finAccount, finPaymentMethod, isReceipt,
+                    strPaymentAmount, false);
+          } else {
+            documentEnabled = paymentDocumentEnabled;
+          }
+
+          if (documentEnabled
+              && !FIN_Utility.isPeriodOpen(vars.getClient(), strDocBaseType, strOrgId,
+                  strPaymentDate) && orgLegalWithAccounting) {
             final OBError myMessage = Utility.translateError(this, vars, vars.getLanguage(),
                 Utility.messageBD(this, "PeriodNotAvailable", vars.getLanguage()));
             vars.setMessage(strTabId, myMessage);
@@ -320,6 +340,69 @@
 
   }
 
+  public boolean getDocumentConfirmation(ConnectionProvider conn, FIN_FinancialAccount finAccount,
+      FIN_PaymentMethod finPaymentMethod, boolean isReceipt, String strPaymentAmount,
+      boolean isPayment) {
+    // Checks if this step is configured to generate accounting for the selected financial account
+    boolean confirmation = false;
+    OBContext.setAdminMode();
+    try {
+      OBCriteria<FinAccPaymentMethod> obCriteria = OBDal.getInstance().createCriteria(
+          FinAccPaymentMethod.class);
+      obCriteria.add(Restrictions.eq(FinAccPaymentMethod.PROPERTY_ACCOUNT, finAccount));
+      obCriteria.add(Restrictions.eq(FinAccPaymentMethod.PROPERTY_PAYMENTMETHOD, finPaymentMethod));
+      obCriteria.setFilterOnReadableClients(false);
+      obCriteria.setFilterOnReadableOrganization(false);
+      List<FinAccPaymentMethod> lines = obCriteria.list();
+      List<FIN_FinancialAccountAccounting> accounts = finAccount.getFINFinancialAccountAcctList();
+      String uponUse = "";
+      if (isPayment) {
+        if (isReceipt) {
+          uponUse = lines.get(0).getUponReceiptUse();
+        } else {
+          uponUse = lines.get(0).getUponPaymentUse();
+        }
+      } else {
+        if (isReceipt) {
+          uponUse = lines.get(0).getUponDepositUse();
+        } else {
+          uponUse = lines.get(0).getUponWithdrawalUse();
+        }
+      }
+      for (FIN_FinancialAccountAccounting account : accounts) {
+        if (confirmation)
+          return confirmation;
+        if (isReceipt) {
+          if (("INT").equals(uponUse) && account.getInTransitPaymentAccountIN() != null)
+            confirmation = true;
+          else if (("DEP").equals(uponUse) && account.getDepositAccount() != null)
+            confirmation = true;
+          else if (("CLE").equals(uponUse) && account.getClearedPaymentAccount() != null)
+            confirmation = true;
+        } else {
+          if (("INT").equals(uponUse) && account.getFINOutIntransitAcct() != null)
+            confirmation = true;
+          else if (("WIT").equals(uponUse) && account.getWithdrawalAccount() != null)
+            confirmation = true;
+          else if (("CLE").equals(uponUse) && account.getClearedPaymentAccountOUT() != null)
+            confirmation = true;
+        }
+        // For payments with Amount ZERO always create an entry as no transaction will be created
+        if (isPayment) {
+          BigDecimal amount = new BigDecimal(strPaymentAmount);
+          if (amount.compareTo(BigDecimal.ZERO) == 0) {
+            confirmation = true;
+          }
+        }
+      }
+    } catch (Exception e) {
+      return confirmation;
+    } finally {
+      OBContext.restorePreviousMode();
+    }
+    return confirmation;
+  }
+
   private void printPage(HttpServletResponse response, VariablesSecureApp vars,
       String strBPfromInvoice, String strBPfromInvoiceId, String strCurrencyId,
       String strInvoiceId, String strOrgId, String strWindowId, String strTabId, boolean isReceipt,
--- a/modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/ad_actionbutton/AddTransaction.html	Wed Apr 02 13:53:38 2014 +0200
+++ b/modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/ad_actionbutton/AddTransaction.html	Wed Apr 02 17:57:02 2014 +0530
@@ -277,31 +277,13 @@
 }
 
 function submitThisPage(strCommand) {
-  var callback=function(paramXMLParticular, XMLHttpRequestObj) {
-      var strText = "";
-      if (getReadyStateHandler(XMLHttpRequestObj)) {
-        try {
-          if (XMLHttpRequestObj.responseText) {
-            strText = XMLHttpRequestObj.responseText;
-            var objson = eval(strText);
-            showJSMessage(objson.text);
-            return false;
-          }else{
-            if (validate() ) {
-              setProcessingMode('popup', true);
-              submitCommandForm(strCommand, false, null, '../org.openbravo.advpaymentmngt.ad_actionbutton/AddTransaction.html', '_self');
-              disableButtonProcess();
-              return true;
-            } else {
-              return true;
-            }
-          }
-        } catch (e) {
+    if (validate() ) {
+        setProcessingMode('popup', true);
+        submitCommandForm(strCommand, false, null, '../org.openbravo.advpaymentmngt.ad_actionbutton/AddTransaction.html', '_self');
+        disableButtonProcess();
+        return true;
       }
-    }
-  }
-  var paramXMLReq = null;
-  return submitXmlHttpRequest(callback, document.frmMain, 'PERIOD', '../org.openbravo.advpaymentmngt.ad_actionbutton/AddTransaction.html', false, null, paramXMLReq);
+      return true;
 }
 
 </script>
--- a/modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/ad_actionbutton/Reconciliation.java	Wed Apr 02 13:53:38 2014 +0200
+++ b/modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/ad_actionbutton/Reconciliation.java	Wed Apr 02 17:57:02 2014 +0530
@@ -25,8 +25,10 @@
 import java.text.SimpleDateFormat;
 import java.util.Calendar;
 import java.util.Date;
+import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServletRequest;
@@ -48,22 +50,27 @@
 import org.openbravo.dal.service.OBDal;
 import org.openbravo.dal.service.OBQuery;
 import org.openbravo.data.FieldProvider;
+import org.openbravo.database.ConnectionProvider;
 import org.openbravo.erpCommon.ad_forms.AcctServer;
 import org.openbravo.erpCommon.utility.DateTimeData;
 import org.openbravo.erpCommon.utility.OBError;
 import org.openbravo.erpCommon.utility.Utility;
 import org.openbravo.model.common.enterprise.DocumentType;
+import org.openbravo.model.financialmgmt.accounting.FIN_FinancialAccountAccounting;
 import org.openbravo.model.financialmgmt.payment.FIN_FinaccTransaction;
 import org.openbravo.model.financialmgmt.payment.FIN_FinancialAccount;
+import org.openbravo.model.financialmgmt.payment.FIN_Payment;
 import org.openbravo.model.financialmgmt.payment.FIN_PaymentDetail;
 import org.openbravo.model.financialmgmt.payment.FIN_PaymentScheduleDetail;
 import org.openbravo.model.financialmgmt.payment.FIN_Reconciliation;
 import org.openbravo.model.financialmgmt.payment.FIN_ReconciliationLine_v;
+import org.openbravo.model.financialmgmt.payment.FinAccPaymentMethod;
 import org.openbravo.xmlEngine.XmlDocument;
 
 public class Reconciliation extends HttpSecureAppServlet {
   private static final long serialVersionUID = 1L;
   private AdvPaymentMngtDao dao;
+  Set<FIN_FinaccTransaction> transactionsToBePosted = new HashSet<FIN_FinaccTransaction>();
 
   public void doPost(HttpServletRequest request, HttpServletResponse response)
       throws ServletException, IOException {
@@ -200,6 +207,7 @@
       if (process) { // Validations
         String strMessage = "";
         boolean raiseException = false;
+
         if (new BigDecimal(strDifference).compareTo(BigDecimal.ZERO) != 0) {
           strMessage = "@APRM_ReconciliationDiscrepancy@" + " " + strDifference;
           raiseException = true;
@@ -236,10 +244,11 @@
 
         boolean orgLegalWithAccounting = FIN_Utility.periodControlOpened(reconciliation.TABLE_NAME,
             reconciliation.getId(), reconciliation.TABLE_NAME + "_ID", "LE");
-        if (!FIN_Utility.isPeriodOpen(reconciliation.getClient().getId(),
-            AcctServer.DOCTYPE_Reconciliation, reconciliation.getOrganization().getId(),
-            strStatementDate)
-            && orgLegalWithAccounting) {
+        boolean documentEnabled = getDocumentConfirmation(this, reconciliation.getId());
+        if (documentEnabled
+            && !FIN_Utility.isPeriodOpen(reconciliation.getClient().getId(),
+                AcctServer.DOCTYPE_Reconciliation, reconciliation.getOrganization().getId(),
+                strStatementDate) && orgLegalWithAccounting) {
           msg.setType("Error");
           msg.setTitle(Utility.messageBD(this, "Error", vars.getLanguage()));
           msg.setMessage(Utility.parseTranslation(this, vars, vars.getLanguage(),
@@ -250,7 +259,7 @@
           return;
         }
 
-        if (orgLegalWithAccounting) {
+        if (documentEnabled && orgLegalWithAccounting) {
           String identifier = linesInNotAvailablePeriod(reconciliation.getId());
           if (!identifier.equalsIgnoreCase("")) {
             msg.setType("Error");
@@ -552,4 +561,100 @@
 
   }
 
+  public List<FIN_FinaccTransaction> getTransactionList(FIN_Reconciliation reconciliation) {
+    OBContext.setAdminMode();
+    List<FIN_FinaccTransaction> transactions = null;
+    try {
+      OBCriteria<FIN_FinaccTransaction> trans = OBDal.getInstance().createCriteria(
+          FIN_FinaccTransaction.class);
+      trans.add(Restrictions.eq(FIN_FinaccTransaction.PROPERTY_RECONCILIATION, reconciliation));
+      trans.setFilterOnReadableClients(false);
+      trans.setFilterOnReadableOrganization(false);
+      transactions = trans.list();
+    } finally {
+      OBContext.restorePreviousMode();
+    }
+    return transactions;
+  }
+
+  /*
+   * Checks if this step (Reconciliation) is configured to generate accounting for the selected
+   * financial account
+   */
+  public boolean getDocumentConfirmation(ConnectionProvider conn, String strRecordId) {
+    OBContext.setAdminMode();
+    try {
+      FIN_Reconciliation reconciliation = OBDal.getInstance().get(FIN_Reconciliation.class,
+          strRecordId);
+      List<FIN_FinaccTransaction> transactions = getTransactionList(reconciliation);
+      List<FIN_FinancialAccountAccounting> accounts = reconciliation.getAccount()
+          .getFINFinancialAccountAcctList();
+      for (FIN_FinaccTransaction transaction : transactions) {
+        FIN_Payment payment = transaction.getFinPayment();
+        // If payment exists, check Payment Method + financial Account Configuration
+        if (payment != null) {
+          OBCriteria<FinAccPaymentMethod> obCriteria = OBDal.getInstance().createCriteria(
+              FinAccPaymentMethod.class);
+          obCriteria.add(Restrictions.eq(FinAccPaymentMethod.PROPERTY_ACCOUNT,
+              reconciliation.getAccount()));
+          obCriteria.add(Restrictions.eq(FinAccPaymentMethod.PROPERTY_PAYMENTMETHOD,
+              payment.getPaymentMethod()));
+          obCriteria.setFilterOnReadableClients(false);
+          obCriteria.setFilterOnReadableOrganization(false);
+          List<FinAccPaymentMethod> lines = obCriteria.list();
+          for (FIN_FinancialAccountAccounting account : accounts) {
+            if (payment.isReceipt()) {
+              if (("INT").equals(lines.get(0).getINUponClearingUse())
+                  && account.getInTransitPaymentAccountIN() != null) {
+                transactionsToBePosted.add(transaction);
+              } else if (("DEP").equals(lines.get(0).getINUponClearingUse())
+                  && account.getDepositAccount() != null) {
+                transactionsToBePosted.add(transaction);
+              } else if (("CLE").equals(lines.get(0).getINUponClearingUse())
+                  && account.getClearedPaymentAccount() != null) {
+                transactionsToBePosted.add(transaction);
+              }
+            } else {
+              if (("INT").equals(lines.get(0).getOUTUponClearingUse())
+                  && account.getFINOutIntransitAcct() != null) {
+                transactionsToBePosted.add(transaction);
+              } else if (("WIT").equals(lines.get(0).getOUTUponClearingUse())
+                  && account.getWithdrawalAccount() != null) {
+                transactionsToBePosted.add(transaction);
+              } else if (("CLE").equals(lines.get(0).getOUTUponClearingUse())
+                  && account.getClearedPaymentAccountOUT() != null) {
+                transactionsToBePosted.add(transaction);
+              }
+            }
+          }
+        } else if (transaction.getGLItem() != null) {
+          for (FIN_FinancialAccountAccounting account : accounts) {
+            if ("BPD".equals(transaction.getTransactionType())
+                && account.getClearedPaymentAccount() != null) {
+              transactionsToBePosted.add(transaction);
+            } else if ("BPW".equals(transaction.getTransactionType())
+                && account.getClearedPaymentAccountOUT() != null) {
+              transactionsToBePosted.add(transaction);
+            }
+          }
+        } else {
+          for (FIN_FinancialAccountAccounting account : accounts) {
+            if ("BF".equals(transaction.getTransactionType())
+                && account.getClearedPaymentAccountOUT() != null) {
+              transactionsToBePosted.add(transaction);
+            }
+          }
+        }
+      }
+    } catch (Exception e) {
+      return false;
+    } finally {
+      OBContext.restorePreviousMode();
+    }
+    if (transactionsToBePosted.size() == 0) {
+      return false;
+    }
+    return true;
+  }
+
 }
--- a/modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/process/FIN_PaymentProcess.java	Wed Apr 02 13:53:38 2014 +0200
+++ b/modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/process/FIN_PaymentProcess.java	Wed Apr 02 17:57:02 2014 +0530
@@ -47,6 +47,7 @@
 import org.openbravo.model.common.currency.ConversionRateDoc;
 import org.openbravo.model.common.enterprise.Organization;
 import org.openbravo.model.common.invoice.Invoice;
+import org.openbravo.model.financialmgmt.accounting.FIN_FinancialAccountAccounting;
 import org.openbravo.model.financialmgmt.payment.FIN_FinaccTransaction;
 import org.openbravo.model.financialmgmt.payment.FIN_Payment;
 import org.openbravo.model.financialmgmt.payment.FIN_PaymentDetail;
@@ -54,6 +55,7 @@
 import org.openbravo.model.financialmgmt.payment.FIN_PaymentProposal;
 import org.openbravo.model.financialmgmt.payment.FIN_PaymentScheduleDetail;
 import org.openbravo.model.financialmgmt.payment.FIN_Payment_Credit;
+import org.openbravo.model.financialmgmt.payment.FinAccPaymentMethod;
 import org.openbravo.model.financialmgmt.payment.PaymentExecutionProcess;
 import org.openbravo.model.pricing.pricelist.PriceList;
 import org.openbravo.scheduling.ProcessBundle;
@@ -62,6 +64,8 @@
 public class FIN_PaymentProcess implements org.openbravo.scheduling.Process {
   private static AdvPaymentMngtDao dao;
 
+  public BigDecimal ZERO = BigDecimal.ZERO;
+
   public void execute(ProcessBundle bundle) throws Exception {
     dao = new AdvPaymentMngtDao();
     final String language = bundle.getContext().getLanguage();
@@ -154,10 +158,11 @@
 
         boolean orgLegalWithAccounting = FIN_Utility.periodControlOpened(payment.TABLE_NAME,
             payment.getId(), payment.TABLE_NAME + "_ID", "LE");
-        if (!FIN_Utility.isPeriodOpen(payment.getClient().getId(), payment.getDocumentType()
-            .getDocumentCategory(), payment.getOrganization().getId(), OBDateUtils
-            .formatDate(payment.getPaymentDate()))
-            && orgLegalWithAccounting) {
+        boolean documentEnabled = getDocumentConfirmation(conProvider, payment.getId());
+        if (documentEnabled
+            && !FIN_Utility.isPeriodOpen(payment.getClient().getId(), payment.getDocumentType()
+                .getDocumentCategory(), payment.getOrganization().getId(), OBDateUtils
+                .formatDate(payment.getPaymentDate())) && orgLegalWithAccounting) {
           msg.setType("Error");
           msg.setTitle(Utility.messageBD(conProvider, "Error", language));
           msg.setMessage(Utility.parseTranslation(conProvider, vars, language,
@@ -1349,4 +1354,57 @@
     return myMessage;
   }
 
+  public boolean getDocumentConfirmation(ConnectionProvider conn, String strRecordId) {
+    // Checks if this step is configured to generate accounting for the selected financial account
+    boolean confirmation = false;
+    OBContext.setAdminMode();
+    try {
+      FIN_Payment payment = OBDal.getInstance().get(FIN_Payment.class, strRecordId);
+      OBCriteria<FinAccPaymentMethod> obCriteria = OBDal.getInstance().createCriteria(
+          FinAccPaymentMethod.class);
+      obCriteria.add(Restrictions.eq(FinAccPaymentMethod.PROPERTY_ACCOUNT, payment.getAccount()));
+      obCriteria.add(Restrictions.eq(FinAccPaymentMethod.PROPERTY_PAYMENTMETHOD,
+          payment.getPaymentMethod()));
+      obCriteria.setFilterOnReadableClients(false);
+      obCriteria.setFilterOnReadableOrganization(false);
+      List<FinAccPaymentMethod> lines = obCriteria.list();
+      List<FIN_FinancialAccountAccounting> accounts = payment.getAccount()
+          .getFINFinancialAccountAcctList();
+      for (FIN_FinancialAccountAccounting account : accounts) {
+        if (confirmation)
+          return confirmation;
+        if (payment.isReceipt()) {
+          if (("INT").equals(lines.get(0).getUponReceiptUse())
+              && account.getInTransitPaymentAccountIN() != null)
+            confirmation = true;
+          else if (("DEP").equals(lines.get(0).getUponReceiptUse())
+              && account.getDepositAccount() != null)
+            confirmation = true;
+          else if (("CLE").equals(lines.get(0).getUponReceiptUse())
+              && account.getClearedPaymentAccount() != null)
+            confirmation = true;
+        } else {
+          if (("INT").equals(lines.get(0).getUponPaymentUse())
+              && account.getFINOutIntransitAcct() != null)
+            confirmation = true;
+          else if (("WIT").equals(lines.get(0).getUponPaymentUse())
+              && account.getWithdrawalAccount() != null)
+            confirmation = true;
+          else if (("CLE").equals(lines.get(0).getUponPaymentUse())
+              && account.getClearedPaymentAccountOUT() != null)
+            confirmation = true;
+        }
+        // For payments with Amount ZERO always create an entry as no transaction will be created
+        if (payment.getAmount().compareTo(ZERO) == 0) {
+          confirmation = true;
+        }
+      }
+    } catch (Exception e) {
+      return confirmation;
+    } finally {
+      OBContext.restorePreviousMode();
+    }
+    return confirmation;
+  }
+
 }
--- a/modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/process/FIN_TransactionProcess.java	Wed Apr 02 13:53:38 2014 +0200
+++ b/modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/process/FIN_TransactionProcess.java	Wed Apr 02 17:57:02 2014 +0530
@@ -35,15 +35,21 @@
 import org.openbravo.erpCommon.utility.OBError;
 import org.openbravo.erpCommon.utility.Utility;
 import org.openbravo.model.common.currency.ConversionRateDoc;
+import org.openbravo.model.financialmgmt.accounting.FIN_FinancialAccountAccounting;
 import org.openbravo.model.financialmgmt.payment.FIN_FinaccTransaction;
 import org.openbravo.model.financialmgmt.payment.FIN_FinancialAccount;
 import org.openbravo.model.financialmgmt.payment.FIN_Payment;
 import org.openbravo.model.financialmgmt.payment.FIN_PaymentDetail;
 import org.openbravo.model.financialmgmt.payment.FIN_PaymentScheduleDetail;
+import org.openbravo.model.financialmgmt.payment.FinAccPaymentMethod;
 import org.openbravo.scheduling.ProcessBundle;
 
 public class FIN_TransactionProcess implements org.openbravo.scheduling.Process {
   private static AdvPaymentMngtDao dao;
+  /** Transaction type - Financial Account */
+  public static final String TRXTYPE_BPDeposit = "BPD";
+  public static final String TRXTYPE_BPWithdrawal = "BPW";
+  public static final String TRXTYPE_BankFee = "BF";
 
   public void execute(ProcessBundle bundle) throws Exception {
     dao = new AdvPaymentMngtDao();
@@ -72,10 +78,11 @@
           // ***********************
           boolean orgLegalWithAccounting = FIN_Utility.periodControlOpened(transaction.TABLE_NAME,
               transaction.getId(), transaction.TABLE_NAME + "_ID", "LE");
-          if (!FIN_Utility.isPeriodOpen(transaction.getClient().getId(),
-              AcctServer.DOCTYPE_FinAccTransaction, transaction.getOrganization().getId(),
-              OBDateUtils.formatDate(transaction.getDateAcct()))
-              && orgLegalWithAccounting) {
+          boolean documentEnabled = getDocumentConfirmation(conProvider, transaction.getId());
+          if (documentEnabled
+              && !FIN_Utility.isPeriodOpen(transaction.getClient().getId(),
+                  AcctServer.DOCTYPE_FinAccTransaction, transaction.getOrganization().getId(),
+                  OBDateUtils.formatDate(transaction.getDateAcct())) && orgLegalWithAccounting) {
             msg.setType("Error");
             msg.setTitle(Utility.messageBD(conProvider, "Error", language));
             msg.setMessage(Utility.parseTranslation(conProvider, vars, language,
@@ -249,4 +256,79 @@
     }
   }
 
+  /*
+   * Checks if this step is configured to generate accounting for the selected financial account
+   */
+  public boolean getDocumentConfirmation(ConnectionProvider conn, String strRecordId) {
+    boolean confirmation = false;
+    OBContext.setAdminMode();
+    try {
+      FIN_FinaccTransaction transaction = OBDal.getInstance().get(FIN_FinaccTransaction.class,
+          strRecordId);
+      List<FIN_FinancialAccountAccounting> accounts = transaction.getAccount()
+          .getFINFinancialAccountAcctList();
+      FIN_Payment payment = transaction.getFinPayment();
+      if (payment != null) {
+        OBCriteria<FinAccPaymentMethod> obCriteria = OBDal.getInstance().createCriteria(
+            FinAccPaymentMethod.class);
+        obCriteria.add(Restrictions.eq(FinAccPaymentMethod.PROPERTY_ACCOUNT,
+            transaction.getAccount()));
+        obCriteria.add(Restrictions.eq(FinAccPaymentMethod.PROPERTY_PAYMENTMETHOD,
+            payment.getPaymentMethod()));
+        obCriteria.setFilterOnReadableClients(false);
+        obCriteria.setFilterOnReadableOrganization(false);
+        List<FinAccPaymentMethod> lines = obCriteria.list();
+        for (FIN_FinancialAccountAccounting account : accounts) {
+          if (confirmation)
+            return confirmation;
+          if (payment.isReceipt()) {
+            if (("INT").equals(lines.get(0).getUponDepositUse())
+                && account.getInTransitPaymentAccountIN() != null)
+              confirmation = true;
+            else if (("DEP").equals(lines.get(0).getUponDepositUse())
+                && account.getDepositAccount() != null)
+              confirmation = true;
+            else if (("CLE").equals(lines.get(0).getUponDepositUse())
+                && account.getClearedPaymentAccount() != null)
+              confirmation = true;
+
+            else if (null == (lines.get(0).getUponDepositUse())
+                && null == (lines.get(0).getINUponClearingUse())
+                && transaction.getAccount() != payment.getAccount()) {
+              confirmation = true;
+            }
+          } else {
+            if (("INT").equals(lines.get(0).getUponWithdrawalUse())
+                && account.getFINOutIntransitAcct() != null)
+              confirmation = true;
+            else if (("WIT").equals(lines.get(0).getUponWithdrawalUse())
+                && account.getWithdrawalAccount() != null)
+              confirmation = true;
+            else if (("CLE").equals(lines.get(0).getUponWithdrawalUse())
+                && account.getClearedPaymentAccountOUT() != null)
+              confirmation = true;
+          }
+        }
+
+      } else {
+        for (FIN_FinancialAccountAccounting account : accounts) {
+          if (confirmation)
+            return confirmation;
+          if ((TRXTYPE_BPDeposit.equals(transaction.getTransactionType()) && account
+              .getDepositAccount() != null)
+              || (TRXTYPE_BPWithdrawal.equals(transaction.getTransactionType()) && account
+                  .getWithdrawalAccount() != null)
+              || (TRXTYPE_BankFee.equals(transaction.getTransactionType()) && account
+                  .getWithdrawalAccount() != null))
+            confirmation = true;
+        }
+      }
+    } catch (Exception e) {
+      return confirmation;
+    } finally {
+      OBContext.restorePreviousMode();
+    }
+    return confirmation;
+  }
+
 }