RollupBillOfMaterial:

* refactor
* fix DB issues
* improved readability
This commit is contained in:
teo_sarca 2008-09-03 20:24:30 +00:00
parent 9426884b5d
commit 8c5bfe4d78
1 changed files with 104 additions and 89 deletions

View File

@ -44,17 +44,15 @@ import org.eevolution.model.MPPProductPlanning;
public class RollupBillOfMaterial extends SvrProcess public class RollupBillOfMaterial extends SvrProcess
{ {
/* Organization */ /* Organization */
private int p_AD_Org_ID = 0; private int p_AD_Org_ID = 0;
/* Account Schema */ /* Account Schema */
private int p_C_AcctSchema_ID = 0; private int p_C_AcctSchema_ID = 0;
/* Cost Type */ /* Cost Type */
private int p_M_CostType_ID = 0; private int p_M_CostType_ID = 0;
/* Product */ /* Product */
private int p_M_Product_ID = 0; private int p_M_Product_ID = 0;
/* Product Category */ /* Product Category */
private int p_M_Product_Category_ID=0; private int p_M_Product_Category_ID = 0;
private int Elementtypeint=0;
/** /**
* Prepare - e.g., get Parameters. * Prepare - e.g., get Parameters.
@ -91,38 +89,19 @@ public class RollupBillOfMaterial extends SvrProcess
*/ */
protected String doIt() throws Exception protected String doIt() throws Exception
{ {
int maxLowLevel = MPPMRP.getMaxLowLevel(getCtx(), get_TrxName());
int m_LowLevel = MPPMRP.getMaxLowLevel(getCtx(), get_TrxName());
// Cost Roll-up for all levels // Cost Roll-up for all levels
for (int index = m_LowLevel ; index >= 0 ; index--) for (int lowLevel = maxLowLevel; lowLevel >= 0; lowLevel--)
{ {
StringBuffer whereClause = new StringBuffer("AD_Client_ID=? AND LowLevel=? AND ProductType='"+MProduct.PRODUCTTYPE_Item+"'"); for (MProduct product : getProducts(lowLevel))
List<Object> params = new ArrayList<Object>();
params.add(getAD_Client_ID());
params.add(m_LowLevel);
if (p_M_Product_ID > 0) {
whereClause.append(" AND p.M_Product_ID=?");
params.add(p_M_Product_ID);
}
if (p_M_Product_Category_ID > 0){
whereClause.append(" AND p.M_Product_Category_ID=?");
params.add(p_M_Product_ID);
}
List<MProduct> products = new Query(getCtx(),MProduct.Table_Name, whereClause.toString(), get_TrxName())
.setParameters(params).list();
for (MProduct product : products)
{ {
for (MCost cost : getCosts(product.get_ID()))
MCost[] costs = MCost.getCosts(getCtx(),getAD_Client_ID(), p_AD_Org_ID , product.getM_Product_ID() , p_M_CostType_ID , p_C_AcctSchema_ID , get_TrxName());
for (MCost cost : costs )
{ {
log.info("Calculate Lower Cost for :"+ product.getName()); log.info("Calculate Lower Cost for :"+ product.getName());
MCostElement element = new MCostElement(getCtx(), cost.getM_CostElement_ID(),get_TrxName()); MCostElement element = cost.getCostElement();
// check if element cost is of Material Type
log.info("Element Cost:"+ element.getName()); log.info("Element Cost:"+ element.getName());
// check if element cost is of Material Type
if (element.getCostElementType().equals(MCostElement.COSTELEMENTTYPE_Material)) if (element.getCostElementType().equals(MCostElement.COSTELEMENTTYPE_Material))
{ {
BigDecimal Material = getCurrentCostPriceLL(MCostElement.COSTELEMENTTYPE_Material , p_AD_Org_ID , product , p_M_CostType_ID , p_C_AcctSchema_ID); BigDecimal Material = getCurrentCostPriceLL(MCostElement.COSTELEMENTTYPE_Material , p_AD_Org_ID , product , p_M_CostType_ID , p_C_AcctSchema_ID);
@ -159,85 +138,121 @@ public class RollupBillOfMaterial extends SvrProcess
cost.saveEx(); cost.saveEx();
} }
/* TODO Comment for future implementation /* TODO Comment for future implementation
else if (element.getCostElementType().equals(MCostElement.COSTELEMENTTYPE_Distribution)) else if (element.getCostElementType().equals(MCostElement.COSTELEMENTTYPE_Distribution))
{ {
BigDecimal Distribution = getCurrentCostPriceLL(MCostElement.COSTELEMENTTYPE_Distribution , p_AD_Org_ID , M_Product_ID , p_M_CostType_ID , p_C_AcctSchema_ID); BigDecimal Distribution = getCurrentCostPriceLL(MCostElement.COSTELEMENTTYPE_Distribution , p_AD_Org_ID , M_Product_ID , p_M_CostType_ID , p_C_AcctSchema_ID);
cost.setCurrentCostPriceLL(Distribution); cost.setCurrentCostPriceLL(Distribution);
cost.saveEx(); cost.saveEx();
}*/ }
} //Costs */
} //Products } // for each Costs
} } // for each Products
} // for each LLC
return "@OK@"; return "@OK@";
} }
/** get the sum Current Cost Price Level Low for this Cost Element Type /**
* @param CostElementType Cost Element Type (Material,Labor,Overhead,Burden) * get the sum Current Cost Price Level Low for this Cost Element Type
* @param AD_Org_ID Organization * @param CostElementType Cost Element Type (Material,Labor,Overhead,Burden)
* @param MProduct Product * @param AD_Org_ID Organization
* @param M_CostType_ID Cost Type * @param MProduct Product
* @param C_AcctSchema_ID Account Schema * @param M_CostType_ID Cost Type
* @return CurrentCostPriceLL Sum Current Cost Price Level Low for this Cost Element Type * @param C_AcctSchema_ID Account Schema
* @return CurrentCostPriceLL Sum Current Cost Price Level Low for this Cost Element Type
*/ */
private BigDecimal getCurrentCostPriceLL(String CostElementType , int AD_Org_ID , MProduct product , int M_CostType_ID , int C_AcctSchema_ID) private BigDecimal getCurrentCostPriceLL(String CostElementType, int AD_Org_ID, MProduct product,
int M_CostType_ID, int C_AcctSchema_ID)
{ {
log.info("getCurrentCostPriceLL.ElementType"+CostElementType); log.info("ElementType: "+CostElementType);
BigDecimal m_cost = Env.ZERO; BigDecimal costPriceLL = Env.ZERO;
MPPProductPlanning pp = MPPProductPlanning.find(getCtx(), getAD_Client_ID(), AD_Org_ID , 0 , 0 , product.getM_Product_ID(), get_TrxName()); MPPProductPlanning pp = MPPProductPlanning.find(getCtx(),
getAD_Client_ID(), AD_Org_ID,
0, // M_Warehouse_ID
0, // S_Resource_ID
product.getM_Product_ID(),
get_TrxName());
int PP_Product_BOM_ID = 0; int PP_Product_BOM_ID = 0;
if(pp != null ) if (pp != null)
PP_Product_BOM_ID = pp.getPP_Product_BOM_ID(); PP_Product_BOM_ID = pp.getPP_Product_BOM_ID();
else else
PP_Product_BOM_ID = MPPProductBOM.getBOMSearchKey(getCtx(), product); PP_Product_BOM_ID = MPPProductBOM.getBOMSearchKey(getCtx(), product);
if (PP_Product_BOM_ID <= 0)
if(PP_Product_BOM_ID <= 0)
return Env.ZERO; return Env.ZERO;
MPPProductBOM bom = MPPProductBOM.get(getCtx(), PP_Product_BOM_ID);
MPPProductBOM bom = new MPPProductBOM(getCtx(), PP_Product_BOM_ID , get_TrxName()); for (MPPProductBOMLine bomline : bom.getLines())
if (bom == null)
return null;
MPPProductBOMLine[] bomlines = bom.getLines();
for (MPPProductBOMLine bomline : bomlines)
{ {
// get the rate for this resource // get the rate for this resource
MCost[] costs = MCost.getCosts(getCtx() , getAD_Client_ID(), p_AD_Org_ID , product.getM_Product_ID() , p_M_CostType_ID , p_C_AcctSchema_ID , get_TrxName()); for (MCost cost : getCosts(product.get_ID()))
for (MCost cost : costs)
{ {
MCostElement element = new MCostElement(getCtx(), cost.getM_CostElement_ID(), get_TrxName()); MCostElement element = cost.getCostElement();
// check if element cost is of type Labor // check if current cost element type is specified cost element type
if (element.getCostElementType().equals(CostElementType)) if (element.getCostElementType().equals(CostElementType))
{ {
BigDecimal QtyPercentage = bomline.getQtyBatch().divide(new BigDecimal(100),8,BigDecimal.ROUND_UP); BigDecimal qtyPercentage = bomline.getQtyBatch().divide(Env.ONEHUNDRED, 8, BigDecimal.ROUND_UP);
BigDecimal QtyBOM = bomline.getQtyBOM(); BigDecimal qtyBOM = bomline.getQtyBOM();
BigDecimal Scrap = bomline.getScrap(); BigDecimal scrapDec = bomline.getScrap().divide(Env.ONEHUNDRED, 4, BigDecimal.ROUND_UP);
Scrap = Scrap.divide(new BigDecimal(100),4,BigDecimal.ROUND_UP); //convert to decimal BigDecimal qtyTotal = Env.ZERO;
BigDecimal QtyTotal = Env.ZERO; if (bomline.isQtyPercentage())
if (bomline.isQtyPercentage()) {
QtyTotal = QtyPercentage.divide( Env.ONE.subtract(Scrap) , 4 ,BigDecimal.ROUND_HALF_UP ); qtyTotal = qtyPercentage.divide(Env.ONE.subtract(scrapDec), 4, BigDecimal.ROUND_HALF_UP);
else }
QtyTotal = QtyBOM.divide( Env.ONE.subtract(Scrap) , 4 ,BigDecimal.ROUND_HALF_UP ); else
{
qtyTotal = qtyBOM.divide(Env.ONE.subtract(scrapDec), 4, BigDecimal.ROUND_HALF_UP);
}
m_cost = m_cost.add(cost.getCurrentCostPriceLL().multiply(QtyTotal)); costPriceLL = costPriceLL.add(cost.getCurrentCostPriceLL().multiply(qtyTotal));
log.info("Cost Element:"+element.getName() + "Total Cost Element:" + m_cost + "QtyPercentage:" + QtyPercentage + "QtyBOM" + QtyBOM) ; log.info("Cost Element:"+element.getName()
+ ", Total Cost Element: " + costPriceLL
+ ", QtyPercentage: " + qtyPercentage
+ ", QtyBOM: " + qtyBOM);
} }
} } // for each cost
} } // for each BOM line
//
// Try find planning Data for this product and get % Yield to calculate cost // Try find planning Data for this product and get % Yield to calculate cost
MPPProductPlanning pps = MPPProductPlanning.find(getCtx(), getAD_Client_ID() , AD_Org_ID , 0 , 0 , product.getM_Product_ID(), get_TrxName()); if (pp != null)
if (pps != null)
{ {
int Yield = pps.getYield(); int yield = pp.getYield();
if(Yield != 0) if(yield != 0)
{ {
BigDecimal DecimalYield = new BigDecimal(Yield/100); BigDecimal decimalYield = new BigDecimal(yield/100);
if (!DecimalYield.equals(Env.ZERO)) costPriceLL = costPriceLL.divide(decimalYield, 4 ,BigDecimal.ROUND_HALF_UP);
m_cost = m_cost.divide(DecimalYield, 4 ,BigDecimal.ROUND_HALF_UP);
} }
} }
return m_cost; return costPriceLL;
} }
private MCost[] getCosts(int product_id)
{
return MCost.getCosts(getCtx(), getAD_Client_ID(), p_AD_Org_ID, product_id,
p_M_CostType_ID, p_C_AcctSchema_ID , get_TrxName());
}
private List<MProduct> getProducts(int lowLevel)
{
List<Object> params = new ArrayList<Object>();
StringBuffer whereClause = new StringBuffer("AD_Client_ID=? AND LowLevel=? AND ProductType=?");
params.add(getAD_Client_ID());
params.add(lowLevel);
params.add(MProduct.PRODUCTTYPE_Item);
if (p_M_Product_ID > 0) {
whereClause.append(" AND M_Product_ID=?");
params.add(p_M_Product_ID);
}
if (p_M_Product_Category_ID > 0){
whereClause.append(" AND M_Product_Category_ID=?");
params.add(p_M_Product_Category_ID);
}
return new Query(getCtx(),MProduct.Table_Name, whereClause.toString(), get_TrxName())
.setParameters(params)
.list();
}
} }