/// <summary> /// When replacing an existing chair of governor with data from an existing governor, this method prepopulates the fields from the governor record. /// </summary> /// <param name="model"></param> /// <param name="viewModel"></param> private void PrepopulateFields(GovernorModel model, CreateEditGovernorViewModel viewModel) { viewModel.AppointingBodyId = model.AppointingBodyId; viewModel.AppointmentEndDate = new DateTimeViewModel(model.AppointmentEndDate); viewModel.AppointmentStartDate = new DateTimeViewModel(model.AppointmentStartDate); viewModel.DOB = new DateTimeViewModel(model.DOB); viewModel.EmailAddress = model.EmailAddress; viewModel.GovernorTitleId = model.Person_TitleId; viewModel.FirstName = model.Person_FirstName; viewModel.MiddleName = model.Person_MiddleName; viewModel.LastName = model.Person_LastName; viewModel.PreviousTitleId = model.PreviousPerson_TitleId; viewModel.PreviousFirstName = model.PreviousPerson_FirstName; viewModel.PreviousMiddleName = model.PreviousPerson_MiddleName; viewModel.PreviousLastName = model.PreviousPerson_LastName; viewModel.TelephoneNumber = model.TelephoneNumber; viewModel.PostCode = model.PostCode; viewModel.EstablishmentUrn = model.EstablishmentUrn; viewModel.GroupUId = model.GroupUId; viewModel.SelectedPreviousGovernorId = model.Id; }
public async Task <ActionResult> AddEditOrReplace(CreateEditGovernorViewModel viewModel) { await PopulateSelectLists(viewModel); viewModel.DisplayPolicy = await _governorsReadService.GetEditorDisplayPolicyAsync(viewModel.GovernorRole, viewModel.GroupUId.HasValue, User); var governorModel = new GovernorModel { AppointingBodyId = viewModel.AppointingBodyId, AppointmentEndDate = viewModel.AppointmentEndDate.ToDateTime(), AppointmentStartDate = viewModel.Mode == CreateEditGovernorViewModel.EditMode.Replace ? viewModel.ReplaceGovernorViewModel.AppointmentEndDate.ToDateTime()?.AddDays(1) : viewModel.AppointmentStartDate.ToDateTime(), DOB = viewModel.DOB.ToDateTime(), EmailAddress = viewModel.EmailAddress, GroupUId = viewModel.GroupUId, EstablishmentUrn = viewModel.EstablishmentUrn, Id = viewModel.GID, Person_FirstName = viewModel.FirstName, Person_MiddleName = viewModel.MiddleName, Person_LastName = viewModel.LastName, Person_TitleId = viewModel.GovernorTitleId, PreviousPerson_FirstName = viewModel.PreviousFirstName, PreviousPerson_MiddleName = viewModel.PreviousMiddleName, PreviousPerson_LastName = viewModel.PreviousLastName, PreviousPerson_TitleId = viewModel.PreviousTitleId, PostCode = viewModel.PostCode, RoleId = (int)viewModel.GovernorRole, TelephoneNumber = viewModel.TelephoneNumber }; var validationResults = await _governorsWriteService.ValidateAsync(governorModel, User); validationResults.ApplyToModelState(ControllerContext, true); if (ModelState.IsValid) { if (!viewModel.EstablishmentUrn.HasValue && !viewModel.GID.HasValue && EnumSets.eSharedGovernorRoles.Contains(viewModel.GovernorRole)) { var existingGovernors = await _governorsReadService.GetGovernorListAsync(null, viewModel.GroupUId, User); var duplicates = existingGovernors.CurrentGovernors.Where(g => g.RoleId == (int)viewModel.GovernorRole && string.Equals($"{g.Person_TitleId} {g.Person_FirstName} {g.Person_MiddleName} {g.Person_LastName}", $"{viewModel.GovernorTitleId} {viewModel.FirstName} {viewModel.MiddleName} {viewModel.LastName}", StringComparison.OrdinalIgnoreCase)); if (duplicates.Any()) { ModelState.Clear(); return(RedirectToRoute("GroupEditGovernance", new { groupUId = viewModel.GroupUId, duplicateGovernorId = duplicates.First().Id })); } } GovernorModel oldGovernorModel = null; if (viewModel.ReinstateAsGovernor && (viewModel.ReplaceGovernorViewModel?.GID.HasValue).GetValueOrDefault()) { oldGovernorModel = await _governorsReadService.GetGovernorAsync(viewModel.ReplaceGovernorViewModel.GID.Value, User); } var response = await _governorsWriteService.SaveAsync(governorModel, User); if (response.Success) { viewModel.GID = response.Response; ModelState.Clear(); if (viewModel.SelectedPreviousGovernorId.HasValue) { await RetireGovernorAsync(viewModel.SelectedPreviousGovernorId.Value, viewModel.ReplaceGovernorViewModel.AppointmentEndDate.ToDateTime().GetValueOrDefault()); if (viewModel.ReinstateAsGovernor && viewModel.ReplaceGovernorViewModel.GID.HasValue) { await ReInstateChairAsNonChairAsync(viewModel.ReplaceGovernorViewModel.GID.Value, governorModel.AppointmentStartDate.GetValueOrDefault(), (oldGovernorModel?.AppointmentEndDate).GetValueOrDefault(), viewModel.GovernorRole); } } var url = viewModel.EstablishmentUrn.HasValue ? $"{Url.RouteUrl("EstabDetails", new { id = viewModel.EstablishmentUrn, saved = true })}#school-governance" : $"{Url.RouteUrl("GroupDetails", new { id = viewModel.GroupUId, saved = true })}#governance"; return(Redirect(url)); } ErrorsToModelState <GovernorModel>(response.Errors); } await _layoutHelper.PopulateLayoutProperties(viewModel, viewModel.EstablishmentUrn, viewModel.GroupUId, User); return(View(viewModel)); }
public async Task <ActionResult> AddEditOrReplace(int?groupUId, int?establishmentUrn, eLookupGovernorRole?role, int?gid) { var replaceGovernorState = new { ReplacementGovernorId = Request.QueryString["gid2"].ToInteger(), AppointmentEndDateDay = Request.QueryString["d"].ToInteger(), AppointmentEndDateMonth = Request.QueryString["m"].ToInteger(), AppointmentEndDateYear = Request.QueryString["y"].ToInteger(), Reinstate = Request.QueryString["rag"] == "true" }; var replaceMode = ((Route)ControllerContext.RouteData.Route).Url.IndexOf("/Replace/", StringComparison.OrdinalIgnoreCase) > -1; if (role == null && gid == null) { throw new EdubaseException("Role was not supplied and no Governor ID was supplied"); } if (role.HasValue) { if (!await RoleAllowed(role.Value, groupUId, establishmentUrn, User)) { return(RedirectToRoute(establishmentUrn.HasValue ? "EstabEditGovernance" : "GroupEditGovernance", new { establishmentUrn, groupUId, roleAlreadyExists = true })); } if (establishmentUrn.HasValue && EnumSets.eSharedGovernorRoles.Contains(role.Value)) { return(RedirectToRoute("SelectSharedGovernor", new { establishmentUrn = establishmentUrn.Value, role = role.Value })); } } var viewModel = new CreateEditGovernorViewModel { GroupUId = groupUId, EstablishmentUrn = establishmentUrn, Mode = CreateEditGovernorViewModel.EditMode.Create }; if (gid.HasValue) { var model = await _governorsReadService.GetGovernorAsync(gid.Value, User); role = (eLookupGovernorRole)model.RoleId.Value; if (replaceMode) { viewModel.Mode = CreateEditGovernorViewModel.EditMode.Replace; viewModel.ReplaceGovernorViewModel.AppointmentEndDate = new DateTimeViewModel(model.AppointmentEndDate); viewModel.ReplaceGovernorViewModel.GID = gid; viewModel.ReplaceGovernorViewModel.Name = model.GetFullName(); if (establishmentUrn.HasValue && role.OneOfThese(eLookupGovernorRole.ChairOfTrustees, eLookupGovernorRole.ChairOfGovernors)) { var models = await _governorsReadService.GetGovernorListAsync(establishmentUrn, principal : User); var governorsOrTrustees = models.CurrentGovernors .Where(x => x.RoleId == (int)eLookupGovernorRole.Governor || x.RoleId == (int)eLookupGovernorRole.Trustee).OrderBy(x => x.Person_LastName).ToArray(); if (replaceGovernorState.ReplacementGovernorId.HasValue) { viewModel.SelectedGovernor = governorsOrTrustees.FirstOrDefault(x => x.Id == replaceGovernorState.ReplacementGovernorId); PrepopulateFields(viewModel.SelectedGovernor, viewModel); } viewModel.ExistingGovernors = governorsOrTrustees.Select(x => new SelectListItem { Text = x.Person_FirstName + " " + x.Person_LastName, Value = x.Id.ToString(), Selected = replaceGovernorState.ReplacementGovernorId.HasValue && replaceGovernorState.ReplacementGovernorId.Value == x.Id }); } } else { viewModel.Mode = CreateEditGovernorViewModel.EditMode.Edit; viewModel.AppointingBodyId = model.AppointingBodyId; viewModel.AppointmentEndDate = new DateTimeViewModel(model.AppointmentEndDate); viewModel.AppointmentStartDate = new DateTimeViewModel(model.AppointmentStartDate); viewModel.DOB = new DateTimeViewModel(model.DOB); viewModel.EmailAddress = model.EmailAddress; viewModel.GovernorTitleId = model.Person_TitleId; viewModel.FirstName = model.Person_FirstName; viewModel.MiddleName = model.Person_MiddleName; viewModel.LastName = model.Person_LastName; viewModel.PreviousTitleId = model.PreviousPerson_TitleId; viewModel.PreviousFirstName = model.PreviousPerson_FirstName; viewModel.PreviousMiddleName = model.PreviousPerson_MiddleName; viewModel.PreviousLastName = model.PreviousPerson_LastName; viewModel.GID = model.Id; viewModel.TelephoneNumber = model.TelephoneNumber; viewModel.PostCode = model.PostCode; viewModel.EstablishmentUrn = model.EstablishmentUrn; viewModel.GroupUId = model.GroupUId; viewModel.IsHistoric = model.AppointmentEndDate.HasValue && model.AppointmentEndDate.Value < DateTime.Now.Date; } } await _layoutHelper.PopulateLayoutProperties(viewModel, establishmentUrn, groupUId, User); viewModel.GovernorRoleName = _nomenclatureService.GetGovernorRoleName(role.Value); viewModel.GovernorRole = role.Value; await PopulateSelectLists(viewModel); viewModel.DisplayPolicy = await _governorsReadService.GetEditorDisplayPolicyAsync(role.Value, groupUId.HasValue, User); ModelState.Clear(); if (replaceGovernorState.AppointmentEndDateDay.HasValue) { viewModel.ReinstateAsGovernor = replaceGovernorState.Reinstate; viewModel.ReplaceGovernorViewModel.AppointmentEndDate.Day = replaceGovernorState.AppointmentEndDateDay; viewModel.ReplaceGovernorViewModel.AppointmentEndDate.Month = replaceGovernorState.AppointmentEndDateMonth; viewModel.ReplaceGovernorViewModel.AppointmentEndDate.Year = replaceGovernorState.AppointmentEndDateYear; } return(View(viewModel)); }
public async Task <ActionResult> AddEditOrReplace(int?groupUId, int?establishmentUrn, eLookupGovernorRole?role, int?gid) { var replaceMode = (ControllerContext.RouteData.Route as System.Web.Routing.Route).Url.IndexOf("/Replace/", StringComparison.OrdinalIgnoreCase) > -1; if (role == null && gid == null) { throw new EdubaseException("Role was not supplied and no Governor ID was supplied"); } if (role.HasValue) { if (!await RoleAllowed(role.Value, groupUId, establishmentUrn, User)) { return(RedirectToRoute(establishmentUrn.HasValue ? "EstabEditGovernance" : "GroupEditGovernance", new { establishmentUrn, groupUId, roleAlreadyExists = true })); } if (establishmentUrn.HasValue && EnumSets.eSharedGovernorRoles.Contains(role.Value)) { return(RedirectToRoute("SelectSharedGovernor", new { establishmentUrn = establishmentUrn.Value, role = role.Value })); } } var viewModel = new CreateEditGovernorViewModel { GroupUId = groupUId, EstablishmentUrn = establishmentUrn, Mode = CreateEditGovernorViewModel.EditMode.Create }; if (gid.HasValue) { var model = await _governorsReadService.GetGovernorAsync(gid.Value, User); role = (eLookupGovernorRole)model.RoleId.Value; if (replaceMode) { viewModel.Mode = CreateEditGovernorViewModel.EditMode.Replace; viewModel.ReplaceGovernorViewModel.AppointmentEndDate = new DateTimeViewModel(model.AppointmentEndDate); viewModel.ReplaceGovernorViewModel.GID = gid; viewModel.ReplaceGovernorViewModel.Name = model.GetFullName(); } else { viewModel.Mode = CreateEditGovernorViewModel.EditMode.Edit; viewModel.AppointingBodyId = model.AppointingBodyId; viewModel.AppointmentEndDate = new DateTimeViewModel(model.AppointmentEndDate); viewModel.AppointmentStartDate = new DateTimeViewModel(model.AppointmentStartDate); viewModel.DOB = new DateTimeViewModel(model.DOB); viewModel.EmailAddress = model.EmailAddress; viewModel.GovernorTitleId = model.Person_TitleId; viewModel.FirstName = model.Person_FirstName; viewModel.MiddleName = model.Person_MiddleName; viewModel.LastName = model.Person_LastName; viewModel.PreviousTitleId = model.PreviousPerson_TitleId; viewModel.PreviousFirstName = model.PreviousPerson_FirstName; viewModel.PreviousMiddleName = model.PreviousPerson_MiddleName; viewModel.PreviousLastName = model.PreviousPerson_LastName; viewModel.GID = model.Id; viewModel.TelephoneNumber = model.TelephoneNumber; viewModel.PostCode = model.PostCode; viewModel.EstablishmentUrn = model.EstablishmentUrn; viewModel.GroupUId = model.GroupUId; viewModel.IsHistoric = model.AppointmentEndDate.HasValue && model.AppointmentEndDate.Value < DateTime.Now.Date; } } await _layoutHelper.PopulateLayoutProperties(viewModel, establishmentUrn, groupUId, User); viewModel.GovernorRoleName = _nomenclatureService.GetGovernorRoleName(role.Value); viewModel.GovernorRole = role.Value; await PopulateSelectLists(viewModel); viewModel.DisplayPolicy = await _governorsReadService.GetEditorDisplayPolicyAsync(role.Value, groupUId.HasValue, User); ModelState.Clear(); return(View(viewModel)); }