import { Component, Input, OnInit } from '@angular/core'; import { FormGroup, FormBuilder, Validators } from '@angular/forms'; import { BsModalRef } from 'ngx-bootstrap'; import { ToastrService } from 'ngx-toastr'; import { AccountNameService } from '../../../services/account.name.service'; @Component({ selector: 'app-create-account', templateUrl: './create-account.component.html', styleUrls: ['./create-account.component.css'] }) export class CreateAccountComponent implements OnInit { accountForm: FormGroup; @Input() title: string; allHeadAccounts: any; allAccountTypes: any; allAccounts: any; markAsSubAccount: boolean = false; enableParentAccountDetails: boolean = false; isBankAccountType: boolean = false; eligibleAccountTypes = [2, 3, 5, 8, 10, 13, 14, 16, 17]; constructor( public bsModalRef: BsModalRef, private toastr: ToastrService, private fb: FormBuilder, private accountNameService: AccountNameService){} ngOnInit(): void{ this.accountNameService.getAllHeadAccounts().subscribe((result: { status: string, message: string, data }) => { this.allHeadAccounts = result.data.map((headAccount) => { return { id: headAccount.head_account_id, name: headAccount.account_name } }); }); this.accountForm = this.fb.group({ head_account_name: ['', Validators.required], description: [''], account_name: ['', Validators.required], account_type_name: ['', Validators.required], parent_account: ['', Validators.required], sub_account: [false], account_code: ['', { validators: [Validators.pattern("^[a-zA-Z ]*$")] }], account_number: ['', { validators: [Validators.required, Validators.pattern("^[0-9]*$")] }] }); } onHeadAccountChanged(headAccountObj) { if (this.accountForm.get('head_account_name').value) { this.accountForm.get('account_type_name').setValue(''); this.accountNameService.getAllAcountTypeNamesByHeadAccountId(headAccountObj.id).subscribe((result: { status: string, message: string, data }) => { if(result && result.data){ this.allAccountTypes = result.data; } }); } } onAccountTypeChanged(accountTypeObj){ if (this.accountForm.get('account_type_name').value) { this.enableParentAccountDetails = false; this.accountForm.get('sub_account').setValue(false); this.accountForm.get('parent_account').setValue(''); if(accountTypeObj.account_type_id === 4){ this.isBankAccountType = true; this.markAsSubAccount = false; this.removeParentAccountValidators(); this.setAccountNumberValidators(); } else{ this.isBankAccountType = false; this.removeAccountNumberValidators(); let validAccountType = this.eligibleAccountTypes.find(accountType => { return accountType === accountTypeObj.account_type_id }); if(validAccountType){ this.setParentAccountValidators(); this.markAsSubAccount = true; } else{ this.markAsSubAccount = false; } } } } setAsSubAccount(){ this.enableParentAccountDetails = !this.enableParentAccountDetails; if (this.enableParentAccountDetails && this.accountForm.get('account_type_name').value) { this.accountForm.get('parent_account').setValue(''); this.accountNameService.getAccountsByAccountTypeId(this.accountForm.get('account_type_name').value).subscribe((result: { status: string, message: string, data }) => { if(result && result.data){ this.allAccounts = result.data; } }); } } async checkDuplicateName() { if (this.accountForm.get('account_name').value) { let accountName = this.accountForm.get('account_name').value.toString().trim(); // Duplicate account name check const body = await this.accountNameService.isExistingAccount(accountName); let accountExist = body['data'] as boolean; if (accountExist) { this.accountForm.get('account_name').setValue(''); } } } removeAccountNumberValidators(){ this.accountForm.get('account_number').clearValidators(); this.accountForm.get('account_number').updateValueAndValidity(); } setAccountNumberValidators(){ this.accountForm.get('account_number').setValidators([Validators.required, Validators.pattern("^[0-9]*$")]); this.accountForm.get('account_number').updateValueAndValidity(); } removeParentAccountValidators(){ this.accountForm.get('parent_account').clearValidators(); this.accountForm.get('parent_account').updateValueAndValidity(); } setParentAccountValidators(){ this.accountForm.get('parent_account').setValidators(Validators.required); this.accountForm.get('parent_account').updateValueAndValidity(); } async onSubmit(){ if( this.accountForm.get('head_account_name').value === '' || this.accountForm.get('account_type_name').value === '' || this.accountForm.get('account_name').value === '' ){ this.accountForm.controls['head_account_name'].markAllAsTouched(); this.accountForm.controls['account_type_name'].markAllAsTouched(); this.accountForm.controls['account_name'].markAllAsTouched(); } if(this.isBankAccountType && this.accountForm.get('account_number').value === ''){ this.accountForm.controls['account_number'].markAllAsTouched(); } if(this.enableParentAccountDetails && this.accountForm.get('parent_account').value === ''){ this.accountForm.controls['parent_account'].markAllAsTouched(); }else{ this.removeParentAccountValidators(); } if(this.accountForm.valid){ const accountFormValues: any = {}; accountFormValues.account_type_id = this.accountForm.get('account_type_name').value; accountFormValues.account_name = this.accountForm.get('account_name').value; accountFormValues.sub_account = this.accountForm.get('sub_account').value; accountFormValues.account_code = this.accountForm.get('account_code').value ? this.accountForm.get('account_code').value : null; accountFormValues.account_number = this.accountForm.get('account_number').value ? this.accountForm.get('account_number').value : null; accountFormValues.description = this.accountForm.get('description').value; accountFormValues.parent_acc_id = this.accountForm.get('parent_account').value ? this.accountForm.get('parent_account').value : null; this.accountNameService.createAccount(accountFormValues).subscribe((result: { status: string, message: string, data }) => { if (result.status === 'success') { this.toastr.success('Account created successfully!'); } }, (error) => { this.toastr.error('Account Creation failed. Please try again!'); }); this.bsModalRef.hide(); } } get head_account_name() { return this.accountForm.get('head_account_name') } get account_name() { return this.accountForm.get('account_name') } get account_type_name() { return this.accountForm.get('account_type_name') } get parent_account() { return this.accountForm.get('parent_account') } get account_code() { return this.accountForm.get('account_code') } get account_number() { return this.accountForm.get('account_number') } }