details:   https://code.openbravo.com/erp/devel/pi/rev/ed8a286684d8
changeset: 16917:ed8a286684d8
user:      Gorka Ion Damián <gorkaion.damian <at> openbravo.com>
date:      Mon Jun 25 18:39:11 2012 +0200
summary:   Partial merge of costing. new methods in 
OrganizationStructureProvider

details:   https://code.openbravo.com/erp/devel/pi/rev/00bff63ad33b
changeset: 16918:00bff63ad33b
user:      Gorka Ion Damián <gorkaion.damian <at> openbravo.com>
date:      Mon Jun 25 18:39:29 2012 +0200
summary:   Partial merge of costing. new FinancialUtils class.

diffstat:

 src/org/openbravo/dal/security/OrganizationStructureProvider.java |   81 ++
 src/org/openbravo/financial/FinancialUtils.java                   |  273 
++++++++++
 2 files changed, 354 insertions(+), 0 deletions(-)

diffs (truncated from 381 to 300 lines):

diff -r ec6a050cad0b -r 00bff63ad33b 
src/org/openbravo/dal/security/OrganizationStructureProvider.java
--- a/src/org/openbravo/dal/security/OrganizationStructureProvider.java Mon Jun 
25 18:34:14 2012 +0200
+++ b/src/org/openbravo/dal/security/OrganizationStructureProvider.java Mon Jun 
25 18:39:29 2012 +0200
@@ -32,6 +32,7 @@
 import org.openbravo.dal.core.DalUtil;
 import org.openbravo.dal.core.OBContext;
 import org.openbravo.dal.core.SessionHandler;
+import org.openbravo.dal.service.OBDal;
 import org.openbravo.model.ad.utility.Tree;
 import org.openbravo.model.ad.utility.TreeNode;
 import org.openbravo.model.common.enterprise.Organization;
@@ -229,6 +230,18 @@
   }
 
   /**
+   * Returns the parent organization of an organization.
+   * 
+   * @param orgId
+   *          the id of the organization for which the parent organization is 
determined.
+   * @return the parent organization.
+   */
+  public Organization getParentOrg(Organization org) {
+    initialize();
+    return OBDal.getInstance().get(Organization.class, 
parentByOrganizationID.get(org.getId()));
+  }
+
+  /**
    * Returns the child organization tree of an organization.
    * 
    * @param orgId
@@ -370,4 +383,72 @@
   public void setClientId(String clientId) {
     this.clientId = clientId;
   }
+
+  /**
+   * Returns the legal entity of the given organization
+   * 
+   * @param org
+   *          organization to get its legal entity
+   * @return legal entity (with or without accounting) organization or null if 
not found
+   */
+  public Organization getLegalEntity(final Organization org) {
+    // Admin mode needed to get the Organization type.
+    OBContext.setAdminMode(true);
+    try {
+      for (final String orgId : getParentList(org.getId(), true)) {
+        final Organization parentOrg = 
OBDal.getInstance().get(Organization.class, orgId);
+        if (parentOrg.getOrganizationType().isLegalEntity()) {
+          return parentOrg;
+        }
+      }
+      return null;
+    } finally {
+      OBContext.restorePreviousMode();
+    }
+  }
+
+  /**
+   * Returns the legal entity or Business Unit of the given organization
+   * 
+   * @param org
+   *          organization to get its legal entity or business unit
+   * @return legal entity (with or without accounting) organization or null if 
not found
+   */
+  public Organization getLegalEntityOrBusinessUnit(final Organization org) {
+    // Admin mode needed to get the Organization type.
+    OBContext.setAdminMode(true);
+    try {
+      for (final String orgId : getParentList(org.getId(), true)) {
+        final Organization parentOrg = 
OBDal.getInstance().get(Organization.class, orgId);
+        if (parentOrg.getOrganizationType().isLegalEntity()
+            || parentOrg.getOrganizationType().isBusinessUnit()) {
+          return parentOrg;
+        }
+      }
+      return null;
+    } finally {
+      OBContext.restorePreviousMode();
+    }
+  }
+
+  /**
+   * Returns the organization that is period control allowed for the org 
Organization. If no
+   * organization is found, it returns NULL.
+   * 
+   * @param org
+   *          Organization to get its period control allowed organization.
+   * @return
+   */
+  public Organization getPeriodControlAllowedOrganization(final Organization 
org) {
+    if (org.isAllowPeriodControl()) {
+      return org;
+    }
+    for (final String orgId : getParentList(org.getId(), false)) {
+      final Organization parentOrg = 
OBDal.getInstance().get(Organization.class, orgId);
+      if (parentOrg.isAllowPeriodControl()) {
+        return parentOrg;
+      }
+    }
+    return null;
+  }
 }
