The MCost record do not is create with a new product.

http://sourceforge.net/tracker2/?func=detail&atid=879332&aid=2496472&group_id=176962
This commit is contained in:
vpj-cd 2009-01-13 17:46:59 +00:00
parent 67d8427774
commit 16eec660c3
5 changed files with 242 additions and 148 deletions

View File

@ -18,6 +18,7 @@ package org.compiere.model;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Properties;
@ -66,6 +67,8 @@ public class MAcctSchema extends X_C_AcctSchema
return retValue;
} // get
/**
* Get AccountSchema of Client
* @param ctx context
@ -73,43 +76,32 @@ public class MAcctSchema extends X_C_AcctSchema
* @return Array of AcctSchema of Client
*/
public static MAcctSchema[] getClientAcctSchema (Properties ctx, int AD_Client_ID)
{
return getClientAcctSchema(ctx, AD_Client_ID, null);
} // getClientAcctSchema
/**
* Get AccountSchema of Client
* @param ctx context
* @param AD_Client_ID client or 0 for all
* @param trxName optional trx
* @return Array of AcctSchema of Client
*/
public static MAcctSchema[] getClientAcctSchema (Properties ctx, int AD_Client_ID, String trxName)
{
// Check Cache
Integer key = new Integer(AD_Client_ID);
if (s_schema.containsKey(key))
return (MAcctSchema[])s_schema.get(key);
if (s_schema.containsKey(AD_Client_ID))
return (MAcctSchema[])s_schema.get(AD_Client_ID);
// Create New
ArrayList<MAcctSchema> list = new ArrayList<MAcctSchema>();
MClientInfo info = MClientInfo.get(ctx, AD_Client_ID, trxName);
MAcctSchema as = MAcctSchema.get (ctx, info.getC_AcctSchema1_ID(), trxName);
if (as.get_ID() != 0 && trxName == null)
MClientInfo info = MClientInfo.get(ctx, AD_Client_ID);
MAcctSchema as = MAcctSchema.get (ctx, info.getC_AcctSchema1_ID());
//if (as.get_ID() != 0 && trxName == null)
if (as.get_ID() != 0)
list.add(as);
ArrayList<Object> params = new ArrayList<Object>();
String whereClause = "IsActive=? "
+ " AND EXISTS (SELECT * FROM C_AcctSchema_GL gl WHERE C_AcctSchema.C_AcctSchema_ID=gl.C_AcctSchema_ID)"
+ " AND EXISTS (SELECT * FROM C_AcctSchema_Default d WHERE C_AcctSchema.C_AcctSchema_ID=d.C_AcctSchema_ID)";
params.add("Y");
String whereClause = "EXISTS (SELECT 1 FROM C_AcctSchema_GL gl WHERE C_AcctSchema.C_AcctSchema_ID=gl.C_AcctSchema_ID)"
+ " AND EXISTS (SELECT 1 FROM C_AcctSchema_Default d WHERE C_AcctSchema.C_AcctSchema_ID=d.C_AcctSchema_ID)";
if (AD_Client_ID != 0)
{
whereClause += " AND AD_Client_ID=?";
whereClause += "AND AD_Client_ID=?";
params.add(AD_Client_ID);
}
List <MAcctSchema> ass = new Query(ctx, MAcctSchema.Table_Name,whereClause,trxName)
Collection <MAcctSchema> ass = new Query(ctx, MAcctSchema.Table_Name,whereClause, null)
.setApplyAccessFilter(true)
.setParameters(params)
.setOrderBy(MAcctSchema.COLUMNNAME_C_AcctSchema_ID)
.list();
@ -118,15 +110,14 @@ public class MAcctSchema extends X_C_AcctSchema
{
if (acctschema.get_ID() != info.getC_AcctSchema1_ID()) // already in list
{
if (acctschema.get_ID() != 0 && trxName == null)
if (acctschema.get_ID() != 0)
list.add(acctschema);
}
}
// Save
MAcctSchema[] retValue = new MAcctSchema [list.size()];
list.toArray(retValue);
if (trxName == null)
s_schema.put(key, retValue);
s_schema.put(AD_Client_ID, retValue);
return retValue;
} // getClientAcctSchema

View File

