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


##########
fineract-provider/src/main/java/org/apache/fineract/portfolio/delinquency/service/DelinquencyReadPlatformServiceImpl.java:
##########
@@ -109,89 +104,22 @@ public Collection<LoanDelinquencyTagHistoryData> 
retrieveDelinquencyRangeHistory
 
     @Override
     public CollectionData calculateLoanCollectionData(final Long loanId) {
-        final LocalDate businessDate = DateUtils.getBusinessLocalDate();
         final Optional<Loan> optLoan = this.loanRepository.findById(loanId);
 
         CollectionData collectionData = CollectionData.template();
         if (optLoan.isPresent()) {
+            final LocalDate businessDate = DateUtils.getBusinessLocalDate();
             final Loan loan = optLoan.get();
 
+            collectionData = 
this.loanCollectionDomainService.getOverdueCollectionData(loan, businessDate);

Review Comment:
   `this.` not needed.



##########
fineract-provider/src/main/java/org/apache/fineract/portfolio/delinquency/service/DelinquencyWritePlatformServiceImpl.java:
##########
@@ -152,61 +149,9 @@ public LoanScheduleDelinquencyData 
calculateDelinquencyData(LoanScheduleDelinque
         if (loan == null) {
             loan = 
this.loanRepository.findOneWithNotFoundDetection(loanScheduleDelinquencyData.getLoanId());
         }
-
-        final Integer graceOnArrearAgeing = 
loan.getLoanProduct().getLoanProductRelatedDetail().getGraceOnArrearsAgeing();
-        final LocalDate overdueSinceDate = getOverdueSinceDate(loan, 
businessDate, graceOnArrearAgeing);
-        final Long overdueDays = calculateOverdueDays(businessDate, 
overdueSinceDate);
-        return new LoanScheduleDelinquencyData(loan.getId(), overdueSinceDate, 
overdueDays, loan);
-    }
-
-    @Override
-    public LocalDate getOverdueSinceDate(final Loan loan, final LocalDate 
businessDate, final Integer graceOnArrearAgeing) {
-        LoanRepaymentScheduleInstallment loanRepaymentSchedule = null;
-        for (LoanRepaymentScheduleInstallment installment : 
loan.getRepaymentScheduleInstallments()) {
-            if (installment.getDueDate().isBefore(businessDate) && 
installment.isNotFullyPaidOff()) {
-                loanRepaymentSchedule = installment;
-                break;
-            }
-        }
-
-        LocalDate overdueSinceDate = null;
-        final MonetaryCurrency loanCurrency = loan.getCurrency();
-        final List<LoanTransaction> loanTransactions = 
loan.retrieveListOfTransactionsByType(LoanTransactionType.CHARGEBACK);
-        if (loanRepaymentSchedule != null) {
-            // Default Due date
-            overdueSinceDate = loanRepaymentSchedule.getDueDate();
-        }
-
-        // If there is some Loan Transaction Chargeback
-        for (LoanTransaction loanTransaction : loanTransactions) {
-            if 
(loanTransaction.getLoanTransactionToRepaymentScheduleMappings().iterator().hasNext())
 {
-                final LoanTransactionToRepaymentScheduleMapping 
transactionMapping = loanTransaction
-                        
.getLoanTransactionToRepaymentScheduleMappings().iterator().next();
-
-                if (transactionMapping != null
-                        && 
!transactionMapping.getLoanRepaymentScheduleInstallment().isPrincipalCompleted(loanCurrency))
 {
-                    overdueSinceDate = loanTransaction.getTransactionDate();
-                    break;
-                }
-            }
-        }
-
-        // Include grace days If It's defined
-        if (overdueSinceDate != null && graceOnArrearAgeing != null) {
-            overdueSinceDate = 
overdueSinceDate.plusDays(graceOnArrearAgeing.longValue());
-        }
-        return overdueSinceDate;
-    }
-
-    private Long calculateOverdueDays(final LocalDate businessDate, LocalDate 
overdueSinceDate) {
-        Long overdueDays = 0L;
-        if (overdueSinceDate != null) {
-            overdueDays = DateUtils.getDifferenceInDays(overdueSinceDate, 
businessDate);
-            if (overdueDays < 0) {
-                overdueDays = 0L;
-            }
-        }
-        return overdueDays;
+        final CollectionData collectionData = 
this.loanCollectionDomainService.getOverdueCollectionData(loan, businessDate);
+        log.debug("Delinquency {}", collectionData.toString());

Review Comment:
   No need for the explicit toString call.



##########
fineract-provider/src/main/java/org/apache/fineract/portfolio/delinquency/service/DelinquencyWritePlatformServiceImpl.java:
##########
@@ -233,15 +174,9 @@ public void 
applyDelinquencyTagToLoan(LoanScheduleDelinquencyData loanDelinquenc
         if (loan.hasDelinquencyBucket()) {
             final LocalDate businessDate = DateUtils.getBusinessLocalDate();
             final DelinquencyBucket delinquencyBucket = 
loan.getLoanProduct().getDelinquencyBucket();
-            final Integer graceOnArrearAgeing = 
loan.getLoanProduct().getLoanProductRelatedDetail().getGraceOnArrearsAgeing();
-
-            LocalDate overdueSinceDate = 
loanDelinquencyData.getOverdueSinceDate();
-            if (overdueSinceDate == null) {
-                getOverdueSinceDate(loan, businessDate, graceOnArrearAgeing);
-            }
-
-            final Long overdueDays = calculateOverdueDays(businessDate, 
overdueSinceDate);
-            lookUpDelinquencyRange(loan, delinquencyBucket, overdueDays);
+            final CollectionData collectionData = 
this.loanCollectionDomainService.getOverdueCollectionData(loan, businessDate);
+            log.debug("Delinquency {}", collectionData.toString());

Review Comment:
   No need for toString



##########
fineract-provider/src/main/java/org/apache/fineract/portfolio/delinquency/service/DelinquencyWritePlatformServiceImpl.java:
##########
@@ -217,12 +162,8 @@ public CommandProcessingResult 
applyDelinquencyTagToLoan(Long loanId, JsonComman
         final DelinquencyBucket delinquencyBucket = 
loan.getLoanProduct().getDelinquencyBucket();
         if (delinquencyBucket != null) {
             final LocalDate businessDate = DateUtils.getBusinessLocalDate();
-
-            final Integer graceOnArrearAgeing = 
loan.getLoanProduct().getLoanProductRelatedDetail().getGraceOnArrearsAgeing();
-            final LocalDate overdueSinceDate = getOverdueSinceDate(loan, 
businessDate, graceOnArrearAgeing);
-
-            final Long overdueDays = calculateOverdueDays(businessDate, 
overdueSinceDate);
-            changes = lookUpDelinquencyRange(loan, delinquencyBucket, 
overdueDays);
+            final CollectionData collectionData = 
this.loanCollectionDomainService.getOverdueCollectionData(loan, businessDate);

Review Comment:
   No need for `this.`



##########
fineract-provider/src/main/java/org/apache/fineract/portfolio/delinquency/service/DelinquencyWritePlatformServiceImpl.java:
##########
@@ -233,15 +174,9 @@ public void 
applyDelinquencyTagToLoan(LoanScheduleDelinquencyData loanDelinquenc
         if (loan.hasDelinquencyBucket()) {
             final LocalDate businessDate = DateUtils.getBusinessLocalDate();
             final DelinquencyBucket delinquencyBucket = 
loan.getLoanProduct().getDelinquencyBucket();
-            final Integer graceOnArrearAgeing = 
loan.getLoanProduct().getLoanProductRelatedDetail().getGraceOnArrearsAgeing();
-
-            LocalDate overdueSinceDate = 
loanDelinquencyData.getOverdueSinceDate();
-            if (overdueSinceDate == null) {
-                getOverdueSinceDate(loan, businessDate, graceOnArrearAgeing);
-            }
-
-            final Long overdueDays = calculateOverdueDays(businessDate, 
overdueSinceDate);
-            lookUpDelinquencyRange(loan, delinquencyBucket, overdueDays);
+            final CollectionData collectionData = 
this.loanCollectionDomainService.getOverdueCollectionData(loan, businessDate);

Review Comment:
   As above.



##########
fineract-provider/src/main/java/org/apache/fineract/portfolio/delinquency/service/DelinquencyWritePlatformServiceImpl.java:
##########
@@ -152,61 +149,9 @@ public LoanScheduleDelinquencyData 
calculateDelinquencyData(LoanScheduleDelinque
         if (loan == null) {
             loan = 
this.loanRepository.findOneWithNotFoundDetection(loanScheduleDelinquencyData.getLoanId());
         }
-
-        final Integer graceOnArrearAgeing = 
loan.getLoanProduct().getLoanProductRelatedDetail().getGraceOnArrearsAgeing();
-        final LocalDate overdueSinceDate = getOverdueSinceDate(loan, 
businessDate, graceOnArrearAgeing);
-        final Long overdueDays = calculateOverdueDays(businessDate, 
overdueSinceDate);
-        return new LoanScheduleDelinquencyData(loan.getId(), overdueSinceDate, 
overdueDays, loan);
-    }
-
-    @Override
-    public LocalDate getOverdueSinceDate(final Loan loan, final LocalDate 
businessDate, final Integer graceOnArrearAgeing) {
-        LoanRepaymentScheduleInstallment loanRepaymentSchedule = null;
-        for (LoanRepaymentScheduleInstallment installment : 
loan.getRepaymentScheduleInstallments()) {
-            if (installment.getDueDate().isBefore(businessDate) && 
installment.isNotFullyPaidOff()) {
-                loanRepaymentSchedule = installment;
-                break;
-            }
-        }
-
-        LocalDate overdueSinceDate = null;
-        final MonetaryCurrency loanCurrency = loan.getCurrency();
-        final List<LoanTransaction> loanTransactions = 
loan.retrieveListOfTransactionsByType(LoanTransactionType.CHARGEBACK);
-        if (loanRepaymentSchedule != null) {
-            // Default Due date
-            overdueSinceDate = loanRepaymentSchedule.getDueDate();
-        }
-
-        // If there is some Loan Transaction Chargeback
-        for (LoanTransaction loanTransaction : loanTransactions) {
-            if 
(loanTransaction.getLoanTransactionToRepaymentScheduleMappings().iterator().hasNext())
 {
-                final LoanTransactionToRepaymentScheduleMapping 
transactionMapping = loanTransaction
-                        
.getLoanTransactionToRepaymentScheduleMappings().iterator().next();
-
-                if (transactionMapping != null
-                        && 
!transactionMapping.getLoanRepaymentScheduleInstallment().isPrincipalCompleted(loanCurrency))
 {
-                    overdueSinceDate = loanTransaction.getTransactionDate();
-                    break;
-                }
-            }
-        }
-
-        // Include grace days If It's defined
-        if (overdueSinceDate != null && graceOnArrearAgeing != null) {
-            overdueSinceDate = 
overdueSinceDate.plusDays(graceOnArrearAgeing.longValue());
-        }
-        return overdueSinceDate;
-    }
-
-    private Long calculateOverdueDays(final LocalDate businessDate, LocalDate 
overdueSinceDate) {
-        Long overdueDays = 0L;
-        if (overdueSinceDate != null) {
-            overdueDays = DateUtils.getDifferenceInDays(overdueSinceDate, 
businessDate);
-            if (overdueDays < 0) {
-                overdueDays = 0L;
-            }
-        }
-        return overdueDays;
+        final CollectionData collectionData = 
this.loanCollectionDomainService.getOverdueCollectionData(loan, businessDate);

Review Comment:
   `this.` not needed



##########
fineract-provider/src/main/java/org/apache/fineract/portfolio/delinquency/service/LoanCollectionDomainServiceImpl.java:
##########
@@ -0,0 +1,135 @@
+/**
+ * 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 javax.transaction.Transactional;
+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;
+import org.springframework.stereotype.Service;
+
+@Slf4j
+@Service
+public class LoanCollectionDomainServiceImpl implements 
LoanCollectionDomainService {

Review Comment:
   Don't like the naming still, especially the terminology CollectionData 
because it doesn't reflect anything about the data it represents.
   But I can live with it for now.
   
   The other thing though which I can't live without is proper unit test 
coverage for this class.



##########
fineract-provider/src/main/java/org/apache/fineract/portfolio/loanaccount/serialization/LoanEventApiJsonValidator.java:
##########
@@ -237,7 +237,7 @@ public void validateChargebackTransaction(final String 
json) {
         final BigDecimal transactionAmount = this.fromApiJsonHelper
                 
.extractBigDecimalWithLocaleNamed(LoanApiConstants.TRANSACTION_AMOUNT_PARAMNAME,
 element);
         
baseDataValidator.reset().parameter(LoanApiConstants.TRANSACTION_AMOUNT_PARAMNAME).value(transactionAmount).notNull()
-                .zeroOrPositiveAmount();
+                .positiveAmount();

Review Comment:
   This is a separate bugfix. Please extract it to a different PR so it can be 
reviewed separately along with its tests.



##########
fineract-provider/src/main/java/org/apache/fineract/portfolio/delinquency/service/LoanCollectionDomainService.java:
##########
@@ -0,0 +1,36 @@
+/**
+ * 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.time.LocalDate;
+import org.apache.fineract.portfolio.loanaccount.data.CollectionData;
+import org.apache.fineract.portfolio.loanaccount.domain.Loan;
+
+public interface LoanCollectionDomainService {
+
+    /**
+     * This method is to calculate the Overdue date and other properties, If 
the loan is overdue or If there is some
+     * Charge back transaction
+     *
+     * @param loan
+     * @param businessDate
+     */
+    CollectionData getOverdueCollectionData(Loan loan, LocalDate businessDate);

Review Comment:
   If we're always passing the current business date, why is it a parameter? 
Can't we just resolve the current business date from within the 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