FINERACT-2090: Restructure Withdraw Loan Application Logic

This commit is contained in:
Ruchi Dhamankar
2024-06-20 14:58:39 +02:00
committed by Adam Saghy
parent 0292171f6f
commit 1c53fe2136
4 changed files with 129 additions and 123 deletions
@@ -42,7 +42,6 @@ import jakarta.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.math.MathContext;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Collection;
@@ -1741,54 +1740,6 @@ public class Loan extends AbstractAuditableWithUTCDateTimeCustom<Long> {
return maturityDate;
}
public Map<String, Object> loanApplicationWithdrawnByApplicant(final AppUser currentUser, final JsonCommand command,
final LoanLifecycleStateMachine loanLifecycleStateMachine) {
final Map<String, Object> actualChanges = new LinkedHashMap<>();
final LoanStatus statusEnum = loanLifecycleStateMachine.dryTransition(LoanEvent.LOAN_WITHDRAWN, this);
if (!statusEnum.hasStateOf(LoanStatus.fromInt(this.loanStatus))) {
loanLifecycleStateMachine.transition(LoanEvent.LOAN_WITHDRAWN, this);
actualChanges.put(PARAM_STATUS, LoanEnumerations.status(this.loanStatus));
LocalDate withdrawnOn = command.localDateValueOfParameterNamed(WITHDRAWN_ON_DATE);
if (withdrawnOn == null) {
withdrawnOn = command.localDateValueOfParameterNamed(EVENT_DATE);
}
final Locale locale = new Locale(command.locale());
final DateTimeFormatter fmt = DateTimeFormatter.ofPattern(command.dateFormat()).withLocale(locale);
this.withdrawnOnDate = withdrawnOn;
this.withdrawnBy = currentUser;
this.closedOnDate = withdrawnOn;
this.closedBy = currentUser;
actualChanges.put(LOCALE, command.locale());
actualChanges.put(DATE_FORMAT, command.dateFormat());
actualChanges.put(WITHDRAWN_ON_DATE, withdrawnOn.format(fmt));
actualChanges.put(CLOSED_ON_DATE, withdrawnOn.format(fmt));
if (DateUtils.isBefore(withdrawnOn, getSubmittedOnDate())) {
final String errorMessage = "The date on which a loan is withdrawn cannot be before its submittal date: "
+ getSubmittedOnDate().toString();
throw new InvalidLoanStateTransitionException("withdraw", "cannot.be.before.submittal.date", errorMessage, command,
getSubmittedOnDate());
}
validateActivityNotBeforeClientOrGroupTransferDate(LoanEvent.LOAN_WITHDRAWN, withdrawnOn);
if (DateUtils.isDateInTheFuture(withdrawnOn)) {
final String errorMessage = "The date on which a loan is withdrawn cannot be in the future.";
throw new InvalidLoanStateTransitionException("withdraw", "cannot.be.a.future.date", errorMessage, command);
}
} else {
final String errorMessage = "Only the loan applications with status 'Submitted and pending approval' are allowed to be withdrawn by applicant.";
throw new InvalidLoanStateTransitionException("withdraw", "cannot.withdraw", errorMessage);
}
return actualChanges;
}
public Map<String, Object> loanApplicationApproval(final AppUser currentUser, final JsonCommand command,
final JsonArray disbursementDataArray, final LoanLifecycleStateMachine loanLifecycleStateMachine) {
validateAccountStatus(LoanEvent.LOAN_APPROVED);
@@ -4278,11 +4229,6 @@ public class Loan extends AbstractAuditableWithUTCDateTimeCustom<Long> {
action = "repayment.or.waiver";
postfix = "cannot.be.made.before.client.transfer.date";
}
case LOAN_WITHDRAWN -> {
errorMessage = "The date on which a loan is withdrawn cannot be earlier than client's transfer date to this office";
action = "withdraw";
postfix = "cannot.be.before.client.transfer.date";
}
case WRITE_OFF_OUTSTANDING -> {
errorMessage = "The date on which a write off is made cannot be earlier than client's transfer date to this office";
action = "writeoff";
@@ -4478,14 +4424,6 @@ public class Loan extends AbstractAuditableWithUTCDateTimeCustom<Long> {
dataValidationErrors.add(error);
}
}
case LOAN_WITHDRAWN -> {
if (!isSubmittedAndPendingApproval()) {
final String defaultUserMessage = "Loan application cannot be withdrawn. Loan Account is not in Submitted and Pending approval state.";
final ApiParameterError error = ApiParameterError
.generalError("error.msg.loan.withdrawn.account.is.not.submitted.pending.approval.state", defaultUserMessage);
dataValidationErrors.add(error);
}
}
case WRITE_OFF_OUTSTANDING -> {
if (!isOpen()) {
final String defaultUserMessage = "Loan Written off is not allowed. Loan Account is not active.";
@@ -29,6 +29,7 @@ import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.apache.fineract.infrastructure.core.api.JsonCommand;
import org.apache.fineract.infrastructure.core.data.ApiParameterError;
@@ -47,18 +48,14 @@ import org.apache.fineract.portfolio.loanaccount.domain.LoanEvent;
import org.apache.fineract.portfolio.loanaccount.domain.LoanLifecycleStateMachine;
import org.apache.fineract.portfolio.loanaccount.domain.LoanStatus;
import org.apache.fineract.portfolio.loanaccount.exception.InvalidLoanStateTransitionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
@RequiredArgsConstructor
public final class LoanApplicationTransitionValidator {
private final FromJsonHelper fromApiJsonHelper;
@Autowired
public LoanApplicationTransitionValidator(final FromJsonHelper fromApiJsonHelper) {
this.fromApiJsonHelper = fromApiJsonHelper;
}
private final LoanLifecycleStateMachine defaultLoanLifecycleStateMachine;
private void throwExceptionIfValidationWarningsExist(final List<ApiParameterError> dataValidationErrors) {
if (!dataValidationErrors.isEmpty()) {
@@ -112,13 +109,13 @@ public final class LoanApplicationTransitionValidator {
throwExceptionIfValidationWarningsExist(dataValidationErrors);
}
public void validateRejection(final JsonCommand command, final Loan loan, final LoanLifecycleStateMachine loanLifecycleStateMachine) {
public void validateRejection(final JsonCommand command, final Loan loan) {
// validate request body
final String json = command.json();
validateLoanRejectionRequestBody(json);
// validate loan rejection
validateLoanRejection(command, loan, loanLifecycleStateMachine);
validateLoanRejection(command, loan);
}
private void validateLoanRejectionRequestBody(String json) {
@@ -146,8 +143,7 @@ public final class LoanApplicationTransitionValidator {
throwExceptionIfValidationWarningsExist(dataValidationErrors);
}
private void validateLoanRejection(final JsonCommand command, final Loan loan,
final LoanLifecycleStateMachine loanLifecycleStateMachine) {
private void validateLoanRejection(final JsonCommand command, final Loan loan) {
// validate client or group is active
checkClientOrGroupActive(loan);
@@ -156,7 +152,7 @@ public final class LoanApplicationTransitionValidator {
// validate loan state transition
final LoanStatus statusEnum = loanLifecycleStateMachine.dryTransition(LoanEvent.LOAN_REJECTED, loan);
final LoanStatus statusEnum = defaultLoanLifecycleStateMachine.dryTransition(LoanEvent.LOAN_REJECTED, loan);
if (!statusEnum.hasStateOf(LoanStatus.fromInt(loan.getLoanStatus()))) {
final LocalDate rejectedOn = command.localDateValueOfParameterNamed(Loan.REJECTED_ON_DATE);
if (DateUtils.isBefore(rejectedOn, loan.getSubmittedOnDate())) {
@@ -178,8 +174,16 @@ public final class LoanApplicationTransitionValidator {
}
}
public void validateApplicantWithdrawal(final String json) {
public void validateApplicantWithdrawal(final JsonCommand command, final Loan loan) {
// validate request body
final String json = command.json();
validateApplicantWithdrawalRequestBody(json);
// validate Loan application withdrawal by applicant
validateLoanApplicantWithdrawal(command, loan);
}
private void validateApplicantWithdrawalRequestBody(String json) {
if (StringUtils.isBlank(json)) {
throw new InvalidJsonException();
}
@@ -204,6 +208,37 @@ public final class LoanApplicationTransitionValidator {
throwExceptionIfValidationWarningsExist(dataValidationErrors);
}
private void validateLoanApplicantWithdrawal(final JsonCommand command, final Loan loan) {
// validate client or group is active
checkClientOrGroupActive(loan);
// validate loan state transition
final LoanStatus statusEnum = defaultLoanLifecycleStateMachine.dryTransition(LoanEvent.LOAN_WITHDRAWN, loan);
if (!statusEnum.hasStateOf(LoanStatus.fromInt(loan.getLoanStatus()))) {
LocalDate withdrawnOn = command.localDateValueOfParameterNamed(Loan.WITHDRAWN_ON_DATE);
if (withdrawnOn == null) {
withdrawnOn = command.localDateValueOfParameterNamed(Loan.EVENT_DATE);
}
if (DateUtils.isBefore(withdrawnOn, loan.getSubmittedOnDate())) {
final String errorMessage = "The date on which a loan is withdrawn cannot be before its submittal date: "
+ loan.getSubmittedOnDate().toString();
throw new InvalidLoanStateTransitionException("withdraw", "cannot.be.before.submittal.date", errorMessage, command,
loan.getSubmittedOnDate());
}
validateActivityNotBeforeClientOrGroupTransferDate(loan, LoanEvent.LOAN_WITHDRAWN, withdrawnOn);
if (DateUtils.isDateInTheFuture(withdrawnOn)) {
final String errorMessage = "The date on which a loan is withdrawn cannot be in the future.";
throw new InvalidLoanStateTransitionException("withdraw", "cannot.be.a.future.date", errorMessage, command);
}
} else {
final String errorMessage = "Only the loan applications with status 'Submitted and pending approval' are allowed to be withdrawn by applicant.";
throw new InvalidLoanStateTransitionException("withdraw", "cannot.withdraw", errorMessage);
}
}
private void validateActivityNotBeforeClientOrGroupTransferDate(final Loan loan, final LoanEvent event, final LocalDate activityDate) {
if (loan.getClient() != null && loan.getClient().getOfficeJoiningDate() != null) {
final LocalDate clientOfficeJoiningDate = loan.getClient().getOfficeJoiningDate();
@@ -217,6 +252,11 @@ public final class LoanApplicationTransitionValidator {
action = "reject";
postfix = "cannot.be.before.client.transfer.date";
}
case LOAN_WITHDRAWN -> {
errorMessage = "The date on which a loan is withdrawn cannot be earlier than client's transfer date to this office";
action = "withdraw";
postfix = "cannot.be.before.client.transfer.date";
}
default -> {
}
}