details:   https://code.openbravo.com/erp/devel/pi/rev/b1843f200a90
changeset: 23951:b1843f200a90
user:      Miguel A. Alsasua <miguel.alsasua <at> openbravo.com>
date:      Tue Jul 01 16:54:52 2014 +0200
summary:   fixed issue 25392:  Wrong customer balance when working with 
multicurrency

diffstat:

 
modules/org.openbravo.advpaymentmngt/src-db/database/model/functions/APRM_GEN_PAYMENTSCHEDULE_INV.xml
 |   86 +++++-
 
modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/process/FIN_PaymentProcess.java
 |  137 ++++++++-
 src-db/database/model/tables/C_BPARTNER.xml                                    
                       |   11 +
 src-db/database/sourcedata/AD_COLUMN.xml                                       
                       |   74 ++++-
 src-db/database/sourcedata/AD_ELEMENT.xml                                      
                       |   54 +++-
 src-db/database/sourcedata/AD_FIELD.xml                                        
                       |  102 ++++++-
 src-db/database/sourcedata/AD_MESSAGE.xml                                      
                       |   12 +
 src-db/database/sourcedata/AD_REFERENCE.xml                                    
                       |   12 +
 src-db/database/sourcedata/OBUIAPP_PARAMETER.xml                               
                       |  118 +++++++
 src-db/database/sourcedata/OBUIAPP_PROCESS.xml                                 
                       |   19 +
 src-db/database/sourcedata/OBUISEL_SELECTOR.xml                                
                       |   16 +
 
src-util/modulescript/build/classes/org/openbravo/modulescript/InitializeBPCurrency.class
             |    0 
 
src-util/modulescript/build/classes/org/openbravo/modulescript/InitializeBPCurrencyData.class
         |    0 
 src-util/modulescript/src/org/openbravo/modulescript/InitializeBPCurrency.java 
                       |   44 ++
 
src-util/modulescript/src/org/openbravo/modulescript/InitializeBPCurrency_data.xsql
                   |   70 ++++
 src/org/openbravo/common/actionhandler/SetNewBPCurrency.java                   
                       |  150 ++++++++++
 src/org/openbravo/event/BusinessPartnerEventHandler.java                       
                       |   69 ++++
 17 files changed, 948 insertions(+), 26 deletions(-)

diffs (truncated from 1348 to 300 lines):

diff -r 27996e18590d -r b1843f200a90 
modules/org.openbravo.advpaymentmngt/src-db/database/model/functions/APRM_GEN_PAYMENTSCHEDULE_INV.xml
--- 
a/modules/org.openbravo.advpaymentmngt/src-db/database/model/functions/APRM_GEN_PAYMENTSCHEDULE_INV.xml
     Tue Jul 01 17:32:24 2014 +0530
+++ 
b/modules/org.openbravo.advpaymentmngt/src-db/database/model/functions/APRM_GEN_PAYMENTSCHEDULE_INV.xml
     Tue Jul 01 16:54:52 2014 +0200
@@ -32,6 +32,7 @@
 V_OutstandingAmount NUMBER;
 V_DueAmount NUMBER;
 V_DayStillDue NUMBER;
+v_ConvertedAmount NUMBER;
 
 v_Client_ID VARCHAR2(32);
 v_Org_ID VARCHAR2(32);
@@ -43,6 +44,7 @@
 v_PaymentTerm C_INVOICE.C_PaymentTerm_ID%TYPE;
 v_PaymentRule C_INVOICE.PaymentRule%TYPE;
 v_Currency_ID VARCHAR2(32);
+v_bpCurrency_id VARCHAR2(32);
 v_BPartner_ID VARCHAR2(32);
 v_DateInvoiced DATE;
 v_TotalLines NUMBER;
@@ -136,6 +138,20 @@
     v_creditmultiplier := -1;
   END IF;
 
+  --Gets the business partner's currency
+  SELECT BP_Currency_ID
+  INTO v_bpCurrency_id
+  from C_BPartner
+  WHERE C_BPartner_ID = v_BPartner_ID;
+  
+  IF (v_bpCurrency_id is null) THEN
+    v_bpCurrency_id:= v_Currency_ID;
+
+    update c_bpartner
+    set BP_Currency_ID = v_bpCurrency_id
+    where c_bpartner_id =v_BPartner_ID;
+  END IF;
+
   -- complete invoice
   IF (p_docaction = 'CO') THEN
     -- Checking Payment Method ID is available
@@ -595,8 +611,29 @@
       WHERE C_INVOICE_ID = P_RECORD_ID;
 
       -- Updating SO_CREDITUSED when Invoices are being processed
+      if(v_bpCurrency_id <> v_Currency_id) THEN
+        SELECT COUNT(*)
+        INTO v_Count
+        FROM C_Conversion_Rate_Document
+        WHERE C_Invoice_ID = P_RECORD_ID
+        AND C_Currency_ID = v_Currency_id
+        AND C_Currency_Id_To = v_bpCurrency_id;
+
+        IF v_Count >0 THEN
+          SELECT RATE*COALESCE(V_OutstandingAmount, 0) * v_creditmultiplier
+          INTO v_ConvertedAmount
+          FROM C_Conversion_Rate_Document
+          WHERE C_Invoice_ID = P_RECORD_ID
+          AND C_Currency_ID = v_Currency_id
+          AND C_Currency_Id_To = v_bpCurrency_id;        
+        ELSE
+          v_ConvertedAmount := 
C_Currency_Convert((COALESCE(V_OutstandingAmount, 0) * v_creditmultiplier), 
v_Currency_id, v_bpCurrency_id, v_DateInvoiced,'S', v_Client_ID, v_Org_ID);
+        END IF;
+      else
+        v_ConvertedAmount := COALESCE(V_OutstandingAmount, 0) * 
v_creditmultiplier;
+      end if;
       UPDATE C_BPARTNER
-      SET SO_CREDITUSED = COALESCE(SO_CREDITUSED,0) + 
(COALESCE(V_OutstandingAmount, 0) * v_creditmultiplier)
+      SET SO_CREDITUSED = COALESCE(SO_CREDITUSED,0) + v_ConvertedAmount
       WHERE C_BPARTNER_ID = v_BPartner_ID;
 
       -- Automatic creation of payment and financial transaction
@@ -923,8 +960,29 @@
                 WHERE C_INVOICE_ID = P_RECORD_ID
                   AND TOTALPAID = GRANDTOTAL;
                 -- Updating SO_CREDITUSED when PAYMENT OCCUR and is not set in 
RPAE status
+                if (v_bpCurrency_id <> v_Currency_id) then
+                  SELECT COUNT(*)
+                  INTO v_Count
+                  FROM C_Conversion_Rate_Document
+                  WHERE C_Invoice_ID = P_RECORD_ID
+                  AND C_Currency_ID = v_Currency_id
+                  AND C_Currency_Id_To = v_bpCurrency_id;
+
+                  IF v_Count >0 THEN
+                    SELECT RATE*(COALESCE(cur_paymentschedule.OUTSTANDINGAMT, 
0) * v_creditmultiplier)
+                    INTO v_ConvertedAmount
+                    FROM C_Conversion_Rate_Document
+                    WHERE C_Invoice_ID = P_RECORD_ID
+                    AND C_Currency_ID = v_Currency_id
+                    AND C_Currency_Id_To = v_bpCurrency_id;
+                  ELSE
+                    v_ConvertedAmount := 
C_Currency_Convert((COALESCE(cur_paymentschedule.OUTSTANDINGAMT, 0) * 
v_creditmultiplier), v_Currency_id, v_bpCurrency_id, v_DateInvoiced,'S', 
v_Client_ID, v_Org_ID);
+                  END IF;  
+                else
+                  v_ConvertedAmount := 
(COALESCE(cur_paymentschedule.OUTSTANDINGAMT, 0) * v_creditmultiplier);
+                end if;
                 UPDATE C_BPARTNER
-                SET SO_CREDITUSED = COALESCE(SO_CREDITUSED,0) - 
(COALESCE(cur_paymentschedule.OUTSTANDINGAMT, 0) * v_creditmultiplier)
+                SET SO_CREDITUSED = COALESCE(SO_CREDITUSED,0) - 
v_ConvertedAmount
                 WHERE C_BPARTNER_ID = v_BPartner_ID;
               END IF;
             END LOOP;
@@ -1023,8 +1081,30 @@
       INTO v_PaidAmount, v_PaymentPlanExpectedTotal
       FROM FIN_PAYMENT_SCHEDULE
       WHERE FIN_PAYMENT_SCHEDULE.C_INVOICE_ID = p_record_id ;
+
+      if v_bpCurrency_id <> v_Currency_id then
+        SELECT COUNT(*)
+        INTO v_Count
+        FROM C_Conversion_Rate_Document
+        WHERE C_Invoice_ID = P_RECORD_ID
+        AND C_Currency_ID = v_Currency_id
+        AND C_Currency_Id_To = v_bpCurrency_id;
+
+        IF v_Count >0 THEN
+          SELECT RATE*((v_PaymentPlanExpectedTotal - v_PaidAmount) * 
v_creditmultiplier)
+          INTO v_ConvertedAmount
+          FROM C_Conversion_Rate_Document
+          WHERE C_Invoice_ID = P_RECORD_ID
+          AND C_Currency_ID = v_Currency_id
+          AND C_Currency_Id_To = v_bpCurrency_id;
+        ELSE
+          v_ConvertedAmount := C_Currency_Convert(((v_PaymentPlanExpectedTotal 
- v_PaidAmount) * v_creditmultiplier), v_Currency_id, v_bpCurrency_id, 
v_DateInvoiced,'S', v_Client_ID, v_Org_ID);
+        END IF;  
+      else
+        v_ConvertedAmount := ((v_PaymentPlanExpectedTotal - v_PaidAmount) * 
v_creditmultiplier);
+      END IF;
       UPDATE C_BPARTNER