\ No newline at end of file
diff -r ec6a050cad0b -r 00bff63ad33b 
src/org/openbravo/financial/FinancialUtils.java
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/src/org/openbravo/financial/FinancialUtils.java   Mon Jun 25 18:39:29 
2012 +0200
@@ -0,0 +1,273 @@
+/*
+ *************************************************************************
+ * The contents of this file are subject to the Openbravo  Public  License
+ * Version  1.0  (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) 2012 Openbravo SLU
+ * All Rights Reserved.
+ * Contributor(s):  ______________________________________.
+ *************************************************************************
+ */
+package org.openbravo.financial;
+
+import java.math.BigDecimal;
+import java.math.RoundingMode;
+import java.util.Date;
+import java.util.List;
+
+import org.apache.log4j.Logger;
+import org.hibernate.criterion.Restrictions;
+import org.openbravo.base.exception.OBException;
+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.model.ad.system.Client;
+import org.openbravo.model.common.currency.ConversionRate;
+import org.openbravo.model.common.currency.Currency;
+import org.openbravo.model.common.enterprise.Organization;
+import org.openbravo.model.common.plm.Product;
+import org.openbravo.model.pricing.pricelist.PriceList;
+import org.openbravo.model.pricing.pricelist.PriceListVersion;
+import org.openbravo.model.pricing.pricelist.ProductPrice;
+
+public class FinancialUtils {
+  private static final Logger log4j = Logger.getLogger(FinancialUtils.class);
+
+  public static final String PRECISION_STANDARD = "A";
+  public static final String PRECISION_COSTING = "C";
+  public static final String PRECISION_PRICE = "P";
+
+  /**
+   * @see #getProductStdPrice(Product, Date, boolean, PriceList, Currency, 
Organization)
+   */
+  public static BigDecimal getProductStdPrice(Product product, Date date,
+      boolean useSalesPriceList, Currency currency, Organization organization) 
throws OBException {
+    return getProductStdPrice(product, date, useSalesPriceList, null, 
currency, organization);
+  }
+
+  /**
+   * Calculates the Standard Price of the given Product. It uses the
+   * {@link #getProductPrice(Product, Date, boolean, PriceList) 
getProductPrice()} method to get the
+   * ProductPrice to be used. In case a conversion is needed it uses the
+   * {@link #getConvertedAmount(BigDecimal, Currency, Currency, Date, 
Organization, String)
+   * getConvertedAmount()} method.
+   * 
+   * @param product
+   *          Product to get its ProductPrice.
+   * @param date
+   *          Date when Product Price is needed.
+   * @param useSalesPriceList
+   *          boolean to set if the price list should be a sales or purchase 
price list.
+   * @param priceList
+   *          PriceList to get its ProductPrice
+   * @param currency
+   *          Currency to convert to the returned price.
+   * @param organization
+   *          Organization where price needs to be used to retrieve the proper 
conversion rate.
+   * @return a BigDecimal with the Standard Price of the Product for the given 
parameters.
+   * @throws OBException
+   *           when no valid ProductPrice is found.
+   */
+  public static BigDecimal getProductStdPrice(Product product, Date date,
+      boolean useSalesPriceList, PriceList pricelist, Currency currency, 
Organization organization)
+      throws OBException {
+    ProductPrice pp = getProductPrice(product, date, useSalesPriceList, 
pricelist);
+    BigDecimal price = pp.getStandardPrice();
+    if 
(!DalUtil.getId(pp.getPriceListVersion().getPriceList().getCurrency()).equals(
+        currency.getId())) {
+      // Conversion is needed.
+      price = getConvertedAmount(price, 
pp.getPriceListVersion().getPriceList().getCurrency(),
+          currency, date, organization, PRECISION_PRICE);
+    }
+
+    return price;
+  }
+
+  /**
+   * @see #getProductPrice(Product, Date, boolean, PriceList, boolean)
+   */
+  public static ProductPrice getProductPrice(Product product, Date date, 
boolean useSalesPriceList)
+      throws OBException {
+    return getProductPrice(product, date, useSalesPriceList, null, true);
+  }
+
+  /**
+   * @see #getProductPrice(Product, Date, boolean, PriceList, boolean)
+   */
+  public static ProductPrice getProductPrice(Product product, Date date, 
boolean useSalesPriceList,
+      PriceList priceList) throws OBException {
+    return getProductPrice(product, date, useSalesPriceList, priceList, true);
+  }
+
+  /**
+   * Method to get a valid ProductPrice for the given Product. It only 
considers PriceList versions
+   * valid on the given date. If a PriceList is given it searches on that one. 
If PriceList null is
+   * passed it search on any Sales or Purchase PriceList based on the 
useSalesPriceList.
+   * 
+   * @param product
+   *          Product to get its ProductPrice.
+   * @param date
+   *          Date when Product Price is needed.
+   * @param useSalesPriceList
+   *          boolean to set if the price list should be a sales or purchase 
price list.
+   * @param priceList
+   *          PriceList to get its ProductPrice
+   * @param throwException
+   *          boolean to determine if an exception has to be thrown when no 
pricelist is found.
+   * @return a valid ProductPrice for the given parameters. Null is no 
exception is to be thrown.
+   * @throws OBException
+   *           when no valid ProductPrice is found and throwException is true.
+   */
+  public static ProductPrice getProductPrice(Product product, Date date, 
boolean useSalesPriceList,
+      PriceList priceList, boolean throwException) throws OBException {
+    StringBuffer where = new StringBuffer();
+    where.append(" as pp");
+    where.append("   join pp." + ProductPrice.PROPERTY_PRICELISTVERSION + " as 
plv");
+    where.append("   join plv." + PriceListVersion.PROPERTY_PRICELIST + " as 
pl");
+    where.append(" where pp." + ProductPrice.PROPERTY_PRODUCT + " = :product");
+    where.append("   and plv." + PriceListVersion.PROPERTY_VALIDFROMDATE + " 
<= :date");
+    if (priceList != null) {
+      where.append("   and pl = :pricelist");
+    } else {
+      where.append("   and pl." + PriceList.PROPERTY_SALESPRICELIST + " = 
:salespricelist");
+    }
+    where.append(" order by pl." + PriceList.PROPERTY_DEFAULT + " desc, plv."
+        + PriceListVersion.PROPERTY_VALIDFROMDATE + " desc");
+
+    OBQuery<ProductPrice> ppQry = 
OBDal.getInstance().createQuery(ProductPrice.class,
+        where.toString());
+    ppQry.setNamedParameter("product", product);
+    ppQry.setNamedParameter("date", date);
+    if (priceList != null) {
+      ppQry.setNamedParameter("pricelist", priceList);
+    } else {
+      ppQry.setNamedParameter("salespricelist", useSalesPriceList);
+    }
+
+    List<ProductPrice> ppList = ppQry.list();
+    if (ppList.isEmpty()) {
+      // No product price found.
+      if (throwException) {
+        throw new OBException("@PriceListVersionNotFound@. @Product@: " + 
product.getIdentifier()
+            + " @Date@: " + OBDateUtils.formatDate(date));
+      } else {
+        return null;
+      }
+    }
+    return ppList.get(0);
+  }
+
+  /**
+   * Method to get the conversion rate defined at system level. If there is 
not a conversion rate
+   * defined on the given Organization it is searched recursively on its 
parent organization until
+   * one is found. If no conversion rate is found null is returned.
+   * 
+   * @param date
+   *          Date conversion is being performed.
+   * @param fromCurrency
+   *          Currency to convert from.
+   * @param toCurrency
+   *          Currency to convert to.
+   * @param org
+   *          Organization of the document that needs to be converted.
+   * @return a valid ConversionRate for the given parameters, null if none is 
found.
+   */
+  public static ConversionRate getConversionRate(Date date, Currency 
fromCurrency,
+      Currency toCurrency, Organization org, Client client) {
+    ConversionRate conversionRate;
+    // Readable Client Org filters to false as organization is filtered 
explicitly.
+    OBContext.setAdminMode(false);
+    try {
+      final OBCriteria<ConversionRate> obcConvRate = 
OBDal.getInstance().createCriteria(

------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
Openbravo-commits mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openbravo-commits

Reply via email to