Advertisement
tko_pb

UOMUtil

Oct 24th, 2018
392
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 13.79 KB | None | 0 0
  1. /*
  2.  *************************************************************************
  3.  * The contents of this file are subject to the Openbravo  Public  License
  4.  * Version  1.1  (the  "License"),  being   the  Mozilla   Public  License
  5.  * Version 1.1  with a permitted attribution clause; you may not  use this
  6.  * file except in compliance with the License. You  may  obtain  a copy of
  7.  * the License at http://www.openbravo.com/legal/license.html
  8.  * Software distributed under the License  is  distributed  on  an "AS IS"
  9.  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
  10.  * License for the specific  language  governing  rights  and  limitations
  11.  * under the License.
  12.  * The Original Code is Openbravo ERP.
  13.  * The Initial Developer of the Original Code is Openbravo SLU
  14.  * All portions are Copyright (C) 2016-2017 Openbravo SLU
  15.  * All Rights Reserved.
  16.  * Contributor(s):  ______________________________________.
  17.  ************************************************************************
  18.  */
  19.  
  20. package org.openbravo.materialmgmt;
  21.  
  22. import java.math.BigDecimal;
  23. import java.math.RoundingMode;
  24. import java.util.ArrayList;
  25. import java.util.HashMap;
  26. import java.util.List;
  27. import java.util.Map;
  28.  
  29. import org.apache.log4j.Logger;
  30. import org.hibernate.NonUniqueResultException;
  31. import org.hibernate.criterion.Restrictions;
  32. import org.openbravo.base.exception.OBException;
  33. import org.openbravo.dal.core.OBContext;
  34. import org.openbravo.dal.service.OBCriteria;
  35. import org.openbravo.dal.service.OBDal;
  36. import org.openbravo.data.FieldProvider;
  37. import org.openbravo.erpCommon.businessUtility.Preferences;
  38. import org.openbravo.erpCommon.utility.FieldProviderFactory;
  39. import org.openbravo.erpCommon.utility.OBMessageUtils;
  40. import org.openbravo.erpCommon.utility.PropertyException;
  41. import org.openbravo.model.common.enterprise.DocumentType;
  42. import org.openbravo.model.common.plm.Product;
  43. import org.openbravo.model.common.plm.ProductAUM;
  44. import org.openbravo.model.common.plm.ProductUOM;
  45. import org.openbravo.model.common.uom.UOM;
  46. import org.openbravo.service.db.DalConnectionProvider;
  47.  
  48. /**
  49.  *
  50.  * Utility class for methods related to Unit of Measure functionality
  51.  *
  52.  * @author Nono Carballo
  53.  *
  54.  */
  55. public class UOMUtil {
  56.  
  57.     private static final Logger log4j = Logger.getLogger(UOMUtil.class);
  58.     private static final String UOM_PROPERTY = "UomManagement";
  59.     private static final String UOM_NOT_APPLICABLE = "NA";
  60.  
  61.     public static final String UOM_PRIMARY = "P";
  62.     public static final String FIELD_PROVIDER_ID = "id";
  63.     public static final String FIELD_PROVIDER_NAME = "name";
  64.  
  65.     /**
  66.      * Get default AUM for a product in a given document
  67.      *
  68.      * @param mProductId
  69.      *          The product Id
  70.      * @param documentTypeId
  71.      *          The document type id if the parent document
  72.      * @return The default AUM for the product for the given document
  73.      */
  74.  
  75.     public static String getDefaultAUMForDocument(String mProductId, String documentTypeId) {
  76.         // Do not check Organization access
  77.         OBContext.setAdminMode(false);
  78.         try {
  79.             if (mProductId == null || documentTypeId == null) {
  80.                 return null;
  81.             }
  82.             DocumentType docType = OBDal.getInstance().get(DocumentType.class, documentTypeId);
  83.             return getDefaultAUMForFlow(mProductId, docType.isSalesTransaction());
  84.         } finally {
  85.             OBContext.restorePreviousMode();
  86.         }
  87.     }
  88.  
  89.     /**
  90.      * Get default AUM for a product in a Sales Flow. Used when document does not have set document
  91.      * type.
  92.      *
  93.      * @param mProductId
  94.      *          The product Id
  95.      * @return The default AUM for the product for the Sales Flow
  96.      */
  97.     public static String getDefaultAUMForSales(String mProductId) {
  98.         return getDefaultAUMForFlow(mProductId, true);
  99.     }
  100.  
  101.     /**
  102.      * Get default AUM for a product in a Purchase Flow. Used when document does not have set document
  103.      * type.
  104.      *
  105.      * @param mProductId
  106.      *          The product Id
  107.      * @return The default AUM for the product for the Purchase Flow
  108.      */
  109.     public static String getDefaultAUMForPurchase(String mProductId) {
  110.         return getDefaultAUMForFlow(mProductId, false);
  111.     }
  112.  
  113.     private static String getDefaultAUMForFlow(String mProductId, boolean isSoTrx) {
  114.         if (mProductId == null) {
  115.             return null;
  116.         }
  117.         String finalAUM = "";
  118.         // Do not check Organization access
  119.         OBContext.setAdminMode(false);
  120.         try {
  121.             OBCriteria<ProductAUM> pAUMCriteria = OBDal.getInstance().createCriteria(ProductAUM.class);
  122.             pAUMCriteria.add(Restrictions.eq("product.id", mProductId));
  123.             pAUMCriteria.add(Restrictions.eq(isSoTrx ? ProductAUM.PROPERTY_SALES
  124.                     : ProductAUM.PROPERTY_PURCHASE, UOM_PRIMARY));
  125.             Product product = OBDal.getInstance().get(Product.class, mProductId);
  126.             if (product != null) {
  127.                 finalAUM = product.getUOM().getId();
  128.             }
  129.             ProductAUM primaryAum = (ProductAUM) pAUMCriteria.uniqueResult();
  130.             if (primaryAum != null) {
  131.                 finalAUM = primaryAum.getUOM().getId();
  132.             }
  133.         } finally {
  134.             OBContext.restorePreviousMode();
  135.         }
  136.         return finalAUM;
  137.     }
  138.  
  139.     /**
  140.      * Get default AUM for a product in Logistic Flow
  141.      *
  142.      * @param mProductId
  143.      *          The Id of the product
  144.      * @return The default AUM to use in Logistic flow or Base UOM
  145.      */
  146.     public static String getDefaultAUMForLogistic(String mProductId) {
  147.         if (mProductId == null) {
  148.             return null;
  149.         }
  150.         String finalAUM = "";
  151.         // Do not check Organization access
  152.         OBContext.setAdminMode(false);
  153.         try {
  154.             OBCriteria<ProductAUM> pAUMCriteria = OBDal.getInstance().createCriteria(ProductAUM.class);
  155.             pAUMCriteria.add(Restrictions.eq("product.id", mProductId));
  156.             pAUMCriteria.add(Restrictions.eq(ProductAUM.PROPERTY_LOGISTICS, UOM_PRIMARY));
  157.             Product product = OBDal.getInstance().get(Product.class, mProductId);
  158.             finalAUM = product.getUOM().getId();
  159.             ProductAUM primaryAum = (ProductAUM) pAUMCriteria.uniqueResult();
  160.             if (primaryAum != null) {
  161.                 finalAUM = primaryAum.getUOM().getId();
  162.             }
  163.         } finally {
  164.             OBContext.restorePreviousMode();
  165.         }
  166.         return finalAUM;
  167.     }
  168.  
  169.  
  170.     /**
  171.      * Get all the available UOM for a product for a given document
  172.      *
  173.      * @param mProductId
  174.      *          The product id
  175.      * @param docTypeId
  176.      *          The document type id if the parent document
  177.      * @return List of the available UOM
  178.      */
  179.     public static List<UOM> getAvailableUOMsForDocument(String mProductId, String docTypeId) {
  180.         List<UOM> lUom = new ArrayList<UOM>();
  181.         // Do not check Organization access
  182.         OBContext.setAdminMode(false);
  183.         try {
  184.             if (mProductId == null || docTypeId == null) {
  185.                 return lUom;
  186.             }
  187.             DocumentType docType = OBDal.getInstance().get(DocumentType.class, docTypeId);
  188.             OBCriteria<ProductAUM> pAUMCriteria = OBDal.getInstance().createCriteria(ProductAUM.class);
  189.             pAUMCriteria.add(Restrictions.eq("product.id", mProductId));
  190.             pAUMCriteria.add(Restrictions.ne(docType.isSalesTransaction() ? ProductAUM.PROPERTY_SALES
  191.                     : ProductAUM.PROPERTY_PURCHASE, UOM_NOT_APPLICABLE));
  192.             pAUMCriteria.addOrderBy("uOM.name", true);
  193.             Product product = OBDal.getInstance().get(Product.class, mProductId);
  194.             List<ProductAUM> pAUMList = pAUMCriteria.list();
  195.             for (ProductAUM pAUM : pAUMList) {
  196.                 lUom.add(pAUM.getUOM());
  197.             }
  198.             lUom.add(product.getUOM());
  199.         } finally {
  200.             OBContext.restorePreviousMode();
  201.         }
  202.         return lUom;
  203.     }
  204.  
  205.     /**
  206.      * Performs conversion for quantity
  207.      *
  208.      * @param mProductId
  209.      *          The product
  210.      * @param qty
  211.      *          The quantity
  212.      * @param toUOM
  213.      *          The UOM
  214.      * @param reverse
  215.      *          true if reverse, false otherwise
  216.      * @throws OBException
  217.      */
  218.  
  219.     private static BigDecimal getConvertedQty(String mProductId, BigDecimal qty, String toUOMId,
  220.             boolean reverse) throws OBException {
  221.         BigDecimal strQty = qty;
  222.         // Do not check Organization access
  223.         OBContext.setAdminMode(false);
  224.         try {
  225.             Product product = OBDal.getInstance().get(Product.class, mProductId);
  226.  
  227.             if (product == null || toUOMId == null || toUOMId == ""
  228.                     || toUOMId.equals(product.getUOM().getId())) {
  229.                 return strQty;
  230.             }
  231.  
  232.             if (qty != null) {
  233.  
  234.                 OBCriteria<ProductAUM> productAUMConversionCriteria = OBDal.getInstance().createCriteria(
  235.                         ProductAUM.class);
  236.                 productAUMConversionCriteria.add(Restrictions.and(
  237.                         Restrictions.eq("product.id", mProductId), Restrictions.eq("uOM.id", toUOMId)));
  238.  
  239.                 try {
  240.                     ProductAUM conversion = (ProductAUM) productAUMConversionCriteria.uniqueResult();
  241.                     if (conversion == null) {
  242.                         throw new OBException(OBMessageUtils.messageBD(new DalConnectionProvider(false),
  243.                                 "NoAUMDefined", OBContext.getOBContext().getLanguage().getLanguage()));
  244.                     }
  245.                     BigDecimal rate = conversion.getConversionRate();
  246.                     UOM uom = conversion.getUOM();
  247.                     if (reverse) {
  248.                         strQty = qty.divide(rate, uom.getStandardPrecision().intValue(), RoundingMode.HALF_UP);
  249.                     } else {
  250.                         strQty = rate.multiply(qty).setScale(uom.getStandardPrecision().intValue(),
  251.                                 RoundingMode.HALF_UP);
  252.                     }
  253.                 } catch (NonUniqueResultException e) {
  254.                     throw new OBException(OBMessageUtils.messageBD(new DalConnectionProvider(false),
  255.                             "DuplicateAUM", OBContext.getOBContext().getLanguage().getLanguage()));
  256.                 }
  257.             }
  258.         } finally {
  259.             OBContext.restorePreviousMode();
  260.         }
  261.         return strQty;
  262.     }
  263.  
  264.     /**
  265.      * Computes base quantity based on alternative quantity
  266.      *
  267.      * @param mProductId
  268.      *          The product
  269.      * @param qty
  270.      *          The alternative quantity
  271.      * @param toUOM
  272.      *          The UOM
  273.      * @throws OBException
  274.      */
  275.  
  276.     public static BigDecimal getConvertedQty(String mProductId, BigDecimal qty, String toUOM)
  277.             throws OBException {
  278.  
  279.         BigDecimal strQty = getConvertedQty(mProductId, qty, toUOM, false);
  280.  
  281.         return strQty;
  282.     }
  283.  
  284.     /**
  285.      * Computes alternative quantity based on base quantity
  286.      *
  287.      * @param mProductId
  288.      *          The product
  289.      * @param qty
  290.      *          The base quantity
  291.      * @param toUOM
  292.      *          The UOM
  293.      * @throws OBException
  294.      */
  295.     public static BigDecimal getConvertedAumQty(String mProductId, BigDecimal qty, String toUOM)
  296.             throws OBException {
  297.  
  298.         BigDecimal strQty = getConvertedQty(mProductId, qty, toUOM, true);
  299.  
  300.         return strQty;
  301.     }
  302.  
  303.     /**
  304.      * Returns if the UomManagement preference is enabled
  305.      *
  306.      * @return true if enabled, false otherwise
  307.      */
  308.     public static boolean isUomManagementEnabled() {
  309.         String propertyValue = "N";
  310.         try {
  311.             propertyValue = Preferences.getPreferenceValue(UOM_PROPERTY, true, "0", null, null, null,
  312.                     null);
  313.         } catch (PropertyException e) {
  314.             log4j.debug("Preference UomManagement not found", e);
  315.         }
  316.         return (propertyValue != null && propertyValue.equals("Y"));
  317.     }
  318.  
  319.     /**
  320.      * Returns a FieldProvider array containing the default AUM for a product in a given flow
  321.      *
  322.      * @param productId
  323.      *          The product Id
  324.      * @param docTypeId
  325.      *          The document type Id
  326.      */
  327.     public static FieldProvider[] selectDefaultAUM(String productId, String docTypeId) {
  328.         FieldProvider[] finalResult = new FieldProvider[1];
  329.         List<Map<String, String>> result = new ArrayList<>();
  330.         Map<String, String> resultMap = new HashMap<>();
  331.         if (productId == null || productId.isEmpty() || docTypeId == null || docTypeId.isEmpty()) {
  332.             return FieldProviderFactory.getFieldProviderArray(result);
  333.         }
  334.         // Do not check Organization access
  335.         OBContext.setAdminMode(false);
  336.         try {
  337.             String id = getDefaultAUMForDocument(productId, docTypeId);
  338.             UOM uom = OBDal.getInstance().get(UOM.class, id);
  339.             resultMap.put(FIELD_PROVIDER_ID, id);
  340.             resultMap.put(FIELD_PROVIDER_NAME, uom.getName());
  341.             result.add(resultMap);
  342.             finalResult = FieldProviderFactory.getFieldProviderArray(result);
  343.         } finally {
  344.             OBContext.restorePreviousMode();
  345.         }
  346.         return finalResult;
  347.     }
  348.  
  349.     /**
  350.      * Returns a FieldProvider array containing the available UOM for a product in a given flow
  351.      *
  352.      * @param productId
  353.      *          The product Id
  354.      * @param docTypeId
  355.      *          The document type Id
  356.      */
  357.     public static FieldProvider[] selectAUM(String productId, String docTypeId) {
  358.         FieldProvider[] finalResult = new FieldProvider[1];
  359.         Map<String, String> resultMap = new HashMap<>();
  360.         List<Map<String, String>> result = new ArrayList<>();
  361.         if (productId == null || productId.isEmpty() || docTypeId == null || docTypeId.isEmpty()) {
  362.             return FieldProviderFactory.getFieldProviderArray(result);
  363.         }
  364.         // Do not check Organization access
  365.         OBContext.setAdminMode(false);
  366.         try {
  367.             List<UOM> availableUOM = getAvailableUOMsForDocument(productId, docTypeId);
  368.             for (UOM uom : availableUOM) {
  369.                 resultMap = new HashMap<>();
  370.                 resultMap.put(FIELD_PROVIDER_ID, uom.getId());
  371.                 resultMap.put(FIELD_PROVIDER_NAME, uom.getName());
  372.                 result.add(resultMap);
  373.             }
  374.             finalResult = FieldProviderFactory.getFieldProviderArray(result);
  375.         } finally {
  376.             OBContext.restorePreviousMode();
  377.         }
  378.         return finalResult;
  379.     }
  380.  
  381.     public static FieldProvider[] selectUOM(String productId) {
  382.         FieldProvider[] finalResult = new FieldProvider[1];
  383.         List<Map<String, String>> result = new ArrayList<>();
  384.         Map<String, String> resultMap = new HashMap<>();
  385.         if (productId == null || productId.isEmpty()) {
  386.             return FieldProviderFactory.getFieldProviderArray(result);
  387.         }
  388.         // Do not check Organization access
  389.         OBContext.setAdminMode(false);
  390.         try {
  391.             OBCriteria<ProductUOM> pUomCriteria = OBDal.getInstance().createCriteria(ProductUOM.class);
  392.             pUomCriteria.add(Restrictions.eq("product.id", productId));
  393.             pUomCriteria.addOrderBy("uOM.name", true);
  394.             List<ProductUOM> pUomList = pUomCriteria.list();
  395.             for (ProductUOM pUom : pUomList) {
  396.                 resultMap = new HashMap<>();
  397.                 resultMap.put(FIELD_PROVIDER_ID, pUom.getId());
  398.                 resultMap.put(FIELD_PROVIDER_NAME, pUom.getUOM().getName());
  399.                 result.add(resultMap);
  400.             }
  401.             finalResult = FieldProviderFactory.getFieldProviderArray(result);
  402.         } finally {
  403.             OBContext.restorePreviousMode();
  404.         }
  405.         return finalResult;
  406.     }
  407.  
  408. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement