Esempio n. 1
0
 private object[] GetProfilePeriodColumnsForProfileTotal(ProfileTotal profile)
 => new object[]
 {
     profile.TypeValue,
     profile.PeriodType,
     profile.Year,
     profile.Occurrence,
     profile.DistributionPeriod,
     profile.Value
 };
Esempio n. 2
0
        private bool IsProfileTotalPaid(
            ProfileVariationPointer profileVariationPointer,
            ProfileTotal profileTotal)
        {
            if (profileVariationPointer == null)
            {
                return(false);
            }

            if (profileTotal.Year > profileVariationPointer.Year || (profileTotal.Year == profileVariationPointer.Year &&
                                                                     MonthNumberFor(profileTotal.TypeValue) >= MonthNumberFor(profileVariationPointer.TypeValue)))
            {
                return(false);
            }

            return(true);
        }
        public async Task GetAllReleasedProfileTotals_Returns_Mapped_Profiling_Result_Given_PublishingApi_Returns_Valid_ProfileTotals_Response()
        {
            ProfileTotal aProfileTotalOneYearInThePast = new ProfileTotal
            {
                Occurrence = 1,
                TypeValue  = "October",
                Year       = DateTime.Now.AddYears(-1).Year,
                Value      = 111.0m
            };
            ProfileTotal aProfileTotalOneMonthInThePast = new ProfileTotal
            {
                Occurrence = 2,
                TypeValue  = DateTime.Now.AddMonths(-1).ToString("MMMM"),
                Year       = DateTime.Now.AddMonths(-1).Year,
                Value      = 112.0m
            };
            ProfileTotal aProfileTotalOneYearInTheFuture = new ProfileTotal
            {
                Occurrence = 3,
                TypeValue  = "April",
                Year       = DateTime.Now.AddYears(1).Year,
                Value      = 113.0m
            };
            List <ProfileTotal> profileTotals = new List <ProfileTotal>
            {
                aProfileTotalOneYearInThePast,
                aProfileTotalOneMonthInThePast,
                aProfileTotalOneYearInTheFuture
            };
            ProfilingVersion latestProfilingVersion = new ProfilingVersion
            {
                Date          = new DateTimeOffset(),
                ProfileTotals = profileTotals,
                Version       = 2
            };
            ProfilingVersion previousProfilingVersion = new ProfilingVersion
            {
                Date          = new DateTimeOffset(),
                ProfileTotals = new List <ProfileTotal>
                {
                    new ProfileTotal
                    {
                        Occurrence = 1,
                        TypeValue  = DateTime.Now.ToString("MMMM"),
                        Year       = DateTime.Now.Year,
                        Value      = 999.0m
                    },
                    new ProfileTotal
                    {
                        Occurrence = 2,
                        TypeValue  = DateTime.Now.ToString("MMMM"),
                        Year       = DateTime.Now.Year,
                        Value      = 1.0m
                    }
                },
                Version = 1
            };
            IDictionary <int, ProfilingVersion> profileVersions =
                new Dictionary <int, ProfilingVersion>
            {
                {
                    latestProfilingVersion.Version, latestProfilingVersion
                },
                {
                    previousProfilingVersion.Version, previousProfilingVersion
                }
            };

            ApiResponse <IDictionary <int, ProfilingVersion> > publishingApiResponse =
                new ApiResponse <IDictionary <int, ProfilingVersion> >(HttpStatusCode.OK, profileVersions);

            _publishingApiClient.GetAllReleasedProfileTotals(
                Arg.Any <string>(),
                Arg.Any <string>(),
                Arg.Any <string>())
            .Returns(publishingApiResponse);

            IActionResult result = await _publishController.GetAllReleasedProfileTotals(
                "A VALID FUNDING STREAM ID",
                "A VALID FUNDING PERIOD ID",
                "A VALID PROVIDER ID");

            ProfilingViewModel profilingViewModelResult = result.As <OkObjectResult>().Value.As <ProfilingViewModel>();

            result.Should().BeAssignableTo <OkObjectResult>();
            profilingViewModelResult.TotalAllocation.Should().Be(latestProfilingVersion.ProfileTotals.Sum(
                                                                     profileTotal => profileTotal.Value));
            profilingViewModelResult.PreviousAllocation.Should().Be(previousProfilingVersion.ProfileTotals.Sum(
                                                                        profileTotal => profileTotal.Value));

            List <ProfilingInstallment> profilingInstallments = profilingViewModelResult.ProfilingInstallments.ToList();

            profilingInstallments[0].InstallmentMonth.Should()
            .Be(aProfileTotalOneYearInThePast.TypeValue);
            profilingInstallments[0].InstallmentYear.Should()
            .Be(aProfileTotalOneYearInThePast.Year);
            profilingInstallments[0].InstallmentNumber.Should()
            .Be(aProfileTotalOneYearInThePast.Occurrence);
            profilingInstallments[0].InstallmentValue.Should()
            .Be(aProfileTotalOneYearInThePast.Value);
            profilingInstallments[0].IsPaid.Should()
            .Be(true);
            profilingInstallments[1].IsPaid.Should()
            .Be(true);
            profilingInstallments[2].IsPaid.Should()
            .Be(false);
        }
Esempio n. 4
0
 private int GetProfileTotalPatternHash(ProfileTotal profileTotal)
 => HashCode.Combine(profileTotal?.Year,
                     profileTotal?.TypeValue,
                     profileTotal?.Occurrence);
Esempio n. 5
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));
        }
Esempio n. 6
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));
        }