import { Component, OnInit, ViewChild } from '@angular/core'; import { ExpenseMastModel } from '../expense-class'; import { Subject, Subscription } from 'rxjs'; import { debounceTime } from 'rxjs/operators'; 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 '../../stock/MedStock.class'; import { LocalService } from '../../../_services/local.service'; import { FormCanDeactivate } from '../../../_guards/form-can-deactivate'; import { NgForm } from '@angular/forms'; @Component({ selector: 'app-sim-expense', templateUrl: './sim-expense.component.html', }) export class SimExpenseComponent extends FormCanDeactivate implements OnInit { @ViewChild('f') form: NgForm; public mast: ExpenseMastModel; 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 itemCategoriesList: any[] = []; public PKAmount = 0; public ExpAmount = 0; public VatAmount = 0; public NetAmount = 0; public DiffAmount = 0; private _isEdit = false; public _MaxDateToday = new Date(); 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.activatedRoute.queryParams.subscribe(params => { const accId = params['id']; if (accId != null) { this._isEdit = true; this.getExpenseDtl(accId); } else { 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; this._isEdit = false; this.getExpHeads(); } }); } getItemTextColor(item) { var items = this.mast.Items.filter(x => x.FK_Item.Name == item.FK_Item.Name); if (items.length > 1) { return 'red'; } } public RemoveFromList(item) { if (item.IsLock) { this.PKAmount -= item.PkgAmount; this.ExpAmount -= item.ExpAmount; this.VatAmount -= item.VatAmount; this.NetAmount -= item.NetExpAmount; this.DiffAmount -= item.AmountDiff; this.mast.ExpAmount -= item.NetExpAmount; } this.mast.Items.splice(this.mast.Items.indexOf(item), 1); } public getExpenseDtl(e) { this._expenseServices.getExpDtl(e).subscribe((res: any) => { if (res) { console.log(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 = this.parseItems(res.Items); this.getExpHeads(); } }, err => { this._notificationService.notify(`danger`, err.error); }); } public parseItems(items) { if (items != undefined && items.length > 0) { this.mast.ExpAmount = 0; items.forEach(c => { c.NetExpAmount = c.ExpAmount + c.VatAmount; c.AmountDiff = c.ExpAmount - c.PkgAmount; this.mast.ExpAmount += c.ExpAmount;//c.NetExpAmount; this.PKAmount += c.PkgAmount; this.ExpAmount += c.ExpAmount; this.VatAmount += c.VatAmount; this.NetAmount += c.NetExpAmount; this.DiffAmount += c.AmountDiff; }); } return items; } public onSubmit() { debugger; if (this.mast.Items.find(x => x.IsLock == false) != null) { this._notificationService.notify(`danger`, `Lock the all record first`); return; } 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.saveExpense(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() { this.mast.ExpAmount = 0; this.PKAmount = 0; this.ExpAmount = 0; this.VatAmount = 0; this.NetAmount = 0; this.DiffAmount = 0; 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) => { console.log(res); console.log(this.mast.Items); // if (this.mast.Items !== undefined && this.mast.Items.length > 0) { // res.forEach(rec => { // let hasValue = this.mast.Items.includes(rec); // console.log(hasValue); // if (hasValue == false) { // this.mast.Items.push(rec); // } else { // console.log('already has value') // } // }); // } else { if(this._isEdit==false){ this.mast.Items = res; } else { this.mast.Items = this.parseItems(res); } // } this.feachting = false; }, err => { }); } public UnlockTheRecord(c) { debugger; if (c.FK_Cr_Account == undefined && c.FK_Dr_Account == undefined) { this._notificationService.notify(`danger`, `Please Choose a Credit Account`); c.Lock == false; } if ((c.ExpAmount > c.PkgAmount) && c.FK_Dr_Account == undefined) { this._notificationService.notify(`danger`, `Please Choose Debit Account`); return; } if (c.ExpAmount < c.PkgAmount) { this._notificationService.notify(`danger`, `Expense Amount should be > or = to Pkg. Amount`); return; } if (c.FK_Cr_Account == undefined) { this._notificationService.notify(`danger`, `Please Choose a Credit Account`); return; } if ((c.VatAmount > 0) && c.FK_Cr_Account == undefined) { this._notificationService.notify(`danger`, `Please Choose a Credit Account`); return; } if (c.IsLock) { 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; } else { c.NetExpAmount = c.ExpAmount + c.VatAmount; c.AmountDiff = c.ExpAmount - c.PkgAmount; 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; } 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 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`); } } }); } }