JohnAlva commented on code in PR #4886:
URL: https://github.com/apache/fineract/pull/4886#discussion_r2308102684


##########
fineract-provider/src/main/java/org/apache/fineract/portfolio/savings/service/SavingsAccountInterestPostingServiceImpl.java:
##########
@@ -268,16 +305,39 @@ public List<PostingPeriod> calculateInterestUsing(final 
MathContext mc, final Lo
             if 
(postedAsOnDates.contains(periodInterval.endDate().plusDays(1))) {
                 isUserPosting = true;
             }
-            final PostingPeriod postingPeriod = 
PostingPeriod.createFromDTO(periodInterval, periodStartingBalance,
-                    
retreiveOrderedNonInterestPostingTransactions(savingsAccountData), 
monetaryCurrency, compoundingPeriodType,
-                    interestCalculationType, interestRateAsFraction, 
daysInYearType.getValue(), upToInterestCalculationDate,
-                    interestPostTransactions, isInterestTransfer, 
minBalanceForInterestCalculation,
-                    isSavingsInterestPostingAtCurrentPeriodEnd, 
overdraftInterestRateAsFraction, minOverdraftForInterestCalculation,
-                    isUserPosting, financialYearBeginningMonth, 
savingsAccountData.isAllowOverdraft());
+            if (savingsAccountData.isAllowOverdraft() && 
!MathUtil.isZero(savingsAccountData.getGlAccountIdForInterestReceivable())) {
 
-            periodStartingBalance = postingPeriod.closingBalance();
+                List<SavingsAccountTransactionData> overdraftTxs = 
listForOverdraft(savingsAccountData, periodInterval);
+                List<SavingsAccountTransactionData> interestPostingTxs = 
listForInterestPosting(savingsAccountData, periodInterval,
+                        monetaryCurrency);
 
-            allPostingPeriods.add(postingPeriod);
+                boolean isOverdraftAccountType = 
isOverdraftAccount(savingsAccountData, periodInterval, monetaryCurrency);
+
+                List<SavingsAccountTransactionData> first = 
isOverdraftAccountType ? overdraftTxs : interestPostingTxs;

Review Comment:
   I’ve updated the variable names to primaryInterestPublication and 
secondaryInterestPublication so they better describe their purpose and make the 
code easier to read



##########
fineract-provider/src/main/java/org/apache/fineract/portfolio/savings/service/SavingsAccountInterestPostingServiceImpl.java:
##########
@@ -268,16 +305,39 @@ public List<PostingPeriod> calculateInterestUsing(final 
MathContext mc, final Lo
             if 
(postedAsOnDates.contains(periodInterval.endDate().plusDays(1))) {
                 isUserPosting = true;
             }
-            final PostingPeriod postingPeriod = 
PostingPeriod.createFromDTO(periodInterval, periodStartingBalance,
-                    
retreiveOrderedNonInterestPostingTransactions(savingsAccountData), 
monetaryCurrency, compoundingPeriodType,
-                    interestCalculationType, interestRateAsFraction, 
daysInYearType.getValue(), upToInterestCalculationDate,
-                    interestPostTransactions, isInterestTransfer, 
minBalanceForInterestCalculation,
-                    isSavingsInterestPostingAtCurrentPeriodEnd, 
overdraftInterestRateAsFraction, minOverdraftForInterestCalculation,
-                    isUserPosting, financialYearBeginningMonth, 
savingsAccountData.isAllowOverdraft());
+            if (savingsAccountData.isAllowOverdraft() && 
!MathUtil.isZero(savingsAccountData.getGlAccountIdForInterestReceivable())) {
 
-            periodStartingBalance = postingPeriod.closingBalance();
+                List<SavingsAccountTransactionData> overdraftTxs = 
listForOverdraft(savingsAccountData, periodInterval);
+                List<SavingsAccountTransactionData> interestPostingTxs = 
listForInterestPosting(savingsAccountData, periodInterval,
+                        monetaryCurrency);
 
-            allPostingPeriods.add(postingPeriod);
+                boolean isOverdraftAccountType = 
isOverdraftAccount(savingsAccountData, periodInterval, monetaryCurrency);
+
+                List<SavingsAccountTransactionData> first = 
isOverdraftAccountType ? overdraftTxs : interestPostingTxs;
+                List<SavingsAccountTransactionData> second = 
isOverdraftAccountType ? interestPostingTxs : overdraftTxs;
+
+                periodStartingBalance = 
appendPostingPeriodIfAny(periodInterval, periodStartingBalance, first, 
monetaryCurrency,
+                        compoundingPeriodType, interestCalculationType, 
interestRateAsFraction, daysInYearType.getValue(),
+                        upToInterestCalculationDate, interestPostTransactions, 
isInterestTransfer, minBalanceForInterestCalculation,
+                        isSavingsInterestPostingAtCurrentPeriodEnd, 
overdraftInterestRateAsFraction, minOverdraftForInterestCalculation,
+                        isUserPosting, financialYearBeginningMonth, 
savingsAccountData.isAllowOverdraft(), allPostingPeriods,
+                        isOverdraftAccountType ? true : false);
+
+                periodStartingBalance = 
appendPostingPeriodIfAny(periodInterval, periodStartingBalance, second, 
monetaryCurrency,

Review Comment:
   The periodStartingBalance is updated twice because each call to 
appendPostingPeriodIfAny processes a different batch of transactions (primary 
and secondary). The second call requires the balance already updated by the 
first call so that interest calculations and minimum balance validations are 
correct and consistent.



##########
fineract-provider/src/main/java/org/apache/fineract/portfolio/savings/service/SavingsAccountInterestPostingServiceImpl.java:
##########
@@ -509,6 +615,22 @@ protected SavingsAccountTransactionData 
findInterestPostingTransactionFor(final
         return postingTransation;
     }
 
+    protected SavingsAccountTransactionData 
findInterestPostingTransactionForInterest(final LocalDate postingDate,
+            final SavingsAccountData savingsAccountData, boolean isOverdraft) {
+        SavingsAccountTransactionData postingTransation = null;
+        List<SavingsAccountTransactionData> trans = 
savingsAccountData.getSavingsAccountTransactionData();

Review Comment:
   I’ve renamed trans to transactions to make it clearer that it holds all the 
account transactions.



##########
fineract-provider/src/main/java/org/apache/fineract/portfolio/savings/service/SavingsAccountInterestPostingServiceImpl.java:
##########
@@ -509,6 +615,22 @@ protected SavingsAccountTransactionData 
findInterestPostingTransactionFor(final
         return postingTransation;
     }
 
+    protected SavingsAccountTransactionData 
findInterestPostingTransactionForInterest(final LocalDate postingDate,
+            final SavingsAccountData savingsAccountData, boolean isOverdraft) {
+        SavingsAccountTransactionData postingTransation = null;
+        List<SavingsAccountTransactionData> trans = 
savingsAccountData.getSavingsAccountTransactionData();
+
+        for (final SavingsAccountTransactionData transaction : trans) {
+            Boolean interestSearch = isOverdraft ? 
transaction.isOverdraftInterestAndNotReversed()
+                    : transaction.isInterestPostingAndNotReversed();
+            if (interestSearch && transaction.occursOn(postingDate) && 
!transaction.isReversalTransaction()) {
+                postingTransation = transaction;

Review Comment:
   I refactored the loop to use stream(), filter(), and findFirst() for a 
cleaner and more readable implementation.



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