private dynamic ToCalculation(Common.TemplateMetadata.Models.Calculation calculation, TemplateMapping templateMapping, IEnumerable <FundingCalculation> calculationResults, IEnumerable <FundingReferenceData> referenceData, string providerId)
        {
            string calculationId = templateMapping.TemplateMappingItems.Where(x => x.TemplateId == calculation.TemplateCalculationId)?.Single().CalculationId;
            IEnumerable <FundingReferenceData> publishedFundingReferenceData = referenceData?.Where(_ => calculation.ReferenceData?.Any(calcReferenceData => calcReferenceData.TemplateReferenceId == _.TemplateReferenceId) ?? false);
            IEnumerable <dynamic> refernceData = publishedFundingReferenceData?.Select(_ => ToReferenceData(calculation.ReferenceData?.Where(calcReferenceData => calcReferenceData.TemplateReferenceId == _.TemplateReferenceId).Single(), _));

            FundingCalculation fundingCalculationResult = calculationResults.Where(x => x.TemplateCalculationId == calculation.TemplateCalculationId)?.SingleOrDefault();

            if (fundingCalculationResult == null)
            {
                int totalCount = calculationResults.Count(c => c.TemplateCalculationId == calculation.TemplateCalculationId);
                throw new InvalidOperationException($"Unable to find calculation result for provider '{providerId}' with TemplateCalculationId '{calculation.TemplateCalculationId}'. A total of {totalCount} results found.");
            }

            return(new
            {
                calculation.Name,
                calculation.TemplateCalculationId,
                fundingCalculationResult.Value,
                ValueFormat = calculation.ValueFormat.ToString(),
                Type = calculation.Type.ToString(),
                calculation.FormulaText,
                AggregationType = calculation.AggregationType.ToString(),
                Calculations = calculation.Calculations?.Select(x => ToCalculation(x, templateMapping, calculationResults, referenceData, providerId)),
                ReferenceData = !refernceData.IsNullOrEmpty() ? refernceData : null
            });
        }
Beispiel #2
0
        private static SchemaJsonCalculation BuildCalculations(IEnumerable <FundingCalculation> fundingCalculations,
                                                               Calculation templateCalculation,
                                                               string providerId)
        {
            FundingCalculation publishedFundingCalculation = fundingCalculations?.Where(_ =>
                                                                                        _.TemplateCalculationId == templateCalculation.TemplateCalculationId)
                                                             .FirstOrDefault();

            if (publishedFundingCalculation == null)
            {
                return(null);
            }

            ICalculationMapper mapper = templateCalculation.AggregationType switch
            {
                AggregationType.PercentageChangeBetweenAandB => new PercentageChangeBetweenAandBCalculation(),
                AggregationType.GroupRate => new GroupRateCalculation(),
                _ => new BasicCalculation()
            };

            return(mapper.ToCalculation(templateCalculation,
                                        publishedFundingCalculation,
                                        fundingCalculations,
                                        providerId));
        }
 private void ThenCalculationValueShouldBe(FundingCalculation fundingCalculation, int expectedValue)
 {
     fundingCalculation
     .Value
     .Should()
     .Be(expectedValue);
 }
Beispiel #4
0
 public virtual SchemaJsonCalculation ToCalculation(Calculation templateCalculation,
                                                    FundingCalculation publishedFundingCalculation,
                                                    IEnumerable <FundingCalculation> publishedFundingCalculations,
                                                    string providerId) =>
 new SchemaJsonCalculation
 {
     Name                  = templateCalculation.Name,
     Type                  = templateCalculation.Type.ToString(),
     AggregationType       = templateCalculation.AggregationType.ToString(),
     FormulaText           = templateCalculation.FormulaText,
     Value                 = publishedFundingCalculation.Value,
     TemplateCalculationId = templateCalculation.TemplateCalculationId,
     ValueFormat           = templateCalculation.ValueFormat.ToString(),
     AllowedEnumTypeValues = templateCalculation.AllowedEnumTypeValues?.Any() == true ? templateCalculation.AllowedEnumTypeValues : null
 };
