You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@fineract.apache.org by "oleksii-novikov-onix (via GitHub)" <gi...@apache.org> on 2026/05/14 15:37:36 UTC

[PR] FINERACT-2455: Add WC near-breach evaluation business step [fineract]

oleksii-novikov-onix opened a new pull request, #5849:
URL: https://github.com/apache/fineract/pull/5849

   ## Description
   
   Describe the changes made and why they were made. (Ignore if these details are present on the associated Apache Fineract JIRA ticket.)
   
   ## Checklist
   
   Please make sure these boxes are checked before submitting your pull request - thanks!
   
   - [ ] Write the commit message as per [our guidelines](https://github.com/apache/fineract/blob/develop/CONTRIBUTING.md#pull-requests)
   - [ ] Acknowledge that we will not review PRs that are not passing the build _("green")_ - it is your responsibility to get a proposed PR to pass the build, not primarily the project's maintainers.
   - [ ] Create/update [unit or integration tests](https://fineract.apache.org/docs/current/#_testing) for verifying the changes made.
   - [ ] Follow our [coding conventions](https://cwiki.apache.org/confluence/display/FINERACT/Coding+Conventions).
   - [ ] Add required Swagger annotation and update API documentation at fineract-provider/src/main/resources/static/legacy-docs/apiLive.htm with details of any API changes
   - [ ] [This PR must not be a "code dump"](https://cwiki.apache.org/confluence/display/FINERACT/Pull+Request+Size+Limit). Large changes can be made in a branch, with assistance. Ask for help on the [developer mailing list](https://fineract.apache.org/#contribute).
   
   Your assigned reviewer(s) will follow our [guidelines for code reviews](https://cwiki.apache.org/confluence/display/FINERACT/Code+Review+Guide).
   


-- 
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: issues-unsubscribe@fineract.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] FINERACT-2455: Add WC near-breach evaluation business step [fineract]

Posted by "adamsaghy (via GitHub)" <gi...@apache.org>.
adamsaghy commented on code in PR #5849:
URL: https://github.com/apache/fineract/pull/5849#discussion_r3265910656


##########
fineract-working-capital-loan/src/main/java/org/apache/fineract/portfolio/workingcapitalloan/service/WorkingCapitalLoanBreachScheduleServiceImpl.java:
##########
@@ -148,25 +147,13 @@ private void applyRepayment(final WorkingCapitalLoanBreachSchedule period, BigDe
     }
 
     @Override
-    public void evaluateBreachAndNearBreach(final WorkingCapitalLoan loan, final LocalDate businessDate) {
+    public void evaluateBreach(final WorkingCapitalLoan loan, final LocalDate businessDate) {
         final List<WorkingCapitalLoanBreachSchedule> allPeriods = repository.findByLoanIdOrderByPeriodNumberAsc(loan.getId());

Review Comment:
   I dont think we need all periods. Fetch only the (one) relevant by effectiveDate, please



##########
fineract-working-capital-loan/src/main/java/org/apache/fineract/portfolio/workingcapitalloan/service/WorkingCapitalLoanBreachScheduleServiceImpl.java:
##########
@@ -148,25 +147,13 @@ private void applyRepayment(final WorkingCapitalLoanBreachSchedule period, BigDe
     }
 
     @Override
-    public void evaluateBreachAndNearBreach(final WorkingCapitalLoan loan, final LocalDate businessDate) {
+    public void evaluateBreach(final WorkingCapitalLoan loan, final LocalDate businessDate) {
         final List<WorkingCapitalLoanBreachSchedule> allPeriods = repository.findByLoanIdOrderByPeriodNumberAsc(loan.getId());

Review Comment:
   I dont think we need all periods. Fetch only the (one) relevant by business date, please



-- 
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: issues-unsubscribe@fineract.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] FINERACT-2455: Add WC near-breach evaluation business step [fineract]

Posted by "adamsaghy (via GitHub)" <gi...@apache.org>.
adamsaghy commented on code in PR #5849:
URL: https://github.com/apache/fineract/pull/5849#discussion_r3265764950


##########
fineract-e2e-tests-runner/src/test/resources/features/WorkingCapitalNearBreachEvaluation.feature:
##########
@@ -295,11 +269,14 @@ Feature: Working Capital Near Breach Evaluation
     And Admin successfully approves the working capital loan on "01 January 2026" with "9000" amount and expected disbursement date on "01 January 2026"
     When Admin successfully disburse the Working Capital loan on "01 January 2026" with "9000" EUR transaction amount
     And Admin runs inline COB job for Working Capital Loan by loanId
+    # P1: 01-01 -> 02-28 (2 months). freq=58d -> candidate eval at 02-28 == toDate (breach due date).
+    # Per spec: no near-breach evaluation on breach due date -> eval excluded. Period has zero applicable eval points.
+    # No eval ran -> nearBreach stays null. Breach evaluation at period end: outstanding=500>0 -> breach=true.
     When Admin sets the business date to "01 March 2026"
     And Admin runs inline COB job for Working Capital Loan by loanId
     Then Working Capital loan breach schedule has the following data:
       | periodNumber | fromDate   | toDate     | minPaymentAmount | outstandingAmount | nearBreach | breach |
-      | 1            | 2026-01-01 | 2026-02-28 | 500.00           | 500.00            | true       | true   |
+      | 1            | 2026-01-01 | 2026-02-28 | 500.00           | 500.00            | null       | true   |

Review Comment:
   We should not `null` the near-breach flag, but retain the last value.



-- 
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: issues-unsubscribe@fineract.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] FINERACT-2455: Add WC near-breach evaluation business step [fineract]

Posted by "adamsaghy (via GitHub)" <gi...@apache.org>.
adamsaghy commented on code in PR #5849:
URL: https://github.com/apache/fineract/pull/5849#discussion_r3265881614


##########
fineract-working-capital-loan/src/main/java/org/apache/fineract/cob/workingcapitalloan/businessstep/NearBreachEvaluationBusinessStep.java:
##########
@@ -0,0 +1,68 @@
+/**
+ * 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.cob.workingcapitalloan.businessstep;
+
+import java.time.LocalDate;
+import java.util.Objects;
+import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.fineract.infrastructure.core.service.DateUtils;
+import org.apache.fineract.portfolio.workingcapitalloan.domain.WorkingCapitalLoan;
+import org.apache.fineract.portfolio.workingcapitalloan.domain.WorkingCapitalLoanDisbursementDetails;
+import org.apache.fineract.portfolio.workingcapitalloan.service.WorkingCapitalLoanNearBreachEvaluationService;
+import org.apache.fineract.portfolio.workingcapitalloanproduct.domain.WorkingCapitalLoanProductRelatedDetails;
+import org.springframework.stereotype.Component;
+
+@Slf4j
+@RequiredArgsConstructor
+@Component
+public class NearBreachEvaluationBusinessStep extends WorkingCapitalLoanCOBBusinessStep {
+
+    private final WorkingCapitalLoanNearBreachEvaluationService nearBreachEvaluationService;
+
+    @Override
+    public WorkingCapitalLoan execute(final WorkingCapitalLoan input) {
+        final boolean isDisbursed = input.getDisbursementDetails().stream()
+                .map(WorkingCapitalLoanDisbursementDetails::getActualDisbursementDate).anyMatch(Objects::nonNull);
+        if (!isDisbursed) {
+            log.debug("Skipping near breach evaluation for WC loan {} - not yet disbursed", input.getId());
+            return input;
+        }

Review Comment:
   Please use the WorkingCapitalLoan.loanStatus instead.



-- 
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: issues-unsubscribe@fineract.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] FINERACT-2455: Add WC near-breach evaluation business step [fineract]

Posted by "adamsaghy (via GitHub)" <gi...@apache.org>.
adamsaghy commented on code in PR #5849:
URL: https://github.com/apache/fineract/pull/5849#discussion_r3265887122


##########
fineract-working-capital-loan/src/main/java/org/apache/fineract/cob/workingcapitalloan/businessstep/NearBreachEvaluationBusinessStep.java:
##########
@@ -0,0 +1,68 @@
+/**
+ * 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.cob.workingcapitalloan.businessstep;
+
+import java.time.LocalDate;
+import java.util.Objects;
+import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.fineract.infrastructure.core.service.DateUtils;
+import org.apache.fineract.portfolio.workingcapitalloan.domain.WorkingCapitalLoan;
+import org.apache.fineract.portfolio.workingcapitalloan.domain.WorkingCapitalLoanDisbursementDetails;
+import org.apache.fineract.portfolio.workingcapitalloan.service.WorkingCapitalLoanNearBreachEvaluationService;
+import org.apache.fineract.portfolio.workingcapitalloanproduct.domain.WorkingCapitalLoanProductRelatedDetails;
+import org.springframework.stereotype.Component;
+
+@Slf4j
+@RequiredArgsConstructor
+@Component
+public class NearBreachEvaluationBusinessStep extends WorkingCapitalLoanCOBBusinessStep {
+
+    private final WorkingCapitalLoanNearBreachEvaluationService nearBreachEvaluationService;
+
+    @Override
+    public WorkingCapitalLoan execute(final WorkingCapitalLoan input) {

Review Comment:
   I would rename `input` field to something more relevant, like: "loan".



-- 
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: issues-unsubscribe@fineract.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] FINERACT-2455: Add WC near-breach evaluation business step [fineract]

Posted by "adamsaghy (via GitHub)" <gi...@apache.org>.
adamsaghy commented on code in PR #5849:
URL: https://github.com/apache/fineract/pull/5849#discussion_r3265793940


##########
fineract-working-capital-loan/src/main/java/org/apache/fineract/portfolio/workingcapitalloan/service/WorkingCapitalLoanNearBreachEvaluationServiceImpl.java:
##########
@@ -0,0 +1,126 @@
+/**
+ * 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.workingcapitalloan.service;
+
+import java.math.BigDecimal;
+import java.time.LocalDate;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Set;
+import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.fineract.organisation.monetary.domain.MoneyHelper;
+import org.apache.fineract.portfolio.loanaccount.domain.LoanTransactionType;
+import org.apache.fineract.portfolio.workingcapitalloan.domain.WorkingCapitalLoan;
+import org.apache.fineract.portfolio.workingcapitalloan.domain.WorkingCapitalLoanBreachSchedule;
+import org.apache.fineract.portfolio.workingcapitalloan.domain.WorkingCapitalLoanPeriodFrequencyType;
+import org.apache.fineract.portfolio.workingcapitalloan.repository.WorkingCapitalLoanBreachScheduleRepository;
+import org.apache.fineract.portfolio.workingcapitalloan.repository.WorkingCapitalLoanTransactionRepository;
+import org.apache.fineract.portfolio.workingcapitalloannearbreach.domain.WorkingCapitalNearBreach;
+import org.springframework.stereotype.Service;
+
+@RequiredArgsConstructor
+@Slf4j
+@Service
+public class WorkingCapitalLoanNearBreachEvaluationServiceImpl implements WorkingCapitalLoanNearBreachEvaluationService {
+
+    private static final Set<LoanTransactionType> REDUCING_TRANSACTION_TYPES = Set.of(LoanTransactionType.REPAYMENT,
+            LoanTransactionType.GOODWILL_CREDIT);
+
+    private final WorkingCapitalLoanBreachScheduleRepository breachScheduleRepository;
+    private final WorkingCapitalLoanTransactionRepository transactionRepository;
+
+    @Override
+    public void evaluateNearBreach(final WorkingCapitalLoan loan, final LocalDate effectiveDate) {
+        final WorkingCapitalNearBreach config = loan.getLoanProductRelatedDetails().getNearBreach();
+        final List<WorkingCapitalLoanBreachSchedule> periods = breachScheduleRepository.findByLoanIdOrderByPeriodNumberAsc(loan.getId());

Review Comment:
   I dont think we need all periods. Fetch only the (one) relevant by effectiveDate, please



-- 
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: issues-unsubscribe@fineract.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] FINERACT-2455: Add WC near-breach evaluation business step [fineract]

Posted by "adamsaghy (via GitHub)" <gi...@apache.org>.
adamsaghy commented on code in PR #5849:
URL: https://github.com/apache/fineract/pull/5849#discussion_r3265887122


##########
fineract-working-capital-loan/src/main/java/org/apache/fineract/cob/workingcapitalloan/businessstep/NearBreachEvaluationBusinessStep.java:
##########
@@ -0,0 +1,68 @@
+/**
+ * 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.cob.workingcapitalloan.businessstep;
+
+import java.time.LocalDate;
+import java.util.Objects;
+import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.fineract.infrastructure.core.service.DateUtils;
+import org.apache.fineract.portfolio.workingcapitalloan.domain.WorkingCapitalLoan;
+import org.apache.fineract.portfolio.workingcapitalloan.domain.WorkingCapitalLoanDisbursementDetails;
+import org.apache.fineract.portfolio.workingcapitalloan.service.WorkingCapitalLoanNearBreachEvaluationService;
+import org.apache.fineract.portfolio.workingcapitalloanproduct.domain.WorkingCapitalLoanProductRelatedDetails;
+import org.springframework.stereotype.Component;
+
+@Slf4j
+@RequiredArgsConstructor
+@Component
+public class NearBreachEvaluationBusinessStep extends WorkingCapitalLoanCOBBusinessStep {
+
+    private final WorkingCapitalLoanNearBreachEvaluationService nearBreachEvaluationService;
+
+    @Override
+    public WorkingCapitalLoan execute(final WorkingCapitalLoan input) {

Review Comment:
   I would rename `input` field to something more relevant, like: "loan", "wcLoan" or something...



-- 
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: issues-unsubscribe@fineract.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] FINERACT-2455: Add WC near-breach evaluation business step [fineract]

Posted by "adamsaghy (via GitHub)" <gi...@apache.org>.
adamsaghy commented on code in PR #5849:
URL: https://github.com/apache/fineract/pull/5849#discussion_r3265804265


##########
fineract-working-capital-loan/src/main/java/org/apache/fineract/portfolio/workingcapitalloan/service/WorkingCapitalLoanNearBreachEvaluationServiceImpl.java:
##########
@@ -0,0 +1,126 @@
+/**
+ * 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.workingcapitalloan.service;
+
+import java.math.BigDecimal;
+import java.time.LocalDate;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Set;
+import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.fineract.organisation.monetary.domain.MoneyHelper;
+import org.apache.fineract.portfolio.loanaccount.domain.LoanTransactionType;
+import org.apache.fineract.portfolio.workingcapitalloan.domain.WorkingCapitalLoan;
+import org.apache.fineract.portfolio.workingcapitalloan.domain.WorkingCapitalLoanBreachSchedule;
+import org.apache.fineract.portfolio.workingcapitalloan.domain.WorkingCapitalLoanPeriodFrequencyType;
+import org.apache.fineract.portfolio.workingcapitalloan.repository.WorkingCapitalLoanBreachScheduleRepository;
+import org.apache.fineract.portfolio.workingcapitalloan.repository.WorkingCapitalLoanTransactionRepository;
+import org.apache.fineract.portfolio.workingcapitalloannearbreach.domain.WorkingCapitalNearBreach;
+import org.springframework.stereotype.Service;
+
+@RequiredArgsConstructor
+@Slf4j
+@Service
+public class WorkingCapitalLoanNearBreachEvaluationServiceImpl implements WorkingCapitalLoanNearBreachEvaluationService {
+
+    private static final Set<LoanTransactionType> REDUCING_TRANSACTION_TYPES = Set.of(LoanTransactionType.REPAYMENT,
+            LoanTransactionType.GOODWILL_CREDIT);
+
+    private final WorkingCapitalLoanBreachScheduleRepository breachScheduleRepository;
+    private final WorkingCapitalLoanTransactionRepository transactionRepository;
+
+    @Override
+    public void evaluateNearBreach(final WorkingCapitalLoan loan, final LocalDate effectiveDate) {
+        final WorkingCapitalNearBreach config = loan.getLoanProductRelatedDetails().getNearBreach();
+        final List<WorkingCapitalLoanBreachSchedule> periods = breachScheduleRepository.findByLoanIdOrderByPeriodNumberAsc(loan.getId());
+        final List<WorkingCapitalLoanBreachSchedule> updatedPeriods = new ArrayList<>();
+        for (final WorkingCapitalLoanBreachSchedule period : periods) {
+            if (period.getNearBreach() != null) {
+                continue;
+            }
+            if (evaluatePeriod(loan.getId(), period, config, effectiveDate)) {
+                updatedPeriods.add(period);
+            }
+        }
+        if (!updatedPeriods.isEmpty()) {
+            breachScheduleRepository.saveAllAndFlush(updatedPeriods);
+        }
+    }
+
+    private boolean evaluatePeriod(final Long loanId, final WorkingCapitalLoanBreachSchedule period, final WorkingCapitalNearBreach config,
+            final LocalDate effectiveDate) {
+        if (period.getMinPaymentAmount().compareTo(BigDecimal.ZERO) == 0) {
+            return false;
+        }
+        final List<LocalDate> evalDates = listEvalDates(period.getFromDate(), period.getToDate(), config.getFrequency(),
+                config.getFrequencyType());
+        if (evalDates.isEmpty()) {
+            return false;
+        }
+        final BigDecimal thresholdFraction = config.getThreshold().divide(BigDecimal.valueOf(100), MoneyHelper.getMathContext());
+        boolean anyPassed = false;
+        for (int index = 0; index < evalDates.size(); index++) {
+            final LocalDate evalDate = evalDates.get(index);
+            if (!effectiveDate.isAfter(evalDate)) {
+                break;
+            }
+            anyPassed = true;
+            final BigDecimal requiredCumulative = thresholdFraction.multiply(BigDecimal.valueOf(index + 1L), MoneyHelper.getMathContext())
+                    .multiply(period.getMinPaymentAmount(), MoneyHelper.getMathContext());
+            final BigDecimal paidCumulative = transactionRepository.sumPaymentsInWindow(loanId, period.getFromDate(), evalDate,

Review Comment:
   Please use Money object for appropriate rounding and number of digist after decimal point.



-- 
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: issues-unsubscribe@fineract.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] FINERACT-2455: Add WC near-breach evaluation business step [fineract]

Posted by "ruzeynalov (via GitHub)" <gi...@apache.org>.
ruzeynalov commented on code in PR #5849:
URL: https://github.com/apache/fineract/pull/5849#discussion_r3266439799


##########
fineract-e2e-tests-runner/src/test/resources/features/WorkingCapitalNearBreachEvaluation.feature:
##########
@@ -295,11 +269,14 @@ Feature: Working Capital Near Breach Evaluation
     And Admin successfully approves the working capital loan on "01 January 2026" with "9000" amount and expected disbursement date on "01 January 2026"
     When Admin successfully disburse the Working Capital loan on "01 January 2026" with "9000" EUR transaction amount
     And Admin runs inline COB job for Working Capital Loan by loanId
+    # P1: 01-01 -> 02-28 (2 months). freq=58d -> candidate eval at 02-28 == toDate (breach due date).
+    # Per spec: no near-breach evaluation on breach due date -> eval excluded. Period has zero applicable eval points.
+    # No eval ran -> nearBreach stays null. Breach evaluation at period end: outstanding=500>0 -> breach=true.
     When Admin sets the business date to "01 March 2026"
     And Admin runs inline COB job for Working Capital Loan by loanId
     Then Working Capital loan breach schedule has the following data:
       | periodNumber | fromDate   | toDate     | minPaymentAmount | outstandingAmount | nearBreach | breach |
-      | 1            | 2026-01-01 | 2026-02-28 | 500.00           | 500.00            | true       | true   |
+      | 1            | 2026-01-01 | 2026-02-28 | 500.00           | 500.00            | null       | true   |

Review Comment:
   @adamsaghy fixed in Gherkin test



-- 
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: issues-unsubscribe@fineract.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] FINERACT-2455: Add WC near-breach evaluation business step [fineract]

Posted by "oleksii-novikov-onix (via GitHub)" <gi...@apache.org>.
oleksii-novikov-onix commented on code in PR #5849:
URL: https://github.com/apache/fineract/pull/5849#discussion_r3271859380


##########
fineract-working-capital-loan/src/main/java/org/apache/fineract/cob/workingcapitalloan/businessstep/NearBreachEvaluationBusinessStep.java:
##########
@@ -0,0 +1,68 @@
+/**
+ * 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.cob.workingcapitalloan.businessstep;
+
+import java.time.LocalDate;
+import java.util.Objects;
+import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.fineract.infrastructure.core.service.DateUtils;
+import org.apache.fineract.portfolio.workingcapitalloan.domain.WorkingCapitalLoan;
+import org.apache.fineract.portfolio.workingcapitalloan.domain.WorkingCapitalLoanDisbursementDetails;
+import org.apache.fineract.portfolio.workingcapitalloan.service.WorkingCapitalLoanNearBreachEvaluationService;
+import org.apache.fineract.portfolio.workingcapitalloanproduct.domain.WorkingCapitalLoanProductRelatedDetails;
+import org.springframework.stereotype.Component;
+
+@Slf4j
+@RequiredArgsConstructor
+@Component
+public class NearBreachEvaluationBusinessStep extends WorkingCapitalLoanCOBBusinessStep {
+
+    private final WorkingCapitalLoanNearBreachEvaluationService nearBreachEvaluationService;
+
+    @Override
+    public WorkingCapitalLoan execute(final WorkingCapitalLoan input) {
+        final boolean isDisbursed = input.getDisbursementDetails().stream()
+                .map(WorkingCapitalLoanDisbursementDetails::getActualDisbursementDate).anyMatch(Objects::nonNull);
+        if (!isDisbursed) {
+            log.debug("Skipping near breach evaluation for WC loan {} - not yet disbursed", input.getId());
+            return input;
+        }

Review Comment:
   Fixed



##########
fineract-working-capital-loan/src/main/java/org/apache/fineract/cob/workingcapitalloan/businessstep/NearBreachEvaluationBusinessStep.java:
##########
@@ -0,0 +1,68 @@
+/**
+ * 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.cob.workingcapitalloan.businessstep;
+
+import java.time.LocalDate;
+import java.util.Objects;
+import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.fineract.infrastructure.core.service.DateUtils;
+import org.apache.fineract.portfolio.workingcapitalloan.domain.WorkingCapitalLoan;
+import org.apache.fineract.portfolio.workingcapitalloan.domain.WorkingCapitalLoanDisbursementDetails;
+import org.apache.fineract.portfolio.workingcapitalloan.service.WorkingCapitalLoanNearBreachEvaluationService;
+import org.apache.fineract.portfolio.workingcapitalloanproduct.domain.WorkingCapitalLoanProductRelatedDetails;
+import org.springframework.stereotype.Component;
+
+@Slf4j
+@RequiredArgsConstructor
+@Component
+public class NearBreachEvaluationBusinessStep extends WorkingCapitalLoanCOBBusinessStep {
+
+    private final WorkingCapitalLoanNearBreachEvaluationService nearBreachEvaluationService;
+
+    @Override
+    public WorkingCapitalLoan execute(final WorkingCapitalLoan input) {

Review Comment:
   Fixed



-- 
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: issues-unsubscribe@fineract.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] FINERACT-2455: Add WC near-breach evaluation business step [fineract]

Posted by "adamsaghy (via GitHub)" <gi...@apache.org>.
adamsaghy commented on code in PR #5849:
URL: https://github.com/apache/fineract/pull/5849#discussion_r3265799331


##########
fineract-working-capital-loan/src/main/java/org/apache/fineract/portfolio/workingcapitalloan/service/WorkingCapitalLoanNearBreachEvaluationServiceImpl.java:
##########
@@ -0,0 +1,126 @@
+/**
+ * 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.workingcapitalloan.service;
+
+import java.math.BigDecimal;
+import java.time.LocalDate;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Set;
+import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.fineract.organisation.monetary.domain.MoneyHelper;
+import org.apache.fineract.portfolio.loanaccount.domain.LoanTransactionType;
+import org.apache.fineract.portfolio.workingcapitalloan.domain.WorkingCapitalLoan;
+import org.apache.fineract.portfolio.workingcapitalloan.domain.WorkingCapitalLoanBreachSchedule;
+import org.apache.fineract.portfolio.workingcapitalloan.domain.WorkingCapitalLoanPeriodFrequencyType;
+import org.apache.fineract.portfolio.workingcapitalloan.repository.WorkingCapitalLoanBreachScheduleRepository;
+import org.apache.fineract.portfolio.workingcapitalloan.repository.WorkingCapitalLoanTransactionRepository;
+import org.apache.fineract.portfolio.workingcapitalloannearbreach.domain.WorkingCapitalNearBreach;
+import org.springframework.stereotype.Service;
+
+@RequiredArgsConstructor
+@Slf4j
+@Service
+public class WorkingCapitalLoanNearBreachEvaluationServiceImpl implements WorkingCapitalLoanNearBreachEvaluationService {
+
+    private static final Set<LoanTransactionType> REDUCING_TRANSACTION_TYPES = Set.of(LoanTransactionType.REPAYMENT,
+            LoanTransactionType.GOODWILL_CREDIT);
+
+    private final WorkingCapitalLoanBreachScheduleRepository breachScheduleRepository;
+    private final WorkingCapitalLoanTransactionRepository transactionRepository;
+
+    @Override
+    public void evaluateNearBreach(final WorkingCapitalLoan loan, final LocalDate effectiveDate) {
+        final WorkingCapitalNearBreach config = loan.getLoanProductRelatedDetails().getNearBreach();
+        final List<WorkingCapitalLoanBreachSchedule> periods = breachScheduleRepository.findByLoanIdOrderByPeriodNumberAsc(loan.getId());
+        final List<WorkingCapitalLoanBreachSchedule> updatedPeriods = new ArrayList<>();
+        for (final WorkingCapitalLoanBreachSchedule period : periods) {
+            if (period.getNearBreach() != null) {
+                continue;
+            }
+            if (evaluatePeriod(loan.getId(), period, config, effectiveDate)) {
+                updatedPeriods.add(period);
+            }
+        }
+        if (!updatedPeriods.isEmpty()) {
+            breachScheduleRepository.saveAllAndFlush(updatedPeriods);
+        }
+    }
+
+    private boolean evaluatePeriod(final Long loanId, final WorkingCapitalLoanBreachSchedule period, final WorkingCapitalNearBreach config,
+            final LocalDate effectiveDate) {
+        if (period.getMinPaymentAmount().compareTo(BigDecimal.ZERO) == 0) {
+            return false;
+        }
+        final List<LocalDate> evalDates = listEvalDates(period.getFromDate(), period.getToDate(), config.getFrequency(),
+                config.getFrequencyType());
+        if (evalDates.isEmpty()) {
+            return false;
+        }
+        final BigDecimal thresholdFraction = config.getThreshold().divide(BigDecimal.valueOf(100), MoneyHelper.getMathContext());
+        boolean anyPassed = false;
+        for (int index = 0; index < evalDates.size(); index++) {
+            final LocalDate evalDate = evalDates.get(index);
+            if (!effectiveDate.isAfter(evalDate)) {
+                break;
+            }
+            anyPassed = true;
+            final BigDecimal requiredCumulative = thresholdFraction.multiply(BigDecimal.valueOf(index + 1L), MoneyHelper.getMathContext())
+                    .multiply(period.getMinPaymentAmount(), MoneyHelper.getMathContext());

Review Comment:
   We can extract this calculation into a method for better readability + please use Money object for appropriate rounding and number of digist after decimal point.



-- 
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: issues-unsubscribe@fineract.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] FINERACT-2455: Add WC near-breach evaluation business step [fineract]

Posted by "oleksii-novikov-onix (via GitHub)" <gi...@apache.org>.
oleksii-novikov-onix commented on code in PR #5849:
URL: https://github.com/apache/fineract/pull/5849#discussion_r3267329976


##########
fineract-e2e-tests-runner/src/test/resources/features/WorkingCapitalNearBreachEvaluation.feature:
##########
@@ -295,11 +269,14 @@ Feature: Working Capital Near Breach Evaluation
     And Admin successfully approves the working capital loan on "01 January 2026" with "9000" amount and expected disbursement date on "01 January 2026"
     When Admin successfully disburse the Working Capital loan on "01 January 2026" with "9000" EUR transaction amount
     And Admin runs inline COB job for Working Capital Loan by loanId
+    # P1: 01-01 -> 02-28 (2 months). freq=58d -> candidate eval at 02-28 == toDate (breach due date).
+    # Per spec: no near-breach evaluation on breach due date -> eval excluded. Period has zero applicable eval points.
+    # No eval ran -> nearBreach stays null. Breach evaluation at period end: outstanding=500>0 -> breach=true.
     When Admin sets the business date to "01 March 2026"
     And Admin runs inline COB job for Working Capital Loan by loanId
     Then Working Capital loan breach schedule has the following data:
       | periodNumber | fromDate   | toDate     | minPaymentAmount | outstandingAmount | nearBreach | breach |
-      | 1            | 2026-01-01 | 2026-02-28 | 500.00           | 500.00            | true       | true   |
+      | 1            | 2026-01-01 | 2026-02-28 | 500.00           | 500.00            | null       | true   |

Review Comment:
   Fixed the implementation as well



-- 
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: issues-unsubscribe@fineract.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] FINERACT-2455: Add WC near-breach evaluation business step [fineract]

Posted by "adamsaghy (via GitHub)" <gi...@apache.org>.
adamsaghy commented on code in PR #5849:
URL: https://github.com/apache/fineract/pull/5849#discussion_r3265799331


##########
fineract-working-capital-loan/src/main/java/org/apache/fineract/portfolio/workingcapitalloan/service/WorkingCapitalLoanNearBreachEvaluationServiceImpl.java:
##########
@@ -0,0 +1,126 @@
+/**
+ * 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.workingcapitalloan.service;
+
+import java.math.BigDecimal;
+import java.time.LocalDate;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Set;
+import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.fineract.organisation.monetary.domain.MoneyHelper;
+import org.apache.fineract.portfolio.loanaccount.domain.LoanTransactionType;
+import org.apache.fineract.portfolio.workingcapitalloan.domain.WorkingCapitalLoan;
+import org.apache.fineract.portfolio.workingcapitalloan.domain.WorkingCapitalLoanBreachSchedule;
+import org.apache.fineract.portfolio.workingcapitalloan.domain.WorkingCapitalLoanPeriodFrequencyType;
+import org.apache.fineract.portfolio.workingcapitalloan.repository.WorkingCapitalLoanBreachScheduleRepository;
+import org.apache.fineract.portfolio.workingcapitalloan.repository.WorkingCapitalLoanTransactionRepository;
+import org.apache.fineract.portfolio.workingcapitalloannearbreach.domain.WorkingCapitalNearBreach;
+import org.springframework.stereotype.Service;
+
+@RequiredArgsConstructor
+@Slf4j
+@Service
+public class WorkingCapitalLoanNearBreachEvaluationServiceImpl implements WorkingCapitalLoanNearBreachEvaluationService {
+
+    private static final Set<LoanTransactionType> REDUCING_TRANSACTION_TYPES = Set.of(LoanTransactionType.REPAYMENT,
+            LoanTransactionType.GOODWILL_CREDIT);
+
+    private final WorkingCapitalLoanBreachScheduleRepository breachScheduleRepository;
+    private final WorkingCapitalLoanTransactionRepository transactionRepository;
+
+    @Override
+    public void evaluateNearBreach(final WorkingCapitalLoan loan, final LocalDate effectiveDate) {
+        final WorkingCapitalNearBreach config = loan.getLoanProductRelatedDetails().getNearBreach();
+        final List<WorkingCapitalLoanBreachSchedule> periods = breachScheduleRepository.findByLoanIdOrderByPeriodNumberAsc(loan.getId());
+        final List<WorkingCapitalLoanBreachSchedule> updatedPeriods = new ArrayList<>();
+        for (final WorkingCapitalLoanBreachSchedule period : periods) {
+            if (period.getNearBreach() != null) {
+                continue;
+            }
+            if (evaluatePeriod(loan.getId(), period, config, effectiveDate)) {
+                updatedPeriods.add(period);
+            }
+        }
+        if (!updatedPeriods.isEmpty()) {
+            breachScheduleRepository.saveAllAndFlush(updatedPeriods);
+        }
+    }
+
+    private boolean evaluatePeriod(final Long loanId, final WorkingCapitalLoanBreachSchedule period, final WorkingCapitalNearBreach config,
+            final LocalDate effectiveDate) {
+        if (period.getMinPaymentAmount().compareTo(BigDecimal.ZERO) == 0) {
+            return false;
+        }
+        final List<LocalDate> evalDates = listEvalDates(period.getFromDate(), period.getToDate(), config.getFrequency(),
+                config.getFrequencyType());
+        if (evalDates.isEmpty()) {
+            return false;
+        }
+        final BigDecimal thresholdFraction = config.getThreshold().divide(BigDecimal.valueOf(100), MoneyHelper.getMathContext());
+        boolean anyPassed = false;
+        for (int index = 0; index < evalDates.size(); index++) {
+            final LocalDate evalDate = evalDates.get(index);
+            if (!effectiveDate.isAfter(evalDate)) {
+                break;
+            }
+            anyPassed = true;
+            final BigDecimal requiredCumulative = thresholdFraction.multiply(BigDecimal.valueOf(index + 1L), MoneyHelper.getMathContext())
+                    .multiply(period.getMinPaymentAmount(), MoneyHelper.getMathContext());

Review Comment:
   We can extract this calculation into a method for better readability



-- 
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: issues-unsubscribe@fineract.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] FINERACT-2455: Add WC near-breach evaluation business step [fineract]

Posted by "adamsaghy (via GitHub)" <gi...@apache.org>.
adamsaghy merged PR #5849:
URL: https://github.com/apache/fineract/pull/5849


-- 
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: issues-unsubscribe@fineract.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] FINERACT-2455: Add WC near-breach evaluation business step [fineract]

Posted by "adamsaghy (via GitHub)" <gi...@apache.org>.
adamsaghy commented on code in PR #5849:
URL: https://github.com/apache/fineract/pull/5849#discussion_r3265895929


##########
fineract-working-capital-loan/src/main/java/org/apache/fineract/cob/workingcapitalloan/businessstep/NearBreachEvaluationBusinessStep.java:
##########
@@ -0,0 +1,68 @@
+/**
+ * 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.cob.workingcapitalloan.businessstep;
+
+import java.time.LocalDate;
+import java.util.Objects;
+import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.fineract.infrastructure.core.service.DateUtils;
+import org.apache.fineract.portfolio.workingcapitalloan.domain.WorkingCapitalLoan;
+import org.apache.fineract.portfolio.workingcapitalloan.domain.WorkingCapitalLoanDisbursementDetails;
+import org.apache.fineract.portfolio.workingcapitalloan.service.WorkingCapitalLoanNearBreachEvaluationService;
+import org.apache.fineract.portfolio.workingcapitalloanproduct.domain.WorkingCapitalLoanProductRelatedDetails;
+import org.springframework.stereotype.Component;
+
+@Slf4j
+@RequiredArgsConstructor
+@Component
+public class NearBreachEvaluationBusinessStep extends WorkingCapitalLoanCOBBusinessStep {
+
+    private final WorkingCapitalLoanNearBreachEvaluationService nearBreachEvaluationService;
+
+    @Override
+    public WorkingCapitalLoan execute(final WorkingCapitalLoan input) {
+        final boolean isDisbursed = input.getDisbursementDetails().stream()
+                .map(WorkingCapitalLoanDisbursementDetails::getActualDisbursementDate).anyMatch(Objects::nonNull);
+        if (!isDisbursed) {
+            log.debug("Skipping near breach evaluation for WC loan {} - not yet disbursed", input.getId());
+            return input;
+        }
+
+        final WorkingCapitalLoanProductRelatedDetails details = input.getLoanProductRelatedDetails();
+        if (details == null || details.getNearBreach() == null) {
+            log.debug("Skipping near breach evaluation for WC loan {} - no near breach configuration", input.getId());
+            return input;
+        }
+
+        final LocalDate businessDate = DateUtils.getBusinessLocalDate();
+        nearBreachEvaluationService.evaluateNearBreach(input, businessDate.plusDays(1L));

Review Comment:
   why to increase the date by 1? COB date is the date on we evaluate the near-breach, am i missing something?



-- 
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: issues-unsubscribe@fineract.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] FINERACT-2455: Add WC near-breach evaluation business step [fineract]

Posted by "oleksii-novikov-onix (via GitHub)" <gi...@apache.org>.
oleksii-novikov-onix commented on code in PR #5849:
URL: https://github.com/apache/fineract/pull/5849#discussion_r3268576752


##########
fineract-working-capital-loan/src/main/java/org/apache/fineract/portfolio/workingcapitalloan/service/WorkingCapitalLoanNearBreachEvaluationServiceImpl.java:
##########
@@ -0,0 +1,126 @@
+/**
+ * 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.workingcapitalloan.service;
+
+import java.math.BigDecimal;
+import java.time.LocalDate;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Set;
+import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.fineract.organisation.monetary.domain.MoneyHelper;
+import org.apache.fineract.portfolio.loanaccount.domain.LoanTransactionType;
+import org.apache.fineract.portfolio.workingcapitalloan.domain.WorkingCapitalLoan;
+import org.apache.fineract.portfolio.workingcapitalloan.domain.WorkingCapitalLoanBreachSchedule;
+import org.apache.fineract.portfolio.workingcapitalloan.domain.WorkingCapitalLoanPeriodFrequencyType;
+import org.apache.fineract.portfolio.workingcapitalloan.repository.WorkingCapitalLoanBreachScheduleRepository;
+import org.apache.fineract.portfolio.workingcapitalloan.repository.WorkingCapitalLoanTransactionRepository;
+import org.apache.fineract.portfolio.workingcapitalloannearbreach.domain.WorkingCapitalNearBreach;
+import org.springframework.stereotype.Service;
+
+@RequiredArgsConstructor
+@Slf4j
+@Service
+public class WorkingCapitalLoanNearBreachEvaluationServiceImpl implements WorkingCapitalLoanNearBreachEvaluationService {
+
+    private static final Set<LoanTransactionType> REDUCING_TRANSACTION_TYPES = Set.of(LoanTransactionType.REPAYMENT,
+            LoanTransactionType.GOODWILL_CREDIT);
+
+    private final WorkingCapitalLoanBreachScheduleRepository breachScheduleRepository;
+    private final WorkingCapitalLoanTransactionRepository transactionRepository;
+
+    @Override
+    public void evaluateNearBreach(final WorkingCapitalLoan loan, final LocalDate effectiveDate) {
+        final WorkingCapitalNearBreach config = loan.getLoanProductRelatedDetails().getNearBreach();
+        final List<WorkingCapitalLoanBreachSchedule> periods = breachScheduleRepository.findByLoanIdOrderByPeriodNumberAsc(loan.getId());

Review Comment:
   Fixed



-- 
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: issues-unsubscribe@fineract.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] FINERACT-2455: Add WC near-breach evaluation business step [fineract]

Posted by "oleksii-novikov-onix (via GitHub)" <gi...@apache.org>.
oleksii-novikov-onix commented on code in PR #5849:
URL: https://github.com/apache/fineract/pull/5849#discussion_r3271870791


##########
fineract-working-capital-loan/src/main/java/org/apache/fineract/cob/workingcapitalloan/businessstep/NearBreachEvaluationBusinessStep.java:
##########
@@ -0,0 +1,68 @@
+/**
+ * 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.cob.workingcapitalloan.businessstep;
+
+import java.time.LocalDate;
+import java.util.Objects;
+import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.fineract.infrastructure.core.service.DateUtils;
+import org.apache.fineract.portfolio.workingcapitalloan.domain.WorkingCapitalLoan;
+import org.apache.fineract.portfolio.workingcapitalloan.domain.WorkingCapitalLoanDisbursementDetails;
+import org.apache.fineract.portfolio.workingcapitalloan.service.WorkingCapitalLoanNearBreachEvaluationService;
+import org.apache.fineract.portfolio.workingcapitalloanproduct.domain.WorkingCapitalLoanProductRelatedDetails;
+import org.springframework.stereotype.Component;
+
+@Slf4j
+@RequiredArgsConstructor
+@Component
+public class NearBreachEvaluationBusinessStep extends WorkingCapitalLoanCOBBusinessStep {
+
+    private final WorkingCapitalLoanNearBreachEvaluationService nearBreachEvaluationService;
+
+    @Override
+    public WorkingCapitalLoan execute(final WorkingCapitalLoan input) {
+        final boolean isDisbursed = input.getDisbursementDetails().stream()
+                .map(WorkingCapitalLoanDisbursementDetails::getActualDisbursementDate).anyMatch(Objects::nonNull);
+        if (!isDisbursed) {
+            log.debug("Skipping near breach evaluation for WC loan {} - not yet disbursed", input.getId());
+            return input;
+        }
+
+        final WorkingCapitalLoanProductRelatedDetails details = input.getLoanProductRelatedDetails();
+        if (details == null || details.getNearBreach() == null) {
+            log.debug("Skipping near breach evaluation for WC loan {} - no near breach configuration", input.getId());
+            return input;
+        }
+
+        final LocalDate businessDate = DateUtils.getBusinessLocalDate();
+        nearBreachEvaluationService.evaluateNearBreach(input, businessDate.plusDays(1L));

Review Comment:
   Reworked this, and also fixed the same issue for breach evaluation.



##########
fineract-working-capital-loan/src/main/java/org/apache/fineract/portfolio/workingcapitalloan/repository/WorkingCapitalLoanTransactionRepository.java:
##########
@@ -37,4 +43,15 @@ public interface WorkingCapitalLoanTransactionRepository extends JpaRepository<W
     Optional<WorkingCapitalLoanTransaction> findByWcLoan_IdAndExternalId(Long wcLoanId, ExternalId externalId);
 
     boolean existsByExternalId(ExternalId externalId);
+
+    @Query("""
+            SELECT COALESCE(SUM(t.transactionAmount), 0)
+            FROM WorkingCapitalLoanTransaction t
+            WHERE t.wcLoan.id = :loanId
+            AND t.transactionDate BETWEEN :fromDate AND :toDate
+            AND t.reversed = false
+            AND t.transactionType IN :reducingTypes
+            """)
+    BigDecimal sumPaymentsInWindow(@Param("loanId") Long loanId, @Param("fromDate") LocalDate fromDate, @Param("toDate") LocalDate toDate,

Review Comment:
   Fixed



-- 
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: issues-unsubscribe@fineract.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] FINERACT-2455: Add WC near-breach evaluation business step [fineract]

Posted by "adamsaghy (via GitHub)" <gi...@apache.org>.
adamsaghy commented on code in PR #5849:
URL: https://github.com/apache/fineract/pull/5849#discussion_r3265902813


##########
fineract-working-capital-loan/src/main/java/org/apache/fineract/portfolio/workingcapitalloan/repository/WorkingCapitalLoanTransactionRepository.java:
##########
@@ -37,4 +43,15 @@ public interface WorkingCapitalLoanTransactionRepository extends JpaRepository<W
     Optional<WorkingCapitalLoanTransaction> findByWcLoan_IdAndExternalId(Long wcLoanId, ExternalId externalId);
 
     boolean existsByExternalId(ExternalId externalId);
+
+    @Query("""
+            SELECT COALESCE(SUM(t.transactionAmount), 0)
+            FROM WorkingCapitalLoanTransaction t
+            WHERE t.wcLoan.id = :loanId
+            AND t.transactionDate BETWEEN :fromDate AND :toDate
+            AND t.reversed = false
+            AND t.transactionType IN :reducingTypes
+            """)
+    BigDecimal sumPaymentsInWindow(@Param("loanId") Long loanId, @Param("fromDate") LocalDate fromDate, @Param("toDate") LocalDate toDate,

Review Comment:
   We dont need this. Paid amount is available on the breach schedule already.



-- 
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: issues-unsubscribe@fineract.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] FINERACT-2455: Add WC near-breach evaluation business step [fineract]

Posted by "oleksii-novikov-onix (via GitHub)" <gi...@apache.org>.
oleksii-novikov-onix commented on code in PR #5849:
URL: https://github.com/apache/fineract/pull/5849#discussion_r3271872464


##########
fineract-working-capital-loan/src/main/java/org/apache/fineract/portfolio/workingcapitalloan/service/WorkingCapitalLoanBreachScheduleServiceImpl.java:
##########
@@ -148,25 +147,13 @@ private void applyRepayment(final WorkingCapitalLoanBreachSchedule period, BigDe
     }
 
     @Override
-    public void evaluateBreachAndNearBreach(final WorkingCapitalLoan loan, final LocalDate businessDate) {
+    public void evaluateBreach(final WorkingCapitalLoan loan, final LocalDate businessDate) {
         final List<WorkingCapitalLoanBreachSchedule> allPeriods = repository.findByLoanIdOrderByPeriodNumberAsc(loan.getId());

Review Comment:
   Fixed



-- 
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: issues-unsubscribe@fineract.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] FINERACT-2455: Add WC near-breach evaluation business step [fineract]

Posted by "oleksii-novikov-onix (via GitHub)" <gi...@apache.org>.
oleksii-novikov-onix commented on code in PR #5849:
URL: https://github.com/apache/fineract/pull/5849#discussion_r3271857287


##########
fineract-working-capital-loan/src/main/java/org/apache/fineract/portfolio/workingcapitalloan/service/WorkingCapitalLoanNearBreachEvaluationServiceImpl.java:
##########
@@ -0,0 +1,126 @@
+/**
+ * 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.workingcapitalloan.service;
+
+import java.math.BigDecimal;
+import java.time.LocalDate;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Set;
+import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.fineract.organisation.monetary.domain.MoneyHelper;
+import org.apache.fineract.portfolio.loanaccount.domain.LoanTransactionType;
+import org.apache.fineract.portfolio.workingcapitalloan.domain.WorkingCapitalLoan;
+import org.apache.fineract.portfolio.workingcapitalloan.domain.WorkingCapitalLoanBreachSchedule;
+import org.apache.fineract.portfolio.workingcapitalloan.domain.WorkingCapitalLoanPeriodFrequencyType;
+import org.apache.fineract.portfolio.workingcapitalloan.repository.WorkingCapitalLoanBreachScheduleRepository;
+import org.apache.fineract.portfolio.workingcapitalloan.repository.WorkingCapitalLoanTransactionRepository;
+import org.apache.fineract.portfolio.workingcapitalloannearbreach.domain.WorkingCapitalNearBreach;
+import org.springframework.stereotype.Service;
+
+@RequiredArgsConstructor
+@Slf4j
+@Service
+public class WorkingCapitalLoanNearBreachEvaluationServiceImpl implements WorkingCapitalLoanNearBreachEvaluationService {
+
+    private static final Set<LoanTransactionType> REDUCING_TRANSACTION_TYPES = Set.of(LoanTransactionType.REPAYMENT,
+            LoanTransactionType.GOODWILL_CREDIT);
+
+    private final WorkingCapitalLoanBreachScheduleRepository breachScheduleRepository;
+    private final WorkingCapitalLoanTransactionRepository transactionRepository;
+
+    @Override
+    public void evaluateNearBreach(final WorkingCapitalLoan loan, final LocalDate effectiveDate) {
+        final WorkingCapitalNearBreach config = loan.getLoanProductRelatedDetails().getNearBreach();
+        final List<WorkingCapitalLoanBreachSchedule> periods = breachScheduleRepository.findByLoanIdOrderByPeriodNumberAsc(loan.getId());
+        final List<WorkingCapitalLoanBreachSchedule> updatedPeriods = new ArrayList<>();
+        for (final WorkingCapitalLoanBreachSchedule period : periods) {
+            if (period.getNearBreach() != null) {
+                continue;
+            }
+            if (evaluatePeriod(loan.getId(), period, config, effectiveDate)) {
+                updatedPeriods.add(period);
+            }
+        }
+        if (!updatedPeriods.isEmpty()) {
+            breachScheduleRepository.saveAllAndFlush(updatedPeriods);
+        }
+    }
+
+    private boolean evaluatePeriod(final Long loanId, final WorkingCapitalLoanBreachSchedule period, final WorkingCapitalNearBreach config,
+            final LocalDate effectiveDate) {
+        if (period.getMinPaymentAmount().compareTo(BigDecimal.ZERO) == 0) {
+            return false;
+        }
+        final List<LocalDate> evalDates = listEvalDates(period.getFromDate(), period.getToDate(), config.getFrequency(),
+                config.getFrequencyType());
+        if (evalDates.isEmpty()) {
+            return false;
+        }
+        final BigDecimal thresholdFraction = config.getThreshold().divide(BigDecimal.valueOf(100), MoneyHelper.getMathContext());
+        boolean anyPassed = false;
+        for (int index = 0; index < evalDates.size(); index++) {
+            final LocalDate evalDate = evalDates.get(index);
+            if (!effectiveDate.isAfter(evalDate)) {
+                break;
+            }
+            anyPassed = true;
+            final BigDecimal requiredCumulative = thresholdFraction.multiply(BigDecimal.valueOf(index + 1L), MoneyHelper.getMathContext())
+                    .multiply(period.getMinPaymentAmount(), MoneyHelper.getMathContext());

Review Comment:
   Fixed



##########
fineract-working-capital-loan/src/main/java/org/apache/fineract/portfolio/workingcapitalloan/service/WorkingCapitalLoanNearBreachEvaluationServiceImpl.java:
##########
@@ -0,0 +1,126 @@
+/**
+ * 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.workingcapitalloan.service;
+
+import java.math.BigDecimal;
+import java.time.LocalDate;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Set;
+import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.fineract.organisation.monetary.domain.MoneyHelper;
+import org.apache.fineract.portfolio.loanaccount.domain.LoanTransactionType;
+import org.apache.fineract.portfolio.workingcapitalloan.domain.WorkingCapitalLoan;
+import org.apache.fineract.portfolio.workingcapitalloan.domain.WorkingCapitalLoanBreachSchedule;
+import org.apache.fineract.portfolio.workingcapitalloan.domain.WorkingCapitalLoanPeriodFrequencyType;
+import org.apache.fineract.portfolio.workingcapitalloan.repository.WorkingCapitalLoanBreachScheduleRepository;
+import org.apache.fineract.portfolio.workingcapitalloan.repository.WorkingCapitalLoanTransactionRepository;
+import org.apache.fineract.portfolio.workingcapitalloannearbreach.domain.WorkingCapitalNearBreach;
+import org.springframework.stereotype.Service;
+
+@RequiredArgsConstructor
+@Slf4j
+@Service
+public class WorkingCapitalLoanNearBreachEvaluationServiceImpl implements WorkingCapitalLoanNearBreachEvaluationService {
+
+    private static final Set<LoanTransactionType> REDUCING_TRANSACTION_TYPES = Set.of(LoanTransactionType.REPAYMENT,
+            LoanTransactionType.GOODWILL_CREDIT);
+
+    private final WorkingCapitalLoanBreachScheduleRepository breachScheduleRepository;
+    private final WorkingCapitalLoanTransactionRepository transactionRepository;
+
+    @Override
+    public void evaluateNearBreach(final WorkingCapitalLoan loan, final LocalDate effectiveDate) {
+        final WorkingCapitalNearBreach config = loan.getLoanProductRelatedDetails().getNearBreach();
+        final List<WorkingCapitalLoanBreachSchedule> periods = breachScheduleRepository.findByLoanIdOrderByPeriodNumberAsc(loan.getId());
+        final List<WorkingCapitalLoanBreachSchedule> updatedPeriods = new ArrayList<>();
+        for (final WorkingCapitalLoanBreachSchedule period : periods) {
+            if (period.getNearBreach() != null) {
+                continue;
+            }
+            if (evaluatePeriod(loan.getId(), period, config, effectiveDate)) {
+                updatedPeriods.add(period);
+            }
+        }
+        if (!updatedPeriods.isEmpty()) {
+            breachScheduleRepository.saveAllAndFlush(updatedPeriods);
+        }
+    }
+
+    private boolean evaluatePeriod(final Long loanId, final WorkingCapitalLoanBreachSchedule period, final WorkingCapitalNearBreach config,
+            final LocalDate effectiveDate) {
+        if (period.getMinPaymentAmount().compareTo(BigDecimal.ZERO) == 0) {
+            return false;
+        }
+        final List<LocalDate> evalDates = listEvalDates(period.getFromDate(), period.getToDate(), config.getFrequency(),
+                config.getFrequencyType());
+        if (evalDates.isEmpty()) {
+            return false;
+        }
+        final BigDecimal thresholdFraction = config.getThreshold().divide(BigDecimal.valueOf(100), MoneyHelper.getMathContext());
+        boolean anyPassed = false;
+        for (int index = 0; index < evalDates.size(); index++) {
+            final LocalDate evalDate = evalDates.get(index);
+            if (!effectiveDate.isAfter(evalDate)) {
+                break;
+            }
+            anyPassed = true;
+            final BigDecimal requiredCumulative = thresholdFraction.multiply(BigDecimal.valueOf(index + 1L), MoneyHelper.getMathContext())
+                    .multiply(period.getMinPaymentAmount(), MoneyHelper.getMathContext());
+            final BigDecimal paidCumulative = transactionRepository.sumPaymentsInWindow(loanId, period.getFromDate(), evalDate,

Review Comment:
   Fixed



-- 
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: issues-unsubscribe@fineract.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] FINERACT-2455: Add WC near-breach evaluation business step [fineract]

Posted by "oleksii-novikov-onix (via GitHub)" <gi...@apache.org>.
oleksii-novikov-onix commented on PR #5849:
URL: https://github.com/apache/fineract/pull/5849#issuecomment-4474854939

   @galovics Thanks, I removed CREDIT_BALANCE_REFUND 


-- 
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: issues-unsubscribe@fineract.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org