FINERACT-1971: Installment level delinquency for loan modification

This commit is contained in:
Ruchi Dhamankar
2024-05-15 15:04:38 +02:00
committed by Adam Saghy
parent 582742d2ef
commit 8b2c9da804
6 changed files with 170 additions and 1 deletions
@@ -1372,6 +1372,8 @@ final class LoansApiResourceSwagger {
public List<PutLoansLoanIdDisbursementData> disbursementData;
@Schema(example = "HORIZONTAL")
public String loanScheduleProcessingType;
@Schema(example = "false")
public Boolean enableInstallmentLevelDelinquency;
static final class PutLoansLoanIdChanges {
@@ -71,7 +71,7 @@ public final class CalculateLoanScheduleQueryFromApiJsonHelper {
LoanApiConstants.repaymentFrequencyDayOfWeekTypeParameterName, LoanApiConstants.isTopup, LoanApiConstants.loanIdToClose,
LoanApiConstants.datatables, LoanApiConstants.isEqualAmortizationParam, LoanProductConstants.RATES_PARAM_NAME,
LoanApiConstants.daysInYearTypeParameterName, LoanApiConstants.fixedPrincipalPercentagePerInstallmentParamName,
LoanProductConstants.FIXED_LENGTH));
LoanProductConstants.FIXED_LENGTH, LoanProductConstants.ENABLE_INSTALLMENT_LEVEL_DELINQUENCY));
private final FromJsonHelper fromApiJsonHelper;
@@ -1134,6 +1134,20 @@ public final class LoanApplicationCommandFromApiJsonHelper {
advancedPaymentAllocationsValidator.checkGroupingOfAllocationRules(allocationRules);
}
if (this.fromApiJsonHelper.parameterExists(LoanProductConstants.ENABLE_INSTALLMENT_LEVEL_DELINQUENCY, element)) {
final Boolean isEnableInstallmentLevelDelinquency = this.fromApiJsonHelper
.extractBooleanNamed(LoanProductConstants.ENABLE_INSTALLMENT_LEVEL_DELINQUENCY, element);
baseDataValidator.reset().parameter(LoanProductConstants.ENABLE_INSTALLMENT_LEVEL_DELINQUENCY)
.value(isEnableInstallmentLevelDelinquency).validateForBooleanValue();
if (loanProduct.getDelinquencyBucket() == null) {
if (isEnableInstallmentLevelDelinquency) {
baseDataValidator.reset().parameter(LoanProductConstants.ENABLE_INSTALLMENT_LEVEL_DELINQUENCY).failWithCode(
"can.be.enabled.for.loan.with.loan.product.having.valid.delinquency.bucket",
"Installment level delinquency cannot be enabled for a loan if Delinquency bucket is not configured for loan product");
}
}
}
if (!dataValidationErrors.isEmpty()) {
throw new PlatformApiDataValidationException("validation.msg.validation.errors.exist", "Validation errors exist.",
dataValidationErrors);
@@ -970,6 +970,14 @@ public class LoanApplicationWritePlatformServiceJpaRepositoryImpl implements Loa
existingLoanApplication.updateLoanCharges(possiblyModifedLoanCharges);
}
// update installment level delinquency
if (command.isChangeInBooleanParameterNamed(LoanProductConstants.ENABLE_INSTALLMENT_LEVEL_DELINQUENCY,
existingLoanApplication.isEnableInstallmentLevelDelinquency())) {
final Boolean enableInstallmentLevelDelinquency = command
.booleanObjectValueOfParameterNamed(LoanProductConstants.ENABLE_INSTALLMENT_LEVEL_DELINQUENCY);
existingLoanApplication.updateEnableInstallmentLevelDelinquency(enableInstallmentLevelDelinquency);
}
if (changes.containsKey("recalculateLoanSchedule")) {
changes.remove("recalculateLoanSchedule");
@@ -20,6 +20,7 @@ package org.apache.fineract.integrationtests;
import static org.apache.fineract.infrastructure.businessdate.domain.BusinessDateType.BUSINESS_DATE;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import java.math.BigDecimal;
import java.util.List;
@@ -31,7 +32,10 @@ import org.apache.fineract.client.models.GetLoansLoanIdLoanInstallmentLevelDelin
import org.apache.fineract.client.models.GetLoansLoanIdResponse;
import org.apache.fineract.client.models.PostLoanProductsRequest;
import org.apache.fineract.client.models.PostLoanProductsResponse;
import org.apache.fineract.client.models.PostLoansResponse;
import org.apache.fineract.client.models.PutLoanProductsProductIdRequest;
import org.apache.fineract.client.models.PutLoansLoanIdRequest;
import org.apache.fineract.client.models.PutLoansLoanIdResponse;
import org.apache.fineract.client.util.CallFailedRuntimeException;
import org.apache.fineract.integrationtests.common.ClientHelper;
import org.apache.fineract.integrationtests.common.SchedulerJobHelper;
@@ -411,6 +415,139 @@ public class InstallmentLevelDelinquencyAPIIntegrationTests extends BaseLoanInte
}
@Test
public void testLoanInstallmentLevelSettingModification() {
runAt("31 May 2023", () -> {
// Create Client
Long clientId = clientHelper.createClient(ClientHelper.defaultClientCreationRequest()).getClientId();
// Create DelinquencyBuckets
Integer delinquencyBucketId = DelinquencyBucketsHelper.createDelinquencyBucket(requestSpec, responseSpec, List.of(//
Pair.of(1, 10), //
Pair.of(11, 30), //
Pair.of(31, 60), //
Pair.of(61, null)//
));
// Create Loan Product
PostLoanProductsRequest loanProductsRequest = create1InstallmentAmountInMultiplesOf4Period1MonthLongWithInterestAndAmortizationProduct(
InterestType.FLAT, AmortizationType.EQUAL_INSTALLMENTS);
// set delinquency bucket
loanProductsRequest.setDelinquencyBucketId(delinquencyBucketId.longValue());
PostLoanProductsResponse loanProductResponse = loanProductHelper.createLoanProduct(loanProductsRequest);
// Apply for loan
Long loanId = loanTransactionHelper
.applyLoan(applyLoanRequest(clientId, loanProductResponse.getResourceId(), "01 January 2023", 1250.0, 4))
.getResourceId();
// verify installment level delinquency setting for loan
GetLoansLoanIdResponse loanResponse = loanTransactionHelper.getLoan(requestSpec, responseSpec, loanId.intValue());
assertThat(loanResponse.getEnableInstallmentLevelDelinquency()).isFalse();
// Modify installment level delinquency as true for loan
PutLoansLoanIdResponse loansModificationResponse = loanTransactionHelper.modifyApplicationForLoan(loanId, "modify",
new PutLoansLoanIdRequest().clientId(clientId).productId(loanProductResponse.getResourceId()).loanType("individual")
.enableInstallmentLevelDelinquency(true).locale("en").dateFormat(DATETIME_PATTERN));
// verify installment level delinquency setting for loan
loanResponse = loanTransactionHelper.getLoan(requestSpec, responseSpec, loanId.intValue());
assertThat(loanResponse.getEnableInstallmentLevelDelinquency()).isTrue();
// Approve Loan
loanTransactionHelper.approveLoan(loanId, approveLoanRequest(1250.0, "01 January 2023"));
// Disburse Loan
disburseLoan(loanId, BigDecimal.valueOf(1250), "01 January 2023");
// Verify Repayment Schedule and Due Dates
verifyRepaymentSchedule(loanId, //
installment(1250.0, null, "01 January 2023"), //
installment(312.0, false, "31 January 2023"), // 120 days delinquent -> range4
installment(312.0, false, "02 March 2023"), // 90 days delinquent -> range4
installment(312.0, false, "01 April 2023"), // 60 days delinquent -> range3
installment(314.0, false, "01 May 2023") // 30 days delinquent -> range2
);
// since the installment level delinquency is modified as true for loan, therefore it
// is calculated
verifyDelinquency(loanId, 120, "1250.0", //
delinquency(11, 30, "314.0"), // 4th installment
delinquency(31, 60, "312.0"), // 3rd installment
delinquency(61, null, "624.0") // 1st installment + 2nd installment
);
});
}
@Test
public void tesInstallmentLevelSettingModificationForLoanWithLoanProductWithoutDelinquencyBucketValidation() {
runAt("31 May 2023", () -> {
// Create Client
Long clientId = clientHelper.createClient(ClientHelper.defaultClientCreationRequest()).getClientId();
// Create Loan Product without delinquency bucket
PostLoanProductsRequest loanProductsRequest = create1InstallmentAmountInMultiplesOf4Period1MonthLongWithInterestAndAmortizationProduct(
InterestType.FLAT, AmortizationType.EQUAL_INSTALLMENTS);
PostLoanProductsResponse loanProductResponse = loanProductHelper.createLoanProduct(loanProductsRequest);
// Apply for loan
Long loanId = loanTransactionHelper
.applyLoan(applyLoanRequest(clientId, loanProductResponse.getResourceId(), "01 January 2023", 1250.0, 4))
.getResourceId();
// Modify Loan with installment level delinquency setting
CallFailedRuntimeException callFailedRuntimeException = Assertions.assertThrows(CallFailedRuntimeException.class,
() -> loanTransactionHelper.modifyApplicationForLoan(loanId, "modify",
new PutLoansLoanIdRequest().clientId(clientId).productId(loanProductResponse.getResourceId())
.loanType("individual").enableInstallmentLevelDelinquency(true).locale("en")
.dateFormat(DATETIME_PATTERN)));
Assertions.assertTrue(callFailedRuntimeException.getMessage().contains(
"Installment level delinquency cannot be enabled for a loan if Delinquency bucket is not configured for loan product"));
});
}
@Test
public void testCalculateRepaymentScheduleWorksWithInstallmentLevelDelinquencySetting() {
runAt("31 May 2023", () -> {
// Create Client
Long clientId = clientHelper.createClient(ClientHelper.defaultClientCreationRequest()).getClientId();
// Create DelinquencyBuckets
Integer delinquencyBucketId = DelinquencyBucketsHelper.createDelinquencyBucket(requestSpec, responseSpec, List.of(//
Pair.of(1, 10), //
Pair.of(11, 30), //
Pair.of(31, 60), //
Pair.of(61, null)//
));
// Create Loan Product
PostLoanProductsRequest loanProductsRequest = create1InstallmentAmountInMultiplesOf4Period1MonthLongWithInterestAndAmortizationProduct(
InterestType.FLAT, AmortizationType.EQUAL_INSTALLMENTS);
// set installment level delinquency as false
loanProductsRequest.setEnableInstallmentLevelDelinquency(false);
loanProductsRequest.setDelinquencyBucketId(delinquencyBucketId.longValue());
PostLoanProductsResponse loanProductResponse = loanProductHelper.createLoanProduct(loanProductsRequest);
// run calculateLoanSchedule command works, while Applying for loan with installment level delinquency
PostLoansResponse loansResponse = loanTransactionHelper
.calculateRepaymentScheduleForApplyLoan(applyLoanRequest(clientId, loanProductResponse.getResourceId(),
"01 January 2023", 1250.0, 4, req -> req.setEnableInstallmentLevelDelinquency(true)), "calculateLoanSchedule");
assertThat(loansResponse).isNotNull();
assertNotNull(loansResponse.getPeriods());
assertThat(loansResponse.getPeriods().size()).isEqualTo(5);
});
}
@Test
public void tesInstallmentLevelSettingForLoanProductWithoutDelinquencyBucketValidation() {
@@ -2019,4 +2019,12 @@ public class LoanTransactionHelper extends IntegrationTest {
return ok(fineract().loans.stateTransitions(loanId, request, "undoapproval"));
}
public PutLoansLoanIdResponse modifyApplicationForLoan(final Long loanId, final String command, final PutLoansLoanIdRequest request) {
return ok(fineract().loans.modifyLoanApplication(loanId, request, command));
}
public PostLoansResponse calculateRepaymentScheduleForApplyLoan(PostLoansRequest request, String command) {
return ok(fineract().loans.calculateLoanScheduleOrSubmitLoanApplication(request, command));
}
}