# HG changeset patch
# User Ben Sommerville <ben.sommerville@eintel.com.au>
# Date 1288137572 -39600
# Node ID e13579f85b703a1f03fb2c413c6101f22df9896e
# Parent  05ad25799505d8b4983a00c0986966d8478a6bb5
Fixes issue 14860: Handle new payment method when creating AP expense invoice

diff -r 05ad25799505 -r e13579f85b70 src/org/openbravo/erpCommon/ad_process/ExpenseAPInvoice.java
--- a/src/org/openbravo/erpCommon/ad_process/ExpenseAPInvoice.java	Wed Oct 20 16:43:43 2010 +1100
+++ b/src/org/openbravo/erpCommon/ad_process/ExpenseAPInvoice.java	Wed Oct 27 10:59:32 2010 +1100
@@ -4,15 +4,15 @@
  * Version  1.0  (the  "License"),  being   the  Mozilla   Public  License
  * Version 1.1  with a permitted attribution clause; you may not  use this
  * file except in compliance with the License. You  may  obtain  a copy of
- * the License at http://www.openbravo.com/legal/license.html 
+ * the License at http://www.openbravo.com/legal/license.html
  * Software distributed under the License  is  distributed  on  an "AS IS"
  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
  * License for the specific  language  governing  rights  and  limitations
- * 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-2009 Openbravo SLU 
- * All Rights Reserved. 
+ * 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-2009 Openbravo SLU
+ * All Rights Reserved.
  * Contributor(s):  ______________________________________.
  ************************************************************************
  */
@@ -31,7 +31,9 @@
 import org.apache.log4j.Logger;
 import org.openbravo.base.secureApp.HttpSecureAppServlet;
 import org.openbravo.base.secureApp.VariablesSecureApp;
+import org.openbravo.dal.core.OBContext;
 import org.openbravo.erpCommon.ad_actionButton.ActionButtonDefaultData;
+import org.openbravo.erpCommon.businessUtility.Preferences;
 import org.openbravo.erpCommon.businessUtility.Tax;
 import org.openbravo.erpCommon.businessUtility.WindowTabs;
 import org.openbravo.erpCommon.utility.ComboTableData;
@@ -39,6 +41,8 @@
 import org.openbravo.erpCommon.utility.LeftTabsBar;
 import org.openbravo.erpCommon.utility.NavigationBar;
 import org.openbravo.erpCommon.utility.OBError;
+import org.openbravo.erpCommon.utility.PropertyException;
+import org.openbravo.erpCommon.utility.PropertyNotFoundException;
 import org.openbravo.erpCommon.utility.SequenceIdData;
 import org.openbravo.erpCommon.utility.ToolBar;
 import org.openbravo.erpCommon.utility.Utility;
@@ -102,6 +106,7 @@
     String strPricelist = "";
     String strSalesrepId = "";
     String strPaymentRule = "";
+    String strPaymentMethodId = "";
     String strPaymentterm = "";
     BigDecimal qty = BigDecimal.ZERO;
     BigDecimal amount = BigDecimal.ZERO;
