Related to issue 30077: code review improvements
authorVíctor Martínez Romanos <victor.martinez@openbravo.com>
Fri, 03 Jul 2015 15:04:14 +0200
changeset 27082 b815e49be324
parent 27081 317d186e5a31
child 27083 22f85795e81f
Related to issue 30077: code review improvements

Use credit popup when completing an invoice only shown when invoice's currency = business partner's currency.
Call getCustomerPaymentsWithCredit() without checking currency != null, because the method already checks it.
Improved warning message with a link to the wiki page.
Amounts are properly rounded.
Improvements in the created payment messages.
Change Business Partner currency: Forced to select a gl item if credit is detected in order to change the currency.
modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/ad_actionbutton/ProcessInvoice.java
modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/dao/AdvPaymentMngtDao.java
src-db/database/sourcedata/AD_MESSAGE.xml
src/org/openbravo/common/actionhandler/SetNewBPCurrency.java
web/js/checkAvailableCredit.js
--- a/modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/ad_actionbutton/ProcessInvoice.java	Fri Jul 03 13:33:24 2015 +0200
+++ b/modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/ad_actionbutton/ProcessInvoice.java	Fri Jul 03 15:04:14 2015 +0200
@@ -296,14 +296,20 @@
             // If the invoice grand total is ZERO or already has payments (due to
             // payment method automation) or the business partner does not have a default financial
             // account defined or invoice's payment method is not inside BP's financial
