Skip to content

Commit

Permalink
FINERACT-2107: Allow interest rate change on fully paid installment f…
Browse files Browse the repository at this point in the history
…or Progressive loans
  • Loading branch information
leksinomi committed Nov 7, 2024
1 parent 492ec44 commit f964349
Show file tree
Hide file tree
Showing 6 changed files with 227 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -200,13 +200,13 @@ public static void validateSupportedParameters(JsonCommand jsonCommand, Set<Stri
}

public static void validateReschedulingInstallment(DataValidatorBuilder dataValidatorBuilder,
LoanRepaymentScheduleInstallment installment) {
LoanRepaymentScheduleInstallment installment, LoanScheduleType loanScheduleType) {
if (installment == null) {
dataValidatorBuilder.reset().parameter(RescheduleLoansApiConstants.rescheduleFromDateParamName)
.failWithCode("repayment.schedule.installment.does.not.exist", "Repayment schedule installment does not exist");
}

if (installment != null && installment.isObligationsMet()) {
if (installment != null && installment.isObligationsMet() && !LoanScheduleType.PROGRESSIVE.equals(loanScheduleType)) {
dataValidatorBuilder.reset().parameter(RescheduleLoansApiConstants.rescheduleFromDateParamName)
.failWithCode("repayment.schedule.installment.obligation.met", "Repayment schedule installment obligation met");
}
Expand Down Expand Up @@ -258,7 +258,8 @@ public void validateForCreateAction(final JsonCommand jsonCommand, final Loan lo
validateMultiDisburseLoan(loan, dataValidatorBuilder);

LoanRepaymentScheduleInstallment installment = loan.fetchLoanRepaymentScheduleInstallmentByDueDate(rescheduleFromDate);
validateReschedulingInstallment(dataValidatorBuilder, installment);
LoanScheduleType loanScheduleType = LoanScheduleType.valueOf(loan.getLoanProductRelatedDetail().getLoanScheduleType().name());
validateReschedulingInstallment(dataValidatorBuilder, installment, loanScheduleType);
validateForOverdueCharges(dataValidatorBuilder, loan, installment);

if (!dataValidationErrors.isEmpty()) {
Expand Down Expand Up @@ -308,8 +309,9 @@ public void validateForApproveAction(final JsonCommand jsonCommand, LoanReschedu

validateLoanIsActive(loan, dataValidatorBuilder);
installment = loan.fetchLoanRepaymentScheduleInstallmentByDueDate(rescheduleFromDate);
LoanScheduleType loanScheduleType = LoanScheduleType.valueOf(loan.getLoanProductRelatedDetail().getLoanScheduleType().name());

validateReschedulingInstallment(dataValidatorBuilder, installment);
validateReschedulingInstallment(dataValidatorBuilder, installment, loanScheduleType);
validateForOverdueCharges(dataValidatorBuilder, loan, installment);

if (!dataValidationErrors.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/**
* 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.rescheduleloan.data;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import org.apache.fineract.infrastructure.core.data.DataValidatorBuilder;
import org.apache.fineract.portfolio.loanaccount.domain.LoanRepaymentScheduleInstallment;
import org.apache.fineract.portfolio.loanaccount.loanschedule.domain.LoanScheduleType;
import org.apache.fineract.portfolio.loanaccount.rescheduleloan.RescheduleLoansApiConstants;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;

@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
class LoanRescheduleRequestDataValidatorImplTest {

@Mock
private DataValidatorBuilder dataValidatorBuilder;
@Mock
private LoanRepaymentScheduleInstallment installment;

@BeforeEach
void setUp() {
when(dataValidatorBuilder.reset()).thenReturn(dataValidatorBuilder);
when(dataValidatorBuilder.parameter(anyString())).thenReturn(dataValidatorBuilder);
}

@Test
void shouldFailWhenInstallmentIsNull() {
LoanRescheduleRequestDataValidatorImpl.validateReschedulingInstallment(dataValidatorBuilder, null, LoanScheduleType.PROGRESSIVE);

verify(dataValidatorBuilder).reset();
verify(dataValidatorBuilder).parameter(RescheduleLoansApiConstants.rescheduleFromDateParamName);
verify(dataValidatorBuilder).failWithCode("repayment.schedule.installment.does.not.exist",
"Repayment schedule installment does not exist");
}

@Test
void shouldFailWhenObligationsMetAndLoanScheduleTypeIsNotProgressive() {
when(installment.isObligationsMet()).thenReturn(true);

LoanRescheduleRequestDataValidatorImpl.validateReschedulingInstallment(dataValidatorBuilder, installment, LoanScheduleType.CUMULATIVE);

verify(dataValidatorBuilder).reset();
verify(dataValidatorBuilder).parameter(RescheduleLoansApiConstants.rescheduleFromDateParamName);
verify(dataValidatorBuilder).failWithCode("repayment.schedule.installment.obligation.met", "Repayment schedule installment obligation met");
}

@Test
void shouldNotFailWhenObligationsMetAndLoanScheduleTypeIsProgressive() {
when(installment.isObligationsMet()).thenReturn(true);

assertDoesNotThrow(() -> LoanRescheduleRequestDataValidatorImpl.validateReschedulingInstallment(dataValidatorBuilder, installment, LoanScheduleType.PROGRESSIVE));

verify(dataValidatorBuilder, never()).failWithCode(anyString(), anyString());
}

@Test
void shouldNotFailWhenObligationsNotMet() {
when(installment.isObligationsMet()).thenReturn(false);

assertDoesNotThrow(() -> LoanRescheduleRequestDataValidatorImpl.validateReschedulingInstallment(dataValidatorBuilder, installment, LoanScheduleType.CUMULATIVE));

verify(dataValidatorBuilder, never()).failWithCode(anyString(), anyString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import org.apache.fineract.portfolio.loanaccount.domain.Loan;
import org.apache.fineract.portfolio.loanaccount.domain.LoanRepaymentScheduleInstallment;
import org.apache.fineract.portfolio.loanaccount.domain.LoanStatus;
import org.apache.fineract.portfolio.loanaccount.loanschedule.domain.LoanScheduleType;
import org.apache.fineract.portfolio.loanaccount.rescheduleloan.RescheduleLoansApiConstants;
import org.apache.fineract.portfolio.loanaccount.rescheduleloan.domain.LoanRescheduleRequest;
import org.apache.fineract.portfolio.loanaccount.rescheduleloan.domain.LoanRescheduleRequestRepository;
Expand Down Expand Up @@ -112,7 +113,8 @@ public void validateForCreateAction(JsonCommand jsonCommand, Loan loan) {
installment = loan.fetchLoanRepaymentScheduleInstallmentByDueDate(rescheduleFromDate);
}

validateReschedulingInstallment(dataValidatorBuilder, installment);
LoanScheduleType loanScheduleType = LoanScheduleType.valueOf(loan.getLoanProductRelatedDetail().getLoanScheduleType().name());
validateReschedulingInstallment(dataValidatorBuilder, installment, loanScheduleType);
validateForOverdueCharges(dataValidatorBuilder, loan, installment);

if (!dataValidationErrors.isEmpty()) {
Expand Down Expand Up @@ -163,7 +165,8 @@ public void validateForApproveAction(JsonCommand jsonCommand, LoanRescheduleRequ
} else {
installment = loan.fetchLoanRepaymentScheduleInstallmentByDueDate(rescheduleFromDate);
}
validateReschedulingInstallment(dataValidatorBuilder, installment);
LoanScheduleType loanScheduleType = LoanScheduleType.valueOf(loan.getLoanProductRelatedDetail().getLoanScheduleType().name());
validateReschedulingInstallment(dataValidatorBuilder, installment, loanScheduleType);
validateForOverdueCharges(dataValidatorBuilder, loan, installment);

if (!dataValidationErrors.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import static org.apache.fineract.integrationtests.BaseLoanIntegrationTest.TransactionProcessingStrategyCode.ADVANCED_PAYMENT_ALLOCATION_STRATEGY;
import static org.apache.fineract.integrationtests.common.loans.LoanApplicationTestBuilder.DUE_PENALTY_INTEREST_PRINCIPAL_FEE_IN_ADVANCE_PENALTY_INTEREST_PRINCIPAL_FEE_STRATEGY;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
Expand Down Expand Up @@ -714,6 +715,35 @@ protected Long createDisbursementPercentageCharge(double percentageAmount) {
return chargeId.longValue();
}

protected void verifyRepaymentSchedule(GetLoansLoanIdResponse savedLoanResponse, GetLoansLoanIdResponse actualLoanResponse,
int totalPeriods, int identicalPeriods) {
List<GetLoansLoanIdRepaymentPeriod> savedPeriods = savedLoanResponse.getRepaymentSchedule().getPeriods();
List<GetLoansLoanIdRepaymentPeriod> actualPeriods = actualLoanResponse.getRepaymentSchedule().getPeriods();

assertEquals(totalPeriods, savedPeriods.size(), "Unexpected number of periods in savedPeriods list.");
assertEquals(totalPeriods, actualPeriods.size(), "Unexpected number of periods in actualPeriods list.");

verifyPeriodsEquality(savedPeriods, actualPeriods, 0, identicalPeriods, true);

verifyPeriodsEquality(savedPeriods, actualPeriods, identicalPeriods, totalPeriods, false);
}

private void verifyPeriodsEquality(List<GetLoansLoanIdRepaymentPeriod> savedPeriods, List<GetLoansLoanIdRepaymentPeriod> actualPeriods,
int startIndex, int endIndex, boolean shouldEqual) {
for (int i = startIndex; i < endIndex; i++) {
Double savedTotalDue = savedPeriods.get(i).getTotalDueForPeriod();
Double actualTotalDue = actualPeriods.get(i).getTotalDueForPeriod();

if (shouldEqual) {
assertEquals(savedTotalDue, actualTotalDue, String.format(
"Period %d should be identical in both responses. Expected: %s, Actual: %s", i + 1, savedTotalDue, actualTotalDue));
} else {
assertNotEquals(savedTotalDue, actualTotalDue, String
.format("Period %d should differ between responses. Saved: %s, Actual: %s", i + 1, savedTotalDue, actualTotalDue));
}
}
}

protected void verifyRepaymentSchedule(Long loanId, Installment... installments) {
GetLoansLoanIdResponse loanResponse = loanTransactionHelper.getLoan(requestSpec, responseSpec, loanId.intValue());
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(DATETIME_PATTERN);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
import org.apache.fineract.client.models.AdvancedPaymentData;
import org.apache.fineract.client.models.GetLoansLoanIdResponse;
import org.apache.fineract.client.models.PostClientsResponse;
import org.apache.fineract.client.models.PostCreateRescheduleLoansRequest;
import org.apache.fineract.client.models.PostCreateRescheduleLoansResponse;
Expand Down Expand Up @@ -369,6 +370,89 @@ public void testCreateLoanRescheduleChangeEMIRequest() {
this.createLoanRescheduleChangeEMIRequest();
}

@Test
public void givenProgressiveLoanWithPaidInstallmentWhenInterestRateChangedThenDueAmountUpdated() {
PostClientsResponse client = clientHelper.createClient(ClientHelper.defaultClientCreationRequest());
Integer commonLoanProductId = createProgressiveLoanProduct();

AtomicReference<PostLoansResponse> loanResponse = new AtomicReference<>();
runAt("2 February 2024", () -> {
loanResponse
.set(applyForLoanWithRecalculation(client.getClientId(), commonLoanProductId, "01 January 2024", "01 January 2024"));

approveAndDisburseLoan(loanResponse.get().getLoanId(), "01 January 2024", BigDecimal.valueOf(100));
makeRepayments(loanResponse.get().getLoanId().intValue());

GetLoansLoanIdResponse savedLoanResponse = loanTransactionHelper.getLoan(requestSpec, responseSpec,
loanResponse.get().getLoanId().intValue());

PostCreateRescheduleLoansResponse rescheduleLoansResponse = rescheduleLoanWithNewInterestRate(loanResponse.get().getLoanId(),
"2 February 2024", BigDecimal.ONE, "3 February 2024");

approveLoanReschedule(rescheduleLoansResponse.getResourceId(), "2 February 2024");

GetLoansLoanIdResponse actualLoanResponse = loanTransactionHelper.getLoan(requestSpec, responseSpec,
loanResponse.get().getLoanId().intValue());

verifyRepaymentSchedule(savedLoanResponse, actualLoanResponse, 7, 3);
});
}

private Integer createProgressiveLoanProduct() {
AdvancedPaymentData defaultAllocation = createDefaultPaymentAllocation("NEXT_INSTALLMENT");
final String loanProductJSON = new LoanProductTestBuilder().withNumberOfRepayments(numberOfRepayments)
.withinterestRatePerPeriod("7").withMaxTrancheCount("10").withMinPrincipal("1").withPrincipal("100")
.withInterestRateFrequencyTypeAsYear()
.withRepaymentStrategy(AdvancedPaymentScheduleTransactionProcessor.ADVANCED_PAYMENT_ALLOCATION_STRATEGY)
.withInterestTypeAsDecliningBalance().addAdvancedPaymentAllocation(defaultAllocation)
.withInterestRecalculationDetails("0", "4", "1").withRecalculationRestFrequencyType("2")
.withInterestCalculationPeriodTypeAsDays().withMultiDisburse().withDisallowExpectedDisbursements()
.withLoanScheduleType(LoanScheduleType.PROGRESSIVE).withLoanScheduleProcessingType(LoanScheduleProcessingType.HORIZONTAL)
.build(null);
return loanTransactionHelper.getLoanProductId(loanProductJSON);
}

private PostLoansResponse applyForLoanWithRecalculation(Long clientId, Integer loanProductId, String expectedDisbursementDate,
String submittedOnDate) {
return loanTransactionHelper.applyLoan(new PostLoansRequest().clientId(clientId).productId(loanProductId.longValue())
.expectedDisbursementDate(expectedDisbursementDate).dateFormat(DATETIME_PATTERN)
.transactionProcessingStrategyCode(AdvancedPaymentScheduleTransactionProcessor.ADVANCED_PAYMENT_ALLOCATION_STRATEGY)
.locale("en").submittedOnDate(submittedOnDate).amortizationType(1).interestRatePerPeriod(BigDecimal.valueOf(7))
.interestCalculationPeriodType(0).interestType(0).repaymentFrequencyType(2).repaymentEvery(1).numberOfRepayments(6)
.loanTermFrequency(6).loanTermFrequencyType(2).principal(BigDecimal.valueOf(100)).loanType("individual"));
}

private void approveAndDisburseLoan(Long loanId, String date, BigDecimal amount) {
loanTransactionHelper.approveLoan(loanId, createLoanApprovalRequest(date, amount));
loanTransactionHelper.disburseLoan(loanId, createDisbursementRequest(date, amount));
}

private PostLoansLoanIdRequest createLoanApprovalRequest(String date, BigDecimal amount) {
return new PostLoansLoanIdRequest().approvedLoanAmount(amount).dateFormat(DATETIME_PATTERN).approvedOnDate(date).locale("en");
}

private PostLoansLoanIdRequest createDisbursementRequest(String date, BigDecimal amount) {
return new PostLoansLoanIdRequest().actualDisbursementDate(date).dateFormat(DATETIME_PATTERN).transactionAmount(amount)
.locale("en");
}

private void makeRepayments(int loanId) {
loanTransactionHelper.makeRepayment("01 February 2024", 17.01f, loanId);
loanTransactionHelper.makeRepayment("02 February 2024", 17.01f, loanId);
}

private PostCreateRescheduleLoansResponse rescheduleLoanWithNewInterestRate(Long loanId, String submittedOnDate,
BigDecimal newInterestRate, String rescheduleFromDate) {
return loanRescheduleRequestHelper.createLoanRescheduleRequest(new PostCreateRescheduleLoansRequest().loanId(loanId)
.dateFormat(DATETIME_PATTERN).locale("en").submittedOnDate(submittedOnDate).newInterestRate(newInterestRate)
.rescheduleReasonId(1L).rescheduleFromDate(rescheduleFromDate));
}

private void approveLoanReschedule(Long rescheduleId, String approvedOnDate) {
loanRescheduleRequestHelper.approveLoanRescheduleRequest(rescheduleId,
new PostUpdateRescheduleLoansRequest().approvedOnDate(approvedOnDate).locale("en").dateFormat(DATETIME_PATTERN));
}

private PostLoansResponse applyForLoanApplication(final Long clientId, final Integer loanProductId, final BigDecimal principal,
final int loanTermFrequency, final int repaymentAfterEvery, final int numberOfRepayments, final BigDecimal interestRate,
final String expectedDisbursementDate, final String submittedOnDate, String transactionProcessorCode,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,11 @@ public LoanProductTestBuilder withDisallowExpectedDisbursements(boolean disallow
return this;
}

public LoanProductTestBuilder withDisallowExpectedDisbursements() {
this.disallowExpectedDisbursements = true;
return this;
}

public LoanProductTestBuilder withFullAccountingConfig(String accountingRule, FullAccountingConfig fullAccountingConfig) {
this.accountingRule = accountingRule;
this.fullAccountingConfig = fullAccountingConfig;
Expand Down Expand Up @@ -640,6 +645,11 @@ public LoanProductTestBuilder withInterestRecalculationRestFrequencyDetails(fina
return this;
}

public LoanProductTestBuilder withRecalculationRestFrequencyType(final String recalculationRestFrequencyType) {
this.recalculationRestFrequencyType = recalculationRestFrequencyType;
return this;
}

public LoanProductTestBuilder withInterestRecalculationCompoundingFrequencyDetails(final String recalculationCompoundingFrequencyType,
final String recalculationCompoundingFrequencyInterval, final Integer recalculationCompoundingFrequencyOnDayType,
final Integer recalculationCompoundingFrequencyDayOfWeekType) {
Expand Down

0 comments on commit f964349

Please sign in to comment.