Exemplo n.º 1
0
        public async Task <ActionResult> JsonResults(EstablishmentSearchViewModel model)
        {
            var payload = await GetEstablishmentSearchPayload(model);

            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));
        }
Exemplo n.º 2
0
        public async Task <ActionResult> MergersTool()
        {
            var settings = new JsonSerializerSettings {
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            };

            var type2PhaseMap     = _establishmentReadService.GetEstabType2EducationPhaseMap().AsInts();
            var type2PhaseMapJson = JsonConvert.SerializeObject(type2PhaseMap, Formatting.None, settings);

            var las     = (await _lookup.LocalAuthorityGetAllAsync()).Select(x => new { x.Id, x.Name });
            var lasJson = JsonConvert.SerializeObject(las, Formatting.None, settings);

            var phases     = (await _lookup.EducationPhasesGetAllAsync()).Select(x => new { x.Id, x.Name });
            var phasesJson = JsonConvert.SerializeObject(phases, Formatting.None, settings);

            var types     = (await _lookup.EstablishmentTypesGetAllAsync()).Select(x => new { x.Id, x.Name });
            var typesJson = JsonConvert.SerializeObject(types, Formatting.None, settings);

            ViewBag.Type2PhaseMapJson    = type2PhaseMapJson;
            ViewBag.LocalAuthoritiesJson = lasJson;
            ViewBag.TypesJson            = typesJson;
            ViewBag.PhasesJson           = phasesJson;

            return(View());
        }
Exemplo n.º 3
0
        public async Task <ActionResult> ProcessAmalgamationEstablishmentsAsync(AmalgamateEstablishmentsModel model)
        {
            var viewModel = new AmalgamateEstablishmentsModel();

            var submittedUrns   = new List <int>();
            var estab0HasErrors = ModelState.Keys.Where(k => k == "Establishment0Urn")
                                  .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.Establishment0Urn.HasValue && !estab0HasErrors)
            {
                ModelState.AddModelError(nameof(model.Establishment0Urn), "Enter the establishment 1 URN");
            }

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

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

                if (estab.GetResult() == null && !estab1HasErrors)
                {
                    ViewData.ModelState.AddModelError(nameof(model.Establishment0Urn),
                                                      "The establishment 1 URN is invalid");
                }
                else
                {
                    viewModel.Establishment0Urn  = model.Establishment0Urn;
                    viewModel.Establishment0Name = estab.GetResult().Name;

                    submittedUrns.Add(model.Establishment0Urn.GetValueOrDefault());
                }
            }

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

                if (estab1.GetResult() == null)
                {
                    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);

                if (estab2.GetResult() == null)
                {
                    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);

                if (estab3.GetResult() == null)
                {
                    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.Establishment0Urn.GetValueOrDefault()))
            {
                ViewData.ModelState.AddModelError("Establishment0Urn", "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/AmalgamateEstablishments.cshtml", model));
            }

            viewModel.EstablishmentPhases = (await _lookupService.EducationPhasesGetAllAsync()).ToSelectList();

            viewModel.EstablishmentTypes = (await _lookupService.EstablishmentTypesGetAllAsync()).ToSelectList();

            viewModel.LocalAuthorities = (await _lookupService.LocalAuthorityGetAllAsync()).ToSelectList();

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