parent
f1c81b17b9
commit
0e6b5c6201
|
@ -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,17 +44,17 @@ 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>();
|
|
||||||
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(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();
|
|
||||||
}
|
|
||||||
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());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return "@OK@";
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
List<Object> params = new ArrayList<Object>();
|
||||||
* Calculate Cost
|
params.add(getAD_Client_ID());
|
||||||
* @param CostElementType Cost Element Type (Labor and Overhead.)
|
if (p_M_Product_ID > 0) {
|
||||||
* @param AD_Org_ID Organization
|
whereClause.append(" AND M_Product_ID=?");
|
||||||
* @param MProduct Product
|
params.add(p_M_Product_ID);
|
||||||
* @param M_CostType_ID Cost Type
|
}
|
||||||
* @param C_AcctSchema_ID Account Schema
|
if (p_M_Product_Category_ID > 0){
|
||||||
* @return Cost for this Element
|
whereClause.append(" AND M_Product_Category_ID=?");
|
||||||
* @throws Exception if not successful
|
params.add(p_M_Product_Category_ID);
|
||||||
*/
|
}
|
||||||
private BigDecimal getCost(String CostElementType ,int AD_Client_ID , int AD_Org_ID , MProduct product , int M_CostType_ID , int C_AcctSchema_ID)
|
|
||||||
{
|
List<MProduct> products = new Query(getCtx(),MProduct.Table_Name, whereClause.toString(), get_TrxName())
|
||||||
BigDecimal cost = Env.ZERO;
|
.setOrderBy(MProduct.COLUMNNAME_LowLevel)
|
||||||
MPPProductPlanning pp = MPPProductPlanning.find(getCtx(), AD_Org_ID , 0 , 0 , product.getM_Product_ID(), get_TrxName());
|
.setParameters(params)
|
||||||
int AD_Workflow_ID = 0;
|
.list();
|
||||||
if(pp != null )
|
|
||||||
AD_Workflow_ID = pp.getAD_Workflow_ID();
|
for (MProduct product : products)
|
||||||
else
|
{
|
||||||
AD_Workflow_ID = MWorkflow.getWorkflowSearchKey(getCtx(), product);
|
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)
|
||||||
if(AD_Workflow_ID <= 0)
|
{
|
||||||
return Env.ZERO;
|
MCostElement element = new MCostElement(getCtx(), cost.getM_CostElement_ID(), get_TrxName());
|
||||||
|
// check if element cost is of type Labor
|
||||||
MWorkflow Workflow = new MWorkflow(getCtx(),AD_Workflow_ID,get_TrxName());
|
if (element.getCostElementType().equals(MCostElement.COSTELEMENTTYPE_Resource))
|
||||||
MWFNode[] nodes = Workflow.getNodes(false,getAD_Client_ID());
|
{
|
||||||
|
BigDecimal Labor = getCost(MCostElement.COSTELEMENTTYPE_Resource , getAD_Client_ID(), p_AD_Org_ID , product , p_M_CostType_ID , p_C_AcctSchema_ID);
|
||||||
for (MWFNode node : nodes )
|
log.info("Labor : " + Labor);
|
||||||
{
|
cost.setCurrentCostPrice(Labor);
|
||||||
BigDecimal rate = getRate(CostElementType, node.getS_Resource_ID(), AD_Org_ID , C_AcctSchema_ID , M_CostType_ID);
|
cost.save();
|
||||||
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());
|
else if (element.getCostElementType().equals(MCostElement.COSTELEMENTTYPE_BurdenMOverhead))
|
||||||
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);
|
BigDecimal Burden = getCost(MCostElement.COSTELEMENTTYPE_BurdenMOverhead, getAD_Client_ID() , p_AD_Org_ID , product , p_M_CostType_ID , p_C_AcctSchema_ID);
|
||||||
if (uom.isHour())
|
log.info("Burden : " + Burden);
|
||||||
{
|
cost.setCurrentCostPrice(Burden);
|
||||||
|
cost.save(get_TrxName());
|
||||||
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);
|
return "@OK@";
|
||||||
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);
|
/**
|
||||||
}
|
* Calculate Cost
|
||||||
return cost;
|
* @param CostElementType Cost Element Type (Labor and Overhead.)
|
||||||
}
|
* @param AD_Org_ID Organization
|
||||||
|
* @param MProduct Product
|
||||||
/**
|
* @param M_CostType_ID Cost Type
|
||||||
* get Rate for this Resource
|
* @param C_AcctSchema_ID Account Schema
|
||||||
* @param CostElementType Cost Element Type (Labor and Overhead.)
|
* @return Cost for this Element
|
||||||
* @param S_Resource_ID Respurce
|
* @throws Exception if not successful
|
||||||
* @param AD_Org_ID Organization
|
*/
|
||||||
* @param C_AcctSchema_ID Account Schema
|
private BigDecimal getCost(String CostElementType ,int AD_Client_ID , int AD_Org_ID , MProduct product , int M_CostType_ID , int C_AcctSchema_ID)
|
||||||
* @param M_CostType_ID Cost Type
|
{
|
||||||
* @return Rate for Resource
|
BigDecimal cost = Env.ZERO;
|
||||||
* @throws Exception if not successful
|
MPPProductPlanning pp = MPPProductPlanning.find(getCtx(), AD_Org_ID , 0 , 0 , product.getM_Product_ID(), get_TrxName());
|
||||||
*/
|
int AD_Workflow_ID = 0;
|
||||||
private BigDecimal getRate(String CostElementType , int S_Resource_ID , int AD_Org_ID , int C_AcctSchema_ID ,int M_CostType_ID)
|
if(pp != null )
|
||||||
{
|
AD_Workflow_ID = pp.getAD_Workflow_ID();
|
||||||
MResource resource = MResource.get(getCtx(), S_Resource_ID);
|
else
|
||||||
MProduct product = resource.getProduct();
|
AD_Workflow_ID = MWorkflow.getWorkflowSearchKey(getCtx(), product);
|
||||||
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(AD_Workflow_ID <= 0)
|
||||||
if (costs != null)
|
return Env.ZERO;
|
||||||
{
|
|
||||||
BigDecimal rate = Env.ZERO;
|
MWorkflow Workflow = new MWorkflow(getCtx(),AD_Workflow_ID,get_TrxName());
|
||||||
|
MWFNode[] nodes = Workflow.getNodes(false,getAD_Client_ID());
|
||||||
for (MCost cost: costs)
|
for (MWFNode node : nodes)
|
||||||
{
|
{
|
||||||
MCostElement element = new MCostElement(getCtx(), cost .getM_CostElement_ID(), get_TrxName());
|
BigDecimal rate = getRate(CostElementType, node.getS_Resource_ID(), AD_Org_ID , C_AcctSchema_ID , M_CostType_ID);
|
||||||
// check if element cost is of type Labor
|
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 = ?" ;
|
||||||
if (element.getCostElementType().equals(CostElementType))
|
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());
|
||||||
rate = rate.add(cost.getCurrentCostPrice());
|
MUOM uom = MUOM.get(getCtx(), C_UOM_ID);
|
||||||
log.info("Org" + AD_Org_ID + "S_Resource" + S_Resource_ID + "C_AcctSchema_ID " + C_AcctSchema_ID);
|
if (uom.isHour())
|
||||||
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue