It is not rolling up correctly the lower level costs

http://sourceforge.net/tracker2/?func=detail&aid=2530003&group_id=176962&atid=879332


Cleanup after revision 8228:
* MCost: refactored getByCostType methods
* organized imports
* fixed indentation
This commit is contained in:
teo_sarca 2009-01-29 07:24:17 +00:00
parent d8e27088c0
commit c9eb93eacb
4 changed files with 59 additions and 95 deletions

View File

@ -52,54 +52,24 @@ public class MCost extends X_M_Cost
private static final long serialVersionUID = 1L;
/**
* Get the the Total Cost for this Cost Element Type and Costing Method
* Get the the Total Cost for Cost Type and Cost Element Type
* @param product Product
* @param as Account Schema
* @param AD_Org_ID Organization ID
* @param M_AttributeSetInstance_ID Attribute Set Instance ID
* @param C_CostType_ID TODO
* @param M_CostType_ID cost type
* @param CostElementType Cost Element Type
* @param Qty Quantity
* @return Get the the Total Cost for this Cost Element Type and Costing Method
* @return Total Costs for Cost Type and Cost Element Type
*/
public static BigDecimal getCostByCostType (MProduct product, MAcctSchema as,
int AD_Org_ID, int M_AttributeSetInstance_ID,
int C_CostType_ID, String CostElementType,
int M_CostType_ID, String CostElementType,
BigDecimal Qty)
{
//Set the Costing Level
String CostingLevel = product.getCostingLevel(as);
if (MAcctSchema.COSTINGLEVEL_Client.equals(CostingLevel))
{
AD_Org_ID = 0;
M_AttributeSetInstance_ID = 0;
}
else if (MAcctSchema.COSTINGLEVEL_Organization.equals(CostingLevel))
M_AttributeSetInstance_ID = 0;
else if (MAcctSchema.COSTINGLEVEL_BatchLot.equals(CostingLevel))
AD_Org_ID = 0;
Collection<MCost> costs = null;
Collection<MCost> costs = getByCostType(product, as, M_CostType_ID, AD_Org_ID, M_AttributeSetInstance_ID,
CostElementType);
BigDecimal m_cost = Env.ZERO;
String whereClause = "AD_Client_ID=? AND AD_Org_ID=?"
+ " AND M_Product_ID=?"
+ " AND M_AttributeSetInstance_ID=?"
+ " AND C_AcctSchema_ID=?"
+ " AND EXISTS ( SELECT 1 FROM M_CostElement ce "
+ " WHERE ce.M_CostElement_ID=M_Cost.M_CostElement_ID "
+ " AND ce.CostElementType=?)";
costs = new Query(product.getCtx(), MCost.Table_Name, whereClause, product.get_TrxName())
.setParameters(new Object[]{
product.getAD_Client_ID(),
AD_Org_ID,
product.getM_Product_ID(),
M_AttributeSetInstance_ID,
as.getC_AcctSchema_ID(),
CostElementType})
.setOnlyActiveRecords(true)
.list();
for(MCost cost : costs)
{
m_cost = cost.getCurrentCostPrice().add(cost.getCurrentCostPriceLL());
@ -109,7 +79,7 @@ public class MCost extends X_M_Cost
} // get
/**
* Get MCost for Cost Type and Cost Element Type
* Get MCosts for Cost Type and Cost Element Type
* @param product Product
* @param as Account Schema
* @param AD_Org_ID Organization ID
@ -117,13 +87,8 @@ public class MCost extends X_M_Cost
* @param CostElementType Cost Element Type
* @return Get MCost Collection for Cost Type and Cost Element Type
*/
public static Collection<MCost> getByCostType (
MProduct product,
MAcctSchema as,
int M_CostType_ID ,
int AD_Org_ID,
int M_AttributeSetInstance_ID ,
String CostElementType)
public static Collection<MCost> getByCostType (MProduct product, MAcctSchema as,
int M_CostType_ID, int AD_Org_ID, int M_AttributeSetInstance_ID, String CostElementType)
{
//Set the Costing Level
String CostingLevel = product.getCostingLevel(as);
@ -133,16 +98,20 @@ public class MCost extends X_M_Cost
M_AttributeSetInstance_ID = 0;
}
else if (MAcctSchema.COSTINGLEVEL_Organization.equals(CostingLevel))
{
M_AttributeSetInstance_ID = 0;
}
else if (MAcctSchema.COSTINGLEVEL_BatchLot.equals(CostingLevel))
{
AD_Org_ID = 0;
}
String whereClause = "AD_Client_ID=? AND AD_Org_ID=?"
+ " AND M_Product_ID=?"
+ " AND M_AttributeSetInstance_ID=?"
+ " AND C_AcctSchema_ID=?"
+ " AND M_CostType_ID=?"
+ " AND EXISTS ( SELECT 1 FROM M_CostElement ce "
+ " AND "+COLUMNNAME_M_Product_ID+"=?"
+ " AND "+COLUMNNAME_M_AttributeSetInstance_ID+"=?"
+ " AND "+COLUMNNAME_C_AcctSchema_ID+"=?"
+ " AND "+COLUMNNAME_M_CostType_ID+"=?"
+ " AND EXISTS (SELECT 1 FROM M_CostElement ce "
+ " WHERE ce.M_CostElement_ID=M_Cost.M_CostElement_ID ";
List<Object> params = new ArrayList<Object>();
@ -154,8 +123,8 @@ public class MCost extends X_M_Cost
params.add(M_CostType_ID);
if(CostElementType != null)
{
params.add(CostElementType);
whereClause += "AND ce.CostElementType=?";
params.add(CostElementType);
}
whereClause += ")";
@ -167,7 +136,7 @@ public class MCost extends X_M_Cost
} // get
/**
* Get MCost for for Cost Type
* Get MCosts for for Cost Type
* @param product Product
* @param as Account Schema
* @param M_CostType_ID Cost Type
@ -790,7 +759,7 @@ public class MCost extends X_M_Cost
/**
* Create standard Costing records for Product
* @param product product
**/
*/
protected static void create (MProduct product)
{
s_log.config(product.getName());

View File

@ -312,7 +312,7 @@ public class MCostElement extends X_M_CostElement
* @param ctx context
* @param trxName transaction
* @return array cost elements
**/
*/
public static MCostElement[] getElements (Properties ctx, String trxName)
{
int AD_Client_ID = Env.getAD_Client_ID(ctx);
@ -333,7 +333,7 @@ public class MCostElement extends X_M_CostElement
* @param trxName transaction
* @return array cost elements
**/
public static Collection getByCostingMethod (Properties ctx, String CostingMethod)
public static Collection<MCostElement> getByCostingMethod (Properties ctx, String CostingMethod)
{
final String whereClause = "CostingMethod=?";
return new Query(ctx, Table_Name, whereClause, null)

View File

@ -31,11 +31,9 @@ import org.adempiere.exceptions.DocTypeNotFoundException;
import org.compiere.model.MAcctSchema;
import org.compiere.model.MClient;
import org.compiere.model.MCost;
import org.compiere.model.MCostElement;
import org.compiere.model.MDocType;
import org.compiere.model.MProduct;
import org.compiere.model.MProject;
import org.compiere.model.MRefList;
import org.compiere.model.MResource;
import org.compiere.model.MStorage;
import org.compiere.model.MTable;
@ -57,10 +55,7 @@ import org.compiere.wf.MWFNodeNext;
import org.compiere.wf.MWorkflow;
/**
* Order Model.
* Please do not set DocStatus and C_DocType_ID directly.
* They are set in the process() method.
* Use DocAction and C_DocTypeTarget_ID instead.
* PP Order Model.
*
* @author Victor Perez www.e-evolution.com
* @author Teo Sarca, www.arhipac.ro
@ -153,7 +148,6 @@ public class MPPOrder extends X_PP_Order implements DocAction
if (resoruce == null)
throw new IllegalStateException("Resource is mandatory.");
setS_Resource_ID(resoruce.getS_Resource_ID());
} // MOrder
/**
@ -567,7 +561,7 @@ public class MPPOrder extends X_PP_Order implements DocAction
//
// Create Standard Costs for Order
Collection <MCost> costs = MCost.getByCostType(getM_Product(), as, as.getM_CostType_ID(),
Collection<MCost> costs = MCost.getByCostType(getM_Product(), as, as.getM_CostType_ID(),
getAD_Org_ID(), getM_AttributeSetInstance_ID());
for (MCost cost : costs)
@ -703,13 +697,15 @@ public class MPPOrder extends X_PP_Order implements DocAction
if (m_processMsg != null)
return false;
// TODO: not implemented
m_processMsg = ModelValidationEngine.get().fireDocValidate(this,ModelValidator.TIMING_AFTER_VOID);
if (m_processMsg != null)
return false;
setProcessed(true);
setDocAction(DOCACTION_None);
return true;
// setProcessed(true);
// setDocAction(DOCACTION_None);
return false;
} // voidIt
public boolean closeIt()
@ -906,12 +902,18 @@ public class MPPOrder extends X_PP_Order implements DocAction
.firstOnly();
}
private MPPOrderWorkflow m_PP_Order_Workflow = null;
public MPPOrderWorkflow getMPPOrderWorkflow()
{
final String whereClause = MPPOrderWorkflow.COLUMNNAME_PP_Order_ID+"=?";
return new Query(getCtx(), MPPOrderWorkflow.Table_Name, whereClause, get_TrxName())
.setParameters(new Object[]{getPP_Order_ID()})
.firstOnly();
if (m_PP_Order_Workflow != null)
{
return m_PP_Order_Workflow;
}
final String whereClause = MPPOrderWorkflow.COLUMNNAME_PP_Order_ID+"=?";
m_PP_Order_Workflow = new Query(getCtx(), MPPOrderWorkflow.Table_Name, whereClause, get_TrxName())
.setParameters(new Object[]{getPP_Order_ID()})
.firstOnly();
return m_PP_Order_Workflow;
}
/**
@ -1055,7 +1057,7 @@ public class MPPOrder extends X_PP_Order implements DocAction
MPPCostCollector.COSTCOLLECTORTYPE_MaterialReceipt, //Production "+"
movementDate, //MovementDate
qtyToDeliver, qtyScrap, qtyReject, //qty,scrap,reject
0,0,
0,0, //durationSetup,duration
trxName);
}

View File

@ -24,7 +24,6 @@ import java.util.List;
import java.util.logging.Level;
import org.compiere.model.MAcctSchema;
import org.compiere.model.MClient;
import org.compiere.model.MCost;
import org.compiere.model.MCostElement;
import org.compiere.model.MProduct;
@ -60,12 +59,9 @@ public class RollupWorkflow extends SvrProcess
private int p_M_Product_Category_ID = 0;
/* Costing Method */
private String p_ConstingMethod = MCostElement.COSTINGMETHOD_StandardCosting;
private MAcctSchema m_as = null;
/**
* Prepare - e.g., get Parameters.
*/
protected void prepare()
{
for (ProcessInfoParameter para : getParameter())
@ -94,23 +90,18 @@ public class RollupWorkflow extends SvrProcess
}
} // prepare
/**
* Perform process.
* @return Message (text with variables)
* @throws Exception if not successful
*/
protected String doIt() throws Exception
{
List<Object> params = new ArrayList<Object>();
StringBuffer whereClause = new StringBuffer("AD_Client_ID=?");
params.add(getAD_Client_ID());
whereClause.append(" AND ").append(MProduct.COLUMNNAME_ProductType).append("=?");
params.add(MProduct.PRODUCTTYPE_Item);
whereClause.append(" AND ").append(MProduct.COLUMNNAME_IsBOM).append("=?");
params.add(true);
if (p_M_Product_ID > 0)
{
whereClause.append(" AND ").append(MProduct.COLUMNNAME_M_Product_ID).append("=?");
@ -128,7 +119,8 @@ public class RollupWorkflow extends SvrProcess
.list();
for (MProduct product : products)
{
{
log.info("Product: "+product);
MPPProductPlanning pp = MPPProductPlanning.find( getCtx(), p_AD_Org_ID , 0, 0, product.get_ID(), get_TrxName());
int AD_Workflow_ID = 0;
if (pp != null)
@ -143,16 +135,17 @@ public class RollupWorkflow extends SvrProcess
{
continue;
}
MWorkflow workflow = MWorkflow.get(getCtx(), AD_Workflow_ID);
log.info("Workflow: "+workflow);
workflow.setCost(Env.ZERO);
MWFNode[] nodes = workflow.getNodes(false, getAD_Client_ID());
for (MWFNode node : nodes)
{
node.setCost(Env.ZERO);
}
BigDecimal labor = Env.ZERO;
BigDecimal burden = Env.ZERO;
Collection<MCostElement> elements = MCostElement.getByCostingMethod(getCtx(), p_ConstingMethod);
@ -167,29 +160,31 @@ public class RollupWorkflow extends SvrProcess
for (MCost cost : costs)
{
if(MCostElement.COSTELEMENTTYPE_Resource.equals(element.getCostElementType())
|| MCostElement.COSTELEMENTTYPE_BurdenMOverhead.equals(element.getCostElementType()))
|| MCostElement.COSTELEMENTTYPE_BurdenMOverhead.equals(element.getCostElementType()))
{
for (MWFNode node : nodes)
{
{
BigDecimal nodeCost = Env.ZERO;
// check if element cost is of type Labor
if (MCostElement.COSTELEMENTTYPE_Resource.equals(element.getCostElementType()))
{
nodeCost = node.getCostForCostElementType(MCostElement.COSTELEMENTTYPE_Resource ,p_C_AcctSchema_ID, p_M_CostType_ID, p_AD_Org_ID, node.getSetupTime(), node.getDuration());
labor = labor.add(nodeCost);
log.info("Node="+node+" : Labor : nodeCost="+nodeCost+" => Cost="+labor);
}
else if (MCostElement.COSTELEMENTTYPE_BurdenMOverhead.equals(element.getCostElementType()))
{
nodeCost = node.getCostForCostElementType(MCostElement.COSTELEMENTTYPE_BurdenMOverhead ,p_C_AcctSchema_ID, p_M_CostType_ID, p_AD_Org_ID, node.getSetupTime(), node.getDuration());
burden = burden.add(nodeCost);
log.info("Node="+node+" : Burden : nodeCost="+nodeCost+" => Cost="+burden);
}
if(nodeCost.signum() != 0)
{
node.setCost(node.getCost().add(nodeCost));
node.saveEx();
}
} // Node
}
// check if element cost is of type Labor
if (MCostElement.COSTELEMENTTYPE_Resource.equals(element.getCostElementType()))
{
@ -203,15 +198,13 @@ public class RollupWorkflow extends SvrProcess
cost.setCurrentCostPrice(burden);
cost.saveEx();
}
}
} // MCost
}
} // MCost
} // Cost Elements
workflow.setCost(labor.add(burden));
workflow.saveEx(get_TrxName());
}
return "@OK@";
}
}
}