-         SET SO_CREDITUSED = COALESCE(SO_CREDITUSED,0) - 
((v_PaymentPlanExpectedTotal - v_PaidAmount) * v_creditmultiplier)
+         SET SO_CREDITUSED = COALESCE(SO_CREDITUSED,0) - v_convertedAmount
       WHERE C_BPARTNER_ID = v_BPartner_ID;
     END;
 
diff -r 27996e18590d -r b1843f200a90 
modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/process/FIN_PaymentProcess.java
--- 
a/modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/process/FIN_PaymentProcess.java
     Tue Jul 01 17:32:24 2014 +0530
+++ 
b/modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/process/FIN_PaymentProcess.java
     Tue Jul 01 16:54:52 2014 +0200
@@ -19,7 +19,9 @@
 package org.openbravo.advpaymentmngt.process;
 
 import java.math.BigDecimal;
+import java.math.RoundingMode;
 import java.util.ArrayList;
+import java.util.Date;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
@@ -34,6 +36,7 @@
 import org.openbravo.base.exception.OBException;
 import org.openbravo.base.provider.OBProvider;
 import org.openbravo.base.secureApp.VariablesSecureApp;
+import org.openbravo.client.kernel.RequestContext;
 import org.openbravo.dal.core.DalUtil;
 import org.openbravo.dal.core.OBContext;
 import org.openbravo.dal.service.OBCriteria;
@@ -44,7 +47,9 @@
 import org.openbravo.erpCommon.utility.OBMessageUtils;
 import org.openbravo.erpCommon.utility.Utility;
 import org.openbravo.model.common.businesspartner.BusinessPartner;
+import org.openbravo.model.common.currency.ConversionRate;
 import org.openbravo.model.common.currency.ConversionRateDoc;
+import org.openbravo.model.common.currency.Currency;
 import org.openbravo.model.common.enterprise.Organization;
 import org.openbravo.model.common.invoice.Invoice;
 import 
org.openbravo.model.financialmgmt.accounting.FIN_FinancialAccountAccounting;
@@ -57,7 +62,6 @@
 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;
 import org.openbravo.service.db.DalConnectionProvider;
 
@@ -302,17 +306,19 @@
               OBDal.getInstance().rollbackAndClose();
               return;
             }
-            PriceList priceList = payment.isReceipt() ? 
businessPartner.getPriceList()
-                : businessPartner.getPurchasePricelist();
-            if (!payment.getCurrency().getId()
-                .equals(priceList != null ? priceList.getCurrency().getId() : 
"")) {
+            String currency = null;
+            if (businessPartner.getCurrency() == null) {
+              currency = payment.getCurrency().getId();
+              businessPartner.setCurrency(payment.getCurrency());
+            } else {
+              currency = businessPartner.getCurrency().getId();
+            }
+            if (!payment.getCurrency().getId().equals(currency)) {
               msg.setType("Error");
               msg.setTitle(Utility.messageBD(conProvider, "Error", language));
               msg.setMessage(String.format(
                   Utility.parseTranslation(conProvider, vars, language, 
"@APRM_CreditCurrency@"),
-                  priceList != null ? priceList.getCurrency().getISOCode() : 
Utility
-                      .parseTranslation(conProvider, vars, language,
-                          "@APRM_CreditNoPricelistCurrency@")));
+                  businessPartner.getCurrency().getISOCode()));
               bundle.setResult(msg);
               OBDal.getInstance().rollbackAndClose();
               return;
@@ -402,10 +408,44 @@
                         .getInvoice().getBusinessPartner();
 
                     // Payments update credit opposite to invoices
