Validation rule and tab restrictions
authorSalvador Zapata <salvador.zapata@openbravo.com>
Fri, 24 Jun 2011 16:31:50 +0200
changeset 13325 c2d21db7bb30
parent 13324 4c67babc7810
child 13326 ecd741920f59
Validation rule and tab restrictions
src-db/database/model/tables/C_CONVERSION_RATE_DOCUMENT.xml
src-db/database/sourcedata/AD_CALLOUT.xml
src/org/openbravo/erpCommon/ad_callouts/SE_CalculateExchangeRate.java
--- a/src-db/database/model/tables/C_CONVERSION_RATE_DOCUMENT.xml	Wed Jun 22 22:09:10 2011 +0200
+++ b/src-db/database/model/tables/C_CONVERSION_RATE_DOCUMENT.xml	Fri Jun 24 16:31:50 2011 +0200
@@ -41,19 +41,19 @@
         <default/>
         <onCreateDefault/>
       </column>
-      <column name="C_INVOICE_ID" primaryKey="false" required="true" type="VARCHAR" size="32" autoIncrement="false">
+      <column name="C_INVOICE_ID" primaryKey="false" required="false" type="VARCHAR" size="32" autoIncrement="false">
         <default/>
         <onCreateDefault/>
       </column>
-      <column name="FIN_PAYMENT_ID" primaryKey="false" required="true" type="VARCHAR" size="32" autoIncrement="false">
+      <column name="FIN_PAYMENT_ID" primaryKey="false" required="false" type="VARCHAR" size="32" autoIncrement="false">
         <default/>
         <onCreateDefault/>
       </column>
-      <column name="APRM_FINACC_TRANSACTION_V_ID" primaryKey="false" required="true" type="VARCHAR" size="32" autoIncrement="false">
+      <column name="APRM_FINACC_TRANSACTION_V_ID" primaryKey="false" required="false" type="VARCHAR" size="32" autoIncrement="false">
         <default/>
         <onCreateDefault/>
       </column>
-      <column name="RATE" primaryKey="false" required="false" type="DECIMAL" autoIncrement="false">
+      <column name="RATE" primaryKey="false" required="true" type="DECIMAL" autoIncrement="false">
         <default/>
         <onCreateDefault/>
       </column>
@@ -67,6 +67,12 @@
       <foreign-key foreignTable="C_CURRENCY" name="C_CURRENCYRATEDOC_C_CURRENCY">
         <reference local="C_CURRENCY_ID" foreign="C_CURRENCY_ID"/>
       </foreign-key>
+      <foreign-key foreignTable="AD_CLIENT" name="C_CURRENCYRATEDOC_CLIENT">
+        <reference local="AD_CLIENT_ID" foreign="AD_CLIENT_ID"/>
+      </foreign-key>
+      <foreign-key foreignTable="AD_ORG" name="C_CURRENCYRATEDOC_ORG">
+        <reference local="AD_ORG_ID" foreign="AD_ORG_ID"/>
+      </foreign-key>
       <foreign-key foreignTable="C_INVOICE" name="C_CURRENCYRATEDOCINV">
         <reference local="C_INVOICE_ID" foreign="C_INVOICE_ID"/>
       </foreign-key>
--- a/src-db/database/sourcedata/AD_CALLOUT.xml	Wed Jun 22 22:09:10 2011 +0200
+++ b/src-db/database/sourcedata/AD_CALLOUT.xml	Fri Jun 24 16:31:50 2011 +0200
@@ -977,4 +977,13 @@
 <!--FF8081812FBFF0CC012FBFF53E16000A-->  <AD_MODULE_ID><![CDATA[0]]></AD_MODULE_ID>
 <!--FF8081812FBFF0CC012FBFF53E16000A--></AD_CALLOUT>
 
