import { Component, OnInit } from '@angular/core'; import { FormGroup, FormBuilder, Validators } from '@angular/forms'; import { BsModalRef } from 'ngx-bootstrap'; import { ToastrService } from 'ngx-toastr'; import { TaxSlabService } from '../../../services/tax.slab.service'; import { TaxGroupService } from '../../../services/tax.group.service'; @Component({ selector: 'app-create-taxes', templateUrl: './create-taxes.component.html', styleUrls: ['./create-taxes.component.css'] }) export class CreateTaxesComponent implements OnInit { taxForm: FormGroup; title: string = 'Create IGST'; constructor( public bsModalRef: BsModalRef, private taxGroupService: TaxGroupService, private taxSlabService: TaxSlabService, private toastr: ToastrService, private fb: FormBuilder){ } ngOnInit(): void{ this.taxForm = this.fb.group({ tax_slab_name: ['', Validators.required], percentage: ['', { validators: [Validators.required, Validators.pattern("^[0-9]*$")] }], tax_group_name: ['', Validators.required], taxType: ['tax_slab'] }); } /* @usage: called on blur of tax_slab_name/tax_group_name feild @purpose: to check the tax_slab_name/tax_group_name is already exsits or not. */ async checkDuplicateName() { if (this.taxForm.get('taxType').value === 'tax_slab') { let taxSlabExist, taxSlabName = this.taxForm.get('tax_slab_name').value; // Duplicate user name check if(taxSlabName){ const body = await this.taxSlabService.isExistingTaxSlabName(taxSlabName); taxSlabExist = body['data'] as boolean; if (taxSlabExist) { this.taxForm.get('tax_slab_name').setValue(''); } } }else{ let taxGroupExist, taxGroupName = this.taxForm.get('tax_group_name').value; if(taxGroupName){ const body = await this.taxGroupService.isExistingTaxGroupName(taxGroupName); taxGroupExist = body['data'] as boolean; if (taxGroupExist) { this.taxForm.get('tax_group_name').setValue(''); } } } } removeValidators() { this.taxForm.get('tax_slab_name').clearValidators(); this.taxForm.get('tax_slab_name').updateValueAndValidity(); } setValidators() { this.taxForm.get('tax_slab_name').setValidators(Validators.required); this.taxForm.get('tax_slab_name').updateValueAndValidity(); } toggleTaxType(taxType) { if (taxType === 'tax_slab') { this.title = 'Create IGST'; this.taxForm.get('tax_group_name').clearValidators(); this.taxForm.get('tax_group_name').updateValueAndValidity(); this.setValidators(); } else{ this.title = 'Create GST'; this.taxForm.get('tax_group_name').setValidators(Validators.required); this.taxForm.get('tax_group_name').updateValueAndValidity(); this.removeValidators(); } } async onSubmit(){ let taxType = this.taxForm.get('taxType').value; if(taxType === 'tax_slab'){ this.taxForm.get('tax_group_name').clearValidators(); this.taxForm.get('tax_group_name').updateValueAndValidity(); if (this.taxForm.get('tax_slab_name').value === '' || this.taxForm.get('percentage').value === ''){ this.taxForm.controls['tax_slab_name'].markAllAsTouched(); this.taxForm.controls['percentage'].markAllAsTouched(); } if (this.taxForm.valid){ this.taxForm.value.tax_slab_type = 'IGST'; this.taxSlabService.createTaxSlab(this.taxForm.value).subscribe((result: { status: string, message: string, data }) => { if (result.status === 'success') { this.toastr.success('Tax Slab created successfully!'); } }, (error) => { if(error.status === 500) this.toastr.error('Tax Slab already exists. Please try again!'); else this.toastr.error('Tax Slab Creation failed. Please try again!'); }); this.bsModalRef.hide(); } }else{ if (this.taxForm.get('tax_group_name').value === '' || this.taxForm.get('percentage').value === ''){ this.taxForm.controls['tax_group_name'].markAllAsTouched(); this.taxForm.controls['percentage'].markAllAsTouched(); } if (this.taxForm.valid){ this.taxForm.value.tax_group_type = 'GST'; this.taxGroupService.createTaxGroup(this.taxForm.value).subscribe((result: { status: string, message: string, data }) => { if (result.status === 'success') { this.toastr.success('Tax Group created successfully!'); } }, (error) => { if(error.status === 500) this.toastr.error('Tax Slab already exists. Please try again!'); else this.toastr.error('Tax Slab Creation failed. Please try again!'); }); this.bsModalRef.hide(); } } } get tax_slab_name() { return this.taxForm.get('tax_slab_name') } get percentage() { return this.taxForm.get('percentage') } get tax_group_name() { return this.taxForm.get('tax_group_name') } get taxType() { return this.taxForm.get('taxType') } }