import { Component, OnInit, ViewChild } from '@angular/core'; import { ActivatedRoute } from '@angular/router'; import { VisaMasterDetailModel, VisaexpenseVieModel, VisaInstallmentVieModel, VisaAdjustmentViewModel } from '../employee-class'; import { EmployeeServices } from '../employee-services'; import { NotificationService } from '../../../_services/notification.service'; import { StoreService } from '../../store/store.services'; import { KGridHelper } from '../../../_helpers/k-grid.class'; import { Subject, Subscription } from 'rxjs'; import { debounceTime } from 'rxjs/operators'; import { SortDescriptor } from '@progress/kendo-data-query'; import { PageChangeEvent } from '@progress/kendo-angular-grid/dist/es2015/main'; import { SettingServices } from '../../settings/setting.services'; import { ItemViewModel } from '../../stock/MedStock.class'; import { VisaExpense } from '../../settings/setting.class'; import { LocalService } from '../../../_services/local.service'; import { NgForm } from '@angular/forms'; import { FormCanDeactivate } from '../../../_guards/form-can-deactivate'; @Component({ selector: 'app-add-employee-visa', templateUrl: './add-employee-visa.component.html' }) export class AddEmployeeVisaComponent extends FormCanDeactivate implements OnInit { @ViewChild('visaform') form: NgForm; public openDilog = false; public openDilogCat = false; public kGrid: KGridHelper = new KGridHelper(); public searchEvent = new Subject(); public searchSubcription: Subscription = null; public openDilogConfirmation = false; public _isEdit = false; public _visaExpenseList: VisaexpenseVieModel[] = []; public visaExpensheadList: VisaExpense[] = []; public employeeAccountList: any[] = []; public paymentAccountList: any[] = []; public expenseAccountList: any[] = []; public incomeAccountList: any[] = []; public expenseIncomeAccountList: any[] = []; public _visaMasterDetailVM: VisaMasterDetailModel = new VisaMasterDetailModel(); public _visaInstallmentVM: VisaInstallmentVieModel = new VisaInstallmentVieModel(); public _visaExpenseVM: VisaexpenseVieModel = new VisaexpenseVieModel(); public _visaExpenseVMAttachMent: File; public _remainingAmount = 0; public _remainingInstalments = 0; public nowDate = new Date(); public maxDate = new Date(); public _visaAdjustmentVm: VisaAdjustmentViewModel; public openVisAdjustmentDialog = false; public openAddVisaExpenseDialog = false; public openAddVisaExpenseDialog_View = false; public openAddVisaInstalmentDialog = false; public openDeleteConfirmationDialog = false; public saving = false; public deleting = false; public client: any; constructor(public activatedRoute: ActivatedRoute, public _visaService: EmployeeServices, public _notificationService: NotificationService, public _settingService: SettingServices, public _storeService: StoreService, public _localService: LocalService) { super(); } ngOnInit() { this.client = this._localService.get('com'); this.activatedRoute.queryParams.subscribe(params => { const visaId = params['id']; if (visaId != null) { this.getVisaById(visaId); this.getAccount(' ', 'E'); this.getAccount(' ', 'CaB'); this.getAccount(' ', 'Exp'); this.getAccount(' ', 'Incom'); this.getAccount(' ', 'ExpIncome'); this.getAccount(' ', 'VisaIncom'); this.getVisaExpenseHeads(); this.getVisaExpenseListbyVisaId(visaId); this._isEdit = true; } else { this.validateVisaCode('-1'); this._visaMasterDetailVM.CreatedDate = this.nowDate; this._isEdit = false; } }); this.maxDate = new Date(); this.maxDate.setHours(this.maxDate.getHours() + 5); this.openAddVisaExpenseDialog = false; this.openAddVisaInstalmentDialog = false; this.initializeProps(); this.handleSearchEvents(); } private initializeProps() { this.kGrid.loading = false; this.kGrid.pageSize = 20; this.kGrid.pageSizes = [20, 50, 100]; } onSubmit() { this.saving = true; this._visaService.saveVisaMasterDetail(this._visaMasterDetailVM).subscribe((res) => { this._notificationService.notify('success', 'Visa Saved Successfully'); this.saving = false; if (this._isEdit == false) { this.ResetVisaForm(); } }, (err) => { this._notificationService.notify('danger', 'Something Went Wrong'); }); } onSubmitVisaExpense() { let iv = new ItemViewModel(); iv.Id = this._visaMasterDetailVM.Id; this._visaExpenseVM.Fk_VisaMasterDetail = iv; console.log(this._visaExpenseVM); this._visaService.saveVisaExpense(this._visaExpenseVM, this._visaExpenseVMAttachMent).subscribe((res) => { this.getVisaById(this._visaMasterDetailVM.Id); this.CloseVisaExpenseDialog(); this._notificationService.notify('success', 'Visa expense Saved Successfully'); this._visaExpenseVM = new VisaexpenseVieModel(); this.getVisaExpenseListbyVisaId(this._visaMasterDetailVM.Id); }, (err) => { this._notificationService.notify('danger', 'Something Went Wrong'); }); } onSubmitVisaInstallment() { let iv = new ItemViewModel(); iv.Id = this._visaMasterDetailVM.Id; this._visaInstallmentVM.Fk_VisaMasterDetail = iv; this._visaService.saveVisaInstalment(this._visaInstallmentVM).subscribe((res) => { this.getVisaById(this._visaMasterDetailVM.Id); this.CloseVisaInstallmentDialog(); this._notificationService.notify('success', 'Visa Installment Saved Successfully'); this._visaInstallmentVM = new VisaInstallmentVieModel(); }, (err) => { this._notificationService.notify('danger', 'Something Went Wrong'); }); } OpenVisaAdjustmentDialog() { this.openVisAdjustmentDialog = true; this._visaAdjustmentVm = new VisaAdjustmentViewModel(); this._visaAdjustmentVm.AdjustmentDate = new Date(); this.getAccount(' ', 'ExpIncome'); } CloseVisaAdjustmentDialog() { this.openVisAdjustmentDialog = false; } onSubmitVisaAdjustment() { this._visaAdjustmentVm.Ref_Id = this._visaMasterDetailVM.RefId; this._visaAdjustmentVm.Visa_Mast_Id = new ItemViewModel(this._visaMasterDetailVM.Id, this._visaMasterDetailVM.RefId); this._visaService.saveVisaAdjustment(this._visaAdjustmentVm).subscribe((res) => { this._notificationService.notify('success', 'Visa expense Saved Successfully'); this._visaAdjustmentVm = new VisaAdjustmentViewModel(); this._visaAdjustmentVm.AdjustmentDate = new Date(); this.CloseVisaAdjustmentDialog(); }, (err) => { this._notificationService.notify('danger', 'Something Went Wrong'); }); } OpenVisaExpenseDialog() { this.openAddVisaExpenseDialog = true; this._visaExpenseVM.CreatedDate = new Date(); this.getAccount(' ', 'Exp'); } OpenVisaExpenseDialogForEdit(item) { this.getAccount(' ', 'Exp'); this.getVisaExpenseHeads(); this._visaExpenseVM = item; this.openAddVisaExpenseDialog = true; this.openAddVisaExpenseDialog_View = false; this._visaExpenseVM.Fk_VisaExpenseHead = item.Fk_VisaExpenseHead; this._visaExpenseVM.Fk_ExpenseAccount = item.Fk_ExpenseAccount; this._visaExpenseVM.Fk_PaymentAccount = item.Fk_PaymentAccount; this._visaExpenseVM.CreatedDate = new Date(item.CreatedDate); this.getVisaExpenseListbyVisaId(this._visaMasterDetailVM.Id); } OpenVisaExpenseDialogForView(item) { this.getAccount(' ', 'Exp'); this.getVisaExpenseHeads(); this._visaExpenseVM = item; this.openAddVisaExpenseDialog_View = true; this._visaExpenseVM.Fk_VisaExpenseHead = item.Fk_VisaExpenseHead; this._visaExpenseVM.Fk_ExpenseAccount = item.Fk_ExpenseAccount; this._visaExpenseVM.Fk_PaymentAccount = item.Fk_PaymentAccount; this._visaExpenseVM.CreatedDate = new Date(item.CreatedDate); this.getVisaExpenseListbyVisaId(this._visaMasterDetailVM.Id); } OpenDeleteConfirmationDialog(item) { this.openDeleteConfirmationDialog = true; this._visaExpenseVM = item; } closeDeleteDialog() { this.openDeleteConfirmationDialog = false; } ConfirmDelete() { this.deleting = true; this._visaService.deleteVisaExpense(this._visaExpenseVM).subscribe((res) => { this._notificationService.notify('success', 'Visa expense Delete Successfully'); this.closeDeleteDialog(); this.deleting = false; this._visaExpenseVM = new VisaexpenseVieModel(); this.getVisaExpenseListbyVisaId(this._visaMasterDetailVM.Id); }, (err) => { this._notificationService.notify('danger', 'Something Went Wrong'); }); } CloseVisaExpenseDialog() { this.openAddVisaExpenseDialog = false; this.openAddVisaExpenseDialog_View = false; this._visaExpenseVM = new VisaexpenseVieModel(); } OpenVisaInstallmentDialog() { this.openAddVisaInstalmentDialog = true; this._visaInstallmentVM.CreatedDate = new Date(); this._visaInstallmentVM.InstallmentNo = (this._visaMasterDetailVM.PaidInstallments + 1); this._visaInstallmentVM.Fk_EmployeAccount = this._visaMasterDetailVM.Fk_EmployeAccount; this._visaInstallmentVM.InstallmentAmount = this._remainingAmount / this._remainingInstalments; console.log(this._visaMasterDetailVM.Fk_EmployeAccount); this.getAccount(' ', 'E'); this.getAccount(' ', 'Incom'); this.getAccount(' ', 'CaB'); this.getVisaExpenseHeads(); this.validateVisaInstalmentCode('-1'); } CloseVisaInstallmentDialog() { this.openAddVisaInstalmentDialog = false; this._visaInstallmentVM = new VisaInstallmentVieModel(); } ResetVisaForm() { this._visaMasterDetailVM = new VisaMasterDetailModel(); this.validateVisaCode('-1'); } ClearForm() { let code = this._visaMasterDetailVM.RefId; this._visaMasterDetailVM = new VisaMasterDetailModel(); this._visaMasterDetailVM.RefId = code; this._visaMasterDetailVM.TotalExpense = 0; if (this._visaExpenseList !== undefined && this._visaExpenseList.length > 0) { this._visaExpenseList.forEach(ve => { this._visaMasterDetailVM.TotalExpense += ve.TotalExpense; }); } } getVisaExpenseListbyVisaId(id: number) { this.kGrid.loading = true; this._visaService.getVisaExpenseListByVisaId(id).subscribe((res: any) => { this._visaExpenseList = res; // this._visaMasterDetailVM.TotalExpense = 0; // if (this._visaExpenseList !== undefined && this._visaExpenseList.length > 0) { // this._visaExpenseList.forEach(ve => { // this._visaMasterDetailVM.TotalExpense += ve.TotalExpense; // }); // } this.kGrid.data = []; this.kGrid.data = res; this.kGrid.totalRecords = this.kGrid.data.length; this.kGrid.gridView = { data: this.kGrid.data, total: this.kGrid.totalRecords }; this.kGrid.loading = false; }, (err) => { this._notificationService.notify('danger', 'Something Went Wrong'); }); } public getFIle(event) { this._visaExpenseVMAttachMent = event.target.files[0]; } getVisaExpenseList() { this.kGrid.loading = true; this._visaService.getVisaExpenseList().subscribe((res: any) => { this._visaExpenseList = res; this.kGrid.data = []; this.kGrid.data = res; this.kGrid.totalRecords = this.kGrid.data.length; this.kGrid.gridView = { data: this.kGrid.data, total: this.kGrid.totalRecords }; this.kGrid.loading = false; console.log(res); }, (err) => { this._notificationService.notify('danger', 'Something Went Wrong'); }); } getVisaById(id: number) { this._visaService.getVisaById(id).subscribe((res: any) => { console.log(res); this._visaMasterDetailVM = res; this._visaMasterDetailVM.CreatedDate = new Date(res.CreatedDate); this._visaMasterDetailVM.Fk_EmployeAccount = res.Fk_EmployeAccount; this._remainingAmount = (this._visaMasterDetailVM.EmployeShare - this._visaMasterDetailVM.ReceivedAmount); this._remainingInstalments = this._visaMasterDetailVM.NoOfInstallment - this._visaMasterDetailVM.PaidInstallments; this._visaMasterDetailVM.IsAutomatic = res.IsAutomatic; // this._notificationService.notify('danger','Something Went Wrong'); this.getVisaExpenseListbyVisaId(res.Id); }, (err) => { this._notificationService.notify('danger', 'Something Went Wrong'); }); } validateVisaCode(code) { this._visaService.validateVisaCode(code).subscribe((res: any) => { if (res.msg === 'Valid') { this._visaMasterDetailVM.RefId = res.code; } }, (err) => { this._notificationService.notify('danger', 'Something Went Wrong'); }); } validateVisaInstalmentCode(code) { this._visaService.validateVisaInstallmentCode(code).subscribe((res: any) => { if (res.msg === 'Valid') { this._visaInstallmentVM.RefId = res.code; } }, (err) => { this._notificationService.notify('danger', 'Something Went Wrong'); }); } public getAccount(x: string, event: string) { this._storeService.getAccountBySearch(x, event).subscribe((res: any) => { if (res) { if (event === 'E') { this.employeeAccountList = res; } if (event === 'VisaIncom') { this.incomeAccountList = res; } if (event === 'CaB') { this.paymentAccountList = res; } if (event === 'Exp') { this.expenseAccountList = res; } if (event === 'ExpIncome') { this.expenseIncomeAccountList = res; } } }, err => { this._notificationService.notify('danger', err.msg); }); } public getVisaExpenseHeads() { this._settingService.getVisaExpenseHeadList().subscribe((res: any) => { this.visaExpensheadList = res; console.log(res); }, (err) => { this._notificationService.notify('danger', err.msg); }); } OnVisaExpenseHeadChange(event) { console.log(event); this._visaExpenseVM.Fk_VisaExpenseHead = event; this._visaExpenseVM.Fk_ExpenseAccount = event.Fk_AccountDefination; this._visaExpenseVM.TotalExpense = event.Amount; this._visaExpenseVM.Title = event.Title; } public handleSearchEvents() { this.searchSubcription = this.searchEvent.pipe( debounceTime(400)).subscribe((x: any) => { console.log(x); if (x) { if (x.filter == 'FK_Exp_Acc') { this.getAccount(x.event, 'E'); this.getAccount(x.event, 'Exp'); this.getAccount(x.event, 'VisaIncom'); this.getAccount(x.event, 'CaB'); } if (x.filter == 'FK_ExpIncom_Acc') { this.getAccount(x.event, 'ExpIncome'); } this.getVisaExpenseHeads(); } }); } public changePagesize(value: any) { this.kGrid.pageSize = +value; this.kGrid.skip = 0; // this.getCategory(); } public sortChange(sort: SortDescriptor[]): void { if (sort[0].field == 'asd') { return; } this.kGrid.sort = sort; // this.sortData(); } // private sortData() { // this.kGrid.gridView = { // data: orderBy(this.kGrid.data, this.kGrid.sort), // total: this.kGrid.totalRecords // }; // } public pageChange(event: PageChangeEvent): void { this.kGrid.skip = event.skip; // this.getCategory(); } }