Beispiel #1
0
        public async Task <IActionResult> GetCurrentProfileConfig
            (string specificationId, string providerId, string fundingStreamId)
        {
            Guard.IsNullOrWhiteSpace(specificationId, nameof(specificationId));
            Guard.IsNullOrWhiteSpace(fundingStreamId, nameof(fundingStreamId));
            Guard.IsNullOrWhiteSpace(providerId, nameof(providerId));

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

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

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

            List <FundingLineProfile> fundingLineProfiles = new List <FundingLineProfile>();

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

            foreach (FundingLine fundingLine in latestPublishedProviderVersion.FundingLines.Where(f => f.Type == FundingLineType.Payment).DistinctBy(f => f.FundingLineCode).OrderBy(f => f.Name))
            {
                string fundingLineCode = fundingLine.FundingLineCode;

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

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

                FundingLineProfile fundingLineProfile = new FundingLineProfile
                {
                    FundingLineCode = fundingLine.FundingLineCode,
                    FundingLineName = templateMetadataDistinctFundingLinesContents?.FundingLines?
                                      .FirstOrDefault(_ => _.FundingLineCode == fundingLineCode)?.Name,
                    CarryOverAmount           = latestPublishedProviderVersion.GetCarryOverTotalForFundingLine(fundingLineCode),
                    LastUpdatedUser           = latestPublishedProviderVersion.GetLatestFundingLineUser(fundingLineCode),
                    LastUpdatedDate           = latestPublishedProviderVersion.GetLatestFundingLineDate(fundingLineCode),
                    ProfilePatternKey         = profilePatternKey,
                    ProfilePatternName        = profilePatternName,
                    ProfilePatternDescription = profilePatternDescription,
                    ProfileTotalAmount        = profileTotals.Any() ? profileTotals.Sum(_ => _.Value) : (decimal?)null,
                    ProfileTotals             = profileTotals
                };

                if (fundingLineProfile.ProfileTotalAmount != null || fundingLineProfile.CarryOverAmount != null)
                {
                    fundingLineProfile.TotalAllocation = (fundingLineProfile.ProfileTotalAmount ?? 0) + (fundingLineProfile.CarryOverAmount ?? 0);
                }

                fundingLineProfiles.Add(fundingLineProfile);
            }

            return(new OkObjectResult(fundingLineProfiles));
        }
Beispiel #2
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));
        }
Beispiel #3
0
        public async Task <IActionResult> GetPreviousProfilesForSpecificationForProviderForFundingLine(
            string specificationId,
            string providerId,
            string fundingStreamId,
            string fundingLineCode)
        {
            List <FundingLineChange> fundingLineChanges = new List <FundingLineChange>();

            IEnumerable <PublishedProviderVersion> publishedProviderVersions = await _resilience.ExecuteAsync(() =>
                                                                                                              _publishedFunding.GetPublishedProviderVersionsForApproval(
                                                                                                                  specificationId,
                                                                                                                  fundingStreamId,
                                                                                                                  providerId));

            PublishedProviderVersion latestPublishedProviderVersion = publishedProviderVersions.FirstOrDefault();

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

            IEnumerable <PublishedProviderVersion> historyPublishedProviderVersions =
                publishedProviderVersions.Except(new[] { latestPublishedProviderVersion });

            IEnumerable <FundingStream> fundingStreams = await _policiesService.GetFundingStreams();

            foreach (PublishedProviderVersion publishedProviderVersion in historyPublishedProviderVersions)
            {
                if (publishedProviderVersion.GetFundingLineTotal(fundingLineCode)
                    != latestPublishedProviderVersion.GetFundingLineTotal(fundingLineCode) ||
                    publishedProviderVersion.GetCarryOverTotalForFundingLine(fundingLineCode)
                    != latestPublishedProviderVersion.GetCarryOverTotalForFundingLine(fundingLineCode))
                {
                    TemplateMetadataDistinctFundingLinesContents templateMetadataDistinctFundingLinesContents =
                        await _policiesService.GetDistinctTemplateMetadataFundingLinesContents(
                            fundingStreamId,
                            latestPublishedProviderVersion.FundingPeriodId,
                            latestPublishedProviderVersion.TemplateVersion);

                    FundingLineChange fundingLineChange = new FundingLineChange
                    {
                        FundingLineTotal         = latestPublishedProviderVersion.GetFundingLineTotal(fundingLineCode),
                        PreviousFundingLineTotal = publishedProviderVersion.GetFundingLineTotal(fundingLineCode),
                        FundingStreamName        = fundingStreams.SingleOrDefault(_ => _.Id == latestPublishedProviderVersion.FundingStreamId)?.Name,
                        FundingLineName          = templateMetadataDistinctFundingLinesContents?.FundingLines?
                                                   .FirstOrDefault(_ => _.FundingLineCode == fundingLineCode)?.Name,
                        CarryOverAmount = latestPublishedProviderVersion.GetCarryOverTotalForFundingLine(fundingLineCode),
                        LastUpdatedUser = latestPublishedProviderVersion.GetLatestFundingLineUser(fundingLineCode),
                        LastUpdatedDate = latestPublishedProviderVersion.GetLatestFundingLineDate(fundingLineCode),
                    };

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

                    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.ActualDate = fundingDate?.Patterns?.SingleOrDefault(_ =>
                                                                                         _.Occurrence == profileTotal.Occurrence &&
                                                                                         _.Period == profileTotal.TypeValue &&
                                                                                         _.PeriodYear == profileTotal.Year)?.PaymentDate;
                    }

                    fundingLineChange.ProfileTotals = profileTotals;
                    fundingLineChanges.Add(fundingLineChange);
                }

                latestPublishedProviderVersion = publishedProviderVersion;
            }

            return(new OkObjectResult(fundingLineChanges));
        }