+                    BigDecimal paidAmount = BigDecimal.ZERO;
+                    Invoice invoiceForConversion = paymentScheduleDetail
+                        .getInvoicePaymentSchedule() != null ? 
paymentScheduleDetail
+                        .getInvoicePaymentSchedule().getInvoice() : null;
+                    paidAmount = BigDecimal.ZERO;
+                    String fromCurrency = payment.getCurrency().getId();
+                    String toCurrency = businessPartner.getCurrency().getId();
+                    if (!fromCurrency.equals(toCurrency)) {
+                      BigDecimal exchangeRate = BigDecimal.ZERO;
+                      // check at invoice document level
+                      List<ConversionRateDoc> conversionRateDocumentForInvoice 
= getConversionRateDocumentForInvoice(
+                          invoiceForConversion, isReceipt);
+                      if (conversionRateDocumentForInvoice.size() > 0) {
+                        exchangeRate = 
conversionRateDocumentForInvoice.get(0).getRate();
+                      } else {
+                        // global
+                        exchangeRate = 
getConversionRate(payment.getOrganization().getId(),
+                            fromCurrency, toCurrency,
+                            invoiceForConversion != null ? 
invoiceForConversion.getInvoiceDate()
+                                : payment.getPaymentDate());
+                      }
+                      if (exchangeRate == BigDecimal.ZERO) {
+                        msg.setType("Error");
+                        msg.setTitle(Utility.messageBD(conProvider, "Error", 
language));
+                        msg.setMessage(Utility.parseTranslation(conProvider, 
vars, language,
+                            "@NoCurrencyConversion@"));
+                        bundle.setResult(msg);
+                        OBDal.getInstance().rollbackAndClose();
+                        return;
+                      }
+                      paidAmount = amount.multiply(exchangeRate);
+                    } else {
+                      paidAmount = amount;
+                    }
                     if (isReceipt) {
-                      decreaseCustomerCredit(businessPartner, amount);
+                      decreaseCustomerCredit(businessPartner, paidAmount);
                     } else {
-                      increaseCustomerCredit(businessPartner, amount);
+                      increaseCustomerCredit(businessPartner, paidAmount);
                     }
                     FIN_AddPayment.updatePaymentScheduleAmounts(paymentDetail,
                         paymentScheduleDetail.getInvoicePaymentSchedule(),
@@ -727,6 +767,9 @@
           BusinessPartner businessPartner = payment.getBusinessPartner();
           // When credit is used (consumed) we compensate so_creditused as 
this amount is already
           // included in the payment details. Credit consumed should not 
affect to so_creditused
+          BigDecimal paidAmount = BigDecimal.ZERO;
+          String fromCurrency = payment.getCurrency().getId();
+          String toCurrency = businessPartner.getCurrency().getId();
           if (payment.getGeneratedCredit().compareTo(BigDecimal.ZERO) == 0
               && payment.getUsedCredit().compareTo(BigDecimal.ZERO) != 0) {
             if (isReceipt) {
@@ -790,10 +833,43 @@
                     // BP SO_CreditUsed
                     businessPartner = 
paymentScheduleDetail.getInvoicePaymentSchedule()
                         .getInvoice().getBusinessPartner();
+                    Invoice invoiceForConversion = paymentScheduleDetail
+                        .getInvoicePaymentSchedule() != null ? 
paymentScheduleDetail
+                        .getInvoicePaymentSchedule().getInvoice() : null;
+                    paidAmount = BigDecimal.ZERO;
+                    fromCurrency = payment.getCurrency().getId();
+                    toCurrency = businessPartner.getCurrency().getId();
+                    if (!fromCurrency.equals(toCurrency)) {
+                      BigDecimal exchangeRate = BigDecimal.ZERO;
+                      // check at invoice document level
+                      List<ConversionRateDoc> conversionRateDocumentForInvoice 
= getConversionRateDocumentForInvoice(
+                          invoiceForConversion, isReceipt);
+                      if (conversionRateDocumentForInvoice.size() > 0) {
+                        exchangeRate = 
conversionRateDocumentForInvoice.get(0).getRate();
+                      } else {
+                        // global
+                        exchangeRate = 
getConversionRate(payment.getOrganization().getId(),
+                            fromCurrency, toCurrency,
+                            invoiceForConversion != null ? 
invoiceForConversion.getInvoiceDate()
+                                : payment.getPaymentDate());
+                      }
+                      if (exchangeRate == BigDecimal.ZERO) {
+                        msg.setType("Error");
+                        msg.setTitle(Utility.messageBD(conProvider, "Error", 
language));
+                        msg.setMessage(Utility.parseTranslation(conProvider, 
vars, language,
+                            "@NoCurrencyConversion@"));
+                        bundle.setResult(msg);
+                        OBDal.getInstance().rollbackAndClose();
+                        return;
+                      }
+                      paidAmount = amount.multiply(exchangeRate);
+                    } else {
+                      paidAmount = amount;
+                    }
                     if (isReceipt) {
-                      increaseCustomerCredit(businessPartner, amount);
+                      increaseCustomerCredit(businessPartner, paidAmount);
                     } else {
-                      decreaseCustomerCredit(businessPartner, amount);
+                      decreaseCustomerCredit(businessPartner, paidAmount);

------------------------------------------------------------------------------
Open source business process management suite built on Java and Eclipse
Turn processes into business applications with Bonita BPM Community Edition
Quickly connect people, data, and systems into organized workflows
Winner of BOSSIE, CODIE, OW2 and Gartner awards
http://p.sf.net/sfu/Bonitasoft
_______________________________________________
Openbravo-commits mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openbravo-commits

Reply via email to