Related to issue 24615: implemented double cash criteria
authorVíctor Martínez Romanos <victor.martinez@openbravo.com>
Tue, 07 Jan 2014 11:07:33 +0100
changeset 21744 fa4b675582c3
parent 21743 72244ef4ab29
child 21745 2b5c92edc255
Related to issue 24615: implemented double cash criteria

For purchase flows the cash vat can be defined by the business partner (vendor) OR the organization.
If both the Cash VAT and the Double Cash Criteria flags are set to Yes, the Cash VAT regime will be automatically applied for the purchase flow too.

The sales flows only depend on the Organization's Cash VAT flag.
src-db/database/model/functions/C_GETTAX.xml
src-db/database/model/tables/AD_ORGINFO.xml
src-db/database/model/triggers/C_INVOICELINE_TRG.xml
src-db/database/sourcedata/AD_COLUMN.xml
src-db/database/sourcedata/AD_ELEMENT.xml
src/org/openbravo/erpCommon/ad_callouts/SE_Invoice_BPartner.java
src/org/openbravo/erpCommon/ad_callouts/SE_Order_BPartner.java
src/org/openbravo/erpCommon/utility/CashVATUtil.java
--- a/src-db/database/model/functions/C_GETTAX.xml	Fri Dec 27 14:31:41 2013 +0100
+++ b/src-db/database/model/functions/C_GETTAX.xml	Tue Jan 07 11:07:33 2014 +0100
@@ -43,7 +43,7 @@
 * under the License.
 * The Original Code is Openbravo ERP.
 * The Initial Developer of the Original Code is Openbravo SLU
-* All portions are Copyright (C) 2001-2013 Openbravo SLU
+* All portions are Copyright (C) 2001-2014 Openbravo SLU
 * All Rights Reserved.
 * Contributor(s):  ______________________________________.
 ************************************************************************/
@@ -60,6 +60,7 @@
   v_aux           VARCHAR2(32) ;
   v_BPTaxCategory VARCHAR2(32);
   v_IsCashVAT     AD_ORGINFO.ISCASHVAT%TYPE;
+  v_IsDoubleCash  AD_ORGINFO.ISDOUBLECASH%TYPE;
   TYPE RECORD IS REF CURSOR;
     BILLTO RECORD;
   BEGIN
@@ -116,7 +117,22 @@
       ELSE
         v_isTaxExempt := 'N';
         v_BPTaxCategory := BILLTO.PO_BP_TaxCategory_ID;
-        v_IsCashVAT := BILLTO.isCashVAT;
+        IF p_forcedcashvat IS NULL THEN
+          IF BILLTO.isCashVAT = 'Y' THEN
+            v_IsCashVAT := 'Y';
+          ELSE
+            -- double cash
+            select coalesce(oi.isCashVAT, 'N'), coalesce(oi.isDoubleCash, 'N')
+            into v_IsCashVAT, v_IsDoubleCash
+            from ad_orginfo oi
+            where oi.ad_org_id = ad_get_org_le_bu(p_org_id, 'LE');
+            IF v_IsCashVAT = 'N' OR v_IsDoubleCash = 'N' THEN
+              v_IsCashVAT:='N';
+            END IF;
+          END IF;
+        ELSE
+          v_IsCashVAT := p_forcedcashvat;
+        END IF;
       END IF;
       EXIT;
     END LOOP;
--- a/src-db/database/model/tables/AD_ORGINFO.xml	Fri Dec 27 14:31:41 2013 +0100
+++ b/src-db/database/model/tables/AD_ORGINFO.xml	Tue Jan 07 11:07:33 2014 +0100
@@ -77,6 +77,10 @@
         <default><![CDATA[N]]></default>
         <onCreateDefault/>
       </column>
+      <column name="ISDOUBLECASH" primaryKey="false" required="true" type="CHAR" size="1" autoIncrement="false">
+        <default><![CDATA[N]]></default>
+        <onCreateDefault><![CDATA['N']]></onCreateDefault>
+      </column>
       <foreign-key foreignTable="AD_ORG" name="AD_ORG_AD_ORGINFO" onDelete="cascade">
         <reference local="AD_ORG_ID" foreign="AD_ORG_ID"/>
       </foreign-key>
