Example #1
0
        /**
         *  Derive MAccount from record
         *	@return Valid Account Combination
         */
        public MAccount GetMAccount()
        {
            MAccount acct = MAccount.Get(GetCtx(), GetAD_Client_ID(), GetAD_Org_ID(),
                                         GetC_AcctSchema_ID(), GetAccount_ID(), GetC_SubAcct_ID(),
                                         GetM_Product_ID(), GetC_BPartner_ID(), GetAD_OrgTrx_ID(),
                                         GetC_LocFrom_ID(), GetC_LocTo_ID(), GetC_SalesRegion_ID(),
                                         GetC_Project_ID(), GetC_Campaign_ID(), GetC_Activity_ID(),
                                         GetUser1_ID(), GetUser2_ID(), GetUserElement1_ID(), GetUserElement2_ID());

            if (acct != null && acct.Get_ID() == 0)
            {
                acct.Save();
            }
            return(acct);
        }
Example #2
0
        /// <summary>
        /// Update Value/Description after change of
        /// account element value/description.
        /// </summary>
        /// <param name="ctx">context</param>
        /// <param name="where">where clause</param>
        /// <param name="trxName">transaction</param>
        public static void UpdateValueDescription(Ctx ctx, String where, Trx trxName)
        {
            String sql = "SELECT * FROM C_ValidCombination";

            if (where != null && where.Length > 0)
            {
                sql += " WHERE " + where;
            }
            sql += " ORDER BY C_ValidCombination_ID";
            int     count  = 0;
            int     errors = 0;
            DataSet ds     = new DataSet();

            try
            {
                ds = DataBase.DB.ExecuteDataset(sql, null, trxName);
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    DataRow  rs      = ds.Tables[0].Rows[i];
                    MAccount account = new MAccount(ctx, rs, trxName);
                    account.SetValueDescription();
                    if (account.Save())
                    {
                        count++;
                    }
                    else
                    {
                        errors++;
                    }
                }
                ds = null;
            }
            catch (Exception e)
            {
                _log.Log(Level.SEVERE, sql, e);
            }
            _log.Fine(where + " #" + count + ", Errors=" + errors);
        }
        }       //	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);
        }
