kulminsky commented on code in PR #4273:
URL: https://github.com/apache/fineract/pull/4273#discussion_r1932729544


##########
fineract-provider/src/main/java/org/apache/fineract/portfolio/loanaccount/service/LoanAccrualActivityProcessingServiceImpl.java:
##########
@@ -92,42 +92,94 @@ public void processAccrualActivityForLoanClosure(@NotNull 
Loan loan) {
         if 
(!loan.getLoanProductRelatedDetail().isEnableAccrualActivityPosting()) {
             return;
         }
+
         LocalDate transactionDate = loan.isOverPaid() ? 
loan.getOverpaidOnDate() : loan.getClosedOnDate();
-        // reverse after closure activities
+
+        // Reverse accrual activities posted after the closure date
         loan.getLoanTransactions(t -> t.isAccrualActivity() && !t.isReversed() 
&& t.getDateOf().isAfter(transactionDate))
                 .forEach(this::reverseAccrualActivityTransaction);
 
-        // calculate activity amounts
         BigDecimal feeChargesPortion = BigDecimal.ZERO;
         BigDecimal penaltyChargesPortion = BigDecimal.ZERO;
         BigDecimal interestPortion = BigDecimal.ZERO;
-        // collect installment amounts
+
+        // Calculate total portions from all installments
         for (LoanRepaymentScheduleInstallment installment : 
loan.getRepaymentScheduleInstallments()) {
-            feeChargesPortion = MathUtil.add(feeChargesPortion, 
installment.getFeeChargesCharged());
-            penaltyChargesPortion = MathUtil.add(penaltyChargesPortion, 
installment.getPenaltyCharges());
-            interestPortion = MathUtil.add(interestPortion, 
installment.getInterestCharged());
+            if (!installment.isDownPayment()) { // Exclude downpayment 
installments
+                feeChargesPortion = MathUtil.add(feeChargesPortion, 
installment.getFeeChargesCharged());
+                penaltyChargesPortion = MathUtil.add(penaltyChargesPortion, 
installment.getPenaltyCharges());
+                interestPortion = MathUtil.add(interestPortion, 
installment.getInterestCharged());
+            }
         }
+
+        // Subtract already posted accrual activities
         List<LoanTransaction> accrualActivities = loan.getLoanTransactions(t 
-> t.isAccrualActivity() && !t.isReversed());
-        // subtract already posted activities
         for (LoanTransaction accrualActivity : accrualActivities) {
-            if (MathUtil.isLessThan(feeChargesPortion, 
accrualActivity.getFeeChargesPortion())
-                    || MathUtil.isLessThan(penaltyChargesPortion, 
accrualActivity.getPenaltyChargesPortion())
-                    || MathUtil.isLessThan(interestPortion, 
accrualActivity.getInterestPortion())) {
-                reverseAccrualActivityTransaction(accrualActivity);
-            } else {
-                feeChargesPortion = MathUtil.subtract(feeChargesPortion, 
accrualActivity.getFeeChargesPortion());
-                penaltyChargesPortion = 
MathUtil.subtract(penaltyChargesPortion, 
accrualActivity.getPenaltyChargesPortion());
-                interestPortion = MathUtil.subtract(interestPortion, 
accrualActivity.getInterestPortion());
+            feeChargesPortion = MathUtil.subtract(feeChargesPortion, 
accrualActivity.getFeeChargesPortion());
+            penaltyChargesPortion = MathUtil.subtract(penaltyChargesPortion, 
accrualActivity.getPenaltyChargesPortion());
+            interestPortion = MathUtil.subtract(interestPortion, 
accrualActivity.getInterestPortion());
+        }
+
+        // Check each past installment for accrual activity
+        for (LoanRepaymentScheduleInstallment installment : 
loan.getRepaymentScheduleInstallments()) {
+            if (!installment.isDownPayment()) {
+                List<LoanTransaction> installmentAccruals = 
accrualActivities.stream()
+                        .filter(t -> 
t.getDateOf().isEqual(installment.getDueDate())).toList();
+
+                if (installmentAccruals.isEmpty()) {
+                    // No AAT for this installment; create one
+                    createAccrualActivityTransaction(loan, installment, 
transactionDate);
+
+                    // Subtract processed portions
+                    feeChargesPortion = MathUtil.subtract(feeChargesPortion, 
installment.getFeeChargesCharged());
+                    penaltyChargesPortion = 
MathUtil.subtract(penaltyChargesPortion, installment.getPenaltyCharges());
+                    interestPortion = MathUtil.subtract(interestPortion, 
installment.getInterestCharged());
+                } else if (installmentAccruals.size() > 1 || 
!isAccrualActivityConsistent(installment, installmentAccruals.get(0))) {
+                    // Reverse and recreate if inconsistent or duplicate
+                    
installmentAccruals.forEach(this::reverseAccrualActivityTransaction);
+                    createAccrualActivityTransaction(loan, installment, 
transactionDate);
+
+                    // Subtract processed portions
+                    feeChargesPortion = MathUtil.subtract(feeChargesPortion, 
installment.getFeeChargesCharged());

Review Comment:
   Done



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