From 4d4502fbe0e8aeebd326fab69993bc788d4c04e7 Mon Sep 17 00:00:00 2001 From: Arnold Galovics Date: Wed, 14 Feb 2024 21:41:43 +0100 Subject: [PATCH] FINERACT-1971: Loan re-aging foundational implementation --- config/spotbugs/exclude.xml | 3 + docker-compose-web-app.yml | 27 +++ .../service/CommandWrapperBuilder.java | 16 ++ .../infrastructure/core/api/JsonCommand.java | 13 ++ .../BusinessEventNotifierServiceImpl.java | 27 ++- .../api/LoanReAgingApiConstants.java | 30 +++ .../data/LoanTransactionEnumData.java | 3 + .../portfolio/loanaccount/domain/Loan.java | 7 + .../loanaccount/domain/LoanTransaction.java | 6 +- .../domain/LoanTransactionType.java | 8 +- .../domain/reaging/LoanReAgeParameter.java | 54 +++++ .../LoanReAgingParameterRepository.java | 28 +++ .../loanproduct/service/LoanEnumerations.java | 2 + .../LoanReAgeTransactionBusinessEvent.java | 36 ++++ ...LoanUndoReAgeTransactionBusinessEvent.java | 36 ++++ .../api/LoanTransactionsApiResource.java | 6 + .../LoanTransactionsApiResourceSwagger.java | 9 + .../reaging/LoanReAgingCommandHandler.java | 50 +++++ .../LoanUndoReAgingCommandHandler.java | 50 +++++ ...ctionDelinquencyRecalculationListener.java | 60 ++++++ .../reaging/LoanReAgingServiceImpl.java | 155 +++++++++++++++ .../service/reaging/LoanReAgingValidator.java | 35 ++++ .../db/changelog/tenant/changelog-tenant.xml | 2 + ...35_add_external_event_for_loan_reaging.xml | 37 ++++ .../parts/0136_loan_reaging_parameters.xml | 74 +++++++ ...entConfigurationValidationServiceTest.java | 6 +- .../BaseLoanIntegrationTest.java | 40 +++- .../ExternalEventConfigurationHelper.java | 10 + .../common/loans/LoanTransactionHelper.java | 8 + .../reaging/LoanReAgingIntegrationTest.java | 184 ++++++++++++++++++ 30 files changed, 1003 insertions(+), 19 deletions(-) create mode 100644 docker-compose-web-app.yml create mode 100644 fineract-loan/src/main/java/org/apache/fineract/portfolio/loanaccount/api/LoanReAgingApiConstants.java create mode 100644 fineract-loan/src/main/java/org/apache/fineract/portfolio/loanaccount/domain/reaging/LoanReAgeParameter.java create mode 100644 fineract-loan/src/main/java/org/apache/fineract/portfolio/loanaccount/domain/reaging/LoanReAgingParameterRepository.java create mode 100644 fineract-provider/src/main/java/org/apache/fineract/infrastructure/event/business/domain/loan/transaction/reaging/LoanReAgeTransactionBusinessEvent.java create mode 100644 fineract-provider/src/main/java/org/apache/fineract/infrastructure/event/business/domain/loan/transaction/reaging/LoanUndoReAgeTransactionBusinessEvent.java create mode 100644 fineract-provider/src/main/java/org/apache/fineract/portfolio/loanaccount/handler/loan/reaging/LoanReAgingCommandHandler.java create mode 100644 fineract-provider/src/main/java/org/apache/fineract/portfolio/loanaccount/handler/loan/reaging/LoanUndoReAgingCommandHandler.java create mode 100644 fineract-provider/src/main/java/org/apache/fineract/portfolio/loanaccount/service/listener/LoanTransactionDelinquencyRecalculationListener.java create mode 100644 fineract-provider/src/main/java/org/apache/fineract/portfolio/loanaccount/service/reaging/LoanReAgingServiceImpl.java create mode 100644 fineract-provider/src/main/java/org/apache/fineract/portfolio/loanaccount/service/reaging/LoanReAgingValidator.java create mode 100644 fineract-provider/src/main/resources/db/changelog/tenant/parts/0135_add_external_event_for_loan_reaging.xml create mode 100644 fineract-provider/src/main/resources/db/changelog/tenant/parts/0136_loan_reaging_parameters.xml create mode 100644 integration-tests/src/test/java/org/apache/fineract/integrationtests/loan/reaging/LoanReAgingIntegrationTest.java diff --git a/config/spotbugs/exclude.xml b/config/spotbugs/exclude.xml index 251015bb6..44160dfd6 100644 --- a/config/spotbugs/exclude.xml +++ b/config/spotbugs/exclude.xml @@ -26,6 +26,9 @@ + + + diff --git a/docker-compose-web-app.yml b/docker-compose-web-app.yml new file mode 100644 index 000000000..7d1b768ef --- /dev/null +++ b/docker-compose-web-app.yml @@ -0,0 +1,27 @@ +# 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. +# + +version: "3.8" +services: + # Frontend service + community-app: + image: openmf/web-app:latest + container_name: mifos-web-app + restart: always + ports: + - 4200:80 diff --git a/fineract-core/src/main/java/org/apache/fineract/commands/service/CommandWrapperBuilder.java b/fineract-core/src/main/java/org/apache/fineract/commands/service/CommandWrapperBuilder.java index 88646406c..edbb8f892 100644 --- a/fineract-core/src/main/java/org/apache/fineract/commands/service/CommandWrapperBuilder.java +++ b/fineract-core/src/main/java/org/apache/fineract/commands/service/CommandWrapperBuilder.java @@ -3656,6 +3656,22 @@ public class CommandWrapperBuilder { return this; } + public CommandWrapperBuilder reAge(final Long loanId) { + this.actionName = "REAGE"; + this.entityName = "LOAN"; + this.loanId = loanId; + this.href = "/loans/" + loanId + "/transactions?command=reAge"; + return this; + } + + public CommandWrapperBuilder undoReAge(final Long loanId) { + this.actionName = "UNDO_REAGE"; + this.entityName = "LOAN"; + this.loanId = loanId; + this.href = "/loans/" + loanId + "/transactions?command=undoReAge"; + return this; + } + public CommandWrapperBuilder createDelinquencyAction(final Long loanId) { this.actionName = "CREATE"; this.entityName = "DELINQUENCY_ACTION"; diff --git a/fineract-core/src/main/java/org/apache/fineract/infrastructure/core/api/JsonCommand.java b/fineract-core/src/main/java/org/apache/fineract/infrastructure/core/api/JsonCommand.java index 9d560ad83..d8211598e 100644 --- a/fineract-core/src/main/java/org/apache/fineract/infrastructure/core/api/JsonCommand.java +++ b/fineract-core/src/main/java/org/apache/fineract/infrastructure/core/api/JsonCommand.java @@ -389,6 +389,19 @@ public final class JsonCommand { return isChanged; } + public > T enumValueOfParameterNamed(String parameterName, Class enumType) { + try { + String value = stringValueOfParameterNamedAllowingNull(parameterName); + if (value != null) { + return Enum.valueOf(enumType, value); + } else { + return null; + } + } catch (IllegalArgumentException e) { + return null; + } + } + public String stringValueOfParameterNamed(final String parameterName) { final String value = this.fromApiJsonHelper.extractStringNamed(parameterName, this.parsedCommand); return StringUtils.defaultIfEmpty(value, ""); diff --git a/fineract-core/src/main/java/org/apache/fineract/infrastructure/event/business/service/BusinessEventNotifierServiceImpl.java b/fineract-core/src/main/java/org/apache/fineract/infrastructure/event/business/service/BusinessEventNotifierServiceImpl.java index ba1c85a29..9809f6342 100644 --- a/fineract-core/src/main/java/org/apache/fineract/infrastructure/event/business/service/BusinessEventNotifierServiceImpl.java +++ b/fineract-core/src/main/java/org/apache/fineract/infrastructure/event/business/service/BusinessEventNotifierServiceImpl.java @@ -62,11 +62,9 @@ public class BusinessEventNotifierServiceImpl implements BusinessEventNotifierSe @Override public void notifyPreBusinessEvent(BusinessEvent businessEvent) { throwExceptionIfBulkEvent(businessEvent); - List businessEventListeners = preListeners.get(businessEvent.getClass()); - if (businessEventListeners != null) { - for (BusinessEventListener eventListener : businessEventListeners) { - eventListener.onBusinessEvent(businessEvent); - } + List businessEventListeners = findSuitableListeners(preListeners, businessEvent.getClass()); + for (BusinessEventListener eventListener : businessEventListeners) { + eventListener.onBusinessEvent(businessEvent); } } @@ -84,11 +82,9 @@ public class BusinessEventNotifierServiceImpl implements BusinessEventNotifierSe public void notifyPostBusinessEvent(BusinessEvent businessEvent) { throwExceptionIfBulkEvent(businessEvent); boolean isExternalEvent = !(businessEvent instanceof NoExternalEvent); - List businessEventListeners = postListeners.get(businessEvent.getClass()); - if (businessEventListeners != null) { - for (BusinessEventListener eventListener : businessEventListeners) { - eventListener.onBusinessEvent(businessEvent); - } + List businessEventListeners = findSuitableListeners(postListeners, businessEvent.getClass()); + for (BusinessEventListener eventListener : businessEventListeners) { + eventListener.onBusinessEvent(businessEvent); } if (isExternalEvent && isExternalEventPostingEnabled()) { // we only want to create external events for operations that were successful, hence the post listener @@ -102,6 +98,17 @@ public class BusinessEventNotifierServiceImpl implements BusinessEventNotifierSe } } + private List findSuitableListeners(Map> listeners, Class eventClazz) { + List result = new ArrayList<>(); + for (Map.Entry> entry : listeners.entrySet()) { + Class registeredClazz = entry.getKey(); + if (registeredClazz.isAssignableFrom(eventClazz)) { + result.addAll(entry.getValue()); + } + } + return result; + } + @Override public > void addPostBusinessEventListener(Class eventType, BusinessEventListener listener) { List businessEventListeners = postListeners.get(eventType); diff --git a/fineract-loan/src/main/java/org/apache/fineract/portfolio/loanaccount/api/LoanReAgingApiConstants.java b/fineract-loan/src/main/java/org/apache/fineract/portfolio/loanaccount/api/LoanReAgingApiConstants.java new file mode 100644 index 000000000..96411c24b --- /dev/null +++ b/fineract-loan/src/main/java/org/apache/fineract/portfolio/loanaccount/api/LoanReAgingApiConstants.java @@ -0,0 +1,30 @@ +/** + * 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.loanaccount.api; + +public interface LoanReAgingApiConstants { + + String localeParameterName = "locale"; + String dateFormatParameterName = "dateFormat"; + String externalIdParameterName = "externalId"; + + String frequency = "frequency"; + String startDate = "startDate"; + String numberOfInstallments = "numberOfInstallments"; +} diff --git a/fineract-loan/src/main/java/org/apache/fineract/portfolio/loanaccount/data/LoanTransactionEnumData.java b/fineract-loan/src/main/java/org/apache/fineract/portfolio/loanaccount/data/LoanTransactionEnumData.java index 8d6db7a77..c3985e489 100644 --- a/fineract-loan/src/main/java/org/apache/fineract/portfolio/loanaccount/data/LoanTransactionEnumData.java +++ b/fineract-loan/src/main/java/org/apache/fineract/portfolio/loanaccount/data/LoanTransactionEnumData.java @@ -19,6 +19,7 @@ package org.apache.fineract.portfolio.loanaccount.data; import lombok.Getter; +import org.apache.fineract.portfolio.loanaccount.domain.LoanTransactionType; /** * Immutable data object represent loan status enumerations. @@ -55,6 +56,7 @@ public class LoanTransactionEnumData { private final boolean chargeback; private final boolean chargeoff; private final boolean downPayment; + private final boolean reAge; public LoanTransactionEnumData(final Long id, final String code, final String value) { this.id = id; @@ -85,6 +87,7 @@ public class LoanTransactionEnumData { this.chargeAdjustment = Long.valueOf(26).equals(this.id); this.chargeoff = Long.valueOf(27).equals(this.id); this.downPayment = Long.valueOf(28).equals(this.id); + this.reAge = Long.valueOf(LoanTransactionType.REAGE.getValue()).equals(this.id); } public boolean isRepaymentType() { diff --git a/fineract-loan/src/main/java/org/apache/fineract/portfolio/loanaccount/domain/Loan.java b/fineract-loan/src/main/java/org/apache/fineract/portfolio/loanaccount/domain/Loan.java index efa3a3ce6..5f7275fa6 100644 --- a/fineract-loan/src/main/java/org/apache/fineract/portfolio/loanaccount/domain/Loan.java +++ b/fineract-loan/src/main/java/org/apache/fineract/portfolio/loanaccount/domain/Loan.java @@ -4280,6 +4280,13 @@ public class Loan extends AbstractAuditableWithUTCDateTimeCustom { return cumulativePaid; } + public Money getTotalPrincipalOutstandingUntil(LocalDate date) { + return getRepaymentScheduleInstallments().stream() + .filter(installment -> installment.getDueDate().isBefore(date) || installment.getDueDate().isEqual(date)) + .map(installment -> installment.getPrincipalOutstanding(loanCurrency())).reduce(Money.zero(loanCurrency()), Money::add); + + } + private Money getTotalInterestOutstandingOnLoan() { Money cumulativeInterest = Money.zero(loanCurrency()); diff --git a/fineract-loan/src/main/java/org/apache/fineract/portfolio/loanaccount/domain/LoanTransaction.java b/fineract-loan/src/main/java/org/apache/fineract/portfolio/loanaccount/domain/LoanTransaction.java index fd42d7e3d..811baeeb2 100644 --- a/fineract-loan/src/main/java/org/apache/fineract/portfolio/loanaccount/domain/LoanTransaction.java +++ b/fineract-loan/src/main/java/org/apache/fineract/portfolio/loanaccount/domain/LoanTransaction.java @@ -316,7 +316,7 @@ public class LoanTransaction extends AbstractAuditableWithUTCDateTimeCustom { && loanTransaction.getOverPaymentPortion(currency).isEqualTo(newLoanTransaction.getOverPaymentPortion(currency)); } - private LoanTransaction(final Loan loan, final Office office, final Integer typeOf, final LocalDate dateOf, final BigDecimal amount, + public LoanTransaction(final Loan loan, final Office office, final Integer typeOf, final LocalDate dateOf, final BigDecimal amount, final BigDecimal principalPortion, final BigDecimal interestPortion, final BigDecimal feeChargesPortion, final BigDecimal penaltyChargesPortion, final BigDecimal overPaymentPortion, final boolean reversed, final PaymentDetail paymentDetail, final ExternalId externalId) { @@ -681,6 +681,10 @@ public class LoanTransaction extends AbstractAuditableWithUTCDateTimeCustom { return getTypeOf().isChargeOff() && isNotReversed(); } + public boolean isReAge() { + return getTypeOf().isReAge() && isNotReversed(); + } + public boolean isIdentifiedBy(final Long identifier) { return getId().equals(identifier); } diff --git a/fineract-loan/src/main/java/org/apache/fineract/portfolio/loanaccount/domain/LoanTransactionType.java b/fineract-loan/src/main/java/org/apache/fineract/portfolio/loanaccount/domain/LoanTransactionType.java index ef5bd3f66..a057300f6 100644 --- a/fineract-loan/src/main/java/org/apache/fineract/portfolio/loanaccount/domain/LoanTransactionType.java +++ b/fineract-loan/src/main/java/org/apache/fineract/portfolio/loanaccount/domain/LoanTransactionType.java @@ -60,7 +60,8 @@ public enum LoanTransactionType { CHARGEBACK(25, "loanTransactionType.chargeback"), // CHARGE_ADJUSTMENT(26, "loanTransactionType.chargeAdjustment"), // CHARGE_OFF(27, "loanTransactionType.chargeOff"), // - DOWN_PAYMENT(28, "loanTransactionType.downPayment"); + DOWN_PAYMENT(28, "loanTransactionType.downPayment"), // + REAGE(29, "loanTransactionType.reAge"); private final Integer value; private final String code; @@ -104,6 +105,7 @@ public enum LoanTransactionType { case 26 -> LoanTransactionType.CHARGE_ADJUSTMENT; case 27 -> LoanTransactionType.CHARGE_OFF; case 28 -> LoanTransactionType.DOWN_PAYMENT; + case 29 -> LoanTransactionType.REAGE; default -> LoanTransactionType.INVALID; }; } @@ -192,6 +194,10 @@ public enum LoanTransactionType { return this.equals(LoanTransactionType.CHARGE_OFF); } + public boolean isReAge() { + return this.equals(LoanTransactionType.REAGE); + } + public boolean isDownPayment() { return this.equals(LoanTransactionType.DOWN_PAYMENT); } diff --git a/fineract-loan/src/main/java/org/apache/fineract/portfolio/loanaccount/domain/reaging/LoanReAgeParameter.java b/fineract-loan/src/main/java/org/apache/fineract/portfolio/loanaccount/domain/reaging/LoanReAgeParameter.java new file mode 100644 index 000000000..78198ea71 --- /dev/null +++ b/fineract-loan/src/main/java/org/apache/fineract/portfolio/loanaccount/domain/reaging/LoanReAgeParameter.java @@ -0,0 +1,54 @@ +/** + * 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.loanaccount.domain.reaging; + +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.EnumType; +import jakarta.persistence.Enumerated; +import jakarta.persistence.Table; +import java.time.LocalDate; +import lombok.AllArgsConstructor; +import lombok.Getter; +import org.apache.fineract.infrastructure.core.domain.AbstractAuditableWithUTCDateTimeCustom; +import org.apache.fineract.portfolio.common.domain.PeriodFrequencyType; + +@Entity +@Table(name = "m_loan_reage_parameter") +@AllArgsConstructor +@Getter +public class LoanReAgeParameter extends AbstractAuditableWithUTCDateTimeCustom { + + // intentionally not doing a JPA relationship since it's not necessary + @Column(name = "loan_transaction_id", nullable = false) + private Long loanTransactionId; + + @Enumerated(EnumType.STRING) + @Column(name = "frequency", nullable = false) + private PeriodFrequencyType frequency; + + @Column(name = "start_date", nullable = false) + private LocalDate startDate; + + @Column(name = "number_of_installments", nullable = false) + private Integer numberOfInstallments; + + // for JPA, don't use + protected LoanReAgeParameter() {} +} diff --git a/fineract-loan/src/main/java/org/apache/fineract/portfolio/loanaccount/domain/reaging/LoanReAgingParameterRepository.java b/fineract-loan/src/main/java/org/apache/fineract/portfolio/loanaccount/domain/reaging/LoanReAgingParameterRepository.java new file mode 100644 index 000000000..7558afbc0 --- /dev/null +++ b/fineract-loan/src/main/java/org/apache/fineract/portfolio/loanaccount/domain/reaging/LoanReAgingParameterRepository.java @@ -0,0 +1,28 @@ +/** + * 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.loanaccount.domain.reaging; + +import java.util.Optional; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.repository.query.Param; + +public interface LoanReAgingParameterRepository extends JpaRepository { + + Optional findByLoanTransactionId(@Param("loanTransactionId") Long loanTransactionId); +} diff --git a/fineract-loan/src/main/java/org/apache/fineract/portfolio/loanproduct/service/LoanEnumerations.java b/fineract-loan/src/main/java/org/apache/fineract/portfolio/loanproduct/service/LoanEnumerations.java index affa36385..efcae196d 100644 --- a/fineract-loan/src/main/java/org/apache/fineract/portfolio/loanproduct/service/LoanEnumerations.java +++ b/fineract-loan/src/main/java/org/apache/fineract/portfolio/loanproduct/service/LoanEnumerations.java @@ -313,6 +313,8 @@ public final class LoanEnumerations { LoanTransactionType.CHARGE_OFF.getCode(), "Charge-off"); case DOWN_PAYMENT -> new LoanTransactionEnumData(LoanTransactionType.DOWN_PAYMENT.getValue().longValue(), LoanTransactionType.DOWN_PAYMENT.getCode(), "Down Payment"); + case REAGE -> new LoanTransactionEnumData(LoanTransactionType.REAGE.getValue().longValue(), LoanTransactionType.REAGE.getCode(), + "Re-age"); }; } diff --git a/fineract-provider/src/main/java/org/apache/fineract/infrastructure/event/business/domain/loan/transaction/reaging/LoanReAgeTransactionBusinessEvent.java b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/event/business/domain/loan/transaction/reaging/LoanReAgeTransactionBusinessEvent.java new file mode 100644 index 000000000..fea932aa7 --- /dev/null +++ b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/event/business/domain/loan/transaction/reaging/LoanReAgeTransactionBusinessEvent.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.infrastructure.event.business.domain.loan.transaction.reaging; + +import org.apache.fineract.infrastructure.event.business.domain.loan.transaction.LoanTransactionBusinessEvent; +import org.apache.fineract.portfolio.loanaccount.domain.LoanTransaction; + +public class LoanReAgeTransactionBusinessEvent extends LoanTransactionBusinessEvent { + + private static final String TYPE = "LoanReAgeTransactionBusinessEvent"; + + public LoanReAgeTransactionBusinessEvent(LoanTransaction value) { + super(value); + } + + @Override + public String getType() { + return TYPE; + } +} diff --git a/fineract-provider/src/main/java/org/apache/fineract/infrastructure/event/business/domain/loan/transaction/reaging/LoanUndoReAgeTransactionBusinessEvent.java b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/event/business/domain/loan/transaction/reaging/LoanUndoReAgeTransactionBusinessEvent.java new file mode 100644 index 000000000..540c2e06e --- /dev/null +++ b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/event/business/domain/loan/transaction/reaging/LoanUndoReAgeTransactionBusinessEvent.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.infrastructure.event.business.domain.loan.transaction.reaging; + +import org.apache.fineract.infrastructure.event.business.domain.loan.transaction.LoanTransactionBusinessEvent; +import org.apache.fineract.portfolio.loanaccount.domain.LoanTransaction; + +public class LoanUndoReAgeTransactionBusinessEvent extends LoanTransactionBusinessEvent { + + private static final String TYPE = "LoanUndoReAgeTransactionBusinessEvent"; + + public LoanUndoReAgeTransactionBusinessEvent(LoanTransaction value) { + super(value); + } + + @Override + public String getType() { + return TYPE; + } +} diff --git a/fineract-provider/src/main/java/org/apache/fineract/portfolio/loanaccount/api/LoanTransactionsApiResource.java b/fineract-provider/src/main/java/org/apache/fineract/portfolio/loanaccount/api/LoanTransactionsApiResource.java index c9eb31fb6..b47944809 100644 --- a/fineract-provider/src/main/java/org/apache/fineract/portfolio/loanaccount/api/LoanTransactionsApiResource.java +++ b/fineract-provider/src/main/java/org/apache/fineract/portfolio/loanaccount/api/LoanTransactionsApiResource.java @@ -81,6 +81,8 @@ public class LoanTransactionsApiResource { public static final String CHARGE_OFF_COMMAND_VALUE = "charge-off"; public static final String UNDO_CHARGE_OFF_COMMAND_VALUE = "undo-charge-off"; public static final String DOWN_PAYMENT = "downPayment"; + public static final String UNDO_REAGE = "undoReAge"; + public static final String REAGE = "reAge"; private final Set responseDataParameters = new HashSet<>(Arrays.asList("id", "type", "date", "currency", "amount", "externalId", LoanApiConstants.REVERSAL_EXTERNAL_ID_PARAMNAME, LoanApiConstants.REVERSED_ON_DATE_PARAMNAME)); @@ -477,6 +479,10 @@ public class LoanTransactionsApiResource { commandRequest = builder.undoChargeOff(resolvedLoanId).build(); } else if (CommandParameterUtil.is(commandParam, DOWN_PAYMENT)) { commandRequest = builder.downPayment(resolvedLoanId).build(); + } else if (CommandParameterUtil.is(commandParam, REAGE)) { + commandRequest = builder.reAge(resolvedLoanId).build(); + } else if (CommandParameterUtil.is(commandParam, UNDO_REAGE)) { + commandRequest = builder.undoReAge(resolvedLoanId).build(); } if (commandRequest == null) { diff --git a/fineract-provider/src/main/java/org/apache/fineract/portfolio/loanaccount/api/LoanTransactionsApiResourceSwagger.java b/fineract-provider/src/main/java/org/apache/fineract/portfolio/loanaccount/api/LoanTransactionsApiResourceSwagger.java index 33ed6e2b0..e42714357 100644 --- a/fineract-provider/src/main/java/org/apache/fineract/portfolio/loanaccount/api/LoanTransactionsApiResourceSwagger.java +++ b/fineract-provider/src/main/java/org/apache/fineract/portfolio/loanaccount/api/LoanTransactionsApiResourceSwagger.java @@ -283,6 +283,15 @@ final class LoanTransactionsApiResourceSwagger { public Long chargeOffReasonId; @Schema(example = "1") public Long writeoffReasonId; + + // command=reAge START + @Schema(example = "frequency") + public String frequency; + @Schema(example = "startDate") + public String startDate; + @Schema(example = "numberOfInstallments") + public Integer numberOfInstallments; + // command=reAge END } @Schema(description = "PostLoansLoanIdTransactionsResponse") diff --git a/fineract-provider/src/main/java/org/apache/fineract/portfolio/loanaccount/handler/loan/reaging/LoanReAgingCommandHandler.java b/fineract-provider/src/main/java/org/apache/fineract/portfolio/loanaccount/handler/loan/reaging/LoanReAgingCommandHandler.java new file mode 100644 index 000000000..7f3778246 --- /dev/null +++ b/fineract-provider/src/main/java/org/apache/fineract/portfolio/loanaccount/handler/loan/reaging/LoanReAgingCommandHandler.java @@ -0,0 +1,50 @@ +/** + * 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.loanaccount.handler.loan.reaging; + +import lombok.RequiredArgsConstructor; +import org.apache.fineract.commands.annotation.CommandType; +import org.apache.fineract.commands.handler.NewCommandSourceHandler; +import org.apache.fineract.infrastructure.DataIntegrityErrorHandler; +import org.apache.fineract.infrastructure.core.api.JsonCommand; +import org.apache.fineract.infrastructure.core.data.CommandProcessingResult; +import org.apache.fineract.portfolio.loanaccount.service.reaging.LoanReAgingServiceImpl; +import org.springframework.dao.DataIntegrityViolationException; +import org.springframework.orm.jpa.JpaSystemException; +import org.springframework.stereotype.Service; + +@Service +@RequiredArgsConstructor +@CommandType(entity = "LOAN", action = "REAGE") +public class LoanReAgingCommandHandler implements NewCommandSourceHandler { + + private final LoanReAgingServiceImpl loanReAgingService; + private final DataIntegrityErrorHandler dataIntegrityErrorHandler; + + @Override + public CommandProcessingResult processCommand(JsonCommand command) { + try { + return loanReAgingService.reAge(command.getLoanId(), command); + } catch (final JpaSystemException | DataIntegrityViolationException dve) { + dataIntegrityErrorHandler.handleDataIntegrityIssues(command, dve.getMostSpecificCause(), dve, "loan.reAge", + "Error while handling re-aging"); + return CommandProcessingResult.empty(); + } + } +} diff --git a/fineract-provider/src/main/java/org/apache/fineract/portfolio/loanaccount/handler/loan/reaging/LoanUndoReAgingCommandHandler.java b/fineract-provider/src/main/java/org/apache/fineract/portfolio/loanaccount/handler/loan/reaging/LoanUndoReAgingCommandHandler.java new file mode 100644 index 000000000..d66583d2c --- /dev/null +++ b/fineract-provider/src/main/java/org/apache/fineract/portfolio/loanaccount/handler/loan/reaging/LoanUndoReAgingCommandHandler.java @@ -0,0 +1,50 @@ +/** + * 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.loanaccount.handler.loan.reaging; + +import lombok.RequiredArgsConstructor; +import org.apache.fineract.commands.annotation.CommandType; +import org.apache.fineract.commands.handler.NewCommandSourceHandler; +import org.apache.fineract.infrastructure.DataIntegrityErrorHandler; +import org.apache.fineract.infrastructure.core.api.JsonCommand; +import org.apache.fineract.infrastructure.core.data.CommandProcessingResult; +import org.apache.fineract.portfolio.loanaccount.service.reaging.LoanReAgingServiceImpl; +import org.springframework.dao.DataIntegrityViolationException; +import org.springframework.orm.jpa.JpaSystemException; +import org.springframework.stereotype.Service; + +@Service +@RequiredArgsConstructor +@CommandType(entity = "LOAN", action = "UNDO_REAGE") +public class LoanUndoReAgingCommandHandler implements NewCommandSourceHandler { + + private final LoanReAgingServiceImpl loanReAgingService; + private final DataIntegrityErrorHandler dataIntegrityErrorHandler; + + @Override + public CommandProcessingResult processCommand(JsonCommand command) { + try { + return loanReAgingService.undoReAge(command.getLoanId(), command); + } catch (final JpaSystemException | DataIntegrityViolationException dve) { + dataIntegrityErrorHandler.handleDataIntegrityIssues(command, dve.getMostSpecificCause(), dve, "loan.undoReAge", + "Error while handling undo re-age"); + return CommandProcessingResult.empty(); + } + } +} diff --git a/fineract-provider/src/main/java/org/apache/fineract/portfolio/loanaccount/service/listener/LoanTransactionDelinquencyRecalculationListener.java b/fineract-provider/src/main/java/org/apache/fineract/portfolio/loanaccount/service/listener/LoanTransactionDelinquencyRecalculationListener.java new file mode 100644 index 000000000..9ee55b142 --- /dev/null +++ b/fineract-provider/src/main/java/org/apache/fineract/portfolio/loanaccount/service/listener/LoanTransactionDelinquencyRecalculationListener.java @@ -0,0 +1,60 @@ +/** + * 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.loanaccount.service.listener; + +import java.util.List; +import lombok.RequiredArgsConstructor; +import org.apache.fineract.infrastructure.event.business.BusinessEventListener; +import org.apache.fineract.infrastructure.event.business.domain.loan.transaction.LoanTransactionBusinessEvent; +import org.apache.fineract.infrastructure.event.business.domain.loan.transaction.reaging.LoanReAgeTransactionBusinessEvent; +import org.apache.fineract.infrastructure.event.business.domain.loan.transaction.reaging.LoanUndoReAgeTransactionBusinessEvent; +import org.apache.fineract.infrastructure.event.business.service.BusinessEventNotifierService; +import org.apache.fineract.portfolio.loanaccount.domain.LoanAccountDomainService; +import org.apache.fineract.portfolio.loanaccount.domain.LoanTransaction; +import org.springframework.beans.factory.InitializingBean; +import org.springframework.stereotype.Component; + +@Component +@RequiredArgsConstructor +public class LoanTransactionDelinquencyRecalculationListener + implements InitializingBean, BusinessEventListener { + + // Extend this list to support more event types so the hardcoded delinquency recalculation can be removed from the + // use-cases + private static final List> SUPPORTED_EVENT_TYPES = List.of(// + LoanReAgeTransactionBusinessEvent.class, // + LoanUndoReAgeTransactionBusinessEvent.class // + );// + + private final LoanAccountDomainService loanAccountDomainService; + private final BusinessEventNotifierService businessEventNotifierService; + + @Override + public void afterPropertiesSet() throws Exception { + businessEventNotifierService.addPostBusinessEventListener(LoanTransactionBusinessEvent.class, this); + } + + @Override + public void onBusinessEvent(LoanTransactionBusinessEvent event) { + if (SUPPORTED_EVENT_TYPES.contains(event.getClass())) { + LoanTransaction tx = event.get(); + loanAccountDomainService.setLoanDelinquencyTag(tx.getLoan(), tx.getTransactionDate()); + } + } +} diff --git a/fineract-provider/src/main/java/org/apache/fineract/portfolio/loanaccount/service/reaging/LoanReAgingServiceImpl.java b/fineract-provider/src/main/java/org/apache/fineract/portfolio/loanaccount/service/reaging/LoanReAgingServiceImpl.java new file mode 100644 index 000000000..f08350527 --- /dev/null +++ b/fineract-provider/src/main/java/org/apache/fineract/portfolio/loanaccount/service/reaging/LoanReAgingServiceImpl.java @@ -0,0 +1,155 @@ +/** + * 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.loanaccount.service.reaging; + +import static java.math.BigDecimal.ZERO; + +import java.math.BigDecimal; +import java.time.LocalDate; +import java.util.Comparator; +import java.util.LinkedHashMap; +import java.util.Map; +import lombok.RequiredArgsConstructor; +import org.apache.fineract.infrastructure.core.api.JsonCommand; +import org.apache.fineract.infrastructure.core.data.CommandProcessingResult; +import org.apache.fineract.infrastructure.core.data.CommandProcessingResultBuilder; +import org.apache.fineract.infrastructure.core.domain.ExternalId; +import org.apache.fineract.infrastructure.core.service.DateUtils; +import org.apache.fineract.infrastructure.core.service.ExternalIdFactory; +import org.apache.fineract.infrastructure.event.business.domain.loan.transaction.reaging.LoanReAgeTransactionBusinessEvent; +import org.apache.fineract.infrastructure.event.business.domain.loan.transaction.reaging.LoanUndoReAgeTransactionBusinessEvent; +import org.apache.fineract.infrastructure.event.business.service.BusinessEventNotifierService; +import org.apache.fineract.organisation.monetary.domain.Money; +import org.apache.fineract.portfolio.common.domain.PeriodFrequencyType; +import org.apache.fineract.portfolio.loanaccount.api.LoanReAgingApiConstants; +import org.apache.fineract.portfolio.loanaccount.domain.Loan; +import org.apache.fineract.portfolio.loanaccount.domain.LoanTransaction; +import org.apache.fineract.portfolio.loanaccount.domain.LoanTransactionRepository; +import org.apache.fineract.portfolio.loanaccount.domain.LoanTransactionType; +import org.apache.fineract.portfolio.loanaccount.domain.reaging.LoanReAgeParameter; +import org.apache.fineract.portfolio.loanaccount.domain.reaging.LoanReAgingParameterRepository; +import org.apache.fineract.portfolio.loanaccount.service.LoanAssembler; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service +@RequiredArgsConstructor +@Transactional +public class LoanReAgingServiceImpl { + + private final LoanAssembler loanAssembler; + private final LoanReAgingValidator reAgingValidator; + private final ExternalIdFactory externalIdFactory; + private final BusinessEventNotifierService businessEventNotifierService; + private final LoanTransactionRepository loanTransactionRepository; + private final LoanReAgingParameterRepository reAgingParameterRepository; + + public CommandProcessingResult reAge(Long loanId, JsonCommand command) { + Loan loan = loanAssembler.assembleFrom(loanId); + reAgingValidator.validateReAge(loan, command); + + Map changes = new LinkedHashMap<>(); + changes.put(LoanReAgingApiConstants.localeParameterName, command.locale()); + changes.put(LoanReAgingApiConstants.dateFormatParameterName, command.dateFormat()); + + LoanTransaction reAgeTransaction = createReAgeTransaction(loan, command); + // important to do a flush before creating the reage parameter since it needs the ID + loanTransactionRepository.saveAndFlush(reAgeTransaction); + + LoanReAgeParameter reAgeParameter = createReAgeParameter(reAgeTransaction, command); + reAgingParameterRepository.saveAndFlush(reAgeParameter); + + // delinquency recalculation will be triggered by the event in a decoupled way via a listener + businessEventNotifierService.notifyPostBusinessEvent(new LoanReAgeTransactionBusinessEvent(reAgeTransaction)); + return new CommandProcessingResultBuilder() // + .withCommandId(command.commandId()) // + .withEntityId(reAgeTransaction.getId()) // + .withEntityExternalId(reAgeTransaction.getExternalId()) // + .withOfficeId(loan.getOfficeId()) // + .withClientId(loan.getClientId()) // + .withGroupId(loan.getGroupId()) // + .withLoanId(command.getLoanId()) // + .with(changes).build(); + } + + private LoanReAgeParameter createReAgeParameter(LoanTransaction reAgeTransaction, JsonCommand command) { + // TODO: these parameters should be checked when the validations are implemented + PeriodFrequencyType periodFrequencyType = command.enumValueOfParameterNamed(LoanReAgingApiConstants.frequency, + PeriodFrequencyType.class); + LocalDate startDate = command.dateValueOfParameterNamed(LoanReAgingApiConstants.startDate); + Integer numberOfInstallments = command.integerValueOfParameterNamed(LoanReAgingApiConstants.numberOfInstallments); + return new LoanReAgeParameter(reAgeTransaction.getId(), periodFrequencyType, startDate, numberOfInstallments); + } + + public CommandProcessingResult undoReAge(Long loanId, JsonCommand command) { + Loan loan = loanAssembler.assembleFrom(loanId); + reAgingValidator.validateUndoReAge(loan, command); + + Map changes = new LinkedHashMap<>(); + changes.put(LoanReAgingApiConstants.localeParameterName, command.locale()); + changes.put(LoanReAgingApiConstants.dateFormatParameterName, command.dateFormat()); + + LoanTransaction reAgeTransaction = findLatestNonReversedReAgeTransaction(loan); + if (reAgeTransaction == null) { + // TODO: when validations implemented; throw exception if there isn't a reage transaction available + } + reverseReAgeTransaction(reAgeTransaction, command); + loanTransactionRepository.saveAndFlush(reAgeTransaction); + + // delinquency recalculation will be triggered by the event in a decoupled way via a listener + businessEventNotifierService.notifyPostBusinessEvent(new LoanUndoReAgeTransactionBusinessEvent(reAgeTransaction)); + return new CommandProcessingResultBuilder() // + .withCommandId(command.commandId()) // + .withEntityId(reAgeTransaction.getId()) // + .withEntityExternalId(reAgeTransaction.getExternalId()) // + .withOfficeId(loan.getOfficeId()) // + .withClientId(loan.getClientId()) // + .withGroupId(loan.getGroupId()) // + .withLoanId(command.getLoanId()) // + .with(changes).build(); + } + + private void reverseReAgeTransaction(LoanTransaction reAgeTransaction, JsonCommand command) { + ExternalId reversalExternalId = externalIdFactory.createFromCommand(command, LoanReAgingApiConstants.externalIdParameterName); + reAgeTransaction.reverse(reversalExternalId); + reAgeTransaction.manuallyAdjustedOrReversed(); + } + + private LoanTransaction findLatestNonReversedReAgeTransaction(Loan loan) { + return loan.getLoanTransactions().stream() // + .filter(LoanTransaction::isNotReversed) // + .filter(LoanTransaction::isReAge) // + .max(Comparator.comparing(LoanTransaction::getTransactionDate)) // + .orElse(null); + } + + private LoanTransaction createReAgeTransaction(Loan loan, JsonCommand command) { + ExternalId txExternalId = externalIdFactory.createFromCommand(command, LoanReAgingApiConstants.externalIdParameterName); + + // reaging transaction date is always the current business date + LocalDate transactionDate = DateUtils.getBusinessLocalDate(); + + // in case of a reaging transaction, only the outstanding principal amount until the business date is considered + Money txPrincipal = loan.getTotalPrincipalOutstandingUntil(transactionDate); + BigDecimal txPrincipalAmount = txPrincipal.getAmount(); + + return new LoanTransaction(loan, loan.getOffice(), LoanTransactionType.REAGE.getValue(), transactionDate, txPrincipalAmount, + txPrincipalAmount, ZERO, ZERO, ZERO, null, false, null, txExternalId); + } +} diff --git a/fineract-provider/src/main/java/org/apache/fineract/portfolio/loanaccount/service/reaging/LoanReAgingValidator.java b/fineract-provider/src/main/java/org/apache/fineract/portfolio/loanaccount/service/reaging/LoanReAgingValidator.java new file mode 100644 index 000000000..a3dfceb8e --- /dev/null +++ b/fineract-provider/src/main/java/org/apache/fineract/portfolio/loanaccount/service/reaging/LoanReAgingValidator.java @@ -0,0 +1,35 @@ +/** + * 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.loanaccount.service.reaging; + +import org.apache.fineract.infrastructure.core.api.JsonCommand; +import org.apache.fineract.portfolio.loanaccount.domain.Loan; +import org.springframework.stereotype.Component; + +@Component +public class LoanReAgingValidator { + + public void validateReAge(Loan loan, JsonCommand command) { + // TODO: implement + } + + public void validateUndoReAge(Loan loan, JsonCommand command) { + // TODO: implement + } +} diff --git a/fineract-provider/src/main/resources/db/changelog/tenant/changelog-tenant.xml b/fineract-provider/src/main/resources/db/changelog/tenant/changelog-tenant.xml index 2085cf5fb..40e78b8d0 100644 --- a/fineract-provider/src/main/resources/db/changelog/tenant/changelog-tenant.xml +++ b/fineract-provider/src/main/resources/db/changelog/tenant/changelog-tenant.xml @@ -154,4 +154,6 @@ + + diff --git a/fineract-provider/src/main/resources/db/changelog/tenant/parts/0135_add_external_event_for_loan_reaging.xml b/fineract-provider/src/main/resources/db/changelog/tenant/parts/0135_add_external_event_for_loan_reaging.xml new file mode 100644 index 000000000..b501842a3 --- /dev/null +++ b/fineract-provider/src/main/resources/db/changelog/tenant/parts/0135_add_external_event_for_loan_reaging.xml @@ -0,0 +1,37 @@ + + + + + + + + + + + + + + + + diff --git a/fineract-provider/src/main/resources/db/changelog/tenant/parts/0136_loan_reaging_parameters.xml b/fineract-provider/src/main/resources/db/changelog/tenant/parts/0136_loan_reaging_parameters.xml new file mode 100644 index 000000000..5539fee94 --- /dev/null +++ b/fineract-provider/src/main/resources/db/changelog/tenant/parts/0136_loan_reaging_parameters.xml @@ -0,0 +1,74 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/fineract-provider/src/test/java/org/apache/fineract/infrastructure/event/external/service/ExternalEventConfigurationValidationServiceTest.java b/fineract-provider/src/test/java/org/apache/fineract/infrastructure/event/external/service/ExternalEventConfigurationValidationServiceTest.java index 69b69d9db..76e225632 100644 --- a/fineract-provider/src/test/java/org/apache/fineract/infrastructure/event/external/service/ExternalEventConfigurationValidationServiceTest.java +++ b/fineract-provider/src/test/java/org/apache/fineract/infrastructure/event/external/service/ExternalEventConfigurationValidationServiceTest.java @@ -98,7 +98,8 @@ public class ExternalEventConfigurationValidationServiceTest { "LoanChargeOffPostBusinessEvent", "LoanUndoChargeOffBusinessEvent", "LoanAccrualTransactionCreatedBusinessEvent", "LoanRescheduledDueAdjustScheduleBusinessEvent", "LoanOwnershipTransferBusinessEvent", "LoanAccountSnapshotBusinessEvent", "LoanTransactionDownPaymentPostBusinessEvent", "LoanTransactionDownPaymentPreBusinessEvent", - "LoanAccountDelinquencyPauseChangedBusinessEvent", "LoanAccountCustomSnapshotBusinessEvent"); + "LoanAccountDelinquencyPauseChangedBusinessEvent", "LoanAccountCustomSnapshotBusinessEvent", + "LoanReAgeTransactionBusinessEvent", "LoanUndoReAgeTransactionBusinessEvent"); List tenants = Arrays .asList(new FineractPlatformTenant(1L, "default", "Default Tenant", "Europe/Budapest", null)); @@ -178,7 +179,8 @@ public class ExternalEventConfigurationValidationServiceTest { "LoanUndoChargeOffBusinessEvent", "LoanAccrualTransactionCreatedBusinessEvent", "LoanRescheduledDueAdjustScheduleBusinessEvent", "LoanOwnershipTransferBusinessEvent", "LoanAccountSnapshotBusinessEvent", "LoanTransactionDownPaymentPostBusinessEvent", "LoanTransactionDownPaymentPreBusinessEvent", - "LoanAccountDelinquencyPauseChangedBusinessEvent", "LoanAccountCustomSnapshotBusinessEvent"); + "LoanAccountDelinquencyPauseChangedBusinessEvent", "LoanAccountCustomSnapshotBusinessEvent", + "LoanReAgeTransactionBusinessEvent", "LoanUndoReAgeTransactionBusinessEvent"); List tenants = Arrays .asList(new FineractPlatformTenant(1L, "default", "Default Tenant", "Europe/Budapest", null)); diff --git a/integration-tests/src/test/java/org/apache/fineract/integrationtests/BaseLoanIntegrationTest.java b/integration-tests/src/test/java/org/apache/fineract/integrationtests/BaseLoanIntegrationTest.java index 790751528..375fdc2c6 100644 --- a/integration-tests/src/test/java/org/apache/fineract/integrationtests/BaseLoanIntegrationTest.java +++ b/integration-tests/src/test/java/org/apache/fineract/integrationtests/BaseLoanIntegrationTest.java @@ -40,6 +40,7 @@ import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Objects; +import java.util.Optional; import java.util.UUID; import java.util.concurrent.atomic.AtomicInteger; import java.util.function.Consumer; @@ -54,6 +55,7 @@ import org.apache.fineract.client.models.BusinessDateRequest; import org.apache.fineract.client.models.GetJournalEntriesTransactionIdResponse; import org.apache.fineract.client.models.GetLoansLoanIdRepaymentPeriod; import org.apache.fineract.client.models.GetLoansLoanIdResponse; +import org.apache.fineract.client.models.GetLoansLoanIdTransactions; import org.apache.fineract.client.models.PaymentAllocationOrder; import org.apache.fineract.client.models.PostChargesResponse; import org.apache.fineract.client.models.PostLoanProductsRequest; @@ -317,10 +319,18 @@ public abstract class BaseLoanIntegrationTest { } else { Assertions.assertEquals(transactions.length, loanDetails.getTransactions().size()); Arrays.stream(transactions).forEach(tr -> { - boolean found = loanDetails.getTransactions().stream() - .anyMatch(item -> Objects.equals(item.getAmount(), tr.amount) && Objects.equals(item.getType().getValue(), tr.type) - && Objects.equals(item.getDate(), LocalDate.parse(tr.date, dateTimeFormatter))); - Assertions.assertTrue(found, "Required transaction not found: " + tr); + Optional optTx = loanDetails.getTransactions().stream() + .filter(item -> Objects.equals(item.getAmount(), tr.amount) // + && Objects.equals(item.getType().getValue(), tr.type) // + && Objects.equals(item.getDate(), LocalDate.parse(tr.date, dateTimeFormatter))) + .findFirst(); + Assertions.assertTrue(optTx.isPresent(), "Required transaction not found: " + tr); + + GetLoansLoanIdTransactions tx = optTx.get(); + + if (tr.reversed != null) { + Assertions.assertEquals(tr.reversed, tx.getManuallyReversed(), "Transaction is not reversed: " + tr); + } }); } } @@ -355,6 +365,20 @@ public abstract class BaseLoanIntegrationTest { inlineLoanCOBHelper.executeInlineCOB(List.of(loanId)); } + protected void reAgeLoan(Long loanId, String frequency, String startDate, Integer numberOfInstallments) { + PostLoansLoanIdTransactionsRequest request = new PostLoansLoanIdTransactionsRequest(); + request.setDateFormat(DATETIME_PATTERN); + request.setLocale("en"); + request.setFrequency(frequency); + request.setStartDate(startDate); + request.setNumberOfInstallments(numberOfInstallments); + loanTransactionHelper.reAge(loanId, request); + } + + protected void undoReAgeLoan(Long loanId) { + loanTransactionHelper.undoReAge(loanId, new PostLoansLoanIdTransactionsRequest()); + } + protected void verifyLastClosedBusinessDate(Long loanId, String lastClosedBusinessDate) { GetLoansLoanIdResponse loanDetails = loanTransactionHelper.getLoanDetails(loanId); Assertions.assertNotNull(loanDetails.getLastClosedBusinessDate()); @@ -554,7 +578,11 @@ public abstract class BaseLoanIntegrationTest { } protected Transaction transaction(double principalAmount, String type, String date) { - return new Transaction(principalAmount, type, date); + return new Transaction(principalAmount, type, date, null); + } + + protected Transaction reversedTransaction(double principalAmount, String type, String date) { + return new Transaction(principalAmount, type, date, true); } protected TransactionExt transaction(double amount, String type, String date, double outstandingAmount, double principalPortion, @@ -657,6 +685,7 @@ public abstract class BaseLoanIntegrationTest { Double amount; String type; String date; + Boolean reversed; } @ToString @@ -711,6 +740,7 @@ public abstract class BaseLoanIntegrationTest { public static class RepaymentFrequencyType { public static final Integer MONTHS = 2; + public static final String MONTHS_STRING = "MONTHS"; } public static class InterestCalculationPeriodType { diff --git a/integration-tests/src/test/java/org/apache/fineract/integrationtests/common/ExternalEventConfigurationHelper.java b/integration-tests/src/test/java/org/apache/fineract/integrationtests/common/ExternalEventConfigurationHelper.java index 42c5e1dd3..2b8b9e1aa 100644 --- a/integration-tests/src/test/java/org/apache/fineract/integrationtests/common/ExternalEventConfigurationHelper.java +++ b/integration-tests/src/test/java/org/apache/fineract/integrationtests/common/ExternalEventConfigurationHelper.java @@ -500,6 +500,16 @@ public class ExternalEventConfigurationHelper { loanAccountCustomSnapshotBusinessEvent.put("enabled", false); defaults.add(loanAccountCustomSnapshotBusinessEvent); + Map loanReAgeTransactionBusinessEvent = new HashMap<>(); + loanReAgeTransactionBusinessEvent.put("type", "LoanReAgeTransactionBusinessEvent"); + loanReAgeTransactionBusinessEvent.put("enabled", false); + defaults.add(loanReAgeTransactionBusinessEvent); + + Map loanUndoReAgeTransactionBusinessEvent = new HashMap<>(); + loanUndoReAgeTransactionBusinessEvent.put("type", "LoanUndoReAgeTransactionBusinessEvent"); + loanUndoReAgeTransactionBusinessEvent.put("enabled", false); + defaults.add(loanUndoReAgeTransactionBusinessEvent); + return defaults; } diff --git a/integration-tests/src/test/java/org/apache/fineract/integrationtests/common/loans/LoanTransactionHelper.java b/integration-tests/src/test/java/org/apache/fineract/integrationtests/common/loans/LoanTransactionHelper.java index f34191726..e61438cc4 100644 --- a/integration-tests/src/test/java/org/apache/fineract/integrationtests/common/loans/LoanTransactionHelper.java +++ b/integration-tests/src/test/java/org/apache/fineract/integrationtests/common/loans/LoanTransactionHelper.java @@ -589,6 +589,14 @@ public class LoanTransactionHelper extends IntegrationTest { return ok(fineract().loanTransactions.executeLoanTransaction(loanId, request, "repayment")); } + public PostLoansLoanIdTransactionsResponse reAge(final Long loanId, final PostLoansLoanIdTransactionsRequest request) { + return ok(fineract().loanTransactions.executeLoanTransaction(loanId, request, "reAge")); + } + + public PostLoansLoanIdTransactionsResponse undoReAge(final Long loanId, final PostLoansLoanIdTransactionsRequest request) { + return ok(fineract().loanTransactions.executeLoanTransaction(loanId, request, "undoReAge")); + } + public PutChargeTransactionChangesResponse undoWaiveLoanCharge(final Long loanId, final Long transactionId, final PutChargeTransactionChangesRequest request) { log.info("--------------------------------- UNDO WAIVE CHARGES FOR LOAN --------------------------------"); diff --git a/integration-tests/src/test/java/org/apache/fineract/integrationtests/loan/reaging/LoanReAgingIntegrationTest.java b/integration-tests/src/test/java/org/apache/fineract/integrationtests/loan/reaging/LoanReAgingIntegrationTest.java new file mode 100644 index 000000000..20fae1996 --- /dev/null +++ b/integration-tests/src/test/java/org/apache/fineract/integrationtests/loan/reaging/LoanReAgingIntegrationTest.java @@ -0,0 +1,184 @@ +/** + * 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.integrationtests.loan.reaging; + +import java.math.BigDecimal; +import java.util.concurrent.atomic.AtomicLong; +import org.apache.fineract.client.models.PostLoanProductsRequest; +import org.apache.fineract.client.models.PostLoanProductsResponse; +import org.apache.fineract.client.models.PostLoansLoanIdResponse; +import org.apache.fineract.client.models.PostLoansRequest; +import org.apache.fineract.client.models.PostLoansResponse; +import org.apache.fineract.integrationtests.BaseLoanIntegrationTest; +import org.apache.fineract.integrationtests.common.ClientHelper; +import org.junit.jupiter.api.Test; + +public class LoanReAgingIntegrationTest extends BaseLoanIntegrationTest { + + @Test + public void test_LoanReAgeTransaction_Works() { + AtomicLong createdLoanId = new AtomicLong(); + + runAt("01 January 2023", () -> { + // Create Client + Long clientId = clientHelper.createClient(ClientHelper.defaultClientCreationRequest()).getClientId(); + + int numberOfRepayments = 1; + int repaymentEvery = 1; + + // Create Loan Product + PostLoanProductsRequest product = createOnePeriod30DaysLongNoInterestPeriodicAccrualProduct() // + .numberOfRepayments(numberOfRepayments) // + .repaymentEvery(repaymentEvery) // + .repaymentFrequencyType(RepaymentFrequencyType.MONTHS.longValue()); // + + PostLoanProductsResponse loanProductResponse = loanProductHelper.createLoanProduct(product); + Long loanProductId = loanProductResponse.getResourceId(); + + // Apply and Approve Loan + double amount = 1250.0; + + PostLoansRequest applicationRequest = applyLoanRequest(clientId, loanProductId, "01 January 2023", amount, numberOfRepayments)// + .repaymentEvery(repaymentEvery)// + .loanTermFrequency(numberOfRepayments)// + .repaymentFrequencyType(RepaymentFrequencyType.MONTHS)// + .loanTermFrequencyType(RepaymentFrequencyType.MONTHS); + + PostLoansResponse postLoansResponse = loanTransactionHelper.applyLoan(applicationRequest); + + PostLoansLoanIdResponse approvedLoanResult = loanTransactionHelper.approveLoan(postLoansResponse.getResourceId(), + approveLoanRequest(amount, "01 January 2023")); + + Long loanId = approvedLoanResult.getLoanId(); + + // disburse Loan + disburseLoan(loanId, BigDecimal.valueOf(1250.0), "01 January 2023"); + + // verify transactions + verifyTransactions(loanId, // + transaction(1250.0, "Disbursement", "01 January 2023") // + ); + + // verify schedule + verifyRepaymentSchedule(loanId, // + installment(0, null, "01 January 2023"), // + installment(1250.0, false, "01 February 2023") // + ); + + createdLoanId.set(loanId); + }); + + runAt("02 February 2023", () -> { + long loanId = createdLoanId.get(); + + // create re-age transaction + reAgeLoan(loanId, RepaymentFrequencyType.MONTHS_STRING, "02 February 2023", 6); + + // verify transactions + verifyTransactions(loanId, // + transaction(1250.0, "Disbursement", "01 January 2023"), // + transaction(1250.0, "Re-age", "02 February 2023") // + ); + + // TODO: verify installments when schedule generation is implemented + }); + } + + @Test + public void test_LoanUndoReAgeTransaction_Works() { + AtomicLong createdLoanId = new AtomicLong(); + + runAt("01 January 2023", () -> { + // Create Client + Long clientId = clientHelper.createClient(ClientHelper.defaultClientCreationRequest()).getClientId(); + + int numberOfRepayments = 1; + int repaymentEvery = 1; + + // Create Loan Product + PostLoanProductsRequest product = createOnePeriod30DaysLongNoInterestPeriodicAccrualProduct() // + .numberOfRepayments(numberOfRepayments) // + .repaymentEvery(repaymentEvery) // + .repaymentFrequencyType(RepaymentFrequencyType.MONTHS.longValue()); // + + PostLoanProductsResponse loanProductResponse = loanProductHelper.createLoanProduct(product); + Long loanProductId = loanProductResponse.getResourceId(); + + // Apply and Approve Loan + double amount = 1250.0; + + PostLoansRequest applicationRequest = applyLoanRequest(clientId, loanProductId, "01 January 2023", amount, numberOfRepayments)// + .repaymentEvery(repaymentEvery)// + .loanTermFrequency(numberOfRepayments)// + .repaymentFrequencyType(RepaymentFrequencyType.MONTHS)// + .loanTermFrequencyType(RepaymentFrequencyType.MONTHS); + + PostLoansResponse postLoansResponse = loanTransactionHelper.applyLoan(applicationRequest); + + PostLoansLoanIdResponse approvedLoanResult = loanTransactionHelper.approveLoan(postLoansResponse.getResourceId(), + approveLoanRequest(amount, "01 January 2023")); + + Long loanId = approvedLoanResult.getLoanId(); + + // disburse Loan + disburseLoan(loanId, BigDecimal.valueOf(1250.0), "01 January 2023"); + + // verify transactions + verifyTransactions(loanId, // + transaction(1250.0, "Disbursement", "01 January 2023") // + ); + + // verify schedule + verifyRepaymentSchedule(loanId, // + installment(0, null, "01 January 2023"), // + installment(1250.0, false, "01 February 2023") // + ); + + createdLoanId.set(loanId); + }); + + runAt("02 February 2023", () -> { + long loanId = createdLoanId.get(); + + // create re-age transaction + reAgeLoan(loanId, RepaymentFrequencyType.MONTHS_STRING, "02 February 2023", 6); + + // verify transactions + verifyTransactions(loanId, // + transaction(1250.0, "Disbursement", "01 January 2023"), // + transaction(1250.0, "Re-age", "02 February 2023") // + ); + }); + + runAt("03 February 2023", () -> { + long loanId = createdLoanId.get(); + + // create re-age transaction + undoReAgeLoan(loanId); + + // verify transactions + verifyTransactions(loanId, // + transaction(1250.0, "Disbursement", "01 January 2023"), // + reversedTransaction(1250.0, "Re-age", "02 February 2023") // + ); + + // TODO: verify installments when schedule generation is implemented + }); + } +}