}       //	setC_Currency_ID

        /// <summary>
        /// set rate
        /// </summary>
        /// <param name="windowNo"></param>
        private void SetRate(int windowNo)
        {
            //  Source info
            int?C_Currency_ID       = GetC_Currency_ID();
            int?C_ConversionType_ID = GetC_ConversionType_ID();

            if (C_Currency_ID == 0 || C_ConversionType_ID == 0)
            {
                return;
            }
            DateTime?DateAcct = GetDateAcct();

            if (DateAcct == null)
            {
                DateAcct = DateTime.Now;// new Timestamp(System.currentTimeMillis());
            }
            //
            int?        C_AcctSchema_ID = GetCtx().GetContextAsInt(windowNo, "C_AcctSchema_ID");
            MAcctSchema ass             = MAcctSchema.Get(GetCtx(), C_AcctSchema_ID.Value);
            int?        AD_Client_ID    = GetAD_Client_ID();
            int?        AD_Org_ID       = GetAD_Org_ID();

            Decimal?CurrencyRate = (Decimal?)MConversionRate.GetRate(C_Currency_ID.Value, ass.GetC_Currency_ID(),
                                                                     DateAcct, C_ConversionType_ID.Value, AD_Client_ID.Value, AD_Org_ID.Value);

            log.Fine("rate = " + CurrencyRate);
            if (CurrencyRate == null)
            {
                CurrencyRate = Env.ZERO;
            }
            SetCurrencyRate(CurrencyRate);
            SetAmt(windowNo);
        }       //	setRate
        }       //	setC_Currency_ID

        /// <summary>
        ///	Set Rate
        /// </summary>
        private void SetRate()
        {
            //  Source info
            int C_Currency_ID       = GetC_Currency_ID();
            int C_ConversionType_ID = GetC_ConversionType_ID();

            if (C_Currency_ID == 0 || C_ConversionType_ID == 0)
            {
                return;
            }
            DateTime?DateAcct = GetDateAcct();

            if (DateAcct == null)
            {
                DateAcct = DateTime.Now;// new Timestamp(System.currentTimeMillis());
            }
            //
            int         C_AcctSchema_ID = GetC_AcctSchema_ID();
            MAcctSchema a            = MAcctSchema.Get(GetCtx(), C_AcctSchema_ID);
            int         AD_Client_ID = GetAD_Client_ID();
            int         AD_Org_ID    = GetAD_Org_ID();

            Decimal?CurrencyRate = (Decimal?)MConversionRate.GetRate(C_Currency_ID, a.GetC_Currency_ID(),
                                                                     DateAcct, C_ConversionType_ID, AD_Client_ID, AD_Org_ID);

            log.Fine("rate = " + CurrencyRate);
            //if (CurrencyRate.Value == null)
            //{
            //    CurrencyRate = Env.ZERO;
            //}
            SetCurrencyRate(CurrencyRate.Value);
        }       //	setRate
