RollupWorkflow:

* organized imports
* fixed indentation
This commit is contained in:
teo_sarca 2008-09-04 12:14:12 +00:00
parent f1c81b17b9
commit 0e6b5c6201
1 changed files with 156 additions and 150 deletions

View File

@ -16,16 +16,23 @@
package org.eevolution.process; package org.eevolution.process;
import java.util.logging.*; import java.math.BigDecimal;
import java.math.*; import java.util.ArrayList;
import java.sql.*; import java.util.List;
import java.util.*; import java.util.logging.Level;
import org.compiere.model.MCost;
import org.compiere.model.*; import org.compiere.model.MCostElement;
import org.compiere.wf.*; import org.compiere.model.MProduct;
import org.compiere.util.*; import org.compiere.model.MResource;
import org.compiere.process.*; import org.compiere.model.MUOM;
import org.compiere.model.Query;
import org.compiere.process.ProcessInfoParameter;
import org.compiere.process.SvrProcess;
import org.compiere.util.DB;
import org.compiere.util.Env;
import org.compiere.wf.MWFNode;
import org.compiere.wf.MWorkflow;
import org.eevolution.model.MPPProductPlanning; import org.eevolution.model.MPPProductPlanning;
/** /**
@ -37,16 +44,16 @@ import org.eevolution.model.MPPProductPlanning;
public class RollupWorkflow extends SvrProcess public class RollupWorkflow 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;
/** /**
* Prepare - e.g., get Parameters. * Prepare - e.g., get Parameters.
@ -65,12 +72,12 @@ public class RollupWorkflow extends SvrProcess
else if (name.equals("C_AcctSchema_ID")) 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")) else if (name.equals("M_CostType_ID"))
p_M_CostType_ID = para[i].getParameterAsInt(); 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(); 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(); p_M_Product_Category_ID = para[i].getParameterAsInt();
else else
log.log(Level.SEVERE,"prepare - Unknown Parameter: " + name); log.log(Level.SEVERE,"prepare - Unknown Parameter: " + name);
} }
} // prepare } // prepare
@ -80,131 +87,130 @@ public class RollupWorkflow extends SvrProcess
* @return Message (text with variables) * @return Message (text with variables)
* @throws Exception if not successful * @throws Exception if not successful
*/ */
protected String doIt() throws Exception protected String doIt() throws Exception
{ {
StringBuffer whereClause = new StringBuffer("AD_Client_ID=? AND ProductType='"+MProduct.PRODUCTTYPE_Item+"'"); StringBuffer whereClause = new StringBuffer("AD_Client_ID=? AND ProductType='"+MProduct.PRODUCTTYPE_Item+"'");
List<Object> params = new ArrayList<Object>(); List<Object> params = new ArrayList<Object>();
params.add(getAD_Client_ID()); params.add(getAD_Client_ID());
if (p_M_Product_ID > 0) { if (p_M_Product_ID > 0) {
whereClause.append(" AND p.M_Product_ID=?"); whereClause.append(" AND M_Product_ID=?");
params.add(p_M_Product_ID); params.add(p_M_Product_ID);
} }
if (p_M_Product_Category_ID > 0){ if (p_M_Product_Category_ID > 0){
whereClause.append(" AND p.M_Product_Category_ID=?"); whereClause.append(" AND M_Product_Category_ID=?");
params.add(p_M_Product_ID); params.add(p_M_Product_Category_ID);
} }
List<MProduct> products = new Query(getCtx(),MProduct.Table_Name, whereClause.toString(), get_TrxName()) List<MProduct> products = new Query(getCtx(),MProduct.Table_Name, whereClause.toString(), get_TrxName())
.setOrderBy(MProduct.COLUMNNAME_LowLevel).setParameters(params).list(); .setOrderBy(MProduct.COLUMNNAME_LowLevel)
.setParameters(params)
.list();
for (MProduct product : products) 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()); 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 ) for (MCost cost : costs)
{ {
MCostElement element = new MCostElement(getCtx(), cost.getM_CostElement_ID(), get_TrxName()); MCostElement element = new MCostElement(getCtx(), cost.getM_CostElement_ID(), get_TrxName());
// check if element cost is of type Labor // check if element cost is of type Labor
if (element.getCostElementType().equals(MCostElement.COSTELEMENTTYPE_Resource)) 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); 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); log.info("Labor : " + Labor);
cost.setCurrentCostPrice(Labor); cost.setCurrentCostPrice(Labor);
cost.save(); cost.save();
} }
else if (element.getCostElementType().equals(MCostElement.COSTELEMENTTYPE_BurdenMOverhead)) 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); 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); log.info("Burden : " + Burden);
cost.setCurrentCostPrice(Burden); cost.setCurrentCostPrice(Burden);
cost.save(get_TrxName()); cost.save(get_TrxName());
} }
} }
} }
return "@OK@"; return "@OK@";
} }
/** /**
* Calculate Cost * Calculate Cost
* @param CostElementType Cost Element Type (Labor and Overhead.) * @param CostElementType Cost Element Type (Labor and Overhead.)
* @param AD_Org_ID Organization * @param AD_Org_ID Organization
* @param MProduct Product * @param MProduct Product
* @param M_CostType_ID Cost Type * @param M_CostType_ID Cost Type
* @param C_AcctSchema_ID Account Schema * @param C_AcctSchema_ID Account Schema
* @return Cost for this Element * @return Cost for this Element
* @throws Exception if not successful * @throws Exception if not successful
*/ */
private BigDecimal getCost(String CostElementType ,int AD_Client_ID , int AD_Org_ID , MProduct product , 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; BigDecimal cost = Env.ZERO;
MPPProductPlanning pp = MPPProductPlanning.find(getCtx(), AD_Org_ID , 0 , 0 , product.getM_Product_ID(), get_TrxName()); MPPProductPlanning pp = MPPProductPlanning.find(getCtx(), AD_Org_ID , 0 , 0 , product.getM_Product_ID(), get_TrxName());
int AD_Workflow_ID = 0; int AD_Workflow_ID = 0;
if(pp != null ) if(pp != null )
AD_Workflow_ID = pp.getAD_Workflow_ID(); AD_Workflow_ID = pp.getAD_Workflow_ID();
else else
AD_Workflow_ID = MWorkflow.getWorkflowSearchKey(getCtx(), product); AD_Workflow_ID = MWorkflow.getWorkflowSearchKey(getCtx(), product);
if(AD_Workflow_ID <= 0)
return Env.ZERO;
if(AD_Workflow_ID <= 0) MWorkflow Workflow = new MWorkflow(getCtx(),AD_Workflow_ID,get_TrxName());
return Env.ZERO; MWFNode[] nodes = Workflow.getNodes(false,getAD_Client_ID());
for (MWFNode node : nodes)
{
BigDecimal rate = getRate(CostElementType, node.getS_Resource_ID(), AD_Org_ID , C_AcctSchema_ID , M_CostType_ID);
String sql = "SELECT CASE WHEN ow.DurationUnit = 's' THEN 1 * ( (onode.SetupTime/ow.QtyBatchSize) + onode.Duration ) WHEN ow.DurationUnit = 'm' THEN 60 * ( (onode.SetupTime/ow.QtyBatchSize) + onode.Duration) WHEN ow.DurationUnit = 'h' THEN 3600 * ( (onode.SetupTime/ow.QtyBatchSize) + onode.Duration) WHEN ow.DurationUnit = 'Y' THEN 31536000 * ( (onode.SetupTime/ow.QtyBatchSize) + onode.Duration) WHEN ow.DurationUnit = 'M' THEN 2592000 * ( (onode.SetupTime/ow.QtyBatchSize) + onode.Duration ) WHEN ow.DurationUnit = 'D' THEN 86400 * ((onode.SetupTime/ow.QtyBatchSize) + onode.Duration) END AS load FROM AD_WF_Node onode INNER JOIN AD_Workflow ow ON (ow.AD_Workflow_ID = onode.AD_Workflow_ID) WHERE onode.AD_WF_Node_ID = ? AND onode.AD_Client_ID = ?" ;
int seconds = DB.getSQLValue(get_TrxName(),sql,node.getAD_WF_Node_ID(),node.getAD_Client_ID());
int C_UOM_ID = DB.getSQLValue(get_TrxName(),"SELECT C_UOM_ID FROM M_Product WHERE S_Resource_ID = ? " , node.getS_Resource_ID());
MUOM uom = MUOM.get(getCtx(), C_UOM_ID);
if (uom.isHour())
{
BigDecimal time = new BigDecimal(seconds);
cost = cost.add(time.multiply(rate).divide(new BigDecimal(3600),BigDecimal.ROUND_HALF_UP,12));
log.info("Yes isHour" + seconds);
log.info("seconds/3600"+ seconds/3600);
log.info("time.multiply(rate)"+ time.multiply(rate));
log.info("Cost" + cost);
}
log.info("Node" + node.getName() + " CostElementType"+ CostElementType +" Duration=" + node.getDuration() + " rate:" + rate + " Cost:" + cost);
}
return cost;
}
MWorkflow Workflow = new MWorkflow(getCtx(),AD_Workflow_ID,get_TrxName()); /**
MWFNode[] nodes = Workflow.getNodes(false,getAD_Client_ID()); * get Rate for this Resource
* @param CostElementType Cost Element Type (Labor and Overhead.)
* @param S_Resource_ID Respurce
* @param AD_Org_ID Organization
* @param C_AcctSchema_ID Account Schema
* @param M_CostType_ID Cost Type
* @return Rate for Resource
* @throws Exception if not successful
*/
private BigDecimal getRate(String CostElementType , int S_Resource_ID , int AD_Org_ID , int C_AcctSchema_ID ,int M_CostType_ID)
{
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;
for (MWFNode node : nodes ) for (MCost cost: costs)
{ {
BigDecimal rate = getRate(CostElementType, node.getS_Resource_ID(), AD_Org_ID , C_AcctSchema_ID , M_CostType_ID); MCostElement element = new MCostElement(getCtx(), cost .getM_CostElement_ID(), get_TrxName());
String sql = "SELECT CASE WHEN ow.DurationUnit = 's' THEN 1 * ( (onode.SetupTime/ow.QtyBatchSize) + onode.Duration ) WHEN ow.DurationUnit = 'm' THEN 60 * ( (onode.SetupTime/ow.QtyBatchSize) + onode.Duration) WHEN ow.DurationUnit = 'h' THEN 3600 * ( (onode.SetupTime/ow.QtyBatchSize) + onode.Duration) WHEN ow.DurationUnit = 'Y' THEN 31536000 * ( (onode.SetupTime/ow.QtyBatchSize) + onode.Duration) WHEN ow.DurationUnit = 'M' THEN 2592000 * ( (onode.SetupTime/ow.QtyBatchSize) + onode.Duration ) WHEN ow.DurationUnit = 'D' THEN 86400 * ((onode.SetupTime/ow.QtyBatchSize) + onode.Duration) END AS load FROM AD_WF_Node onode INNER JOIN AD_Workflow ow ON (ow.AD_Workflow_ID = onode.AD_Workflow_ID) WHERE onode.AD_WF_Node_ID = ? AND onode.AD_Client_ID = ?" ; // check if element cost is of type Labor
int seconds = DB.getSQLValue(get_TrxName(),sql,node.getAD_WF_Node_ID(),node.getAD_Client_ID()); if (element.getCostElementType().equals(CostElementType))
int C_UOM_ID = DB.getSQLValue(get_TrxName(),"SELECT C_UOM_ID FROM M_Product WHERE S_Resource_ID = ? " , node.getS_Resource_ID()); {
MUOM uom = MUOM.get(getCtx(), C_UOM_ID); rate = rate.add(cost.getCurrentCostPrice());
if (uom.isHour()) log.info("Org" + AD_Org_ID + "S_Resource" + S_Resource_ID + "C_AcctSchema_ID " + C_AcctSchema_ID);
{ log.info("Element rate=" + CostElementType + "rate:" + rate);
}
BigDecimal time = new BigDecimal(seconds); }
cost = cost.add(time.multiply(rate).divide(new BigDecimal(3600),BigDecimal.ROUND_HALF_UP,12)); return rate;
log.info("Yes isHour" + seconds); }
log.info("seconds/3600"+ seconds/3600); return Env.ZERO;
log.info("time.multiply(rate)"+ time.multiply(rate)); }
log.info("Cost" + cost);
}
log.info("Node" + node.getName() + " CostElementType"+ CostElementType +" Duration=" + node.getDuration() + " rate:" + rate + " Cost:" + cost);
}
return cost;
}
/**
* get Rate for this Resource
* @param CostElementType Cost Element Type (Labor and Overhead.)
* @param S_Resource_ID Respurce
* @param AD_Org_ID Organization
* @param C_AcctSchema_ID Account Schema
* @param M_CostType_ID Cost Type
* @return Rate for Resource
* @throws Exception if not successful
*/
private BigDecimal getRate(String CostElementType , int S_Resource_ID , int AD_Org_ID , int C_AcctSchema_ID ,int M_CostType_ID)
{
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;
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))
{
rate = rate.add(cost.getCurrentCostPrice());
log.info("Org" + AD_Org_ID + "S_Resource" + S_Resource_ID + "C_AcctSchema_ID " + C_AcctSchema_ID);
log.info("Element rate=" + CostElementType + "rate:" + rate);
}
}
return rate;
}
return Env.ZERO;
}
} }