[ 2037954 ] BOM AND Workflow Cost Roll-Up

http://sourceforge.net/tracker/index.php?func=detail&aid=2037954&group_id=176962&atid=879332
This commit is contained in:
vpj-cd 2008-08-22 04:55:59 +00:00
parent 408e8a3364
commit 500cac765f
7 changed files with 222 additions and 472 deletions

View File

@ -25,6 +25,7 @@ import java.util.Properties;
import java.util.logging.Level;
import org.compiere.model.MMenu;
import org.compiere.model.MProduct;
import org.compiere.model.MWindow;
import org.compiere.model.X_AD_WF_Node;
import org.compiere.model.X_AD_Workflow;
@ -919,5 +920,18 @@ public class MWorkflow extends X_AD_Workflow
// wf.start(M_Requsition_ID);
} // main
/**
* Get AD_Workflow_ID for given M_Product_ID
* @param M_Product_ID
* @return AD_Workflow_ID
*/
public static int getWorkflowSearchKey(Properties ctx, MProduct product)
{
int AD_Client_ID = Env.getAD_Client_ID(ctx);
String sql = "SELECT AD_Workflow_ID FROM AD_Workflow "
+" WHERE Value = ? AND AD_Client_ID = ?";
return DB.getSQLValue(null, sql, product.getValue(), AD_Client_ID);
}
} // MWorkflow_ID

View File