Example #4
0
        /// <summary>
        /// Get existing Account or create it
        /// </summary>
        /// <param name="ctx">context</param>
        /// <param name="AD_Client_ID">client id</param>
        /// <param name="AD_Org_ID"> organisation id</param>
        /// <param name="C_AcctSchema_ID"> account schema id</param>
        /// <param name="Account_ID">account id</param>
        /// <param name="C_SubAcct_ID">sub-Account id</param>
        /// <param name="M_Product_ID">product id</param>
        /// <param name="C_BPartner_ID">Bussness partner id</param>
        /// <param name="AD_OrgTrx_ID"> ordTax id</param>
        /// <param name="C_LocFrom_ID">C_LocFrom_ID</param>
        /// <param name="C_LocTo_ID"></param>
        /// <param name="C_SalesRegion_ID"></param>
        /// <param name="C_Project_ID"></param>
        /// <param name="C_Campaign_ID"></param>
        /// <param name="C_Activity_ID"></param>
        /// <param name="User1_ID"></param>
        /// <param name="User2_ID"></param>
        /// <param name="UserElement1_ID"></param>
        /// <param name="UserElement2_ID"></param>
        /// <returns>account or null</returns>
        public static MAccount Get(Ctx ctx, int AD_Client_ID, int AD_Org_ID, int C_AcctSchema_ID,
                                   int Account_ID, int C_SubAcct_ID, int M_Product_ID, int C_BPartner_ID, int AD_OrgTrx_ID,
                                   int C_LocFrom_ID, int C_LocTo_ID, int C_SalesRegion_ID, int C_Project_ID, int C_Campaign_ID,
                                   int C_Activity_ID, int User1_ID, int User2_ID, int UserElement1_ID, int UserElement2_ID)
        {
            MAccount existingAccount = null;
            //
            StringBuilder info = new StringBuilder();
            StringBuilder sql  = new StringBuilder("SELECT * FROM C_ValidCombination "
                                                                                          //	Mandatory fields
                                                   + "WHERE AD_Client_ID=" + AD_Client_ID //	#1
                                                   + " AND AD_Org_ID=" + AD_Org_ID
                                                   + " AND C_AcctSchema_ID=" + C_AcctSchema_ID
                                                   + " AND Account_ID=" + Account_ID); //	#4

            //	Optional fields
            if (C_SubAcct_ID == 0)
            {
                sql.Append(" AND C_SubAcct_ID IS NULL");
            }
            else
            {
                sql.Append(" AND C_SubAcct_ID=" + C_SubAcct_ID);
            }
            if (M_Product_ID == 0)
            {
                sql.Append(" AND M_Product_ID IS NULL");
            }
            else
            {
                sql.Append(" AND M_Product_ID=" + M_Product_ID);
            }
            if (C_BPartner_ID == 0)
            {
                sql.Append(" AND C_BPartner_ID IS NULL");
            }
            else
            {
                sql.Append(" AND C_BPartner_ID=" + C_BPartner_ID);
            }
            if (AD_OrgTrx_ID == 0)
            {
                sql.Append(" AND AD_OrgTrx_ID IS NULL");
            }
            else
            {
                sql.Append(" AND AD_OrgTrx_ID=" + AD_OrgTrx_ID);
            }
            if (C_LocFrom_ID == 0)
            {
                sql.Append(" AND C_LocFrom_ID IS NULL");
            }
            else
            {
                sql.Append(" AND C_LocFrom_ID=" + C_LocFrom_ID);
            }
            if (C_LocTo_ID == 0)
            {
                sql.Append(" AND C_LocTo_ID IS NULL");
            }
            else
            {
                sql.Append(" AND C_LocTo_ID=" + C_LocTo_ID);
            }
            if (C_SalesRegion_ID == 0)
            {
                sql.Append(" AND C_SalesRegion_ID IS NULL");
            }
            else
            {
                sql.Append(" AND C_SalesRegion_ID=" + C_SalesRegion_ID);
            }
            if (C_Project_ID == 0)
            {
                sql.Append(" AND C_Project_ID IS NULL");
            }
            else
            {
                sql.Append(" AND C_Project_ID=" + C_Project_ID);
            }
            if (C_Campaign_ID == 0)
            {
                sql.Append(" AND C_Campaign_ID IS NULL");
            }
            else
            {
                sql.Append(" AND C_Campaign_ID=" + C_Campaign_ID);
            }
            if (C_Activity_ID == 0)
            {
                sql.Append(" AND C_Activity_ID IS NULL");
            }
            else
            {
                sql.Append(" AND C_Activity_ID=" + C_Activity_ID);
            }
            if (User1_ID == 0)
            {
                sql.Append(" AND User1_ID IS NULL");
            }
            else
            {
                sql.Append(" AND User1_ID=" + User1_ID);
            }
            if (User2_ID == 0)
            {
                sql.Append(" AND User2_ID IS NULL");
            }
            else
            {
                sql.Append(" AND User2_ID=" + User2_ID);
            }
            if (UserElement1_ID == 0)
            {
                sql.Append(" AND UserElement1_ID IS NULL");
            }
            else
            {
                sql.Append(" AND UserElement1_ID=" + UserElement1_ID);
            }
            if (UserElement2_ID == 0)
            {
                sql.Append(" AND UserElement2_ID IS NULL");
            }
            else
            {
                sql.Append(" AND UserElement2_ID=" + UserElement2_ID);
            }
            sql.Append(" AND IsActive='Y'");
            //	sql.Append(" ORDER BY IsFullyQualified DESC");
            try
            {
                DataSet ds = null;
                //IDataReader dr=null;
                ds = DataBase.DB.ExecuteDataset(sql.ToString(), null, null);
                //  --  Mandatory Accounting fields
                info.Append("AD_Client_ID=").Append(AD_Client_ID).Append(",AD_Org_ID=").Append(AD_Org_ID);
                //	Schema
                info.Append(",C_AcctSchema_ID=").Append(C_AcctSchema_ID);
                //	Account
                info.Append(",Account_ID=").Append(Account_ID).Append(" ");

                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                //if (dr.Read())
                {
                    DataRow rs = ds.Tables[0].Rows[i];
                    existingAccount = new MAccount(ctx, rs, null);
                }
            }
            catch (Exception e)
            {
                _log.Log(Level.SEVERE, info + "\n" + sql, e);
            }
            //	Existing
            if (existingAccount != null)
            {
                return(existingAccount);
            }
            //	New
            MAccount newAccount = new MAccount(ctx, 0, null);

            newAccount.SetClientOrg(AD_Client_ID, AD_Org_ID);
            newAccount.SetC_AcctSchema_ID(C_AcctSchema_ID);
            newAccount.SetAccount_ID(Account_ID);
            //	--  Optional Accounting fields
            newAccount.SetC_SubAcct_ID(C_SubAcct_ID);
            newAccount.SetM_Product_ID(M_Product_ID);
            newAccount.SetC_BPartner_ID(C_BPartner_ID);
            newAccount.SetAD_OrgTrx_ID(AD_OrgTrx_ID);
            newAccount.SetC_LocFrom_ID(C_LocFrom_ID);
            newAccount.SetC_LocTo_ID(C_LocTo_ID);
            newAccount.SetC_SalesRegion_ID(C_SalesRegion_ID);
            newAccount.SetC_Project_ID(C_Project_ID);
            newAccount.SetC_Campaign_ID(C_Campaign_ID);
            newAccount.SetC_Activity_ID(C_Activity_ID);
            newAccount.SetUser1_ID(User1_ID);
            newAccount.SetUser2_ID(User2_ID);
            newAccount.SetUserElement1_ID(UserElement1_ID);
            newAccount.SetUserElement2_ID(UserElement2_ID);
            //
            if (!newAccount.Save())
            {
                _log.Log(Level.SEVERE, "Could not create new account - " + info);
                return(null);
            }
            _log.Fine("New: " + newAccount);
            return(newAccount);
        }