@ -51,6 +51,106 @@ 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
* @param product Product
* @param as Account Schema
* @param AD_Org_ID Organization ID
* @param M_AttributeSetInstance_ID Attribute Set Instance ID
* @param CostingMethod Costing Method
* @param CostElementType Cost Element Type
* @param Qty Quantity
* @return Get the the Total Cost for this Cost Element Type and Costing Method
*/
public static BigDecimal getCostByCostingMethod (MProduct product, MAcctSchema as,
int AD_Org_ID, int M_AttributeSetInstance_ID,
String CostingMethod, 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;
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.CostingMethod=? 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(),
CostingMethod, CostElementType})
.list();
for(MCost cost : costs)
{
m_cost = cost.getCurrentCostPrice().add(cost.getCurrentCostPriceLL());
}
return m_cost.multiply(Qty);
} // get
/**
* Get MCost for this Cost Element Type and Costing Method
* @param product Product
* @param as Account Schema
* @param AD_Org_ID Organization ID
* @param M_AttributeSetInstance_ID Attribute Set Instance ID
* @param CostingMethod Costing Method
* @param CostElementType Cost Element Type
* @return Get MCost Collection for this Cost Element Type and Costing Method
*/
public static Collection<MCost> getByCostingMethod (MProduct product, MAcctSchema as,
int AD_Org_ID, int M_AttributeSetInstance_ID,
String CostingMethod, String CostElementType)
{
//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;
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.CostingMethod=? AND ce.CostElementType=?)";
return 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(),
CostingMethod, CostElementType}).list();
} // get
/**
* Retrieve/Calculate Current Cost Price
@ -655,82 +755,69 @@ public class MCost extends X_M_Cost
}
} // create
/**
* Create standard Costing records for Product
* @param product product
*/
**/
protected static void create (MProduct product)
{
s_log.config(product.getName());
// Cost Elements
MCostElement[] ces = MCostElement.getCostingMethods(product);
MCostElement ce = null;
for (int i = 0; i < ces.length; i++)
{
if (MCostElement.COSTINGMETHOD_StandardCosting.equals(ces[i].getCostingMethod()))
Collection <MCostElement> ces = MCostElement.getCostElementToCostingMethods(product);
for(MCostElement ce : ces)
{
MAcctSchema[] mass = MAcctSchema.getClientAcctSchema(product.getCtx(),
product.getAD_Client_ID());
MOrg[] orgs = null;
int M_ASI_ID = 0; // No Attribute
for (MAcctSchema as : mass)
{
ce = ces[i];
break;
}
}
if (ce == null)
{
s_log.fine("No Standard Costing in System");
return;
}
MAcctSchema[] mass = MAcctSchema.getClientAcctSchema(product.getCtx(),
product.getAD_Client_ID(), product.get_TrxName());
MOrg[] orgs = null;
int M_ASI_ID = 0; // No Attribute
for (int i = 0; i < mass.length; i++)
{
MAcctSchema as = mass[i];
String cl = product.getCostingLevel(as);
// Create Std Costing
if (MAcctSchema.COSTINGLEVEL_Client.equals(cl))
{
MCost cost = MCost.get (product, M_ASI_ID,
as, 0, ce.getM_CostElement_ID());
if (cost.is_new())
{
if (cost.save())
s_log.config("Std.Cost for " + product.getName()
+ " - " + as.getName());
else
s_log.warning("Not created: Std.Cost for " + product.getName()
+ " - " + as.getName());
}
}
else if (MAcctSchema.COSTINGLEVEL_Organization.equals(cl))
{
if (orgs == null)
orgs = MOrg.getOfClient(product);
for (int o = 0; o < orgs.length; o++)
String cl = product.getCostingLevel(as);
// Create Std Costing
if (MAcctSchema.COSTINGLEVEL_Client.equals(cl))
{
MCost cost = MCost.get (product, M_ASI_ID,
as, orgs[o].getAD_Org_ID(), ce.getM_CostElement_ID());
as, 0, ce.getM_CostElement_ID());
if (cost.is_new())
{
if (cost.save())
s_log.config("Std.Cost for " + product.getName()
+ " - " + orgs[o].getName()
s_log.config("Std.Cost for " + product.getName()
+ " - " + as.getName());
else
s_log.warning("Not created: Std.Cost for " + product.getName()
+ " - " + orgs[o].getName()
+ " - " + as.getName());
}
} // for all orgs
}
else
{
s_log.warning("Not created: Std.Cost for " + product.getName()
+ " - Costing Level on Batch/Lot");
}
} // accounting schema loop
}
else if (MAcctSchema.COSTINGLEVEL_Organization.equals(cl))
{
if (orgs == null)
orgs = MOrg.getOfClient(product);
for (MOrg o : orgs)
{
MCost cost = MCost.get (product, M_ASI_ID,
as, o.getAD_Org_ID(), ce.getM_CostElement_ID());
if (cost.is_new())
{
if (cost.save())
s_log.config("Std.Cost for " + product.getName()
+ " - " + o.getName()
+ " - " + as.getName());
else
s_log.warning("Not created: Std.Cost for " + product.getName()
+ " - " + o.getName()
+ " - " + as.getName());
}
} // for all orgs
}
else
{
s_log.warning("Not created: Std.Cost for " + product.getName()
+ " - Costing Level on Batch/Lot");
}
} // accounting schema loop
}
} // create
@ -1583,61 +1670,6 @@ public class MCost extends X_M_Cost
} // beforeDelete
/**
* Get the the Total Cost for this Cost Element Type and Costing Method
* @param product Product
* @param as Account Schema
* @param AD_Org_ID Organization ID
* @param M_AttributeSetInstance_ID Attribute Set Instance ID
* @param CostingMethod Costing Method
* @param CostElementType Cost Element Type
* @param Qty Quantity
* @return Get the the Total Cost for this Cost Element Type and Costing Method
*/
public static BigDecimal getCostByCostingMethod (MProduct product, MAcctSchema as,
int AD_Org_ID, int M_AttributeSetInstance_ID,
String CostingMethod, 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;
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.CostingMethod=? 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(),
CostingMethod, CostElementType})
.list();
for(MCost cost : costs)
{
m_cost = cost.getCurrentCostPrice().add(cost.getCurrentCostPriceLL());
}
return m_cost.multiply(Qty);
} // get
/**
* Test
* @param args ignored

View File

@ -781,7 +781,7 @@ public class MCostDetail extends X_M_CostDetail
// Create Material Cost elements
if (getM_CostElement_ID() == 0)
{
MCostElement[] ces = MCostElement.getCostingMethods(this);
MCostElement[] ces = MCostElement.getMaterialCostingMethods(this);
for (int i = 0; i < ces.length; i++)
{
MCostElement ce = ces[i];
@ -1130,7 +1130,7 @@ public class MCostDetail extends X_M_CostDetail
// Create Material Cost elements
if (getM_CostElement_ID() == 0)
{
MCostElement[] ces = MCostElement.getCostingMethods(this);
MCostElement[] ces = MCostElement.getMaterialCostingMethods(this);
for (int i = 0; i < ces.length; i++)
{
MCostElement ce = ces[i];

View File

@ -19,6 +19,7 @@ package org.compiere.model;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Properties;
import java.util.logging.Level;
@ -85,6 +86,49 @@ public class MCostElement extends X_M_CostElement
if (retValue != null)
return retValue;
if(CostingMethod.equals(MCostElement.COSTINGMETHOD_StandardCosting))
{
retValue = new MCostElement (po.getCtx(), 0, po.get_TrxName());
retValue.setClientOrg(po.getAD_Client_ID(), 0);
String name = MRefList.getListName(po.getCtx(), COSTELEMENTTYPE_AD_Reference_ID, COSTELEMENTTYPE_Resource);
if (name == null || name.length() == 0)
name = CostingMethod;
retValue.setName(name);
retValue.setCostElementType(COSTELEMENTTYPE_Resource);
retValue.setCostingMethod(CostingMethod);
retValue.save();
retValue = new MCostElement (po.getCtx(), 0, po.get_TrxName());
retValue.setClientOrg(po.getAD_Client_ID(), 0);
name = MRefList.getListName(po.getCtx(), COSTELEMENTTYPE_AD_Reference_ID, COSTELEMENTTYPE_BurdenMOverhead);
if (name == null || name.length() == 0)
name = "BurdenMOverhead";
retValue.setName(name);
retValue.setCostElementType(COSTELEMENTTYPE_BurdenMOverhead);
retValue.setCostingMethod(CostingMethod);
retValue.save();
retValue = new MCostElement (po.getCtx(), 0, po.get_TrxName());
retValue.setClientOrg(po.getAD_Client_ID(), 0);
name = MRefList.getListName(po.getCtx(), COSTELEMENTTYPE_AD_Reference_ID, COSTELEMENTTYPE_Overhead);
if (name == null || name.length() == 0)
name = "Overhead";
retValue.setName(name);
retValue.setCostElementType(COSTELEMENTTYPE_Overhead);
retValue.setCostingMethod(CostingMethod);
retValue.save();
retValue = new MCostElement (po.getCtx(), 0, po.get_TrxName());
retValue.setClientOrg(po.getAD_Client_ID(), 0);
name = MRefList.getListName(po.getCtx(), COSTELEMENTTYPE_AD_Reference_ID, COSTELEMENTTYPE_OutsideProcessing);
if (name == null || name.length() == 0)
name = "OutsideProcessing";
retValue.setName(name);
retValue.setCostElementType(COSTELEMENTTYPE_OutsideProcessing);
retValue.setCostingMethod(CostingMethod);
retValue.save();
}
// Create New
retValue = new MCostElement (po.getCtx(), 0, po.get_TrxName());
retValue.setClientOrg(po.getAD_Client_ID(), 0);
@ -95,6 +139,7 @@ public class MCostElement extends X_M_CostElement
retValue.setCostElementType(COSTELEMENTTYPE_Material);
retValue.setCostingMethod(CostingMethod);
retValue.save();
//
return retValue;
} // getMaterialCostElement
@ -141,13 +186,25 @@ public class MCostElement extends X_M_CostElement
return retValue;
} // getMaterialCostElement
/**
* Get active Material Cost Element for client
* @param po parent
* @return cost element array
*/
public static Collection<MCostElement> getCostElementToCostingMethods (PO po)
{
final String whereClause = "CostingMethod IS NOT NULL";
return new Query(po.getCtx(),MCostElement.Table_Name,whereClause,po.get_TrxName())
.setOnlyActiveRecords(true)
.list();
} // getCostElementCostingMethod
/**
* Get active Material Cost Element for client
* @param po parent
* @return cost element array
*/
public static MCostElement[] getCostingMethods (PO po)
public static MCostElement[] getMaterialCostingMethods (PO po)
{
ArrayList<MCostElement> list = new ArrayList<MCostElement>();
String sql = "SELECT * FROM M_CostElement "
@ -267,6 +324,21 @@ public class MCostElement extends X_M_CostElement
MCostElement[] retValue = new MCostElement[list.size()];
list.toArray(retValue);
return retValue;
}
/**
* Get All Cost Elements for current AD_Client_ID
* @param ctx context
* @param trxName transaction
* @return array cost elements
**/
public static Collection getByCostingMethod (Properties ctx, String CostingMethod)
{
final String whereClause = "CostingMethod=?";
return new Query(ctx, Table_Name, whereClause, null)
.setClient_ID()
.setParameters(new Object[]{CostingMethod})
.list();
}
/** Cache */

