mariiaKraievska commented on code in PR #4446:
URL: https://github.com/apache/fineract/pull/4446#discussion_r1991187835


##########
fineract-loan/src/main/java/org/apache/fineract/portfolio/loanaccount/service/ReprocessLoanTransactionsServiceImpl.java:
##########
@@ -63,12 +67,49 @@ public void removeLoanCharge(final Loan loan, final 
LoanCharge loanCharge) {
         loan.removeLoanCharge(loanCharge).ifPresent(this::handleChangedDetail);
     }
 
+    @Override
+    public void processLatestTransaction(final LoanTransaction 
loanTransaction, final Loan loan) {
+        final ChangedTransactionDetail changedTransactionDetail = 
loan.getTransactionProcessor().processLatestTransaction(loanTransaction,
+                new TransactionCtx(loan.getCurrency(), 
loan.getRepaymentScheduleInstallments(), loan.getActiveCharges(),
+                        new MoneyHolder(loan.getTotalOverpaidAsMoney()), new 
ChangedTransactionDetail()));
+        if (!loan.isInterestRecalculationEnabled()) {
+            final List<LoanTransaction> newTransactions = 
changedTransactionDetail.getTransactionChanges().stream()
+                    
.map(TransactionChangeData::getNewTransaction).peek(transaction -> 
transaction.updateLoan(loan)).toList();
+
+            loan.getLoanTransactions().addAll(newTransactions);
+        }
+
+        loan.updateLoanSummaryDerivedFields();
+        handleChangedDetail(changedTransactionDetail);
+    }
+
     private void handleChangedDetail(final ChangedTransactionDetail 
changedTransactionDetail) {
-        for (final Map.Entry<Long, LoanTransaction> mapEntry : 
changedTransactionDetail.getNewTransactionMappings().entrySet()) {
-            
loanAccountService.saveLoanTransactionWithDataIntegrityViolationChecks(mapEntry.getValue());
-            
loanAccountTransfersService.updateLoanTransaction(mapEntry.getKey(), 
mapEntry.getValue());
+        final List<TransactionChangeData> sortedChanges = 
changedTransactionDetail.getTransactionChanges().stream()

Review Comment:
   I added this sorting because there are scenarios when the accrual 
transaction is created and added to the changedTransactionDetail list later 
than the charge-off transaction, even though logically it should be saved first.
   
   Specifically, during reprocessing of a charge-off transaction that must be 
reverse-replayed afterward, a new accrual transaction might be created during 
the execution of the processLatestTransaction method. At this point, the 
accrual transaction gets appended to the list after the charge-off transaction, 
which was added earlier in the processSingleTransaction method here:
   
   ```
   if (!isNew) {
               // For existing transactions, check if the re-payment breakup 
(principal, interest, fees, penalties) has
               // changed.
               processTransaction = 
LoanTransaction.copyTransactionProperties(loanTransaction);
               ctx.getChangedTransactionDetail().addTransactionChange(new 
TransactionChangeData(loanTransaction, processTransaction));
           }
           // Reset derived component of new loan transaction and re-process 
transaction
           processLatestTransaction(processTransaction, ctx);
   
   ```
   Meanwhile, the accrual transaction is only added later during the execution 
of processLatestTransaction(processTransaction, ctx).
   Therefore, explicit sorting ensures the correct order before saving.
   
   Does this explanation clarify the reason behind the additional sorting?



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