@ -99,8 +99,8 @@ public class MPPOrderBOMLine extends X_PP_Order_BOMLine
//Phantom
if(getComponentType().equals(MPPProductBOMLine.COMPONENTTYPE_Phantom))
{
int PP_Product_BOM_ID = MPPProductBOM.getBOMSearchKey(getCtx(), getM_Product_ID());
MProduct parent = MProduct.get(getCtx(), getM_Product_ID());
int PP_Product_BOM_ID = MPPProductBOM.getBOMSearchKey(getCtx(), parent);
if (PP_Product_BOM_ID <= 0)
return true;
@ -111,7 +111,7 @@ public class MPPOrderBOMLine extends X_PP_Order_BOMLine
for(int i = 0 ; i < PP_Product_BOMline.length ; i++ )
{
MPPOrderBOMLine PP_Order_BOMLine = new MPPOrderBOMLine(getCtx(), 0, get_TrxName());
MProduct product = MProduct.get(getCtx(),PP_Product_BOMline[i].getM_Product_ID());
MProduct component = MProduct.get(getCtx(),PP_Product_BOMline[i].getM_Product_ID());
PP_Order_BOMLine.setDescription(PP_Product_BOMline[i].getDescription());
PP_Order_BOMLine.setHelp(PP_Product_BOMline[i].getHelp());
PP_Order_BOMLine.setM_ChangeNotice_ID(PP_Product_BOMline[i].getM_ChangeNotice_ID());
@ -137,7 +137,7 @@ public class MPPOrderBOMLine extends X_PP_Order_BOMLine
if (PP_Order_BOMLine.isQtyPercentage())
{
BigDecimal qty = PP_Order_BOMLine.getQtyBatch().multiply(QtyOrdered);
log.fine("product:"+product.getName() +" Qty:"+qty + " QtyOrdered:"+ QtyOrdered + " PP_Order_BOMLine.getQtyBatch():" + PP_Order_BOMLine.getQtyBatch());
log.fine("product:"+component.getName() +" Qty:"+qty + " QtyOrdered:"+ QtyOrdered + " PP_Order_BOMLine.getQtyBatch():" + PP_Order_BOMLine.getQtyBatch());
if(PP_Order_BOMLine.getComponentType().equals(COMPONENTTYPE_Packing))
PP_Order_BOMLine.setQtyRequiered(qty.divide(new BigDecimal(100),8,BigDecimal.ROUND_UP));
else if (PP_Order_BOMLine.getComponentType().equals(COMPONENTTYPE_Component) || PP_Order_BOMLine.getComponentType().equals(COMPONENTTYPE_Phantom))

View File

@ -346,11 +346,9 @@ public class MPPProductBOM extends X_PP_Product_BOM
* @param M_Product_ID
* @return PP_Product_BOM_ID
*/
public static int getBOMSearchKey(Properties ctx, int M_Product_ID)
public static int getBOMSearchKey(Properties ctx, MProduct product)
{
int AD_Client_ID = Env.getAD_Client_ID(ctx);
MProduct product = MProduct.get(ctx, M_Product_ID);
String sql = "SELECT pb.PP_Product_BOM_ID FROM PP_Product_BOM pb"
+" WHERE pb.Value = ? AND pb.AD_Client_ID = ?";
return DB.getSQLValue(null, sql, product.getValue(), AD_Client_ID);

View File

@ -140,22 +140,5 @@ public class MPPProductPlanning extends X_PP_Product_Planning
.setOrderBy("AD_Org_ID DESC NULLS LAST, M_Warehouse_ID DESC NULLS LAST, S_Resource_ID DESC NULLS LAST")
.first();
}
/**************************************************************************
* find planning data demand & supply to this warehouse
* @param ctx Context
* @param AD_Org_ID Organization ID
* @param M_Product_ID Product ID
* @param trxName Transaction Name
* @return MPPProductPlanning Planning Data
*/
public static MPPProductPlanning getDemandSupplyResource(Properties ctx , int AD_Org_ID , int M_Product_ID, String trxName )
{
String whereClause = "AD_Org_ID = ? AND "+COLUMNNAME_M_Product_ID+"=?";
return new Query(ctx, MPPProductPlanning.Table_Name, whereClause, trxName)
.setParameters(new Object[]{AD_Org_ID, M_Product_ID})
.first();
}
} // Product Data Planning

View File

@ -48,6 +48,7 @@ import org.compiere.util.DB;
import org.compiere.util.Env;
import org.compiere.util.Msg;
import org.compiere.util.TimeUtil;
import org.compiere.wf.MWorkflow;
import org.eevolution.model.MDDNetworkDistribution;
import org.eevolution.model.MDDNetworkDistributionLine;
import org.eevolution.model.MDDOrder;
@ -524,8 +525,12 @@ public class MRP extends SvrProcess
//Find the BOM to this Product
if (m_product_planning.getPP_Product_BOM_ID() == 0 && product.isBOM())
{
m_product_planning.setPP_Product_BOM_ID(MPPProductBOM.getBOMSearchKey(getCtx(), m_product_planning.getM_Product_ID()));
m_product_planning.setPP_Product_BOM_ID(MPPProductBOM.getBOMSearchKey(getCtx(), product));
}
if (m_product_planning.getAD_Workflow_ID() == 0 && product.isBOM())
{
m_product_planning.setAD_Workflow_ID(MWorkflow.getWorkflowSearchKey(getCtx(), product));
}
if (m_product_planning.getPlanner_ID() == 0 )
{
m_product_planning.setPlanner_ID(Planner_ID);

View File

@ -24,6 +24,7 @@ import java.util.*;
import org.compiere.model.*;
import org.compiere.util.*;
import org.compiere.wf.MWorkflow;
import org.compiere.process.*;
import org.eevolution.model.MPPMRP;
import org.eevolution.model.MPPProductBOM;
@ -31,25 +32,29 @@ import org.eevolution.model.MPPProductBOMLine;
import org.eevolution.model.MPPProductPlanning;
import org.eevolution.model.QueryDB;
import com.sun.org.apache.bcel.internal.generic.RETURN;
/**
* Rollup Bill of Material
* Roll-UP Bill of Material
*
* @author Victor Perez, e-Evolution, S.C.
* @author victor.perez@e-evolution.com, e-Evolution, S.C.
* @version $Id: RollupBillOfMaterial.java,v 1.1 2004/06/22 05:24:03 vpj-cd Exp $
*/
public class RollupBillOfMaterial extends SvrProcess
{
/** */
private int p_AD_Org_ID = 0;
//private int p_M_Warehouse_ID = 0;
private int p_M_Product_Category_ID=0;
private int p_M_Product_ID = 0;
private int p_M_CostType_ID = 0;
//private int p_S_Resource_ID = 0;
private int p_C_AcctSchema_ID = 0;
private int Elementtypeint=0;
/* Organization */
private int p_AD_Org_ID = 0;
/* Account Schema */
private int p_C_AcctSchema_ID = 0;
/* Cost Type */
private int p_M_CostType_ID = 0;
/* Product */
private int p_M_Product_ID = 0;
/* Product Category */
private int p_M_Product_Category_ID=0;
private int Elementtypeint=0;
/**
* Prepare - e.g., get Parameters.
*/
@ -64,234 +69,164 @@ public class RollupBillOfMaterial extends SvrProcess
if (para[i].getParameter() == null)
;
else if (name.equals("AD_Org_ID"))
{
p_AD_Org_ID = ((BigDecimal)para[i].getParameter()).intValue();
}
/*else if (name.equals("M_Warehouse_ID"))
{
p_M_Warehouse_ID = ((BigDecimal)para[i].getParameter()).intValue();
}*/
else if (name.equals("M_Product_Category_ID"))
{
p_M_Product_Category_ID = ((BigDecimal)para[i].getParameter()).intValue();
}
else if (name.equals("M_Product_ID"))
{
p_M_Product_ID = ((BigDecimal)para[i].getParameter()).intValue();
}
/*else if (name.equals("S_Resource_ID"))
{
p_S_Resource_ID = ((BigDecimal)para[i].getParameter()).intValue();
}*/
else if (name.equals("M_CostType_ID"))
{
p_M_CostType_ID = ((BigDecimal)para[i].getParameter()).intValue();
}
else if (name.equals("C_AcctSchema_ID"))
{
p_C_AcctSchema_ID = ((BigDecimal)para[i].getParameter()).intValue();
}
else
p_AD_Org_ID = para[i].getParameterAsInt();
else if (name.equals(MAcctSchema.COLUMNNAME_C_AcctSchema_ID))
p_C_AcctSchema_ID = para[i].getParameterAsInt();
else if (name.equals(MCostType.COLUMNNAME_M_CostType_ID))
p_M_CostType_ID = para[i].getParameterAsInt();
else if (name.equals(MProduct.COLUMNNAME_M_Product_ID))
p_M_Product_ID = para[i].getParameterAsInt();
else if (name.equals(MProduct.COLUMNNAME_M_Product_Category_ID))
p_M_Product_Category_ID = para[i].getParameterAsInt();
else
log.log(Level.SEVERE,"prepare - Unknown Parameter: " + name);
}
} // prepare
/**
* Generate Calculate Cost
* @return info
* @throws Exception
*/
protected String doIt() throws Exception
{
int lowlevel = MPPMRP.getMaxLowLevel(getCtx(), get_TrxName());
// Calculate Rollup for all levels
for (int index = lowlevel ; index >= 0 ; index--)
{
StringBuffer sql = new StringBuffer ("SELECT p.M_Product_ID FROM M_Product p WHERE p.ProductType = '" + MProduct.PRODUCTTYPE_Item + "' AND AD_Client_ID = ? AND p.LowLevel = " + index);
if (p_M_Product_ID != 0)
{
sql.append(" AND p.M_Product_ID = " + p_M_Product_ID);
}
if (p_M_Product_Category_ID != 0)
{
sql.append(" AND p.M_Product_Category_ID = " + p_M_Product_Category_ID);
}
PreparedStatement pstmt = null;
try
{
pstmt = DB.prepareStatement (sql.toString(),get_TrxName());
pstmt.setInt(1, getAD_Client_ID());
ResultSet rs = pstmt.executeQuery ();
while (rs.next())
{
int M_Product_ID = rs.getInt("M_Product_ID");
int m_LowLevel = MPPMRP.getMaxLowLevel(getCtx(), get_TrxName());
// Cost Roll-up for all levels
for (int index = m_LowLevel ; index >= 0 ; index--)
{
StringBuffer whereClause = new StringBuffer("AD_Client_ID=? AND LowLevel=? AND ProductType='"+MProduct.PRODUCTTYPE_Item+"'");
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)
{
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());
MCostElement element = new MCostElement(getCtx(), cost.getM_CostElement_ID(),get_TrxName());
// check if element cost is of Material Type
log.info("Element Cost:"+ element.getName());
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);
log.info("Material Cost Low Level:" + Material);
cost.setCurrentCostPriceLL(Material);
cost.saveEx();
}
else if (element.getCostElementType().equals(MCostElement.COSTELEMENTTYPE_Resource))
{
BigDecimal Labor = getCurrentCostPriceLL(MCostElement.COSTELEMENTTYPE_Resource, p_AD_Org_ID , product , p_M_CostType_ID , p_C_AcctSchema_ID);
log.info("Labor Cost Low Level:" + Labor);
cost.setCurrentCostPriceLL(Labor);
cost.saveEx();
}
else if (element.getCostElementType().equals(MCostElement.COSTELEMENTTYPE_BurdenMOverhead))
{
BigDecimal Burder = getCurrentCostPriceLL(MCostElement.COSTELEMENTTYPE_BurdenMOverhead , p_AD_Org_ID , product , p_M_CostType_ID , p_C_AcctSchema_ID);
log.info("Burden Cost Low Level:" + Burder);
cost.setCurrentCostPriceLL(Burder);
cost.saveEx();
}
else if (element.getCostElementType().equals(MCostElement.COSTELEMENTTYPE_Overhead))
{
BigDecimal Overhead = getCurrentCostPriceLL(MCostElement.COSTELEMENTTYPE_Overhead , p_AD_Org_ID , product , p_M_CostType_ID , p_C_AcctSchema_ID);
log.info("Overhead Cost Low Level:" + Overhead);
cost.setCurrentCostPriceLL(Overhead);
cost.saveEx();
}
else if (element.getCostElementType().equals(MCostElement.COSTELEMENTTYPE_OutsideProcessing))
{
BigDecimal Subcontract = getCurrentCostPriceLL(MCostElement.COSTELEMENTTYPE_OutsideProcessing , p_AD_Org_ID , product , p_M_CostType_ID , p_C_AcctSchema_ID);
log.info("Subcontract Cost Low Level:" + Subcontract);
cost.setCurrentCostPriceLL(Subcontract);
cost.saveEx();
}
/* TODO Comment for future implementation
else if (element.getCostElementType().equals(MCostElement.COSTELEMENTTYPE_Distribution))
{
StringBuffer sqlw = new StringBuffer ("SELECT p.M_Warehouse_ID FROM M_Warehouse p WHERE IsActive = 'Y' AND AD_Client_ID = " + getAD_Client_ID());
/*if (p_M_Warehouse_ID != 0)
{
sqlw.append(" AND p.M_Warehouse_ID = " + p_M_Warehouse_ID);
} */
log.info("Rollup Bill of Material sql :" + sql.toString());
PreparedStatement pstmtw = null;
pstmtw = DB.prepareStatement (sqlw.toString(),get_TrxName());
ResultSet rsw = pstmtw.executeQuery ();
int M_Warehouse_ID=0;
while (rsw.next())
{
M_Warehouse_ID = rsw.getInt(1);
MCost[] costs = MCost.getCosts(getCtx(),getAD_Client_ID(), p_AD_Org_ID , M_Product_ID , p_M_CostType_ID , p_C_AcctSchema_ID , get_TrxName());
MProduct product = new MProduct(getCtx(), M_Product_ID ,get_TrxName());
log.info("Product Search Key-Name:" + product.getValue() + "-" + product.getName());
if (costs != null)
{
for (MCost cost : costs )
{
MCostElement element = new MCostElement(getCtx(), cost.getM_CostElement_ID(),get_TrxName());
// check if element cost is of type Labor
log.info("Element Cost:"+ element.getName());
if (element.getCostElementType().equals(element.COSTELEMENTTYPE_Material))
{
BigDecimal Material = getCurrentCostPriceLL(element.COSTELEMENTTYPE_Material , p_AD_Org_ID , M_Product_ID , p_M_CostType_ID , p_C_AcctSchema_ID);
log.info("Material Cost Low Level:" + Material);
if (cost.getCurrentCostPrice().compareTo(Env.ZERO)==0)
{
cost.setCurrentCostPriceLL(Material);
cost.save();
}
continue;
}
if (element.getCostElementType().equals(element.COSTELEMENTTYPE_Resource))
{
BigDecimal Labor = getCurrentCostPriceLL(element.COSTELEMENTTYPE_Resource, p_AD_Org_ID , M_Product_ID , p_M_CostType_ID , p_C_AcctSchema_ID);
log.info("Labor Cost Low Level:" + Labor);
cost.setCurrentCostPriceLL(Labor);
cost.save();
continue;
}
if (element.getCostElementType().equals(element.COSTELEMENTTYPE_BurdenMOverhead))
{
BigDecimal Burder = getCurrentCostPriceLL(element.COSTELEMENTTYPE_BurdenMOverhead , p_AD_Org_ID , M_Product_ID , p_M_CostType_ID , p_C_AcctSchema_ID);
log.info("Burden Cost Low Level:" + Burder);
cost.setCurrentCostPriceLL(Burder);
cost.save();
continue;
}
if (element.getCostElementType().equals(element.COSTELEMENTTYPE_Overhead))
{
BigDecimal Overhead = getCurrentCostPriceLL(element.COSTELEMENTTYPE_Overhead , p_AD_Org_ID , M_Product_ID , p_M_CostType_ID , p_C_AcctSchema_ID);
log.info("Overhead Cost Low Level:" + Overhead);
cost.setCurrentCostPriceLL(Overhead);
cost.save();
continue;
}
if (element.getCostElementType().equals(element.COSTELEMENTTYPE_OutsideProcessing))
{
BigDecimal Subcontract = getCurrentCostPriceLL(element.COSTELEMENTTYPE_OutsideProcessing , p_AD_Org_ID , M_Product_ID , p_M_CostType_ID , p_C_AcctSchema_ID);
log.info("Subcontract Cost Low Level:" + Subcontract);
cost.setCurrentCostPriceLL(Subcontract);
cost.save();
continue;
}
/*if (element.getCostElementType().equals(element.PP_ELEMENTTYPE_Distribution))
{
BigDecimal Distribution = getCostLL(element.PP_ELEMENTTYPE_Distribution , p_AD_Org_ID , M_Product_ID , M_Warehouse_ID , p_S_Resource_ID , p_PP_Cost_Group_ID , p_C_AcctSchema_ID);
//pc[e].setCostLLAmt(Distribution);
System.out.println("Distribution" + Distribution);
pc[e].save(get_TrxName());
continue;
}*/
}
} // end if
} // end while warehouse
rsw.close();
pstmtw.close();
} // end while product
rs.close();
pstmt.close();
}
catch (Exception e)
{
log.log(Level.SEVERE,"doIt - " + sql, e);
return null;
}
}
return "ok";
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.saveEx();
}*/
} //Costs
} //Products
}
return "@OK@";
}
/** get Current Cost Price Low Level
/** get the sum Current Cost Price Level Low for this Cost Element Type
* @param CostElementType Cost Element Type (Material,Labor,Overhead,Burden)
* @param AD_Org_ID Organization
* @param M_Product_ID Product
* @param MProduct Product
* @param M_CostType_ID Cost 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 , int M_Product_ID , 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);
BigDecimal totalcost = Env.ZERO;
BigDecimal m_cost = Env.ZERO;
MPPProductPlanning pp = MPPProductPlanning.find(getCtx(), getAD_Client_ID(), AD_Org_ID , 0 , 0 , product.getM_Product_ID(), get_TrxName());
int PP_Product_BOM_ID = 0;
if(pp != null )
PP_Product_BOM_ID = pp.getPP_Product_BOM_ID();
else
PP_Product_BOM_ID = MPPProductBOM.getBOMSearchKey(getCtx(), product);
if(PP_Product_BOM_ID <= 0)
return Env.ZERO;
MPPProductBOM bom = new MPPProductBOM(getCtx(), PP_Product_BOM_ID , get_TrxName());
if (bom == null)
return null;
MPPProductBOM bom = new MPPProductBOM(getCtx(), getPP_Product_BOM_ID(AD_Org_ID , M_Product_ID , CostElementType),get_TrxName());
MPPProductBOMLine[] bomlines = bom.getLines();
for (MPPProductBOMLine bomline : bomlines)
{
// get the rate for this resource
MCost[] costs = MCost.getCosts(getCtx() , getAD_Client_ID(), p_AD_Org_ID , M_Product_ID , p_M_CostType_ID , p_C_AcctSchema_ID , get_TrxName());
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)
{
MCostElement element = new MCostElement(getCtx(), cost.getM_CostElement_ID(), get_TrxName());
// check if element cost is of type Labor
if (element.getCostElementType().equals(CostElementType))
{
BigDecimal QtyPercentage = bomline.getQtyBatch().divide(new BigDecimal(100),8,BigDecimal.ROUND_UP);
BigDecimal QtyBOM = bomline.getQtyBOM();
BigDecimal Scrap = bomline.getScrap();
Scrap = Scrap.divide(new BigDecimal(100),4,BigDecimal.ROUND_UP); //convert to decimal
Scrap = Scrap.divide(new BigDecimal(100),4,BigDecimal.ROUND_UP); //convert to decimal
BigDecimal QtyTotal = Env.ZERO;
if (bomline.isQtyPercentage())
{
if (bomline.isQtyPercentage())
QtyTotal = QtyPercentage.divide( Env.ONE.subtract(Scrap) , 4 ,BigDecimal.ROUND_HALF_UP );
}
else
{
QtyTotal = QtyBOM.divide( Env.ONE.subtract(Scrap) , 4 ,BigDecimal.ROUND_HALF_UP );
}
totalcost = totalcost.add(cost.getCurrentCostPriceLL().multiply(QtyTotal));
log.info("Cost Element:"+element.getName() + "Total Cost Element:" + totalcost + "QtyPercentage:" + QtyPercentage + "QtyBOM" + QtyBOM) ;
m_cost = m_cost.add(cost.getCurrentCostPriceLL().multiply(QtyTotal));
log.info("Cost Element:"+element.getName() + "Total Cost Element:" + m_cost + "QtyPercentage:" + QtyPercentage + "QtyBOM" + QtyBOM) ;
}
}
}
// Calculate Yield Cost
MPPProductPlanning pps = MPPProductPlanning.getDemandSupplyResource(getCtx(), AD_Org_ID , M_Product_ID, get_TrxName());
// 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 (pps != null)
{
int Yield = pps.getYield();
@ -299,116 +234,9 @@ public class RollupBillOfMaterial extends SvrProcess
{
BigDecimal DecimalYield = new BigDecimal(Yield/100);
if (!DecimalYield.equals(Env.ZERO))
totalcost = totalcost.divide(DecimalYield, 4 ,BigDecimal.ROUND_HALF_UP);
m_cost = m_cost.divide(DecimalYield, 4 ,BigDecimal.ROUND_HALF_UP);
}
}
return totalcost;
}
private int getM_Product_ID(int S_Resource_ID)
{
QueryDB query = new QueryDB("org.compiere.model.X_M_Product");
String filter = "S_Resource_ID = " + S_Resource_ID;
java.util.List results = query.execute(filter);
Iterator select = results.iterator();
while (select.hasNext())
{
X_M_Product M_Product = (X_M_Product) select.next();
return M_Product.getM_Product_ID();
}
return 0;
}
private int getPP_Product_BOM_ID(int AD_Org_ID , int M_Product_ID , String CostElementType)
{
boolean pp = false;
String sqlec = "SELECT M_CostElement_ID FROM M_CostElement WHERE CostElementType=? AND AD_CLient_ID = " + getAD_Client_ID();
PreparedStatement pstmtec = null;
try
{
pstmtec = DB.prepareStatement(sqlec, get_TrxName());
pstmtec.setString(1, CostElementType);
ResultSet rsec = pstmtec.executeQuery();
if (rsec.next())
{
Elementtypeint=rsec.getInt(1);
}
rsec.close();
pstmtec.close();
pstmtec = null;
}
catch (SQLException ex)
{
log.log(Level.SEVERE,"getLines", ex);
}
String sql = "SELECT * FROM M_Cost WHERE M_Product_ID=? AND C_Acctschema_ID = ? AND M_CostType_ID = ? AND M_CostElement_ID = ? AND AD_Client_ID = ?";
PreparedStatement pstmt = null;
try
{
pstmt = DB.prepareStatement(sql,get_TrxName());
pstmt.setInt(1, M_Product_ID);
pstmt.setInt(2, p_C_AcctSchema_ID);
pstmt.setInt(3, p_M_CostType_ID);
pstmt.setInt(4, Elementtypeint);
//pstmt.setInt(5, M_Warehouse_ID);
//pstmt.setInt(6, S_Resource_ID);
pstmt.setInt(5, getAD_Client_ID());
ResultSet rs = pstmt.executeQuery();
while (rs.next())
{
pp= true;
}
rs.close();
pstmt.close();
pstmt = null;
}
catch (SQLException ex)
{
log.log(Level.SEVERE,"getLines", ex);
}
//System.out.println("pp ********** " +pp);
MProduct M_Product = new MProduct(getCtx(), M_Product_ID,null);
if (pp)
{
try
{
// System.out.println("producto value ********** * " +M_Product.getValue());
StringBuffer sqlprod = new StringBuffer("SELECT PP_Product_BOM_ID FROM PP_Product_BOM WHERE Value Like '" + M_Product.getValue() + "%' AND AD_Client_ID = "+getAD_Client_ID() +" ORDER BY Value");
PreparedStatement pstmtprod = DB.prepareStatement(sqlprod.toString(), get_TrxName());
//System.out.println("query ********** * " +sqlprod.toString());
ResultSet rsprod = pstmtprod.executeQuery();
if (rsprod.next())
{
//System.out.println("producto del bom ********** * " +rsprod.getInt(1));
return rsprod.getInt(1);
}
rsprod.close();
pstmtprod.close();
}
catch (SQLException s)
{
//log.log(Level.SEVERE, s);
}
}
return 0;
}
} // OrderOpen
}
return m_cost;
}
}

View File

@ -37,11 +37,15 @@ import org.eevolution.model.MPPProductPlanning;
public class RollupWorkflow extends SvrProcess
{
/* Organization */
private int p_AD_Org_ID = 0;
/* Account Schema */
private int p_C_AcctSchema_ID = 0;
private int p_M_Product_ID = 0;
private int p_M_CostType_ID = 0;
/* Cost Type */
private int p_M_CostType_ID = 0;
/* Product */
private int p_M_Product_ID = 0;
/* Product Category */
private int p_M_Product_Category_ID = 0;
/**
@ -56,26 +60,16 @@ public class RollupWorkflow extends SvrProcess
if (para[i].getParameter() == null)
;
else if (name.equals("AD_Org_ID"))
{
else if (name.equals("AD_Org_ID"))
p_AD_Org_ID = para[i].getParameterAsInt();
}
else if (name.equals("C_AcctSchema_ID"))
{
p_C_AcctSchema_ID = para[i].getParameterAsInt();
}
p_C_AcctSchema_ID = para[i].getParameterAsInt();
else if (name.equals("M_CostType_ID"))
{
p_M_CostType_ID = para[i].getParameterAsInt();
}
else if (name.equals("M_Product_ID"))
{
else if (name.equals("M_Product_ID"))
p_M_Product_ID = para[i].getParameterAsInt();
}
else if (name.equals("M_Product_Category_ID"))
{
else if (name.equals("M_Product_Category_ID"))
p_M_Product_Category_ID = para[i].getParameterAsInt();
}
else
log.log(Level.SEVERE,"prepare - Unknown Parameter: " + name);
}
@ -88,64 +82,45 @@ public class RollupWorkflow extends SvrProcess
*/
protected String doIt() throws Exception
{
StringBuffer sql = new StringBuffer ("SELECT p.M_Product_ID FROM M_Product p WHERE p.ProductType = '" + MProduct.PRODUCTTYPE_Item + "' AND");
if (p_M_Product_ID != 0)
{
sql.append(" p.M_Product_ID = " + p_M_Product_ID + " AND ");
}
if (p_M_Product_Category_ID != 0)
{
sql.append(" p.M_Product_Category_ID = " + p_M_Product_Category_ID + " AND ");
}
sql.append(" p.AD_Client_ID = " + getAD_Client_ID());
sql.append(" ORDER BY p.LowLevel");
PreparedStatement pstmt = null;
try
{
pstmt = DB.prepareStatement (sql.toString(), get_TrxName());
ResultSet rs = pstmt.executeQuery ();
while (rs.next())
{
int M_Product_ID = rs.getInt("M_Product_ID");
MCost[] costs = MCost.getCosts(getCtx() , getAD_Client_ID(), p_AD_Org_ID , M_Product_ID , p_M_CostType_ID , p_C_AcctSchema_ID , get_TrxName());
StringBuffer whereClause = new StringBuffer("AD_Client_ID=? AND ProductType='"+MProduct.PRODUCTTYPE_Item+"'");
List<Object> params = new ArrayList<Object>();
params.add(getAD_Client_ID());
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())
.setOrderBy(MProduct.COLUMNNAME_LowLevel).setParameters(params).list();
for (MProduct product : products)
{
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 )
{
MCostElement element = new MCostElement(getCtx(), cost.getM_CostElement_ID(), get_TrxName());
// check if element cost is of type Labor
if (element.getCostElementType().equals(element.COSTELEMENTTYPE_Resource))
{
BigDecimal Labor = getCost(element.COSTELEMENTTYPE_Resource , getAD_Client_ID(), p_AD_Org_ID , M_Product_ID , p_M_CostType_ID , p_C_AcctSchema_ID);
MCostElement element = new MCostElement(getCtx(), cost.getM_CostElement_ID(), get_TrxName());
// check if element cost is of type Labor
if (element.getCostElementType().equals(MCostElement.COSTELEMENTTYPE_Resource))
{
BigDecimal Labor = getCost(MCostElement.COSTELEMENTTYPE_Resource , getAD_Client_ID(), p_AD_Org_ID , product , p_M_CostType_ID , p_C_AcctSchema_ID);
log.info("Labor : " + Labor);
cost.setCurrentCostPrice(Labor);
cost.save();
continue;
}
if (element.getCostElementType().equals(element.COSTELEMENTTYPE_BurdenMOverhead))
{
BigDecimal Burden = getCost(element.COSTELEMENTTYPE_BurdenMOverhead, getAD_Client_ID() , p_AD_Org_ID , M_Product_ID , p_M_CostType_ID , p_C_AcctSchema_ID);
}
else if (element.getCostElementType().equals(MCostElement.COSTELEMENTTYPE_BurdenMOverhead))
{
BigDecimal Burden = getCost(MCostElement.COSTELEMENTTYPE_BurdenMOverhead, getAD_Client_ID() , p_AD_Org_ID , product , p_M_CostType_ID , p_C_AcctSchema_ID);
log.info("Burden : " + Burden);
cost.setCurrentCostPrice(Burden);
cost.save(get_TrxName());
continue;
}
}
}
}
rs.close();
pstmt.close();
}
catch (Exception e)
{
log.log(Level.SEVERE,"doIt - " + sql, e);
return null;
}
}
return "@OK@";
}
@ -154,19 +129,25 @@ public class RollupWorkflow extends SvrProcess
* Calculate Cost
* @param CostElementType Cost Element Type (Labor and Overhead.)
* @param AD_Org_ID Organization
* @param M_Product_ID Product
* @param MProduct Product
* @param M_CostType_ID Cost Type
* @param C_AcctSchema_ID Account Schema
* @return Cost for this Element
* @throws Exception if not successful
*/
private BigDecimal getCost(String CostElementType ,int AD_Client_ID , int AD_Org_ID , int M_Product_ID , int M_CostType_ID , int C_AcctSchema_ID)
private BigDecimal getCost(String CostElementType ,int AD_Client_ID , int AD_Org_ID , MProduct product , int M_CostType_ID , int C_AcctSchema_ID)
{
BigDecimal cost = Env.ZERO;
MPPProductPlanning pp = MPPProductPlanning.find(getCtx(), AD_Client_ID, AD_Org_ID , 0 , 0 , product.getM_Product_ID(), get_TrxName());
int AD_Workflow_ID = 0;
if(pp != null )
AD_Workflow_ID = pp.getAD_Workflow_ID();
else
AD_Workflow_ID = MWorkflow.getWorkflowSearchKey(getCtx(), product);
if(AD_Workflow_ID <= 0)
return Env.ZERO;
int AD_Workflow_ID = getAD_Workflow_ID(AD_Client_ID , AD_Org_ID , M_Product_ID);
if (AD_Workflow_ID != 0)
{
MWorkflow Workflow = new MWorkflow(getCtx(),AD_Workflow_ID,get_TrxName());
MWFNode[] nodes = Workflow.getNodes(false,getAD_Client_ID());
@ -189,11 +170,7 @@ public class RollupWorkflow extends SvrProcess
}
log.info("Node" + node.getName() + " CostElementType"+ CostElementType +" Duration=" + node.getDuration() + " rate:" + rate + " Cost:" + cost);
}
return cost;
}
return cost;
return cost;
}
/**
@ -208,8 +185,9 @@ public class RollupWorkflow extends SvrProcess
*/
private BigDecimal getRate(String CostElementType , int S_Resource_ID , int AD_Org_ID , int C_AcctSchema_ID ,int M_CostType_ID)
{
int M_Product_ID = getM_Product_ID(S_Resource_ID);
MCost[] costs = MCost.getCosts(getCtx() , getAD_Client_ID(), p_AD_Org_ID , M_Product_ID , p_M_CostType_ID , p_C_AcctSchema_ID , get_TrxName());
MResource resource = MResource.get(getCtx(), S_Resource_ID);
MProduct product = resource.getProduct();
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());
if (costs != null)
{
BigDecimal rate = Env.ZERO;
@ -228,61 +206,5 @@ public class RollupWorkflow extends SvrProcess
return rate;
}
return Env.ZERO;
}
/**
* get Product of Resource
* @param S_Resource_ID Resource
* @return Product ID
**/
private int getM_Product_ID(int S_Resource_ID)
{
String whereClause = "S_Resource_ID = ?";
Query query = MTable.get(getCtx(), MProduct.Table_ID)
.createQuery(whereClause, get_TrxName());
query.setParameters(new Object[]{S_Resource_ID});
List<MProduct> products = query.list();
for(MProduct product : products)
{
return product.getM_Product_ID();
}
return 0;
}
/**
* get Manufacturing Workflow
* @param AD_Org Organization ID
* @param M_Product_ID Product ID
* @return Workflow ID
**/
private int getAD_Workflow_ID(int AD_Client_ID ,int AD_Org_ID , int M_Product_ID)
{
MPPProductPlanning pp = MPPProductPlanning.get(getCtx(), AD_Client_ID, AD_Org_ID , M_Product_ID, get_TrxName());
MProduct M_Product = new MProduct(getCtx(), M_Product_ID,null);
int AD_Workflow_ID = 0;
if ( pp == null )
{
String whereClause = "Name=?";
Query query = MTable.get(getCtx(), MWorkflow.Table_ID)
.createQuery(whereClause, get_TrxName());
query.setParameters(new Object[]{M_Product.getName()});
List<MWorkflow> workflows = query.list();
for (MWorkflow workflow : workflows)
{
return workflow.getAD_Workflow_ID();
}
}
else
{
AD_Workflow_ID = pp.getAD_Workflow_ID();
}
return AD_Workflow_ID;
}
}
}