Files
cbsnexora/src/app/deposits/fixed-deposits/fixed-deposits-account-actions/add-charge-fixed-deposits-account/add-charge-fixed-deposits-account.component.ts
T
2026-01-31 07:07:59 -06:00

171 lines
6.2 KiB
TypeScript

/**
* Copyright since 2025 Mifos Initiative
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
/** Angular Imports */
import { Component, OnInit, inject } from '@angular/core';
import {
UntypedFormGroup,
UntypedFormBuilder,
Validators,
UntypedFormControl,
ReactiveFormsModule
} from '@angular/forms';
import { Router, ActivatedRoute, RouterLink } from '@angular/router';
import { Dates } from 'app/core/utils/dates';
/** Custom Services */
import { SavingsService } from 'app/savings/savings.service';
import { SettingsService } from 'app/settings/settings.service';
import { STANDALONE_SHARED_IMPORTS } from 'app/standalone-shared.module';
/**
* Add Fixed Deposits Charge component.
* Fixed deposits endpoint is not supported so using Savings endpoint.
*/
@Component({
selector: 'mifosx-add-charge-fixed-deposits-account',
templateUrl: './add-charge-fixed-deposits-account.component.html',
styleUrls: ['./add-charge-fixed-deposits-account.component.scss'],
imports: [
...STANDALONE_SHARED_IMPORTS
]
})
export class AddChargeFixedDepositsAccountComponent implements OnInit {
private formBuilder = inject(UntypedFormBuilder);
private route = inject(ActivatedRoute);
private router = inject(Router);
private dateUtils = inject(Dates);
private savingsService = inject(SavingsService);
private settingsService = inject(SettingsService);
/** Minimum Due Date allowed. */
minDate = new Date(2000, 0, 1);
/** Maximum Due Date allowed. */
maxDate = new Date();
/** Add Fixed Deposits Charge form. */
fixedDepositsChargeForm: UntypedFormGroup;
/** savings charge options. */
savingsChargeOptions: any;
/** savings Id of the savings account. */
fixedDepositAccountId: string;
/** charge details */
chargeDetails: any;
/**
* Retrieves charge template data from `resolve`
* @param {FormBuilder} formBuilder Form Builder
* @param {ActivatedRoute} route Activated Route
* @param {Router} router Router
* @param {Dates} dateUtils Date Utils
* @param {SavingsService} savingsService Savings Service
* @param {SettingsService} settingsService Settings Service
*/
constructor() {
this.route.data.subscribe((data: { fixedDepositsAccountActionData: any }) => {
this.savingsChargeOptions = data.fixedDepositsAccountActionData.chargeOptions;
});
this.fixedDepositAccountId = this.route.parent.snapshot.params['fixedDepositAccountId'];
}
/**
* Creates the Fixed Deposits Charge form.
*/
ngOnInit() {
this.maxDate = this.settingsService.businessDate;
this.createFixedDepositsChargeForm();
this.buildDependencies();
}
buildDependencies() {
this.fixedDepositsChargeForm.controls.chargeId.valueChanges.subscribe((chargeId) => {
this.savingsService.getChargeTemplate(chargeId).subscribe((data: any) => {
this.chargeDetails = data;
const chargeTimeType = data.chargeTimeType.id;
if (data.chargeTimeType.value === 'Withdrawal Fee' || data.chargeTimeType.value === 'Saving No Activity Fee') {
this.chargeDetails.dueDateNotRequired = true;
}
if (data.chargeTimeType.value === 'Annual Fee' || data.chargeTimeType.value === 'Monthly Fee') {
this.chargeDetails.chargeTimeTypeAnnualOrMonth = true;
}
if (!this.chargeDetails.dueDateNotRequired && !this.chargeDetails.chargeTimeTypeAnnualOrMonth) {
this.fixedDepositsChargeForm.addControl('dueDate', new UntypedFormControl('', Validators.required));
} else {
this.fixedDepositsChargeForm.removeControl('dueDate');
}
if (!this.chargeDetails.dueDateNotRequired && this.chargeDetails.chargeTimeTypeAnnualOrMonth) {
this.fixedDepositsChargeForm.addControl('feeOnMonthDay', new UntypedFormControl('', Validators.required));
} else {
this.fixedDepositsChargeForm.removeControl('feeOnMonthDay');
}
if (chargeTimeType.value === 'Monthly Fee') {
this.fixedDepositsChargeForm.addControl(
'feeInterval',
new UntypedFormControl(data.feeInterval, Validators.required)
);
} else {
this.fixedDepositsChargeForm.removeControl('feeInterval');
}
this.fixedDepositsChargeForm.patchValue({
amount: data.amount,
chargeCalculationType: data.chargeCalculationType.id,
chargeTimeType: data.chargeTimeType.id
});
});
});
}
/**
* Creates the Fixed Deposits Charge form.
*/
createFixedDepositsChargeForm() {
this.fixedDepositsChargeForm = this.formBuilder.group({
chargeId: [
'',
Validators.required
],
amount: [
'',
Validators.required
],
chargeCalculationType: [{ value: '', disabled: true }],
chargeTimeType: [{ value: '', disabled: true }]
});
}
/**
* Submits savings charge.
*/
submit() {
const savingsCharge = this.fixedDepositsChargeForm.value;
savingsCharge.locale = this.settingsService.language.code;
if (!savingsCharge.feeInterval) {
savingsCharge.feeInterval = this.chargeDetails.feeInterval;
}
if (this.chargeDetails.dueDateNotRequired !== true) {
if (this.chargeDetails.chargeTimeTypeAnnualOrMonth) {
const monthDayFormat = 'MMMM-dd'; // TODO: Update once language and date settings are setup
savingsCharge.monthDayFormat = monthDayFormat;
if (savingsCharge.feeOnMonthDay) {
const prevDate = this.fixedDepositsChargeForm.value.feeOnMonthDay;
savingsCharge.feeOnMonthDay = this.dateUtils.formatDate(prevDate, monthDayFormat);
}
} else {
const dateFormat = this.settingsService.dateFormat;
savingsCharge.dateFormat = dateFormat;
if (savingsCharge.dueDate) {
const prevDate = this.fixedDepositsChargeForm.value.dueDate;
savingsCharge.dueDate = this.dateUtils.formatDate(prevDate, dateFormat);
}
}
}
this.savingsService.createSavingsCharge(this.fixedDepositAccountId, 'charges', savingsCharge).subscribe(() => {
this.router.navigate(['../../'], { relativeTo: this.route });
});
}
}