示例#1
0
 private async Task PopulateLists(ChangeHistoryViewModel model)
 {
     model.SuggesterGroups = (await _svc.GetSuggesterGroupsAsync(User)).OrderBy(s => s.Name).Select(s => new SelectListItem {
         Text = s.Name, Value = s.Code
     });
     model.ApproverGroups      = model.SuggesterGroups;
     model.EstablishmentTypes  = (await _lookupService.EstablishmentTypesGetAllAsync()).OrderBy(e => e.Name).Select(e => new LookupItemViewModel(e));
     model.GroupTypes          = (await _lookupService.GroupTypesGetAllAsync()).OrderBy(g => g.Name).Select(g => new LookupItemViewModel(g));
     model.EstablishmentFields = (await _svc.GetEstablishmentFieldsAsync(User)).OrderBy(e => e.Text).Select(s => new StringLookupItemViewModel(s.Key, s.Text));
 }
示例#2
0
        public async Task <ActionResult> JsonResults(EstablishmentSearchViewModel model)
        {
            var payload = await GetEstablishmentSearchPayload(model);

            //payload.Object.Skip = 0;
            payload.Object.Take = 100;

            if (!payload.Success)
            {
                model.Error = payload.ErrorMessage;
            }
            await ProcessEstablishmentsSearch(model, payload.Object);

            var localAuthorities   = (await _lookupService.LocalAuthorityGetAllAsync());
            var establishmentTypes = await _lookupService.EstablishmentTypesGetAllAsync();

            var educationPhases = await _lookupService.EducationPhasesGetAllAsync();

            var counties = (await _lookupService.CountiesGetAllAsync()).Where(c => c.Id != 63); //remove "not recorded"

            HttpContext.Response.Headers.Add("x-count", model.Count.ToString());

            var filtered = model.Results
                           .Select(result => new { Result = result, LA = localAuthorities.SingleOrDefault(la => la.Id == result.LocalAuthorityId) })
                           .Select(a => new
            {
                Name     = a.Result.Name,
                Location = a.Result.Location,
                Address  = StringUtil.ConcatNonEmpties(", ", a.Result.Address_Line1,
                                                       a.Result.Address_Locality,
                                                       a.Result.Address_Line3,
                                                       a.Result.Address_CityOrTown,
                                                       counties.FirstOrDefault(c => c.Id == a.Result.Address_CountyId)?.Name,
                                                       a.Result.Address_PostCode),
                Urn            = a.Result.Urn,
                LAESTAB        = a.LA?.Code != null && a.Result.EstablishmentNumber.HasValue ? $"{a.LA.Code}/{a.Result.EstablishmentNumber.Value:D4}" : string.Empty,
                Status         = model.EstablishmentStatuses.FirstOrDefault(x => x.Id == a.Result.StatusId)?.Name ?? "Not recorded",
                LocalAuthority = a.LA?.Name ?? "Not recorded",
                PhaseType      = string.Concat(educationPhases.FirstOrDefault(x => x.Id == a.Result.EducationPhaseId)?.Name ?? "Not recorded", ", ", establishmentTypes.FirstOrDefault(x => x.Id == a.Result.TypeId)?.Name ?? "Not recorded"),
            });

            return(Json(filtered));
        }
示例#3
0
        public async Task <dynamic> GetListAsync(DateTime from, DateTime to, int skip, int take)
        {
            var estabTypes = await _lookupService.EstablishmentTypesGetAllAsync();

            var apiResult = (await _establishmentReadService.SearchAsync(new EstablishmentSearchPayload
            {
                Skip = skip,
                Take = take,
                SortBy = eSortBy.NameAlphabeticalAZ,
                Filters = new EstablishmentSearchFilters
                {
                    OpenDateMin = from,
                    OpenDateMax = to,
                    EstablishmentTypeGroupIds = new[] { (int)eLookupEstablishmentTypeGroup.Academies },
                    StatusIds = new[] { (int)eLookupEstablishmentStatus.ProposedToOpen }
                },
                Select = new List <string>
                {
                    nameof(M.Name),
                    nameof(M.Urn),
                    nameof(M.TypeId),
                    nameof(M.OpenDate),
                    nameof(M.PredecessorName),
                    nameof(M.PredecessorUrn)
                }
            }, User));


            return(new
            {
                Items = apiResult.Items.Select(x => new
                {
                    x.Urn,
                    x.Name,
                    EstablishmentType = x.TypeId.HasValue ? estabTypes.FirstOrDefault(t => t.Id == x.TypeId)?.Name : null,
                    OpeningDate = x.OpenDate,
                    DisplayDate = x.OpenDate?.ToString("d MMMM yyyy"),
                    x.PredecessorName,
                    x.PredecessorUrn,
                }).OrderBy(x => x.OpeningDate),
                apiResult.Count
            });
        }
