import { Component, OnInit, ViewChild } from '@angular/core'; import { GenralExpenseModel } from '../expense-class'; import { Subject, Subscription } from 'rxjs'; import { StoreService } from '../../store/store.services'; import { NotificationService } from '../../../_services/notification.service'; import { ExpenseServices } from '../expense-services'; import { debounceTime } from 'rxjs/operators'; import { ItemViewModel } from '../../stock/MedStock.class'; import { ActivatedRoute } from '@angular/router'; import { tick } from '@angular/core/src/render3'; import { LocalService } from '../../../_services/local.service'; import { NgForm } from '@angular/forms'; import { FormCanDeactivate } from '../../../_guards/form-can-deactivate'; @Component({ selector: 'app-trafic-fine-expense', templateUrl: './trafic-fine-expense.component.html', }) export class TraficFineExpenseComponent extends FormCanDeactivate implements OnInit { @ViewChild('f') form: NgForm; public mast: GenralExpenseModel public saving = false; public nowDate = new Date(); public feachting = false; public searchEvent = new Subject(); public searchSubcription: Subscription = null; public drAccountList: any[] = []; public crAccountList: any[] = []; public expAccountList: any[] = []; public expHeadList: any[] = []; public itemList: any[] = []; public SaveList: any[] = []; public empList: any[] = []; public itemCategoriesList: any[] = []; public AccCodeValid: any; constructor( public _storeService: StoreService, public _notificationService: NotificationService, public _expenseServices: ExpenseServices, private activatedRoute: ActivatedRoute, private _localService: LocalService ) { super(); } ngOnInit() { //this.getItemCategory(); this.handleSearchEvents(); this.getAccount(` `, `B`); this.getAccount(` `, `A`); // this.getAccount(` `, `E`); this.getAccount(` `, `EmpExp`); //Traffic Fine Collections //Traffic Fine Collections this.getAccount(` `, `Mis`); this.getAccount(` `, `EmpExp`); // this.getAccount(' ', 'FK_Surcharge_Account'); // this.getAccount(' ', 'FK_Cr_Account'); this.getEmp(` `); this.activatedRoute.queryParams.subscribe(params => { const accId = params['id']; if (accId != null) this.getExpenseDtl(accId); else { this.mast = new GenralExpenseModel(); this.mast.FK_Item_Category = new ItemViewModel(); this.mast.FK_Item_Category.Id = 1; this.mast.FK_Item_Category.Name = 'Vehicle'; this.mast.FK_ExpHead = new ItemViewModel(); this.mast.FK_ExpHead.Id = 2; this.mast.FK_ExpHead.Name = 'Trafic Fine '; this.mast.FK_Emp = new ItemViewModel(); this.mast.ExpAmount = 0; this.mast.ExpDate = new Date(); console.log('my Log') // console.log(this.crAccountList); this.validateAccCode(`-1`); } }); this.nowDate = new Date(); this.nowDate.setHours(this.nowDate.getHours() + 5); } public getExpenseDtl(e) { this._expenseServices.getGerExpDtl(e).subscribe((res: any) => { if (res) { this.AccCodeValid = true; this.mast = new GenralExpenseModel(); this.mast = res; if (res.ExpDate) this.mast.ExpDate = new Date(res.ExpDate); this.getItem(` `, 'e'); this.getExpHeads('e'); this.getAccount(` `, `B`); this.getAccount(` `, `Mis`); this.getAccount(` `, `EmpExp`); // this.getAccount(` `, `E`); } }, err => { this._notificationService.notify(`danger`, err.error); }); } public validateAccCode(x) { debugger; this._expenseServices.validateAccCode(x).subscribe((res: any) => { if (res.msg == `Valid`) { this.AccCodeValid = true; this.mast.Code = res.code; } else { this.AccCodeValid = false; } }, err => { this._notificationService.notify(`danger`, err); }); } public onSubmit() { this.saving = true; this.mast.IsGeneralExpense = false; let surchargeAcc = this.mast.FK_Surcharge_Account; let crAcc = this.mast.FK_Cr_Account; let emp: any = Object.assign({}, this.mast); if (this.mast.ExpDate) emp.ExpDate = this.mast.ExpDate.toDateString(); this._expenseServices.saveVehcleExpense(emp).subscribe((res: any) => { this.saving = false; this.mast = new GenralExpenseModel(); this.mast.FK_Item_Category = new ItemViewModel(); this.mast.FK_Item_Category.Id = 1; this.mast.FK_Item_Category.Name = 'Vehicle'; this.mast.FK_ExpHead = new ItemViewModel(); this.mast.FK_ExpHead.Id = 2; this.mast.FK_ExpHead.Name = 'Traffic Fine'; this.mast.FK_Emp = undefined; this.mast.ExpAmount = 0; this.mast.FK_Dr_Account = undefined; this.mast.surchargeAmount = 0; this.mast.FK_Surcharge_Account = surchargeAcc; this.mast.FK_Cr_Account = crAcc; this.validateAccCode(`-1`); this._notificationService.notify(`success`, `Save Successfully!`); }, err => { this._notificationService.notify(`danger`, err.msg); this.saving = false; this.mast = new GenralExpenseModel(); this.mast.CreationDate = new Date(); this.mast.FK_Emp = new ItemViewModel(); this.mast.ExpAmount = 0; this.validateAccCode(`-1`); }); } public ClearForm() { this.mast = new GenralExpenseModel(); this.mast.CreationDate = new Date(); this.mast.FK_Emp = new ItemViewModel(); this.mast.ExpAmount = 0; this.validateAccCode(`-1`); } public getFIle(event) { this.mast.file = event.target.files[0]; } public CalculateVatPlusTotal() { // this.mast.VatAmount = ((this.mast.ExpAmount+this.mast.surchargeAmount)*5)/105; this.mast.NetExpAmount = this.mast.ExpAmount + this.mast.surchargeAmount; //+this.mast.VatAmount; } public getItem(e, f) { if (this.mast.FK_Item_Category == null) { this._notificationService.notify(`danger`, `Select a Catrogry First`); return; } if (f == 'a') { this.mast.FK_Dr_Account = null; this.mast.FK_Emp = null; } this._expenseServices.getItemsVehcleBilling(e, this.mast.FK_Item_Category.Id).subscribe((res: any) => { this.itemList = res; }, err => { }); } public GetEmployee() { this._expenseServices.getEmpAndAcc(this.mast.FK_Item.Id, this.mast.ExpDate,'T').subscribe((res: any) => { if (res.err == `N`) { this.mast.FK_Emp = res.emp; this.mast.FK_Dr_Account = res.acc; } else { this._notificationService.notify(`danger`, `Not Link to any Employee`); } }, err => { this._notificationService.notify(`danger`, err); }); } public UnlockTheRecord(c) { if (c.IsLock) { c.VatAmount = null c.AmountDiff = null; this.mast.ExpAmount -= c.NetExpAmount; c.NetExpAmount = null; } else { c.VatAmount = (c.ExpAmount * 5) / 100; c.NetExpAmount = c.ExpAmount + c.VatAmount; c.AmountDiff = c.ExpAmount - c.PkgAmount; debugger; this.mast.ExpAmount += c.NetExpAmount; } c.IsLock = !c.IsLock; } public getItemCategory() { this._storeService.getItemCategory().subscribe((res: any) => { if (res) { this.itemCategoriesList = res; } }, err => { this._notificationService.notify(`danger`, err.msg); }); } public getAccount(x: string, event: string) { debugger; this._storeService.getAccountBySearch(x, event).subscribe((res: any) => { if (res) { if (event == 'B') { this.expAccountList = res; } // if (event == 'E') // this.drAccountList = res; if (event == 'EmpExp') { this.drAccountList = res; } if (event == 'Mis') { this.crAccountList = res; } var crAccount = res.filter(x => x.Name.split('-')[1] == 'Traffic Fine Collections')[0]; if (crAccount !== undefined) { this.mast.FK_Cr_Account = new ItemViewModel(crAccount.Id, crAccount.Name); } var surchargeAccount = res.filter(x => x.Name.split('-')[1] == 'Traffic Fines Surcharge')[0]; if (surchargeAccount !== undefined) { this.mast.FK_Surcharge_Account = new ItemViewModel(surchargeAccount.Id, surchargeAccount.Name); } console.log(res); } }, err => { this._notificationService.notify(`danger`, err.msg); }); } public getExpHeads(f) { if (f == 'a') { this.mast.FK_Item = null; } this.itemList = []; this._expenseServices.getExpHeads(this.mast.FK_Item_Category.Id).subscribe((res: any) => { this.expHeadList = res; }, err => { }); } public getEmp(x: string) { debugger; this._storeService.getEmpBySearch(x).subscribe((res: any) => { if (res) { this.empList = res; } }, err => { this._notificationService.notify(`danger`, err.msg); }); } public handleSearchEvents() { this.searchSubcription = this.searchEvent.pipe( debounceTime(400)).subscribe((x: any) => { console.log(x); if (x) { debugger; if (x.filter == `Fk_Exp_Acc`) { this.getAccount(x.event, `B`); } if (x.filter == `FK_Dr_Account`) { this.getAccount(x.event, `EmpExp`); } if (x.filter == `FK_Cr_Account`) { this.getAccount(x.event, `Mis`); } if (x.filter == `FK_Item`) { this.getItem(x.event, 'a') } if (x.filter == `FK_Emp`) { this.getEmp(x.event); } } }); } }