@@ -100,6 +104,7 @@
       </foreign-key>
       <check name="AD_ORGINFO_ISACTIVE_CHECK"><![CDATA[ISACTIVE IN ('Y', 'N')]]></check>
       <check name="AD_ORGINFO_ISCASHVAT_CH"><![CDATA[((ISCASHVAT = 'Y') AND (ISTAXUNDEDUCTABLE = 'N')) OR (ISCASHVAT = 'N')]]></check>
+      <check name="AD_ORGINFO_ISDOUBLE_CH"><![CDATA[ISDOUBLECASH IN ('Y', 'N')]]></check>
       <check name="AD_ORGINFO_ISTAXUNDEDUCTABLE"><![CDATA[ISTAXUNDEDUCTABLE IN ('Y', 'N')]]></check>
     </table>
   </database>
--- a/src-db/database/model/triggers/C_INVOICELINE_TRG.xml	Fri Dec 27 14:31:41 2013 +0100
+++ b/src-db/database/model/triggers/C_INVOICELINE_TRG.xml	Tue Jan 07 11:07:33 2014 +0100
@@ -106,7 +106,8 @@
  -- Update C_Invoice.IsCashVAT flag from the line tax rate.
  -- We do it this way to force any invoicing process to properly set the C_Invoice.IsCashVAT
  -- If a user manually changes the tax rate when entering a line, the invoice's header IsCash VAT may change
- IF ((INSERTING OR UPDATING) AND :NEW.C_TAX_ID IS NOT NULL) THEN
+ IF (INSERTING OR UPDATING) THEN
+  IF (:NEW.C_TAX_ID IS NOT NULL) THEN
    SELECT COALESCE(IsCashVAT, 'N')
    INTO v_IsCashVAT_Tax
    FROM C_Tax
@@ -122,6 +123,7 @@
      SET IsCashVAT = v_IsCashVAT_Tax
      WHERE C_Invoice_ID=:NEW.C_Invoice_ID;
    END IF;
+  END IF;
  END IF;
 
 
--- a/src-db/database/sourcedata/AD_COLUMN.xml	Fri Dec 27 14:31:41 2013 +0100
+++ b/src-db/database/sourcedata/AD_COLUMN.xml	Tue Jan 07 11:07:33 2014 +0100
@@ -230190,7 +230190,7 @@
 <!--378DF78B090E4DFDA313E6248B204ABD-->  <ISDESENCRYPTABLE><![CDATA[N]]></ISDESENCRYPTABLE>
 <!--378DF78B090E4DFDA313E6248B204ABD-->  <DEVELOPMENTSTATUS><![CDATA[RE]]></DEVELOPMENTSTATUS>
 <!--378DF78B090E4DFDA313E6248B204ABD-->  <AD_MODULE_ID><![CDATA[0]]></AD_MODULE_ID>
-<!--378DF78B090E4DFDA313E6248B204ABD-->  <POSITION><![CDATA[20]]></POSITION>
+<!--378DF78B090E4DFDA313E6248B204ABD-->  <POSITION><![CDATA[19]]></POSITION>
 <!--378DF78B090E4DFDA313E6248B204ABD-->  <ISTRANSIENT><![CDATA[N]]></ISTRANSIENT>
 <!--378DF78B090E4DFDA313E6248B204ABD-->  <ISAUTOSAVE><![CDATA[Y]]></ISAUTOSAVE>
 <!--378DF78B090E4DFDA313E6248B204ABD-->  <VALIDATEONNEW><![CDATA[Y]]></VALIDATEONNEW>
@@ -234277,6 +234277,42 @@
 <!--4D413EE93F2F43B6BCF5B9B8AD95025D-->  <ISUSEDSEQUENCE><![CDATA[N]]></ISUSEDSEQUENCE>
 <!--4D413EE93F2F43B6BCF5B9B8AD95025D--></AD_COLUMN>
 
