adamsaghy commented on code in PR #5718:
URL: https://github.com/apache/fineract/pull/5718#discussion_r3044265477
##########
fineract-working-capital-loan/src/main/java/org/apache/fineract/portfolio/workingcapitalloan/service/WorkingCapitalLoanDelinquencyRangeScheduleServiceImpl.java:
##########
@@ -175,26 +188,103 @@ private LocalDate calculateToDate(LocalDate fromDate,
Integer frequency, Delinqu
};
}
- private BigDecimal calculateExpectedAmount(WorkingCapitalLoan loan,
DelinquencyMinimumPaymentPeriodAndRule rule) {
- BigDecimal minimumPayment = rule.getMinimumPayment();
+ private BigDecimal calculateExpectedAmount(final WorkingCapitalLoan loan,
final DelinquencyMinimumPaymentPeriodAndRule rule,
+ final WorkingCapitalLoanDelinquencyAction rescheduleOverride) {
+ final BigDecimal principal = loan.getApprovedPrincipal();
+ if (principal == null) {
+ return BigDecimal.ZERO;
+ }
+ if (rescheduleOverride != null) {
+ return MathUtil.percentageOf(principal,
rescheduleOverride.getMinimumPayment(), MathContext.DECIMAL128);
+ }
+ final BigDecimal minimumPayment = rule.getMinimumPayment();
if (minimumPayment == null) {
return BigDecimal.ZERO;
}
if
(DelinquencyMinimumPaymentType.FLAT.equals(rule.getMinimumPaymentType())) {
return minimumPayment;
}
- BigDecimal principal = loan.getApprovedPrincipal();
- if (principal == null) {
- return BigDecimal.ZERO;
- }
BigDecimal discount = loan.getLoanProductRelatedDetails() != null ?
loan.getLoanProductRelatedDetails().getDiscount() : null;
BigDecimal base = discount != null ? principal.add(discount) :
principal;
return MathUtil.percentageOf(base, minimumPayment,
MathContext.DECIMAL128);
}
+ private Optional<WorkingCapitalLoanDelinquencyAction>
findLatestRescheduleAction(final Long loanId) {
+ return
actionRepository.findTopByWorkingCapitalLoanIdAndActionOrderByIdDesc(loanId,
DelinquencyAction.RESCHEDULE);
+ }
+
+ @Override
+ public void rescheduleMinimumPayment(final WorkingCapitalLoan loan, final
WorkingCapitalLoanDelinquencyAction rescheduleAction) {
+ final LocalDate businessDate = DateUtils.getBusinessLocalDate();
+ final BigDecimal newExpectedAmount = calculateExpectedAmount(loan,
null, rescheduleAction);
+ final Integer newFrequency = rescheduleAction.getFrequency();
+ final DelinquencyFrequencyType newFreqType =
rescheduleAction.getFrequencyType();
+
+ final List<WorkingCapitalLoanDelinquencyRangeSchedule> periods =
repository.findByLoanIdOrderByPeriodNumberAsc(loan.getId());
+
+ WorkingCapitalLoanDelinquencyRangeSchedule currentPeriod = null;
+ final List<WorkingCapitalLoanDelinquencyRangeSchedule> futurePeriods =
new ArrayList<>();
+
+ for (final WorkingCapitalLoanDelinquencyRangeSchedule period :
periods) {
+ if (period.getMinPaymentCriteriaMet() != null) {
+ continue;
+ }
+ final boolean isCurrent =
!period.getFromDate().isAfter(businessDate) &&
!period.getToDate().isBefore(businessDate);
+ final boolean isFuture =
period.getFromDate().isAfter(businessDate);
+
+ if (isCurrent) {
+ currentPeriod = period;
+ period.setExpectedAmount(newExpectedAmount);
+
period.setOutstandingAmount(newExpectedAmount.subtract(period.getPaidAmount()).max(BigDecimal.ZERO));
+ } else if (isFuture) {
+ futurePeriods.add(period);
+ }
+ }
+
+ repository.deleteAll(futurePeriods);
+ repository.flush();
+
+ if (currentPeriod != null) {
+ repository.saveAndFlush(currentPeriod);
+ regenerateFuturePeriods(loan, currentPeriod, newExpectedAmount,
newFrequency, newFreqType);
+ }
+
+ evaluateExpiredPeriods(loan, businessDate);
+
+ log.debug("Rescheduled delinquency range schedule for WC loan {}: new
minimumPayment={}%, frequency={} {}", loan.getId(),
+ rescheduleAction.getMinimumPayment(), newFrequency,
newFreqType);
+ }
+
+ private void regenerateFuturePeriods(final WorkingCapitalLoan loan, final
WorkingCapitalLoanDelinquencyRangeSchedule currentPeriod,
+ final BigDecimal expectedAmount, final Integer frequency, final
DelinquencyFrequencyType frequencyType) {
+ final LocalDate businessDate = DateUtils.getBusinessLocalDate();
+ int periodNumber = currentPeriod.getPeriodNumber();
+ LocalDate fromDate = currentPeriod.getToDate().plusDays(1);
+
+ while (!fromDate.isAfter(businessDate)) {
+ final LocalDate toDate = calculateToDate(fromDate, frequency,
frequencyType);
+ periodNumber++;
+
+ final WorkingCapitalLoanDelinquencyRangeSchedule period = new
WorkingCapitalLoanDelinquencyRangeSchedule();
Review Comment:
We should introduce versioning to avoid concurrent modifications.
--
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]