示例#4
0
        public async Task <ActionResult> BulkAcademies(BulkAcademiesViewModel model, int?removeUrn, int?editUrn, string action)
        {
            var establishmentTypeFullList = (await _lookup.EstablishmentTypesGetAllAsync()).ToList();

            model.ItemTypes = establishmentTypeFullList.ToSelectList();
            EstablishmentModel est = null;

            SelectListItem[] filteredItems = null;

            // validation
            if (action == "search")
            {
                if (model.SearchUrn == null)
                {
                    if (ModelState.ContainsKey(nameof(model.SearchUrn)) &&
                        ModelState[nameof(model.SearchUrn)].Errors.Any())
                    {
                        // remove the existing error, as we'll add a fresh one
                        ModelState[nameof(model.SearchUrn)].Errors.Clear();
                    }

                    ModelState.AddModelError(nameof(model.SearchUrn), "Please enter a valid URN");
                }
                else if (model.ItemsToAdd?.Any(x => x.Urn == model.SearchUrn) == true)
                {
                    ModelState.AddModelError(nameof(model.SearchUrn), "URN is a duplicate");
                }
                else
                {
                    var estCall = await _establishmentReadService.GetAsync((int)model.SearchUrn, User);

                    est = estCall.GetResult();
                    if (est == null)
                    {
                        ModelState.AddModelError(nameof(model.SearchUrn), "Please enter a valid URN");
                    }
                    else
                    {
                        filteredItems = (await GetFilteredBulkAcademyTypes((int)est.Urn, establishmentTypeFullList)).ToSelectList(est?.TypeId)?.ToArray();
                        if (filteredItems?.Length == 0)
                        {
                            ModelState.AddModelError(nameof(model.SearchUrn), "Please enter a valid URN");
                        }
                    }
                }
            }

            if (action == "add")
            {
                model.FilteredItemTypes = (await GetFilteredBulkAcademyTypes(model.FoundItem.Urn ?? 0, establishmentTypeFullList)).ToSelectList(model.FoundItem.EstablishmentTypeId);

                if (model.FoundItem.EstablishmentTypeId == null)
                {
                    ModelState.AddModelError(nameof(model.FilteredItemTypes), "Please select an establishment type");
                }
            }

            // cancel either an original addition or an edit
            if (action == "cancel")
            {
                model.SearchUrn = null;
                model.FoundItem = null;
                ModelState.Clear();
                return(View(model));
            }

            if (!ModelState.IsValid)
            {
                return(View(model));
            }
            ModelState.Clear();

            // remove
            if (removeUrn != null)
            {
                model.ItemsToAdd?.RemoveAll(x => x.Urn == removeUrn);
                return(View(model));
            }

            // edit/find
            if (editUrn != null || model.SearchUrn != null)
            {
                if (editUrn != null)
                {
                    var itm = model.ItemsToAdd?.First(x => x.Urn == editUrn);
                    model.FilteredItemTypes = (await GetFilteredBulkAcademyTypes((int)editUrn, establishmentTypeFullList)).ToSelectList(itm?.EstablishmentTypeId);
                    model.FoundItem         = itm;
                    ViewBag.ButtonText      = "Update establishment";
                }
                else
                {
                    model.FilteredItemTypes = filteredItems;
                    model.FoundItem         = new BulkAcademyViewModel()
                    {
                        Urn  = est.Urn,
                        Name = est.Name,
                        EstablishmentTypeId = est.TypeId,
                        OpeningDate         = DateTime.Now,
                        Address             = StringUtil.ConcatNonEmpties(", ",
                                                                          est.Address_Line1,
                                                                          est.Address_Locality,
                                                                          est.Address_Line3,
                                                                          est.Address_CityOrTown,
                                                                          est.Address_PostCode)
                    };
                }

                return(View(model));
            }

            // save/review list
            if (model.FoundItem != null)
            {
                if (model.ItemsToAdd == null)
                {
                    model.ItemsToAdd = new List <BulkAcademyViewModel>();
                }

                if (model.ItemsToAdd != null)
                {
                    model.ItemsToAdd.RemoveAll(x => x.Urn == model.FoundItem.Urn);

                    model.ItemsToAdd.Add(new BulkAcademyViewModel
                    {
                        Urn  = model.FoundItem.Urn,
                        Name = model.FoundItem.Name,
                        EstablishmentTypeId = model.FoundItem.EstablishmentTypeId,
                        OpeningDate         = model.FoundItem.OpeningDate,
                        Address             = model.FoundItem.Address
                    });
                }

                model.FoundItem = null;
                return(View(model));
            }

            // submit
            if (action == "create" && model.ItemsToAdd?.Any() == true)
            {
                var processResponse = await ProcessBulkAcademies(model.ItemsToAdd);

                model.ProgressGuid = processResponse.Item1;
                model.ItemsToAdd   = processResponse.Item2;
                model.IsComplete   = true;
            }

            return(View(model));
        }