+<!--4DC486F035E44F98B3BC21904E46834B--><AD_COLUMN>
+<!--4DC486F035E44F98B3BC21904E46834B-->  <AD_COLUMN_ID><![CDATA[4DC486F035E44F98B3BC21904E46834B]]></AD_COLUMN_ID>
+<!--4DC486F035E44F98B3BC21904E46834B-->  <AD_CLIENT_ID><![CDATA[0]]></AD_CLIENT_ID>
+<!--4DC486F035E44F98B3BC21904E46834B-->  <AD_ORG_ID><![CDATA[0]]></AD_ORG_ID>
+<!--4DC486F035E44F98B3BC21904E46834B-->  <ISACTIVE><![CDATA[Y]]></ISACTIVE>
+<!--4DC486F035E44F98B3BC21904E46834B-->  <NAME><![CDATA[IsDoubleCash]]></NAME>
+<!--4DC486F035E44F98B3BC21904E46834B-->  <DESCRIPTION><![CDATA[Double Cash Criteria]]></DESCRIPTION>
+<!--4DC486F035E44F98B3BC21904E46834B-->  <HELP><![CDATA[Organization is subscribed to the Double Cash criteria. If both the Cash VAT and the Double Cash Criteria flags are set to Yes, the Cash VAT regime will be automatically applied for the purchase flow too.]]></HELP>
+<!--4DC486F035E44F98B3BC21904E46834B-->  <COLUMNNAME><![CDATA[IsDoubleCash]]></COLUMNNAME>
+<!--4DC486F035E44F98B3BC21904E46834B-->  <AD_TABLE_ID><![CDATA[228]]></AD_TABLE_ID>
+<!--4DC486F035E44F98B3BC21904E46834B-->  <AD_REFERENCE_ID><![CDATA[20]]></AD_REFERENCE_ID>
+<!--4DC486F035E44F98B3BC21904E46834B-->  <FIELDLENGTH><![CDATA[1]]></FIELDLENGTH>
+<!--4DC486F035E44F98B3BC21904E46834B-->  <DEFAULTVALUE><![CDATA[N]]></DEFAULTVALUE>
+<!--4DC486F035E44F98B3BC21904E46834B-->  <ISKEY><![CDATA[N]]></ISKEY>
+<!--4DC486F035E44F98B3BC21904E46834B-->  <ISPARENT><![CDATA[N]]></ISPARENT>
+<!--4DC486F035E44F98B3BC21904E46834B-->  <ISMANDATORY><![CDATA[Y]]></ISMANDATORY>
+<!--4DC486F035E44F98B3BC21904E46834B-->  <ISUPDATEABLE><![CDATA[Y]]></ISUPDATEABLE>
+<!--4DC486F035E44F98B3BC21904E46834B-->  <ISIDENTIFIER><![CDATA[N]]></ISIDENTIFIER>
+<!--4DC486F035E44F98B3BC21904E46834B-->  <SEQNO><![CDATA[111]]></SEQNO>
+<!--4DC486F035E44F98B3BC21904E46834B-->  <ISTRANSLATED><![CDATA[N]]></ISTRANSLATED>
+<!--4DC486F035E44F98B3BC21904E46834B-->  <ISENCRYPTED><![CDATA[N]]></ISENCRYPTED>
+<!--4DC486F035E44F98B3BC21904E46834B-->  <ISSELECTIONCOLUMN><![CDATA[N]]></ISSELECTIONCOLUMN>
+<!--4DC486F035E44F98B3BC21904E46834B-->  <AD_ELEMENT_ID><![CDATA[2D2C066AA9964E058D08DF9AF169773D]]></AD_ELEMENT_ID>
+<!--4DC486F035E44F98B3BC21904E46834B-->  <ISSESSIONATTR><![CDATA[N]]></ISSESSIONATTR>
+<!--4DC486F035E44F98B3BC21904E46834B-->  <ISSECONDARYKEY><![CDATA[N]]></ISSECONDARYKEY>
+<!--4DC486F035E44F98B3BC21904E46834B-->  <ISDESENCRYPTABLE><![CDATA[N]]></ISDESENCRYPTABLE>
+<!--4DC486F035E44F98B3BC21904E46834B-->  <DEVELOPMENTSTATUS><![CDATA[RE]]></DEVELOPMENTSTATUS>
+<!--4DC486F035E44F98B3BC21904E46834B-->  <AD_MODULE_ID><![CDATA[0]]></AD_MODULE_ID>
+<!--4DC486F035E44F98B3BC21904E46834B-->  <POSITION><![CDATA[22]]></POSITION>
+<!--4DC486F035E44F98B3BC21904E46834B-->  <ISTRANSIENT><![CDATA[N]]></ISTRANSIENT>
+<!--4DC486F035E44F98B3BC21904E46834B-->  <ISAUTOSAVE><![CDATA[Y]]></ISAUTOSAVE>
+<!--4DC486F035E44F98B3BC21904E46834B-->  <VALIDATEONNEW><![CDATA[Y]]></VALIDATEONNEW>
+<!--4DC486F035E44F98B3BC21904E46834B-->  <IMAGESIZEVALUESACTION><![CDATA[N]]></IMAGESIZEVALUESACTION>
+<!--4DC486F035E44F98B3BC21904E46834B-->  <ISUSEDSEQUENCE><![CDATA[N]]></ISUSEDSEQUENCE>
+<!--4DC486F035E44F98B3BC21904E46834B--></AD_COLUMN>
+
 <!--4E6C7BECF01242F197D5B26635D9FA19--><AD_COLUMN>
 <!--4E6C7BECF01242F197D5B26635D9FA19-->  <AD_COLUMN_ID><![CDATA[4E6C7BECF01242F197D5B26635D9FA19]]></AD_COLUMN_ID>
 <!--4E6C7BECF01242F197D5B26635D9FA19-->  <AD_CLIENT_ID><![CDATA[0]]></AD_CLIENT_ID>
