Ejemplo n.º 1
0
        public async Task <ArrangementRequest> CalculateAsCalculationService(
            ArrangementRequest arrangementRequest, PriceCalculationParameters priceCalculationParameters)
        {
            List <PricedScheduledPeriod> pricedAndScheduledPeriods = null;

            if (priceCalculationParameters.ScheduledPeriods != null)
            {
                // var conversionMethod = await _configurationService.GetEffective("offer/fee-currency-conversion-method", "Buy to middle");
                pricedAndScheduledPeriods = SchedulingPeriodsResolver.PricePeriods(arrangementRequest,
                                                                                   priceCalculationParameters, _offerPriceCalculation);
            }

            // Perform price calculation for main conditions (whole repayment period)
            List <ScheduledPeriod> scheduledPeriods = new List <ScheduledPeriod>();
            var hasPeriods = priceCalculationParameters.ScheduledPeriods != null && priceCalculationParameters.ScheduledPeriods.Count() > 0;

            if (hasPeriods)
            {
                priceCalculationParameters.ScheduledPeriods.ForEach(p => scheduledPeriods.Add(p));
                priceCalculationParameters.ScheduledPeriods.Clear();
            }
            _ = await _offerPriceCalculation.CalculatePrice(arrangementRequest, priceCalculationParameters);

            if (hasPeriods)
            {
                priceCalculationParameters.ScheduledPeriods = scheduledPeriods;
            }

            var request = new CalculateInstallmentPlanRequestCS
            {
                Amount = (double)(arrangementRequest is FinanceServiceArrangementRequest fR ? fR.Amount : 0),
                RegularInterestPercentage = (double)(arrangementRequest?.Conditions?.InterestRates?.Where(r => r.Kind == InterestRateKinds.RegularInterest && string.IsNullOrEmpty(r.Periods)).Select(r => r.CalculatedRate).FirstOrDefault() ?? 0),
                // todo check
                StartDate = priceCalculationParameters.RequestDate,
                Currency  = arrangementRequest is FinanceServiceArrangementRequest fRc?GetCurrencyCode(fRc.Currency) : "978",
                                RegularInterestUnitOfTime = CalculationService.Services.SimpleUnitOfTime.Y,
                                Term    = arrangementRequest is FinanceServiceArrangementRequest fRt ? fRt.Term : "0",
                                Periods = pricedAndScheduledPeriods
            };

            request = AppendFees(request, arrangementRequest);
            request = AppendPredefinedPeriods(request, arrangementRequest);

            var plan = _calculator.CalculateInstallmentPlan(request);

            arrangementRequest.CalculationDate      = priceCalculationParameters.RequestDate;
            arrangementRequest.NumberOfInstallments = (int)(plan.NumberOfInstallments ?? 0);
            arrangementRequest.InstallmentPlan      = AggregatesModel.ApplicationAggregate.InstallmentPlanRow.FromInstallmentPlanCSList(plan.Installments);
            // TODO Think about this casts
            if (arrangementRequest is FinanceServiceArrangementRequest fSR)
            {
                fSR.Eapr = (decimal)(plan.EffectiveInterestRate ?? 0);
                if (arrangementRequest is TermLoanRequest trl)
                {
                    trl.Annuity = (decimal)(plan.Annuity ?? 0);
                }
            }
            return(arrangementRequest);
        }
Ejemplo n.º 2
0
        /*public static List<PricedScheduledPeriod> ScheduleAndPricePeriods(ResolveSchedulingPeriodsRequest request,
         *  ArrangementRequest arrangementRequest, PriceCalculationParameters priceParams, OfferPriceCalculation priceCalc,
         *  string conversionMethod)
         * {
         *  var scheduledAndPricedPeriods = new List<PricedScheduledPeriod>();
         *  ArrangementRequest arrangementRequestTemp;
         *  foreach (var period in priceParams.ScheduledPeriods)
         *  {
         *      arrangementRequestTemp = Mapper.Map<ArrangementRequest, ArrangementRequest>(arrangementRequest);
         *      arrangementRequestTemp.CalculateOffer(priceParams, priceCalc, conversionMethod);
         *      scheduledAndPricedPeriods.Add(new PricedScheduledPeriod
         *      {
         *          Percentage = (double)(arrangementRequestTemp?.Conditions?.InterestRates?.Where(r => r.Kind == InterestRateKinds.RegularInterest).Select(r => r.CalculatedRate).FirstOrDefault() ?? 0),
         *          PeriodType = period.PeriodType,
         *          StartDate = period.StartDate,
         *          EndDate = period.EndDate,
         *          // TODO To be resolved from DMN maybe?
         *          UnitOfTime = CalculationService.Services.SimpleUnitOfTime.Y
         *      });
         *  }
         *  return scheduledAndPricedPeriods;
         * }*/

        public static List <PricedScheduledPeriod> PricePeriods(
            ArrangementRequest arrangementRequest, PriceCalculationParameters priceParams,
            OfferPriceCalculation priceCalc)
        {
            var scheduledAndPricedPeriods = new List <PricedScheduledPeriod>();
            var scheduledPeriods          = new List <ScheduledPeriod>();
            var rates = new List <InterestRateCondition>();

            if (priceParams.ScheduledPeriods != null)
            {
                priceParams.ScheduledPeriods.ForEach(p => scheduledPeriods.Add(p));
            }
            if (priceParams.InterestRates != null)
            {
                priceParams.InterestRates.ForEach(r => rates.Add(r));
            }
            foreach (var period in scheduledPeriods)
            {
                priceParams.ScheduledPeriods.Clear();
                priceParams.ScheduledPeriods.Add(period);
                _ = priceCalc.CalculatePrice(arrangementRequest, priceParams).Result;
                // arrangementRequest.CalculateOffer(priceParams, priceCalc, conversionMethod);
                scheduledAndPricedPeriods.Add(new PricedScheduledPeriod
                {
                    Percentage = (double)(arrangementRequest?.Conditions?.InterestRates?.Where(r => r.Kind == InterestRateKinds.RegularInterest).Select(r => r.CalculatedRate).FirstOrDefault() ?? 0),
                    PeriodType = period.PeriodType,
                    StartDate  = period.StartDate,
                    EndDate    = period.EndDate,
                    // TODO To be resolved from DMN maybe?
                    UnitOfTime = CalculationService.Services.SimpleUnitOfTime.Y
                });
            }
            if (scheduledPeriods != null)
            {
                priceParams.ScheduledPeriods = scheduledPeriods;
            }
            if (rates != null)
            {
                priceParams.InterestRates = rates;
            }
            return(scheduledAndPricedPeriods);
        }