adamsaghy commented on code in PR #3902: URL: https://github.com/apache/fineract/pull/3902#discussion_r1615784412
########## fineract-progressive-loan/src/main/java/org/apache/fineract/portfolio/loanproduct/ratefactor/RateFactorFunctions.java: ########## @@ -0,0 +1,134 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.fineract.portfolio.loanproduct.ratefactor; + +import java.math.BigDecimal; +import java.math.MathContext; +import java.time.LocalDate; +import java.util.ArrayList; +import java.util.List; +import org.apache.fineract.infrastructure.core.service.DateUtils; +import org.apache.fineract.organisation.monetary.domain.MoneyHelper; +import org.apache.fineract.portfolio.common.domain.DaysInYearType; +import org.apache.fineract.portfolio.loanaccount.domain.LoanRepaymentScheduleInstallment; + +public class RateFactorFunctions { + + public static final Integer PRECISION = 8; + + protected RateFactorFunctions() {} + + /** + * To calculate the monthly payment, we first need to calculate something called the Rate Factor. We're going to be + * using simple interest. The Rate Factor for simple interest is calculated by the following formula: + * + * + * R = 1 + (r * d / y) + * + * @param interestRate + * (r) + * @param daysInPeriod + * (d) + * @param daysInYear + * (y) + */ + public static BigDecimal rateFactor(final BigDecimal interestRate, final Long daysInPeriod, final Integer daysInYear, + final Integer precision) { + final BigDecimal daysPeriod = BigDecimal.valueOf(daysInPeriod); + BigDecimal daysYear = BigDecimal.valueOf(daysInYear); + final MathContext mc = new MathContext(precision, MoneyHelper.getRoundingMode()); + + return BigDecimal.ONE.add(interestRate.multiply(daysPeriod.divide(daysYear, mc), mc), mc); + } + + /** + * To calculate the function value for each period, we are going to use the next formula: + * + * fn = 1 + fnValueFrom * rateFactorEnd + * + * @param periodFrom + * + * @param periodEnd + * + * @param mathContext + * + */ + public static RateFactorPeriodData fnValue(RateFactorPeriodData periodFrom, final RateFactorPeriodData periodEnd, + final MathContext mc) { + BigDecimal fnValue = BigDecimal.ONE; + if (periodEnd.getPeriod() > 1) { + fnValue = BigDecimal.ONE.add(periodFrom.getFnValue().multiply(periodEnd.getRateFactor(), mc)); + } + periodEnd.setFnValue(fnValue); + return periodEnd; + } + + /** + * + * + * emi = (R^n * principal) / fnValue + * + * @param periods + * + * @param principal + * + * @param mathContext + * + */ + public static BigDecimal calculateEMI(final List<RateFactorPeriodData> periods, final BigDecimal principal, final MathContext mc, + final Integer precision) { + final BigDecimal lastFnValue = periods.get(periods.size() - 1).getFnValue(); + BigDecimal rnValue = BigDecimal.ONE; + for (RateFactorPeriodData rateFactorPeriod : periods) { + if (rateFactorPeriod.getPeriod() == 1) { + rnValue = rateFactorPeriod.getRateFactor(); + } else { + rnValue = rnValue.multiply(rateFactorPeriod.getRateFactor(), mc); + } + } + BigDecimal emiAmount = rnValue.multiply(principal.divide(lastFnValue, mc), mc); + return emiAmount.setScale(precision, MoneyHelper.getRoundingMode()); Review Comment: We dont need to rescale. The MathContext should contain the required precision. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