-            // account do not cancel credit
+            // account or the business partner's currency is not equal to the invoice's currency do
+            // not cancel credit
             if (BigDecimal.ZERO.compareTo(invoice.getGrandTotalAmount()) != 0
                 && isPaymentMethodConfigured(invoice)
                 && !isInvoiceWithPayments(invoice)
                 && (AcctServer.DOCTYPE_ARInvoice.equals(invoiceDocCategory) || AcctServer.DOCTYPE_APInvoice
-                    .equals(invoiceDocCategory))) {
-              creditPayments = dao.getCustomerPaymentsWithCredit(invoice.getOrganization(),
-                  invoice.getBusinessPartner(), invoice.isSalesTransaction());
+                    .equals(invoiceDocCategory))
+                && (invoice.getBusinessPartner().getCurrency() != null && StringUtils.equals(
+                    invoice.getCurrency().getId(), invoice.getBusinessPartner().getCurrency()
+                        .getId()))) {
+              creditPayments = dao
+                  .getCustomerPaymentsWithCredit(invoice.getOrganization(),
+                      invoice.getBusinessPartner(), invoice.isSalesTransaction(),
+                      invoice.getCurrency());
               if (creditPayments != null && !creditPayments.isEmpty()) {
                 printPageCreditPaymentGrid(response, vars, strC_Invoice_ID, strdocaction, strTabId,
                     strC_Invoice_ID, strdocaction, strWindowId, strTabId, invoice.getInvoiceDate(),
--- a/modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/dao/AdvPaymentMngtDao.java	Fri Jul 03 13:33:24 2015 +0200
+++ b/modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/dao/AdvPaymentMngtDao.java	Fri Jul 03 15:04:14 2015 +0200
@@ -1901,12 +1901,7 @@
   public BigDecimal getCustomerCredit(BusinessPartner bp, boolean isReceipt, Organization Org,
       Currency currency) {
     BigDecimal creditAmount = BigDecimal.ZERO;
-    List<FIN_Payment> paymentList = null;
-    if (currency == null) {
-      paymentList = getCustomerPaymentsWithCredit(Org, bp, isReceipt);
-    } else {
-      paymentList = getCustomerPaymentsWithCredit(Org, bp, isReceipt, currency);
-    }
+    List<FIN_Payment> paymentList = getCustomerPaymentsWithCredit(Org, bp, isReceipt, currency);
     for (FIN_Payment payment : paymentList)
       creditAmount = creditAmount.add(payment.getGeneratedCredit()).subtract(
           payment.getUsedCredit());
--- a/src-db/database/sourcedata/AD_MESSAGE.xml	Fri Jul 03 13:33:24 2015 +0200
+++ b/src-db/database/sourcedata/AD_MESSAGE.xml	Fri Jul 03 15:04:14 2015 +0200
@@ -23236,11 +23236,12 @@
 <!--B080C88FF56E4F8594967A6164977D5F-->  <AD_ORG_ID><![CDATA[0]]></AD_ORG_ID>
 <!--B080C88FF56E4F8594967A6164977D5F-->  <ISACTIVE><![CDATA[Y]]></ISACTIVE>
 <!--B080C88FF56E4F8594967A6164977D5F-->  <VALUE><![CDATA[BPCurrencyChange]]></VALUE>
-<!--B080C88FF56E4F8594967A6164977D5F-->  <MSGTEXT><![CDATA[Changing the currency of the Business Partner will not allow you to use available credit in the old currency.
-
-If you want to convert available credit in the old currency into credit in the new currency automatically, select a G/L Item. 
-
-Otherwise, you will have to do it manually following this document: http://wiki.openbravo.com/wiki/How_to_convert_credit_to_a_different_currency]]></MSGTEXT>
+<!--B080C88FF56E4F8594967A6164977D5F-->  <MSGTEXT><![CDATA[Detected available credit for this business partner. <br/>
+Changing the Business Partner's currency will not allow you to use available credit in the old currency.
+
+If you want to convert the available credit in the old currency into credit in the new currency automatically, select a G/L Item. 
+<br/>
+Otherwise you will have to do it manually following this document: <a href='http://wiki.openbravo.com/wiki/How_to_convert_credit_to_a_different_currency' target='_blank'>How to convert credit to a different currency</a>]]></MSGTEXT>
 <!--B080C88FF56E4F8594967A6164977D5F-->  <MSGTYPE><![CDATA[W]]></MSGTYPE>
 <!--B080C88FF56E4F8594967A6164977D5F-->  <AD_MODULE_ID><![CDATA[0]]></AD_MODULE_ID>
 <!--B080C88FF56E4F8594967A6164977D5F-->  <ISINCLUDEINI18N><![CDATA[Y]]></ISINCLUDEINI18N>
--- a/src/org/openbravo/common/actionhandler/SetNewBPCurrency.java	Fri Jul 03 13:33:24 2015 +0200
+++ b/src/org/openbravo/common/actionhandler/SetNewBPCurrency.java	Fri Jul 03 15:04:14 2015 +0200
@@ -114,7 +114,9 @@
         if (!strSetAmount) {
           creditRate = rate;
         } else if (creditUsed.compareTo(BigDecimal.ZERO) != 0) {
-          creditRate = BigDecimal.valueOf(amount).divide(creditUsed);
+          creditRate = BigDecimal.valueOf(amount).divide(creditUsed,
+              FIN_Utility.getConversionRatePrecision(RequestContext.get().getVariablesSecureApp()),
+              RoundingMode.HALF_UP);
         }
 
         // Loop through all payment documents which generate credit
@@ -136,7 +138,7 @@
                 .getDocumentType().getDocumentCategory(), "DocumentNo_FIN_Payment"));
             payment1.setProcessed(false);
             payment1.setPosted("N");
-            payment1.setDescription("GL Item: " + glItem.getName());
+            payment1.setDescription(null);
             payment1.setGeneratedCredit(BigDecimal.ZERO);
             payment1.setUsedCredit(BigDecimal.ZERO);
 
@@ -167,6 +169,14 @@
             OBDal.getInstance().save(paymentScheduleDetail1);
             FIN_PaymentProcess.doProcessPayment(payment1, "D", false, null, null);
 
+            // Modify description of original credit payment
+            String paymentCreditDesc = paymentCredit.getDescription()
+                + "\n"
+                + String.format(OBMessageUtils.messageBD("APRM_CreditUsedPayment"),
+                    payment1.getDocumentNo());
+            paymentCredit.setDescription((paymentCreditDesc.length() > 255) ? paymentCreditDesc
+                .substring(0, 251).concat("...").toString() : paymentCreditDesc.toString());
+
             // Create a payment to refund the credit
             FIN_Payment payment2 = (FIN_Payment) DalUtil.copy(paymentCredit, false);
             payment2.setPaymentDate(new Date());
@@ -175,7 +185,8 @@
                 .getDocumentType().getDocumentCategory(), "DocumentNo_FIN_Payment"));
             payment2.setProcessed(false);
             payment2.setPosted("N");
-            payment2.setDescription("Refunded payment: " + payment1.getDocumentNo());
+            payment2.setDescription(OBMessageUtils.messageBD("APRM_RefundPayment") + ": "
+                + payment1.getDocumentNo());
             payment2.setGeneratedCredit(BigDecimal.ZERO);
             payment2.setUsedCredit(creditAmount);
 
@@ -246,8 +257,10 @@
                 .getDocumentType().getDocumentCategory(), "DocumentNo_FIN_Payment"));
             payment3.setProcessed(false);
             payment3.setPosted("N");