@@ -125,7 +130,7 @@
       for (int i = 0; i < data.length; i++) {
         String docTargetType = ExpenseAPInvoiceData.cDoctypeTarget(this, data[i].adClientId,
             data[i].adOrgId);
-        
+
         // Checks some employee data
         strEmpl = data[i].bpname;
         strProd = data[i].prodname;
@@ -143,13 +148,24 @@
         if (strcBpartnerLocationId.equals(""))
           throw new Exception("ShiptoNotdefined");
 
-        strPaymentRule = ExpenseAPInvoiceData.paymentrule(this, data[i].cBpartnerId);
-        if (strPaymentRule.equals(""))
-          throw new Exception("FormofPaymentNotdefined");
+        if( isNewFlow() ) {
+          strPaymentMethodId = ExpenseAPInvoiceData.paymentmethodId(this, data[i].cBpartnerId);
+          if (strPaymentMethodId.equals("")) {
+            throw new Exception("PayementMethodNotdefined");
+          }
+          strPaymentRule = ExpenseAPInvoiceData.paymentrule(this, data[i].cBpartnerId);
+          if (strPaymentRule.equals("")) {
+            strPaymentRule="P";
+          }
 
+        } else {
+          strPaymentRule = ExpenseAPInvoiceData.paymentrule(this, data[i].cBpartnerId);
+          if (strPaymentRule.equals(""))
+            throw new Exception("FormofPaymentNotdefined");
+        }
         strPaymentterm = ExpenseAPInvoiceData.paymentterm(this, data[i].cBpartnerId);
         if (strPaymentterm.equals(""))
-          throw new Exception("PaymenttermNotdefined");        
+          throw new Exception("PaymenttermNotdefined");
 
         // Checks if there are invoices not processed that full filled
         // the requirements
@@ -160,13 +176,13 @@
           strcInvoiceIdOld = ExpenseAPInvoiceData.selectInvoiceHeaderNoProject(conn, this,
               data[i].adClientId, data[i].adOrgId, strDateInvoiced, data[i].cBpartnerId,
               strBPCCurrencyId, data[i].cActivityId, data[i].cCampaignId,
-              strcBpartnerLocationId, strPaymentRule, strPaymentterm);
-          
+              strcBpartnerLocationId, strPaymentRule, strPaymentMethodId, strPaymentterm);
+
         } else {
           strcInvoiceIdOld = ExpenseAPInvoiceData.selectInvoiceHeader(conn, this,
               data[i].adClientId, data[i].adOrgId, strDateInvoiced, data[i].cBpartnerId,
               strBPCCurrencyId, data[i].cProjectId, data[i].cActivityId, data[i].cCampaignId,
-              strcBpartnerLocationId, strPaymentRule, strPaymentterm);
+              strcBpartnerLocationId, strPaymentRule, strPaymentMethodId, strPaymentterm);
         }
 
         if (strcInvoiceIdOld.equals("")) {
@@ -185,7 +201,7 @@
                 "N", data[i].adClientId, data[i].adOrgId, "", "", strDocumentno, "", "", "Y",
                 docTargetType, strDateInvoiced, strDateInvoiced, data[i].cBpartnerId,
                 strcBpartnerLocationId, "", strPricelistId, strBPCCurrencyId, strSalesrepId, "N",
-                "", "", strPaymentRule, strPaymentterm, "N", "N", data[i].cProjectId,
+                "", "", strPaymentRule, strPaymentMethodId,  strPaymentterm, "N", "N", data[i].cProjectId,
                 data[i].cActivityId, data[i].cCampaignId, vars.getOrg(), "", "", "0", "0", "DR",
                 strDocType, "N", "CO", "N", vars.getUser(), vars.getUser());
           } catch (ServletException ex) {
@@ -528,4 +544,26 @@
   public String getServletInfo() {
     return "Servlet ExpenseAPInvoice";
   } // end of getServletInfo() method
+
+  @SuppressWarnings("deprecation")
+  private boolean isNewFlow() {
+    // Extra check for Payment Flow-disabling switch
+    try {
+      // Use Utility.getPropertyValue for backward compatibility
+      try {
+        Preferences.getPreferenceValue("FinancialManagement", true, null, null, OBContext
+            .getOBContext().getUser(), null, null);
+        return true;
+      } catch (PropertyNotFoundException e) {
+        if (Utility.getPropertyValue("FinancialManagement", OBContext.getOBContext()
+            .getCurrentClient().getId(), OBContext.getOBContext().getCurrentOrganization().getId()) != null) {
+          return true;
+        } else
+          return false;
+      }
+    } catch (PropertyException e) {
+      return false;
+    }
+  }
+
 }
diff -r 05ad25799505 -r e13579f85b70 src/org/openbravo/erpCommon/ad_process/ExpenseAPInvoice_data.xsql
--- a/src/org/openbravo/erpCommon/ad_process/ExpenseAPInvoice_data.xsql	Wed Oct 20 16:43:43 2010 +1100
+++ b/src/org/openbravo/erpCommon/ad_process/ExpenseAPInvoice_data.xsql	Wed Oct 27 10:59:32 2010 +1100
@@ -74,6 +74,7 @@
     <Parameter name="cCampaignId" optional="true" after="AND C_INVOICE.ISSOTRX = 'N'"><![CDATA[ AND C_INVOICE.C_CAMPAIGN_ID = ?]]></Parameter>
     <Parameter name="cBpartnerLocationId" optional="true" after="AND C_INVOICE.ISSOTRX = 'N'"><![CDATA[ AND C_INVOICE.C_BPARTNER_LOCATION_ID = ?]]></Parameter>
     <Parameter name="paymentrule" optional="true" after="AND C_INVOICE.ISSOTRX = 'N'"><![CDATA[ AND C_INVOICE.PAYMENTRULE = ?]]></Parameter>
