public AmortizationScheduleResponseDto Calculate(AmortizationScheduleRequestDto request)
        {
            var response = new AmortizationScheduleResponseDto();

            var validator = new FrenchValidator();

            var validationResult = validator.Validate(request);

            if (!validationResult.IsValid)
            {
                response = validationResult.ToErrorEntiti <ValidationResult, AmortizationScheduleResponseDto>();

                return(response);
            }
            response.Status = 200;

            InitPeriods(ref response, request.AmortizationSchedule.NumberPayments);

            var previousPeriod = new PeriodEntiti();

            response.AmortizationSchedule.PeriodsList.ToList().ForEach(period =>
            {
                CalculatePeriod(ref period, request, previousPeriod);

                previousPeriod = period;
            });

            return(response);
        }
Ejemplo n.º 2
0
        private void CalculatePeriod(ref PeriodEntiti period, AmortizationScheduleRequestDto request, PeriodEntiti previousPeriod)
        {
            var interesMensual = (request.AmortizationSchedule.AnnualInterest / int.Parse(FinantialDefinitionsResource.Porcent)) / int.Parse(FinantialDefinitionsResource.MonthsInAYear);

            if (period.NumPeriod == int.Parse(FinantialDefinitionsResource.InitialPeriod))
            {
                previousPeriod.SaldoFinal   = request.AmortizationSchedule.LoanAmount;
                previousPeriod.AbonoCapital = request.AmortizationSchedule.LoanAmount / request.AmortizationSchedule.NumberPayments;
                period.Intereses            = Math.Truncate(100 * (previousPeriod.SaldoFinal * interesMensual)) / 100;
            }

            period.SaldoInicial = previousPeriod.SaldoFinal;
            period.Intereses    = Math.Truncate(100 * (period.SaldoInicial * interesMensual)) / 100;
            period.AbonoCapital = Math.Truncate(100 * (previousPeriod.AbonoCapital)) / 100;
            period.Cuota        = Math.Truncate(100 * (period.Intereses + period.AbonoCapital)) / 100;
            period.SaldoFinal   = period.SaldoInicial - previousPeriod.AbonoCapital;
        }
        private void CalculatePeriod(ref PeriodEntiti period, AmortizationScheduleRequestDto request, PeriodEntiti previousPeriod)
        {
            var interesMensual = (request.AmortizationSchedule.AnnualInterest / int.Parse(FinantialDefinitionsResource.Porcent)) / int.Parse(FinantialDefinitionsResource.MonthsInAYear);

            if (period.NumPeriod == int.Parse(FinantialDefinitionsResource.InitialPeriod))
            {
                previousPeriod.SaldoFinal = request.AmortizationSchedule.LoanAmount;

                var numberOne  = int.Parse(FinantialDefinitionsResource.OneNumber);
                var baseNumber = (double)(numberOne + interesMensual);
                var pow        = (decimal)Math.Pow(baseNumber, request.AmortizationSchedule.NumberPayments);

                previousPeriod.Cuota = request.AmortizationSchedule.LoanAmount * ((pow * interesMensual) / (pow - numberOne));
            }

            period.SaldoInicial = previousPeriod.SaldoFinal;
            period.Cuota        = previousPeriod.Cuota;
            period.Intereses    = period.SaldoInicial * interesMensual;
            period.AbonoCapital = period.Cuota - period.Intereses;
            period.SaldoFinal   = period.SaldoInicial - period.AbonoCapital;
        }