Skip to content

Commit

Permalink
FINERACT-2042 Handling overpayment of chargeback with credit allocations
Browse files Browse the repository at this point in the history
  • Loading branch information
reluxa committed Feb 27, 2024
1 parent d06b056 commit 4371eda
Show file tree
Hide file tree
Showing 17 changed files with 1,346 additions and 424 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@
import org.apache.fineract.portfolio.loanproduct.domain.LoanProduct;
import org.apache.fineract.portfolio.loanproduct.domain.LoanProductRelatedDetail;
import org.apache.fineract.portfolio.loanproduct.domain.LoanRescheduleStrategyMethod;
import org.apache.fineract.portfolio.loanproduct.domain.PaymentAllocationTransactionType;
import org.apache.fineract.portfolio.loanproduct.domain.RecalculationFrequencyType;
import org.apache.fineract.portfolio.loanproduct.domain.RepaymentStartDateType;
import org.apache.fineract.portfolio.loanproduct.service.LoanEnumerations;
Expand Down Expand Up @@ -5945,10 +5946,18 @@ private void updateLoanOutstandingBalances() {
.minus(loanTransaction.getOverPaymentPortion(getCurrency()));
loanTransaction.updateOutstandingLoanBalance(outstanding.getAmount());
} else if (loanTransaction.isChargeback() || loanTransaction.isCreditBalanceRefund()) {
Money transactionOutstanding = loanTransaction.getAmount(getCurrency());
Money transactionOutstanding = loanTransaction.getPrincipalPortion(getCurrency());
if (!loanTransaction.getOverPaymentPortion(getCurrency()).isZero()) {
transactionOutstanding = loanTransaction.getAmount(getCurrency())
.minus(loanTransaction.getOverPaymentPortion(getCurrency()));
// in case of advanced payment strategy and creditAllocations the full amount is recognized first
if (this.getCreditAllocationRules() != null && this.getCreditAllocationRules().size() > 0) {
Money payedPrincipal = loanTransaction.getLoanTransactionToRepaymentScheduleMappings().stream()
.map(mapping -> mapping.getPrincipalPortion(getCurrency())).reduce(Money.zero(getCurrency()), Money::plus);
transactionOutstanding = loanTransaction.getPrincipalPortion(getCurrency()).minus(payedPrincipal);
} else {
// in case legacy payment strategy
transactionOutstanding = loanTransaction.getAmount(getCurrency())
.minus(loanTransaction.getOverPaymentPortion(getCurrency()));
}
if (transactionOutstanding.isLessThanZero()) {
transactionOutstanding = Money.zero(getCurrency());
}
Expand Down Expand Up @@ -7184,6 +7193,13 @@ public List<LoanPaymentAllocationRule> getPaymentAllocationRules() {
return paymentAllocationRules;
}

public LoanPaymentAllocationRule getPaymentAllocationRuleOrDefault(PaymentAllocationTransactionType transactionType) {
Optional<LoanPaymentAllocationRule> paymentAllocationRule = this.getPaymentAllocationRules().stream()
.filter(rule -> rule.getTransactionType().equals(transactionType)).findFirst();
return paymentAllocationRule.orElse(this.getPaymentAllocationRules().stream()
.filter(rule -> rule.getTransactionType().equals(PaymentAllocationTransactionType.DEFAULT)).findFirst().get());
}

public void setPaymentAllocationRules(List<LoanPaymentAllocationRule> loanPaymentAllocationRules) {
this.paymentAllocationRules = loanPaymentAllocationRules;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,14 @@ public class LoanRepaymentScheduleInstallment extends AbstractAuditableWithUTCDa
@Column(name = "is_additional", nullable = false)
private boolean additional;

@Column(name = "credits_amount", scale = 6, precision = 19, nullable = true)
private BigDecimal credits;
@Column(name = "credited_principal", scale = 6, precision = 19, nullable = true)
private BigDecimal creditedPrincipal;

@Column(name = "credited_fee", scale = 6, precision = 19, nullable = true)
private BigDecimal creditedFee;

@Column(name = "credited_penalty", scale = 6, precision = 19, nullable = true)
private BigDecimal creditedPenalty;

@Column(name = "is_down_payment", nullable = false)
private boolean isDownPayment;
Expand Down Expand Up @@ -246,8 +252,16 @@ public LocalDate getDueDate() {
return this.dueDate;
}

public Money getCredits(final MonetaryCurrency currency) {
return Money.of(currency, this.credits);
public Money getCreditedPrincipal(final MonetaryCurrency currency) {
return Money.of(currency, this.creditedPrincipal);
}

public Money getCreditedFee(final MonetaryCurrency currency) {
return Money.of(currency, this.creditedFee);
}

public Money getCreditedPenalty(final MonetaryCurrency currency) {
return Money.of(currency, this.creditedPenalty);
}

public Money getPrincipal(final MonetaryCurrency currency) {
Expand Down Expand Up @@ -408,9 +422,17 @@ public void resetDerivedComponents() {

this.obligationsMet = false;
this.obligationsMetOnDate = null;
if (this.credits != null) {
this.principal = this.principal.subtract(this.credits);
this.credits = null;
if (this.creditedPrincipal != null) {
this.principal = this.principal.subtract(this.creditedPrincipal);
this.creditedPrincipal = null;
}
if (this.creditedFee != null) {
this.feeChargesCharged = this.feeChargesCharged.subtract(this.creditedFee);
this.creditedFee = null;
}
if (this.creditedPenalty != null) {
this.penaltyCharges = this.penaltyCharges.subtract(this.creditedPenalty);
this.creditedPenalty = null;
}
}

Expand Down Expand Up @@ -780,11 +802,27 @@ public void addToPrincipal(final LocalDate transactionDate, final Money transact
checkIfRepaymentPeriodObligationsAreMet(transactionDate, transactionAmount.getCurrency());
}

public void addToCredits(final BigDecimal amount) {
if (this.credits == null) {
this.credits = amount;
public void addToCreditedPrincipal(final BigDecimal amount) {
if (this.creditedPrincipal == null) {
this.creditedPrincipal = amount;
} else {
this.creditedPrincipal = this.creditedPrincipal.add(amount);
}
}

public void addToCreditedFee(final BigDecimal amount) {
if (this.creditedFee == null) {
this.creditedFee = amount;
} else {
this.creditedFee = this.creditedFee.add(amount);
}
}

public void addToCreditedPenalty(final BigDecimal amount) {
if (this.creditedPenalty == null) {
this.creditedPenalty = amount;
} else {
this.credits = this.credits.add(amount);
this.creditedPenalty = this.creditedPenalty.add(amount);
}
}

Expand Down Expand Up @@ -908,7 +946,7 @@ private void reduceAdvanceAndLateTotalsForRepaymentPeriod(final LocalDate transa
}

public void updateCredits(final LocalDate transactionDate, final Money transactionAmount) {
addToCredits(transactionAmount.getAmount());
addToCreditedPrincipal(transactionAmount.getAmount());
addToPrincipal(transactionDate, transactionAmount);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ public class LoanSummary {
@Column(name = "total_charges_due_at_disbursement_derived", scale = 6, precision = 19)
private BigDecimal totalFeeChargesDueAtDisbursement;

@Column(name = "fee_adjustments_derived", scale = 6, precision = 19)
private BigDecimal totalFeeAdjustments;

@Column(name = "fee_charges_repaid_derived", scale = 6, precision = 19)
private BigDecimal totalFeeChargesRepaid;

Expand All @@ -89,6 +92,9 @@ public class LoanSummary {
@Column(name = "penalty_charges_charged_derived", scale = 6, precision = 19)
private BigDecimal totalPenaltyChargesCharged;

@Column(name = "penalty_adjustments_derived", scale = 6, precision = 19)
private BigDecimal totalPenaltyAdjustments;

@Column(name = "penalty_charges_repaid_derived", scale = 6, precision = 19)
private BigDecimal totalPenaltyChargesRepaid;

Expand Down Expand Up @@ -204,6 +210,8 @@ public void updateTotalWaived(final BigDecimal totalWaived) {
public void zeroFields() {
this.totalPrincipalDisbursed = BigDecimal.ZERO;
this.totalPrincipalAdjustments = BigDecimal.ZERO;
this.totalFeeAdjustments = BigDecimal.ZERO;
this.totalPenaltyAdjustments = BigDecimal.ZERO;
this.totalPrincipalRepaid = BigDecimal.ZERO;
this.totalPrincipalWrittenOff = BigDecimal.ZERO;
this.totalPrincipalOutstanding = BigDecimal.ZERO;
Expand Down Expand Up @@ -238,6 +246,8 @@ public void updateSummary(final MonetaryCurrency currency, final Money principal
this.totalPrincipalDisbursed = principal.getAmount();
this.totalPrincipalAdjustments = summaryWrapper.calculateTotalPrincipalAdjusted(repaymentScheduleInstallments, currency)
.getAmount();
this.totalFeeAdjustments = summaryWrapper.calculateTotalFeeAdjusted(repaymentScheduleInstallments, currency).getAmount();
this.totalPenaltyAdjustments = summaryWrapper.calculateTotalPenaltyAdjusted(repaymentScheduleInstallments, currency).getAmount();
this.totalPrincipalRepaid = summaryWrapper.calculateTotalPrincipalRepaid(repaymentScheduleInstallments, currency).getAmount();
this.totalPrincipalWrittenOff = summaryWrapper.calculateTotalPrincipalWrittenOff(repaymentScheduleInstallments, currency)
.getAmount();
Expand All @@ -259,7 +269,9 @@ public void updateSummary(final MonetaryCurrency currency, final Money principal
this.totalFeeChargesCharged = totalFeeChargesCharged.getAmount();

Money totalFeeChargesRepaidAtDisbursement = summaryWrapper.calculateTotalChargesRepaidAtDisbursement(charges, currency);
this.totalFeeChargesRepaid = totalFeeChargesRepaidAtDisbursement.getAmount();
Money totalFeeChargesRepaidAfterDisbursement = summaryWrapper.calculateTotalFeeChargesRepaid(repaymentScheduleInstallments,
currency);
this.totalFeeChargesRepaid = totalFeeChargesRepaidAfterDisbursement.plus(totalFeeChargesRepaidAtDisbursement).getAmount();

if (charges != null) {
this.totalFeeChargesWaived = summaryWrapper.calculateTotalFeeChargesWaived(charges, currency).getAmount();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,25 @@ public Money calculateTotalPrincipalAdjusted(final List<LoanRepaymentScheduleIns
final MonetaryCurrency currency) {
Money total = Money.zero(currency);
for (final LoanRepaymentScheduleInstallment installment : repaymentScheduleInstallments) {
total = total.plus(installment.getCredits(currency));
total = total.plus(installment.getCreditedPrincipal(currency));
}
return total;
}

public Money calculateTotalFeeAdjusted(final List<LoanRepaymentScheduleInstallment> repaymentScheduleInstallments,
final MonetaryCurrency currency) {
Money total = Money.zero(currency);
for (final LoanRepaymentScheduleInstallment installment : repaymentScheduleInstallments) {
total = total.plus(installment.getCreditedFee(currency));
}
return total;
}

public Money calculateTotalPenaltyAdjusted(final List<LoanRepaymentScheduleInstallment> repaymentScheduleInstallments,
final MonetaryCurrency currency) {
Money total = Money.zero(currency);
for (final LoanRepaymentScheduleInstallment installment : repaymentScheduleInstallments) {
total = total.plus(installment.getCreditedPenalty(currency));
}
return total;
}
Expand Down Expand Up @@ -247,7 +265,8 @@ public Money calculateTotalChargesRepaidAtDisbursement(Set<LoanCharge> charges,
return total;
}
for (final LoanCharge loanCharge : charges) {
if (!loanCharge.isPenaltyCharge() && loanCharge.getAmountPaid(currency).isGreaterThanZero()) {
if (!loanCharge.isPenaltyCharge() && loanCharge.getAmountPaid(currency).isGreaterThanZero()
&& loanCharge.isDisbursementCharge()) {
total = total.plus(loanCharge.getAmountPaid(currency));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ protected void processCreditTransaction(LoanTransaction loanTransaction, MoneyHo
for (final LoanRepaymentScheduleInstallment currentInstallment : installments) {
pastDueDate = currentInstallment.getDueDate();
if (!currentInstallment.isAdditional() && DateUtils.isAfter(currentInstallment.getDueDate(), transactionDate)) {
currentInstallment.addToCredits(transactionAmount.getAmount());
currentInstallment.addToCreditedPrincipal(transactionAmount.getAmount());
currentInstallment.addToPrincipal(transactionDate, transactionAmount);
if (repaidAmount.isGreaterThanZero()) {
currentInstallment.payPrincipalComponent(loanTransaction.getTransactionDate(), repaidAmount);
Expand Down Expand Up @@ -526,7 +526,7 @@ protected void processCreditTransaction(LoanTransaction loanTransaction, MoneyHo
if (!loanTransactionMapped) {
if (loanTransaction.getTransactionDate().equals(pastDueDate)) {
LoanRepaymentScheduleInstallment currentInstallment = installments.get(installments.size() - 1);
currentInstallment.addToCredits(transactionAmount.getAmount());
currentInstallment.addToCreditedPrincipal(transactionAmount.getAmount());
currentInstallment.addToPrincipal(transactionDate, transactionAmount);
if (repaidAmount.isGreaterThanZero()) {
currentInstallment.payPrincipalComponent(loanTransaction.getTransactionDate(), repaidAmount);
Expand All @@ -539,7 +539,7 @@ protected void processCreditTransaction(LoanTransaction loanTransaction, MoneyHo
pastDueDate, transactionDate, transactionAmount.getAmount(), zeroMoney.getAmount(), zeroMoney.getAmount(),
zeroMoney.getAmount(), false, null);
installment.markAsAdditional();
installment.addToCredits(transactionAmount.getAmount());
installment.addToCreditedPrincipal(transactionAmount.getAmount());
loan.addLoanRepaymentScheduleInstallment(installment);

if (repaidAmount.isGreaterThanZero()) {
Expand Down
Loading

0 comments on commit 4371eda

Please sign in to comment.