details:   https://code.openbravo.com/erp/devel/pi/rev/50eb50e31627
changeset: 25229:50eb50e31627
user:      Gorka Ion Damián <gorkaion.damian <at> openbravo.com>
date:      Thu Nov 13 10:52:31 2014 +0100
summary:   Related to issue 28155.Related to issue 28157.Use conversionRateDocs

Some documents might include custom conversion rates. The FinancialUtils
utility class is extended with a new method that receives a List of rates
defined at document level. The existing method now calls the new one setting
an empty list on the new parameter.

details:   https://code.openbravo.com/erp/devel/pi/rev/b43300702aff
changeset: 25230:b43300702aff
user:      Gorka Ion Damián <gorkaion.damian <at> openbravo.com>
date:      Thu Nov 13 11:01:31 2014 +0100
summary:   Fixed issue 28155. Use custom conversion rate of documents.

Use new getConvertedAmount with the list of conversion rated defined for the
invoice.
Create a Invoice object to ease method readability.

diffstat:

 src/org/openbravo/costing/PriceDifferenceProcess.java |  15 +++--
 src/org/openbravo/financial/FinancialUtils.java       |  49 +++++++++++++++---
 2 files changed, 49 insertions(+), 15 deletions(-)

diffs (147 lines):

diff -r f007a86ec722 -r b43300702aff 
src/org/openbravo/costing/PriceDifferenceProcess.java
--- a/src/org/openbravo/costing/PriceDifferenceProcess.java     Thu Nov 13 
12:48:51 2014 +0100
+++ b/src/org/openbravo/costing/PriceDifferenceProcess.java     Thu Nov 13 
11:01:31 2014 +0100
@@ -34,6 +34,7 @@
 import org.openbravo.financial.FinancialUtils;
 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.common.plm.Product;
 import org.openbravo.model.materialmgmt.cost.CostAdjustment;
 import org.openbravo.model.materialmgmt.cost.CostAdjustmentLine;
