Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
 - reverse handling of disbursement with down payment
  • Loading branch information
reluxa authored and adamsaghy committed Sep 11, 2023
1 parent 3a09259 commit 6d3357e
Show file tree
Hide file tree
Showing 7 changed files with 2,092 additions and 13 deletions.
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

0 comments on commit 6d3357e

Please sign in to comment.