--- a/src-db/database/sourcedata/AD_ELEMENT.xml	Fri Dec 27 14:31:41 2013 +0100
+++ b/src-db/database/sourcedata/AD_ELEMENT.xml	Tue Jan 07 11:07:33 2014 +0100
@@ -21265,6 +21265,20 @@
 <!--29ADAE46594E4364B62CB72C8D029D6F-->  <ISGLOSSARY><![CDATA[N]]></ISGLOSSARY>
 <!--29ADAE46594E4364B62CB72C8D029D6F--></AD_ELEMENT>
 
+<!--2D2C066AA9964E058D08DF9AF169773D--><AD_ELEMENT>
+<!--2D2C066AA9964E058D08DF9AF169773D-->  <AD_ELEMENT_ID><![CDATA[2D2C066AA9964E058D08DF9AF169773D]]></AD_ELEMENT_ID>
+<!--2D2C066AA9964E058D08DF9AF169773D-->  <AD_CLIENT_ID><![CDATA[0]]></AD_CLIENT_ID>
+<!--2D2C066AA9964E058D08DF9AF169773D-->  <AD_ORG_ID><![CDATA[0]]></AD_ORG_ID>
+<!--2D2C066AA9964E058D08DF9AF169773D-->  <ISACTIVE><![CDATA[Y]]></ISACTIVE>
+<!--2D2C066AA9964E058D08DF9AF169773D-->  <COLUMNNAME><![CDATA[IsDoubleCash]]></COLUMNNAME>
+<!--2D2C066AA9964E058D08DF9AF169773D-->  <NAME><![CDATA[Double Cash Criteria]]></NAME>
+<!--2D2C066AA9964E058D08DF9AF169773D-->  <PRINTNAME><![CDATA[Double Cash Criteria]]></PRINTNAME>
+<!--2D2C066AA9964E058D08DF9AF169773D-->  <DESCRIPTION><![CDATA[Double Cash Criteria]]></DESCRIPTION>
+<!--2D2C066AA9964E058D08DF9AF169773D-->  <HELP><![CDATA[Organization is subscribed to the Double Cash criteria. If both the Cash VAT and the Double Cash Criteria flags are set to Yes, the Cash VAT regime will be automatically applied for the purchase flow too.]]></HELP>
+<!--2D2C066AA9964E058D08DF9AF169773D-->  <AD_MODULE_ID><![CDATA[0]]></AD_MODULE_ID>
+<!--2D2C066AA9964E058D08DF9AF169773D-->  <ISGLOSSARY><![CDATA[N]]></ISGLOSSARY>
+<!--2D2C066AA9964E058D08DF9AF169773D--></AD_ELEMENT>
+
 <!--2D2C180E4B004B76AA0CE7C2ABA651A3--><AD_ELEMENT>
 <!--2D2C180E4B004B76AA0CE7C2ABA651A3-->  <AD_ELEMENT_ID><![CDATA[2D2C180E4B004B76AA0CE7C2ABA651A3]]></AD_ELEMENT_ID>
 <!--2D2C180E4B004B76AA0CE7C2ABA651A3-->  <AD_CLIENT_ID><![CDATA[0]]></AD_CLIENT_ID>
