forked from apache/fineract
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FINERACT-1981: Rate factor using simple interest for EMI calculation
- Loading branch information
Showing
3 changed files
with
168 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
...c/main/java/org/apache/fineract/portfolio/loanproduct/ratefactor/RateFactorFunctions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/** | ||
* 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; | ||
|
||
public class RateFactorFunctions { | ||
|
||
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 MathContext mc) { | ||
final BigDecimal daysPeriod = BigDecimal.valueOf(daysInPeriod); | ||
final BigDecimal daysYear = BigDecimal.valueOf(daysInYear); | ||
|
||
return BigDecimal.ONE.add(interestRate.multiply(daysPeriod.divide(daysYear, mc), mc), mc); | ||
} | ||
|
||
} |
107 changes: 107 additions & 0 deletions
107
...st/java/org/apache/fineract/portfolio/loanproduct/ratefactor/RateFactorFunctionsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/** | ||
* 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.math.RoundingMode; | ||
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; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.MockedStatic; | ||
import org.mockito.Mockito; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class RateFactorFunctionsTest { | ||
|
||
private static MockedStatic<MoneyHelper> moneyHelper = Mockito.mockStatic(MoneyHelper.class); | ||
|
||
private static List<LoanRepaymentScheduleInstallment> periods; | ||
private final BigDecimal interestRate = BigDecimal.valueOf(0.09482); | ||
|
||
@BeforeAll | ||
public static void init() { | ||
periods = new ArrayList<>(); | ||
LocalDate startDate = LocalDate.of(2024, 01, 1); | ||
periods.add(createPeriod(1, startDate, startDate.plusMonths(1))); | ||
periods.add(createPeriod(2, startDate.plusMonths(1), startDate.plusMonths(2))); | ||
periods.add(createPeriod(3, startDate.plusMonths(2), startDate.plusMonths(3))); | ||
periods.add(createPeriod(4, startDate.plusMonths(3), startDate.plusMonths(4))); | ||
periods.add(createPeriod(5, startDate.plusMonths(4), startDate.plusMonths(5))); | ||
periods.add(createPeriod(6, startDate.plusMonths(5), startDate.plusMonths(6))); | ||
|
||
// When | ||
moneyHelper.when(() -> MoneyHelper.getRoundingMode()).thenReturn(RoundingMode.HALF_EVEN); | ||
moneyHelper.when(() -> MoneyHelper.getMathContext()).thenReturn(new MathContext(8, RoundingMode.HALF_EVEN)); | ||
} | ||
|
||
@Test | ||
public void testRateFactorFunctionDay365() { | ||
// Given | ||
final DaysInYearType daysInYearType = DaysInYearType.DAYS_365; | ||
final String[] expectedValues = new String[] { "1.0080532", "1.0075336", "1.0080532", "1.0077934", "1.0080532", "1.0077934" }; | ||
|
||
// Then | ||
for (LoanRepaymentScheduleInstallment period : periods) { | ||
final Long daysInPeriod = DateUtils.getDifferenceInDays(period.getFromDate(), period.getDueDate()); | ||
final Integer daysInYear = DateUtils.daysInYear(daysInYearType, period.getFromDate()); | ||
BigDecimal rateFactor = RateFactorFunctions.rateFactor(interestRate, daysInPeriod, daysInYear, MoneyHelper.getMathContext()); | ||
|
||
Assertions.assertEquals(expectedValues[period.getInstallmentNumber() - 1], rateFactor.toString()); | ||
} | ||
} | ||
|
||
@Test | ||
public void testRateFactorFunctionActual() { | ||
// Given | ||
final DaysInYearType daysInYearType = DaysInYearType.ACTUAL; | ||
final String[] expectedValues = new String[] { "1.0080312", "1.0075131", "1.0080312", "1.0077721", "1.0080312", "1.0077721" }; | ||
|
||
// Then | ||
for (LoanRepaymentScheduleInstallment period : periods) { | ||
final Long daysInPeriod = DateUtils.getDifferenceInDays(period.getFromDate(), period.getDueDate()); | ||
final Integer daysInYear = DateUtils.daysInYear(daysInYearType, period.getFromDate()); | ||
|
||
BigDecimal rateFactor = RateFactorFunctions.rateFactor(interestRate, daysInPeriod, daysInYear, MoneyHelper.getMathContext()); | ||
|
||
Assertions.assertEquals(expectedValues[period.getInstallmentNumber() - 1], rateFactor.toString()); | ||
} | ||
} | ||
|
||
@NotNull | ||
private static LoanRepaymentScheduleInstallment createPeriod(int periodId, LocalDate start, LocalDate end) { | ||
LoanRepaymentScheduleInstallment period = Mockito.mock(LoanRepaymentScheduleInstallment.class); | ||
Mockito.when(period.getInstallmentNumber()).thenReturn(periodId); | ||
Mockito.when(period.getFromDate()).thenReturn(start); | ||
Mockito.when(period.getDueDate()).thenReturn(end); | ||
|
||
return period; | ||
} | ||
|
||
} |