feat(WEB-505): implement waive penalties for loan repayment and reschedule (#2941)

Fix waive-penalties amount calc and align translations across locales

fix:test issue

Fix waive penalties recalculation, clean up subscription leaks, and align penalty translations
This commit is contained in:
Shubham Kumar
2025-12-28 16:09:08 -06:00
committed by GitHub
parent cb52f59dc3
commit 62c175ff15
21 changed files with 668 additions and 38 deletions
@@ -8,8 +8,10 @@ import { LoansService } from 'app/loans/loans.service';
import { SettingsService } from 'app/settings/settings.service';
import { Dates } from 'app/core/utils/dates';
import { Currency } from 'app/shared/models/general.model';
import { PenaltyManagementService } from 'app/loans/services/penalty-management.service';
import { InputAmountComponent } from '../../../../shared/input-amount/input-amount.component';
import { MatSlideToggle } from '@angular/material/slide-toggle';
import { MatCheckbox } from '@angular/material/checkbox';
import { CdkTextareaAutosize } from '@angular/cdk/text-field';
import { FormatNumberPipe } from '../../../../pipes/format-number.pipe';
import { STANDALONE_SHARED_IMPORTS } from 'app/standalone-shared.module';
@@ -25,6 +27,7 @@ import { STANDALONE_SHARED_IMPORTS } from 'app/standalone-shared.module';
...STANDALONE_SHARED_IMPORTS,
InputAmountComponent,
MatSlideToggle,
MatCheckbox,
CdkTextareaAutosize,
FormatNumberPipe
]
@@ -36,6 +39,7 @@ export class MakeRepaymentComponent implements OnInit {
private router = inject(Router);
private dateUtils = inject(Dates);
private settingsService = inject(SettingsService);
private penaltyManagementService = inject(PenaltyManagementService);
@Input() dataObject: any;
/** Loan Id */
@@ -44,6 +48,14 @@ export class MakeRepaymentComponent implements OnInit {
paymentTypes: any;
/** Show payment details */
showPaymentDetails = false;
/** Waive Penalties toggle */
waivePenalties = false;
/** Penalties list */
penalties: any[] = [];
/** Selected penalty IDs */
selectedPenalties: number[] = [];
/** Select all penalties checkbox */
selectAllPenalties = false;
/** Minimum Date allowed. */
minDate = new Date(2000, 0, 1);
/** Maximum Date allowed. */
@@ -79,6 +91,7 @@ export class MakeRepaymentComponent implements OnInit {
if (this.dataObject.currency) {
this.currency = this.dataObject.currency;
}
this.loadPenalties();
}
/**
@@ -97,22 +110,11 @@ export class MakeRepaymentComponent implements OnInit {
});
if (this.isCapitalizedIncome()) {
this.repaymentLoanForm.addControl(
'transactionAmount',
new UntypedFormControl('', [
Validators.required,
Validators.min(0.001),
Validators.max(this.dataObject.amount)
])
);
this.repaymentLoanForm.addControl('transactionAmount', new UntypedFormControl('', []));
this.updateTransactionAmountValidators(false);
} else {
this.repaymentLoanForm.addControl(
'transactionAmount',
new UntypedFormControl('', [
Validators.required,
Validators.min(0.001)
])
);
this.repaymentLoanForm.addControl('transactionAmount', new UntypedFormControl('', []));
this.updateTransactionAmountValidators(false);
}
if (this.isCapitalizedIncome() || this.isBuyDownFee()) {
this.repaymentLoanForm.addControl('classificationId', new UntypedFormControl(''));
@@ -169,6 +171,141 @@ export class MakeRepaymentComponent implements OnInit {
return code.includes('merchantissuedrefund') || code.includes('payoutrefund');
}
/**
* Load penalties for the loan
* Penalties are charges calculated for installments in the payment schedule.
* Each penalty charge has a dueDate that corresponds to an installment due date.
*/
loadPenalties() {
this.penaltyManagementService.loadPenalties(this.loanId).subscribe({
next: (penalties: any[]) => {
this.penalties = penalties;
},
error: (error: any) => {
console.error('Error loading penalties:', error);
this.penalties = [];
}
});
}
/**
* Toggle waive penalties
* Following the addPaymentDetails() pattern
*/
toggleWaivePenalties() {
this.waivePenalties = !this.waivePenalties;
if (!this.waivePenalties) {
// Reset selections when toggling off
this.selectedPenalties = [];
this.selectAllPenalties = false;
this.updateTransactionAmountValidators(false);
this.recalculateTransactionAmount();
} else {
this.recalculateTransactionAmount();
}
}
/**
* Toggle select all penalties
* Following the toggleSelects() pattern from loans-active-client-members
*/
toggleSelectAllPenalties() {
const result = this.penaltyManagementService.toggleSelectAllPenalties(this.selectAllPenalties, this.penalties);
this.selectAllPenalties = result.selectAllPenalties;
this.selectedPenalties = result.selectedPenalties;
this.recalculateTransactionAmount();
}
/**
* Toggle individual penalty selection
* Following the toggleSelect() pattern from loans-active-client-members
*/
togglePenaltySelection(penaltyId: number) {
const result = this.penaltyManagementService.togglePenaltySelection(
penaltyId,
this.selectedPenalties,
this.penalties
);
this.selectedPenalties = result.selectedPenalties;
this.selectAllPenalties = result.selectAllPenalties;
this.recalculateTransactionAmount();
}
/**
* Check if penalty is selected
*/
isPenaltySelected(penaltyId: number): boolean {
return this.penaltyManagementService.isPenaltySelected(penaltyId, this.selectedPenalties);
}
/**
* Get penalty display key or plain text for translation/output
* Normalizes common backend values (like MORA / labels.inputs.*) to translation keys
*/
getPenaltyDisplayKey(penalty: any): string {
return this.penaltyManagementService.getPenaltyDisplayKey(penalty);
}
/**
* Recalculate transaction amount when penalties are waived
*/
recalculateTransactionAmount() {
const currentAmountValue = this.repaymentLoanForm.value.transactionAmount;
const currentAmount =
currentAmountValue !== undefined && currentAmountValue !== null
? Number(currentAmountValue)
: Number(this.dataObject.amount ?? 0);
const baseAmount = isNaN(currentAmount) ? Number(this.dataObject.amount ?? 0) : currentAmount;
if (!this.waivePenalties || this.selectedPenalties.length === 0) {
// Reset to original amount if no penalties selected
this.repaymentLoanForm.patchValue(
{
transactionAmount: baseAmount
},
{ emitEvent: false }
);
return;
}
// Calculate total waived amount
let totalWaived = 0;
this.selectedPenalties.forEach((penaltyId: number) => {
const penalty = this.penalties.find((p: any) => p.id === penaltyId);
if (penalty) {
totalWaived += penalty.amountOutstanding || penalty.amount || 0;
}
});
// Calculate new transaction amount
const newAmount = Math.max(0, baseAmount - totalWaived);
// Allow zero when fully waived
this.updateTransactionAmountValidators(this.waivePenalties && newAmount === 0);
this.repaymentLoanForm.patchValue(
{
transactionAmount: newAmount
},
{ emitEvent: false }
);
}
/**
* Update transaction amount validators to allow or disallow zero
*/
private updateTransactionAmountValidators(allowZero: boolean) {
const validators = [
Validators.required,
Validators.min(allowZero ? 0 : 0.001)
];
if (this.isCapitalizedIncome()) {
validators.push(Validators.max(this.dataObject.amount));
}
this.repaymentLoanForm.controls.transactionAmount.setValidators(validators);
this.repaymentLoanForm.controls.transactionAmount.updateValueAndValidity({ emitEvent: false });
}
/** Submits the repayment form */
submit() {
const repaymentLoanFormData = this.repaymentLoanForm.value;
@@ -188,6 +325,12 @@ export class MakeRepaymentComponent implements OnInit {
data.interestRefundCalculation = false;
}
delete data.skipInterestRefund;
// Add waived penalty charge IDs if penalties are being waived
if (this.waivePenalties && this.selectedPenalties.length > 0) {
data.chargeIds = this.selectedPenalties;
}
this.loanService.submitLoanActionButton(this.loanId, data, this.command).subscribe((response: any) => {
this.router.navigate(['../../transactions'], { relativeTo: this.route });
});