galovics commented on code in PR #4220:
URL: https://github.com/apache/fineract/pull/4220#discussion_r1884131063


##########
fineract-provider/src/main/java/org/apache/fineract/portfolio/loanaccount/service/LoanAccrualActivityProcessingServiceImpl.java:
##########
@@ -46,58 +51,93 @@
 public class LoanAccrualActivityProcessingServiceImpl implements 
LoanAccrualActivityProcessingService {
 
     private final LoanRepositoryWrapper loanRepositoryWrapper;
-    private final LoanWritePlatformService loanWritePlatformService;
     private final ExternalIdFactory externalIdFactory;
     private final BusinessEventNotifierService businessEventNotifierService;
-    private final LoanChargeValidator loanChargeValidator;
+    private final LoanTransactionAssembler loanTransactionAssembler;
+    private final LoanAccountDomainService loanAccountDomainService;
 
     @Override
     @Transactional(propagation = Propagation.REQUIRES_NEW)
-    public void makeAccrualActivityTransaction(Long loanId, final LocalDate 
currentDate) {
+    public void makeAccrualActivityTransaction(@NotNull Long loanId, @NotNull 
LocalDate currentDate) {
         Loan loan = loanRepositoryWrapper.findOneWithNotFoundDetection(loanId, 
true);
         makeAccrualActivityTransaction(loan, currentDate);
     }
 
     @Override
-    public Loan makeAccrualActivityTransaction(Loan loan, final LocalDate 
currentDate) {
-        if 
(loan.getLoanProductRelatedDetail().isEnableAccrualActivityPosting()) {
-            // check if loan has installment due on business day
-            Optional<LoanRepaymentScheduleInstallment> first = 
loan.getRepaymentScheduleInstallments().stream()
-                    .filter(loanRepaymentScheduleInstallment -> 
loanRepaymentScheduleInstallment.getDueDate().isEqual(currentDate))
-                    .findFirst();
-            if (first.isPresent()) {
-                final LoanRepaymentScheduleInstallment installment = 
first.get();
-                // check if there is no not-replayed-accrual-activity related 
to business date
-                List<LoanTransaction> loanTransactions = 
loan.getLoanTransactions(loanTransaction -> loanTransaction.isNotReversed()
-                        && loanTransaction.isAccrualActivity() && 
loanTransaction.getTransactionDate().isEqual(currentDate));
-                if (loanTransactions.isEmpty()) {
-                    loan = 
loanWritePlatformService.makeAccrualActivityTransaction(loan, installment, 
currentDate);
-                }
+    public void makeAccrualActivityTransaction(@NotNull Loan loan, @NotNull 
LocalDate currentDate) {
+        if 
(!loan.getLoanProductRelatedDetail().isEnableAccrualActivityPosting()) {
+            return;
+        }
+        // check if loan has installment in the past or due on current date
+        List<LoanRepaymentScheduleInstallment> installments = loan
+                .getRepaymentScheduleInstallments(i -> !i.isDownPayment() && 
!DateUtils.isBefore(currentDate, i.getDueDate()));
+        for (LoanRepaymentScheduleInstallment installment : installments) {
+            LocalDate dueDate = installment.getDueDate();
+            // check if there is any not-replayed-accrual-activity related to 
business date
+            ArrayList<LoanTransaction> existingActivities = new ArrayList<>(
+                    loan.getLoanTransactions(t -> t.isNotReversed() && 
t.isAccrualActivity() && t.getTransactionDate().isEqual(dueDate)));
+            if (existingActivities.isEmpty()) {
+                makeAccrualActivityTransaction(loan, installment, dueDate);
+            } else {
+                LoanTransaction existingActivity = existingActivities.get(0);
+                reverseReplayAccrualActivityTransaction(loan, 
existingActivity, installment, dueDate);
+                existingActivities.remove(existingActivity);
+                
existingActivities.forEach(this::reverseAccrualActivityTransaction);
             }
         }
-        return loan;
     }
 
     @Override
     @Transactional
-    public void processAccrualActivityForLoanClosure(Loan loan) {
+    public void processAccrualActivityForLoanClosure(@NotNull Loan loan) {
+        if 
(!loan.getLoanProductRelatedDetail().isEnableAccrualActivityPosting()) {
+            return;
+        }
         LocalDate date = loan.isOverPaid() ? loan.getOverpaidOnDate() : 
loan.getClosedOnDate();
         List<LoanTransaction> accrualActivityTransaction = 
loan.getLoanTransactions().stream().filter(LoanTransaction::isNotReversed)
                 
.filter(LoanTransaction::isAccrualActivity).filter(loanTransaction -> 
loanTransaction.getDateOf().isAfter(date)).toList();
         if (!accrualActivityTransaction.isEmpty()) {
             
accrualActivityTransaction.forEach(this::reverseAccrualActivityTransaction);
         }
         LoanTransaction loanTransaction = 
assembleClosingAccrualActivityTransaction(loan, date);
-        if (loanTransaction.getAmount().compareTo(BigDecimal.ZERO) != 0) {
-            loanWritePlatformService.makeAccrualActivityTransaction(loan, 
loanTransaction);
+        if (loanTransaction != null) {
+            makeAccrualActivityTransaction(loan, loanTransaction);
         }
     }
 
-    private void reverseAccrualActivityTransaction(LoanTransaction 
loanTransaction) {
-        
loanChargeValidator.validateRepaymentTypeTransactionNotBeforeAChargeRefund(loanTransaction.getLoan(),
 loanTransaction, "reversed");
-        loanTransaction.reverse();
-        LoanAdjustTransactionBusinessEvent.Data data = new 
LoanAdjustTransactionBusinessEvent.Data(loanTransaction);
-        businessEventNotifierService.notifyPostBusinessEvent(new 
LoanAdjustTransactionBusinessEvent(data));
+    @Override
+    @Transactional
+    public void processAccrualActivityForLoanReopen(@NotNull Loan loan) {

Review Comment:
   I honestly disagree with it and the reason of not reverting the method 
movement (rewrote most of the methods) doesn't have a ground. Even if you 
refactored the method bodies, you can revert the method movement - making it 
possible to review it.
   
   I'll approve it just to get the ball going but next time, please keep in 
mind to not do unnecessary changes that makes PR reviews hell.



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