Fixed bug 27872: NPE when reactivating payment for BPartner without currency
authorVíctor Martínez Romanos <victor.martinez@openbravo.com>
Wed, 15 Oct 2014 13:40:56 +0200
changeset 25147 8502fee8a8c7
parent 25146 297839b59486
child 25148 6af26661c789
Fixed bug 27872: NPE when reactivating payment for BPartner without currency

When reactivating the payment, the FIN_PaymentProcess was getting the business partner currency's ID where, in some strange circunstances, it may not be defined yet.
Moreover, getting the BP currency ID at this point is not needed at all, so these lines have been removed.

Finally, in another part of the code, we have done a trivial refactor to use Currency objects instead of Strings to have a better control over NPE.
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	Wed Oct 22 10:35:37 2014 +0200
+++ b/modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/process/FIN_PaymentProcess.java	Wed Oct 15 13:40:56 2014 +0200
@@ -766,12 +766,9 @@
         try {
           BusinessPartner businessPartner = payment.getBusinessPartner();
           BigDecimal paidAmount = BigDecimal.ZERO;
-          String fromCurrency = payment.getCurrency().getId();
-          String toCurrency = "";
           if (!(businessPartner == null)) {
             // 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
-            toCurrency = businessPartner.getCurrency().getId();
             if (payment.getGeneratedCredit().compareTo(BigDecimal.ZERO) == 0
                 && payment.getUsedCredit().compareTo(BigDecimal.ZERO) != 0) {
               if (isReceipt) {
@@ -840,11 +837,13 @@
                         .getInvoicePaymentSchedule() != null ? paymentScheduleDetail
                         .getInvoicePaymentSchedule().getInvoice() : null;
                     paidAmount = BigDecimal.ZERO;
-                    fromCurrency = payment.getCurrency().getId();
-                    toCurrency = "";
                     if (!(businessPartner == null)) {
-                      toCurrency = businessPartner.getCurrency().getId();
-                      if (!fromCurrency.equals(toCurrency)) {
+                      final Currency fromCurrency = payment.getCurrency();
+                      // At this point the BP must have a currency, because it is set when
+                      // processing the payment associated to the invoice
+                      final Currency toCurrency = businessPartner.getCurrency();
+                      if (fromCurrency != null && toCurrency != null
+                          && !fromCurrency.getId().equals(toCurrency.getId())) {
                         BigDecimal exchangeRate = BigDecimal.ZERO;
                         // check at invoice document level
                         List<ConversionRateDoc> conversionRateDocumentForInvoice = getConversionRateDocumentForInvoice(
@@ -854,7 +853,7 @@
                         } else {
                           // global
                           exchangeRate = getConversionRate(payment.getOrganization().getId(),
-                              fromCurrency, toCurrency,
+                              fromCurrency.getId(), toCurrency.getId(),
                               invoiceForConversion != null ? invoiceForConversion.getInvoiceDate()
                                   : payment.getPaymentDate());
                         }