View File

@ -622,7 +622,7 @@ public class MProduct extends X_M_Product
"p.M_Product_Category_ID=" + getM_Product_Category_ID());
insert_Tree(X_AD_Tree.TREETYPE_Product);
//
MAcctSchema[] mass = MAcctSchema.getClientAcctSchema(getCtx(), getAD_Client_ID(), get_TrxName());
MAcctSchema[] mass = MAcctSchema.getClientAcctSchema(getCtx(), getAD_Client_ID());
for (int i = 0; i < mass.length; i++)
{
// Old
@ -674,12 +674,11 @@ public class MProduct extends X_M_Product
costings[i].delete(true, get_TrxName());
// [ 1674225 ] Delete Product: Costing deletion error
MAcctSchema[] mass = MAcctSchema.getClientAcctSchema(getCtx(),
getAD_Client_ID(), get_TrxName());
MAcctSchema[] mass = MAcctSchema.getClientAcctSchema(getCtx(),getAD_Client_ID());
for(int i=0; i<mass.length; i++)
{
// Get Cost Elements
MCostElement[] ces = MCostElement.getCostingMethods(this);
MCostElement[] ces = MCostElement.getMaterialCostingMethods(this);
MCostElement ce = null;
for(int j=0; j<ces.length; j++)
{
@ -751,7 +750,7 @@ public class MProduct extends X_M_Product
public boolean isASIMandatory(boolean isSOTrx) {
//
// If CostingLevel is BatchLot ASI is always mandatory - check all client acct schemas
MAcctSchema[] mass = MAcctSchema.getClientAcctSchema(getCtx(), getAD_Client_ID(), get_TrxName());
MAcctSchema[] mass = MAcctSchema.getClientAcctSchema(getCtx(), getAD_Client_ID());
for (MAcctSchema as : mass)
{
String cl = getCostingLevel(as);