public EntityResponse<DispatchSupplier> AddSupplier(AddSupplierRequest request) { try { var supplier = request.Supplier; using (var scope = new TransactionScope()) { using (var db = new LomsContext()) { if (supplier.Suburb != null) { supplier.Country = null; supplier.State = null; } else if (supplier.State != null) supplier.Country = null; db.DispatchSuppliers.ApplyChanges(supplier); db.SaveChanges(); //create manager var user = request.Manager; user.SupplierId = supplier.Id; user.Login = "******"; db.DispatchSupplierUsers.ApplyChanges(user); db.SaveChanges(); user.Login = string.Format("sup{0:000}-{1:000}", supplier.Id, user.Id); user.Role = SupplierUserRole.Manager; if (request.HasOnlineAccess) { MembershipCreateStatus ret; MembershipUser membershipUser = Membership.CreateUser(user.Login, request.ManagerPassword, user.Email, "Who am I?", "I", true, null, out ret); if (ret != MembershipCreateStatus.Success) throw new ApplicationException(ret.ToString()); user.AspNetUserId = (Guid)membershipUser.ProviderUserKey; if (!Roles.RoleExists(RoleName.SupplierUser)) Roles.CreateRole(RoleName.SupplierUser); if (!Roles.RoleExists(RoleName.SupplierManager)) Roles.CreateRole(RoleName.SupplierManager); if (!Roles.IsUserInRole(user.Login, RoleName.SupplierUser)) Roles.AddUserToRole(user.Login, RoleName.SupplierUser); if (user.Role == SupplierUserRole.Manager && !Roles.IsUserInRole(user.Login, RoleName.SupplierManager)) Roles.AddUserToRole(user.Login, RoleName.SupplierManager); else if (user.Role != SupplierUserRole.Manager && Roles.IsUserInRole(user.Login, RoleName.SupplierManager)) Roles.RemoveUserFromRole(user.Login, RoleName.SupplierManager); } db.DispatchSupplierUsers.ApplyChanges(user); db.SaveChanges(); } //save cities foreach (var supplierCity in request.Cities) { using (var db = new LomsContext()) { supplierCity.SupplierId = supplier.Id; db.DispatchSupplierCities.ApplyChanges(supplierCity); db.SaveChanges(); } } scope.Complete(); } using (var db = new LomsContext()) { supplier = db.DispatchSuppliers.IncludeAll("Country", "State", "State.Country", "Suburb", "Suburb.Country", "Suburb.State", "Suburb.State.Country").Where(s => s.Id == supplier.Id).SingleOrDefault(); return new EntityResponse<DispatchSupplier>(supplier); } } catch (Exception ex) { StringBuilder builder = new StringBuilder(); builder.AppendLine(ex.Message); if (ex.InnerException != null) { builder.AppendLine(ex.InnerException.Message); if (ex.InnerException.InnerException != null) builder.AppendLine(ex.InnerException.InnerException.Message); } return new EntityResponse<DispatchSupplier>(builder.ToString()); } }
/// <summary> /// save the dispatch supplier with all the details, including manager, cities of service and vechiletypes /// </summary> private void SaveDispatchSupplier() { if (SelectedSupplier == null) return; AddSupplierRequest addSupplierRequest = new AddSupplierRequest(); addSupplierRequest.Supplier = SelectedSupplier; addSupplierRequest.Manager = Manager; addSupplierRequest.HasOnlineAccess = HasOnlineAccess; addSupplierRequest.ManagerPassword = ManagerPassword; IList<DispatchSupplierCity> dispatchSupplierCityList = new List<DispatchSupplierCity>(); foreach (CitiesOfServiceControlViewModel cityServiceViewModel in CitiesViewModels) { if (cityServiceViewModel.SelectedCity == null || cityServiceViewModel.SelectedCity.Id <=0) continue; DispatchSupplierCity supplierCity = new DispatchSupplierCity(); supplierCity.AssignByEmail = cityServiceViewModel.AssignByEmail; supplierCity.AssignByFax = cityServiceViewModel.AssignByFax; supplierCity.AssignByPda = cityServiceViewModel.AssignByPDA; supplierCity.AssignByPhone = cityServiceViewModel.AssignByPhone; supplierCity.AssignBySms = cityServiceViewModel.AssignBySMS; supplierCity.City = cityServiceViewModel.SelectedCity; supplierCity.CityId = cityServiceViewModel.SelectedCity.Id; supplierCity.ProspectResponseTime = cityServiceViewModel.SelectedProspectResponseTimes != null ? cityServiceViewModel.SelectedProspectResponseTimes.Value : 0; supplierCity.QuotationResponseTime = cityServiceViewModel.SelectedQuotationResponseTime != null ? cityServiceViewModel.SelectedQuotationResponseTime.Value : 0; foreach (ViewVehicleType viewVehicleType in cityServiceViewModel.VehicleTypes) { if (viewVehicleType.IsSelected) { DispatchSupplierCityVehicleType supplierVehicleType = new DispatchSupplierCityVehicleType(); supplierVehicleType.SupplierCityId = supplierCity.Id; supplierVehicleType.VehicleTypeId = viewVehicleType.VehicleType.Id; supplierVehicleType.VehicleType = viewVehicleType.VehicleType; supplierVehicleType.TypeId = viewVehicleType.SelectedStatus != null ? viewVehicleType.SelectedStatus.Id : 0; supplierCity.VehicleTypes.Add(supplierVehicleType); } } dispatchSupplierCityList.Add(supplierCity); } addSupplierRequest.Cities = dispatchSupplierCityList; DispatcherService.BeginAddSupplier(addSupplierRequest, CreateAsyncCallback(ar => DispatcherService.EndAddSupplier(ar), response => { if (response.Errors.Count() == 0) { if (OnSupplierAdded != null) OnSupplierAdded(this, EventArgs.Empty); EnableDisableSave(false); //disable the save button to avoid multiple click by users to same the duplicate data } else { ShowErrors(response.Errors); EnableDisableSave(true); } }), null); }