/** * 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 { UntypedFormBuilder, UntypedFormGroup, Validators, ReactiveFormsModule } from '@angular/forms'; import { Router, ActivatedRoute, RouterLink } from '@angular/router'; /** Custom Services */ import { SystemService } from '../../system.service'; import { STANDALONE_SHARED_IMPORTS } from 'app/standalone-shared.module'; import { AlertService } from 'app/core/alert/alert.service'; /** * Create Account Number Preference Component. */ @Component({ selector: 'mifosx-create-account-number-preference', templateUrl: './create-account-number-preference.component.html', styleUrls: ['./create-account-number-preference.component.scss'], imports: [ ...STANDALONE_SHARED_IMPORTS ] }) export class CreateAccountNumberPreferenceComponent implements OnInit { private formBuilder = inject(UntypedFormBuilder); private systemService = inject(SystemService); private route = inject(ActivatedRoute); private router = inject(Router); private alertService = inject(AlertService); /** Account Number Preferences Form */ accountNumberPreferenceForm: UntypedFormGroup; /** Account Number Preferences Template Data */ accountNumberPreferencesTemplateData: any; /** Prefix Type Data */ prefixTypeData: any[]; /** * Retrieves the account number preferences template data from `resolve`. * @param {FormBuilder} formBuilder Form Builder. * @param {SystemService} systemService Accounting Service. * @param {ActivatedRoute} route Activated Route. * @param {Router} router Router for navigation. */ constructor() { this.route.data.subscribe((data: { accountNumberPreferencesTemplate: any }) => { this.accountNumberPreferencesTemplateData = data.accountNumberPreferencesTemplate; }); } /** * Creates the account number preference form. * Subscribe on Form Controls to change Prefix Type data. */ ngOnInit() { this.createAccountNumberPreferenceForm(); this.getPrefixTypeValue(); } /** * Subscribes on Form Controls to change Prefix Type data. */ getPrefixTypeValue() { this.accountNumberPreferenceForm.get('accountType').valueChanges.subscribe((accountId) => { this.prefixTypeData = this.accountNumberPreferencesTemplateData.prefixTypeOptions[ `accountType.${this.accountNumberPreferencesTemplateData.accountTypeOptions.find((accountType: any) => accountType.id === accountId).value.toLowerCase()}` ]; }); } /** * Creates the account number preference form. */ createAccountNumberPreferenceForm() { this.accountNumberPreferenceForm = this.formBuilder.group({ accountType: [ '', Validators.required ], prefixType: [''] }); } /** * Submits the account number preference form and creates a account number preference, * if successful redirects to view created account number preference. */ submit() { const accountNumberPreference = this.accountNumberPreferenceForm.value; if (accountNumberPreference.prefixType === '') { accountNumberPreference.prefixType = undefined; } this.systemService.createAccountNumberPreference(accountNumberPreference).subscribe((response: any) => { if (response?.rollbackTransaction) { this.alertService.alert({ type: 'Success', message: 'Account number preference creation has been submitted for approval.' }); this.router.navigate(['../'], { relativeTo: this.route }); return; } this.router.navigate( [ '../', response.resourceId ], { relativeTo: this.route } ); }); } }