import { Component, OnInit, ViewChild } from '@angular/core'; import { Subject, Subscription } from 'rxjs'; import { debounceTime } from 'rxjs/operators'; import { ActivatedRoute } from '@angular/router'; import { GenralExpenseModel } from '../../../expense-class'; import { StoreService } from '../../../../store/store.services'; import { NotificationService } from '../../../../../_services/notification.service'; import { ExpenseServices } from '../../../expense-services'; import { ItemViewModel } from '../../../../stock/MedStock.class'; import { LocalService } from '../../../../../_services/local.service'; import { NgForm } from '@angular/forms'; import { FormCanDeactivate } from '../../../../../_guards/form-can-deactivate'; @Component({ selector: 'app-gernal-exp', templateUrl: './gernal-exp.component.html', }) export class GernalExpComponent 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 ExpensList: any[] = []; public PKAmount = 0; public ExpAmount = 0; public TotlaExp = 0; public VatAmount = 0; public NetAmount = 0; public DiffAmount = 0; public itemCategoriesList: any[] = []; public AccCodeValid: any; public _isEdit = false; constructor( public _storeService: StoreService, public _notificationService: NotificationService, public _expenseServices: ExpenseServices, private activatedRoute: ActivatedRoute, private _localService: LocalService ) { super(); } ngOnInit() { this.getItemCategory(); this.handleSearchEvents(); this.getAccount(` `, `Exp`); this.getAccount(` `, `A`); this.getAccount(` `, `E`); this.getAccount(` `, `CaB`); this.getEmp(` `); this.activatedRoute.queryParams.subscribe(params => { const accId = params['id']; if (accId != null) { this._isEdit = true; this.getExpenseDtl(accId); } else { this._isEdit = 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= 'Trafic Fine '; this.mast.FK_Emp = new ItemViewModel(); this.mast.ExpAmount = 0; this.mast.PaymentType = `Net Paid`; this.validateAccCode(`-1`); } }); } public getExpenseDtl(e) { this._expenseServices.getGerExpDtl(e).subscribe((res: any) => { if (res) { this.AccCodeValid = true; this.mast = new GenralExpenseModel(); this.mast = res; this.mast.ExpDate = new Date(res.ExpDate); this.mast.FromDate = new Date(res.FromDate); this.mast.ToDate = new Date(res.ToDate); this.mast.ExpAmount = (res.NetExpAmount - res.VatAmount); this.mast.PaymentType = res.PaymentType; this.mast.VatAmount = res.VatAmount; this.mast.FK_Emp = res.FK_Emp;//new ItemViewModel(res.FK_Emp.Id, res.FK_Emp.Name); this.mast.FK_Item_Category = new ItemViewModel(res.FK_Item_Category.Id, res.FK_Item_Category.Name); this.mast.FK_ExpHead = res.FK_ExpHead;//new ItemViewModel(res.FK_ExpHead.Id, res.FK_ExpHead.Name); this.mast.Id = res.Id; this.mast.Code = res.Code; this.mast.FK_Item = res.FK_Item; this.mast.FK_Cr_Account = res.FK_Cr_Account; console.log(res); console.log(this.mast); this.getExpHeads('e'); this.getItem(` `, 'e'); } }, err => { this._notificationService.notify(`danger`, err.error); }); } public validateAccCode(x) { 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(o) { let emp: any = Object.assign({}, o); if (o.ExpDate) emp.ExpDate = o.ExpDate.toDateString(); if (o.ToDate) emp.ToDate = o.ToDate.toDateString(); if (o.FromDate) emp.FromDate = o.FromDate.toDateString(); emp.IsGeneralExpense = false; this._expenseServices.saveGerExpenseWithOutTF(emp).subscribe((res: any) => { if (res) { this._notificationService.notify(`success`, `Expense Saved Successfully!`); this.ClearForm(); } }, err => { this._notificationService.notify(`danger`, err.msg); }); } public ClearForm() { this.mast = new GenralExpenseModel(); this.mast.CreationDate = new Date(); this.mast.FK_Emp = new ItemViewModel(); this.mast.ExpAmount = 0; this.mast.IsGeneralExpense = false; this.ExpensList = []; this.TotlaExp = 0; this.PKAmount = 0; this.VatAmount = 0; this.NetAmount = 0; this.DiffAmount = 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() { debugger; // this.mast.ExpDate = new Date(this.mast.ExpDate.getDate() + 1); this._expenseServices.getEmpAndAcc(this.mast.FK_Item.Id, this.mast.ExpDate, 'GT').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) { debugger; if (c.ExpAmount < c.PkgAmount) { this._notificationService.notify(`danger`, `Expense Amount should be > or = to Pkg. Amount`); return; } // if(c.IsLock){ // this.PKAmount -= c.PkgAmount; // this.ExpAmount -= c.ExpAmount; // this.VatAmount -=c.VatAmount; // this.NetAmount -=c.NetExpAmount; // this.DiffAmount-=c.AmountDiff ; // 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; if (c.PkgAmount == 0) { this.mast.FK_Surcharge_Account = null; } debugger; this.PKAmount += c.PkgAmount; this.ExpAmount += c.ExpAmount; this.VatAmount += c.VatAmount; this.NetAmount += c.NetExpAmount; this.DiffAmount += c.AmountDiff; this.TotlaExp += c.NetExpAmount; this.ExpensList.push(c); this.mast = new GenralExpenseModel(); this.mast.FK_ExpHead = null; this.mast.FK_Cr_Account = null; this.mast.FK_Dr_Account = null; this.mast.FK_Item_Category = c.FK_Item_Category; this.mast.ExpDate = c.ExpDate; this.mast.ToDate = c.ToDate; this.mast.FromDate = c.FromDate; this.mast.PaymentType = c.PaymentType; this.mast.Remarks = c.Remarks; // } c.IsLock = !c.IsLock; this.validateAccCode(`-1`); } 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) { let e = event === 'CE' ? 'E' : event; this._storeService.getAccountBySearch(x, e).subscribe((res: any) => { if (res) { debugger; if (event == 'Exp') { this.expAccountList = res } if (event == 'E') { this.drAccountList = res; } if (event == 'S' || event == `CaB` || event == 'CE') { this.crAccountList = res; } } }, err => { this._notificationService.notify(`danger`, err.msg); }); } public getExpHeads(f) { if (f == 'a') { this.mast.FK_Item = null; } let gex = this.mast; // gex.FK_Item_Category.Id = this.mast.FK_Item_Category.Id; // // gex.Id = this.mast.Id; // gex.Code = this.mast.Code; // gex.ExpDate = this.mast.ExpDate; // gex.PaymentType = this.mast.PaymentType; // let selectedCatId = this.mast.FK_Item_Category.Id; this.itemList = []; this._expenseServices.getExpHeads(gex.FK_Item_Category.Id).subscribe((res: any) => { this.expHeadList = res; console.log(res); }, err => { }); // this.mast = new GenralExpenseModel(); this.mast.Id = gex.Id; this.mast.Code = gex.Code; this.mast.ExpDate = gex.ExpDate; this.mast.PaymentType = gex.PaymentType; this.mast.FK_Item_Category = gex.FK_Item_Category; // this.mast = gex; } public getEmp(x: string) { this._storeService.getEmpBySearch(x).subscribe((res: any) => { if (res) { this.empList = res; } }, err => { this._notificationService.notify(`danger`, err.msg); }); } public saveBayBookList() { this.saving = true; this.saveExpenseWithTfList(); // this.ExpensList.forEach(element => { // this.onSubmit(element); // }); // this.ExpensList = []; } private saveExpenseWithTfList() { this.saving = true; this._expenseServices.saveGerExpenseWithTFList(this.ExpensList).subscribe((res) => { this._notificationService.notify(`success`, `Save Successfully`); this.saving = false; this.ClearForm(); }, (err) => { this._notificationService.notify(`danger`, err.msg); }); } public ClearList() { this.ExpensList = []; } public RemoveItem(i, c) { this.PKAmount -= c.PkgAmount; this.ExpAmount -= c.ExpAmount; this.VatAmount -= c.VatAmount; this.NetAmount -= c.NetExpAmount; this.DiffAmount -= c.AmountDiff; this.ExpensList.splice(i, 1); } 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, `Exp`); } if (x.filter == `FK_Dr_Account`) { this.getAccount(x.event, `E`); } if (x.filter == `FK_Cr_Account`) { if (this.mast.PaymentType == `Net Paid`) { this.getAccount(x.event, `CaB`); } if (this.mast.PaymentType == `Credit`) { this.getAccount(x.event, `S`); } if (this.mast.PaymentType == `Employee_Accs`) { this.getAccount(x.event, `CE`); } } if (x.filter == `FK_Item`) { this.getItem(x.event, 'a'); } if (x.filter == `FK_Emp`) { this.getEmp(x.event); } } }); } }