예제 #1
0
 private IReadOnlyCollection <DeliveryProfilePeriod> GetDistributionPeriodForAllocation(
     IReadOnlyCollection <DeliveryProfilePeriod> profilePattern)
 {
     return(profilePattern
            .Select(ppp => DeliveryProfilePeriod.CreateInstance(ppp.TypeValue, ppp.Occurrence, ppp.Type, ppp.Year, ppp.ProfileValue, ppp.DistributionPeriod))
            .ToList());
 }
예제 #2
0
 private IReadOnlyCollection <DeliveryProfilePeriod> GetProfilePeriodsForAllocation(
     IReadOnlyCollection <ProfilePeriodPattern> profilePattern)
 {
     return(profilePattern
            .Select(ppp => DeliveryProfilePeriod.CreateInstance(ppp.Period, ppp.Occurrence, ppp.PeriodType, ppp.PeriodYear, 0m, ppp.DistributionPeriod))
            .Distinct()
            .ToList());
 }
예제 #3
0
 public static DeliveryProfilePeriod WithValue(this DeliveryProfilePeriod original, decimal newValue)
 {
     return(DeliveryProfilePeriod.CreateInstance(original.TypeValue,
                                                 original.Occurrence,
                                                 original.Type,
                                                 original.Year,
                                                 newValue,
                                                 original.DistributionPeriod));
 }
        public async Task CalculateProfileService_ShouldCorrectlyCalculateNormalPESportsPremium()
        {
            // arrange
            FundingStreamPeriodProfilePattern pattern = TestResource.FromJson <FundingStreamPeriodProfilePattern>(
                NamespaceResourcesResideIn, "Resources.PESPORTSPREM.json");

            ProfileRequest peSportsPremReq = new ProfileRequest(
                fundingStreamId: "PSG",
                fundingPeriodId: "AY-1819",
                fundingLineCode: "FL1",
                fundingValue: 200);

            IProfilePatternRepository mockProfilePatternRepository = Substitute.For <IProfilePatternRepository>();

            mockProfilePatternRepository
            .GetProfilePattern(Arg.Any <string>(), Arg.Any <string>(), Arg.Any <string>(), Arg.Any <string>())
            .Returns(pattern);

            ICalculateProfileService calculateProfileService = GetCalculateProfileServiceWithMockedDependencies(mockProfilePatternRepository);

            // act
            IActionResult responseResult = await calculateProfileService.ProcessProfileAllocationRequest(peSportsPremReq);

            // assert
            responseResult
            .Should()
            .BeOfType <OkObjectResult>();

            OkObjectResult            responseAsOkObjectResult = responseResult as OkObjectResult;
            AllocationProfileResponse response = responseAsOkObjectResult.Value as AllocationProfileResponse;

            DeliveryProfilePeriod deliveryProfilePeriodReturnedForOct = response.DeliveryProfilePeriods.ToArray().FirstOrDefault(q => q.TypeValue == "October");
            DeliveryProfilePeriod deliveryProfilePeriodReturnedForApr = response.DeliveryProfilePeriods.ToArray().FirstOrDefault(q => q.TypeValue == "April");

            response.DeliveryProfilePeriods.Length.Should().Be(3);

            deliveryProfilePeriodReturnedForOct
            .Should().NotBeNull();
            deliveryProfilePeriodReturnedForOct
            .ProfileValue
            .Should().Be(117M);

            deliveryProfilePeriodReturnedForApr
            .Should().NotBeNull();
            deliveryProfilePeriodReturnedForApr
            .ProfileValue
            .Should().Be(83M);
        }
        public async Task ProfilesFundingLinesNormallyThenReProfilesUsingTheseResultsWithSameAmountStrategyIfFundingTheSame()
        {
            decimal newFundingTotal = NewRandomTotal();

            ReProfileRequest request = NewReProfileRequest(_ => _.WithFundingValue(newFundingTotal)
                                                           .WithExistingFundingValue(newFundingTotal));
            AllocationProfileResponse profileResponse = NewAllocationProfileResponse();

            string key = NewRandomString();

            FundingStreamPeriodProfilePattern profilePattern = NewFundingStreamPeriodProfilePattern(_ =>
                                                                                                    _.WithReProfilingConfiguration(NewProfilePatternReProfilingConfiguration(cfg =>
                                                                                                                                                                             cfg.WithIsEnabled(true)
                                                                                                                                                                             .WithSameAmountStrategyKey(key))));

            DistributionPeriods   distributionPeriods1   = NewDistributionPeriods();
            DeliveryProfilePeriod deliveryProfilePeriod1 = NewDeliveryProfilePeriod(_ => _.WithProfiledValue(10));
            DeliveryProfilePeriod deliveryProfilePeriod2 = NewDeliveryProfilePeriod(_ => _.WithProfiledValue(newFundingTotal - 20));

            GivenTheProfilePattern(request, profilePattern);
            AndTheReProfilingStrategy(key);
            AndTheProfiling(request, profilePattern, profileResponse);
            AndTheReProfilingStrategyResponse(profileResponse, request, profilePattern, NewReProfileStrategyResult(_ =>
                                                                                                                   _.WithDistributionPeriods(distributionPeriods1)
                                                                                                                   .WithDeliveryProfilePeriods(deliveryProfilePeriod1, deliveryProfilePeriod2)
                                                                                                                   .WithCarryOverAmount(10)));

            ActionResult <ReProfileResponse> reProfileResponse = await WhenTheFundingLineIsReProfiled(request);

            reProfileResponse?
            .Value
            .Should()
            .BeEquivalentTo(new ReProfileResponse
            {
                DistributionPeriods       = new [] { distributionPeriods1 },
                CarryOverAmount           = 10,
                DeliveryProfilePeriods    = new [] { deliveryProfilePeriod1, deliveryProfilePeriod2 },
                ProfilePatternKey         = profilePattern.ProfilePatternKey,
                ProfilePatternDisplayName = profilePattern.ProfilePatternDisplayName
            });
        }