--- a/src/org/openbravo/erpCommon/ad_callouts/SE_Invoice_BPartner.java	Fri Dec 27 14:31:41 2013 +0100
+++ b/src/org/openbravo/erpCommon/ad_callouts/SE_Invoice_BPartner.java	Tue Jan 07 11:07:33 2014 +0100
@@ -11,7 +11,7 @@
  * under the License.
  * The Original Code is Openbravo ERP.
  * The Initial Developer of the Original Code is Openbravo SLU
- * All portions are Copyright (C) 2001-2013 Openbravo SLU
+ * All portions are Copyright (C) 2001-2014 Openbravo SLU
  * All Rights Reserved.
  * Contributor(s):  ______________________________________.
  ************************************************************************
@@ -30,8 +30,6 @@
 import org.apache.commons.lang.StringUtils;
 import org.hibernate.criterion.Restrictions;
 import org.openbravo.advpaymentmngt.utility.FIN_Utility;
-import org.openbravo.base.filter.RequestFilter;
-import org.openbravo.base.filter.ValueListFilter;
 import org.openbravo.base.secureApp.HttpSecureAppServlet;
 import org.openbravo.base.secureApp.VariablesSecureApp;
 import org.openbravo.dal.core.OBContext;
@@ -52,7 +50,6 @@
 
 public class SE_Invoice_BPartner extends HttpSecureAppServlet {
   private static final long serialVersionUID = 1L;
-  private static final RequestFilter filterYesNo = new ValueListFilter("Y", "N");
 
   public void init(ServletConfig config) {
     super.init(config);
@@ -336,16 +333,21 @@
       resultado.append(", new Array('MESSAGE', \"" + message + "\")");
 
       // Cash VAT
-      // Purchase flow only (from Business Partner)
+      // Purchase flow only (from Business Partner OR organization) "double cash"
       if (StringUtils.equals("N", strIsSOTrx)) {
-        final String calculatedIsCashVat = CashVATUtil.getBusinessPartnerIsCashVAT(strBPartner);
-        if (calculatedIsCashVat != null && filterYesNo.accept(calculatedIsCashVat)) {
-          resultado.append(", \nnew Array(\"");
-          resultado.append("inpiscashvat");
-          resultado.append("\", \"");
-          resultado.append(FormatUtilities.replaceJS(calculatedIsCashVat));
-          resultado.append("\")");
+        final String bpCashVAT = CashVATUtil.getBusinessPartnerIsCashVAT(strBPartner);
+        resultado.append(", \nnew Array(\"");
+        resultado.append("inpiscashvat");
+        resultado.append("\", \"");
+        if (StringUtils.equals("Y", bpCashVAT)) {
+          resultado.append("Y");
+        } else {
+          final String orgCashVAT = CashVATUtil.getOrganizationIsCashVAT(strOrgId);
+          final String orgDoubleCash = CashVATUtil.getOrganizationIsDoubleCash(strOrgId);
+          resultado.append(StringUtils.equals("Y", orgCashVAT)
+              && StringUtils.equals("Y", orgDoubleCash) ? "Y" : "N");
         }
+        resultado.append("\")");
       }
 
       resultado.append(");");
--- a/src/org/openbravo/erpCommon/ad_callouts/SE_Order_BPartner.java	Fri Dec 27 14:31:41 2013 +0100
+++ b/src/org/openbravo/erpCommon/ad_callouts/SE_Order_BPartner.java	Tue Jan 07 11:07:33 2014 +0100
@@ -11,7 +11,7 @@
  * under the License.
  * The Original Code is Openbravo ERP.
  * The Initial Developer of the Original Code is Openbravo SLU
- * All portions are Copyright (C) 2001-2013 Openbravo SLU
+ * All portions are Copyright (C) 2001-2014 Openbravo SLU
  * All Rights Reserved.
  * Contributor(s):  ______________________________________.
  ************************************************************************
@@ -24,8 +24,6 @@
 
 import org.apache.commons.lang.StringUtils;
 import org.openbravo.advpaymentmngt.utility.FIN_Utility;
-import org.openbravo.base.filter.RequestFilter;
-import org.openbravo.base.filter.ValueListFilter;
 import org.openbravo.dal.service.OBDal;
 import org.openbravo.data.FieldProvider;
 import org.openbravo.erpCommon.businessUtility.BpartnerMiscData;
@@ -37,7 +35,6 @@
 
 public class SE_Order_BPartner extends SimpleCallout {
   private static final long serialVersionUID = 1L;
-  private static final RequestFilter filterYesNo = new ValueListFilter("Y", "N");
 
   @Override
   protected void execute(CalloutInfo info) throws ServletException {
@@ -353,11 +350,17 @@
     info.addResult("MESSAGE", message.toString());
 
     // Cash VAT
-    // Purchase flow only (from Business Partner)
+    // Purchase flow only (from Business Partner OR organization) "double cash"
     if (StringUtils.equals("N", strIsSOTrx)) {
-      final String calculatedIsCashVat = CashVATUtil.getBusinessPartnerIsCashVAT(strBPartner);
-      if (calculatedIsCashVat != null && filterYesNo.accept(calculatedIsCashVat)) {
-        info.addResult("inpiscashvat", calculatedIsCashVat);
+      final String bpCashVAT = CashVATUtil.getBusinessPartnerIsCashVAT(strBPartner);
+      if (StringUtils.equals("Y", bpCashVAT)) {
+        info.addResult("inpiscashvat", "Y");
+      } else {
+        final String orgCashVAT = CashVATUtil.getOrganizationIsCashVAT(strOrgId);
+        final String orgDoubleCash = CashVATUtil.getOrganizationIsDoubleCash(strOrgId);
+        info.addResult("inpiscashvat",
+            StringUtils.equals("Y", orgCashVAT) && StringUtils.equals("Y", orgDoubleCash) ? "Y"
+                : "N");
       }
     }
   }
--- a/src/org/openbravo/erpCommon/utility/CashVATUtil.java	Fri Dec 27 14:31:41 2013 +0100
+++ b/src/org/openbravo/erpCommon/utility/CashVATUtil.java	Tue Jan 07 11:07:33 2014 +0100
@@ -11,7 +11,7 @@
  * under the License. 
  * The Original Code is Openbravo ERP. 
  * The Initial Developer of the Original Code is Openbravo SLU 
- * All portions are Copyright (C) 2013 Openbravo SLU
+ * All portions are Copyright (C) 2013-2014 Openbravo SLU
  * All Rights Reserved. 
  * Contributor(s):  ______________________________________.
  ************************************************************************
@@ -84,6 +84,33 @@
   }
 
   /**
+   * Returns the associated legal entity Double Cash Criteria configuration. Useful for purchase
+   * flows
+   * 
+   * @param strOrgId
+   *          organization id
+   * @return "Y", "N" or null if not found
+   */
+  public static String getOrganizationIsDoubleCash(final String strOrgId) {
+    try {
+      OBContext.setAdminMode(true);
+      final Organization org = OBDal.getInstance().get(Organization.class, strOrgId);
+      final Organization legalEntity = OBContext.getOBContext()
+          .getOrganizationStructureProvider(org.getClient().getId()).getLegalEntity(org);
+      if (legalEntity != null && legalEntity.getOrganizationInformationList() != null
+          && !legalEntity.getOrganizationInformationList().isEmpty()) {
+        return legalEntity.getOrganizationInformationList().get(0).isDoubleCash() ? "Y" : "N";
+      }
+    } catch (final Exception e) {
+      log4j.error("Error getting organization'" + strOrgId + "' double cash. Returning null", e);
+    } finally {
+      OBContext.restorePreviousMode();
+    }
+
+    return null;
+  }
+
+  /**
    * Returns the Cash VAT configuration for the given Vendor (Business Partner)
    * 
    * @param strBPId