Esempio n. 1
0
        /// <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());
        }
Esempio n. 2
0
        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);
        }