Exemplo n.º 1
0
        public async Task <IActionResult> GetPublishedProviderProfileTotalsForSpecificationForProviderForFundingLine(
            string specificationId,
            string providerId,
            string fundingStreamId,
            string fundingLineCode)
        {
            Guard.IsNullOrWhiteSpace(specificationId, nameof(specificationId));
            Guard.IsNullOrWhiteSpace(fundingStreamId, nameof(fundingStreamId));
            Guard.IsNullOrWhiteSpace(fundingLineCode, nameof(fundingLineCode));
            Guard.IsNullOrWhiteSpace(providerId, nameof(providerId));

            PublishedProviderVersion latestPublishedProviderVersion = await _resilience.ExecuteAsync(() =>
                                                                                                     _publishedFunding.GetLatestPublishedProviderVersionBySpecificationId(
                                                                                                         specificationId,
                                                                                                         fundingStreamId,
                                                                                                         providerId));

            if (latestPublishedProviderVersion == null)
            {
                return(new NotFoundResult());
            }

            IEnumerable <ProfileVariationPointer> profileVariationPointers
                = await _specificationService.GetProfileVariationPointers(specificationId);

            TemplateMetadataDistinctFundingLinesContents templateMetadataDistinctFundingLinesContents =
                await _policiesService.GetDistinctTemplateMetadataFundingLinesContents(
                    fundingStreamId,
                    latestPublishedProviderVersion.FundingPeriodId,
                    latestPublishedProviderVersion.TemplateVersion);

            IEnumerable <FundingStreamPeriodProfilePattern> fundingStreamPeriodProfilePatterns =
                await _profilingService.GetProfilePatternsForFundingStreamAndFundingPeriod(
                    latestPublishedProviderVersion.FundingStreamId,
                    latestPublishedProviderVersion.FundingPeriodId);

            (string profilePatternKey, string profilePatternName, string profilePatternDescription) =
                GetProfilePatternDetails(fundingLineCode, latestPublishedProviderVersion, fundingStreamPeriodProfilePatterns);

            FundingLineProfile fundingLineProfile = new FundingLineProfile
            {
                FundingLineCode = fundingLineCode,
                FundingLineName = templateMetadataDistinctFundingLinesContents?.FundingLines?
                                  .FirstOrDefault(_ => _.FundingLineCode == fundingLineCode)?.Name,
                ProfilePatternKey         = profilePatternKey,
                ProfilePatternName        = profilePatternName,
                ProfilePatternDescription = profilePatternDescription,
                ProviderId      = latestPublishedProviderVersion.ProviderId,
                UKPRN           = latestPublishedProviderVersion.Provider.UKPRN,
                ProviderName    = latestPublishedProviderVersion.Provider.Name,
                CarryOverAmount = latestPublishedProviderVersion.GetCarryOverTotalForFundingLine(fundingLineCode) ?? 0
            };

            ProfileVariationPointer currentProfileVariationPointer
                = profileVariationPointers?.SingleOrDefault(_ =>
                                                            _.FundingStreamId == fundingStreamId && _.FundingLineId == fundingLineCode);

            ProfileTotal[] profileTotals = new PaymentFundingLineProfileTotals(latestPublishedProviderVersion, fundingLineCode)
                                           .ToArray();

            fundingLineProfile.TotalAllocation = latestPublishedProviderVersion
                                                 .FundingLines
                                                 .Where(_ => _.Type == FundingLineType.Payment)
                                                 .SingleOrDefault(_ => _.FundingLineCode == fundingLineCode)
                                                 ?.Value;

            fundingLineProfile.ProfileTotalAmount = profileTotals.Sum(_ => _.Value);

            FundingDate fundingDate = await _policiesService.GetFundingDate(
                fundingStreamId,
                latestPublishedProviderVersion.FundingPeriodId,
                fundingLineCode);

            for (int index = 0; index < profileTotals.Count(); index++)
            {
                ProfileTotal profileTotal = profileTotals[index];
                profileTotal.InstallmentNumber = index + 1;

                profileTotal.IsPaid = IsProfileTotalPaid(currentProfileVariationPointer, profileTotal);

                profileTotal.ActualDate = fundingDate?.Patterns?.SingleOrDefault(_ =>
                                                                                 _.Occurrence == profileTotal.Occurrence &&
                                                                                 _.Period == profileTotal.TypeValue &&
                                                                                 _.PeriodYear == profileTotal.Year)?.PaymentDate;
            }

            fundingLineProfile.AmountAlreadyPaid = profileTotals.Where(_ => _.IsPaid).Sum(_ => _.Value);
            fundingLineProfile.RemainingAmount   = fundingLineProfile.TotalAllocation - fundingLineProfile.AmountAlreadyPaid;

            foreach (ProfileTotal profileTotal in profileTotals.Where(_ => !_.IsPaid))
            {
                profileTotal.ProfileRemainingPercentage = fundingLineProfile.TotalAllocation.HasValue && fundingLineProfile.TotalAllocation > 0 ?
                                                          profileTotal.Value / (fundingLineProfile.TotalAllocation - fundingLineProfile.AmountAlreadyPaid) * 100 : 0;
            }

            fundingLineProfile.ProfileTotals = profileTotals;

            fundingLineProfile.LastUpdatedDate = latestPublishedProviderVersion.GetLatestFundingLineDate(fundingLineCode);
            fundingLineProfile.LastUpdatedUser = latestPublishedProviderVersion.GetLatestFundingLineUser(fundingLineCode);

            return(new OkObjectResult(fundingLineProfile));
        }