adamsaghy commented on code in PR #4961:
URL: https://github.com/apache/fineract/pull/4961#discussion_r2315366820


##########
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;
+                        }
+                    }

Review Comment:
   Stream Api find first would be better here.



-- 
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]

Reply via email to