Skip to content

Commit

Permalink
cleanup/refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
teosarca committed Jul 20, 2018
1 parent 57e7df6 commit b07d0ed
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 188 deletions.
Expand Up @@ -40,11 +40,13 @@
*/

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.adempiere.model.engines.CostDimension;
import org.adempiere.util.LegacyAdapters;
import org.adempiere.util.Services;
import org.compiere.model.I_C_AcctSchema;
import org.compiere.model.I_M_CostElement;
Expand All @@ -56,10 +58,10 @@
import org.compiere.model.MProduct;
import org.compiere.model.Query;
import org.compiere.util.DB;
import org.compiere.util.Env;
import org.eevolution.api.IProductBOMDAO;
import org.eevolution.model.I_PP_Product_BOM;
import org.eevolution.model.I_PP_Product_BOMLine;
import org.eevolution.model.I_PP_Product_Planning;
import org.eevolution.model.MPPProductBOM;
import org.eevolution.model.MPPProductBOMLine;
import org.eevolution.model.MPPProductPlanning;
import org.eevolution.model.X_PP_Order_BOMLine;
Expand All @@ -79,6 +81,10 @@
@SuppressWarnings("deprecation") // hide those to not polute our Warnings
public class RollupBillOfMaterial extends JavaProcess
{
// services
private final transient IProductBOMDAO productBOMsRepo = Services.get(IProductBOMDAO.class);
private final transient IMRPDAO mrpDAO = Services.get(IMRPDAO.class);

/* Organization */
private int p_AD_Org_ID = 0;
/* Account Schema */
Expand Down Expand Up @@ -135,7 +141,7 @@ protected String doIt() throws Exception
{
resetCostsLLForLLC0();
//
int maxLowLevel = Services.get(IMRPDAO.class).getMaxLowLevel(this);
int maxLowLevel = mrpDAO.getMaxLowLevel(this);
// Cost Roll-up for all levels
for (int lowLevel = maxLowLevel; lowLevel >= 0; lowLevel--)
{
Expand All @@ -158,9 +164,9 @@ protected String doIt() throws Exception
}
if (PP_Product_BOM_ID <= 0)
{
PP_Product_BOM_ID = Services.get(IProductBOMDAO.class).retrieveDefaultBOMId(product);
PP_Product_BOM_ID = productBOMsRepo.retrieveDefaultBOMId(product);
}
MPPProductBOM bom = MPPProductBOM.get(getCtx(), PP_Product_BOM_ID);
final I_PP_Product_BOM bom = productBOMsRepo.retrieveBOMById(getCtx(), PP_Product_BOM_ID);
if (bom == null)
{
createNotice(product, "@NotFound@ @PP_Product_BOM_ID@");
Expand All @@ -171,7 +177,7 @@ protected String doIt() throws Exception
return "@OK@";
}

protected void rollup(MProduct product, MPPProductBOM bom)
protected void rollup(MProduct product, I_PP_Product_BOM bom)
{
for (I_M_CostElement element : getCostElements())
{
Expand All @@ -193,20 +199,22 @@ protected void rollup(MProduct product, MPPProductBOM bom)
* @param element cost element
* @param baseCost base product cost (BOM Cost)
*/
private void updateCoProductCosts(MPPProductBOM bom, MCost baseCost)
private void updateCoProductCosts(I_PP_Product_BOM bom, MCost baseCost)
{
// Skip if not BOM found
if (bom == null)
return;

BigDecimal costPriceTotal = Env.ZERO;
for (MPPProductBOMLine bomline : bom.getLines())
BigDecimal costPriceTotal = BigDecimal.ZERO;
for (I_PP_Product_BOMLine bomline : productBOMsRepo.retrieveLines(bom))
{
if (!bomline.isCoProduct())
final MPPProductBOMLine bomLinePO = LegacyAdapters.convertToPO(bomline);

if (!bomLinePO.isCoProduct())
{
continue;
}
final BigDecimal costPrice = baseCost.getCurrentCostPriceLL().multiply(bomline.getCostAllocationPerc());
final BigDecimal costPrice = baseCost.getCurrentCostPriceLL().multiply(getCostAllocationPerc(bomLinePO));
//
// Get/Create Cost
MCost cost = MCost.get(baseCost.getCtx(), baseCost.getAD_Client_ID(), baseCost.getAD_Org_ID(),
Expand Down Expand Up @@ -237,23 +245,39 @@ private void updateCoProductCosts(MPPProductBOM bom, MCost baseCost)
}
}

/**
* @return co-product cost allocation percent (i.e. -1/qty)
*/
private BigDecimal getCostAllocationPerc(final MPPProductBOMLine bomLine)
{
final BigDecimal qty = bomLine.getQty(false).negate();
BigDecimal allocationPercent = BigDecimal.ZERO;
if (qty.signum() != 0)
{
allocationPercent = BigDecimal.ONE.divide(qty, 4, RoundingMode.HALF_UP);
}
return allocationPercent;
}

/**
* Get the sum Current Cost Price Level Low for this Cost Element
* @param bom MPPProductBOM
* @param element MCostElement
* @return Cost Price Lower Level
*/
private BigDecimal getCurrentCostPriceLL(MPPProductBOM bom, I_M_CostElement element)
private BigDecimal getCurrentCostPriceLL(I_PP_Product_BOM bom, I_M_CostElement element)
{
log.info("Element: "+ element);
BigDecimal costPriceLL = Env.ZERO;
BigDecimal costPriceLL = BigDecimal.ZERO;
if(bom == null)
return costPriceLL;

for (MPPProductBOMLine bomline : bom.getLines())
for (I_PP_Product_BOMLine bomline : productBOMsRepo.retrieveLines(bom))
{
final MPPProductBOMLine bomLinePO = LegacyAdapters.convertToPO(bomline);

// Skip co-product
if (bomline.isCoProduct())
if (bomLinePO.isCoProduct())
{
continue;
}
Expand All @@ -267,12 +291,12 @@ private BigDecimal getCurrentCostPriceLL(MPPProductBOM bom, I_M_CostElement elem
// get the rate for this resource
for (MCost cost : getCosts(component, element.getM_CostElement_ID()))
{
BigDecimal qty = bomline.getQty(true);
BigDecimal qty = bomLinePO.getQty(true);

// ByProducts
if (bomline.isByProduct())
if (bomLinePO.isByProduct())
{
cost.setCurrentCostPriceLL(Env.ZERO);
cost.setCurrentCostPriceLL(BigDecimal.ZERO);
}

BigDecimal costPrice = cost.getCurrentCostPrice().add(cost.getCurrentCostPriceLL());
Expand Down
Expand Up @@ -20,7 +20,9 @@
import java.util.Properties;

import org.adempiere.exceptions.AdempiereException;
import org.eevolution.model.MPPProductBOM;
import org.adempiere.util.Services;
import org.eevolution.api.IProductBOMDAO;
import org.eevolution.model.I_PP_Product_BOM;

import de.metas.i18n.Msg;

Expand Down Expand Up @@ -110,8 +112,8 @@ protected boolean beforeSave (boolean newRecord)
// Derive ChangeNotice from BOM if defined
if (newRecord && getPP_Product_BOM_ID() != 0 && getM_ChangeNotice_ID() == 0)
{
MPPProductBOM bom = MPPProductBOM.get(getCtx(), getPP_Product_BOM_ID());
if (bom.getM_ChangeNotice_ID() != 0)
final I_PP_Product_BOM bom = Services.get(IProductBOMDAO.class).retrieveBOMById(getCtx(), getPP_Product_BOM_ID());
if (bom.getM_ChangeNotice_ID() > 0)
{
setM_ChangeNotice_ID(bom.getM_ChangeNotice_ID());
}
Expand Down
Expand Up @@ -18,15 +18,11 @@
package org.eevolution.model;

import java.sql.ResultSet;
import java.sql.Timestamp;
import java.util.List;
import java.util.Properties;

import org.adempiere.util.LegacyAdapters;
import org.adempiere.util.Services;
import org.compiere.model.I_M_Product;
import org.compiere.model.MProduct;
import org.eevolution.api.IProductBOMBL;
import org.eevolution.api.IProductBOMDAO;

/**
Expand All @@ -37,23 +33,8 @@
*/
public class MPPProductBOM extends X_PP_Product_BOM
{
/**
*
*/
private static final long serialVersionUID = -5770988975738210823L;
// /** Cache */
// private static CCache<Integer, MPPProductBOM> s_cache = new CCache<Integer, MPPProductBOM>(Table_Name, 40, 5);

// /** BOM Lines */
// private List<MPPProductBOMLine> m_lines = null;

/**
* Get Product BOM by ID (cached)
*
* @param ctx
* @param PP_Product_BOM_ID
* @return product bom
*/
@Deprecated
public static MPPProductBOM get(Properties ctx, int PP_Product_BOM_ID)
{
Expand All @@ -62,70 +43,6 @@ public static MPPProductBOM get(Properties ctx, int PP_Product_BOM_ID)
return bomPO;
}

/**
* Get BOM with Default Logic (Product = BOM Product and BOM Value = Product Value)
*
* @param product
* @param trxName
* @return product BOM
*/
@Deprecated
public static MPPProductBOM getDefault(I_M_Product product, String trxName)
{
final I_PP_Product_BOM bom = Services.get(IProductBOMDAO.class).retrieveDefaultBOM(product);
final MPPProductBOM bomPO = LegacyAdapters.convertToPO(bom);
return bomPO;
}

/**
* Get BOM for Product
*
* @param product product
* @param ad_org_id Organization ID
* @param trxName Transaction Name
* @return BOM
*/
public static MPPProductBOM get(MProduct product, int ad_org_id, String trxName)
{
MPPProductBOM bom = null;
Properties ctx = product.getCtx();
// find Default BOM in Product Data Planning
if (ad_org_id > 0)
{
MPPProductPlanning pp = MPPProductPlanning.get(ctx, product.getAD_Client_ID(), ad_org_id, product.getM_Product_ID(), trxName);
if (pp != null && pp.getPP_Product_BOM_ID() > 0)
{
bom = new MPPProductBOM(ctx, pp.getPP_Product_BOM_ID(), trxName);
}
}
if (bom == null)
{
// Find BOM with Default Logic where product = bom product and bom value = value
bom = getDefault(product, trxName);
}

return bom;
}

/**
* Get BOM with valid dates for Product
*
* @param product product
* @param ad_org_id Organization ID
* @param valid Date to Validate
* @param trxName Transaction Name
* @return BOM
*/
public static MPPProductBOM get(MProduct product, int ad_org_id, Timestamp valid, String trxName)
{
MPPProductBOM bom = get(product, ad_org_id, trxName);
if (bom != null && bom.isValidFromTo(valid))
{
return bom;
}
return null;
}

public MPPProductBOM(Properties ctx, int PP_Product_BOM_ID, String trxName)
{
super(ctx, PP_Product_BOM_ID, trxName);
Expand All @@ -136,54 +53,18 @@ public MPPProductBOM(Properties ctx, ResultSet rs, String trxName)
super(ctx, rs, trxName);
}

/**
* Get BOM Lines valid date for Product BOM
*
* @param valid Date to Validate
* @return BOM Lines
*/
@Deprecated
public MPPProductBOMLine[] getLines(Timestamp valid)
{
List<I_PP_Product_BOMLine> lines = Services.get(IProductBOMDAO.class).retrieveLines(this, valid);
final MPPProductBOMLine[] linesArr = LegacyAdapters.convertToPOArray(lines, MPPProductBOMLine.class);
return linesArr;
} // getLines

/**
* Get BOM Lines for Product BOM from cache
*
* @return BOM Lines
*/
@Deprecated
public MPPProductBOMLine[] getLines()
{
return getLines(false);
}

/**
* Get BOM Lines for Product BOM
*
* @return BOM Lines
*/
@Deprecated
public MPPProductBOMLine[] getLines(boolean reload)
{
List<I_PP_Product_BOMLine> lines = Services.get(IProductBOMDAO.class).retrieveLines(this);
final List<I_PP_Product_BOMLine> lines = Services.get(IProductBOMDAO.class).retrieveLines(this);
final MPPProductBOMLine[] linesArr = LegacyAdapters.convertToPOArray(lines, MPPProductBOMLine.class);
return linesArr;
} // getLines

@Deprecated
public boolean isValidFromTo(Timestamp date)
{
return Services.get(IProductBOMBL.class).isValidFromTo(this, date);
}

@Override
public String toString()
{
StringBuffer sb = new StringBuffer("MPPProductBOM[")
StringBuilder sb = new StringBuilder("MPPProductBOM[")
.append(get_ID()).append("-").append(getDocumentNo())
.append(", Value=").append(getValue())
.append("]");
Expand Down

0 comments on commit b07d0ed

Please sign in to comment.