/** * 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'; /** Custom Services */ import { CurrentAccountsService } from '../../current-accounts.service'; import { SettingsService } from 'app/settings/settings.service'; import { Dates } from 'app/core/utils/dates'; import { FacilityChargeFilterService } from 'app/shared/charges/facility-charge-filter.service'; import { STANDALONE_SHARED_IMPORTS } from 'app/standalone-shared.module'; /** * Add Current Account Charge component. */ @Component({ selector: 'mifosx-add-charge-current-account', templateUrl: './add-charge-current-account.component.html', styleUrls: ['./add-charge-current-account.component.scss'], imports: [ ...STANDALONE_SHARED_IMPORTS ] }) export class AddChargeCurrentAccountComponent implements OnInit { private formBuilder = inject(UntypedFormBuilder); private route = inject(ActivatedRoute); private router = inject(Router); private dateUtils = inject(Dates); private currentAccountsService = inject(CurrentAccountsService); private facilityChargeFilter = inject(FacilityChargeFilterService); private settingsService = inject(SettingsService); /** Minimum Due Date allowed. */ minDate = new Date(2000, 0, 1); /** Maximum Due Date allowed. */ maxDate = new Date(); /** Add Current Account Charge form. */ currentAccountChargeForm: UntypedFormGroup; /** current account charge options. */ currentAccountChargeOptions: any; /** current account Id of the current account. */ currentAccountId: 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 {CurrentAccountsService} currentAccountsService Current Accounts Service * @param {SettingsService} settingsService Settings Service */ constructor() { this.route.data.subscribe((data: { currentAccountActionData: any }) => { this.facilityChargeFilter .excludeFacilityCharges(data.currentAccountActionData.chargeOptions) .subscribe((charges: any[]) => (this.currentAccountChargeOptions = charges)); }); this.currentAccountId = this.route.snapshot.params['currentAccountId']; } /** * Creates the Current Account Charge form. */ ngOnInit() { this.maxDate = this.settingsService.businessDate; this.createCurrentAccountChargeForm(); this.buildDependencies(); } buildDependencies() { this.currentAccountChargeForm.controls.chargeId.valueChanges.subscribe((chargeId) => { this.currentAccountsService.getChargeTemplate(chargeId).subscribe((data: any) => { this.chargeDetails = data; const chargeTimeType = data.chargeTimeType.id; if ( data.chargeTimeType.value === 'Withdrawal Fee' || data.chargeTimeType.value === 'Current Account 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.currentAccountChargeForm.addControl('dueDate', new UntypedFormControl('', Validators.required)); } else { this.currentAccountChargeForm.removeControl('dueDate'); } if (!this.chargeDetails.dueDateNotRequired && this.chargeDetails.chargeTimeTypeAnnualOrMonth) { this.currentAccountChargeForm.addControl('feeOnMonthDay', new UntypedFormControl('', Validators.required)); } else { this.currentAccountChargeForm.removeControl('feeOnMonthDay'); } if (chargeTimeType.value === 'Monthly Fee') { this.currentAccountChargeForm.addControl( 'feeInterval', new UntypedFormControl(data.feeInterval, Validators.required) ); } else { this.currentAccountChargeForm.removeControl('feeInterval'); } this.currentAccountChargeForm.patchValue({ amount: data.amount, chargeCalculationType: data.chargeCalculationType.id, chargeTimeType: data.chargeTimeType.id }); }); }); } /** * Creates the Current Account Charge form. */ createCurrentAccountChargeForm() { this.currentAccountChargeForm = this.formBuilder.group({ chargeId: [ '', Validators.required ], amount: [ '', Validators.required ], chargeCalculationType: [{ value: '', disabled: true }], chargeTimeType: [{ value: '', disabled: true }] }); } /** * Submits current account charge. */ submit() { const currentAccountCharge = this.currentAccountChargeForm.value; currentAccountCharge.locale = this.settingsService.language.code; if (!currentAccountCharge.feeInterval) { currentAccountCharge.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 currentAccountCharge.monthDayFormat = monthDayFormat; if (currentAccountCharge.feeOnMonthDay) { const prevDate = this.currentAccountChargeForm.value.feeOnMonthDay; currentAccountCharge.feeOnMonthDay = this.dateUtils.formatDate(prevDate, monthDayFormat); } } else { const dateFormat = this.settingsService.dateFormat; currentAccountCharge.dateFormat = dateFormat; if (currentAccountCharge.dueDate) { const prevDate = this.currentAccountChargeForm.value.dueDate; currentAccountCharge.dueDate = this.dateUtils.formatDate(prevDate, dateFormat); } } } this.currentAccountsService .createCurrentAccountCharge(this.currentAccountId, 'charges', currentAccountCharge) .subscribe(() => { this.router.navigate(['../../transactions'], { relativeTo: this.route }); }); } }