private static PublishedProviderFundingStructureItem RecursivelyAddFundingLines(
            IEnumerable <Common.TemplateMetadata.Models.FundingLine> fundingLines,
            List <TemplateMappingItem> templateMappingItems,
            int level,
            Common.TemplateMetadata.Models.FundingLine fundingLine,
            PublishedProviderVersion publishedProviderVersion)
        {
            level++;

            List <PublishedProviderFundingStructureItem> innerFundingStructureItems = new List <PublishedProviderFundingStructureItem>();

            // If funding line has calculations, recursively add them to list of inner FundingStructureItems
            if (fundingLine.Calculations != null && fundingLine.Calculations.Any())
            {
                foreach (Common.TemplateMetadata.Models.Calculation calculation in fundingLine.Calculations)
                {
                    innerFundingStructureItems.Add(
                        RecursivelyMapCalculationsToFundingStructureItem(
                            calculation,
                            level,
                            templateMappingItems,
                            publishedProviderVersion));
                }
            }

            // If funding line has more funding lines, recursively add them to list of inner FundingStructureItems
            if (fundingLine.FundingLines != null && fundingLine.FundingLines.Any())
            {
                foreach (Common.TemplateMetadata.Models.FundingLine line in fundingLines)
                {
                    innerFundingStructureItems.Add(RecursivelyAddFundingLines(
                                                       line.FundingLines,
                                                       templateMappingItems,
                                                       level,
                                                       line,
                                                       publishedProviderVersion));
                }
            }

            CalculateFunding.Models.Publishing.FundingLine publishedProviderFundingLine = publishedProviderVersion.FundingLines.FirstOrDefault(_ => _.TemplateLineId == fundingLine.TemplateLineId);
            string calculationValue = null;

            if (publishedProviderFundingLine != null)
            {
                calculationValue = publishedProviderFundingLine.Value.AsFormatCalculationType(CalculationValueFormat.Currency);
            }

            // Add FundingStructureItem
            PublishedProviderFundingStructureItem fundingStructureItem = MapToFundingStructureItem(
                level,
                fundingLine.Name,
                fundingLine.FundingLineCode,
                PublishedProviderFundingStructureType.FundingLine,
                null,
                null,
                innerFundingStructureItems.Any() ? innerFundingStructureItems : null,
                calculationValue);

            return(fundingStructureItem);
        }
Esempio n. 2
0
        private List <PublishingModels.FundingLine> GenerateFundingLines(IEnumerable <AggregateFundingLine> fundingLineAggregates,
                                                                         IEnumerable <Common.TemplateMetadata.Models.FundingLine> fundingLineDefinitions)
        {
            List <PublishingModels.FundingLine> fundingLines = new List <PublishingModels.FundingLine>();

            foreach (AggregateFundingLine aggregateFundingLine in fundingLineAggregates)
            {
                if (aggregateFundingLine == null)
                {
                    throw new InvalidOperationException("Null aggregate funding line");
                }

                Common.TemplateMetadata.Models.FundingLine fundingLineDefinition = fundingLineDefinitions.FirstOrDefault(_
                                                                                                                         => _.TemplateLineId == aggregateFundingLine.TemplateLineId);
                if (fundingLineDefinition == null)
                {
                    throw new InvalidOperationException($"Unable to find funding line with TemplateLineId '{aggregateFundingLine.TemplateLineId}'");
                }

                PublishingModels.FundingLine fundingLine = new PublishingModels.FundingLine()
                {
                    FundingLineCode = fundingLineDefinition.FundingLineCode,
                    Name            = fundingLineDefinition.Name,
                    TemplateLineId  = fundingLineDefinition.TemplateLineId,
                    Type            = fundingLineDefinition.Type.AsMatchingEnum <FundingLineType>(),
                    Value           = aggregateFundingLine.Value ?? 0,
                };

                fundingLines.Add(fundingLine);

                fundingLine.DistributionPeriods = aggregateFundingLine.DistributionPeriods?.ToList();

                if (aggregateFundingLine.FundingLines.AnyWithNullCheck())
                {
                    fundingLines.AddRange(GenerateFundingLines(aggregateFundingLine.FundingLines, fundingLineDefinitions));
                }
            }

            return(fundingLines);
        }