Skip to content

Commit

Permalink
SU-477 (#1345)
Browse files Browse the repository at this point in the history
* SU-477 add logic to handle flat aval charge for migration
  • Loading branch information
faheem205 authored Dec 6, 2024
1 parent b70075d commit 2f595d7
Show file tree
Hide file tree
Showing 8 changed files with 599 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,11 @@ public boolean isAvalCharge() {
return ChargeCalculationType.fromInt(this.chargeCalculation).isPercentageOfAval();
}

public boolean isAvalChargeFlatForMigration() {
// Charge is distributed among the installments
return ChargeCalculationType.fromInt(this.chargeCalculation).isFlatAvalForMigration();
}

public boolean isMandatoryInsurance() {
// Charge is distributed among the installments
return ChargeCalculationType.fromInt(this.chargeCalculation).isMandatoryInsuranceCharge();
Expand Down Expand Up @@ -1048,6 +1053,9 @@ public void validateChargeIsSetupCorrectly() {
} else if (this.isFlatHono()) {
verifyChargeConfiguration(code, ChargeCalculationTypeBaseItemsEnum.FLAT.getIndex(),
ChargeCalculationTypeBaseItemsEnum.HOORARIOS.getIndex(), null, null, null);
} else if (this.isAvalChargeFlatForMigration()) {
verifyChargeConfiguration(code, ChargeCalculationTypeBaseItemsEnum.AVAL.getIndex(),
ChargeCalculationTypeBaseItemsEnum.FLAT.getIndex(), null, null, null);
} else {
throw new GeneralPlatformDomainRuleException("error.msg.charge.not.setup.correctly", "Charge not setup correctly",
this.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1585,6 +1585,11 @@ public boolean isPercentageOfAval() {
&& isPercentageOfDisbursement();
}

public boolean isFlatAvalForMigration() {
return this.byteRepresentation.charAt(ChargeCalculationTypeBaseItemsEnum.AVAL.getIndex()) == '1' && this.equals(FLAT_AVAL)
&& isFlat();
}

public boolean isPercentageOfHonorarios() {
return this.byteRepresentation.charAt(ChargeCalculationTypeBaseItemsEnum.HOORARIOS.getIndex()) == '1'
&& this.byteRepresentation.charAt(ChargeCalculationTypeBaseItemsEnum.FLAT.getIndex()) == '1';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,7 @@ public void update(final BigDecimal amount, final LocalDate dueDate, final BigDe
case FLAT_AMOUNT:
case FLAT_SEGOVOLUNTARIO:
case FLAT_SEGO:
case FLAT_AVAL:
if (isInstalmentFee()) {
if (numberOfRepayments == null) {
numberOfRepayments = this.loan.fetchNumberOfInstallmensAfterExceptions();
Expand Down Expand Up @@ -1508,6 +1509,17 @@ public BigDecimal calculateCustomFeeChargeToInstallment(Integer installmentNumbe
BigDecimal computedAmount = LoanCharge.percentageOf(outstandingBalance.getAmount(), this.percentage);
BigDecimal finalAmount = computedAmount.divide(installmentCount, 0, RoundingMode.HALF_UP);
customAmout = customAmout.add(finalAmount);
} else if (this.isVoluntaryInsurance() || this.isAvalChargeFlatForMigration()) {
BigDecimal chargeAmount = BigDecimal.ZERO;
if (this.installmentCharges().isEmpty()) {
chargeAmount = this.amountOrPercentage;
} else {
final LoanInstallmentCharge installmentCharge = this.getInstallmentLoanCharge(installmentNumber);
if (installmentCharge != null) {
chargeAmount = installmentCharge.getAmount();
}
}
customAmout = customAmout.add(chargeAmount);
}
return customAmout;
}
Expand Down Expand Up @@ -1537,7 +1549,12 @@ public boolean isCustomPercentageBasedDistributedCharge() {

public boolean isAvalCharge() {
// Charge is distributed among the installments
return getChargeCalculation().isPercentageOfAval();
return getChargeCalculation().isPercentageOfAval() || getChargeCalculation().isFlatAvalForMigration();
}

public boolean isAvalChargeFlatForMigration() {
// Charge is distributed among the installments
return ChargeCalculationType.fromInt(this.chargeCalculation).isFlatAvalForMigration();
}

public boolean isMandatoryInsurance() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -730,19 +730,33 @@ public Money payHonorariosChargesComponent(final LocalDate transactionDate, fina

}

public Money payAvalChargesComponent(final LocalDate transactionDate, final Money transactionAmountRemaining,
public Money payAvalChargesComponent(final LocalDate transactionDate, Money transactionAmountRemaining,
final boolean isWriteOffTransaction, LoanTransaction loanTransaction) {

final MonetaryCurrency currency = transactionAmountRemaining.getCurrency();
Money loanChargePaidByPortion = Money.zero(currency);
Money feePortionOfTransaction = Money.zero(currency);
Money loanChargePaidByPortion = Money.zero(currency);
if (transactionAmountRemaining.isZero()) {
return feePortionOfTransaction;
}
for (LoanInstallmentCharge installmentCharge : getInstallmentCharges()) {
if (installmentCharge.getLoanCharge().isAvalCharge()) {

for (LoanInstallmentCharge vatCharge : getInstallmentCharges()) {
if (Objects.equals(installmentCharge.getLoanCharge().getCharge().getId(),
vatCharge.getLoanCharge().getCharge().getParentChargeId())) {
feePortionOfTransaction = payLoanCharge(vatCharge, transactionDate, transactionAmountRemaining, currency,
feePortionOfTransaction, isWriteOffTransaction, loanChargePaidByPortion);
updateChargePaidByAmount(feePortionOfTransaction.minus(loanChargePaidByPortion), loanTransaction,
vatCharge.getLoanCharge());
transactionAmountRemaining = transactionAmountRemaining.minus(feePortionOfTransaction);
loanChargePaidByPortion = feePortionOfTransaction.minus(loanChargePaidByPortion);
break;
}
}
feePortionOfTransaction = payLoanCharge(installmentCharge, transactionDate, transactionAmountRemaining, currency,
feePortionOfTransaction, isWriteOffTransaction, loanChargePaidByPortion);

updateChargePaidByAmount(feePortionOfTransaction.minus(loanChargePaidByPortion), loanTransaction,
installmentCharge.getLoanCharge());
loanChargePaidByPortion = feePortionOfTransaction.minus(loanChargePaidByPortion);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,16 @@ public Set<LoanCharge> fromParsedJson(final JsonElement element, List<LoanDisbur
final ExternalId externalId = externalIdFactory.create(externalIdStr);
if (id == null) {
final Charge chargeDefinition = this.chargeRepository.findOneWithNotFoundDetection(chargeId);

Boolean isMigratedLoan = this.fromApiJsonHelper.extractBooleanNamed(LoanApiConstants.IS_MIGRAR_LOAN, element);
if (isMigratedLoan == null) {
isMigratedLoan = Boolean.FALSE;
}
if (!isMigratedLoan && chargeDefinition.isAvalChargeFlatForMigration()) {
final String defaultUserMessage = "Selected Aval Charge is to be used only for migrated loans.";
throw new LoanChargeCannotBeAddedException("loanCharge", "aval.charge", defaultUserMessage, null,
chargeDefinition.getName());
}
if (chargeDefinition.isFlatHono()) {
amount = BigDecimal.ZERO;
}
Expand All @@ -193,8 +203,8 @@ public Set<LoanCharge> fromParsedJson(final JsonElement element, List<LoanDisbur
}

boolean getPercentageAmountFromTable = chargeDefinition.isGetPercentageFromTable();
if (getPercentageAmountFromTable || ChargeCalculationType.fromInt(chargeDefinition.getChargeCalculation())
.equals(ChargeCalculationType.DISB_AVAL)) {
if (!isMigratedLoan && (getPercentageAmountFromTable || ChargeCalculationType
.fromInt(chargeDefinition.getChargeCalculation()).equals(ChargeCalculationType.DISB_AVAL))) {
ChargeCalculationType calculation = chargeCalculation;
if (calculation == null) {
calculation = ChargeCalculationType.fromInt(chargeDefinition.getChargeCalculation());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1696,7 +1696,7 @@ left join (
select mlcpd.loan_transaction_id , sum(mlcpd.amount) amount from
m_loan_charge_paid_by mlcpd
join m_loan_charge mlc on mlc.id = mlcpd.loan_charge_id
where mlc.charge_calculation_enum = 41
where mlc.charge_calculation_enum in (41, 286)
group by mlcpd.loan_transaction_id
) aval on aval.loan_transaction_id = tr.id
left join (
Expand All @@ -1706,7 +1706,7 @@ select mlcpd.loan_transaction_id, sum(mlcpd.amount) amount from
join m_charge mc on mc.id = mlc.charge_id
join m_charge parent on parent.id = mc.parent_charge_id
where mc.charge_calculation_enum = 342
and parent.charge_calculation_enum = 41
and parent.charge_calculation_enum in (41, 286)
group by mlcpd.loan_transaction_id
) vat_aval on vat_aval.loan_transaction_id = tr.id
left join (
Expand Down Expand Up @@ -3511,7 +3511,7 @@ select mlc.loan_id, sum(mlic.amount_outstanding_derived) outstanding_amount from
and mlrs.completed_derived != true
and mlrs.duedate >= (select overdue_since_date_derived from m_loan_arrears_aging where loan_id = mlc.loan_id)
and (mlrs.duedate <= current_date or (mlrs.duedate >= current_date and mlrs.fromdate <= current_date))
where mlc.charge_calculation_enum = 41
where mlc.charge_calculation_enum in (41, 286)
group by mlc.loan_id
) aval_chg ON aval_chg.loan_id = ml.id
LEFT JOIN (
Expand All @@ -3523,7 +3523,7 @@ SELECT sum(mlic.amount_outstanding_derived) outstanding_amount, mlc2.loan_id FRO
and (mlrs.duedate <= current_date or (mlrs.duedate >= current_date and mlrs.fromdate <= current_date))
JOIN m_charge mc2 ON mc2.id = mlc2.charge_id AND mc2.charge_calculation_enum = 342
JOIN m_charge parent_charge on parent_charge.id = mc2.parent_charge_id
WHERE parent_charge.charge_calculation_enum = 41
WHERE parent_charge.charge_calculation_enum in (41, 286)
group by mlc2.loan_id
) aval_vat_chg ON aval_vat_chg.loan_id = ml.id
LEFT join (
Expand All @@ -3533,7 +3533,7 @@ select mlc.loan_id, sum(mlic.amount_outstanding_derived) outstanding_amount from
and mlrs.completed_derived != true
and mlrs.duedate >= (select overdue_since_date_derived from m_loan_arrears_aging where loan_id = mlc.loan_id)
and (mlrs.duedate <= current_date or (mlrs.duedate >= current_date and mlrs.fromdate <= current_date))
where mlc.charge_calculation_enum NOT IN (468, 575, 231, 342, 41)
where mlc.charge_calculation_enum NOT IN (468, 575, 231, 342, 41, 286)
group by mlc.loan_id
) other_chg ON other_chg.loan_id = ml.id
LEFT JOIN (
Expand All @@ -3545,7 +3545,7 @@ SELECT sum(mlic.amount_outstanding_derived) outstanding_amount, mlc2.loan_id FRO
and (mlrs.duedate <= current_date or (mlrs.duedate >= current_date and mlrs.fromdate <= current_date))
JOIN m_charge mc2 ON mc2.id = mlc2.charge_id AND mc2.charge_calculation_enum = 342
JOIN m_charge parent_charge on parent_charge.id = mc2.parent_charge_id
WHERE parent_charge.charge_calculation_enum NOT IN (468, 575, 231, 41)
WHERE parent_charge.charge_calculation_enum NOT IN (468, 575, 231, 41, 286)
group by mlc2.loan_id
) other_vat_chg ON other_vat_chg.loan_id = ml.id
LEFT join (
Expand Down Expand Up @@ -3639,7 +3639,7 @@ select mlc.loan_id, sum(mlic.amount_outstanding_derived) outstanding_amount from
and mlrs.completed_derived != true
and mlrs.duedate >= (select overdue_since_date_derived from m_loan_arrears_aging where loan_id = mlc.loan_id)
and (mlrs.duedate <= current_date or (mlrs.duedate >= current_date and mlrs.fromdate <= current_date))
where mlc.charge_calculation_enum = 41
where mlc.charge_calculation_enum in (41, 286)
group by mlc.loan_id
) aval_chg ON aval_chg.loan_id = ml.id
LEFT JOIN (
Expand All @@ -3651,7 +3651,7 @@ SELECT sum(mlic.amount_outstanding_derived) outstanding_amount, mlc2.loan_id FRO
and (mlrs.duedate <= current_date or (mlrs.duedate >= current_date and mlrs.fromdate <= current_date))
JOIN m_charge mc2 ON mc2.id = mlc2.charge_id AND mc2.charge_calculation_enum = 342
JOIN m_charge parent_charge on parent_charge.id = mc2.parent_charge_id
WHERE parent_charge.charge_calculation_enum = 41
WHERE parent_charge.charge_calculation_enum in (41, 286)
group by mlc2.loan_id
) aval_vat_chg ON aval_vat_chg.loan_id = ml.id
LEFT join (
Expand All @@ -3661,7 +3661,7 @@ select mlc.loan_id, sum(mlic.amount_outstanding_derived) outstanding_amount from
and mlrs.completed_derived != true
and mlrs.duedate >= (select overdue_since_date_derived from m_loan_arrears_aging where loan_id = mlc.loan_id)
and (mlrs.duedate <= current_date or (mlrs.duedate >= current_date and mlrs.fromdate <= current_date))
where mlc.charge_calculation_enum NOT IN (468, 575, 231, 342, 41)
where mlc.charge_calculation_enum NOT IN (468, 575, 231, 342, 41, 286)
group by mlc.loan_id
) other_chg ON other_chg.loan_id = ml.id
LEFT JOIN (
Expand All @@ -3673,7 +3673,7 @@ SELECT sum(mlic.amount_outstanding_derived) outstanding_amount, mlc2.loan_id FRO
and (mlrs.duedate <= current_date or (mlrs.duedate >= current_date and mlrs.fromdate <= current_date))
JOIN m_charge mc2 ON mc2.id = mlc2.charge_id AND mc2.charge_calculation_enum = 342
JOIN m_charge parent_charge on parent_charge.id = mc2.parent_charge_id
WHERE parent_charge.charge_calculation_enum NOT IN (468, 575, 231, 41)
WHERE parent_charge.charge_calculation_enum NOT IN (468, 575, 231, 41, 286)
group by mlc2.loan_id
) other_vat_chg ON other_vat_chg.loan_id = ml.id
LEFT join (
Expand Down Expand Up @@ -3767,7 +3767,7 @@ select mlc.loan_id, sum(mlic.amount_outstanding_derived) outstanding_amount from
and mlrs.completed_derived != true
and mlrs.duedate >= (select overdue_since_date_derived from m_loan_arrears_aging where loan_id = mlc.loan_id)
and (mlrs.duedate <= current_date or (mlrs.duedate >= current_date and mlrs.fromdate <= current_date))
where mlc.charge_calculation_enum = 41
where mlc.charge_calculation_enum in (41, 286)
group by mlc.loan_id
) aval_chg ON aval_chg.loan_id = ml.id
LEFT JOIN (
Expand All @@ -3779,7 +3779,7 @@ SELECT sum(mlic.amount_outstanding_derived) outstanding_amount, mlc2.loan_id FRO
and (mlrs.duedate <= current_date or (mlrs.duedate >= current_date and mlrs.fromdate <= current_date))
JOIN m_charge mc2 ON mc2.id = mlc2.charge_id AND mc2.charge_calculation_enum = 342
JOIN m_charge parent_charge on parent_charge.id = mc2.parent_charge_id
WHERE parent_charge.charge_calculation_enum = 41
WHERE parent_charge.charge_calculation_enum in (41, 286)
group by mlc2.loan_id
) aval_vat_chg ON aval_vat_chg.loan_id = ml.id
LEFT join (
Expand All @@ -3789,7 +3789,7 @@ select mlc.loan_id, sum(mlic.amount_outstanding_derived) outstanding_amount from
and mlrs.completed_derived != true
and mlrs.duedate >= (select overdue_since_date_derived from m_loan_arrears_aging where loan_id = mlc.loan_id)
and (mlrs.duedate <= current_date or (mlrs.duedate >= current_date and mlrs.fromdate <= current_date))
where mlc.charge_calculation_enum NOT IN (468, 575, 231, 342, 41)
where mlc.charge_calculation_enum NOT IN (468, 575, 231, 342, 41, 286)
group by mlc.loan_id
) other_chg ON other_chg.loan_id = ml.id
LEFT JOIN (
Expand All @@ -3801,7 +3801,7 @@ SELECT sum(mlic.amount_outstanding_derived) outstanding_amount, mlc2.loan_id FRO
and (mlrs.duedate <= current_date or (mlrs.duedate >= current_date and mlrs.fromdate <= current_date))
JOIN m_charge mc2 ON mc2.id = mlc2.charge_id AND mc2.charge_calculation_enum = 342
JOIN m_charge parent_charge on parent_charge.id = mc2.parent_charge_id
WHERE parent_charge.charge_calculation_enum NOT IN (468, 575, 231, 41)
WHERE parent_charge.charge_calculation_enum NOT IN (468, 575, 231, 41, 286)
group by mlc2.loan_id
) other_vat_chg ON other_vat_chg.loan_id = ml.id
LEFT join (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -327,4 +327,5 @@
<include file="parts/20242711011009135_SU_439_add_migration_columns_loan.xml" relativeToChangelogFile="true"/>
<include file="parts/20243008011009148_SU_319_add_table_collection_house_history.xml" relativeToChangelogFile="true"/>
<include file="parts/20243008011009148_SU_432_update-job-run.xml" relativeToChangelogFile="true"/>
<include file="parts/20240507011009134_SU_477_update_reports_migration_aval_charge.xml" relativeToChangelogFile="true"/>
</databaseChangeLog>
Loading

0 comments on commit 2f595d7

Please sign in to comment.