+<!--FF80818130BD09B60130BD15FF1C000F--><AD_CALLOUT>
+<!--FF80818130BD09B60130BD15FF1C000F-->  <AD_CALLOUT_ID><![CDATA[FF80818130BD09B60130BD15FF1C000F]]></AD_CALLOUT_ID>
+<!--FF80818130BD09B60130BD15FF1C000F-->  <AD_CLIENT_ID><![CDATA[0]]></AD_CLIENT_ID>
+<!--FF80818130BD09B60130BD15FF1C000F-->  <AD_ORG_ID><![CDATA[0]]></AD_ORG_ID>
+<!--FF80818130BD09B60130BD15FF1C000F-->  <ISACTIVE><![CDATA[Y]]></ISACTIVE>
+<!--FF80818130BD09B60130BD15FF1C000F-->  <NAME><![CDATA[SE_CalculateExchangeRate]]></NAME>
+<!--FF80818130BD09B60130BD15FF1C000F-->  <AD_MODULE_ID><![CDATA[0]]></AD_MODULE_ID>
+<!--FF80818130BD09B60130BD15FF1C000F--></AD_CALLOUT>
+
 </data>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/org/openbravo/erpCommon/ad_callouts/SE_CalculateExchangeRate.java	Fri Jun 24 16:31:50 2011 +0200
@@ -0,0 +1,77 @@
+/*
+ *************************************************************************
+ * The contents of this file are subject to the Openbravo  Public  License
+ * Version  1.1  (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
+ * 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) 2010 Openbravo SLU
+ * All Rights Reserved.
+ * Contributor(s):  ______________________________________.
+ ************************************************************************
+ */
+package org.openbravo.erpCommon.ad_callouts;
+
+import java.math.BigDecimal;
+
+import javax.servlet.ServletException;
+
+import org.openbravo.advpaymentmngt.APRM_FinaccTransactionV;
+import org.openbravo.dal.service.OBDal;
+import org.openbravo.model.common.invoice.Invoice;
+import org.openbravo.model.financialmgmt.payment.FIN_Payment;
+
+public class SE_CalculateExchangeRate extends SimpleCallout {
+
+  private static final long serialVersionUID = 1L;
+  private static final String ADWINDOW_PurchaseInvoice = "183";
+  private static final String ADWINDOW_SalesInvoice = "167";
+  private static final String ADWINDOW_PaymentOut = "6F8F913FA60F4CBD93DC1D3AA696E76E";
+  private static final String ADWINDOW_PaymentIn = "E547CE89D4C04429B6340FFA44E70716";
+  private static final String ADWINDOW_Transaction = "94EAA455D2644E04AB25D93BE5157B6D";
+
+  @Override
+  protected void execute(CalloutInfo info) throws ServletException {
+    String strWindowId = info.getWindowId();
+
+    String strLastFieldChanged = info.getLastFieldChanged();
+    BigDecimal originalAmt = new BigDecimal(BigDecimal.ZERO.toString());
+    BigDecimal rate = new BigDecimal(info.vars.getStringParameter("inprate"));
+    BigDecimal foreignAmt = new BigDecimal(info.vars.getStringParameter("inpforeignAmount"));
+    try {
+      // info.addResult("inppayinAllow", paymentMethod.isPayinAllow() ? "Y" : "N");
+      if (strWindowId.equals(ADWINDOW_PurchaseInvoice) || strWindowId.equals(ADWINDOW_SalesInvoice)) {
+        String strcInvoiceId = info.vars.getStringParameter("inpcInvoiceId");
+        Invoice invoice = OBDal.getInstance().get(Invoice.class, strcInvoiceId);
+        originalAmt = invoice.getGrandTotalAmount();
+
+      } else if (strWindowId.equals(ADWINDOW_PaymentOut) || strWindowId.equals(ADWINDOW_PaymentIn)) {
+        String strfinPaymentId = info.vars.getStringParameter("inpfinPaymentId");
+        FIN_Payment payment = OBDal.getInstance().get(FIN_Payment.class, strfinPaymentId);
+        originalAmt = payment.getAmount();
+      } else if (strWindowId.equals(ADWINDOW_Transaction)) {
+        String strfinTransactionId = info.vars.getStringParameter("inpaprmFinaccTransactionVId");
+        APRM_FinaccTransactionV transaction = OBDal.getInstance().get(
+            APRM_FinaccTransactionV.class, strfinTransactionId);
+        originalAmt = transaction.getForeignAmount();
+      }
+      if (strLastFieldChanged.equals("inprate")) {
+        foreignAmt = originalAmt.multiply(rate);
+        info.addResult("inpforeignAmount", foreignAmt);
+      } else if (strLastFieldChanged.equals("inpforeignAmount")) {
+        rate = foreignAmt.divide(originalAmt, 12, 4);
+        info.addResult("inprate", rate);
+      }
+
+    } catch (Exception e) {
+      log4j.info("No default info for the selected payment method");
+    }
+
+  }
+}