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
            });
        }
        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));
        }
Ejemplo n.º 3
0
 private static bool IsAggregationOrHasChildCalculations(Common.TemplateMetadata.Models.Calculation fundingCalculation)
 {
     return(fundingCalculation.AggregationType != Common.TemplateMetadata.Enums.AggregationType.None ||
            fundingCalculation.Calculations?.Any() == true);
 }
Ejemplo n.º 4
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 static string GetCalculationId(
     Common.TemplateMetadata.Models.Calculation calculation,
     IEnumerable <TemplateMappingItem> templateMappingItems) =>
 templateMappingItems
 .FirstOrDefault(_ => _.TemplateId == calculation.TemplateCalculationId)?
 .CalculationId;