-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FINERACT-1981: separate library to generate loan schedule
- Loading branch information
Showing
4 changed files
with
253 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
40 changes: 40 additions & 0 deletions
40
...he/fineract/portfolio/loanaccount/loanschedule/domain/LoanRepaymentScheduleModelData.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,40 @@ | ||
/** | ||
* 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.loanaccount.loanschedule.domain; | ||
|
||
import jakarta.validation.constraints.NotBlank; | ||
import jakarta.validation.constraints.NotNull; | ||
import java.math.BigDecimal; | ||
import java.time.LocalDate; | ||
import java.util.List; | ||
import java.util.Set; | ||
import org.apache.fineract.organisation.monetary.domain.ApplicationCurrency; | ||
import org.apache.fineract.portfolio.common.domain.DaysInMonthType; | ||
import org.apache.fineract.portfolio.common.domain.DaysInYearType; | ||
import org.apache.fineract.portfolio.loanaccount.data.HolidayDetailDTO; | ||
import org.apache.fineract.portfolio.loanaccount.data.LoanTermVariationsData; | ||
import org.apache.fineract.portfolio.loanaccount.domain.LoanCharge; | ||
|
||
public record LoanRepaymentScheduleModelData(@NotNull LocalDate scheduleGenerationStartDate, @NotNull ApplicationCurrency currency, | ||
@NotNull BigDecimal disbursementAmount, @NotNull LocalDate disbursementDate, @NotNull int numberOfRepayments, | ||
@NotNull int repaymentFrequency, @NotBlank String repaymentFrequencyType, @NotNull BigDecimal nominalInterestRate, | ||
@NotNull boolean downPaymentEnabled, @NotNull DaysInMonthType daysInMonth, @NotNull DaysInYearType daysInYear, | ||
BigDecimal downPaymentPercentage, Integer installmentAmountInMultiplesOf, Integer fixedLength, | ||
@NotNull List<LoanTermVariationsData> loanTermVariations, HolidayDetailDTO holidayDetailDTO, Set<LoanCharge> loanCharges) { | ||
} |
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
135 changes: 135 additions & 0 deletions
135
.../apache/fineract/portfolio/loanaccount/loanschedule/domain/LoanScheduleGeneratorTest.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,135 @@ | ||
/** | ||
* 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.loanaccount.loanschedule.domain; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.when; | ||
|
||
import java.math.BigDecimal; | ||
import java.math.MathContext; | ||
import java.math.RoundingMode; | ||
import java.time.LocalDate; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Set; | ||
import org.apache.fineract.infrastructure.core.data.EnumOptionData; | ||
import org.apache.fineract.organisation.monetary.domain.ApplicationCurrency; | ||
import org.apache.fineract.organisation.monetary.domain.MonetaryCurrency; | ||
import org.apache.fineract.organisation.monetary.domain.Money; | ||
import org.apache.fineract.organisation.monetary.domain.MoneyHelper; | ||
import org.apache.fineract.portfolio.common.domain.DaysInMonthType; | ||
import org.apache.fineract.portfolio.common.domain.DaysInYearType; | ||
import org.apache.fineract.portfolio.loanaccount.data.HolidayDetailDTO; | ||
import org.apache.fineract.portfolio.loanaccount.data.LoanTermVariationsData; | ||
import org.apache.fineract.portfolio.loanaccount.domain.LoanCharge; | ||
import org.apache.fineract.portfolio.loanaccount.domain.LoanTermVariationType; | ||
import org.apache.fineract.portfolio.loanproduct.calc.ProgressiveEMICalculator; | ||
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 LoanScheduleGeneratorTest { | ||
|
||
private static final ProgressiveEMICalculator emiCalculator = new ProgressiveEMICalculator(null); | ||
private static MockedStatic<MoneyHelper> moneyHelper = Mockito.mockStatic(MoneyHelper.class); | ||
private static final ApplicationCurrency applicationCurrency = new ApplicationCurrency("USD", "USD", 2, 1, "USD", "$"); | ||
private static final MonetaryCurrency monetaryCurrency = MonetaryCurrency.fromApplicationCurrency(applicationCurrency); | ||
|
||
@BeforeAll | ||
public static void init() { | ||
moneyHelper.when(MoneyHelper::getRoundingMode).thenReturn(RoundingMode.HALF_EVEN); | ||
moneyHelper.when(MoneyHelper::getMathContext).thenReturn(new MathContext(12, RoundingMode.HALF_EVEN)); | ||
} | ||
|
||
@Test | ||
void testGenerateLoanSchedule() { | ||
HolidayDetailDTO holidayDetailDTO = null; | ||
Set<LoanCharge> loanCharges = Collections.emptySet(); | ||
List<LoanTermVariationsData> loanTermVariationsDataForTest = createLoanTermVariationsDataForTest(); | ||
|
||
LoanRepaymentScheduleModelData modelData = new LoanRepaymentScheduleModelData(LocalDate.of(2024, 1, 1), // schedule | ||
// generation | ||
// start | ||
// date | ||
applicationCurrency, // currency | ||
BigDecimal.valueOf(192.22), // disbursement amount | ||
LocalDate.of(2024, 1, 15), // disbursement date | ||
6, // number of repayments | ||
1, // repayment frequency | ||
"MONTHS", // repayment frequency type | ||
BigDecimal.valueOf(9.99), // nominal interest rate | ||
true, // downpayment enabled | ||
DaysInMonthType.DAYS_30, // daysInMonth | ||
DaysInYearType.DAYS_360, // daysInYear | ||
null, // downpayment percentage (optional) | ||
null, // installmentAmountInMultiplesOf (optional) | ||
null, // fixed_length (optional) | ||
loanTermVariationsDataForTest, // loanTermVariationsDataForTest | ||
holidayDetailDTO, // holidayDetailDTO | ||
loanCharges // loanCharges | ||
); | ||
|
||
final MathContext mc = MoneyHelper.getMathContext(); | ||
|
||
final List<LoanScheduleModelRepaymentPeriod> expectedRepaymentPeriods = new ArrayList<>(); | ||
|
||
expectedRepaymentPeriods.add(repayment(1, LocalDate.of(2024, 1, 1), LocalDate.of(2024, 2, 1))); | ||
expectedRepaymentPeriods.add(repayment(2, LocalDate.of(2024, 2, 1), LocalDate.of(2024, 3, 1))); | ||
expectedRepaymentPeriods.add(repayment(3, LocalDate.of(2024, 3, 1), LocalDate.of(2024, 4, 1))); | ||
expectedRepaymentPeriods.add(repayment(4, LocalDate.of(2024, 4, 1), LocalDate.of(2024, 5, 1))); | ||
expectedRepaymentPeriods.add(repayment(5, LocalDate.of(2024, 5, 1), LocalDate.of(2024, 6, 1))); | ||
expectedRepaymentPeriods.add(repayment(6, LocalDate.of(2024, 6, 1), LocalDate.of(2024, 7, 1))); | ||
|
||
ScheduledDateGenerator mockScheduledDateGenerator = Mockito.mock(ScheduledDateGenerator.class); | ||
ProgressiveLoanScheduleGenerator generator = new ProgressiveLoanScheduleGenerator(mockScheduledDateGenerator, emiCalculator); | ||
when(mockScheduledDateGenerator.generateRepaymentPeriods(any(), any(), any())).thenReturn(expectedRepaymentPeriods); | ||
LoanScheduleModel loanSchedule = generator.generate(mc, modelData); | ||
|
||
List<LoanScheduleModelPeriod> periods = loanSchedule.getPeriods(); | ||
|
||
assertEquals(7, periods.size(), "Expected 5 periods including the downpayment period."); | ||
} | ||
|
||
public List<LoanTermVariationsData> createLoanTermVariationsDataForTest() { | ||
Long id = 1L; | ||
EnumOptionData termType = new EnumOptionData(1L, LoanTermVariationType.EMI_AMOUNT.getCode(), "Interest Rate Change"); | ||
LocalDate termVariationApplicableFrom = LocalDate.of(2024, 1, 1); | ||
BigDecimal decimalValue = BigDecimal.valueOf(5.0); | ||
LocalDate dateValue = LocalDate.of(2024, 12, 31); | ||
boolean isSpecificToInstallment = true; | ||
|
||
List<LoanTermVariationsData> loanTermVariationsDataList = new ArrayList<>(); | ||
loanTermVariationsDataList.add( | ||
new LoanTermVariationsData(id, termType, termVariationApplicableFrom, decimalValue, dateValue, isSpecificToInstallment)); | ||
|
||
return loanTermVariationsDataList; | ||
} | ||
|
||
private static LoanScheduleModelRepaymentPeriod repayment(int periodNumber, LocalDate fromDate, LocalDate dueDate) { | ||
final Money zeroAmount = Money.zero(monetaryCurrency); | ||
return LoanScheduleModelRepaymentPeriod.repayment(periodNumber, fromDate, dueDate, zeroAmount, zeroAmount, zeroAmount, zeroAmount, | ||
zeroAmount, zeroAmount, false); | ||
} | ||
} |