import { Component, OnInit } from '@angular/core'; import { ExpenseMastModel, ExpenseDetailModel } 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 { ActivatedRoute } from '@angular/router'; import { ItemViewModel } from '../../store/store-model'; import { debounceTime } from 'rxjs/operators'; import { LocalService } from '../../../_services/local.service'; @Component({ selector: 'app-gen-exp', templateUrl: './gen-exp.component.html', }) export class GenExpComponent implements OnInit { public mast: ExpenseMastModel; public item = new ExpenseDetailModel(); public saving = false; 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 itemCategoriesList: any[] = []; public PKAmount = 0; public ExpAmount = 0; public VatAmount = 0; public NetAmount = 0; public DiffAmount = 0; constructor( public _storeService: StoreService, public _notificationService: NotificationService, public _expenseServices: ExpenseServices, public activatedRoute: ActivatedRoute, public _localService: LocalService ) { } ngOnInit() { this.getItemCategory(); this.handleSearchEvents(); this.getAccount(` `, `Exp`); this.getAccount(` `, `A`); this.getAccount(` `, `E`); this.activatedRoute.queryParams.subscribe(params => { const accId = params['id']; if (accId != null) this.getExpenseDtl(accId); else { this.mast = new ExpenseMastModel(); this.mast.Items = []; this.mast.FkCategory = new ItemViewModel(); // this.mast.FkCategory.Id = 3; // this.mast.FkCategory.Name = 'Sim'; this.mast.CreationDate = new Date(); this.mast.ExpAmount = 0; this.item.FK_ExpHead = new ItemViewModel(); this.item.FK_Cr_Account = new ItemViewModel(); this.item.FK_Dr_Account = new ItemViewModel(); this.item.FK_Exp_Account = new ItemViewModel(); this.getExpHeads(); } }); } public getExpenseDtl(e) { this._expenseServices.getExpDtl(e).subscribe((res: any) => { if (res) { this.mast = new ExpenseMastModel(); this.mast = res; this.mast.UptoDate = new Date(res.UptoDate); this.mast.CreationDate = new Date(res.CreationDate); this.mast.BillFromDate = new Date(res.BillFromDate); this.mast.Items = res.Items; this.getExpHeads(); } }, err => { this._notificationService.notify(`danger`, err.error); }); } public onSubmit() { debugger; this.saving = true; let emp: any = Object.assign({}, this.mast); if (this.mast.BillFromDate) emp.BillFromDate = this.mast.BillFromDate.toDateString(); if (this.mast.UptoDate) emp.UptoDate = this.mast.UptoDate.toDateString(); this._expenseServices.saveGernalExpenseNew(emp).subscribe((res: any) => { this._notificationService.notify(`success`, `Save Successfully!`); this.saving = false; this.mast = new ExpenseMastModel(); this.mast.FkCategory = new ItemViewModel(); this.mast.FkCategory.Id = 3; this.mast.FkCategory.Name = 'Sim'; this.mast.CreationDate = new Date(); this.mast.ExpAmount = 0; }, err => { this._notificationService.notify(`danger`, err.msg); this.saving = false; }); } public ClearForm() { } public FeatchEmpdetail() { var td = null; var fd = null; if (this.mast.BillFromDate != null) fd = this.mast.BillFromDate.toLocaleDateString(); if (this.mast.UptoDate != null) td = this.mast.UptoDate.toLocaleDateString(); this.feachting = true; this._expenseServices.getItemsForBilling(this.mast.FkCategory.Id, fd, td).subscribe((res: any) => { this.mast.Items = res; this.feachting = false; }, err => { }); } 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.mast.ExpAmount -= c.NetExpAmount; this.mast.Items.splice(i, 1); } 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; debugger; this.mast.ExpAmount += c.NetExpAmount; this.PKAmount += c.PkgAmount; this.ExpAmount += c.ExpAmount; this.VatAmount += c.VatAmount; this.NetAmount += c.NetExpAmount; this.DiffAmount += c.AmountDiff; this.mast.Items.push(c); this.item = new ExpenseDetailModel(); this.item.FK_ExpHead = null; this.item.FK_Cr_Account = null; this.item.FK_Dr_Account = null; this.item.FK_Exp_Account = null; // } 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 == 'Exp') this.expAccountList = res if (event == 'E') this.drAccountList = res; if (event == 'A') this.crAccountList = res; debugger; } }, err => { this._notificationService.notify(`danger`, err.msg); }); } public getExpHeads() { this._expenseServices.getExpHeads(this.mast.FkCategory.Id).subscribe((res: any) => { this.expHeadList = res; }, err => { }); } public attachfile(event) { // this.instalment.file = event.target.files[0]; } public getItem(e, f) { if (this.mast.FkCategory == null) { this._notificationService.notify(`danger`, `Select a Catrogry First`); return; } if (f == 'a') { this.item.FK_Dr_Account = null; // this.mast.FK_Emp = null; } this._expenseServices.getItemsVehcleBilling(e, this.mast.FkCategory.Id).subscribe((res: any) => { this.itemList = res; }, err => { }); } public GetEmployee() { // TODO: Underconcideration debugger; // var day = new Date(); this.mast.CreationDate.setDate(this.mast.CreationDate.getDate() + 1); this._expenseServices.getEmpAndAcc(this.item.FK_Item.Id, this.mast.CreationDate, 'G').subscribe((res: any) => { if (res.err == `N`) { // this.mast.FK_Emp = res.emp; this.item.FK_Dr_Account = res.acc; } else { this._notificationService.notify(`danger`, `Not Link to any Employee`); } }, err => { this._notificationService.notify(`danger`, err); }); } 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, `Exp`); } if (x.filter == `FK_Dr_Account`) { this.getAccount(x.event, `E`); } if (x.filter == `FK_Cr_Account`) { this.getAccount(x.event, `A`); } if (x.filter == `FK_Item`) { this.getItem(x.event, 'a') } } }); } }