Skip to content

Commit

Permalink
FINERACT-2107: Extract Merchant issued refund and Payout refund handling
Browse files Browse the repository at this point in the history
  • Loading branch information
adamsaghy committed Oct 24, 2024
1 parent 393f439 commit 4198b0b
Show file tree
Hide file tree
Showing 10 changed files with 386 additions and 236 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2352,7 +2352,7 @@ private ChangedTransactionDetail handleRepaymentOrRecoveryOrWaiverTransaction(fi

loanTransaction.updateLoan(this);

final boolean isTransactionChronologicallyLatest = isChronologicallyLatestRepaymentOrWaiver(loanTransaction, getLoanTransactions());
final boolean isTransactionChronologicallyLatest = isChronologicallyLatestRepaymentOrWaiver(loanTransaction);

if (loanTransaction.isNotZero(loanCurrency())) {
addLoanTransaction(loanTransaction);
Expand Down Expand Up @@ -2396,7 +2396,7 @@ private ChangedTransactionDetail handleRepaymentOrRecoveryOrWaiverTransaction(fi
final LoanRepaymentScheduleTransactionProcessor loanRepaymentScheduleTransactionProcessor = this.transactionProcessorFactory
.determineProcessor(this.transactionProcessingStrategyCode);

final LoanRepaymentScheduleInstallment currentInstallment = fetchLoanRepaymentScheduleInstallment(
final LoanRepaymentScheduleInstallment currentInstallment = fetchLoanRepaymentScheduleInstallmentByDueDate(
loanTransaction.getTransactionDate());

boolean reprocess = isForeclosure() || !isTransactionChronologicallyLatest || adjustedTransaction != null
Expand Down Expand Up @@ -2471,7 +2471,7 @@ private LocalDate extractTransactionDate(LoanTransaction loanTransaction) {
return loanTransactionDate;
}

public LoanRepaymentScheduleInstallment fetchLoanRepaymentScheduleInstallment(LocalDate dueDate) {
public LoanRepaymentScheduleInstallment fetchLoanRepaymentScheduleInstallmentByDueDate(LocalDate dueDate) {
return getRepaymentScheduleInstallments().stream() //
.filter(installment -> dueDate.equals(installment.getDueDate())).findFirst() //
.orElse(null);
Expand Down Expand Up @@ -2504,8 +2504,7 @@ public List<LoanTransaction> retrieveListOfTransactionsByType(final LoanTransact
.sorted(LoanTransactionComparator.INSTANCE).collect(Collectors.toList());
}

private boolean doPostLoanTransactionChecks(final LocalDate transactionDate,
final LoanLifecycleStateMachine loanLifecycleStateMachine) {
public boolean doPostLoanTransactionChecks(final LocalDate transactionDate, final LoanLifecycleStateMachine loanLifecycleStateMachine) {
boolean statusChanged = false;
boolean isOverpaid = MathUtil.isGreaterThanZero(totalOverpaid);
if (isOverpaid) {
Expand Down Expand Up @@ -2549,8 +2548,7 @@ private void handleLoanOverpayment(LocalDate transactionDate, final LoanLifecycl
this.actualMaturityDate = null;
}

private boolean isChronologicallyLatestRepaymentOrWaiver(final LoanTransaction loanTransaction,
final List<LoanTransaction> loanTransactions) {
public boolean isChronologicallyLatestRepaymentOrWaiver(final LoanTransaction loanTransaction) {
boolean isChronologicallyLatestRepaymentOrWaiver = true;

final LocalDate currentTransactionDate = loanTransaction.getTransactionDate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.fineract.infrastructure.core.data.CommandProcessingResultBuilder;
import org.apache.fineract.infrastructure.core.domain.ExternalId;
import org.apache.fineract.portfolio.delinquency.validator.LoanDelinquencyActionData;
import org.apache.fineract.portfolio.loanaccount.data.HolidayDetailDTO;
import org.apache.fineract.portfolio.loanaccount.data.LoanRefundRequestData;
import org.apache.fineract.portfolio.loanaccount.data.ScheduleGeneratorDTO;
import org.apache.fineract.portfolio.paymentdetail.domain.PaymentDetail;

public interface LoanAccountDomainService {
Expand All @@ -54,8 +56,6 @@ LoanTransaction makeDisburseTransaction(Long loanId, LocalDate transactionDate,
LoanTransaction makeRefundForActiveLoan(Long accountId, CommandProcessingResultBuilder builderResult, LocalDate transactionDate,
BigDecimal transactionAmount, PaymentDetail paymentDetail, String noteText, ExternalId txnExternalId);

void updateLoanCollateralTransaction(Set<LoanCollateralManagement> loanCollateralManagementList);

void updateLoanCollateralStatus(Set<LoanCollateralManagement> loanCollateralManagementSet, boolean isReleased);

/**
Expand Down Expand Up @@ -94,6 +94,13 @@ LoanTransaction foreCloseLoan(Loan loan, LocalDate foreClourseDate, String noteT
LoanTransaction creditBalanceRefund(Loan loan, LocalDate transactionDate, BigDecimal transactionAmount, String noteText,
ExternalId externalId, PaymentDetail paymentDetail);

Pair<LoanTransaction, LoanTransaction> makeRefund(Loan loan, ScheduleGeneratorDTO scheduleGeneratorDTO,
LoanTransactionType loanTransactionType, LocalDate transactionDate, BigDecimal transactionAmount, PaymentDetail paymentDetail,
ExternalId txnExternalId);

void updateAndSavePostDatedChecksForIndividualAccount(Loan loan, LoanTransaction transaction);

LoanTransaction applyInterestRefund(Loan loan, LoanRefundRequestData loanRefundRequest);

void updateAndSaveLoanCollateralTransactionsForIndividualAccounts(Loan loan, LoanTransaction transaction);
}
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,12 @@ public static LoanTransaction downPayment(final Office office, final Money amoun
externalId);
}

public static LoanTransaction refund(Loan loan, LoanTransactionType loanTransactionType, BigDecimal transactionAmount,
PaymentDetail paymentDetail, LocalDate transactionDate, ExternalId txnExternalId) {
return new LoanTransaction(loan, loan.getOffice(), loanTransactionType, paymentDetail, transactionAmount, transactionDate,
txnExternalId);
}

public void setLoanTransactionToRepaymentScheduleMappings(final Integer installmentId, final BigDecimal chargePerInstallment) {
for (LoanTransactionToRepaymentScheduleMapping loanTransactionToRepaymentScheduleMapping : this.loanTransactionToRepaymentScheduleMappings) {
final LoanRepaymentScheduleInstallment loanRepaymentScheduleInstallment = loanTransactionToRepaymentScheduleMapping
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,10 @@
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.domain.LoanTransactionType;
import org.apache.fineract.portfolio.loanaccount.service.LoanWritePlatformService;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.orm.jpa.JpaSystemException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -37,19 +34,10 @@
public class LoanMerchantIssuedRefundCommandHandler implements NewCommandSourceHandler {

private final LoanWritePlatformService writePlatformService;
private final DataIntegrityErrorHandler dataIntegrityErrorHandler;

@Transactional
@Override
public CommandProcessingResult processCommand(final JsonCommand command) {
try {
boolean isRecoveryRepayment = false;
return this.writePlatformService.makeLoanRepayment(LoanTransactionType.MERCHANT_ISSUED_REFUND, command.getLoanId(), command,
isRecoveryRepayment);
} catch (final JpaSystemException | DataIntegrityViolationException dve) {
dataIntegrityErrorHandler.handleDataIntegrityIssues(command, dve.getMostSpecificCause(), dve, "loan.merchantIssuedRefund",
"Merchant Issued Refund");
return CommandProcessingResult.empty();
}
return this.writePlatformService.makeRefund(command.getLoanId(), LoanTransactionType.MERCHANT_ISSUED_REFUND, command);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,10 @@
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.domain.LoanTransactionType;
import org.apache.fineract.portfolio.loanaccount.service.LoanWritePlatformService;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.orm.jpa.JpaSystemException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -37,19 +34,10 @@
public class LoanPayoutRefundCommandHandler implements NewCommandSourceHandler {

private final LoanWritePlatformService writePlatformService;
private final DataIntegrityErrorHandler dataIntegrityErrorHandler;

@Transactional
@Override
public CommandProcessingResult processCommand(final JsonCommand command) {
try {
boolean isRecoveryRepayment = false;
return this.writePlatformService.makeLoanRepayment(LoanTransactionType.PAYOUT_REFUND, command.getLoanId(), command,
isRecoveryRepayment);
} catch (final JpaSystemException | DataIntegrityViolationException dve) {
dataIntegrityErrorHandler.handleDataIntegrityIssues(command, dve.getMostSpecificCause(), dve, "loan.payoutRefund",
"Payout Refund");
return CommandProcessingResult.empty();
}
return this.writePlatformService.makeRefund(command.getLoanId(), LoanTransactionType.PAYOUT_REFUND, command);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,6 @@ void applyMeetingDateChanges(Calendar calendar, Collection<CalendarInstance> loa

@Transactional
CommandProcessingResult undoChargeOff(JsonCommand command);

CommandProcessingResult makeRefund(Long loanId, LoanTransactionType loanTransactionType, JsonCommand command);
}
Loading

0 comments on commit 4198b0b

Please sign in to comment.