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


##########
fineract-provider/src/main/java/org/apache/fineract/portfolio/delinquency/service/DelinquencyWritePlatformServiceImpl.java:
##########
@@ -134,8 +146,72 @@ public CommandProcessingResult 
deleteDelinquencyBucket(Long delinquencyBucketId,
                         "Data integrity issue with resource: " + 
delinquencyBucket.getId());
             }
             repositoryBucket.delete(delinquencyBucket);
+            return new 
CommandProcessingResultBuilder().withCommandId(command.commandId()).withEntityId(delinquencyBucket.getId()).build();
         }
-        return new 
CommandProcessingResultBuilder().withCommandId(command.commandId()).withEntityId(delinquencyBucket.getId()).build();
+        return new 
CommandProcessingResultBuilder().withCommandId(command.commandId()).withEntityId(delinquencyBucketId).build();
+    }
+
+    @Override
+    public LoanScheduleDelinquencyData 
calculateDelinquencyData(LoanScheduleDelinquencyData 
loanScheduleDelinquencyData) {
+        final LocalDate businessDate = DateUtils.getBusinessLocalDate();
+        Loan loan = loanScheduleDelinquencyData.getLoan();
+        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(), 
loan.getLoanProduct().getId(), overdueSinceDate, overdueDays, loan);
+    }
+
+    private LocalDate getOverdueSinceDate(final Loan loan, final LocalDate 
businessDate, final Integer graceOnArrearAgeing) {
+        final LoanRepaymentScheduleInstallment loanRepaymentSchedule = 
this.loanRepaymentScheduleInstallmentRepository
+                
.findFirstByLoanAndDueDateLessThanEqualAndObligationsMetOnDateOrderByDueDate(loan,
 businessDate, null);
+
+        LocalDate overdueSinceDate = null;
+        final MonetaryCurrency loanCurrency = loan.getCurrency();
+        if (loanRepaymentSchedule != null) {
+            // Default Due date
+            overdueSinceDate = loanRepaymentSchedule.getDueDate();
+            // Include grace days If It's defined
+            if (overdueSinceDate != null && graceOnArrearAgeing != null) {
+                overdueSinceDate = 
overdueSinceDate.plusDays(graceOnArrearAgeing.longValue());
+            }
+
+            // If there is some Loan Transaction Chargeback
+            List<LoanTransaction> loanTransactions = 
this.loanTransactionRepository
+                    
.findByLoanAndTypeOfAndDateOfLessThanEqualAndReversedOrderByDateOfDesc(loan, 
LoanTransactionType.CHARGEBACK.getValue(),
+                            overdueSinceDate, false);
+            for (LoanTransaction loanTransaction : loanTransactions) {
+                final LoanTransactionToRepaymentScheduleMapping 
transactionMapping = this.loanTransactionRepaymentScheduleMappingRepository

Review Comment:
   LoanTransactionToRepaymentScheduleMapping is part of the LoanTransaction. 
You do not need to go to DB to fetch it. Please iterate through the 
loanTransaction.getLoanTransactionToRepaymentScheduleMappings()



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