+    <Parameter name="paymentmethodId" optional="true" after="AND C_INVOICE.ISSOTRX = 'N'"><![CDATA[ AND C_INVOICE.FIN_PAYMENTMETHOD_ID = ?]]></Parameter>
     <Parameter name="cPaymentTermId" optional="true" after="AND C_INVOICE.ISSOTRX = 'N'"><![CDATA[ AND C_INVOICE.C_PAYMENTTERM_ID = ?]]></Parameter>
   </SqlMethod>
   
@@ -102,6 +103,7 @@
     <Parameter name="cCampaignId" optional="true" after="AND C_INVOICE.ISSOTRX = 'N'"><![CDATA[ AND C_INVOICE.C_CAMPAIGN_ID = ?]]></Parameter>
     <Parameter name="cBpartnerLocationId" optional="true" after="AND C_INVOICE.ISSOTRX = 'N'"><![CDATA[ AND C_INVOICE.C_BPARTNER_LOCATION_ID = ?]]></Parameter>
     <Parameter name="paymentrule" optional="true" after="AND C_INVOICE.ISSOTRX = 'N'"><![CDATA[ AND C_INVOICE.PAYMENTRULE = ?]]></Parameter>
+    <Parameter name="paymentmethodId" optional="true" after="AND C_INVOICE.ISSOTRX = 'N'"><![CDATA[ AND C_INVOICE.FIN_PAYMENTMETHOD_ID = ?]]></Parameter>
     <Parameter name="cPaymentTermId" optional="true" after="AND C_INVOICE.ISSOTRX = 'N'"><![CDATA[ AND C_INVOICE.C_PAYMENTTERM_ID = ?]]></Parameter>
   </SqlMethod>
 
@@ -115,9 +117,9 @@
       AND AD_ORG_ID = ?
       AND M_PRODUCT_ID = ?
       AND C_UOM_ID = ?
-      AND PRICESTD = ?
-      AND PRICELIST = ?
-      AND PRICELIMIT = ?
+      AND PRICESTD = TO_NUMBER(?)
+      AND PRICELIST = TO_NUMBER(?)
+      AND PRICELIMIT = TO_NUMBER(?)
       AND C_INVOICELINE.ISACTIVE = 'Y'
       GROUP BY C_INVOICELINE_ID
       ]]></Sql>
@@ -239,6 +241,17 @@
     <Parameter name="cBpartnerId"/>
   </SqlMethod>
 
+  <SqlMethod name="paymentmethodId" type="preparedStatement" return="String" default="">
+    <SqlMethodComment>nt></SqlMethodComment>
+    <Sql>
+      <![CDATA[
+      SELECT po_paymentmethod_id
+      FROM C_BPARTNER
+      WHERE C_BPARTNER_ID = ?
+      ]]></Sql>
+    <Parameter name="cBpartnerId"/>
+  </SqlMethod>
+
   <SqlMethod name="pricelistId" type="preparedStatement" return="String" default="">
     <SqlMethodComment></SqlMethodComment>
     <Sql>
@@ -299,7 +312,7 @@
          , C_DocTypeTarget_ID, DateInvoiced, DateAcct, TaxDate
          , C_BPartner_ID, C_BPartner_Location_ID, AD_User_ID, M_PriceList_ID
          , C_Currency_ID, SalesRep_ID, IsDiscountPrinted, C_Charge_ID
-         , ChargeAmt, PaymentRule, C_PaymentTerm_ID, CreateFrom
+         , ChargeAmt, PaymentRule, FIN_PaymentMethod_ID, C_PaymentTerm_ID, CreateFrom
          
          , GenerateTo, C_Project_ID, C_Activity_ID, C_Campaign_ID
          , AD_OrgTrx_ID, User1_ID, User2_ID, TotalLines
@@ -315,7 +328,7 @@
          , (?), TO_DATE(?), TO_DATE(?), TO_DATE(?)
          , (?), (?), (?), (?)
          , (?), (?), (?), (?)
-         , TO_NUMBER(?), (?), (?), (?)
+         , TO_NUMBER(?), (?), (?), (?), (?)
          
          , (?), (?), (?), (?)
          , (?), (?), (?), TO_NUMBER(?)
@@ -354,6 +367,7 @@
 <Parameter name="cChargeId"/>
 <Parameter name="chargeamt"/>
 <Parameter name="paymentrule"/>
+<Parameter name="paymentmethodId"/>
 <Parameter name="cPaymenttermId"/>
 <Parameter name="createfrom"/>
 <Parameter name="generateto"/>
