Beispiel #1
0
        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));
        }
        private void CreateGrids(GovernorsDetailsDto dto, IEnumerable <GovernorModel> governors, bool isHistoric, int?groupUid, int?establishmentUrn)
        {
            var roles = dto.ApplicableRoles.Where(role => !EnumSets.eSharedGovernorRoles.Contains(role)
                                                  ||
                                                  (RoleEquivalence.GetLocalEquivalentToSharedRole(role) != null &&
                                                   !dto.ApplicableRoles.Contains(RoleEquivalence.GetLocalEquivalentToSharedRole(role).Value)));

            foreach (var role in roles)
            {
                var equivalantRoles = RoleEquivalence.GetEquivalentToLocalRole(role).Cast <int>().ToList();
                var pluralise       = !EnumSets.eSingularGovernorRoles.Contains(role);


                var grid = new GovernorGridViewModel($"{_nomenclatureService.GetGovernorRoleName(role, eTextCase.SentenceCase, pluralise)}{(isHistoric ? " (in past 12 months)" : string.Empty)}")
                {
                    Tag              = isHistoric ? "historic" : "current",
                    Role             = role,
                    IsSharedRole     = EnumSets.eSharedGovernorRoles.Contains(role),
                    GroupUid         = groupUid,
                    EstablishmentUrn = establishmentUrn,
                    IsHistoricRole   = isHistoric,
                    RoleName         = _nomenclatureService.GetGovernorRoleName(role)
                };

                var displayPolicy = dto.RoleDisplayPolicies.Get(role);
                Guard.IsNotNull(displayPolicy, () => new Exception($"The display policy should not be null for the role '{role}'"));
                bool includeEndDate = ((isHistoric && role == GR.Member || role != GR.Member) && displayPolicy.AppointmentEndDate) ||
                                      (role.OneOfThese(GR.ChiefFinancialOfficer, GR.AccountingOfficer) && isHistoric);

                SetupHeader(role, grid, displayPolicy, includeEndDate);

                var list = governors.Where(x => x.RoleId.HasValue && equivalantRoles.Contains(x.RoleId.Value));
                foreach (var governor in list)
                {
                    var isShared       = governor.RoleId.HasValue && EnumSets.SharedGovernorRoles.Contains(governor.RoleId.Value);
                    var establishments = string.Join(", ",
                                                     governor.Appointments?.Select(a => $"{a.EstablishmentName}, URN: {a.EstablishmentUrn}") ??
                                                     new string[] { });
                    var appointment = governor.Appointments?.SingleOrDefault(a => a.EstablishmentUrn == EstablishmentUrn);
                    var startDate   = (isShared && appointment != null) ? appointment.AppointmentStartDate : governor.AppointmentStartDate;
                    var endDate     = (isShared && appointment != null) ? appointment.AppointmentEndDate : governor.AppointmentEndDate;

                    var row = grid.AddRow(governor, endDate)
                              .AddCell(governor.GetFullName(), displayPolicy.FullName)
                              .AddCell(string.IsNullOrWhiteSpace(establishments) ? null : establishments, role.OneOfThese(GR.LocalGovernor, GR.ChairOfLocalGoverningBody))
                              .AddCell(governor.Id, displayPolicy.Id)
                              .AddCell(AppointingBodies.FirstOrDefault(x => x.Id == governor.AppointingBodyId)?.Name, displayPolicy.AppointingBodyId)
                              .AddCell(startDate?.ToString("dd/MM/yyyy"), displayPolicy.AppointmentStartDate)
                              .AddCell(endDate?.ToString("dd/MM/yyyy"), includeEndDate)
                              .AddCell(governor.PostCode, displayPolicy.PostCode)
                              .AddCell(governor.DOB?.ToString("dd/MM/yyyy"), displayPolicy.DOB)
                              .AddCell(governor.GetPreviousFullName(), displayPolicy.PreviousFullName)
                              .AddCell(governor.EmailAddress, displayPolicy.EmailAddress)
                              .AddCell(governor.TelephoneNumber, displayPolicy.TelephoneNumber);

                    if (isHistoric)
                    {
                        var gov = new HistoricGovernorViewModel
                        {
                            AppointingBodyId     = governor.AppointingBodyId,
                            AppointingBody       = AppointingBodies.FirstOrDefault(x => x.Id == governor.AppointingBodyId)?.Name,
                            AppointmentEndDate   = new DateTimeViewModel(governor.AppointmentEndDate),
                            AppointmentStartDate = new DateTimeViewModel(governor.AppointmentStartDate),
                            FullName             = governor.GetFullName(),
                            RoleName             = _nomenclatureService.GetGovernorRoleName(role)
                        };

                        HistoricGovernors.Add(gov);
                    }
                }

                grid.Rows         = grid.Rows.OrderByDescending(x => x.SortValue).ThenBy(x => x.Model.GetFullName()).ToList();
                HistoricGovernors = HistoricGovernors.OrderByDescending(x => x.AppointmentEndDate.ToDateTime()).ThenBy(x => x.FullName).ToList();

                if (isHistoric)
                {
                    HistoricGrids.Add(grid);
                }
                else
                {
                    Grids.Add(grid);
                }
            }
        }
        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));
        }