/// <summary> /// Create New Standard Costs /// </summary> /// <param name="as1">accounting schema</param> private void CreateNew(MAcctSchema as1) { if (!as1.GetCostingLevel().Equals(MAcctSchema.COSTINGLEVEL_Client)) { String txt = "Costing Level prevents creating new Costing records for " + as1.GetName(); log.Warning(txt); AddLog(0, null, null, txt); return; } String sql = "SELECT * FROM M_Product p " + "WHERE NOT EXISTS (SELECT * FROM M_Cost c WHERE c.M_Product_ID=p.M_Product_ID" + " AND c.M_CostType_ID=" + as1.GetM_CostType_ID() + " AND c.C_AcctSchema_ID=" + as1.GetC_AcctSchema_ID() + " AND c.M_CostElement_ID=" + _ce.GetM_CostElement_ID() + " AND c.M_AttributeSetInstance_ID=0) " + "AND AD_Client_ID=" + as1.GetAD_Client_ID(); if (_M_Product_Category_ID != 0) { sql += " AND M_Product_Category_ID=" + _M_Product_Category_ID; } int counter = 0; DataTable dt = null; IDataReader idr = null; try { idr = DB.ExecuteReader(sql, null, null); dt = new DataTable(); dt.Load(idr); idr.Close(); foreach (DataRow dr in dt.Rows) { if (CreateNew(new MProduct(GetCtx(), dr, null), as1)) { counter++; } } } catch (Exception e) { if (idr != null) { idr.Close(); } log.Log(Level.SEVERE, sql, e); } finally { dt = null; if (idr != null) { idr.Close(); } } log.Info("#" + counter); AddLog(0, null, new Decimal(counter), "Created for " + as1.GetName()); }
/// <summary> /// Create Facts (the accounting logic) for /// MMM. /// <pre> /// Movement /// Inventory DR CR /// InventoryTo DR CR /// </pre> /// </summary> /// <param name="as1"></param> /// <returns>Fact</returns> public override List <Fact> CreateFacts(MAcctSchema as1) { // create Fact Header Fact fact = new Fact(this, as1, Fact.POST_Actual); SetC_Currency_ID(as1.GetC_Currency_ID()); // Line pointers FactLine dr = null; FactLine cr = null; for (int i = 0; i < _lines.Length; i++) { DocLine line = _lines[i]; Decimal costs = line.GetProductCosts(as1, line.GetAD_Org_ID(), false); // ** Inventory DR CR dr = fact.CreateLine(line, line.GetAccount(ProductCost.ACCTTYPE_P_Asset, as1), as1.GetC_Currency_ID(), Decimal.Negate(costs)); // from (-) CR if (dr == null) { continue; } dr.SetM_Locator_ID(line.GetM_Locator_ID()); dr.SetQty(Decimal.Negate(line.GetQty().Value)); // outgoing // ** InventoryTo DR CR cr = fact.CreateLine(line, line.GetAccount(ProductCost.ACCTTYPE_P_Asset, as1), as1.GetC_Currency_ID(), costs); // to (+) DR if (cr == null) { continue; } cr.SetM_Locator_ID(line.GetM_LocatorTo_ID()); cr.SetQty(line.GetQty()); // Only for between-org movements if (dr.GetAD_Org_ID() != cr.GetAD_Org_ID()) { String costingLevel = as1.GetCostingLevel(); MProductCategoryAcct pca = MProductCategoryAcct.Get(GetCtx(), line.GetProduct().GetM_Product_Category_ID(), as1.GetC_AcctSchema_ID(), GetTrx()); if (pca.GetCostingLevel() != null) { costingLevel = pca.GetCostingLevel(); } if (!MAcctSchema.COSTINGLEVEL_Organization.Equals(costingLevel)) { continue; } // String description = line.GetDescription(); if (description == null) { description = ""; } if (!IsPosted()) { // Cost Detail From MCostDetail.CreateMovement(as1, dr.GetAD_Org_ID(), // locator org line.GetM_Product_ID(), line.GetM_AttributeSetInstance_ID(), line.Get_ID(), 0, Decimal.Negate(costs), Decimal.Negate(line.GetQty().Value), true, description + "(|->)", GetTrx(), GetRectifyingProcess()); // Cost Detail To MCostDetail.CreateMovement(as1, cr.GetAD_Org_ID(), // locator org line.GetM_Product_ID(), line.GetM_AttributeSetInstance_ID(), line.Get_ID(), 0, costs, line.GetQty().Value, false, description + "(|<-)", GetTrx(), GetRectifyingProcess()); } } } // List <Fact> facts = new List <Fact>(); facts.Add(fact); return(facts); }
public override List <Fact> CreateFacts(MAcctSchema as1) { List <Fact> facts = new List <Fact>(); MProductCategoryAcct pca = null; String costingMethodOfSchema = as1.GetCostingMethod(); String costingLevelOfSchema = as1.GetCostingLevel(); // Get the costing method and the costing level of the product Category for the current accounting schema. if (mpc != null) { pca = MProductCategoryAcct.Get(GetCtx(), mpc.GetM_Product_Category_ID(), as1.GetC_AcctSchema_ID(), null); if (pca.GetCostingMethod() != null) { costingMethod = pca.GetCostingMethod(); } else { costingMethod = costingMethodOfSchema; } if (pca.GetCostingLevel() != null) { costingLevel = pca.GetCostingLevel(); } else { costingLevel = costingLevelOfSchema; } // proceed only if the costing method is standard if (!costingMethod.Equals(X_C_AcctSchema.COSTINGMETHOD_StandardCosting)) { return(facts); } } Fact fact = new Fact(this, as1, Fact.POST_Actual); FactLine dr = null; FactLine cr = null; for (int i = 0; i < _lines.Length; i++) { DocLine line = _lines[i]; if (mpc == null) { pca = MProductCategoryAcct.Get(GetCtx(), MProduct.Get(GetCtx(), line.GetM_Product_ID()). GetM_Product_Category_ID(), as1.GetC_AcctSchema_ID(), null); if (pca.GetCostingMethod() != null) { costingMethod = pca.GetCostingMethod(); } else { costingMethod = costingMethodOfSchema; } if (pca.GetCostingLevel() != null) { costingLevel = pca.GetCostingLevel(); } else { costingLevel = costingLevelOfSchema; } // proceed only if the costing method is standard if (!costingMethod.Equals(X_C_AcctSchema.COSTINGMETHOD_StandardCosting)) { return(facts); } } Decimal currentCost = Env.ZERO; Decimal oldCost = Env.ZERO; Decimal Qty = Env.ZERO; String sql = " SELECT Sum(CurrentCostPrice), Sum(LastCostPrice), Sum(CurrentQty)" + " FROM M_Cost WHERE M_Product_ID = " + line.GetM_Product_ID() + " AND C_AcctSchema_ID = " + as1.GetC_AcctSchema_ID() + " AND M_CostElement_ID = " + m_ce.GetM_CostElement_ID() + " AND M_CostType_ID = " + as1.GetM_CostType_ID() + " AND AD_Client_ID = " + GetAD_Client_ID(); if (costingLevel.Equals(X_C_AcctSchema.COSTINGLEVEL_Client)) { sql += " AND AD_Org_ID = 0" + " AND M_AttributeSetInstance_ID = 0"; } else if (costingLevel.Equals(X_C_AcctSchema.COSTINGLEVEL_Organization)) { sql += " AND M_AttributeSetInstance_ID = 0"; } else if (costingLevel.Equals(X_C_AcctSchema.COSTINGLEVEL_BatchLot)) { sql += " AND AD_Org_ID = 0"; } IDataReader idr = null; try { idr = DB.ExecuteReader(sql, null, GetTrx()); while (idr.Read()) { currentCost = Util.GetValueOfDecimal(idr[0]); oldCost = Util.GetValueOfDecimal(idr[1]); Qty = Util.GetValueOfDecimal(idr[2]); line.SetQty(Qty, costupdate.IsSOTrx()); Decimal PriceCost = Decimal.Subtract(currentCost, oldCost); Decimal amt = Env.ZERO; if (Env.Signum(PriceCost) != 0) { amt = Decimal.Multiply(Qty, PriceCost); } line.SetAmount(amt); if (Env.Signum(amt) == 0) { continue; } MAccount db_acct, cr_acct; /* Decide the Credit and Debit Accounts */ if (Env.Signum(amt) == 1) { db_acct = line.GetAccount(ProductCost.ACCTTYPE_P_Asset, as1); cr_acct = line.GetAccount(ProductCost.ACCTTYPE_P_CostAdjustment, as1); } else { cr_acct = line.GetAccount(ProductCost.ACCTTYPE_P_Asset, as1); db_acct = line.GetAccount(ProductCost.ACCTTYPE_P_CostAdjustment, as1); } /* Create Credit and Debit lines*/ dr = fact.CreateLine(line, db_acct, as1.GetC_Currency_ID(), Math.Abs(amt), null); if (dr == null) { log.SaveError("NoProductCosts", "No Product Costs"); return(null); } cr = fact.CreateLine(line, cr_acct, as1.GetC_Currency_ID(), null, Math.Abs(amt)); if (cr == null) { log.SaveError("NoProductCosts", "No Product Costs"); return(null); } } idr.Close(); } catch (Exception e) { log.Log(Level.SEVERE, sql, e); } finally { if (idr != null) { idr.Close(); idr = null; } } } facts.Add(fact); return(facts); }