From 0917881e0d7512c4e7981597bb3d0d64e18018da Mon Sep 17 00:00:00 2001 From: Leonardo Hildt Date: Wed, 27 Sep 2023 09:38:12 -0300 Subject: [PATCH] Fix FBR-104 Adjustment on the formula to calculate the charge amount for Tasa Administrativa --- .../fineract/portfolio/charge/domain/Charge.java | 8 +++++--- .../portfolio/loanaccount/domain/LoanCharge.java | 13 +++++++++---- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/fineract-provider/src/main/java/org/apache/fineract/portfolio/charge/domain/Charge.java b/fineract-provider/src/main/java/org/apache/fineract/portfolio/charge/domain/Charge.java index d00522c82b9..bfc355f8dd3 100644 --- a/fineract-provider/src/main/java/org/apache/fineract/portfolio/charge/domain/Charge.java +++ b/fineract-provider/src/main/java/org/apache/fineract/portfolio/charge/domain/Charge.java @@ -40,6 +40,8 @@ import javax.persistence.OneToMany; import javax.persistence.Table; import javax.persistence.UniqueConstraint; + +import org.apache.commons.lang3.tuple.Pair; import org.apache.fineract.accounting.glaccount.data.GLAccountData; import org.apache.fineract.accounting.glaccount.domain.GLAccount; import org.apache.fineract.infrastructure.core.api.JsonCommand; @@ -826,14 +828,14 @@ public void setChargeRanges(List chargeRanges) { this.chargeRanges = chargeRanges; } - public BigDecimal getAddOnDisbursementChargeRate(LocalDate disbursementDate, LocalDate firstRepaymentDate) { + public Pair getAddOnDisbursementChargeRate(LocalDate disbursementDate, LocalDate firstRepaymentDate) { BigDecimal addOnDisbursementChargeRate = BigDecimal.ZERO; int defaultDays = LoanProductConstants.DEFAULT_LIMIT_OF_DAYS_FOR_ADDON; + int daysAddOnApplicable = 0; if (isDisbursementCharge() && isAddOnDisbursementType() && this.chargeRanges != null && !this.chargeRanges.isEmpty()) { // calculate days since disbursement date int numberOfDays = Math.toIntExact(daysBetween(disbursementDate, firstRepaymentDate)); - int daysAddOnApplicable = 0; if (numberOfDays > defaultDays) { daysAddOnApplicable = numberOfDays - defaultDays; } @@ -846,7 +848,7 @@ public BigDecimal getAddOnDisbursementChargeRate(LocalDate disbursementDate, Loc } } - return addOnDisbursementChargeRate; + return Pair.of(daysAddOnApplicable, addOnDisbursementChargeRate); } private static long daysBetween(LocalDate d1, LocalDate d2) { diff --git a/fineract-provider/src/main/java/org/apache/fineract/portfolio/loanaccount/domain/LoanCharge.java b/fineract-provider/src/main/java/org/apache/fineract/portfolio/loanaccount/domain/LoanCharge.java index 648828bdb59..1c2d05ff1c3 100644 --- a/fineract-provider/src/main/java/org/apache/fineract/portfolio/loanaccount/domain/LoanCharge.java +++ b/fineract-provider/src/main/java/org/apache/fineract/portfolio/loanaccount/domain/LoanCharge.java @@ -39,6 +39,8 @@ import javax.persistence.OneToOne; import javax.persistence.Table; import javax.persistence.UniqueConstraint; + +import org.apache.commons.lang3.tuple.Pair; import org.apache.fineract.infrastructure.core.api.JsonCommand; import org.apache.fineract.infrastructure.core.domain.AbstractPersistableCustom; import org.apache.fineract.organisation.monetary.domain.MonetaryCurrency; @@ -424,16 +426,19 @@ public void update(final BigDecimal amount, final LocalDate dueDate, final BigDe if (this.loan != null && isDisbursementCharge() && this.isAddOnDisbursementType()) { LocalDate disbursementDate = this.loan.getDisbursementDate(); LocalDate firstRepaymentDate = this.loan.fetchRepaymentScheduleInstallment(1).getDueDate(); - BigDecimal feeRate = this.charge.getAddOnDisbursementChargeRate(disbursementDate, firstRepaymentDate); + Pair addOnDaysAndRate = this.charge.getAddOnDisbursementChargeRate(disbursementDate, firstRepaymentDate); + Integer daysToApplyAddOn = addOnDaysAndRate.getLeft(); + BigDecimal feeRate = addOnDaysAndRate.getRight(); this.percentage = feeRate; updatedAmount = feeRate; + loanCharge = percentageOf(this.amountPercentageAppliedTo).multiply(BigDecimal.valueOf(daysToApplyAddOn), MoneyHelper.getMathContext()); } else { this.percentage = amount; + if (loanCharge.compareTo(BigDecimal.ZERO) == 0) { + loanCharge = percentageOf(this.amountPercentageAppliedTo); + } } this.amountPercentageAppliedTo = loanPrincipal; - if (loanCharge.compareTo(BigDecimal.ZERO) == 0) { - loanCharge = percentageOf(this.amountPercentageAppliedTo); - } this.amount = minimumAndMaximumCap(loanCharge); break; }