Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FINERACT-1958 - reverse handling of disbursement with down payment #3442

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3044,7 +3044,12 @@ public Map<String, Object> undoDisbursal(final ScheduleGeneratorDTO scheduleGene
? Boolean.FALSE
: Boolean.TRUE;
this.loanRepaymentScheduleDetail.setPrincipal(this.approvedPrincipal);
if (this.loanProduct.isMultiDisburseLoan()) {
// Remove All the Disbursement Details If the Loan Product is disabled and exists one
if (this.loanProduct().isDisallowExpectedDisbursements() && !getDisbursementDetails().isEmpty()) {
for (LoanDisbursementDetails disbursementDetail : getAllDisbursementDetails()) {
disbursementDetail.reverse();
}
} else {
for (final LoanDisbursementDetails details : getDisbursementDetails()) {
details.updateActualDisbursementDate(null);
}
Expand Down Expand Up @@ -5535,6 +5540,16 @@ public ChangedTransactionDetail recalculateScheduleFromLastTransaction(final Sch

}

public ChangedTransactionDetail recalculateScheduleFromLastTransaction(final ScheduleGeneratorDTO generatorDTO) {
if (this.repaymentScheduleDetail().isInterestRecalculationEnabled()) {
regenerateRepaymentScheduleWithInterestRecalculation(generatorDTO);
} else {
regenerateRepaymentSchedule(generatorDTO);
}
return processTransactions();

}

public ChangedTransactionDetail handleRegenerateRepaymentScheduleWithInterestRecalculation(final ScheduleGeneratorDTO generatorDTO) {
regenerateRepaymentScheduleWithInterestRecalculation(generatorDTO);
return processTransactions();
Expand Down Expand Up @@ -6505,7 +6520,7 @@ public Map<String, Object> undoLastDisbursal(ScheduleGeneratorDTO scheduleGenera
}
}
reverseExistingTransactionsTillLastDisbursal(lastDisbursalTransaction);
loan.recalculateScheduleFromLastTransaction(scheduleGeneratorDTO, existingTransactionIds, existingReversedTransactionIds);
loan.recalculateScheduleFromLastTransaction(scheduleGeneratorDTO);
actualChanges.put("undolastdisbursal", "true");
actualChanges.put("disbursedAmount", this.getDisbursedAmount());
updateLoanSummaryDerivedFields();
Expand All @@ -6529,6 +6544,28 @@ public void reverseExistingTransactionsTillLastDisbursal(LoanTransaction lastDis
transaction.reverse();
}
}
if (isAutoRepaymentForDownPaymentEnabled()) {
// identify down-payment amount for the transaction
BigDecimal disbursedAmountPercentageForDownPayment = this.loanRepaymentScheduleDetail
.getDisbursedAmountPercentageForDownPayment();
Money downPaymentMoney = Money.of(getCurrency(),
MathUtil.percentageOf(lastDisbursalTransaction.getAmount(), disbursedAmountPercentageForDownPayment, 19));

// find the matching down-payment transaction based on date, amount and it also must have downpayment
// installment linked
Optional<LoanTransaction> downPaymentTransaction = this.loanTransactions.stream()
.filter(tr -> tr.getTransactionDate().equals(lastDisbursalTransaction.getTransactionDate())
&& hasAnInstallmentWithDownPayment(tr, downPaymentMoney.getAmount()))
.max(Comparator.comparing(LoanTransaction::getId));

// reverse the down-payment transaction
downPaymentTransaction.ifPresent(LoanTransaction::reverse);
}
}

private boolean hasAnInstallmentWithDownPayment(LoanTransaction tr, BigDecimal amount) {
return tr.getAmount().compareTo(amount) == 0 && tr.getLoanTransactionToRepaymentScheduleMappings().stream()
.anyMatch(mapping -> mapping.getLoanRepaymentScheduleInstallment().isDownPayment());
}

private void updateLoanToLastDisbursalState(LoanDisbursementDetails disbursementDetail) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -910,7 +910,7 @@ public Set<LoanTransactionToRepaymentScheduleMapping> getLoanTransactionToRepaym
}

public Boolean isAllowTypeTransactionAtTheTimeOfLastUndo() {
return isDisbursement() || isAccrual() || isRepaymentAtDisbursement();
return isDisbursement() || isAccrual() || isRepaymentAtDisbursement() || isRepayment();
}

public boolean isAccrualTransaction() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -869,16 +869,6 @@ public CommandProcessingResult undoLoanDisbursal(final Long loanId, final JsonCo
final Map<String, Object> accountingBridgeData = loan.deriveAccountingBridgeData(currency.getCode(), existingTransactionIds,
existingReversedTransactionIds, isAccountTransfer);
journalEntryWritePlatformService.createJournalEntriesForLoan(accountingBridgeData);

// Remove All the Disbursement Details If the Loan Product is disabled and exists one
if (loan.loanProduct().isDisallowExpectedDisbursements() && !loan.getDisbursementDetails().isEmpty()) {
List<LoanDisbursementDetails> reversedDisbursementDetails = new ArrayList<>();
for (LoanDisbursementDetails disbursementDetail : loan.getAllDisbursementDetails()) {
disbursementDetail.reverse();
reversedDisbursementDetails.add(disbursementDetail);
}
this.loanDisbursementDetailsRepository.saveAllAndFlush(reversedDisbursementDetails);
}
loanAccrualTransactionBusinessEventService.raiseBusinessEventForAccrualTransactions(loan, existingTransactionIds);
businessEventNotifierService.notifyPostBusinessEvent(new LoanUndoDisbursalBusinessEvent(loan));
}
Expand Down
Loading