Beispiel #1
0
        /// <summary>
        /// Add Or Update Business Partner
        /// </summary>
        /// <param name="searchKey"></param>
        /// <param name="name"></param>
        /// <param name="name2"></param>
        /// <param name="greeting"></param>
        /// <param name="bpGroup"></param>
        /// <param name="bpRelation"></param>
        /// <param name="bpLocation"></param>
        /// <param name="contact"></param>
        /// <param name="greeting1"></param>
        /// <param name="title"></param>
        /// <param name="email"></param>
        /// <param name="address"></param>
        /// <param name="phoneNo"></param>
        /// <param name="phoneNo2"></param>
        /// <param name="fax"></param>
        /// <param name="ctx"></param>
        /// <param name="_windowNo"></param>
        /// <param name="BPtype"></param>
        /// <param name="C_BPartner_ID"></param>
        /// <returns></returns>

        public string AddBPartner(string searchKey, string name, string name2, string greeting, string bpGroup, string bpRelation, string bpLocation, string contact, string greeting1, string title, string email, string address, string phoneNo, string phoneNo2, string fax, Ctx ctx, int _windowNo, string BPtype, int C_BPartner_ID, bool isCustomer, bool isVendor, bool isProspect, string fileUrl, string umobile, string webUrl, bool isEmployee)
        {
            StringBuilder strError     = new StringBuilder();
            int           AD_Client_ID = ctx.GetAD_Client_ID();

            if (C_BPartner_ID > 0)
            {
                _partner = new MBPartner(ctx, C_BPartner_ID, null);
            }
            else
            {
                _partner = MBPartner.GetTemplate(ctx, AD_Client_ID);
            }
            bool isSOTrx = ctx.IsSOTrx(_windowNo);

            _partner.SetIsCustomer(isSOTrx);
            _partner.SetIsVendor(!isSOTrx);
            // JID_1197 IN Business partner  updating Createdby,Updatedby,Created,Updated fields as per changed date
            _partner.Set_ValueNoCheck("CreatedBy", ctx.GetAD_User_ID());
            _partner.Set_ValueNoCheck("Created", DateTime.Now);
            _partner.Set_ValueNoCheck("Updated", DateTime.Now);
            _partner.Set_ValueNoCheck("UpdatedBy", ctx.GetAD_User_ID());
            if (BPtype != null && (!isCustomer && !isVendor))
            {
                if (BPtype.Contains("Customer"))
                {
                    _partner.SetIsCustomer(true);
                }
                if (BPtype.Contains("Employee"))
                {
                    _partner.SetIsEmployee(true);
                }
                if (BPtype.Contains("Vendor"))
                {
                    _partner.SetIsVendor(true);
                }
                if (BPtype.Contains("Prospect"))
                {
                    _partner.SetIsProspect(true);
                }

                /*
                 * if (BPtype == "Customer")
                 * {
                 *  _partner.SetIsCustomer(true);
                 * }
                 * else if (BPtype == "Employee")
                 * {
                 *  _partner.SetIsEmployee(true);
                 * }
                 * else if (BPtype == "Vendor")
                 * {
                 *  _partner.SetIsVendor(true);
                 * }
                 * else if (BPtype == "Prospect")
                 * {
                 *  _partner.SetIsProspect(true);
                 * }*/
            }
            if (isCustomer)
            {
                _partner.SetIsCustomer(true);
            }
            else
            {
                _partner.SetIsCustomer(false);
            }
            if (isVendor)
            {
                _partner.SetIsVendor(true);
            }
            else
            {
                _partner.SetIsVendor(false);
            }
            if (isProspect)
            {
                _partner.SetIsProspect(true);
            }
            else
            {
                _partner.SetIsProspect(false);
            }

            if (isEmployee)
            {
                _partner.SetIsEmployee(true);
            }
            else
            {
                _partner.SetIsEmployee(false);
            }

            if (searchKey == null || searchKey.Length == 0)
            {
                //	get Table Documet No
                searchKey = MSequence.GetDocumentNo(ctx.GetAD_Client_ID(), "C_BPartner", null, ctx);
                //Dispatcher.BeginInvoke(() => { txtValue.Text = value; });
            }
            _partner.SetValue(searchKey);
            //
            _partner.SetName(name);
            _partner.SetURL(webUrl);
            //  _partner.SetName2(name2);
            //KeyNamePair p = (KeyNamePair)cmbGreetingBP.SelectedItem;
            //if (greeting >0)
            //{
            //    _partner.SetC_Greeting_ID(greeting);
            //}
            //else
            //{
            //    _partner.SetC_Greeting_ID(0);
            //}
            if (greeting != string.Empty)
            {
                _partner.SetC_Greeting_ID(Convert.ToInt32(greeting));
            }
            else
            {
                _partner.SetC_Greeting_ID(0);
            }
            /***************************************************/
            _partner.SetC_BP_Group_ID(Util.GetValueOfInt(bpGroup));
            /***************************************************/

            if (_partner.Save())
            {
                log.Fine("C_BPartner_ID=" + _partner.GetC_BPartner_ID());
            }
            else
            {
                // Classes.ShowMessage.Error("SearchKeyExist", null);
                strError.Append("SearchKeyExist");
                //this.Cursor = Cursors.Arrow;
                return(strError.ToString());
            }

            //	***** Business Partner - Location *****
            if (_pLocation == null)
            {
                if (C_BPartner_ID > 0)
                {
                    _pLocation = new MBPartnerLocation(ctx, GetBPartnerLocationID(_partner.Get_ID()), null);
                    if (_pLocation.Get_ID() <= 0)
                    {
                        _pLocation = new MBPartnerLocation(_partner);
                    }
                }
                else
                {
                    _pLocation = new MBPartnerLocation(_partner);
                }
            }
            if (address != string.Empty)
            {
                _pLocation.SetC_Location_ID(Convert.ToInt32(address));
            }

            //
            _pLocation.SetPhone(phoneNo);
            // _pLocation.SetPhone2(phoneNo2);
            _pLocation.SetFax(fax);
            if (_pLocation.Save())
            {
                log.Fine("C_BPartner_Location_ID=" + _pLocation.GetC_BPartner_Location_ID());
            }
            else
            {
                //   ADialog.error(m_WindowNo, this, "BPartnerNotSaved", Msg.translate(Env.getCtx(), "C_BPartner_Location_ID"));
                // Classes.ShowMessage.Error("BPartnerNotSaved", null);
                //this.Cursor = Cursors.Arrow;
                strError.Append("BPartnerNotSaved");
                return(strError.ToString());
            }

            //	***** Business Partner - User *****
            //String contact = txtContact.Text;
            //String email = txtEMail.Text;
            if (_user == null && (contact.Length > 0 || email.Length > 0))
            {
                if (C_BPartner_ID > 0)
                {
                    _user = new MUser(ctx, GetUserID(_partner.Get_ID()), null);
                }
                else
                {
                    _user = new MUser(_partner);
                }
            }
            if (_user != null)
            {
                if (contact.Length == 0)
                {
                    contact = name;
                }
                _user.SetName(contact);
                _user.SetEMail(email);
                _user.SetTitle(title);
                _user.SetC_Location_ID(Convert.ToInt32(address));

                // = (KeyNamePair)cmbGreetingC.SelectedItem;

                //if (greeting1 >0)
                //    _user.SetC_Greeting_ID(greeting1);
                if (greeting1 != string.Empty)
                {
                    _user.SetC_Greeting_ID(Convert.ToInt32(greeting1));
                }
                else
                {
                    _user.SetC_Greeting_ID(0);
                }
                //
                _user.SetPhone(phoneNo);
                // _user.SetPhone2(phoneNo2);
                _user.SetMobile(umobile);
                _user.SetFax(fax);
                _user.SetC_BPartner_Location_ID(_pLocation.GetC_BPartner_Location_ID());
                if (_user.Save())
                {
                    if (fileUrl != null && fileUrl != string.Empty)
                    {
                        _user.SetAD_Image_ID(SaveUserImage(ctx, fileUrl, _user.GetAD_User_ID()));
                    }
                    if (_user.Save())
                    {
                        log.Fine("AD_User_ID(AD_Image_ID)=" + _user.GetAD_User_ID() + "(" + _user.GetAD_Image_ID() + ")");
                    }
                    log.Fine("AD_User_ID=" + _user.GetAD_User_ID());
                }
                else
                {
                    //Classes.ShowMessage.Error("BPartnerNotSaved", null);
                    //this.Cursor = Cursors.Arrow;
                    strError.Append("BPartnerNotSaved");
                    return(strError.ToString());
                }

                /*************************************************/
                if ((bpRelation != null && bpLocation != null) && (bpRelation != string.Empty && bpLocation != string.Empty))
                {
                    if (bpRelation.ToString().Trim() == "" || bpLocation.ToString().Trim() == "")
                    {
                        int dele = DB.ExecuteQuery("DELETE from C_BP_Relation where c_bpartner_id=" + _partner.GetC_BPartner_ID(), null, null);
                        if (dele == -1)
                        {
                            log.SaveError("C_BP_RelationNotDeleted", "c_bpartner_id=" + _partner.GetC_BPartner_ID());
                        }
                    }
                    else
                    {
                        //Business Partner Relation
                        if (C_BPartner_ID > 0)
                        {
                            _bprelation = new X_C_BP_Relation(ctx, GetBPRelationID(_partner.Get_ID()), null);
                        }
                        else
                        {
                            _bprelation = new X_C_BP_Relation(ctx, 0, null);
                        }
                        _bprelation.SetAD_Client_ID(_partner.GetAD_Client_ID());
                        _bprelation.SetAD_Org_ID(_partner.GetAD_Org_ID());
                        _bprelation.SetName(_partner.GetName());
                        _bprelation.SetDescription(_partner.GetDescription());
                        _bprelation.SetC_BPartner_ID(_partner.GetC_BPartner_ID());
                        _bprelation.SetC_BPartner_Location_ID(_pLocation.GetC_BPartner_Location_ID());
                        _bprelation.SetC_BP_Relation_ID(Util.GetValueOfInt(bpRelation));
                        _bprelation.SetC_BPartnerRelation_ID(Util.GetValueOfInt(bpRelation));
                        _bprelation.SetC_BPartnerRelation_Location_ID(Util.GetValueOfInt(bpLocation));
                        _bprelation.SetIsBillTo(true);
                        if (_bprelation.Save())
                        {
                            log.Fine("C_BP_Relation_ID=" + _bprelation.GetC_BP_Relation_ID());
                        }
                        else
                        {
                            //Classes.ShowMessage.Error("BPRelationNotSaved", null);

                            //this.Cursor = Cursors.Arrow;
                            strError.Append("BPRelationNotSaved");
                            return(strError.ToString());
                        }
                    }
                }
                /*************************************************/
            }
            return(strError.ToString());
        }
        }       //	prepare

        /// <summary>
        /// Perform Process.
        /// </summary>
        /// <returns>Message (text with variables)</returns>
        protected override String DoIt()
        {
            log.Info("C_BPartner_ID=" + _C_BPartner_ID
                     + ", AD_Org_ID=" + _AD_Org_ID
                     + ", AD_OrgType_ID=" + _AD_OrgType_ID
                     + ", AD_Role_ID=" + _AD_Role_ID);
            if (_C_BPartner_ID == 0)
            {
                throw new Exception("No Business Partner ID");
            }
            MBPartner bp = new MBPartner(GetCtx(), _C_BPartner_ID, Get_Trx());

            if (bp.Get_ID() == 0)
            {
                throw new Exception("Business Partner not found - C_BPartner_ID=" + _C_BPartner_ID);
            }
            //	BP Location
            MBPartnerLocation[] locs = bp.GetLocations(false);
            if (locs == null || locs.Length == 0)
            {
                throw new ArgumentException("Business Partner has no Location");
            }
            //	Location
            int C_Location_ID = locs[0].GetC_Location_ID();

            if (C_Location_ID == 0)
            {
                throw new ArgumentException("Business Partner Location has no Address");
            }

            //	Create Org
            Boolean newOrg = _AD_Org_ID == 0;
            MOrg    org    = new MOrg(GetCtx(), _AD_Org_ID, Get_Trx());

            if (newOrg)
            {
                org.SetValue(bp.GetValue());
                org.SetName(bp.GetName());
                org.SetDescription(bp.GetDescription());
                if (!org.Save())
                {
                    return(GetRetrievedError(org, "Organization not saved"));
                    //throw new Exception("Organization not saved");
                }
            }
            else        //	check if linked to already
            {
                int C_BPartner_ID = org.GetLinkedC_BPartner_ID();
                if (C_BPartner_ID > 0)
                {
                    throw new ArgumentException("Organization '" + org.GetName()
                                                + "' already linked (to C_BPartner_ID=" + C_BPartner_ID + ")");
                }
            }
            _AD_Org_ID = org.GetAD_Org_ID();

            //	Update Org Info
            MOrgInfo oInfo = org.GetInfo();

            oInfo.SetAD_OrgType_ID(_AD_OrgType_ID);
            if (newOrg)
            {
                oInfo.SetC_Location_ID(C_Location_ID);
            }

            //	Create Warehouse
            MWarehouse wh = null;

            if (!newOrg)
            {
                MWarehouse[] whs = MWarehouse.GetForOrg(GetCtx(), _AD_Org_ID);
                if (whs != null && whs.Length > 0)
                {
                    wh = whs[0]; //	pick first
                }
            }
            //	New Warehouse
            if (wh == null)
            {
                wh = new MWarehouse(org);
                if (!wh.Save())
                {
                    return(GetRetrievedError(wh, "Warehouse not saved"));
                    //throw new Exception("Warehouse not saved");
                }
            }
            //	Create Locator
            MLocator mLoc = wh.GetDefaultLocator();

            if (mLoc == null)
            {
                mLoc = new MLocator(wh, "Standard");
                mLoc.SetIsDefault(true);
                mLoc.Save();
            }

            //	Update/Save Org Info
            oInfo.SetM_Warehouse_ID(wh.GetM_Warehouse_ID());
            if (!oInfo.Save(Get_Trx()))
            {
                return(GetRetrievedError(oInfo, "Organization Info not saved"));
                //throw new Exception("Organization Info not saved");
            }

            //	Update BPartner
            bp.SetAD_OrgBP_ID(_AD_Org_ID);
            if (bp.GetAD_Org_ID() != 0)
            {
                bp.SetClientOrg(bp.GetAD_Client_ID(), 0); //	Shared BPartner
            }

            //	Save BP
            if (!bp.Save())
            {
                return(GetRetrievedError(bp, "Business Partner not updated"));
                //throw new Exception("Business Partner not updated");
            }

            //	Limit to specific Role
            if (_AD_Role_ID != 0)
            {
                Boolean          found       = false;
                MRoleOrgAccess[] orgAccesses = MRoleOrgAccess.GetOfOrg(GetCtx(), _AD_Org_ID);
                //	delete all accesses except the specific
                for (int i = 0; i < orgAccesses.Length; i++)
                {
                    if (orgAccesses[i].GetAD_Role_ID() == _AD_Role_ID)
                    {
                        found = true;
                    }
                    else
                    {
                        orgAccesses[i].Delete(true);
                    }
                }
                //	create access
                if (!found)
                {
                    MRoleOrgAccess orgAccess = new MRoleOrgAccess(org, _AD_Role_ID);
                    orgAccess.Save();
                }
            }

            //	Reset Client Role
            MRole.GetDefault(GetCtx(), true);

            return("Business Partner - Organization Link created");
        } //	doIt