diff --git a/base/src/org/compiere/model/MAcctSchema.java b/base/src/org/compiere/model/MAcctSchema.java index 85582fb840..1787aeb60a 100644 --- a/base/src/org/compiere/model/MAcctSchema.java +++ b/base/src/org/compiere/model/MAcctSchema.java @@ -18,7 +18,6 @@ package org.compiere.model; import java.sql.ResultSet; import java.util.ArrayList; -import java.util.Collection; import java.util.List; import java.util.Properties; @@ -67,8 +66,6 @@ public class MAcctSchema extends X_C_AcctSchema return retValue; } // get - - /** * Get AccountSchema of Client * @param ctx context @@ -76,32 +73,43 @@ 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 - if (s_schema.containsKey(AD_Client_ID)) - return (MAcctSchema[])s_schema.get(AD_Client_ID); + Integer key = new Integer(AD_Client_ID); + if (s_schema.containsKey(key)) + return (MAcctSchema[])s_schema.get(key); // Create New ArrayList list = new ArrayList(); - MClientInfo info = MClientInfo.get(ctx, AD_Client_ID); - MAcctSchema as = MAcctSchema.get (ctx, info.getC_AcctSchema1_ID()); - //if (as.get_ID() != 0 && trxName == null) + MClientInfo info = MClientInfo.get(ctx, AD_Client_ID, trxName); + MAcctSchema as = MAcctSchema.get (ctx, info.getC_AcctSchema1_ID(), trxName); if (as.get_ID() != 0) list.add(as); - ArrayList params = new ArrayList(); - 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)"; - + 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"); if (AD_Client_ID != 0) { - whereClause += "AND AD_Client_ID=?"; + whereClause += " AND AD_Client_ID=?"; params.add(AD_Client_ID); } - Collection ass = new Query(ctx, MAcctSchema.Table_Name,whereClause, null) - .setApplyAccessFilter(true) + List ass = new Query(ctx, MAcctSchema.Table_Name,whereClause,trxName) .setParameters(params) .setOrderBy(MAcctSchema.COLUMNNAME_C_AcctSchema_ID) .list(); @@ -117,7 +125,7 @@ public class MAcctSchema extends X_C_AcctSchema // Save MAcctSchema[] retValue = new MAcctSchema [list.size()]; list.toArray(retValue); - s_schema.put(AD_Client_ID, retValue); + s_schema.put(key, retValue); return retValue; } // getClientAcctSchema diff --git a/base/src/org/compiere/model/MCost.java b/base/src/org/compiere/model/MCost.java index c3ca57e96b..de827d4c82 100644 --- a/base/src/org/compiere/model/MCost.java +++ b/base/src/org/compiere/model/MCost.java @@ -761,14 +761,82 @@ public class MCost extends X_M_Cost * @param product product **/ protected static void create (MProduct product) + { + s_log.config(product.getName()); + + // Cost Elements + Collection ces = MCostElement.getCostElementsWithCostingMethods(product); + + MAcctSchema[] mass = MAcctSchema.getClientAcctSchema(product.getCtx(), + product.getAD_Client_ID(), product.get_TrxName()); + MOrg[] orgs = null; + + int M_ASI_ID = 0; // No Attribute + for (MAcctSchema as : mass) + { + String cl = product.getCostingLevel(as); + // Create Std Costing + if (MAcctSchema.COSTINGLEVEL_Client.equals(cl)) + { + for(MCostElement ce : ces) + { + 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 (MOrg o : orgs) + { + for(MCostElement ce : ces) + { + 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 + + /** + * Delete standard Costing records for Product + * @param product product + **/ + protected static void delete (MProduct product) { s_log.config(product.getName()); // Cost Elements - Collection ces = MCostElement.getCostElementToCostingMethods(product); - for(MCostElement ce : ces) - { + Collection ces = MCostElement.getCostElementsWithCostingMethods(product); + MAcctSchema[] mass = MAcctSchema.getClientAcctSchema(product.getCtx(), - product.getAD_Client_ID()); + product.getAD_Client_ID(), product.get_TrxName()); MOrg[] orgs = null; int M_ASI_ID = 0; // No Attribute @@ -778,16 +846,12 @@ public class MCost extends X_M_Cost // 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()); + for(MCostElement ce : ces) + { + MCost cost = MCost.get (product, M_ASI_ID, + as, 0, ce.getM_CostElement_ID()); + if(cost != null) + cost.deleteEx(true); } } else if (MAcctSchema.COSTINGLEVEL_Organization.equals(cl)) @@ -796,18 +860,12 @@ public class MCost extends X_M_Cost 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(MCostElement ce : ces) + { + MCost cost = MCost.get (product, M_ASI_ID, + as, o.getAD_Org_ID(), ce.getM_CostElement_ID()); + if(cost != null) + cost.deleteEx(true); } } // for all orgs } @@ -817,10 +875,8 @@ public class MCost extends X_M_Cost + " - Costing Level on Batch/Lot"); } } // accounting schema loop - } } // create - /************************************************************************** * Calculate Average Invoice from Trx * @param product product @@ -1603,7 +1659,9 @@ public class MCost extends X_M_Cost */ protected boolean beforeSave (boolean newRecord) { - MCostElement ce = getCostElement(); + //The method getCostElement() not should be cached because is a transaction + //MCostElement ce = getCostElement(); + MCostElement ce = (MCostElement)getM_CostElement(); // Check if data entry makes sense if (m_manual) { diff --git a/base/src/org/compiere/model/MCostDetail.java b/base/src/org/compiere/model/MCostDetail.java index 1ace019bbc..c54f75228a 100644 --- a/base/src/org/compiere/model/MCostDetail.java +++ b/base/src/org/compiere/model/MCostDetail.java @@ -781,7 +781,7 @@ public class MCostDetail extends X_M_CostDetail // Create Material Cost elements if (getM_CostElement_ID() == 0) { - MCostElement[] ces = MCostElement.getMaterialCostingMethods(this); + MCostElement[] ces = MCostElement.getMaterialWithCostingMethods(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.getMaterialCostingMethods(this); + MCostElement[] ces = MCostElement.getMaterialWithCostingMethods(this); for (int i = 0; i < ces.length; i++) { MCostElement ce = ces[i]; diff --git a/base/src/org/compiere/model/MCostElement.java b/base/src/org/compiere/model/MCostElement.java index 1f9b31cbe3..a5f5a6fe0c 100644 --- a/base/src/org/compiere/model/MCostElement.java +++ b/base/src/org/compiere/model/MCostElement.java @@ -191,11 +191,12 @@ public class MCostElement extends X_M_CostElement * @param po parent * @return cost element array */ - public static Collection getCostElementToCostingMethods (PO po) + public static Collection getCostElementsWithCostingMethods (PO po) { final String whereClause = "CostingMethod IS NOT NULL"; return new Query(po.getCtx(),MCostElement.Table_Name,whereClause,po.get_TrxName()) .setOnlyActiveRecords(true) + .setClient_ID() .list(); } // getCostElementCostingMethod @@ -204,7 +205,7 @@ public class MCostElement extends X_M_CostElement * @param po parent * @return cost element array */ - public static MCostElement[] getMaterialCostingMethods (PO po) + public static MCostElement[] getMaterialWithCostingMethods (PO po) { ArrayList list = new ArrayList(); String sql = "SELECT * FROM M_CostElement " @@ -384,12 +385,18 @@ public class MCostElement extends X_M_CostElement protected boolean beforeSave (boolean newRecord) { // Check Unique Costing Method - if (COSTELEMENTTYPE_Material.equals(getCostElementType()) + if ( + ( COSTELEMENTTYPE_Material.equals(getCostElementType()) + || COSTELEMENTTYPE_Resource.equals(getCostElementType()) + || COSTELEMENTTYPE_BurdenMOverhead.equals(getCostElementType()) + || COSTELEMENTTYPE_Overhead.equals(getCostElementType()) + || COSTELEMENTTYPE_OutsideProcessing.equals(getCostElementType()) + ) && (newRecord || is_ValueChanged("CostingMethod"))) { String sql = "SELECT COALESCE(MAX(M_CostElement_ID),0) FROM M_CostElement " - + "WHERE AD_Client_ID=? AND CostingMethod=?"; - int id = DB.getSQLValue(get_TrxName(), sql, getAD_Client_ID(), getCostingMethod()); + + "WHERE AD_Client_ID=? AND CostingMethod=? AND CostElementType=?"; + int id = DB.getSQLValue(get_TrxName(), sql, getAD_Client_ID(), getCostingMethod() , getCostElementType()); if (id > 0 && id != get_ID()) { log.saveError("AlreadyExists", Msg.getElement(getCtx(), "CostingMethod")); diff --git a/base/src/org/compiere/model/MProduct.java b/base/src/org/compiere/model/MProduct.java index b2347bcf7d..61c3bd9a47 100644 --- a/base/src/org/compiere/model/MProduct.java +++ b/base/src/org/compiere/model/MProduct.java @@ -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()); + MAcctSchema[] mass = MAcctSchema.getClientAcctSchema(getCtx(), getAD_Client_ID(), get_TrxName()); for (int i = 0; i < mass.length; i++) { // Old @@ -673,12 +673,14 @@ public class MProduct extends X_M_Product for (int i = 0; i < costings.length; i++) costings[i].delete(true, get_TrxName()); + MCost.delete(this); + // [ 1674225 ] Delete Product: Costing deletion error - MAcctSchema[] mass = MAcctSchema.getClientAcctSchema(getCtx(),getAD_Client_ID()); + /*MAcctSchema[] mass = MAcctSchema.getClientAcctSchema(getCtx(),getAD_Client_ID(), get_TrxName()); for(int i=0; i