/// <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); }