-            payment3.setDescription("GL Item: " + glItem.getName());
-            payment3.setGeneratedCredit(creditAmount.multiply(creditRate));
+            payment3.setDescription(null);
+            final BigDecimal generatedCredit = creditAmount.multiply(creditRate).setScale(
+                currency.getStandardPrecision().intValue(), RoundingMode.HALF_UP);
+            payment3.setGeneratedCredit(generatedCredit);
             payment3.setUsedCredit(BigDecimal.ZERO);
 
             // Create a payment detail to create the credit with a glitem
@@ -256,7 +269,7 @@
             paymentDetail3.setClient(paymentCredit.getClient());
             paymentDetail3.setOrganization(paymentCredit.getOrganization());
             paymentDetail3.setFinPayment(payment3);
-            paymentDetail3.setAmount(creditAmount.multiply(creditRate));
+            paymentDetail3.setAmount(generatedCredit);
             paymentDetail3.setRefund(false);
             paymentDetail3.setPrepayment(true);
 
@@ -266,7 +279,7 @@
             paymentDetail4.setClient(paymentCredit.getClient());
             paymentDetail4.setOrganization(paymentCredit.getOrganization());
             paymentDetail4.setFinPayment(payment3);
-            paymentDetail4.setAmount(creditAmount.multiply(creditRate).negate());
+            paymentDetail4.setAmount(generatedCredit.negate());
             paymentDetail4.setGLItem(glItem);
             paymentDetail4.setRefund(false);
             paymentDetail4.setPrepayment(false);
@@ -277,7 +290,7 @@
             paymentScheduleDetail3.setClient(paymentCredit.getClient());
             paymentScheduleDetail3.setOrganization(paymentCredit.getOrganization());
             paymentScheduleDetail3.setPaymentDetails(paymentDetail3);
-            paymentScheduleDetail3.setAmount(creditAmount.multiply(creditRate));
+            paymentScheduleDetail3.setAmount(generatedCredit);
 
             // Create a payment schedule detail to create the credit with a glitem
             FIN_PaymentScheduleDetail paymentScheduleDetail4 = OBProvider.getInstance().get(
@@ -285,7 +298,7 @@
             paymentScheduleDetail4.setClient(paymentCredit.getClient());
             paymentScheduleDetail4.setOrganization(paymentCredit.getOrganization());
             paymentScheduleDetail4.setPaymentDetails(paymentDetail4);
-            paymentScheduleDetail4.setAmount(creditAmount.multiply(creditRate).negate());
+            paymentScheduleDetail4.setAmount(generatedCredit.negate());
 
             // Process the payment
             paymentDetail3.getFINPaymentScheduleDetailList().add(paymentScheduleDetail3);
@@ -297,6 +310,7 @@
             OBDal.getInstance().save(paymentDetail4);
             OBDal.getInstance().save(paymentScheduleDetail3);
             OBDal.getInstance().save(paymentScheduleDetail4);
+            OBDal.getInstance().save(paymentCredit);
             FIN_PaymentProcess.doProcessPayment(payment3, "D", false, null, null);
 
             i++;
--- a/web/js/checkAvailableCredit.js	Fri Jul 03 13:33:24 2015 +0200
+++ b/web/js/checkAvailableCredit.js	Fri Jul 03 15:04:14 2015 +0200
@@ -26,12 +26,19 @@
 
   callback = function (response, data, request) {
     if (data.availableCredit) {
-      view.messageBar.setMessage(isc.OBMessageBar.TYPE_INFO, null, OB.I18N.getLabel('BPCurrencyChange'));
+      view.messageBar.setMessage(isc.OBMessageBar.TYPE_WARNING, null, OB.I18N.getLabel('BPCurrencyChange'));
       form.getItem('c_glitem_id').visible = true;
+      form.getItem('c_glitem_id').setRequired(true);
+      if (form.getItem('c_glitem_id').textBoxStyle.indexOf('Required') == -1) {
+        form.getItem('c_glitem_id').textBoxStyle += 'Required';
+      }
     } else {
       form.getItem('c_glitem_id').visible = false;
     }
     form.redraw();
+    if (view) {
+      view.handleButtonsStatus();
+    }
   };
 
   OB.RemoteCallManager.call('org.openbravo.common.actionhandler.CheckAvailableCreditActionHandler', {