diff --git a/fineract-loan/src/main/java/org/apache/fineract/portfolio/loanaccount/rescheduleloan/data/LoanRescheduleRequestDataValidator.java b/fineract-loan/src/main/java/org/apache/fineract/portfolio/loanaccount/rescheduleloan/data/LoanRescheduleRequestDataValidator.java index c41fb4da6cc..87dc0f86fd4 100644 --- a/fineract-loan/src/main/java/org/apache/fineract/portfolio/loanaccount/rescheduleloan/data/LoanRescheduleRequestDataValidator.java +++ b/fineract-loan/src/main/java/org/apache/fineract/portfolio/loanaccount/rescheduleloan/data/LoanRescheduleRequestDataValidator.java @@ -19,7 +19,9 @@ package org.apache.fineract.portfolio.loanaccount.rescheduleloan.data; import org.apache.fineract.infrastructure.core.api.JsonCommand; +import org.apache.fineract.infrastructure.core.data.DataValidatorBuilder; import org.apache.fineract.portfolio.loanaccount.domain.Loan; +import org.apache.fineract.portfolio.loanaccount.domain.LoanRepaymentScheduleInstallment; import org.apache.fineract.portfolio.loanaccount.rescheduleloan.domain.LoanRescheduleRequest; public interface LoanRescheduleRequestDataValidator { @@ -29,4 +31,6 @@ public interface LoanRescheduleRequestDataValidator { void validateForApproveAction(JsonCommand jsonCommand, LoanRescheduleRequest loanRescheduleRequest); void validateForRejectAction(JsonCommand jsonCommand, LoanRescheduleRequest loanRescheduleRequest); + + void validateReschedulingInstallment(DataValidatorBuilder dataValidatorBuilder, LoanRepaymentScheduleInstallment installment); } diff --git a/fineract-loan/src/main/java/org/apache/fineract/portfolio/loanaccount/rescheduleloan/data/LoanRescheduleRequestDataValidatorImpl.java b/fineract-loan/src/main/java/org/apache/fineract/portfolio/loanaccount/rescheduleloan/data/LoanRescheduleRequestDataValidatorImpl.java index bb6c02a389a..7bc58bd2390 100644 --- a/fineract-loan/src/main/java/org/apache/fineract/portfolio/loanaccount/rescheduleloan/data/LoanRescheduleRequestDataValidatorImpl.java +++ b/fineract-loan/src/main/java/org/apache/fineract/portfolio/loanaccount/rescheduleloan/data/LoanRescheduleRequestDataValidatorImpl.java @@ -199,8 +199,8 @@ public static void validateSupportedParameters(JsonCommand jsonCommand, Set progressiveLoanRescheduleRequestDataValidator.validateReschedulingInstallment(dataValidatorBuilder, installment)); + + verify(dataValidatorBuilder, never()).failWithCode(anyString(), anyString()); + } +} diff --git a/integration-tests/src/test/java/org/apache/fineract/integrationtests/BaseLoanIntegrationTest.java b/integration-tests/src/test/java/org/apache/fineract/integrationtests/BaseLoanIntegrationTest.java index 54159aefd54..bee30bc8c63 100644 --- a/integration-tests/src/test/java/org/apache/fineract/integrationtests/BaseLoanIntegrationTest.java +++ b/integration-tests/src/test/java/org/apache/fineract/integrationtests/BaseLoanIntegrationTest.java @@ -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; @@ -714,6 +715,35 @@ protected Long createDisbursementPercentageCharge(double percentageAmount) { return chargeId.longValue(); } + protected void verifyRepaymentSchedule(GetLoansLoanIdResponse savedLoanResponse, GetLoansLoanIdResponse actualLoanResponse, + int totalPeriods, int identicalPeriods) { + List savedPeriods = savedLoanResponse.getRepaymentSchedule().getPeriods(); + List 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 savedPeriods, List 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); diff --git a/integration-tests/src/test/java/org/apache/fineract/integrationtests/LoanRescheduleRequestTest.java b/integration-tests/src/test/java/org/apache/fineract/integrationtests/LoanRescheduleRequestTest.java index 2090b7ba7eb..89b8e416703 100644 --- a/integration-tests/src/test/java/org/apache/fineract/integrationtests/LoanRescheduleRequestTest.java +++ b/integration-tests/src/test/java/org/apache/fineract/integrationtests/LoanRescheduleRequestTest.java @@ -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; @@ -369,6 +370,89 @@ public void testCreateLoanRescheduleChangeEMIRequest() { this.createLoanRescheduleChangeEMIRequest(); } + @Test + public void givenProgressiveLoanWithPaidInstallmentWhenInterestRateChangedThenDueAmountUpdated() { + PostClientsResponse client = clientHelper.createClient(ClientHelper.defaultClientCreationRequest()); + Integer commonLoanProductId = createProgressiveLoanProduct(); + + AtomicReference 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, diff --git a/integration-tests/src/test/java/org/apache/fineract/integrationtests/common/loans/LoanProductTestBuilder.java b/integration-tests/src/test/java/org/apache/fineract/integrationtests/common/loans/LoanProductTestBuilder.java index 928988db85d..e9439c25922 100644 --- a/integration-tests/src/test/java/org/apache/fineract/integrationtests/common/loans/LoanProductTestBuilder.java +++ b/integration-tests/src/test/java/org/apache/fineract/integrationtests/common/loans/LoanProductTestBuilder.java @@ -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; @@ -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) {