FINERACT-1971: Loan re-aging foundational implementation

This commit is contained in:
Arnold Galovics
2024-02-20 15:43:49 +01:00
committed by Arnold Gálovics
parent 0d06e2bb2c
commit 4d4502fbe0
30 changed files with 1003 additions and 19 deletions
+3
View File
@@ -26,6 +26,9 @@
<Match>
<Package name="~.*\.domain"/>
</Match>
<Match>
<Package name="~.*\.domain\..*"/>
</Match>
<Match>
<Package name="~.*\.data"/>
</Match>
+27
View File
@@ -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
@@ -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";
@@ -389,6 +389,19 @@ public final class JsonCommand {
return isChanged;
}
public <T extends Enum<T>> T enumValueOfParameterNamed(String parameterName, Class<T> 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, "");
@@ -62,11 +62,9 @@ public class BusinessEventNotifierServiceImpl implements BusinessEventNotifierSe
@Override
public void notifyPreBusinessEvent(BusinessEvent<?> businessEvent) {
throwExceptionIfBulkEvent(businessEvent);
List<BusinessEventListener> businessEventListeners = preListeners.get(businessEvent.getClass());
if (businessEventListeners != null) {
for (BusinessEventListener eventListener : businessEventListeners) {
eventListener.onBusinessEvent(businessEvent);
}
List<BusinessEventListener> 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<BusinessEventListener> businessEventListeners = postListeners.get(businessEvent.getClass());
if (businessEventListeners != null) {
for (BusinessEventListener eventListener : businessEventListeners) {
eventListener.onBusinessEvent(businessEvent);
}
List<BusinessEventListener> 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<BusinessEventListener> findSuitableListeners(Map<Class, List<BusinessEventListener>> listeners, Class<?> eventClazz) {
List<BusinessEventListener> result = new ArrayList<>();
for (Map.Entry<Class, List<BusinessEventListener>> entry : listeners.entrySet()) {
Class<?> registeredClazz = entry.getKey();
if (registeredClazz.isAssignableFrom(eventClazz)) {
result.addAll(entry.getValue());
}
}
return result;
}
@Override
public <T extends BusinessEvent<?>> void addPostBusinessEventListener(Class<T> eventType, BusinessEventListener<T> listener) {
List<BusinessEventListener> businessEventListeners = postListeners.get(eventType);
@@ -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";
}
@@ -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() {
@@ -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());
@@ -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);
}
@@ -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);
}
@@ -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() {}
}
@@ -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<LoanReAgeParameter, Long> {
Optional<LoanReAgeParameter> findByLoanTransactionId(@Param("loanTransactionId") Long loanTransactionId);
}
@@ -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");
};
}
@@ -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;
}
}
@@ -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;
}
}
@@ -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<String> 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) {
@@ -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")
@@ -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();
}
}
}
@@ -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();
}
}
}
@@ -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<LoanTransactionBusinessEvent> {
// Extend this list to support more event types so the hardcoded delinquency recalculation can be removed from the
// use-cases
private static final List<Class<? extends LoanTransactionBusinessEvent>> 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());
}
}
}
@@ -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<String, Object> 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<String, Object> 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);
}
}
@@ -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
}
}
@@ -154,4 +154,6 @@
<include file="parts/0132_add_configuration_loan_next_repayment_date_calculation.xml" relativeToChangelogFile="true" />
<include file="parts/0133_transaction_summary_with_asset_owner_report_recovery_repayments_chargeoff_reason.xml" relativeToChangelogFile="true" />
<include file="parts/0134_transaction_summary_with_asset_owner_report_down_payment_amount_fix.xml" relativeToChangelogFile="true" />
<include file="parts/0135_add_external_event_for_loan_reaging.xml" relativeToChangelogFile="true" />
<include file="parts/0136_loan_reaging_parameters.xml" relativeToChangelogFile="true" />
</databaseChangeLog>
@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
-->
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.1.xsd">
<changeSet author="fineract" id="1">
<insert tableName="m_external_event_configuration">
<column name="type" value="LoanReAgeTransactionBusinessEvent"/>
<column name="enabled" valueBoolean="false"/>
</insert>
</changeSet>
<changeSet author="fineract" id="2">
<insert tableName="m_external_event_configuration">
<column name="type" value="LoanUndoReAgeTransactionBusinessEvent"/>
<column name="enabled" valueBoolean="false"/>
</insert>
</changeSet>
</databaseChangeLog>
@@ -0,0 +1,74 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
-->
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.1.xsd">
<changeSet author="fineract" id="1">
<createTable tableName="m_loan_reage_parameter">
<column autoIncrement="true" name="id" type="BIGINT">
<constraints nullable="false" primaryKey="true"/>
</column>
<column name="frequency" type="VARCHAR(100)">
<constraints nullable="false"/>
</column>
<column name="number_of_installments" type="SMALLINT">
<constraints nullable="false"/>
</column>
<column name="start_date" type="DATE">
<constraints nullable="false"/>
</column>
<column name="created_by" type="BIGINT">
<constraints nullable="false"/>
</column>
<column name="last_modified_by" type="BIGINT">
<constraints nullable="false"/>
</column>
</createTable>
</changeSet>
<changeSet id="2-mysql" author="fineract" context="mysql">
<addColumn tableName="m_loan_reage_parameter">
<column name="created_on_utc" type="DATETIME(6)">
<constraints nullable="false"/>
</column>
<column name="last_modified_on_utc" type="DATETIME(6)">
<constraints nullable="false"/>
</column>
</addColumn>
</changeSet>
<changeSet id="2-postgresql" author="fineract" context="postgresql">
<addColumn tableName="m_loan_reage_parameter">
<column name="created_on_utc" type="TIMESTAMP WITH TIME ZONE">
<constraints nullable="false"/>
</column>
<column name="last_modified_on_utc" type="TIMESTAMP WITH TIME ZONE">
<constraints nullable="false"/>
</column>
</addColumn>
</changeSet>
<changeSet id="3" author="fineract">
<addColumn tableName="m_loan_reage_parameter">
<column name="loan_transaction_id" type="BIGINT">
<constraints nullable="false"/>
</column>
</addColumn>
</changeSet>
</databaseChangeLog>
@@ -98,7 +98,8 @@ public class ExternalEventConfigurationValidationServiceTest {
"LoanChargeOffPostBusinessEvent", "LoanUndoChargeOffBusinessEvent", "LoanAccrualTransactionCreatedBusinessEvent",
"LoanRescheduledDueAdjustScheduleBusinessEvent", "LoanOwnershipTransferBusinessEvent", "LoanAccountSnapshotBusinessEvent",
"LoanTransactionDownPaymentPostBusinessEvent", "LoanTransactionDownPaymentPreBusinessEvent",
"LoanAccountDelinquencyPauseChangedBusinessEvent", "LoanAccountCustomSnapshotBusinessEvent");
"LoanAccountDelinquencyPauseChangedBusinessEvent", "LoanAccountCustomSnapshotBusinessEvent",
"LoanReAgeTransactionBusinessEvent", "LoanUndoReAgeTransactionBusinessEvent");
List<FineractPlatformTenant> 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<FineractPlatformTenant> tenants = Arrays
.asList(new FineractPlatformTenant(1L, "default", "Default Tenant", "Europe/Budapest", null));
@@ -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<GetLoansLoanIdTransactions> 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 {
@@ -500,6 +500,16 @@ public class ExternalEventConfigurationHelper {
loanAccountCustomSnapshotBusinessEvent.put("enabled", false);
defaults.add(loanAccountCustomSnapshotBusinessEvent);
Map<String, Object> loanReAgeTransactionBusinessEvent = new HashMap<>();
loanReAgeTransactionBusinessEvent.put("type", "LoanReAgeTransactionBusinessEvent");
loanReAgeTransactionBusinessEvent.put("enabled", false);
defaults.add(loanReAgeTransactionBusinessEvent);
Map<String, Object> loanUndoReAgeTransactionBusinessEvent = new HashMap<>();
loanUndoReAgeTransactionBusinessEvent.put("type", "LoanUndoReAgeTransactionBusinessEvent");
loanUndoReAgeTransactionBusinessEvent.put("enabled", false);
defaults.add(loanUndoReAgeTransactionBusinessEvent);
return defaults;
}
@@ -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 --------------------------------");
@@ -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
});
}
}