예제 #1
0
        public ActionResult Edit(EditCompanyViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                // get company
                int userId = _security.GetUserId(User.Identity.Name);
                var company = _serviceLayer.GetUserProfiles(u => u.UserId == userId).FirstOrDefault().Company;

                if (viewModel.Address1 != null)
                    company.Address1 = viewModel.Address1.Trim();
                if (viewModel.Address2 != null)
                    company.Address2 = viewModel.Address2.Trim();

                company.City = viewModel.City;
                company.CompanyName = viewModel.CompanyName.Trim();
                company.OperatingDistance = viewModel.OperatingDistance;
                company.Phone = Util.ConvertPhoneForStorage(viewModel.Phone.Trim());
                company.PostalCode = viewModel.PostalCode.Trim();
                //company.StateId = viewModel.StateId;
                company.State = _serviceLayer.GetState(viewModel.StateId.Value);
                GeoLocator locator = new GeoLocator();

                if (company.Address1 == null && company.City == null && company.StateId != null && company.PostalCode != null)
                {

                    company.GeoLocation = locator.GetFromStateZip(company.State.Abbr, company.PostalCode);
                }
                else if ((company.Address1 == null || company.Address1 == string.Empty) && company.StateId != null && company.PostalCode != null)
                {
                    company.GeoLocation = locator.GetFromCityStateZip(company.City, company.State.Abbr, company.PostalCode);
                }
                else if ((company.Address1 != null && company.Address1 != string.Empty) && (company.City != null && company.City != string.Empty) && company.StateId != null && company.PostalCode != null)
                {
                    company.GeoLocation = locator.GetFromAddress(company.Address1, company.City, company.State.Abbr, company.PostalCode);
                }

                // did business type change?
                if (company.BusinessType != viewModel.BusinessType)
                {

                    // add new role for all users in company
                    switch (viewModel.BusinessType.Value)
                    {
                        case BusinessType.GeneralContractor:
                            _security.AddUsersToRole(company.Users.Select(x => x.Email).ToArray(), "general_contractor");
                            break;
                        case BusinessType.SubContractor:
                            _security.AddUsersToRole(company.Users.Select(x => x.Email).ToArray(), "subcontractor");
                            break;
                        case BusinessType.Architect:
                            _security.AddUsersToRole(company.Users.Select(x => x.Email).ToArray(), "architect");
                            break;
                        case BusinessType.Engineer:
                            _security.AddUsersToRole(company.Users.Select(x => x.Email).ToArray(), "engineer");
                            break;
                        case BusinessType.Owner:
                            _security.AddUsersToRole(company.Users.Select(x => x.Email).ToArray(), "owner_client");
                            break;
                        case BusinessType.MaterialsVendor:
                            _security.AddUsersToRole(company.Users.Select(x => x.Email).ToArray(), "materials_vendor");
                            break;
                        case BusinessType.MaterialsMfg:
                            _security.AddUsersToRole(company.Users.Select(x => x.Email).ToArray(), "materials_manufacturer");
                            break;
                        case BusinessType.Consultant:
                            _security.AddUsersToRole(company.Users.Select(x => x.Email).ToArray(), "consultant");
                            break;
                    };

                    // remove old role for all users in company
                    switch (company.BusinessType)
                    {
                        case BusinessType.GeneralContractor:
                            _security.RemoveUsersFromRole(company.Users.Select(x => x.Email).ToArray(), "general_contractor");
                            break;
                        case BusinessType.SubContractor:
                            _security.RemoveUsersFromRole(company.Users.Select(x => x.Email).ToArray(), "subcontractor");
                            break;
                        case BusinessType.Architect:
                            _security.RemoveUsersFromRole(company.Users.Select(x => x.Email).ToArray(), "architect");
                            break;
                        case BusinessType.Engineer:
                            _security.RemoveUsersFromRole(company.Users.Select(x => x.Email).ToArray(), "engineer");
                            break;
                        case BusinessType.Owner:
                            _security.RemoveUsersFromRole(company.Users.Select(x => x.Email).ToArray(), "owner_client");
                            break;
                        case BusinessType.MaterialsVendor:
                            _security.RemoveUsersFromRole(company.Users.Select(x => x.Email).ToArray(), "materials_vendor");
                            break;
                        case BusinessType.MaterialsMfg:
                            _security.RemoveUsersFromRole(company.Users.Select(x => x.Email).ToArray(), "materials_manufacturer");
                            break;
                        case BusinessType.Consultant:
                            _security.RemoveUsersFromRole(company.Users.Select(x => x.Email).ToArray(), "consultant");
                            break;
                    };

                    // update company business type
                    company.BusinessType = viewModel.BusinessType.Value;
                }

                // update changes in the database
                if (_serviceLayer.Update(company))
                {
                    return RedirectToRoute("Default", new { controller = "Account", action = "Manage", message = ManageMessageId.ChangeCompanyInfoSuccess });
                }
                else
                {
                    Util.MapValidationErrors(_serviceLayer.ValidationDic, this.ModelState);
                    viewModel.States = _serviceLayer.GetStates().Select(x => new SelectListItem { Text = x.Abbr, Value = x.Id.ToString(), Selected = x.Id == viewModel.StateId });
                    //viewModel.BusinessTypes = _serviceLayer.GetBusinessTypes().Select(x => new SelectListItem { Text = x.Name, Value = x.Id.ToString(), Selected = x.Id == viewModel.BusinessTypeId });
                    return View(viewModel);
                }
            }
            else
            {
                viewModel.States = _serviceLayer.GetStates().Select(x => new SelectListItem { Text = x.Abbr, Value = x.Id.ToString(), Selected = x.Id == viewModel.StateId });
                //viewModel.BusinessTypes = _serviceLayer.GetBusinessTypes().Select(x => new SelectListItem { Text = x.Name, Value = x.Id.ToString(), Selected = x.Id == viewModel.BusinessTypeId });
                return View(viewModel);
            }
        }
예제 #2
0
        public ActionResult Edit()
        {
            // get users company
            int userId = _security.GetUserId(User.Identity.Name);
            CompanyProfile company = _serviceLayer.GetUserProfiles(u => u.UserId == userId).FirstOrDefault().Company;

            // transpose into viewmodel
            EditCompanyViewModel viewModel = new EditCompanyViewModel
            {
                Address1 = company.Address1,
                Address2 = company.Address2,
                BusinessType = company.BusinessType,
                City = company.City,
                CompanyName = company.CompanyName,
                Id = company.Id,
                OperatingDistance = company.OperatingDistance,
                Phone = company.Phone == null || company.Phone == "" ? "" : Util.ConvertPhoneForDisplay(company.Phone),
                PostalCode = company.PostalCode,
                StateId = company.StateId
            };

            // fill states and business types
            viewModel.States = _serviceLayer.GetStates().Select(x => new SelectListItem { Text = x.Abbr, Value = x.Id.ToString(), Selected = x.Id == viewModel.StateId });
            viewModel.BusinessTypes = Util.CreateSelectListFromEnum(typeof(BusinessType), company.BusinessType.ToString());

            return View(viewModel);
        }