FINERACT-2178: Ability to retrieve a loan's balance at a certain point in time in the past

This commit is contained in:
Arnold Galovics
2025-02-17 21:48:05 +01:00
committed by Arnold Gálovics
parent 986dceea1c
commit 0f1be3c913
29 changed files with 1523 additions and 8 deletions
@@ -67,6 +67,7 @@ import org.apache.fineract.client.models.GetLoansLoanIdStatus;
import org.apache.fineract.client.models.GetLoansLoanIdTransactions;
import org.apache.fineract.client.models.GetLoansLoanIdTransactionsTemplateResponse;
import org.apache.fineract.client.models.JournalEntryTransactionItem;
import org.apache.fineract.client.models.LoanPointInTimeData;
import org.apache.fineract.client.models.PaymentAllocationOrder;
import org.apache.fineract.client.models.PostChargesResponse;
import org.apache.fineract.client.models.PostLoanProductsRequest;
@@ -80,7 +81,9 @@ import org.apache.fineract.client.models.PostLoansRequest;
import org.apache.fineract.client.models.PostLoansResponse;
import org.apache.fineract.client.models.PutGlobalConfigurationsRequest;
import org.apache.fineract.client.models.PutLoansLoanIdResponse;
import org.apache.fineract.client.models.RetrieveLoansPointInTimeRequest;
import org.apache.fineract.client.util.CallFailedRuntimeException;
import org.apache.fineract.client.util.Calls;
import org.apache.fineract.infrastructure.configuration.api.GlobalConfigurationConstants;
import org.apache.fineract.integrationtests.client.IntegrationTest;
import org.apache.fineract.integrationtests.common.BatchHelper;
@@ -706,6 +709,26 @@ public abstract class BaseLoanIntegrationTest extends IntegrationTest {
loanTransactionHelper.undoLastDisbursalLoan(loanId, new PostLoansLoanIdRequest());
}
protected LoanPointInTimeData getPointInTimeData(Long loanId, String date) {
return Calls.ok(fineractClient().loansPointInTimeApi.retrieveLoanPointInTime(loanId, date, DATETIME_PATTERN, "en"));
}
protected List<LoanPointInTimeData> getPointInTimeData(List<Long> loanIds, String date) {
RetrieveLoansPointInTimeRequest request = new RetrieveLoansPointInTimeRequest().loanIds(loanIds).date(date)
.dateFormat(DATETIME_PATTERN).locale("en");
return Calls.ok(fineractClient().loansPointInTimeApi.retrieveLoansPointInTime(request));
}
protected void verifyOutstanding(LoanPointInTimeData loan, OutstandingAmounts outstanding) {
assertThat(BigDecimal.valueOf(outstanding.principalOutstanding))
.isEqualByComparingTo(loan.getPrincipal().getPrincipalOutstanding());
assertThat(BigDecimal.valueOf(outstanding.interestOutstanding)).isEqualByComparingTo(loan.getInterest().getInterestOutstanding());
assertThat(BigDecimal.valueOf(outstanding.feeOutstanding)).isEqualByComparingTo(loan.getFee().getFeeChargesOutstanding());
assertThat(BigDecimal.valueOf(outstanding.penaltyOutstanding))
.isEqualByComparingTo(loan.getPenalty().getPenaltyChargesOutstanding());
assertThat(BigDecimal.valueOf(outstanding.totalOutstanding)).isEqualByComparingTo(loan.getTotal().getTotalOutstanding());
}
// Note: this is buggy because if multiple journal entries are for the same account, amount and type, the
// verification will pass
// not all journal entries have been validated - since there might be duplicates
@@ -1158,8 +1181,8 @@ public abstract class BaseLoanIntegrationTest extends IntegrationTest {
loanBalance);
}
protected OutstandingAmounts outstanding(double principal, double fee, double penalty, double total) {
return new OutstandingAmounts(principal, fee, penalty, total);
protected OutstandingAmounts outstanding(double principal, double interestOutstanding, double fee, double penalty, double total) {
return new OutstandingAmounts(principal, interestOutstanding, fee, penalty, total);
}
protected BatchRequestBuilder batchRequest() {
@@ -1321,6 +1344,7 @@ public abstract class BaseLoanIntegrationTest extends IntegrationTest {
public static class OutstandingAmounts {
Double principalOutstanding;
Double interestOutstanding;
Double feeOutstanding;
Double penaltyOutstanding;
Double totalOutstanding;
@@ -597,7 +597,7 @@ public class LoanChargebackWithCreditAllocationsIntegrationTests extends BaseLoa
installment(312.0, 0, 0, 0, 0, true, "01 March 2023"), //
installment(312.0, 0, 0, 0, 0, true, "01 April 2023"), //
installment(314.0, 0, 0, 0, 0, true, "01 May 2023"), //
installment(100.0, 0, 0, 0, outstanding(50.0, 0d, 0d, 50.0), false, "02 May 2023") //
installment(100.0, 0, 0, 0, outstanding(50.0, 0.0, 0d, 0d, 50.0), false, "02 May 2023") //
);
});
}
@@ -659,7 +659,7 @@ public class LoanChargebackWithCreditAllocationsIntegrationTests extends BaseLoa
// DEFAULT payment allocation is ..., DUE_PENALTY, DUE_FEE, DUE_PRINCIPAL, ...
verifyRepaymentSchedule(loanId, //
installment(1250.0, null, "01 January 2023"), //
installment(342.0, 0, 100, 40, outstanding(30.0, 20.0, 0.0, 50.0), false, "01 February 2023"), //
installment(342.0, 0, 100, 40, outstanding(30.0, 0.0, 20.0, 0.0, 50.0), false, "01 February 2023"), //
installment(312.0, 0, 0, 0, 0.0, true, "01 March 2023"), //
installment(312.0, 0, 0, 0, 0.0, true, "01 April 2023"), //
installment(314.0, 0, 0, 0, 0.0, true, "01 May 2023") //
@@ -726,7 +726,7 @@ public class LoanChargebackWithCreditAllocationsIntegrationTests extends BaseLoa
// DEFAULT payment allocation is ..., DUE_PRINCIPAL, DUE_FEE, DUE_PENALTY ...
verifyRepaymentSchedule(loanId, //
installment(1250.0, null, "01 January 2023"), //
installment(342.0, 0, 100, 40, outstanding(0.0, 30.0, 20.0, 50.0), false, "01 February 2023"), //
installment(342.0, 0, 100, 40, outstanding(0.0, 0.0, 30.0, 20.0, 50.0), false, "01 February 2023"), //
installment(312.0, 0, 0, 0, 0.0, true, "01 March 2023"), //
installment(312.0, 0, 0, 0, 0.0, true, "01 April 2023"), //
installment(314.0, 0, 0, 0, 0.0, true, "01 May 2023") //
@@ -0,0 +1,659 @@
/**
* 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.integrationtests.loan.pointintime;
import static org.apache.fineract.integrationtests.BaseLoanIntegrationTest.TransactionProcessingStrategyCode.ADVANCED_PAYMENT_ALLOCATION_STRATEGY;
import java.math.BigDecimal;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
import org.apache.fineract.client.models.ChargeData;
import org.apache.fineract.client.models.LoanPointInTimeData;
import org.apache.fineract.client.models.PostLoanProductsRequest;
import org.apache.fineract.client.models.PostLoanProductsResponse;
import org.apache.fineract.client.models.PostLoansLoanIdResponse;
import org.apache.fineract.client.models.PostLoansRequest;
import org.apache.fineract.client.models.PostLoansRequestChargeData;
import org.apache.fineract.client.models.PostLoansResponse;
import org.apache.fineract.integrationtests.BaseLoanIntegrationTest;
import org.apache.fineract.integrationtests.common.ClientHelper;
import org.junit.jupiter.api.Test;
public class LoanPointInTimeTest extends BaseLoanIntegrationTest {
@Test
public void test_LoanPointInTimeDataWorks_ForPrincipalOutstandingCalculation() {
AtomicReference<Long> aLoanId = new AtomicReference<>();
runAt("01 January 2023", () -> {
// Create Client
Long clientId = clientHelper.createClient(ClientHelper.defaultClientCreationRequest()).getClientId();
int numberOfRepayments = 3;
int repaymentEvery = 1;
// Create charges
double charge1Amount = 1.0;
double charge2Amount = 1.5;
Long charge1Id = createDisbursementPercentageCharge(charge1Amount);
Long charge2Id = createDisbursementPercentageCharge(charge2Amount);
// Create Loan Product
PostLoanProductsRequest product = createOnePeriod30DaysLongNoInterestPeriodicAccrualProduct() //
.numberOfRepayments(numberOfRepayments) //
.repaymentEvery(repaymentEvery) //
.installmentAmountInMultiplesOf(null) //
.repaymentFrequencyType(RepaymentFrequencyType.MONTHS.longValue()) //
.interestType(InterestType.DECLINING_BALANCE)//
.interestCalculationPeriodType(InterestCalculationPeriodType.DAILY)//
.interestRecalculationCompoundingMethod(InterestRecalculationCompoundingMethod.NONE)//
.rescheduleStrategyMethod(RescheduleStrategyMethod.ADJUST_LAST_UNPAID_PERIOD)//
.isInterestRecalculationEnabled(true)//
.recalculationRestFrequencyInterval(1)//
.recalculationRestFrequencyType(RecalculationRestFrequencyType.DAILY)//
.rescheduleStrategyMethod(RescheduleStrategyMethod.REDUCE_EMI_AMOUNT)//
.allowPartialPeriodInterestCalcualtion(false)//
.disallowExpectedDisbursements(false)//
.allowApprovedDisbursedAmountsOverApplied(false)//
.overAppliedNumber(null)//
.overAppliedCalculationType(null)//
.multiDisburseLoan(null)//
.charges(List.of(new ChargeData().id(charge1Id), new ChargeData().id(charge2Id)));//
PostLoanProductsResponse loanProductResponse = loanProductHelper.createLoanProduct(product);
Long loanProductId = loanProductResponse.getResourceId();
// Apply and Approve Loan
double amount = 5000.0;
PostLoansRequest applicationRequest = applyLoanRequest(clientId, loanProductId, "01 January 2023", amount, numberOfRepayments)//
.repaymentEvery(repaymentEvery)//
.loanTermFrequency(numberOfRepayments)//
.repaymentFrequencyType(RepaymentFrequencyType.MONTHS)//
.loanTermFrequencyType(RepaymentFrequencyType.MONTHS)//
.interestType(InterestType.DECLINING_BALANCE)//
.interestCalculationPeriodType(InterestCalculationPeriodType.DAILY)//
.charges(List.of(//
new PostLoansRequestChargeData().chargeId(charge1Id).amount(BigDecimal.valueOf(charge1Amount)), //
new PostLoansRequestChargeData().chargeId(charge2Id).amount(BigDecimal.valueOf(charge2Amount))//
));//
PostLoansResponse postLoansResponse = loanTransactionHelper.applyLoan(applicationRequest);
PostLoansLoanIdResponse approvedLoanResult = loanTransactionHelper.approveLoan(postLoansResponse.getResourceId(),
approveLoanRequest(amount, "01 January 2023"));
aLoanId.getAndSet(approvedLoanResult.getLoanId());
Long loanId = aLoanId.get();
// disburse Loan
disburseLoan(loanId, BigDecimal.valueOf(5000.0), "01 January 2023");
// verify transactions
verifyTransactions(loanId, //
transaction(5000.0, "Disbursement", "01 January 2023"), //
transaction(125.0, "Repayment (at time of disbursement)", "01 January 2023") //
);
});
runAt("01 February 2023", () -> {
Long loanId = aLoanId.get();
// repay 500
addRepaymentForLoan(loanId, 500.0, "01 February 2023");
// check point in time data
LoanPointInTimeData pointInTimeData = getPointInTimeData(loanId, "01 February 2023");
verifyOutstanding(pointInTimeData, outstanding(4500.0, 0.0, 0.0, 0.0, 4500.0));
// verify transactions
verifyTransactions(loanId, //
transaction(5000.0, "Disbursement", "01 January 2023"), //
transaction(125.0, "Repayment (at time of disbursement)", "01 January 2023"), //
transaction(500.0, "Repayment", "01 February 2023") //
);
});
runAt("09 February 2023", () -> {
Long loanId = aLoanId.get();
// repay 500
addRepaymentForLoan(loanId, 500.0, "09 February 2023");
// check point in time data
LoanPointInTimeData pointInTimeData = getPointInTimeData(loanId, "01 February 2023");
verifyOutstanding(pointInTimeData, outstanding(4500.0, 0.0, 0.0, 0.0, 4500.0));
pointInTimeData = getPointInTimeData(loanId, "07 February 2023");
verifyOutstanding(pointInTimeData, outstanding(4500.0, 0.0, 0.0, 0.0, 4500.0));
pointInTimeData = getPointInTimeData(loanId, "09 February 2023");
verifyOutstanding(pointInTimeData, outstanding(4000.0, 0.0, 0.0, 0.0, 4000.0));
// verify transactions
verifyTransactions(loanId, //
transaction(5000.0, "Disbursement", "01 January 2023"), //
transaction(125.0, "Repayment (at time of disbursement)", "01 January 2023"), //
transaction(500.0, "Repayment", "01 February 2023"), //
transaction(500.0, "Repayment", "09 February 2023") //
);
});
runAt("01 March 2023", () -> {
Long loanId = aLoanId.get();
// repay 500
addRepaymentForLoan(loanId, 500.0, "01 March 2023");
// check point in time data
LoanPointInTimeData pointInTimeData = getPointInTimeData(loanId, "01 February 2023");
verifyOutstanding(pointInTimeData, outstanding(4500.0, 0.0, 0.0, 0.0, 4500.0));
pointInTimeData = getPointInTimeData(loanId, "10 February 2023");
verifyOutstanding(pointInTimeData, outstanding(4000.0, 0.0, 0.0, 0.0, 4000.0));
pointInTimeData = getPointInTimeData(loanId, "01 March 2023");
verifyOutstanding(pointInTimeData, outstanding(3500.0, 0.0, 0.0, 0.0, 3500.0));
// verify transactions
verifyTransactions(loanId, //
transaction(5000.0, "Disbursement", "01 January 2023"), //
transaction(125.0, "Repayment (at time of disbursement)", "01 January 2023"), //
transaction(500.0, "Repayment", "01 February 2023"), //
transaction(500.0, "Repayment", "09 February 2023"), //
transaction(500.0, "Repayment", "01 March 2023") //
);
});
}
@Test
public void test_LoanPointInTimeDataWorks_ForAllOutstandingCalculation_WhenLoanIsCumulative_AndInterestIsEnabled() {
AtomicReference<Long> aLoanId = new AtomicReference<>();
runAt("01 January 2023", () -> {
// Create Client
Long clientId = clientHelper.createClient(ClientHelper.defaultClientCreationRequest()).getClientId();
int numberOfRepayments = 3;
int repaymentEvery = 1;
// Create Loan Product
PostLoanProductsRequest product = createOnePeriod30DaysLongNoInterestPeriodicAccrualProduct() //
.numberOfRepayments(numberOfRepayments) //
.repaymentEvery(repaymentEvery) //
.installmentAmountInMultiplesOf(null) //
.repaymentFrequencyType(RepaymentFrequencyType.MONTHS.longValue()) //
.interestType(InterestType.DECLINING_BALANCE)//
.interestRatePerPeriod(10.0)//
.interestCalculationPeriodType(InterestCalculationPeriodType.DAILY)//
.interestRecalculationCompoundingMethod(InterestRecalculationCompoundingMethod.NONE)//
.rescheduleStrategyMethod(RescheduleStrategyMethod.ADJUST_LAST_UNPAID_PERIOD)//
.isInterestRecalculationEnabled(true)//
.recalculationRestFrequencyInterval(1)//
.recalculationRestFrequencyType(RecalculationRestFrequencyType.DAILY)//
.rescheduleStrategyMethod(RescheduleStrategyMethod.REDUCE_EMI_AMOUNT)//
.allowPartialPeriodInterestCalcualtion(false)//
.disallowExpectedDisbursements(false)//
.allowApprovedDisbursedAmountsOverApplied(false)//
.overAppliedNumber(null)//
.overAppliedCalculationType(null)//
.multiDisburseLoan(null);//
PostLoanProductsResponse loanProductResponse = loanProductHelper.createLoanProduct(product);
Long loanProductId = loanProductResponse.getResourceId();
// Apply and Approve Loan
double amount = 5000.0;
PostLoansRequest applicationRequest = applyLoanRequest(clientId, loanProductId, "01 January 2023", amount, numberOfRepayments)//
.repaymentEvery(repaymentEvery)//
.interestRatePerPeriod(BigDecimal.valueOf(10.0))//
.loanTermFrequency(numberOfRepayments)//
.repaymentFrequencyType(RepaymentFrequencyType.MONTHS)//
.loanTermFrequencyType(RepaymentFrequencyType.MONTHS)//
.interestType(InterestType.DECLINING_BALANCE)//
.interestCalculationPeriodType(InterestCalculationPeriodType.DAILY);//
PostLoansResponse postLoansResponse = loanTransactionHelper.applyLoan(applicationRequest);
PostLoansLoanIdResponse approvedLoanResult = loanTransactionHelper.approveLoan(postLoansResponse.getResourceId(),
approveLoanRequest(amount, "01 January 2023"));
aLoanId.getAndSet(approvedLoanResult.getLoanId());
Long loanId = aLoanId.get();
Long chargeId = createCharge(100.0).getResourceId();
addLoanCharge(loanId, chargeId, "02 February 2023", 100.0);
// disburse Loan
disburseLoan(loanId, BigDecimal.valueOf(5000.0), "01 January 2023");
// verify transactions
verifyTransactions(loanId, //
transaction(5000.0, "Disbursement", "01 January 2023") //
);
});
runAt("01 February 2023", () -> {
Long loanId = aLoanId.get();
// repay 500
addRepaymentForLoan(loanId, 250.0, "01 February 2023");
// check point in time data
LoanPointInTimeData pointInTimeData = getPointInTimeData(loanId, "01 February 2023");
verifyOutstanding(pointInTimeData, outstanding(5000.0, 767.70, 0.0, 0.0, 5767.70));
// verify transactions
verifyTransactions(loanId, //
transaction(5000.0, "Disbursement", "01 January 2023"), //
transaction(250.0, "Repayment", "01 February 2023") //
);
});
runAt("09 February 2023", () -> {
Long loanId = aLoanId.get();
// repay 500
addRepaymentForLoan(loanId, 500.0, "09 February 2023");
// check point in time data
LoanPointInTimeData pointInTimeData = getPointInTimeData(loanId, "01 February 2023");
verifyOutstanding(pointInTimeData, outstanding(5000.0, 767.70, 0.0, 0.0, 5767.70));
pointInTimeData = getPointInTimeData(loanId, "07 February 2023");
verifyOutstanding(pointInTimeData, outstanding(5000.0, 800.22, 100.0, 0.0, 5900.22));
pointInTimeData = getPointInTimeData(loanId, "09 February 2023");
verifyOutstanding(pointInTimeData, outstanding(4759.59, 551.47, 100.0, 0.0, 5411.06));
// verify transactions
verifyTransactions(loanId, //
transaction(5000.0, "Disbursement", "01 January 2023"), //
transaction(250.0, "Repayment", "01 February 2023"), //
transaction(500.0, "Repayment", "09 February 2023") //
);
});
runAt("01 March 2023", () -> {
Long loanId = aLoanId.get();
// repay 500
addRepaymentForLoan(loanId, 500.0, "01 March 2023");
// check point in time data
LoanPointInTimeData pointInTimeData = getPointInTimeData(loanId, "01 February 2023");
verifyOutstanding(pointInTimeData, outstanding(5000.0, 767.70, 0.0, 0.0, 5767.70));
pointInTimeData = getPointInTimeData(loanId, "10 February 2023");
verifyOutstanding(pointInTimeData, outstanding(4759.59, 556.02, 100.0, 0.0, 5415.61));
pointInTimeData = getPointInTimeData(loanId, "01 March 2023");
verifyOutstanding(pointInTimeData, outstanding(4259.59, 642.46, 100.0, 0.0, 5002.05));
// verify transactions
verifyTransactions(loanId, //
transaction(5000.0, "Disbursement", "01 January 2023"), //
transaction(250.0, "Repayment", "01 February 2023"), //
transaction(500.0, "Repayment", "09 February 2023"), //
transaction(500.0, "Repayment", "01 March 2023") //
);
});
runAt("05 March 2023", () -> {
Long loanId = aLoanId.get();
// repay full loan
addRepaymentForLoan(loanId, 5032.52, "05 March 2023");
// check point in time data
LoanPointInTimeData pointInTimeData = getPointInTimeData(loanId, "01 February 2023");
verifyOutstanding(pointInTimeData, outstanding(5000.0, 767.70, 0.0, 0.0, 5767.70));
pointInTimeData = getPointInTimeData(loanId, "10 February 2023");
verifyOutstanding(pointInTimeData, outstanding(4759.59, 556.02, 100.0, 0.0, 5415.61));
pointInTimeData = getPointInTimeData(loanId, "01 March 2023");
verifyOutstanding(pointInTimeData, outstanding(4259.59, 642.46, 100.0, 0.0, 5002.05));
pointInTimeData = getPointInTimeData(loanId, "05 March 2023");
verifyOutstanding(pointInTimeData, outstanding(0.0, 0.0, 0.0, 0.0, 0.0));
// verify transactions
verifyTransactions(loanId, //
transaction(5000.0, "Disbursement", "01 January 2023"), //
transaction(250.0, "Repayment", "01 February 2023"), //
transaction(500.0, "Repayment", "09 February 2023"), //
transaction(500.0, "Repayment", "01 March 2023"), //
transaction(5032.52, "Repayment", "05 March 2023"), //
transaction(1282.52, "Accrual", "05 March 2023") //
);
});
}
@Test
public void test_LoanPointInTimeDataWorks_ForAllOutstandingCalculation_WhenLoanIsProgressive_AndInterestIsEnabled() {
AtomicReference<Long> aLoanId = new AtomicReference<>();
runAt("01 January 2023", () -> {
// Create Client
Long clientId = clientHelper.createClient(ClientHelper.defaultClientCreationRequest()).getClientId();
int numberOfRepayments = 3;
int repaymentEvery = 1;
// Create Loan Product
PostLoanProductsRequest product = create4IProgressive() //
.numberOfRepayments(numberOfRepayments) //
.repaymentEvery(repaymentEvery) //
.installmentAmountInMultiplesOf(null) //
.repaymentFrequencyType(RepaymentFrequencyType.MONTHS.longValue()) //
.interestType(InterestType.DECLINING_BALANCE)//
.interestRatePerPeriod(10.0)//
.interestCalculationPeriodType(InterestCalculationPeriodType.DAILY)//
.interestRecalculationCompoundingMethod(InterestRecalculationCompoundingMethod.NONE)//
.isInterestRecalculationEnabled(true)//
.recalculationRestFrequencyInterval(1)//
.recalculationRestFrequencyType(RecalculationRestFrequencyType.DAILY)//
.rescheduleStrategyMethod(RescheduleStrategyMethod.ADJUST_LAST_UNPAID_PERIOD)//
.allowPartialPeriodInterestCalcualtion(false)//
.disallowExpectedDisbursements(false)//
.allowApprovedDisbursedAmountsOverApplied(false)//
.overAppliedNumber(null)//
.overAppliedCalculationType(null)//
.currencyCode("USD").multiDisburseLoan(null);//
PostLoanProductsResponse loanProductResponse = loanProductHelper.createLoanProduct(product);
Long loanProductId = loanProductResponse.getResourceId();
// Apply and Approve Loan
double amount = 5000.0;
PostLoansRequest applicationRequest = applyLoanRequest(clientId, loanProductId, "01 January 2023", amount, numberOfRepayments)//
.repaymentEvery(repaymentEvery)//
.transactionProcessingStrategyCode(ADVANCED_PAYMENT_ALLOCATION_STRATEGY).interestRatePerPeriod(BigDecimal.valueOf(10.0))//
.loanTermFrequency(numberOfRepayments)//
.repaymentFrequencyType(RepaymentFrequencyType.MONTHS)//
.loanTermFrequencyType(RepaymentFrequencyType.MONTHS)//
.interestType(InterestType.DECLINING_BALANCE)//
.interestCalculationPeriodType(InterestCalculationPeriodType.DAILY);//
PostLoansResponse postLoansResponse = loanTransactionHelper.applyLoan(applicationRequest);
PostLoansLoanIdResponse approvedLoanResult = loanTransactionHelper.approveLoan(postLoansResponse.getResourceId(),
approveLoanRequest(amount, "01 January 2023"));
aLoanId.getAndSet(approvedLoanResult.getLoanId());
Long loanId = aLoanId.get();
Long chargeId = createCharge(100.0).getResourceId();
addLoanCharge(loanId, chargeId, "02 February 2023", 100.0);
// disburse Loan
disburseLoan(loanId, BigDecimal.valueOf(5000.0), "01 January 2023");
// verify transactions
verifyTransactions(loanId, //
transaction(5000.0, "Disbursement", "01 January 2023") //
);
});
runAt("01 February 2023", () -> {
Long loanId = aLoanId.get();
// repay 500
addRepaymentForLoan(loanId, 250.0, "01 February 2023");
// check point in time data
LoanPointInTimeData pointInTimeData = getPointInTimeData(loanId, "01 February 2023");
verifyOutstanding(pointInTimeData, outstanding(4750.0, 83.56, 0.0, 0.0, 4833.56));
// verify transactions
verifyTransactions(loanId, //
transaction(5000.0, "Disbursement", "01 January 2023"), //
transaction(250.0, "Repayment", "01 February 2023") //
);
});
runAt("09 February 2023", () -> {
Long loanId = aLoanId.get();
// repay 500
addRepaymentForLoan(loanId, 500.0, "09 February 2023");
// check point in time data
LoanPointInTimeData pointInTimeData = getPointInTimeData(loanId, "01 February 2023");
verifyOutstanding(pointInTimeData, outstanding(4750.0, 83.56, 0.0, 0.0, 4833.56));
pointInTimeData = getPointInTimeData(loanId, "07 February 2023");
verifyOutstanding(pointInTimeData, outstanding(4750.0, 86.10, 100.0, 0.0, 4936.10));
pointInTimeData = getPointInTimeData(loanId, "09 February 2023");
verifyOutstanding(pointInTimeData, outstanding(4250.0, 86.93, 100.0, 0.0, 4436.93));
// verify transactions
verifyTransactions(loanId, //
transaction(5000.0, "Disbursement", "01 January 2023"), //
transaction(250.0, "Repayment", "01 February 2023"), //
transaction(500.0, "Repayment", "09 February 2023") //
);
});
runAt("01 March 2023", () -> {
Long loanId = aLoanId.get();
// repay 500
addRepaymentForLoan(loanId, 500.0, "01 March 2023");
// check point in time data
LoanPointInTimeData pointInTimeData = getPointInTimeData(loanId, "01 February 2023");
verifyOutstanding(pointInTimeData, outstanding(4750.0, 83.56, 0.0, 0.0, 4833.56));
pointInTimeData = getPointInTimeData(loanId, "10 February 2023");
verifyOutstanding(pointInTimeData, outstanding(4250.0, 87.20, 100.0, 0.0, 4437.20));
pointInTimeData = getPointInTimeData(loanId, "01 March 2023");
verifyOutstanding(pointInTimeData, outstanding(3750.0, 92.36, 100.0, 0.0, 3942.36));
// verify transactions
verifyTransactions(loanId, //
transaction(5000.0, "Disbursement", "01 January 2023"), //
transaction(250.0, "Repayment", "01 February 2023"), //
transaction(500.0, "Repayment", "09 February 2023"), //
transaction(500.0, "Repayment", "01 March 2023") //
);
});
runAt("05 March 2023", () -> {
Long loanId = aLoanId.get();
// repay full loan
addRepaymentForLoan(loanId, 3942.36, "05 March 2023");
// check point in time data
LoanPointInTimeData pointInTimeData = getPointInTimeData(loanId, "01 February 2023");
verifyOutstanding(pointInTimeData, outstanding(4750.0, 83.56, 0.0, 0.0, 4833.56));
pointInTimeData = getPointInTimeData(loanId, "10 February 2023");
verifyOutstanding(pointInTimeData, outstanding(4250.0, 87.20, 100.0, 0.0, 4437.20));
pointInTimeData = getPointInTimeData(loanId, "01 March 2023");
verifyOutstanding(pointInTimeData, outstanding(3750.0, 92.36, 100.0, 0.0, 3942.36));
pointInTimeData = getPointInTimeData(loanId, "05 March 2023");
verifyOutstanding(pointInTimeData, outstanding(0.0, 0.0, 0.0, 0.0, 0.0));
// verify transactions
verifyTransactions(loanId, //
transaction(5000.0, "Disbursement", "01 January 2023"), //
transaction(250.0, "Repayment", "01 February 2023"), //
transaction(500.0, "Repayment", "09 February 2023"), //
transaction(500.0, "Repayment", "01 March 2023"), //
transaction(3942.36, "Repayment", "05 March 2023"), //
transaction(182.31, "Accrual", "05 March 2023") //
);
});
}
@Test
public void test_LoansPointInTimeDataWorks_ForPrincipalOutstandingCalculation() {
AtomicReference<Long> aLoanId = new AtomicReference<>();
AtomicReference<Long> aLoanId2 = new AtomicReference<>();
runAt("01 January 2023", () -> {
// Create Client
Long clientId = clientHelper.createClient(ClientHelper.defaultClientCreationRequest()).getClientId();
int numberOfRepayments = 3;
int repaymentEvery = 1;
// Create charges
double charge1Amount = 1.0;
double charge2Amount = 1.5;
Long charge1Id = createDisbursementPercentageCharge(charge1Amount);
Long charge2Id = createDisbursementPercentageCharge(charge2Amount);
// Create Loan Product
PostLoanProductsRequest product = createOnePeriod30DaysLongNoInterestPeriodicAccrualProduct() //
.numberOfRepayments(numberOfRepayments) //
.repaymentEvery(repaymentEvery) //
.installmentAmountInMultiplesOf(null) //
.repaymentFrequencyType(RepaymentFrequencyType.MONTHS.longValue()) //
.interestType(InterestType.DECLINING_BALANCE)//
.interestCalculationPeriodType(InterestCalculationPeriodType.DAILY)//
.interestRecalculationCompoundingMethod(InterestRecalculationCompoundingMethod.NONE)//
.rescheduleStrategyMethod(RescheduleStrategyMethod.ADJUST_LAST_UNPAID_PERIOD)//
.isInterestRecalculationEnabled(true)//
.recalculationRestFrequencyInterval(1)//
.recalculationRestFrequencyType(RecalculationRestFrequencyType.DAILY)//
.rescheduleStrategyMethod(RescheduleStrategyMethod.REDUCE_EMI_AMOUNT)//
.allowPartialPeriodInterestCalcualtion(false)//
.disallowExpectedDisbursements(false)//
.allowApprovedDisbursedAmountsOverApplied(false)//
.overAppliedNumber(null)//
.overAppliedCalculationType(null)//
.multiDisburseLoan(null)//
.charges(List.of(new ChargeData().id(charge1Id), new ChargeData().id(charge2Id)));//
PostLoanProductsResponse loanProductResponse = loanProductHelper.createLoanProduct(product);
Long loanProductId = loanProductResponse.getResourceId();
// Apply and Approve Loan
double amount = 5000.0;
PostLoansRequest applicationRequest = applyLoanRequest(clientId, loanProductId, "01 January 2023", amount, numberOfRepayments)//
.repaymentEvery(repaymentEvery)//
.loanTermFrequency(numberOfRepayments)//
.repaymentFrequencyType(RepaymentFrequencyType.MONTHS)//
.loanTermFrequencyType(RepaymentFrequencyType.MONTHS)//
.interestType(InterestType.DECLINING_BALANCE)//
.interestCalculationPeriodType(InterestCalculationPeriodType.DAILY)//
.charges(List.of(//
new PostLoansRequestChargeData().chargeId(charge1Id).amount(BigDecimal.valueOf(charge1Amount)), //
new PostLoansRequestChargeData().chargeId(charge2Id).amount(BigDecimal.valueOf(charge2Amount))//
));//
PostLoansResponse postLoansResponse = loanTransactionHelper.applyLoan(applicationRequest);
PostLoansResponse postLoansResponse2 = loanTransactionHelper.applyLoan(applicationRequest);
PostLoansLoanIdResponse approvedLoanResult = loanTransactionHelper.approveLoan(postLoansResponse.getResourceId(),
approveLoanRequest(amount, "01 January 2023"));
PostLoansLoanIdResponse approvedLoanResult2 = loanTransactionHelper.approveLoan(postLoansResponse2.getResourceId(),
approveLoanRequest(amount, "01 January 2023"));
aLoanId.getAndSet(approvedLoanResult.getLoanId());
aLoanId2.getAndSet(approvedLoanResult2.getLoanId());
Long loanId = aLoanId.get();
Long loanId2 = aLoanId2.get();
// disburse Loan
disburseLoan(loanId, BigDecimal.valueOf(5000.0), "01 January 2023");
disburseLoan(loanId2, BigDecimal.valueOf(5000.0), "01 January 2023");
// verify transactions
verifyTransactions(loanId, //
transaction(5000.0, "Disbursement", "01 January 2023"), //
transaction(125.0, "Repayment (at time of disbursement)", "01 January 2023") //
);
verifyTransactions(loanId2, //
transaction(5000.0, "Disbursement", "01 January 2023"), //
transaction(125.0, "Repayment (at time of disbursement)", "01 January 2023") //
);
});
runAt("01 February 2023", () -> {
Long loanId = aLoanId.get();
Long loanId2 = aLoanId2.get();
// repay 500
addRepaymentForLoan(loanId, 500.0, "01 February 2023");
addRepaymentForLoan(loanId2, 500.0, "01 February 2023");
// check point in time data
List<LoanPointInTimeData> pointInTimeData = getPointInTimeData(List.of(loanId, loanId2), "01 February 2023");
verifyOutstanding(pointInTimeData.get(0), outstanding(4500.0, 0.0, 0.0, 0.0, 4500.0));
verifyOutstanding(pointInTimeData.get(1), outstanding(4500.0, 0.0, 0.0, 0.0, 4500.0));
// verify transactions
verifyTransactions(loanId, //
transaction(5000.0, "Disbursement", "01 January 2023"), //
transaction(125.0, "Repayment (at time of disbursement)", "01 January 2023"), //
transaction(500.0, "Repayment", "01 February 2023") //
);
verifyTransactions(loanId2, //
transaction(5000.0, "Disbursement", "01 January 2023"), //
transaction(125.0, "Repayment (at time of disbursement)", "01 January 2023"), //
transaction(500.0, "Repayment", "01 February 2023") //
);
});
runAt("01 March 2023", () -> {
Long loanId = aLoanId.get();
Long loanId2 = aLoanId2.get();
// repay 500
addRepaymentForLoan(loanId, 500.0, "01 March 2023");
addRepaymentForLoan(loanId2, 500.0, "01 March 2023");
// check point in time data
List<LoanPointInTimeData> pointInTimeData = getPointInTimeData(List.of(loanId, loanId2), "01 February 2023");
verifyOutstanding(pointInTimeData.get(0), outstanding(4500.0, 0.0, 0.0, 0.0, 4500.0));
verifyOutstanding(pointInTimeData.get(1), outstanding(4500.0, 0.0, 0.0, 0.0, 4500.0));
pointInTimeData = getPointInTimeData(List.of(loanId, loanId2), "01 March 2023");
verifyOutstanding(pointInTimeData.get(0), outstanding(4000.0, 0.0, 0.0, 0.0, 4000.0));
verifyOutstanding(pointInTimeData.get(1), outstanding(4000.0, 0.0, 0.0, 0.0, 4000.0));
// verify transactions
verifyTransactions(loanId, //
transaction(5000.0, "Disbursement", "01 January 2023"), //
transaction(125.0, "Repayment (at time of disbursement)", "01 January 2023"), //
transaction(500.0, "Repayment", "01 February 2023"), //
transaction(500.0, "Repayment", "01 March 2023") //
);
verifyTransactions(loanId2, //
transaction(5000.0, "Disbursement", "01 January 2023"), //
transaction(125.0, "Repayment (at time of disbursement)", "01 January 2023"), //
transaction(500.0, "Repayment", "01 February 2023"), //
transaction(500.0, "Repayment", "01 March 2023") //
);
});
}
}