Example #1
0
        //public BusinessUnitVM GetBusinessUnitPer()
        //{
        //    try
        //    {
        //        BusinessUnitVM _objBusinessUnitVM = new BusinessUnitVM();
        //        _objBusinessUnitVM.BusinessUnit = unitOfWork.TblBussinessUnitRepository.Get().Count();


        //        return _objBusinessUnitVM;
        //    }
        //    catch (Exception ex)
        //    {

        //        throw;
        //    }
        //}

        public List <BusinessUnitVM> GetBusinessUnitPer()
        {
            try
            {
                var businessUnitData = unitOfWork.TblBussinessUnitRepository.Get().ToList();

                List <BusinessUnitVM> businessUnitList = new List <BusinessUnitVM>();

                foreach (var businessUnit in businessUnitData)
                {
                    BusinessUnitVM businessUnitVM = new BusinessUnitVM();
                    businessUnitVM.BusinessUnitID = businessUnit.BUID;
                    businessUnitVM.BusinessUnit   = businessUnit.BussinessUnit;

                    businessUnitVM.count = unitOfWork.TblClientRepository.Get(o => o.BUID == businessUnitVM.BusinessUnitID).Count();

                    businessUnitList.Add(businessUnitVM);
                }

                return(businessUnitList);
            }
            catch (Exception ex)
            {
                throw;
            }
        }
        public bool UpdateBusinessUnit(BusinessUnitVM businessUnitVM)
        {
            using (var dbTransaction = unitOfWork.dbContext.Database.BeginTransaction())
            {
                try
                {
                    tblBussinessUnit bussinessUnit = unitOfWork.TblBussinessUnitRepository.GetByID(businessUnitVM.BusinessUnitID);
                    bussinessUnit.BussinessUnit = businessUnitVM.BusinessUnit;
                    bussinessUnit.CompID        = businessUnitVM.CompanyID;
                    bussinessUnit.IsActive      = businessUnitVM.IsActive;
                    bussinessUnit.ModifiedDate  = DateTime.Now;
                    bussinessUnit.ModifiedBy    = businessUnitVM.ModifiedBy;
                    unitOfWork.TblBussinessUnitRepository.Update(bussinessUnit);
                    unitOfWork.Save();

                    //Complete the Transaction
                    dbTransaction.Commit();
                    return(true);
                }
                catch (Exception ex)
                {
                    //Roll back the Transaction
                    dbTransaction.Rollback();
                    return(false);
                }
            }
        }
        public BusinessUnitVM GetBusinessUnitByID(int businessUnitID)
        {
            try
            {
                var businessUnitData = unitOfWork.TblBussinessUnitRepository.GetByID(businessUnitID);

                BusinessUnitVM businessUnitVM = new BusinessUnitVM();
                businessUnitVM.BusinessUnitID = businessUnitData.BUID;
                businessUnitVM.BusinessUnit   = businessUnitData.BussinessUnit;
                businessUnitVM.CompanyID      = businessUnitData.CompID != null?Convert.ToInt32(businessUnitData.CompID) : 0;

                if (businessUnitVM.CompanyID > 0)
                {
                    businessUnitVM.CompanyName = businessUnitData.tblCompany.CompanyName;
                }

                businessUnitVM.IsActive    = businessUnitData.IsActive;
                businessUnitVM.CreatedDate = businessUnitData.CreatedDate != null?businessUnitData.CreatedDate.ToString() : string.Empty;

                businessUnitVM.ModifiedDate = businessUnitData.ModifiedDate != null?businessUnitData.ModifiedDate.ToString() : string.Empty;

                businessUnitVM.CreatedBy = businessUnitData.CreatedBy != null?Convert.ToInt32(businessUnitData.CreatedBy) : 0;

                businessUnitVM.ModifiedBy = businessUnitData.ModifiedBy != null?Convert.ToInt32(businessUnitData.ModifiedBy) : 0;

                return(businessUnitVM);
            }
            catch (Exception ex)
            {
                throw;
            }
        }
        public IHttpActionResult UpdateBusinessUnit([FromBody] JObject data)
        {
            try
            {
                int    businessUnitID = !string.IsNullOrEmpty(data.SelectToken("businessUnitID").Value <string>()) ? Convert.ToInt32(data.SelectToken("businessUnitID").Value <string>()) : 0;
                string businessUnit   = !string.IsNullOrEmpty(data.SelectToken("businessUnit").Value <string>()) ? data.SelectToken("businessUnit").Value <string>() : string.Empty;
                int    companyID      = !string.IsNullOrEmpty(data.SelectToken("companyID").Value <string>()) ? Convert.ToInt32(data.SelectToken("companyID").Value <string>()) : 0;
                bool   isActive       = !string.IsNullOrEmpty(data.SelectToken("isActive").Value <string>()) ? Convert.ToBoolean(data.SelectToken("isActive").Value <string>()) : false;
                int    userID         = !string.IsNullOrEmpty(data.SelectToken("userID").Value <string>()) ? Convert.ToInt32(data.SelectToken("userID").Value <string>()) : 0;

                if (!manageBusinessUnit.IsBusinessUnitAvailable(businessUnitID, businessUnit))
                {
                    BusinessUnitVM businessUnitVM = new BusinessUnitVM();
                    businessUnitVM.BusinessUnitID = businessUnitID;
                    businessUnitVM.BusinessUnit   = businessUnit;
                    businessUnitVM.CompanyID      = companyID;
                    businessUnitVM.IsActive       = isActive;
                    businessUnitVM.ModifiedBy     = userID;

                    bool status = manageBusinessUnit.UpdateBusinessUnit(businessUnitVM);

                    if (status)
                    {
                        return(Json(new { status = true, message = "Successfully updated" }));
                    }
                    else
                    {
                        return(Json(new { status = false, message = "Update Failed" }));
                    }
                }
                else
                {
                    return(Json(new { status = false, message = "Business Unit Name already exists" }));
                }
            }
            catch (Exception ex)
            {
                return(Json(new { status = false, message = "Unknown error occurred" }));
            }
        }
        public List <BusinessUnitVM> GetAllBusinessUnits()
        {
            try
            {
                var businessUnitData = unitOfWork.TblBussinessUnitRepository.Get().ToList();

                List <BusinessUnitVM> businessUnitList = new List <BusinessUnitVM>();

                foreach (var businessUnit in businessUnitData)
                {
                    BusinessUnitVM businessUnitVM = new BusinessUnitVM();
                    businessUnitVM.BusinessUnitID = businessUnit.BUID;
                    businessUnitVM.BusinessUnit   = businessUnit.BussinessUnit;
                    businessUnitVM.CompanyID      = businessUnit.CompID != null?Convert.ToInt32(businessUnit.CompID) : 0;

                    if (businessUnitVM.CompanyID > 0)
                    {
                        businessUnitVM.CompanyName = businessUnit.tblCompany.CompanyName;
                    }

                    businessUnitVM.IsActive    = businessUnit.IsActive;
                    businessUnitVM.CreatedDate = businessUnit.CreatedDate != null?businessUnit.CreatedDate.ToString() : string.Empty;

                    businessUnitVM.ModifiedDate = businessUnit.ModifiedDate != null?businessUnit.ModifiedDate.ToString() : string.Empty;

                    businessUnitVM.CreatedBy = businessUnit.CreatedBy != null?Convert.ToInt32(businessUnit.CreatedBy) : 0;

                    businessUnitVM.ModifiedBy = businessUnit.ModifiedBy != null?Convert.ToInt32(businessUnit.ModifiedBy) : 0;

                    businessUnitList.Add(businessUnitVM);
                }

                return(businessUnitList);
            }
            catch (Exception ex)
            {
                throw;
            }
        }