adamsaghy commented on code in PR #4961:
URL: https://github.com/apache/fineract/pull/4961#discussion_r2315411575
##########
fineract-provider/src/main/java/org/apache/fineract/portfolio/loanaccount/service/LoanDisbursementService.java:
##########
@@ -145,9 +148,67 @@ public Money adjustDisburseAmount(final Loan loan,
@NotNull final JsonCommand co
if (details.isEmpty()) {
diff =
loan.getLoanRepaymentScheduleDetail().getPrincipal().minus(principalDisbursed).getAmount();
} else {
- for (LoanDisbursementDetails disbursementDetails : details) {
-
disbursementDetails.updateActualDisbursementDate(actualDisbursementDate);
- disbursementDetails.updatePrincipal(principalDisbursed);
+ // Check if this is a tranche-based loan (has multiple
predefined disbursement details)
+ // versus a non-tranche multi-disbursal loan (creates
disbursement details on-the-fly)
+ boolean isTrancheBasedLoan =
hasMultipleOrPreDefinedDisbursementDetails(loan, details);
+
+ if (isTrancheBasedLoan && details.size() > 1) {
+ // For tranche loans with multiple tranches on the same
date:
+ // Apply smart selection to prevent consuming all tranches
+ // First, try to find an exact match for the disbursement
amount
+ LoanDisbursementDetails matchingTranche = null;
+ for (LoanDisbursementDetails disbursementDetails :
details) {
+ if
(disbursementDetails.principal().compareTo(principalDisbursed) == 0) {
+ matchingTranche = disbursementDetails;
+ break;
+ }
+ }
+
+ if (matchingTranche != null) {
+ // Found exact match - disburse only this tranche
+
matchingTranche.updateActualDisbursementDate(actualDisbursementDate);
+ matchingTranche.updatePrincipal(principalDisbursed);
+ } else {
+ LoanDisbursementDetails bestTranche = null;
+ BigDecimal bestTrancheSize = null;
+ LoanDisbursementDetails largestTranche = null;
+ BigDecimal largestTrancheSize = BigDecimal.ZERO;
+
+ // Find the best fitting tranche
+ for (LoanDisbursementDetails disbursementDetails :
details) {
+ BigDecimal trancheAmount =
disbursementDetails.principal();
+
+ // Track the largest tranche as fallback
+ if (trancheAmount.compareTo(largestTrancheSize) >
0) {
+ largestTranche = disbursementDetails;
+ largestTrancheSize = trancheAmount;
+ }
+
+ // Find smallest tranche that can accommodate the
full amount
+ if (trancheAmount.compareTo(principalDisbursed) >=
0) {
+ if (bestTranche == null ||
trancheAmount.compareTo(bestTrancheSize) < 0) {
+ bestTranche = disbursementDetails;
+ bestTrancheSize = trancheAmount;
+ }
+ }
+ }
+
+ // Use best fitting tranche, or largest if none fits
fully
+ LoanDisbursementDetails selectedTranche = (bestTranche
!= null) ? bestTranche : largestTranche;
+
+ if (selectedTranche != null) {
+ // Update the selected tranche with the
disbursement
+
selectedTranche.updateActualDisbursementDate(actualDisbursementDate);
+
selectedTranche.updatePrincipal(principalDisbursed);
+ }
Review Comment:
I dont really understand this logic. Based on the expected disbursement
details there are entries that were already disbursed (actual disbursement date
is set) and those that are planned to be (actual disbursement date is `null`),
when a new disbursement occurs, we should update the next in line (based on
expected disbursement date) and update with the disbursement date and amount.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]