@@ -77,11 +78,12 @@
     BigDecimal expectedCost = BigDecimal.ZERO;
     BigDecimal invoiceQty = BigDecimal.ZERO;
     for (ReceiptInvoiceMatch matchInv : 
receiptLine.getProcurementReceiptInvoiceMatchList()) {
-      if 
(matchInv.getInvoiceLine().getInvoice().getDocumentStatus().equals("VO")) {
+      Invoice invoice = matchInv.getInvoiceLine().getInvoice();
+      if (invoice.getDocumentStatus().equals("VO")) {
         // Skip voided invoices.
         continue;
       }
-      if (!matchInv.getInvoiceLine().getInvoice().isProcessed()) {
+      if (!invoice.isProcessed()) {
         // Skip not processed invoices.
         continue;
       }
@@ -92,12 +94,13 @@
         invoiceQty = invoiceQty.add(matchInv.getQuantity());
       }
       invoiceAmt = 
matchInv.getQuantity().multiply(matchInv.getInvoiceLine().getUnitPrice());
-      invoiceAmt = FinancialUtils.getConvertedAmount(invoiceAmt, 
matchInv.getInvoiceLine()
-          .getInvoice().getCurrency(), trxCurrency, trxDate, trxOrg,
-          FinancialUtils.PRECISION_STANDARD);
+
+      invoiceAmt = FinancialUtils.getConvertedAmount(invoiceAmt, 
invoice.getCurrency(),
+          trxCurrency, trxDate, trxOrg, FinancialUtils.PRECISION_STANDARD,
+          invoice.getCurrencyConversionRateDocList());
       expectedCost = expectedCost.add(invoiceAmt);
 
-      Date invoiceDate = 
matchInv.getInvoiceLine().getInvoice().getInvoiceDate();
+      Date invoiceDate = invoice.getInvoiceDate();
       if (costAdjDateAcct == null || costAdjDateAcct.before(invoiceDate)) {
         costAdjDateAcct = invoiceDate;
       }
diff -r f007a86ec722 -r b43300702aff 
src/org/openbravo/financial/FinancialUtils.java
--- a/src/org/openbravo/financial/FinancialUtils.java   Thu Nov 13 12:48:51 
2014 +0100
+++ b/src/org/openbravo/financial/FinancialUtils.java   Thu Nov 13 11:01:31 
2014 +0100
@@ -21,6 +21,7 @@
 import java.math.BigDecimal;
 import java.math.RoundingMode;
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.Date;
 import java.util.List;
 
@@ -28,14 +29,17 @@
 import org.apache.log4j.Logger;
 import org.hibernate.criterion.Restrictions;
 import org.openbravo.base.exception.OBException;
+import org.openbravo.base.util.Check;
 import org.openbravo.dal.core.DalUtil;
 import org.openbravo.dal.core.OBContext;
 import org.openbravo.dal.service.OBCriteria;
 import org.openbravo.dal.service.OBDal;
 import org.openbravo.dal.service.OBQuery;
 import org.openbravo.erpCommon.utility.OBDateUtils;
+import org.openbravo.erpCommon.utility.OBMessageUtils;
 import org.openbravo.model.ad.system.Client;
 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.plm.Product;
@@ -224,6 +228,17 @@
   }
 
   /**
+   * It calls
+   * {@link FinancialUtils#getConvertedAmount(BigDecimal, Currency, Currency, 
Date, Organization, String, List)}
+   * with an empty list of conversion rates at document level
+   */
+  public static BigDecimal getConvertedAmount(BigDecimal amount, Currency 
curFrom, Currency curTo,
+      Date date, Organization org, String strPrecision) throws OBException {
+    return getConvertedAmount(amount, curFrom, curTo, date, org, strPrecision,
+        Collections.<ConversionRateDoc> emptyList());
+  }
+
+  /**
    * Converts an amount.
    * 
    * @param amount
@@ -238,21 +253,38 @@
    *          Organization of the document that needs to be converted.
    * @param strPrecision
    *          type of precision to be used to round the converted amount.
+   * @param rateDocs
+   *          list of conversion rates defined on the document of the amount 
that needs to be
+   *          converted.
    * @return a BigDecimal representing the converted amount.
    * @throws OBException
    *           when no Conversion Rate is found for the given parameters.
    */
-
   public static BigDecimal getConvertedAmount(BigDecimal amount, Currency 
curFrom, Currency curTo,
-      Date date, Organization org, String strPrecision) throws OBException {
+      Date date, Organization org, String strPrecision, 
List<ConversionRateDoc> rateDocs)
+      throws OBException {
+    Check.isNotNull(rateDocs, OBMessageUtils.messageBD("ParameterMissing") + " 
(rateDocs)");
     if (curFrom.getId().equals(curTo.getId()) || amount.signum() == 0) {
       return amount;
     }
-    ConversionRate cr = getConversionRate(date, curFrom, curTo, org, 
org.getClient());
-    if (cr == null) {
-      throw new OBException("@NoCurrencyConversion@ " + curFrom.getISOCode() + 
" @to@ "
-          + curTo.getISOCode() + " @ForDate@ " + OBDateUtils.formatDate(date)
-          + " @And@ @ACCS_AD_ORG_ID_D@ " + org.getIdentifier());
+    BigDecimal rate = null;
+    if (rateDocs.size() > 0) {
+      for (ConversionRateDoc rateDoc : rateDocs) {
+        if (curFrom.getId().equals(rateDoc.getCurrency().getId())
+            && curTo.getId().equals(rateDoc.getToCurrency().getId())) {
+          rate = rateDoc.getRate();
+          break;
+        }
+      }
+    }
+    if (rate == null) {
+      ConversionRate cr = getConversionRate(date, curFrom, curTo, org, 
org.getClient());
+      if (cr == null) {
+        throw new OBException("@NoCurrencyConversion@ " + curFrom.getISOCode() 
+ " @to@ "
+            + curTo.getISOCode() + " @ForDate@ " + OBDateUtils.formatDate(date)
+            + " @And@ @ACCS_AD_ORG_ID_D@ " + org.getIdentifier());
+      }
+      rate = cr.getMultipleRateBy();
     }
     Long precision = curTo.getStandardPrecision();
     if (PRECISION_COSTING.equals(strPrecision)) {
@@ -260,8 +292,7 @@
     } else if (PRECISION_PRICE.equals(strPrecision)) {
       precision = curTo.getPricePrecision();
     }
-    return 
amount.multiply(cr.getMultipleRateBy()).setScale(precision.intValue(),
-        RoundingMode.HALF_UP);
+    return amount.multiply(rate).setScale(precision.intValue(), 
RoundingMode.HALF_UP);
   }
 
   /**

------------------------------------------------------------------------------
Comprehensive Server Monitoring with Site24x7.
Monitor 10 servers for $9/Month.
Get alerted through email, SMS, voice calls or mobile push notifications.
Take corrective actions from your mobile device.
http://pubads.g.doubleclick.net/gampad/clk?id=154624111&iu=/4140/ostg.clktrk
_______________________________________________
Openbravo-commits mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openbravo-commits

Reply via email to