示例#5
0
        // public ActionResult Index() => View();
        public async Task <ActionResult> ManageAcademyOpenings(int skip = 0, string sortBy = "OpenDate-desc")
        {
            int take = 50;
            var now  = DateTime.Now;

            var from = new DateTime(now.Year, now.Month, 1);

            var to = from.AddYears(30);


            var property = typeof(EditAcademyOpeningViewModel).GetProperty(sortBy);

            var estabTypes = await _lookupService.EstablishmentTypesGetAllAsync();

            var result = await(_establishmentReadService.SearchAsync(
                                   new EstablishmentSearchPayload
            {
                Skip    = skip,
                Take    = take,
                SortBy  = eSortBy.NameAlphabeticalAZ,
                Filters = new EstablishmentSearchFilters
                {
                    OpenDateMin = from,
                    OpenDateMax = to,
                    EstablishmentTypeGroupIds = new[] { (int)eLookupEstablishmentTypeGroup.Academies },
                    StatusIds = new[] { (int)eLookupEstablishmentStatus.ProposedToOpen }
                },
                Select = new List <string>
                {
                    nameof(M.Name),
                    nameof(M.Urn),
                    nameof(M.TypeId),
                    nameof(M.OpenDate),
                    nameof(M.PredecessorName),
                    nameof(M.PredecessorUrn)
                }
            }, User));


            var academyOpenings = new List <EditAcademyOpeningViewModel>();

            foreach (var x in result.Items)
            {
                int preUrn = 0;
                int.TryParse(x.PredecessorUrn, out preUrn);

                academyOpenings.Add(new EditAcademyOpeningViewModel()
                {
                    Urn = (int)x.Urn,
                    EstablishmentName = x.Name,
                    EstablishmentType =
                        x.TypeId.HasValue ? estabTypes.FirstOrDefault(t => t.Id == x.TypeId)?.Name : null,
                    OpeningDate     = new UI.Models.DateTimeViewModel(x.OpenDate),
                    OpenDate        = x.OpenDate.GetValueOrDefault(),
                    PredecessorName = x.PredecessorName,
                    PredecessorUrn  = preUrn.ToString()
                });
            }

            switch (sortBy)
            {
            case "OpenDate-desc":
                academyOpenings.Sort((x, y) => x.OpenDate.CompareTo(y.OpenDate));
                break;

            case "OpenDate-asc":
                academyOpenings.Sort((x, y) => y.OpenDate.CompareTo(x.OpenDate));
                break;

            case "Urn-asc":
                academyOpenings.Sort((x, y) => x.Urn.CompareTo(y.Urn));
                break;

            case "Urn-desc":
                academyOpenings.Sort((x, y) => y.Urn.CompareTo(x.Urn));
                break;

            case "EstablishmentName-asc":
                academyOpenings.Sort((x, y) => x.EstablishmentName.CompareTo(y.EstablishmentName));
                break;

            case "EstablishmentName-desc":
                academyOpenings.Sort((x, y) => y.EstablishmentName.CompareTo(x.EstablishmentName));
                break;

            case "EstablishmentType-asc":
                academyOpenings.Sort((x, y) => x.EstablishmentType.CompareTo(y.EstablishmentType));
                break;

            case "EstablishmentType-desc":
                academyOpenings.Sort((x, y) => y.EstablishmentType.CompareTo(x.EstablishmentType));
                break;

            case "PredecessorUrn-asc":
                academyOpenings.Sort((x, y) => x.PredecessorUrn.CompareTo(y.PredecessorUrn));
                break;

            case "PredecessorUrn-desc":
                academyOpenings.Sort((x, y) => y.PredecessorUrn.CompareTo(x.PredecessorUrn));
                break;

            case "PredecessorName-asc":
                academyOpenings.Sort(delegate(EditAcademyOpeningViewModel x, EditAcademyOpeningViewModel y)
                {
                    if (y.PredecessorName == null && y.PredecessorName == null)
                    {
                        return(0);
                    }
                    else if (x.PredecessorName == null)
                    {
                        return(1);
                    }
                    else if (y.PredecessorName == null)
                    {
                        return(-1);
                    }
                    else
                    {
                        return(x.PredecessorName.CompareTo(y.PredecessorName));
                    }
                });
                break;

            case "PredecessorName-desc":
                academyOpenings.Sort(delegate(EditAcademyOpeningViewModel x, EditAcademyOpeningViewModel y)
                {
                    if (y.PredecessorName == null && y.PredecessorName == null)
                    {
                        return(0);
                    }
                    else if (x.PredecessorName == null)
                    {
                        return(-1);
                    }
                    else if (y.PredecessorName == null)
                    {
                        return(1);
                    }
                    else
                    {
                        return(y.PredecessorName.CompareTo(x.PredecessorName));
                    }
                });
                break;
            }


            var vm = new ManageAcademyOpeningsViewModel()
            {
                AcademyOpenings =
                    new PaginatedResult <EditAcademyOpeningViewModel>(skip, take, result.Count, academyOpenings),
                Items = academyOpenings
            };

            vm.Count = result.Count;
            vm.Skip  = skip;
            return(View("Index", vm));
        }
        private async Task <IEnumerable <int> > GetEstablishmentTypes()
        {
            var types = await _lookupService.EstablishmentTypesGetAllAsync();

            return(types.OrderBy(x => x.Id).Select(x => x.Id));
        }