Exemple #3
0
        /// <summary>
        /// Is used to check costing method belongs to PO costing method  like (Average PO, Weighted Average PO or Last PO)
        /// Firts we check costing method on Product category, if not found then we will check on Primary Accounting Schema
        /// </summary>
        /// <param name="ctx">current context</param>
        /// <param name="AD_Client_ID">Client reference</param>
        /// <param name="M_Product_ID">Product whom costing method is to be determine</param>
        /// <param name="trxName">Transaction</param>
        /// <returns>True/False</returns>
        public static bool IsPOCostingmethod(Ctx ctx, int AD_Client_ID, int M_Product_ID, Trx trxName)
        {
            MProductCategory pc = null;
            bool             isPOcostingMethod = false;
            string           costingMethod     = null;
            MClient          client            = MClient.Get(ctx, AD_Client_ID);
            MProduct         product           = MProduct.Get(ctx, M_Product_ID);

            if (product != null)
            {
                pc = MProductCategory.Get(product.GetCtx(), product.GetM_Product_Category_ID());
                if (pc != null)
                {
                    // check costing method from product category
                    costingMethod = pc.GetCostingMethod();
                    if (costingMethod == "C")
                    {
                        costingMethod = Util.GetValueOfString(DB.ExecuteScalar(@"SELECT costingmethod FROM M_CostElement WHERE M_CostElement_ID IN 
                                        (SELECT CAST(M_Ref_CostElement AS INTEGER) FROM M_CostElementLine WHERE M_CostElement_ID=" + pc.GetM_CostElement_ID() + @" )
                                        AND CostingMethod IS NOT NULL", null, trxName));
                    }
                }
                if (String.IsNullOrEmpty(costingMethod))
                {
                    // check costing method against primary accounting schema
                    MClientInfo clientInfo = MClientInfo.Get(ctx, AD_Client_ID);
                    MAcctSchema actSchema  = MAcctSchema.Get(ctx, clientInfo.GetC_AcctSchema1_ID());
                    if (actSchema != null)
                    {
                        costingMethod = actSchema.GetCostingMethod();
                        if (costingMethod == "C")
                        {
                            costingMethod = Util.GetValueOfString(DB.ExecuteScalar(@"SELECT costingmethod FROM M_CostElement WHERE M_CostElement_ID IN 
                                        (SELECT CAST(M_Ref_CostElement AS INTEGER) FROM M_CostElementLine WHERE M_CostElement_ID=" + actSchema.GetM_CostElement_ID() + @" )
                                        AND CostingMethod IS NOT NULL", null, trxName));
                        }
                    }
                }
            }
            if (costingMethod.Equals(COSTINGMETHOD_WeightedAveragePO) ||
                costingMethod.Equals(COSTINGMETHOD_AveragePO) ||
                costingMethod.Equals(COSTINGMETHOD_LastPOPrice))
            {
                isPOcostingMethod = true;
            }
            else
            {
                isPOcostingMethod = false;
            }

            return(isPOcostingMethod);
        }
 /// <summary>
 /// Get Primary Accounting Schema
 /// </summary>
 /// <returns>Acct Schema or null</returns>
 internal MAcctSchema GetAcctSchema()
 {
     if (info == null)
     {
         info = MOrgInfo.Get(GetCtx(), GetAD_Client_ID(), Get_TrxName());
     }
     if (info != null)
     {
         int C_AcctSchema_ID = info.GetC_AcctSchema_ID();
         if (C_AcctSchema_ID != 0)
         {
             return(MAcctSchema.Get(GetCtx(), C_AcctSchema_ID));
         }
     }
     return(null);
 }
        }       //	setCurrencyRate

        /// <summary>
        ///     Set Accounted Amounts
        /// </summary>
        /// <param name="windowNo">window no</param>
        private void SetAmt(int windowNo)
        {
            //  Get Target Currency & Precision from C_AcctSchema.C_Currency_ID
            int?        C_AcctSchema_ID = GetCtx().GetContextAsInt(windowNo, "C_AcctSchema_ID");
            MAcctSchema ass             = MAcctSchema.Get(GetCtx(), C_AcctSchema_ID.Value);
            int?        Precision       = ass.GetStdPrecision();

            Decimal?CurrencyRate = GetCurrencyRate();

            if (CurrencyRate == null)
            {
                CurrencyRate = Env.ONE;
                SetCurrencyRate(CurrencyRate);
            }

            //  AmtAcct = AmtSource * CurrencyRate  ==> Precision
            Decimal?AmtSourceDr = GetAmtSourceDr();

            if (AmtSourceDr == null)
            {
                AmtSourceDr = Env.ZERO;
            }
            Decimal?AmtSourceCr = GetAmtSourceCr();

            if (AmtSourceCr == null)
            {
                AmtSourceCr = Env.ZERO;
            }

            Decimal?AmtAcctDr = (Decimal.Multiply(AmtSourceDr.Value, CurrencyRate.Value));

            //AmtAcctDr = AmtAcctDr.setScale(Precision, BigDecimal.ROUND_HALF_UP);
            AmtAcctDr = Decimal.Round(AmtAcctDr.Value, Precision.Value, MidpointRounding.AwayFromZero);
            SetAmtAcctDr(AmtAcctDr);
            Decimal?AmtAcctCr = Decimal.Multiply(AmtSourceCr.Value, CurrencyRate.Value);

            AmtAcctCr = Decimal.Round(AmtAcctCr.Value, Precision.Value, MidpointRounding.AwayFromZero);
            SetAmtAcctCr(AmtAcctCr);
        }       //	setAmt
        }   //  getClientAcctSchema

        // by amit 23-12-2015
        public static MAcctSchema[] GetClientAcctSchemas(Ctx ctx, int AD_Client_ID, Trx trxName)
        {
            //  Check Cache
            int key = AD_Client_ID;

            if (_schema.ContainsKey(key))
            {
                return((MAcctSchema[])_schema[key]);
            }

            //  Create New
            List <MAcctSchema> list = new List <MAcctSchema>();
            MClientInfo        info = MClientInfo.Get(ctx, AD_Client_ID, trxName);
            MAcctSchema        ass  = MAcctSchema.Get(ctx, info.GetC_AcctSchema1_ID(), trxName);

            if (ass.Get_ID() != 0 && trxName == null)
            {
                list.Add(ass);
            }

            //	Other
            String sql = "SELECT C_AcctSchema_ID FROM C_AcctSchema acs "
                         + "WHERE IsActive='Y'";

            if (AD_Client_ID != 0)
            {
                sql += " AND AD_Client_ID=" + AD_Client_ID;
            }
            sql += " ORDER BY C_AcctSchema_ID";

            IDataReader dr = null;

            try
            {
                dr = DataBase.DB.ExecuteReader(sql, null, trxName);
                while (dr.Read())
                {
                    int id = Utility.Util.GetValueOfInt(dr[0].ToString());
                    if (id != info.GetC_AcctSchema1_ID())       //	already in list
                    {
                        ass = MAcctSchema.Get(ctx, id, trxName);
                        if (ass.Get_ID() != 0 && trxName == null)
                        {
                            list.Add(ass);
                        }
                    }
                }
                dr.Close();
                dr = null;
            }
            catch (System.Data.Common.DbException e)
            {
                if (dr != null)
                {
                    dr.Close();
                }
                _log.Log(Level.SEVERE, sql, e);
            }
            finally
            {
                if (dr != null)
                {
                    dr.Close();
                    dr = null;
                }
            }
            //  Save
            MAcctSchema[] retValue = new MAcctSchema[list.Count];
            retValue = list.ToArray();
            if (trxName == null)
            {
                _schema.Add(key, retValue);
            }
            return(retValue);
        }   //  getClientAcctSchema
        }       //	afterDelete

        /** Update combination and optionally **/
        private bool GetOrCreateCombination(Boolean newRecord)
        {
            int Account_ID = 0, C_SubAcct_ID = 0, M_Product_ID = 0, C_BPartner_ID = 0, AD_Org_ID = 0, AD_OrgTrx_ID = 0,
                C_LocFrom_ID = 0, C_LocTo_ID = 0, C_SalesRegion_ID = 0, C_Project_ID = 0, C_Campaign_ID = 0,
                C_Activity_ID = 0, User1_ID = 0, User2_ID = 0;

            if (GetC_ValidCombination_ID() == 0 ||
                (!newRecord && (Is_ValueChanged("Account_ID") ||
                                Is_ValueChanged("M_Product_ID") ||
                                Is_ValueChanged("C_BPartner_ID") ||
                                Is_ValueChanged("AD_Org_ID") ||
                                Is_ValueChanged("C_Project_ID") ||
                                Is_ValueChanged("C_Campaign_ID") ||
                                Is_ValueChanged("C_Activity_ID"))))
            {
                MJournal gl = new MJournal(GetCtx(), GetGL_Journal_ID(), Get_TrxName());

                // Validate all mandatory combinations are set
                MAcctSchema          asc      = MAcctSchema.Get(GetCtx(), gl.GetC_AcctSchema_ID());
                MAcctSchemaElement[] elements = MAcctSchemaElement.GetAcctSchemaElements(asc);
                for (int i = 0; i < elements.Length; i++)
                {
                    MAcctSchemaElement elem = elements[i];
                    String             et   = elem.GetElementType();
                    if (MAcctSchemaElement.ELEMENTTYPE_Account.Equals(et) && Get_ColumnIndex("Account_ID") > 0)
                    {
                        Account_ID = Util.GetValueOfInt(Get_Value("Account_ID"));
                    }
                    if (MAcctSchemaElement.ELEMENTTYPE_Account.Equals(et) && Get_ColumnIndex("C_SubAcct_ID") > 0)
                    {
                        C_SubAcct_ID = Util.GetValueOfInt(Get_Value("C_SubAcct_ID"));
                    }
                    if (MAcctSchemaElement.ELEMENTTYPE_Activity.Equals(et) && Get_ColumnIndex("C_Activity_ID") > 0)
                    {
                        C_Activity_ID = Util.GetValueOfInt(Get_Value("C_Activity_ID"));
                    }
                    if (MAcctSchemaElement.ELEMENTTYPE_BPartner.Equals(et) && Get_ColumnIndex("C_BPartner_ID") > 0)
                    {
                        C_BPartner_ID = Util.GetValueOfInt(Get_Value("C_BPartner_ID"));
                    }
                    if (MAcctSchemaElement.ELEMENTTYPE_Campaign.Equals(et) && Get_ColumnIndex("C_BPartner_ID") > 0)
                    {
                        C_BPartner_ID = Util.GetValueOfInt(Get_Value("C_BPartner_ID"));
                    }
                    if (MAcctSchemaElement.ELEMENTTYPE_Organization.Equals(et))
                    {
                        AD_Org_ID = GetAD_Org_ID();
                    }
                    if (MAcctSchemaElement.ELEMENTTYPE_OrgTrx.Equals(et) && Get_ColumnIndex("AD_OrgTrx_ID") > 0)
                    {
                        AD_OrgTrx_ID = Util.GetValueOfInt(Get_Value("AD_OrgTrx_ID"));
                    }
                    if (MAcctSchemaElement.ELEMENTTYPE_Product.Equals(et) && Get_ColumnIndex("C_LocFrom_ID") > 0)
                    {
                        C_LocFrom_ID = Util.GetValueOfInt(Get_Value("C_LocFrom_ID"));
                    }
                    if (MAcctSchemaElement.ELEMENTTYPE_Product.Equals(et) && Get_ColumnIndex("C_LocTo_ID") > 0)
                    {
                        C_LocTo_ID = Util.GetValueOfInt(Get_Value("C_LocTo_ID"));
                    }
                    if (MAcctSchemaElement.ELEMENTTYPE_Product.Equals(et) && Get_ColumnIndex("M_Product_ID") > 0)
                    {
                        M_Product_ID = Util.GetValueOfInt(Get_Value("M_Product_ID"));
                    }
                    if (MAcctSchemaElement.ELEMENTTYPE_Project.Equals(et) && Get_ColumnIndex("C_Project_ID") > 0)
                    {
                        C_Project_ID = Util.GetValueOfInt(Get_Value("C_Project_ID"));
                    }
                    if (MAcctSchemaElement.ELEMENTTYPE_Project.Equals(et) && Get_ColumnIndex("C_Campaign_ID") > 0)
                    {
                        C_Campaign_ID = Util.GetValueOfInt(Get_Value("C_Campaign_ID"));
                    }
                    if (MAcctSchemaElement.ELEMENTTYPE_SalesRegion.Equals(et) && Get_ColumnIndex("C_SalesRegion_ID") > 0)
                    {
                        C_SalesRegion_ID = Util.GetValueOfInt(Get_Value("C_SalesRegion_ID"));
                    }
                    if (MAcctSchemaElement.ELEMENTTYPE_UserList1.Equals(et) && Get_ColumnIndex("User1_ID") > 0)
                    {
                        User1_ID = Util.GetValueOfInt(Get_Value("User1_ID"));
                    }
                    if (MAcctSchemaElement.ELEMENTTYPE_UserList2.Equals(et) && Get_ColumnIndex("User2_ID") > 0)
                    {
                        User2_ID = Util.GetValueOfInt(Get_Value("User2_ID"));
                    }
                }

                MAccount acct = MAccount.Get(GetCtx(), GetAD_Client_ID(), AD_Org_ID, gl.GetC_AcctSchema_ID(), Account_ID,
                                             C_SubAcct_ID, M_Product_ID, C_BPartner_ID, AD_OrgTrx_ID, C_LocFrom_ID, C_LocTo_ID, C_SalesRegion_ID,
                                             C_Project_ID, C_Campaign_ID, C_Activity_ID, User1_ID, User2_ID, 0, 0, 0, 0, 0, 0, 0, 0, 0);

                if (acct != null)
                {
                    acct.Save(Get_TrxName());   // get ID from transaction
                    SetC_ValidCombination_ID(acct.Get_ID());

                    //if (acct.GetAlias() != null && acct.GetAlias().length > 0)
                    //    setAlias_ValidCombination_ID(acct.get_ID());
                    //else
                    //    setAlias_ValidCombination_ID(0);
                }
            }
            else
            {
                fillDimensionsFromCombination();
            }
            return(true);
        }