Beispiel #5
0
        private DataColumn NewCalculationDataColumn(FundingCalculation calculation)
        {
            Calculation templateCalculation = GetTemplateCalculation(calculation.TemplateCalculationId);

            string columnName = $"Calc_{templateCalculation.TemplateCalculationId}_{templateCalculation.Name.Replace(" ", "")}";

            return(templateCalculation.ValueFormat switch
            {
                var format when format == CalculationValueFormat.Currency ||
                format == CalculationValueFormat.Number ||
                format == CalculationValueFormat.Percentage
                => NewDataColumn <decimal>(columnName, allowNull: true),
                CalculationValueFormat.Boolean => NewDataColumn <bool>(columnName, allowNull: true),
                CalculationValueFormat.String => NewDataColumn <string>(columnName, 128, true),
                _ => throw new InvalidOperationException("Unknown value format")
            });
        private static PublishedProviderFundingStructureItem RecursivelyMapCalculationsToFundingStructureItem(
            Common.TemplateMetadata.Models.Calculation calculation,
            int level,
            List <TemplateMappingItem> templateMappingItems,
            PublishedProviderVersion publishedProviderVersion)
        {
            level++;

            List <PublishedProviderFundingStructureItem> innerFundingStructureItems = null;

            string             calculationId      = GetCalculationId(calculation, templateMappingItems);
            FundingCalculation fundingCalculation = publishedProviderVersion.Calculations.FirstOrDefault(_ => _.TemplateCalculationId == calculation.TemplateCalculationId);

            string calculationType  = calculation.Type.ToString();
            string calculationValue = null;

            if (fundingCalculation != null)
            {
                calculationType  = calculation.ValueFormat.ToString();
                calculationValue = fundingCalculation.Value.AsFormatCalculationType(calculation.ValueFormat);
            }

            if (calculation.Calculations != null && calculation.Calculations.Any())
            {
                innerFundingStructureItems = calculation.Calculations.Select(innerCalculation =>
                                                                             RecursivelyMapCalculationsToFundingStructureItem(
                                                                                 innerCalculation,
                                                                                 level,
                                                                                 templateMappingItems,
                                                                                 publishedProviderVersion))
                                             .ToList();
            }

            return(MapToFundingStructureItem(
                       level,
                       calculation.Name,
                       null,
                       PublishedProviderFundingStructureType.Calculation,
                       calculationType,
                       calculationId,
                       innerFundingStructureItems,
                       calculationValue));
        }
Beispiel #7
0
            public override SchemaJsonCalculation ToCalculation(Calculation templateCalculation,
                                                                FundingCalculation publishedFundingCalculation,
                                                                IEnumerable <FundingCalculation> publishedFundingCalculations,
                                                                string providerId)
            {
                dynamic schemaJsonCalculation = base.ToCalculation(templateCalculation,
                                                                   publishedFundingCalculation,
                                                                   publishedFundingCalculations,
                                                                   providerId);

                GroupRate templateCalculationGroupRate = templateCalculation.GroupRate;

                schemaJsonCalculation.GroupRate = new SchemaJsonGroupRate
                {
                    Denominator = templateCalculationGroupRate.Denominator,
                    Numerator   = templateCalculationGroupRate.Numerator
                };

                return(schemaJsonCalculation);
            }
Beispiel #8
0
            public override SchemaJsonCalculation ToCalculation(Calculation templateCalculation,
                                                                FundingCalculation publishedFundingCalculation,
                                                                IEnumerable <FundingCalculation> publishedFundingCalculations,
                                                                string providerId)
            {
                SchemaJsonCalculation schemaJsonCalculation = base.ToCalculation(templateCalculation,
                                                                                 publishedFundingCalculation,
                                                                                 publishedFundingCalculations,
                                                                                 providerId);

                PercentageChangeBetweenAandB templateCalculationPercentageChangeBetweenAandB = templateCalculation.PercentageChangeBetweenAandB;

                schemaJsonCalculation.PercentageChangeBetweenAandB = new SchemaJsonPercentageChangeBetweenAandB
                {
                    CalculationA = templateCalculationPercentageChangeBetweenAandB.CalculationA,
                    CalculationB = templateCalculationPercentageChangeBetweenAandB.CalculationB,
                    CalculationAggregationType = templateCalculationPercentageChangeBetweenAandB.CalculationAggregationType.ToString()
                };

                return(schemaJsonCalculation);
            }
            public override SchemaJsonCalculation ToSchemaJsonCalculation(Calculation templateCalculation,
                                                                          FundingCalculation publishedFundingCalculation,
                                                                          IEnumerable <FundingCalculation> publishedFundingCalculations,
                                                                          string organisationGroupTypeIdentifier,
                                                                          string organisationGroupIdentifierValue)
            {
                SchemaJsonCalculation schemaJsonCalculation = base.ToSchemaJsonCalculation(templateCalculation,
                                                                                           publishedFundingCalculation,
                                                                                           publishedFundingCalculations,
                                                                                           organisationGroupTypeIdentifier,
                                                                                           organisationGroupIdentifierValue);

                GroupRate templateCalculationGroupRate = templateCalculation.GroupRate;

                schemaJsonCalculation.GroupRate = new SchemaJsonGroupRate
                {
                    Denominator = templateCalculationGroupRate.Denominator,
                    Numerator   = templateCalculationGroupRate.Numerator
                };

                return(schemaJsonCalculation);
            }
        public async Task MovesPredecessorPupilNumbersOntoSuccessor()
        {
            uint calculationOneId = NewRandomUint();
            uint calculationTwoId = NewRandomUint();

            int predecessorPupilNumberOne = NewRandomPupilNumber();
            int predecessorPupilNumberTwo = NewRandomPupilNumber();
            int successorPupilNumberOne   = NewRandomPupilNumber();
            int successorPupilNumberTwo   = NewRandomPupilNumber();
            int successorPupilNumberThree = NewRandomPupilNumber();
            int successorPupilNumberFour  = NewRandomPupilNumber();

            GivenThePupilNumberCalculationIds(calculationOneId, calculationTwoId);
            AndTheFundingCalculations(NewFundingCalculation(_ => _.WithTemplateCalculationId(calculationOneId)
                                                            .WithValue(predecessorPupilNumberOne)),
                                      NewFundingCalculation(),
                                      NewFundingCalculation(_ => _.WithTemplateCalculationId(calculationTwoId)
                                                            .WithValue(predecessorPupilNumberTwo)));

            FundingCalculation successorCalculationOne = NewFundingCalculation(_ => _.WithTemplateCalculationId(calculationOneId)
                                                                               .WithValue(successorPupilNumberOne));
            FundingCalculation successorCalculationTwo   = NewFundingCalculation(_ => _.WithValue(successorPupilNumberTwo));
            FundingCalculation successorCalculationThree = NewFundingCalculation(_ => _.WithTemplateCalculationId(calculationTwoId)
                                                                                 .WithValue(successorPupilNumberThree));
            FundingCalculation successorCalculationFour = NewFundingCalculation(_ => _.WithValue(successorPupilNumberFour));

            AndTheSuccessorFundingCalculations(successorCalculationOne,
                                               successorCalculationTwo,
                                               successorCalculationThree,
                                               successorCalculationFour);

            await WhenTheChangeIsApplied();

            ThenCalculationValueShouldBe(successorCalculationOne, predecessorPupilNumberOne + successorPupilNumberOne);
            AndCalculationValueShouldBe(successorCalculationTwo, successorPupilNumberTwo);
            AndCalculationValueShouldBe(successorCalculationThree, successorPupilNumberThree + predecessorPupilNumberTwo);
            AndCalculationValueShouldBe(successorCalculationFour, successorPupilNumberFour);
        }
            public override SchemaJsonCalculation ToSchemaJsonCalculation(Calculation templateCalculation,
                                                                          FundingCalculation publishedFundingCalculation,
                                                                          IEnumerable <FundingCalculation> publishedFundingCalculations,
                                                                          string organisationGroupTypeIdentifier,
                                                                          string organisationGroupIdentifierValue)
            {
                SchemaJsonCalculation schemaJsonCalculation = base.ToSchemaJsonCalculation(templateCalculation,
                                                                                           publishedFundingCalculation,
                                                                                           publishedFundingCalculations,
                                                                                           organisationGroupTypeIdentifier,
                                                                                           organisationGroupIdentifierValue);

                PercentageChangeBetweenAandB templateCalculationPercentageChangeBetweenAandB = templateCalculation.PercentageChangeBetweenAandB;

                schemaJsonCalculation.PercentageChangeBetweenAandB = new SchemaJsonPercentageChangeBetweenAandB
                {
                    CalculationA = templateCalculationPercentageChangeBetweenAandB.CalculationA,
                    CalculationB = templateCalculationPercentageChangeBetweenAandB.CalculationB,
                    CalculationAggregationType = templateCalculationPercentageChangeBetweenAandB.CalculationAggregationType.ToString()
                };

                return(schemaJsonCalculation);
            }
Beispiel #12
0
        private SchemaJsonCalculation BuildSchemaJsonCalculations(IEnumerable <FundingReferenceData> referenceData, IEnumerable <FundingCalculation> fundingCalculations, Common.TemplateMetadata.Models.Calculation calculation, string organisationGroupTypeIdentifier, string organisationGroupIdentifierValue)
        {
            FundingCalculation publishedFundingCalculation = fundingCalculations?.Where(_ => _.TemplateCalculationId == calculation.TemplateCalculationId)?.FirstOrDefault();

            if (publishedFundingCalculation == null)
            {
                throw new InvalidOperationException($"Unable to find calculation result for TemplateCalculationId '{calculation.TemplateCalculationId}' for group identifier '{organisationGroupTypeIdentifier}' value '{organisationGroupIdentifierValue}'");
            }
            IEnumerable <FundingReferenceData> publishedFundingReferenceData = referenceData?.Where(_ => calculation.ReferenceData?.Any(calcReferenceData => calcReferenceData.TemplateReferenceId == _.TemplateReferenceId) ?? false);
            IEnumerable <dynamic> refernceData = publishedFundingReferenceData?.Select(_ => ToReferenceData(calculation.ReferenceData.Where(calcReferenceData => calcReferenceData.TemplateReferenceId == _.TemplateReferenceId).Single(), _));

            return(new SchemaJsonCalculation
            {
                Name = calculation.Name,
                Type = calculation.Type.ToString(),
                AggregationType = calculation.AggregationType.ToString(),
                FormulaText = calculation.FormulaText,
                Value = publishedFundingCalculation.Value,
                TemplateCalculationId = calculation.TemplateCalculationId,
                ValueFormat = calculation.ValueFormat.ToString(),
                Calculations = calculation.Calculations?.Where(IsAggregationOrHasChildCalculations)?.Select(_ => BuildSchemaJsonCalculations(referenceData, fundingCalculations, _, organisationGroupTypeIdentifier, organisationGroupIdentifierValue)),
                ReferenceData = !refernceData.IsNullOrEmpty() ? refernceData : null
            });
        }
 private void AndCalculationValueShouldBe(FundingCalculation fundingCalculation, int expectedValue)
 {
     ThenCalculationValueShouldBe(fundingCalculation, expectedValue);
 }