Skip to content

Commit

Permalink
FINERACT-1968: Advanced payment allocation - Default allocation
Browse files Browse the repository at this point in the history
  • Loading branch information
adamsaghy committed Sep 5, 2023
1 parent 26eca0f commit 6d93899
Show file tree
Hide file tree
Showing 14 changed files with 2,163 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import jakarta.ws.rs.ext.ExceptionMapper;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

Expand All @@ -40,7 +41,8 @@ public int errorCode() {

@Override
public Response toResponse(RuntimeException runtimeException) {
return Response.status(SC_INTERNAL_SERVER_ERROR).entity(Map.of("Exception", runtimeException.getMessage()))
return Response.status(SC_INTERNAL_SERVER_ERROR)
.entity(Map.of("Exception", ObjectUtils.defaultIfNull(runtimeException.getMessage(), "No error message available")))
.type(MediaType.APPLICATION_JSON).build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,14 @@ public static Money min(Money first, Money second, Money third, boolean notNull)
return min(min(first, second, notNull), third, notNull);
}

/**
* Calculate percentage of a value
*
* @param value
* @param percentage
* @param precision
* @return
*/
public static BigDecimal percentageOf(final BigDecimal value, final BigDecimal percentage, final int precision) {
BigDecimal percentageOf = BigDecimal.ZERO;
if (isGreaterThanZero(value)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -702,7 +702,7 @@ public ChangedTransactionDetail reprocessTransactions() {
final LoanRepaymentScheduleTransactionProcessor loanRepaymentScheduleTransactionProcessor = this.transactionProcessorFactory
.determineProcessor(this.transactionProcessingStrategyCode);
final List<LoanTransaction> allNonContraTransactionsPostDisbursement = retrieveListOfTransactionsPostDisbursement();
changedTransactionDetail = loanRepaymentScheduleTransactionProcessor.reprocessLoanTranactions(getDisbursementDate(),
changedTransactionDetail = loanRepaymentScheduleTransactionProcessor.reprocessLoanTransactions(getDisbursementDate(),
allNonContraTransactionsPostDisbursement, getCurrency(), getRepaymentScheduleInstallments(), getActiveCharges());
for (final Map.Entry<Long, LoanTransaction> mapEntry : changedTransactionDetail.getNewTransactionMappings().entrySet()) {

Expand Down Expand Up @@ -863,7 +863,7 @@ public void removeLoanCharge(final LoanCharge loanCharge) {
* affected Transactions
***/
final List<LoanTransaction> allNonContraTransactionsPostDisbursement = retrieveListOfTransactionsPostDisbursement();
loanRepaymentScheduleTransactionProcessor.reprocessLoanTranactions(getDisbursementDate(),
loanRepaymentScheduleTransactionProcessor.reprocessLoanTransactions(getDisbursementDate(),
allNonContraTransactionsPostDisbursement, getCurrency(), getRepaymentScheduleInstallments(), getActiveCharges());
}
this.charges.remove(loanCharge);
Expand Down Expand Up @@ -931,7 +931,7 @@ public Map<String, Object> updateLoanCharge(final LoanCharge loanCharge, final J
* affected Transactions
***/
final List<LoanTransaction> allNonContraTransactionsPostDisbursement = retrieveListOfTransactionsPostDisbursement();
loanRepaymentScheduleTransactionProcessor.reprocessLoanTranactions(getDisbursementDate(),
loanRepaymentScheduleTransactionProcessor.reprocessLoanTransactions(getDisbursementDate(),
allNonContraTransactionsPostDisbursement, getCurrency(), getRepaymentScheduleInstallments(), getActiveCharges());
} else {
// reprocess loan schedule based on charge been waived.
Expand Down Expand Up @@ -1124,7 +1124,7 @@ public LoanTransaction waiveLoanCharge(final LoanCharge loanCharge, final LoanLi
* affected Transactions
***/
final List<LoanTransaction> allNonContraTransactionsPostDisbursement = retrieveListOfTransactionsPostDisbursement();
loanRepaymentScheduleTransactionProcessor.reprocessLoanTranactions(getDisbursementDate(),
loanRepaymentScheduleTransactionProcessor.reprocessLoanTransactions(getDisbursementDate(),
allNonContraTransactionsPostDisbursement, getCurrency(), getRepaymentScheduleInstallments(), getActiveCharges());
} else {
// reprocess loan schedule based on charge been waived.
Expand Down Expand Up @@ -2706,7 +2706,7 @@ private ChangedTransactionDetail reprocessTransactionForDisbursement() {
if (!allNonContraTransactionsPostDisbursement.isEmpty()) {
final LoanRepaymentScheduleTransactionProcessor loanRepaymentScheduleTransactionProcessor = this.transactionProcessorFactory
.determineProcessor(this.transactionProcessingStrategyCode);
changedTransactionDetail = loanRepaymentScheduleTransactionProcessor.reprocessLoanTranactions(getDisbursementDate(),
changedTransactionDetail = loanRepaymentScheduleTransactionProcessor.reprocessLoanTransactions(getDisbursementDate(),
allNonContraTransactionsPostDisbursement, getCurrency(), getRepaymentScheduleInstallments(), getActiveCharges());
for (final Map.Entry<Long, LoanTransaction> mapEntry : changedTransactionDetail.getNewTransactionMappings().entrySet()) {
mapEntry.getValue().updateLoan(this);
Expand Down Expand Up @@ -3351,7 +3351,7 @@ private ChangedTransactionDetail handleRepaymentOrRecoveryOrWaiverTransaction(fi
regenerateRepaymentScheduleWithInterestRecalculation(scheduleGeneratorDTO);
}
final List<LoanTransaction> allNonContraTransactionsPostDisbursement = retrieveListOfTransactionsPostDisbursement();
changedTransactionDetail = loanRepaymentScheduleTransactionProcessor.reprocessLoanTranactions(getDisbursementDate(),
changedTransactionDetail = loanRepaymentScheduleTransactionProcessor.reprocessLoanTransactions(getDisbursementDate(),
allNonContraTransactionsPostDisbursement, getCurrency(), getRepaymentScheduleInstallments(), getActiveCharges());
for (final Map.Entry<Long, LoanTransaction> mapEntry : changedTransactionDetail.getNewTransactionMappings().entrySet()) {
mapEntry.getValue().updateLoan(this);
Expand Down Expand Up @@ -3779,7 +3779,7 @@ public ChangedTransactionDetail undoWrittenOff(LoanLifecycleStateMachine loanLif
if (this.repaymentScheduleDetail().isInterestRecalculationEnabled()) {
regenerateRepaymentScheduleWithInterestRecalculation(scheduleGeneratorDTO);
}
ChangedTransactionDetail changedTransactionDetail = loanRepaymentScheduleTransactionProcessor.reprocessLoanTranactions(
ChangedTransactionDetail changedTransactionDetail = loanRepaymentScheduleTransactionProcessor.reprocessLoanTransactions(
getDisbursementDate(), allNonContraTransactionsPostDisbursement, getCurrency(), getRepaymentScheduleInstallments(),
getActiveCharges());
updateLoanSummaryDerivedFields();
Expand Down Expand Up @@ -3928,7 +3928,7 @@ private ChangedTransactionDetail closeDisbursements(final ScheduleGeneratorDTO s
regenerateRepaymentScheduleWithInterestRecalculation(scheduleGeneratorDTO);
}
final List<LoanTransaction> allNonContraTransactionsPostDisbursement = retrieveListOfTransactionsPostDisbursement();
changedTransactionDetail = loanRepaymentScheduleTransactionProcessor.reprocessLoanTranactions(getDisbursementDate(),
changedTransactionDetail = loanRepaymentScheduleTransactionProcessor.reprocessLoanTransactions(getDisbursementDate(),
allNonContraTransactionsPostDisbursement, getCurrency(), getRepaymentScheduleInstallments(), getActiveCharges());
for (final Map.Entry<Long, LoanTransaction> mapEntry : changedTransactionDetail.getNewTransactionMappings().entrySet()) {
mapEntry.getValue().updateLoan(this);
Expand Down Expand Up @@ -5402,7 +5402,7 @@ public ChangedTransactionDetail updateDisbursementDateAndAmountForTranche(final
final LoanRepaymentScheduleTransactionProcessor loanRepaymentScheduleTransactionProcessor = this.transactionProcessorFactory
.determineProcessor(this.transactionProcessingStrategyCode);
final List<LoanTransaction> allNonContraTransactionsPostDisbursement = retrieveListOfTransactionsPostDisbursement();
ChangedTransactionDetail changedTransactionDetail = loanRepaymentScheduleTransactionProcessor.reprocessLoanTranactions(
ChangedTransactionDetail changedTransactionDetail = loanRepaymentScheduleTransactionProcessor.reprocessLoanTransactions(
getDisbursementDate(), allNonContraTransactionsPostDisbursement, getCurrency(), getRepaymentScheduleInstallments(),
getActiveCharges());
for (final Map.Entry<Long, LoanTransaction> mapEntry : changedTransactionDetail.getNewTransactionMappings().entrySet()) {
Expand Down Expand Up @@ -5545,7 +5545,7 @@ public ChangedTransactionDetail processTransactions() {
final LoanRepaymentScheduleTransactionProcessor loanRepaymentScheduleTransactionProcessor = this.transactionProcessorFactory
.determineProcessor(this.transactionProcessingStrategyCode);
final List<LoanTransaction> allNonContraTransactionsPostDisbursement = retrieveListOfTransactionsPostDisbursement();
ChangedTransactionDetail changedTransactionDetail = loanRepaymentScheduleTransactionProcessor.reprocessLoanTranactions(
ChangedTransactionDetail changedTransactionDetail = loanRepaymentScheduleTransactionProcessor.reprocessLoanTransactions(
getDisbursementDate(), allNonContraTransactionsPostDisbursement, getCurrency(), getRepaymentScheduleInstallments(),
getActiveCharges());
for (final Map.Entry<Long, LoanTransaction> mapEntry : changedTransactionDetail.getNewTransactionMappings().entrySet()) {
Expand Down Expand Up @@ -5788,7 +5788,7 @@ public void processPostDisbursementTransactions() {
for (LoanTransaction loanTransaction : allNonContraTransactionsPostDisbursement) {
copyTransactions.add(LoanTransaction.copyTransactionProperties(loanTransaction));
}
loanRepaymentScheduleTransactionProcessor.reprocessLoanTranactions(getDisbursementDate(), copyTransactions, getCurrency(),
loanRepaymentScheduleTransactionProcessor.reprocessLoanTransactions(getDisbursementDate(), copyTransactions, getCurrency(),
getRepaymentScheduleInstallments(), getActiveCharges());

updateLoanSummaryDerivedFields();
Expand Down Expand Up @@ -6392,7 +6392,7 @@ private ChangedTransactionDetail handleRefundTransaction(final LoanTransaction l
getRepaymentScheduleInstallments(), getActiveCharges(), getTotalOverpaidAsMoney());
} else {
final List<LoanTransaction> allNonContraTransactionsPostDisbursement = retrieveListOfTransactionsPostDisbursement();
changedTransactionDetail = loanRepaymentScheduleTransactionProcessor.reprocessLoanTranactions(getDisbursementDate(),
changedTransactionDetail = loanRepaymentScheduleTransactionProcessor.reprocessLoanTransactions(getDisbursementDate(),
allNonContraTransactionsPostDisbursement, getCurrency(), getRepaymentScheduleInstallments(), getActiveCharges());
for (final Map.Entry<Long, LoanTransaction> mapEntry : changedTransactionDetail.getNewTransactionMappings().entrySet()) {
mapEntry.getValue().updateLoan(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ void processLatestTransaction(LoanTransaction loanTransaction, MonetaryCurrency
* before existing transactions or and adjustment is made to an existing in which case the entire loan schedule
* needs to be re-processed.
*/
ChangedTransactionDetail reprocessLoanTranactions(LocalDate disbursementDate, List<LoanTransaction> repaymentsOrWaivers,
ChangedTransactionDetail reprocessLoanTransactions(LocalDate disbursementDate, List<LoanTransaction> repaymentsOrWaivers,
MonetaryCurrency currency, List<LoanRepaymentScheduleInstallment> repaymentScheduleInstallments, Set<LoanCharge> charges);

Money handleRepaymentSchedule(List<LoanTransaction> transactionsPostDisbursement, MonetaryCurrency currency,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,15 @@
public enum PaymentAllocationTransactionType {

DEFAULT(null), //
REPAYMENT(LoanTransactionType.REPAYMENT); //
REPAYMENT(LoanTransactionType.REPAYMENT), //
DOWN_PAYMENT(LoanTransactionType.DOWN_PAYMENT), //
MERCHANT_ISSUED_REFUND(LoanTransactionType.MERCHANT_ISSUED_REFUND), //
PAYOUT_REFUND(LoanTransactionType.PAYOUT_REFUND), //
GOODWILL_CREDIT(LoanTransactionType.GOODWILL_CREDIT), //
CHARGE_REFUND(LoanTransactionType.CHARGE_REFUND), //
CHARGE_ADJUSTMENT(LoanTransactionType.CHARGE_ADJUSTMENT), //
WAIVE_INTEREST(LoanTransactionType.WAIVE_INTEREST),//
; //

private final LoanTransactionType loanTransactionType;
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public boolean accept(String s) {
}

@Override
public ChangedTransactionDetail reprocessLoanTranactions(final LocalDate disbursementDate,
public ChangedTransactionDetail reprocessLoanTransactions(final LocalDate disbursementDate,
final List<LoanTransaction> transactionsPostDisbursement, final MonetaryCurrency currency,
final List<LoanRepaymentScheduleInstallment> installments, final Set<LoanCharge> charges) {

Expand Down Expand Up @@ -382,7 +382,7 @@ private void reprocessChargebackTransactionRelation(ChangedTransactionDetail cha
}
}

private void reprocessInstallments(List<LoanRepaymentScheduleInstallment> installments, MonetaryCurrency currency) {
protected void reprocessInstallments(List<LoanRepaymentScheduleInstallment> installments, MonetaryCurrency currency) {
LoanRepaymentScheduleInstallment lastInstallment = installments.get(installments.size() - 1);
if (lastInstallment.isAdditional() && lastInstallment.getDue(currency).isZero()) {
installments.remove(lastInstallment);
Expand Down Expand Up @@ -413,7 +413,7 @@ private List<LoanTransaction> getMergedTransactionList(List<LoanTransaction> tra
return mergedList;
}

private void createNewTransaction(LoanTransaction loanTransaction, LoanTransaction newLoanTransaction,
protected void createNewTransaction(LoanTransaction loanTransaction, LoanTransaction newLoanTransaction,
ChangedTransactionDetail changedTransactionDetail) {
loanTransaction.reverse();
loanTransaction.updateExternalId(null);
Expand Down Expand Up @@ -538,7 +538,7 @@ private void processCreditTransaction(LoanTransaction loanTransaction, Money ove
}
}

private Money handleTransactionAndCharges(final LoanTransaction loanTransaction, final MonetaryCurrency currency,
protected Money handleTransactionAndCharges(final LoanTransaction loanTransaction, final MonetaryCurrency currency,
final List<LoanRepaymentScheduleInstallment> installments, final Set<LoanCharge> charges, final Money chargeAmountToProcess,
final boolean isFeeCharge) {
if (loanTransaction.isRepaymentLikeType() || loanTransaction.isInterestWaiver() || loanTransaction.isRecoveryRepayment()) {
Expand Down Expand Up @@ -574,7 +574,7 @@ private Money handleTransactionAndCharges(final LoanTransaction loanTransaction,
return transactionAmountUnprocessed;
}

private Money processTransaction(final LoanTransaction loanTransaction, final MonetaryCurrency currency,
protected Money processTransaction(final LoanTransaction loanTransaction, final MonetaryCurrency currency,
final List<LoanRepaymentScheduleInstallment> installments, final Set<LoanCharge> charges, Money amountToProcess) {
int installmentIndex = 0;

Expand Down Expand Up @@ -607,7 +607,7 @@ private Money processTransaction(final LoanTransaction loanTransaction, final Mo
return transactionAmountUnprocessed;
}

private Set<LoanCharge> extractFeeCharges(final Set<LoanCharge> loanCharges) {
protected Set<LoanCharge> extractFeeCharges(final Set<LoanCharge> loanCharges) {
final Set<LoanCharge> feeCharges = new HashSet<>();
for (final LoanCharge loanCharge : loanCharges) {
if (loanCharge.isFeeCharge()) {
Expand All @@ -617,7 +617,7 @@ private Set<LoanCharge> extractFeeCharges(final Set<LoanCharge> loanCharges) {
return feeCharges;
}

private Set<LoanCharge> extractPenaltyCharges(final Set<LoanCharge> loanCharges) {
protected Set<LoanCharge> extractPenaltyCharges(final Set<LoanCharge> loanCharges) {
final Set<LoanCharge> penaltyCharges = new HashSet<>();
for (final LoanCharge loanCharge : loanCharges) {
if (loanCharge.isPenaltyCharge()) {
Expand All @@ -627,7 +627,7 @@ private Set<LoanCharge> extractPenaltyCharges(final Set<LoanCharge> loanCharges)
return penaltyCharges;
}

private void updateChargesPaidAmountBy(final LoanTransaction loanTransaction, final Money feeCharges, final Set<LoanCharge> charges,
protected void updateChargesPaidAmountBy(final LoanTransaction loanTransaction, final Money feeCharges, final Set<LoanCharge> charges,
final Integer installmentNumber) {

Money amountRemaining = feeCharges;
Expand Down Expand Up @@ -661,7 +661,7 @@ private void updateChargesPaidAmountBy(final LoanTransaction loanTransaction, fi

}

private LoanCharge findEarliestUnpaidChargeFromUnOrderedSet(final Set<LoanCharge> charges, final MonetaryCurrency currency) {
protected LoanCharge findEarliestUnpaidChargeFromUnOrderedSet(final Set<LoanCharge> charges, final MonetaryCurrency currency) {
LoanCharge earliestUnpaidCharge = null;
LoanCharge installemntCharge = null;
LoanInstallmentCharge chargePerInstallment = null;
Expand All @@ -687,7 +687,7 @@ private LoanCharge findEarliestUnpaidChargeFromUnOrderedSet(final Set<LoanCharge
return earliestUnpaidCharge;
}

private void handleWriteOff(final LoanTransaction loanTransaction, final MonetaryCurrency currency,
protected void handleWriteOff(final LoanTransaction loanTransaction, final MonetaryCurrency currency,
final List<LoanRepaymentScheduleInstallment> installments) {

final LocalDate transactionDate = loanTransaction.getTransactionDate();
Expand All @@ -712,12 +712,12 @@ private void handleWriteOff(final LoanTransaction loanTransaction, final Monetar
loanTransaction.updateComponentsAndTotal(principalPortion, interestPortion, feeChargesPortion, penaltychargesPortion);
}

private void handleChargeback(LoanTransaction loanTransaction, MonetaryCurrency currency, Money overpaidAmount,
protected void handleChargeback(LoanTransaction loanTransaction, MonetaryCurrency currency, Money overpaidAmount,
List<LoanRepaymentScheduleInstallment> installments) {
processCreditTransaction(loanTransaction, overpaidAmount, currency, installments);
}

private void handleRefund(LoanTransaction loanTransaction, MonetaryCurrency currency,
protected void handleRefund(LoanTransaction loanTransaction, MonetaryCurrency currency,
List<LoanRepaymentScheduleInstallment> installments, final Set<LoanCharge> charges) {
List<LoanTransactionToRepaymentScheduleMapping> transactionMappings = new ArrayList<>();
final Comparator<LoanRepaymentScheduleInstallment> byDate = Comparator.comparing(LoanRepaymentScheduleInstallment::getDueDate);
Expand Down
Loading

0 comments on commit 6d93899

Please sign in to comment.