private decimal?CalculateAmountPerUnit(SchoolFinancialDataModel dataModel, string fieldName, UnitType unit, decimal total) { var rawAmount = dataModel.GetDecimal(fieldName); var pupilCount = dataModel.PupilCount; var teacherCount = dataModel.TeacherCount; if (rawAmount == null) { return(null); } switch (unit) { case UnitType.AbsoluteMoney: return(rawAmount); case UnitType.PerTeacher: return((teacherCount == 0) ? null : (rawAmount / (decimal)teacherCount)); case UnitType.PerPupil: return((pupilCount == 0) ? null : (rawAmount / (decimal)pupilCount)); case UnitType.PercentageOfTotal: return((total == 0) ? 0 : ((rawAmount / total) * 100)); default: return(rawAmount); } }
public BenchmarkCriteria BuildFromSimpleComparisonCriteria(SchoolFinancialDataModel benchmarkSchoolData, bool includeFsm, bool includeSen, bool includeEal, bool includeLa, int percentageMargin = 0) { var criteria = new BenchmarkCriteria(); criteria.SchoolOverallPhase = new [] { benchmarkSchoolData.SchoolOverallPhase }; criteria.UrbanRural = new [] { benchmarkSchoolData.UrbanRural }; var minMarginFactor = 1 - ((percentageMargin + CriteriaSearchConfig.DEFAULT_MARGIN) / 100d); var maxMarginFactor = 1 + ((percentageMargin + CriteriaSearchConfig.DEFAULT_MARGIN) / 100d); criteria.MinNoPupil = benchmarkSchoolData.PupilCount * minMarginFactor; criteria.MaxNoPupil = benchmarkSchoolData.PupilCount * maxMarginFactor; if (includeFsm) { var fsm = double.Parse(benchmarkSchoolData.PercentageOfEligibleFreeSchoolMeals); criteria.MinPerFSM = (fsm - percentageMargin) < 0 ? 0 : (fsm - percentageMargin); criteria.MaxPerFSM = fsm + percentageMargin; } if (includeSen) { var sen = double.Parse(benchmarkSchoolData.PercentageOfPupilsWithSen); criteria.MinPerSEN = (sen - percentageMargin < 0) ? 0 : (sen - percentageMargin); criteria.MaxPerSEN = sen + percentageMargin; } if (includeEal) { var eal = double.Parse(benchmarkSchoolData.PercentageOfPupilsWithEal); criteria.MinPerEAL = (eal - percentageMargin) < 0 ? 0 : (eal - percentageMargin); criteria.MaxPerEAL = eal + percentageMargin; } if (includeLa) { criteria.LocalAuthorityCode = benchmarkSchoolData.LaNumber; } return(criteria); }
private decimal?CalculateWFAmount(SchoolFinancialDataModel schoolData, string fieldName, UnitType unit) { decimal?amount = null; decimal?rawAmount = null; switch (unit) { case UnitType.AbsoluteCount: amount = schoolData.GetDecimal(fieldName); break; case UnitType.NoOfPupilsPerMeasure: rawAmount = schoolData.GetDecimal(fieldName); if (rawAmount == null || rawAmount == 0) { break; } amount = (schoolData.PupilCount == 0) ? null : ((decimal)schoolData.PupilCount / rawAmount); if (amount.HasValue) { amount = decimal.Round(amount.GetValueOrDefault(), 2, MidpointRounding.AwayFromZero); } break; case UnitType.HeadcountPerFTE: string fieldNameBase = fieldName.Contains("FullTimeEquivalent") ? fieldName.Substring(0, fieldName.Length - 18) : fieldName.Substring(0, fieldName.Length - 9); var total = schoolData.GetDecimal(fieldNameBase + "Headcount").GetValueOrDefault(); rawAmount = schoolData.GetDecimal(fieldNameBase + "FullTimeEquivalent"); if (rawAmount == null) { break; } if (total == 0) { amount = 0; } else { amount = (total == 0) ? 0 : (total / rawAmount); amount = decimal.Round(amount.GetValueOrDefault(), 2, MidpointRounding.AwayFromZero); } break; case UnitType.FTERatioToTotalFTE: total = schoolData.GetDecimal("TotalSchoolWorkforceFullTimeEquivalent").GetValueOrDefault(); rawAmount = schoolData.GetDecimal(fieldName); if (rawAmount == null) { break; } if (total == 0) { amount = 0; } else { amount = (total == 0) ? 0 : (rawAmount / total) * 100; amount = decimal.Round(amount.GetValueOrDefault(), 2, MidpointRounding.AwayFromZero); } break; } return(amount); }
public async Task <ActionResult> Index( string urn, SimpleCriteria simpleCriteria, BenchmarkCriteria benchmarkCriteria, int basketSize = ComparisonListLimit.DEFAULT, SchoolFinancialDataModel benchmarkSchoolData = null, EstablishmentType searchedEstabType = EstablishmentType.All, ComparisonType comparisonType = ComparisonType.Manual, ComparisonArea areaType = ComparisonArea.All, string laCode = null, RevenueGroupType tab = RevenueGroupType.Expenditure, CentralFinancingType financing = CentralFinancingType.Include) { ChartGroupType chartGroup; switch (tab) { case RevenueGroupType.Expenditure: chartGroup = ChartGroupType.TotalExpenditure; break; case RevenueGroupType.Income: chartGroup = ChartGroupType.TotalIncome; break; case RevenueGroupType.Balance: chartGroup = ChartGroupType.InYearBalance; break; case RevenueGroupType.Workforce: chartGroup = ChartGroupType.Workforce; break; default: chartGroup = ChartGroupType.All; break; } var defaultUnitType = tab == RevenueGroupType.Workforce ? UnitType.AbsoluteCount : UnitType.AbsoluteMoney; var benchmarkCharts = await BuildSchoolBenchmarkChartsAsync(tab, chartGroup, defaultUnitType, financing); var establishmentType = DetectEstablishmentType(base.ExtractSchoolComparisonListFromCookie()); var chartGroups = _benchmarkChartBuilder.Build(tab, establishmentType).DistinctBy(c => c.ChartGroup).ToList(); string selectedArea = ""; switch (areaType) { case ComparisonArea.All: selectedArea = "All England"; break; case ComparisonArea.LaCode: case ComparisonArea.LaName: selectedArea = _laService.GetLaName(laCode); break; } string schoolArea = ""; if (benchmarkSchoolData != null) { schoolArea = _laService.GetLaName(benchmarkSchoolData.LaNumber.ToString()); } var academiesTerm = FormatHelpers.FinancialTermFormatAcademies(_financialDataService.GetLatestDataYearPerSchoolType(SchoolFinancialType.Academies)); var maintainedTerm = FormatHelpers.FinancialTermFormatMaintained(_financialDataService.GetLatestDataYearPerSchoolType(SchoolFinancialType.Maintained)); var vm = new BenchmarkChartListViewModel(benchmarkCharts, base.ExtractSchoolComparisonListFromCookie(), chartGroups, comparisonType, benchmarkCriteria, simpleCriteria, benchmarkSchoolData, establishmentType, searchedEstabType, schoolArea, selectedArea, academiesTerm, maintainedTerm, areaType, laCode, urn, basketSize); ViewBag.Tab = tab; ViewBag.ChartGroup = chartGroup; ViewBag.UnitType = defaultUnitType; ViewBag.HomeSchoolId = vm.SchoolComparisonList.HomeSchoolUrn; ViewBag.EstablishmentType = vm.EstablishmentType; ViewBag.Financing = financing; ViewBag.ChartFormat = ChartFormat.Charts; return(View("Index", vm)); }
public BenchmarkCriteria BuildFromSimpleComparisonCriteria(SchoolFinancialDataModel benchmarkSchoolData, SimpleCriteria simpleCriteria, int percentageMargin = 0) { return(BuildFromSimpleComparisonCriteria(benchmarkSchoolData, simpleCriteria.IncludeFsm.GetValueOrDefault(), simpleCriteria.IncludeSen.GetValueOrDefault(), simpleCriteria.IncludeEal.GetValueOrDefault(), simpleCriteria.IncludeLa.GetValueOrDefault(), percentageMargin)); }
public BenchmarkChartListViewModel(List <ChartViewModel> modelList, ComparisonListModel comparisonList, List <ChartViewModel> chartGroups, ComparisonType comparisonType, BenchmarkCriteria advancedCriteria, SimpleCriteria simpleCriteria, SchoolFinancialDataModel benchmarkSchoolData, EstablishmentType estabType, EstablishmentType searchedEstabType, string schoolArea, string selectedArea, string latestTermAcademies, string latestTermMaintained, ComparisonArea areaType, string laCode, string urn, int basketSize, TrustComparisonViewModel trustComparisonList = null) { base.SchoolComparisonList = comparisonList; base.ModelList = modelList; this.ChartGroups = chartGroups; this.AdvancedCriteria = advancedCriteria; this.SimpleCriteria = simpleCriteria; this.ComparisonType = comparisonType; this.BenchmarkSchoolData = benchmarkSchoolData; this.EstablishmentType = estabType; this.SearchedEstablishmentType = searchedEstabType; this.SchoolArea = schoolArea; this.SelectedArea = selectedArea; this.TrustComparisonList = trustComparisonList; this.LatestTermAcademies = latestTermAcademies; this.LatestTermMaintained = latestTermMaintained; this.AreaType = areaType; this.LaCode = laCode; this.URN = urn; this.BasketSize = basketSize; }
public async Task <ComparisonResult> GenerateBenchmarkListWithSimpleComparisonAsync( BenchmarkCriteria benchmarkCriteria, EstablishmentType estType, int basketSize, SimpleCriteria simpleCriteria, SchoolFinancialDataModel defaultSchoolFinancialDataModel) { var benchmarkSchools = await _financialDataService.SearchSchoolsByCriteriaAsync(benchmarkCriteria, estType); if (benchmarkSchools.Count > basketSize) //Original query returns more than required. Cut from top by proximity. { benchmarkSchools = benchmarkSchools.OrderBy(b => Math.Abs(b.GetPropertyValue <int>("No Pupils") - defaultSchoolFinancialDataModel.PupilCount)).Take(basketSize).ToList(); benchmarkCriteria.MinNoPupil = benchmarkSchools.Min(s => s.GetPropertyValue <int>("No Pupils")); benchmarkCriteria.MaxNoPupil = benchmarkSchools.Max(s => s.GetPropertyValue <int>("No Pupils")); //Update the criteria to reflect the max and min pupil count of the found schools } var tryCount = 0; while (benchmarkSchools.Count < basketSize) //Original query returns less than required { if (++tryCount > CriteriaSearchConfig.MAX_TRY_LIMIT) //Max query try reached. Return whatever is found. { break; } benchmarkCriteria = _benchmarkCriteriaBuilderService.BuildFromSimpleComparisonCriteria(defaultSchoolFinancialDataModel, simpleCriteria, tryCount); benchmarkSchools = await _financialDataService.SearchSchoolsByCriteriaAsync(benchmarkCriteria, estType); if (benchmarkSchools.Count > basketSize) //Number jumping to more than ideal. Cut from top by proximity. { benchmarkSchools = benchmarkSchools.OrderBy(b => Math.Abs(b.GetPropertyValue <int>("No Pupils") - defaultSchoolFinancialDataModel.PupilCount)).Take(basketSize).ToList(); benchmarkCriteria.MinNoPupil = benchmarkSchools.Min(s => s.GetPropertyValue <int>("No Pupils")); benchmarkCriteria.MaxNoPupil = benchmarkSchools.Max(s => s.GetPropertyValue <int>("No Pupils")); //Update the criteria to reflect the max and min pupil count of the found schools break; } } tryCount = 1; while (benchmarkSchools.Count < basketSize) //Query return is still less than required { var urbanRuralDefault = defaultSchoolFinancialDataModel.UrbanRural; var urbanRuralKey = Dictionaries.UrbanRuralDictionary.First(d => d.Value == urbanRuralDefault).Key; var urbanRuralQuery = Dictionaries.UrbanRuralDictionary.Where(d => d.Key >= urbanRuralKey - tryCount && d.Key <= urbanRuralKey + tryCount).Select(d => d.Value).ToArray(); benchmarkCriteria.UrbanRural = urbanRuralQuery; benchmarkSchools = await _financialDataService.SearchSchoolsByCriteriaAsync(benchmarkCriteria, estType); if (benchmarkSchools.Count > basketSize) //Number jumping to more than ideal. Cut from top by proximity. { benchmarkSchools = benchmarkSchools.OrderBy(b => Math.Abs(b.GetPropertyValue <int>("No Pupils") - defaultSchoolFinancialDataModel.PupilCount)).Take(basketSize).ToList(); benchmarkCriteria.MinNoPupil = benchmarkSchools.Min(s => s.GetPropertyValue <int>("No Pupils")); benchmarkCriteria.MaxNoPupil = benchmarkSchools.Max(s => s.GetPropertyValue <int>("No Pupils")); //Update the criteria to reflect the max and min pupil count of the found schools break; } if (urbanRuralQuery.Length == Dictionaries.UrbanRuralDictionary.Count) { break; } tryCount++; } return(new ComparisonResult() { BenchmarkSchools = benchmarkSchools, BenchmarkCriteria = benchmarkCriteria }); }