示例#7
0
        public async Task <ActionResult> ProcessMergeEstablishmentsAsync(MergeEstablishmentsModel model)
        {
            var viewModel     = new MergeEstablishmentsModel();
            var submittedUrns = new List <int>();

            var leadHasErrors = ModelState.Keys.Where(k => k == "LeadEstablishmentUrn")
                                .Select(k => ModelState[k].Errors).First().Select(e => e.ErrorMessage).Any();

            var estab1HasErrors = ModelState.Keys.Where(k => k == "Establishment1Urn")
                                  .Select(k => ModelState[k].Errors).First().Select(e => e.ErrorMessage).Any();

            if (!model.LeadEstablishmentUrn.HasValue && !leadHasErrors)
            {
                ModelState.AddModelError(nameof(model.LeadEstablishmentUrn), "Enter the lead establishment URN");
            }

            if (!model.Establishment1Urn.HasValue && !model.Establishment2Urn.HasValue &&
                !model.Establishment3Urn.HasValue && !estab1HasErrors)
            {
                ModelState.AddModelError(nameof(model.Establishment1Urn), "Enter the establishment 1 URN");
            }

            // validate the establishments exist
            if (model.LeadEstablishmentUrn.HasValue)
            {
                var leadEstab =
                    await _establishmentReadService.GetAsync(model.LeadEstablishmentUrn.GetValueOrDefault(), User);

                if (leadEstab.GetResult() == null)
                {
                    ViewData.ModelState.AddModelError(nameof(model.LeadEstablishmentUrn),
                                                      "The lead establishment URN is invalid");
                }
                else
                {
                    var estabTypes = await _lookupService.EstablishmentTypesGetAllAsync();

                    viewModel.LeadEstablishmentUrn  = model.LeadEstablishmentUrn;
                    viewModel.LeadEstablishmentName = leadEstab.GetResult().Name;
                    viewModel.EstablishmentType     =
                        estabTypes.FirstOrDefault(t => t.Id == leadEstab.GetResult().TypeId)?.Name;
                    submittedUrns.Add(model.LeadEstablishmentUrn.GetValueOrDefault());
                }
            }

            if (model.Establishment1Urn.HasValue)
            {
                var estab1 =
                    await _establishmentReadService.GetAsync(model.Establishment1Urn.GetValueOrDefault(), User);

                var hasErrors = ModelState.Keys.Where(k => k == "Establishment1Urn")
                                .Select(k => ModelState[k].Errors).First().Select(e => e.ErrorMessage).Any();

                if (estab1.GetResult() == null)
                {
                    if (!hasErrors)
                    {
                        ViewData.ModelState.AddModelError(nameof(model.Establishment1Urn),
                                                          "The establishment 1 URN is invalid");
                    }
                }
                else
                {
                    viewModel.Establishment1Urn  = model.Establishment1Urn;
                    viewModel.Establishment1Name = estab1.GetResult().Name;
                    submittedUrns.Add(model.Establishment1Urn.GetValueOrDefault());
                }
            }

            if (model.Establishment2Urn.HasValue)
            {
                var estab2 =
                    await _establishmentReadService.GetAsync(model.Establishment2Urn.GetValueOrDefault(), User);

                var hasErrors = ModelState.Keys.Where(k => k == "Establishment2Urn")
                                .Select(k => ModelState[k].Errors).First().Select(e => e.ErrorMessage).Any();

                if (estab2.GetResult() == null)
                {
                    if (!hasErrors)
                    {
                        ViewData.ModelState.AddModelError(nameof(model.Establishment2Urn),
                                                          "The establishment 2 URN is invalid");
                    }
                }
                else
                {
                    viewModel.Establishment2Urn  = model.Establishment2Urn;
                    viewModel.Establishment2Name = estab2.GetResult().Name;
                    submittedUrns.Add(model.Establishment2Urn.GetValueOrDefault());
                }
            }

            if (model.Establishment3Urn.HasValue)
            {
                var estab3 =
                    await _establishmentReadService.GetAsync(model.Establishment3Urn.GetValueOrDefault(), User);

                var hasErrors = ModelState.Keys.Where(k => k == "Establishment3Urn")
                                .Select(k => ModelState[k].Errors).First().Select(e => e.ErrorMessage).Any();

                if (estab3.GetResult() == null)
                {
                    if (!hasErrors)
                    {
                        ViewData.ModelState.AddModelError(nameof(model.Establishment3Urn),
                                                          "The establishment 3 URN is invalid");
                    }
                }
                else
                {
                    viewModel.Establishment3Urn  = model.Establishment3Urn;
                    viewModel.Establishment3Name = estab3.GetResult().Name;
                    submittedUrns.Add(model.Establishment3Urn.GetValueOrDefault());
                }
            }

            var duplicates = submittedUrns.GroupBy(x => x)
                             .Where(g => g.Count() > 1)
                             .ToDictionary(x => x.Key, x => x.Count());


            if (duplicates.ContainsKey(model.LeadEstablishmentUrn.GetValueOrDefault()))
            {
                ViewData.ModelState.AddModelError("LeadEstablishmentUrn", "Duplicate URN. Please correct the URN.");
            }

            if (duplicates.ContainsKey(model.Establishment1Urn.GetValueOrDefault()))
            {
                ViewData.ModelState.AddModelError("Establishment1Urn", "Duplicate URN. Please correct the URN.");
            }

            if (duplicates.ContainsKey(model.Establishment2Urn.GetValueOrDefault()))
            {
                ViewData.ModelState.AddModelError("Establishment2Urn", "Duplicate URN. Please correct the URN.");
            }

            if (duplicates.ContainsKey(model.Establishment3Urn.GetValueOrDefault()))
            {
                ViewData.ModelState.AddModelError("Establishment3Urn", "Duplicate URN. Please correct the URN.");
            }


            if (!ModelState.IsValid)
            {
                return(View("~/Views/Tools/Mergers/MergeEstablishments.cshtml", model));
            }

            return(View("~/Views/Tools/Mergers/ConfirmMerger.cshtml", viewModel));
        }