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


##########
fineract-provider/src/main/java/org/apache/fineract/portfolio/delinquency/service/DelinquencyUtils.java:
##########
@@ -0,0 +1,132 @@
+/**
+ * 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 lombok.extern.slf4j.Slf4j;
+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;
+
+@Slf4j
+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();
+        BigDecimal amountAvailable = BigDecimal.ZERO;
+        BigDecimal outstandingAmount = BigDecimal.ZERO;
+        boolean oldestOverdueInstallment = false;
+        boolean overdueSinceDateWasSet = false;
+        boolean firstNotYetDueInstallment = false;
+
+        log.debug("Loan id {} with {} installments", loan.getId(), 
loan.getRepaymentScheduleInstallments().size());
+        // Get the oldest overdue installment if exists one
+        for (LoanRepaymentScheduleInstallment installment : 
loan.getRepaymentScheduleInstallments()) {
+            if (installment.isObligationsMet()) {
+                continue;
+            }
+
+            if (installment.getDueDate().isBefore(businessDate)) {
+                log.debug("Loan Id: {} with installment {} due date {}", 
loan.getId(), installment.getInstallmentNumber(),
+                        installment.getDueDate());
+                outstandingAmount = 
outstandingAmount.add(installment.getTotalOutstanding(loanCurrency).getAmount());
+                if (!oldestOverdueInstallment) {
+                    log.debug("Oldest installment {} {}", 
installment.getInstallmentNumber(), installment.getDueDate());
+                    oldestOverdueInstallment = true;
+                    overdueSinceDate = installment.getDueDate();
+                    overdueSinceDateWasSet = true;
+
+                    amountAvailable = 
installment.getTotalPaid(loanCurrency).getAmount();
+
+                    for (LoanTransactionToRepaymentScheduleMapping 
mappngInstallment : installment
+                            .getLoanTransactionToRepaymentScheduleMappings()) {
+                        final LoanTransaction loanTransaction = 
mappngInstallment.getLoanTransaction();
+                        if (loanTransaction.isChargeback()) {
+                            amountAvailable = 
amountAvailable.subtract(loanTransaction.getAmount());
+                            if (amountAvailable.compareTo(BigDecimal.ZERO) < 
0) {
+                                overdueSinceDate = 
loanTransaction.getTransactionDate();
+                                break;
+                            }
+                        }
+                    }
+                }
+            } else if (!firstNotYetDueInstallment) {
+                log.debug("Loan Id: {} with installment {} due date {}", 
loan.getId(), installment.getInstallmentNumber(),
+                        installment.getDueDate());
+                firstNotYetDueInstallment = true;
+                amountAvailable = 
installment.getTotalPaid(loanCurrency).getAmount().subtract(installment.getTotalPaidInAdvance());

Review Comment:
   Are you sure we need to deduct the total paid in advance? 



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