import { Component, OnInit } from '@angular/core'; import { StockAssignHistoryViewModel, ItemViewModel } from '../store-model'; import { Subject, Subscription } from 'rxjs'; import { debounceTime } from 'rxjs/operators'; import { StoreService } from '../store.services'; import { NotificationService } from '../../../_services/notification.service'; import { LocalService } from '../../../_services/local.service'; @Component({ selector: 'app-item-assing-emp', templateUrl: './item-assing-emp.component.html', }) export class ItemAssingEmpComponent implements OnInit { public sa_Emp = new StockAssignHistoryViewModel(); public saving = false; public searchEvent = new Subject(); public searchSubcription: Subscription = null; public avaQty: number; public itemList = []; public empList = []; public maxDate = new Date(); public minDate = new Date(); public isRented = false; public assigingPrintList: any[] = []; constructor(public _storeService: StoreService, public _notificationService: NotificationService, private _localService: LocalService) { } ngOnInit() { //this.sa_Emp.FK_Employee = new ItemViewModel(); this.handleSearchEvents(); } public onSubmit() { let sa: any = Object.assign({}, this.sa_Emp); sa.Issue_Date = this.sa_Emp.Issue_Date.toLocaleString(); debugger; this.saving = true; this._storeService.saveStockassigning(sa).subscribe((res: any) => { this._notificationService.notify(`success`, `Save Successfully!`); this.assigingPrintList.push(sa); this.sa_Emp = new StockAssignHistoryViewModel(); this.sa_Emp.FK_Employee = new ItemViewModel(); this.avaQty = null; this.saving = false; }, err => { this._notificationService.notify(`danger`, err.msg); this.saving = false; }); } batchesOfSelectedItems = []; isBatchNoValid = true; validateBatchCode() { let contains = this.batchesOfSelectedItems.find(x => x.spBatch === this.sa_Emp.Item_Batch); this.isBatchNoValid = (contains === undefined) ? true : false; } selectedBatchValue:ItemViewModel; BatchChange(event) { if (event !== undefined && event.SPD_ID !== undefined) { this.selectedBatchValue = event; console.log(this.selectedBatchValue); this._storeService.GetBatchStockStatus(event.SPD_ID).subscribe((res: any) => { this.avaQty = res; if (this.avaQty == 0) { this.batchesOfSelectedItems.splice(this.batchesOfSelectedItems.indexOf(event), 1); } var bItem = event;//JSON.parse(JSON.stringify(this.sa_Emp.Item_Batch)); this.sa_Emp.Item_Batch = 0; this.sa_Emp.Item_Batch = bItem.spBatch; this.sa_Emp.SPD_ID = bItem.SPD_ID; console.log(this.sa_Emp); }, (err) => { }); } } public getItem(x: string) { this._storeService.getItemInStock(x).subscribe((res: any) => { if (res) { this.itemList = res // console.log(this.itemList); } }, err => { this._notificationService.notify(`danger`, err.msg); }); } public dropdownValueChanged = (value, filter) => { if (!value) { return; } if (filter == `itemCode`) { debugger; console.log(this.itemList.find(x => x.Code == value)); this.sa_Emp.FK_Item_Id = this.itemList.find(x => x.Code == value).Id; this.sa_Emp.Item_Name = this.itemList.find(x => x.Code == value).Name; this.sa_Emp.Issue_Qty = 1; if (this.itemList.find(x => x.Code == value).Rented_Qty != null) { this.avaQty = this.itemList.find(x => x.Code == value).Rented_Qty; this.isRented = true; } else { this.avaQty = this.itemList.find(x => x.Code == value).In_Qty; this.isRented = false; } // this.sa_Emp.Item_Code = `${this.itemList.find(x => x.Code == value).Item_Code}`; // if (this.sa_Emp.Item_Code !== undefined ) { this.getStockStats(this.sa_Emp.FK_Item_Id); let exBatches: any = this.itemList.find(x => x.Code === value).ExistingBatches; if(exBatches.length>0){ this.batchesOfSelectedItems = exBatches; this.selectedBatchValue= undefined; }else { this.batchesOfSelectedItems = []; this.selectedBatchValue= undefined; } // .filter(x => (x.spItemsQty !== undefined && x.spItemsQty !== 0) && (x.spBatch !== undefined && x.spBatch !== null)) // this.batchesOfSelectedItems = this.batchesOfSelectedItems.find(x => (x.spItemsQty !== undefined && x.spItemsQty !== 0)); // } // this.sa_Emp = new StockAssignHistoryViewModel(); // this.sa_Emp.FK_Employee = new ItemViewModel(); // this.getStockStats(this.sa_Emp.FK_Item_Id); this.getItemLastDates(); } if (filter == `emp`) { debugger; this.sa_Emp.FK_Employee.Id = this.empList.find(x => x.Name == value).Id; } } public getItemLastDates() { this._storeService.getStockLastDates(this.sa_Emp.FK_Item_Id).subscribe((res: any) => { if (res) { if (res.msg != `N`) this.minDate = new Date(res.date) else this.minDate = null; } }, err => { this._notificationService.notify(`danger`, err.msg); }); } public getStockStats(x: number) { this._storeService.getStockStatus(x).subscribe((res: any) => { if (res) { if (res.err == 'N') { if (res.In_Qty != null) { this.avaQty = res.In_Qty; } } else { this._notificationService.notify(`danger`, res.err); this.sa_Emp = new StockAssignHistoryViewModel(); this.sa_Emp.FK_Employee = new ItemViewModel(); } } }, err => { this._notificationService.notify(`danger`, err.msg); }); } 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) { if (x.filter == `code`) { if (x.event != ``) { this.getItem(x.event); } else { this.avaQty = null; this.isRented = false; this.sa_Emp = new StockAssignHistoryViewModel(); this.sa_Emp.FK_Employee = new ItemViewModel(); } } } if (x.filter == `emp`) { this.getEmp(x.event); } }); } }