private PublishedProviderResult CreateSuccessorResult(SpecificationCurrentVersion specification, ProviderChangeItem providerChangeItem, Reference author, Period fundingPeriod) { FundingStream fundingStream = GetFundingStream(specification, providerChangeItem); PublishedFundingStreamDefinition fundingStreamDefinition = _mapper.Map <PublishedFundingStreamDefinition>(fundingStream); AllocationLine allocationLine = fundingStream.AllocationLines.FirstOrDefault(a => a.ProviderLookups.Any(l => l.ProviderType == providerChangeItem.SuccessorProvider.ProviderType && l.ProviderSubType == providerChangeItem.SuccessorProvider.ProviderSubType)); PublishedAllocationLineDefinition publishedAllocationLine = _mapper.Map <PublishedAllocationLineDefinition>(allocationLine); PublishedProviderResult successorResult = new PublishedProviderResult { FundingPeriod = fundingPeriod, FundingStreamResult = new PublishedFundingStreamResult { AllocationLineResult = new PublishedAllocationLineResult { AllocationLine = publishedAllocationLine, Current = new PublishedAllocationLineResultVersion { Author = author, Calculations = null, // don't set calcs as result hasn't been generated from calculation run Date = DateTimeOffset.Now, Provider = providerChangeItem.SuccessorProvider, ProviderId = providerChangeItem.SuccessorProviderId, SpecificationId = specification.Id, Status = AllocationLineStatus.Held, Value = 0, Version = 1 }, HasResultBeenVaried = true }, DistributionPeriod = $"{fundingStream.PeriodType.Id}{specification.FundingPeriod.Id}", FundingStream = fundingStreamDefinition, FundingStreamPeriod = $"{fundingStream.Id}{specification.FundingPeriod.Id}" }, ProviderId = providerChangeItem.SuccessorProviderId, SpecificationId = specification.Id, }; successorResult.FundingStreamResult.AllocationLineResult.Current.PublishedProviderResultId = successorResult.Id; EnsurePredecessors(successorResult, providerChangeItem.UpdatedProvider.Id); return(successorResult); }
private IEnumerable <PublishedFundingStreamResult> AssembleFundingStreamResults(ProviderResult providerResult, SpecificationCurrentVersion specificationCurrentVersion, Reference author, IEnumerable <FundingStream> allFundingStreams) { IList <PublishedFundingStreamResult> publishedFundingStreamResults = new List <PublishedFundingStreamResult>(); Dictionary <string, PublishedAllocationLineDefinition> publishedAllocationLines = new Dictionary <string, PublishedAllocationLineDefinition>(); foreach (Reference fundingStreamReference in specificationCurrentVersion.FundingStreams) { FundingStream fundingStream = allFundingStreams.FirstOrDefault(m => m.Id == fundingStreamReference.Id); if (fundingStream == null) { throw new NonRetriableException($"Failed to find a funding stream for id: {fundingStreamReference.Id}"); } PublishedFundingStreamDefinition publishedFundingStreamDefinition = _mapper.Map <PublishedFundingStreamDefinition>(fundingStream); List <PublishedProviderCalculationResult> publishedProviderCalculationResults = new List <PublishedProviderCalculationResult>(providerResult.CalculationResults.Count()); foreach (CalculationResult calculationResult in providerResult.CalculationResults) { (Policy policy, Policy parentPolicy, Calculation calculation) = FindPolicy(calculationResult.CalculationSpecification?.Id, specificationCurrentVersion.Policies); if (calculation == null) { throw new NonRetriableException($"Calculation specification not found in specification. Calculation Spec Id ='{calculationResult?.CalculationSpecification?.Id}'"); } if (calculation.CalculationType == CalculationType.Number && !calculation.IsPublic) { continue; } PublishedProviderCalculationResult publishedProviderCalculationResult = new PublishedProviderCalculationResult() { CalculationSpecification = calculationResult.CalculationSpecification, AllocationLine = calculationResult.AllocationLine, IsPublic = calculation.IsPublic, CalculationType = ConvertCalculationType(calculationResult.CalculationType), Value = calculationResult.Value, CalculationVersion = calculationResult.Version }; if (policy != null) { publishedProviderCalculationResult.Policy = new PolicySummary(policy.Id, policy.Name, policy.Description); } if (parentPolicy != null) { publishedProviderCalculationResult.ParentPolicy = new PolicySummary(parentPolicy.Id, parentPolicy.Name, parentPolicy.Description); } publishedProviderCalculationResults.Add(publishedProviderCalculationResult); } IEnumerable <IGrouping <string, CalculationResult> > allocationLineGroups = providerResult .CalculationResults .Where(c => c.CalculationType == Models.Calcs.CalculationType.Funding && c.Value.HasValue && c.AllocationLine != null && !string.IsNullOrWhiteSpace(c.AllocationLine.Id)) .GroupBy(m => m.AllocationLine.Id); foreach (IGrouping <string, CalculationResult> allocationLineResultGroup in allocationLineGroups) { PublishedAllocationLineDefinition publishedAllocationLine; if (!publishedAllocationLines.TryGetValue(allocationLineResultGroup.Key, out publishedAllocationLine)) { AllocationLine allocationLine = fundingStream.AllocationLines.FirstOrDefault(m => m.Id == allocationLineResultGroup.Key); if (allocationLine != null) { publishedAllocationLine = _mapper.Map <PublishedAllocationLineDefinition>(allocationLine); publishedAllocationLines.Add(allocationLineResultGroup.Key, publishedAllocationLine); } } if (publishedAllocationLine != null) { PublishedFundingStreamResult publishedFundingStreamResult = new PublishedFundingStreamResult { FundingStream = publishedFundingStreamDefinition, FundingStreamPeriod = $"{fundingStream.Id}{specificationCurrentVersion.FundingPeriod.Id}", DistributionPeriod = $"{fundingStream.PeriodType.Id}{specificationCurrentVersion.FundingPeriod.Id}" }; PublishedAllocationLineResultVersion publishedAllocationLineResultVersion = new PublishedAllocationLineResultVersion { Author = author, Date = DateTimeOffset.Now, Status = AllocationLineStatus.Held, Value = allocationLineResultGroup.Sum(m => m.Value), Provider = providerResult.Provider, SpecificationId = specificationCurrentVersion.Id, ProviderId = providerResult.Provider.Id, Calculations = publishedProviderCalculationResults.Where(c => c.AllocationLine == null || string.Equals(c.AllocationLine.Id, publishedAllocationLine.Id, StringComparison.InvariantCultureIgnoreCase)), }; publishedFundingStreamResult.AllocationLineResult = new PublishedAllocationLineResult { AllocationLine = publishedAllocationLine, Current = publishedAllocationLineResultVersion }; publishedFundingStreamResults.Add(publishedFundingStreamResult); } } } return(publishedFundingStreamResults); }