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


##########
fineract-provider/src/main/java/org/apache/fineract/portfolio/loanaccount/service/LoanDisbursementService.java:
##########
@@ -145,9 +152,45 @@ public Money adjustDisburseAmount(final Loan loan, 
@NonNull 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-based loans, find the matching tranche by 
amount first, then by order
+                    LoanDisbursementDetails selectedTranche = null;
+
+                    // First try to find a tranche that exactly matches the 
requested disbursement amount
+                    for (LoanDisbursementDetails disbursementDetails : 
details) {
+                        if (disbursementDetails.actualDisbursementDate() == 
null
+                                && 
disbursementDetails.principal().compareTo(principalDisbursed) == 0) {
+                            selectedTranche = disbursementDetails;
+                            break;
+                        }
+                    }
+
+                    // If no exact match found, take the first available 
tranche (next in line)
+                    if (selectedTranche == null) {
+                        for (LoanDisbursementDetails disbursementDetails : 
details) {
+                            if (disbursementDetails.actualDisbursementDate() 
== null) {
+                                selectedTranche = disbursementDetails;
+                                break;
+                            }
+                        }
+                    }
+
+                    if (selectedTranche != null) {
+                        // Update the selected tranche with the actual 
disbursement
+                        
selectedTranche.updateActualDisbursementDate(actualDisbursementDate);
+                        selectedTranche.updatePrincipal(principalDisbursed);
+                    }

Review Comment:
   I dont think it is matching with the idea to pick the next in line, not yet 
disbursed disbursement details and update with the "actual disbursement" 
details: date + amount. Why do we need to this find the disbursement details 
where the amount is matching logic?



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