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


##########
fineract-provider/src/main/java/org/apache/fineract/portfolio/delinquency/service/DelinquencyUtils.java:
##########
@@ -0,0 +1,117 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.fineract.portfolio.delinquency.service;
+
+import java.math.BigDecimal;
+import java.time.LocalDate;
+import java.util.Iterator;
+import org.apache.fineract.infrastructure.core.service.DateUtils;
+import org.apache.fineract.organisation.monetary.domain.MonetaryCurrency;
+import org.apache.fineract.portfolio.loanaccount.data.CollectionData;
+import org.apache.fineract.portfolio.loanaccount.domain.Loan;
+import 
org.apache.fineract.portfolio.loanaccount.domain.LoanRepaymentScheduleInstallment;
+import org.apache.fineract.portfolio.loanaccount.domain.LoanTransaction;
+import 
org.apache.fineract.portfolio.loanaccount.domain.LoanTransactionToRepaymentScheduleMapping;
+
+public final class DelinquencyUtils {
+
+    private DelinquencyUtils() {}
+
+    public static CollectionData getOverdueCollectionData(final Loan loan, 
final LocalDate businessDate) {
+        final MonetaryCurrency loanCurrency = loan.getCurrency();
+        LocalDate overdueSinceDate = null;
+        CollectionData collectionData = CollectionData.template();
+        LoanRepaymentScheduleInstallment nextInstallment = null;
+        BigDecimal amountAvailable = BigDecimal.ZERO;
+        BigDecimal outstandingAmount = BigDecimal.ZERO;
+        boolean oldestOverdueInstallment = false;
+
+        // Get the oldest overdue installment if exists one
+        for (LoanRepaymentScheduleInstallment installment : 
loan.getRepaymentScheduleInstallments()) {
+            if (!installment.isObligationsMet()) {
+                if (installment.getDueDate().isBefore(businessDate)) {
+                    if (!oldestOverdueInstallment) {
+                        oldestOverdueInstallment = true;
+                        overdueSinceDate = installment.getDueDate();
+                    }
+                    outstandingAmount = 
outstandingAmount.add(installment.getTotalOutstanding(loanCurrency).getAmount());
+                } else {
+                    if (nextInstallment == null) {
+                        nextInstallment = installment;
+                        break;
+                    }
+                }
+            }
+        }
+
+        if (nextInstallment == null && 
loan.getRepaymentScheduleInstallments().size() > 0) {
+            nextInstallment = 
loan.getRepaymentScheduleInstallments().get(loan.getRepaymentScheduleInstallments().size()
 - 1);
+        }
+
+        // If exists a next no overdue installment
+        if (nextInstallment != null) {

Review Comment:
   I think this logic is not considering properly the chargeback transactions.
   **Example:**
   - There are 2 installments
   - First one is not fully paid and overdue and contains a chargeback 
transaction
   - The second one is not due yet 
   
   The logic here will not set the overdueSinceDate by the chargeback 
transaction as it was never checked.
   The nextInstallment will be set to check the second installment, which is 
not even due and  might overwrite the overdueSinceDate which is wrong, the 
first unpaid chargeback transaction is in the first unpaid installment
   



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