예제 #6
0
        private IReadOnlyCollection <DeliveryProfilePeriod> ApplyProfilePattern(
            decimal fundingValue,
            IReadOnlyCollection <ProfilePeriodPattern> profilePattern,
            IReadOnlyCollection <DeliveryProfilePeriod> profilePeriods,
            RoundingStrategy roundingStrategy)
        {
            List <DeliveryProfilePeriod> calculatedDeliveryProfile = new List <DeliveryProfilePeriod>();

            if (profilePeriods.Any())
            {
                decimal allocationValueToBeProfiled = Convert.ToDecimal(fundingValue);
                decimal runningTotal = 0;

                List <DeliveryProfilePeriod> profiledValues = profilePeriods.Select(pp =>
                {
                    ProfilePeriodPattern profilePeriodPattern = profilePattern.Single(
                        pattern => string.Equals(pattern.Period, pp.TypeValue) &&
                        string.Equals(pattern.DistributionPeriod, pp.DistributionPeriod) &&
                        pattern.PeriodYear == pp.Year &&
                        pattern.Occurrence == pp.Occurrence);

                    decimal profilePercentage = profilePeriodPattern
                                                .PeriodPatternPercentage;

                    decimal profiledValue = profilePercentage * allocationValueToBeProfiled;
                    if (profiledValue != 0)
                    {
                        profiledValue /= 100;
                    }

                    decimal roundedValue;


                    if (roundingStrategy == RoundingStrategy.RoundUp)
                    {
                        roundedValue = profiledValue
                                       .RoundToDecimalPlaces(2)
                                       .RoundToDecimalPlaces(0);
                    }
                    else
                    {
                        roundedValue = (int)profiledValue;
                    }

                    if (runningTotal + roundedValue > allocationValueToBeProfiled)
                    {
                        roundedValue = allocationValueToBeProfiled - runningTotal;
                    }

                    runningTotal += roundedValue;

                    return(pp.WithValue(roundedValue));
                }).ToList();

                DeliveryProfilePeriod last = profiledValues.Last();

                IEnumerable <DeliveryProfilePeriod> withoutLast = profiledValues.Take(profiledValues.Count - 1).ToList();

                calculatedDeliveryProfile.AddRange(
                    withoutLast.Append(
                        last.WithValue(allocationValueToBeProfiled - withoutLast.Sum(cdp => cdp.ProfileValue))));
            }

            return(calculatedDeliveryProfile);
        }