import { Component, OnInit, ViewChild } from '@angular/core'; import { Sale_PurchaseMastViewModel, ItemViewModel, Sale_PurchaseDetailViewModel } 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 { Router, ActivatedRoute } from '@angular/router'; import { LocalService } from '../../../_services/local.service'; import { FormCanDeactivate } from '../../../_guards/form-can-deactivate'; import { NgForm } from '@angular/forms'; @Component({ selector: 'app-sales-form', templateUrl: './sales-form.component.html' }) export class SalesFormComponent extends FormCanDeactivate implements OnInit { @ViewChild('f') form: NgForm; public saving = false; public P_Mast = new Sale_PurchaseMastViewModel(); public sp_item = new Sale_PurchaseDetailViewModel(); public searchEvent = new Subject(); public searchSubcription: Subscription = null; public accountList = []; public cusAccountList = []; public cashAndBankAccountList = []; public itemList = []; public avaQty: number; public oldrate = 0; public showAdd = false; public nowDate = new Date(); public isSpItemEdit = false; public isEdit = false; constructor(public _storeService: StoreService, public _notificationService: NotificationService, private router: Router, private activatedRoute: ActivatedRoute, private _localService: LocalService) { super(); } ngOnInit() { this.activatedRoute.queryParams.subscribe(params => { const accId = params['id']; if (accId != null) { this.isEdit = true; this.getAccount(' ', 'Incom'); this.getAccount(' ', 'CCS'); this.getAccount(' ', 'E'); this.getAccount(' ', 'CaB'); this.getOdrrDtl(accId); if (this.P_Mast.PaymentType === undefined || this.P_Mast.PaymentType === ' ') { this.P_Mast.PaymentType = 'Credit'; } } else { this.isEdit = false; this.InitialzedProp(); } }); this.handleSearchEvents(); } public getOdrrDtl(x) { this._storeService.getOderDtl(x).subscribe((res: any) => { debugger; this.P_Mast = res.item; if (this.P_Mast.PaidAmount > 0) { this._notificationService.notify('danger', `Sorry This Order Can't be Edit Any More.`); this.router.navigate(['/store/p-list']); } this.P_Mast.SP_Date = new Date(res.item.SP_Date); }, err => { this._notificationService.notify('danger', err); }); } public ClearForm() { this.InitialzedProp(); } public onSubmit() { debugger; this.saving = true; this._storeService.savePurchaseInvoice(this.P_Mast).subscribe((res: any) => { this._notificationService.notify('success', 'Invoice Save Successfully'); this.InitialzedProp(); this.saving = false; }, err => { this._notificationService.notify('danger', err); // this.InitialzedProp(); this.saving = false; }); } batchesOfSelectedItems = []; isBatchNoValid = true; validateBatchCode() { let contains = this.batchesOfSelectedItems.find(x => x.spBatch === this.sp_item.Batch); this.isBatchNoValid = (contains === undefined) ? true : false; } BatchChange(event) { console.log(event); this._storeService.GetBatchStockStatus(event.SPD_ID).subscribe((res: any) => { this.avaQty = res; if (this.avaQty == 0) { this.batchesOfSelectedItems.splice(this.batchesOfSelectedItems.indexOf(event), 1); } }, (err) => { }); } public InitialzedProp() { this.P_Mast = new Sale_PurchaseMastViewModel(); // this.P_Mast.FK_Dabit_Acc = new ItemViewModel(); // this.P_Mast.FK_Sup_Cradit_Acc = new ItemViewModel(); this.P_Mast.SP_Items = []; this.P_Mast.SP_Date = new Date(); this.P_Mast.PaymentType = `Credit`; this.P_Mast.FK_Inv_Type = new ItemViewModel(); this.P_Mast.FK_Inv_Type.Id = 1; this.sp_item.VAT_Type = 'VAT ON'; this.sp_item.VAT_Value = 5; } public getAccount(x: string, event: string) { this._storeService.getAccountBySearch(x, event).subscribe((res: any) => { if (res) { if (event === 'Incom') { this.accountList = res; } if (event === 'CCS') { this.cusAccountList = res; } if (event === 'CaB') { this.cashAndBankAccountList = res; } if (event === 'E') { this.cusAccountList = []; this.cusAccountList = res; } } }, err => { this._notificationService.notify('danger', err.msg); }); } public getItem(x: string) { this._storeService.getItemBySearch(x).subscribe((res: any) => { if (res) { this.itemList = res; } }, err => { this._notificationService.notify('danger', err.msg); }); } public attachfile(event) { this.P_Mast.file = event.target.files[0]; } public dropdownValueChanged = (value, filter) => { if (!value) { return; } if (filter === 'coustomer') { this.P_Mast.FK_Dabit_Acc.Id = this.accountList.find(x => x.Name === value).Id; } if (filter === 'employee') { this.P_Mast.FK_Dabit_Acc.Id = this.accountList.find(x => x.Name === value).Id; } if (filter === 'FK_Sup_Cradit_Acc') { this.P_Mast.FK_Sup_Cradit_Acc.Id = this.accountList.find(x => x.Name === value).Id; } if (filter === 'itemCode') { debugger; this.sp_item.Item_Code = this.itemList.find(x => x.Code === value).Code; this.sp_item.FK_Item_Id = this.itemList.find(x => x.Code === value).Id; this.sp_item.Item_Name = this.itemList.find(x => x.Code === value).Name; this.sp_item.UOM = this.itemList.find(x => x.Code === value).Measure; this.sp_item.Rate = this.itemList.find(x => x.Code === value).Purchase_Price; this.sp_item.Item_Type = this.itemList.find(x => x.Code === value).Item_Type; if (this.sp_item.Item_Type === 'Product') { this.getStockStats(this.sp_item.FK_Item_Id); let exBatches: any = this.itemList.find(x => x.Code === value).ExistingBatches; this.batchesOfSelectedItems = exBatches; } } } public getPStock() { if (this.sp_item.Item_Type === 'Product') { this.getStockStats(this.sp_item.FK_Item_Id); if (this.sp_item.Item_Code && this.sp_item.Item_Type && this.avaQty >= this.sp_item.Qty && this.sp_item.Qty && this.sp_item.Item_Name) { this.showAdd = true; } } else { if (this.sp_item.Item_Name !== null) { this.showAdd = true; } } } public getStockStats(x: number) { this._storeService.getStockStatus(x).subscribe((res: any) => { if (res) { if (res.err === 'N') { this.avaQty = res.In_Qty; } else { this._notificationService.notify('danger', 'Not Avalibel in Stock'); this.sp_item = new Sale_PurchaseDetailViewModel(); this.avaQty = null; this.sp_item.VAT_Type = 'VAT ON'; this.showAdd = false; } } }, err => { this._notificationService.notify('danger', err.msg); }); } EditItem(item) { this.sp_item = item; this.P_Mast.Inv_Amount -= this.sp_item.Total; this.isSpItemEdit = true; } public AddToCart() { if (this.sp_item.Item_Type == 'Product') { var bItem = JSON.parse(JSON.stringify(this.sp_item.Batch)); this.sp_item.Batch = 0; this.sp_item.Batch = bItem.spBatch; this.sp_item.SPD_ID = bItem.SPD_ID; } debugger; this.P_Mast.SP_Items.push(this.sp_item); this.P_Mast.Inv_Amount += this.sp_item.Total; this.sp_item = new Sale_PurchaseDetailViewModel(); this.avaQty = null; this.sp_item.VAT_Type = 'VAT ON'; this.sp_item.VAT_Value = 5; this.showAdd = false; } public calculateVat(x) { if (this.sp_item.Rate === null || this.sp_item.Qty === null) return; if (this.sp_item.VAT_Type === 'VAT ON') { if (this.oldrate !== 0) { this.sp_item.Rate = this.oldrate; this.oldrate = 0; } let amount = this.sp_item.Rate * this.sp_item.Qty; this.sp_item.VatAmount = (amount * x) / 100; this.sp_item.Total = amount + this.sp_item.VatAmount; } if (this.sp_item.VAT_Type === 'VAT FROM') { this.oldrate = this.sp_item.Rate; let amount = this.sp_item.Rate * this.sp_item.Qty; this.sp_item.VatAmount = (amount * x) / (100 + x); this.sp_item.Rate -= (this.sp_item.VatAmount / this.sp_item.Qty); this.sp_item.Total = (this.sp_item.Rate * this.sp_item.Qty) + this.sp_item.VatAmount; } if (this.isSpItemEdit == false) { this.AddToCart(); } if (this.isSpItemEdit == true) { this.UpdateCartItem(); } } UpdateCartItem() { this.nowDate = new Date(); this.P_Mast.SP_Items.splice(this.P_Mast.SP_Items.indexOf(this.sp_item), 1, this.sp_item); this.P_Mast.Inv_Amount += this.sp_item.Total; this.sp_item = new Sale_PurchaseDetailViewModel(); this.sp_item.VAT_Type = 'VAT ON'; this.sp_item.VAT_Value = 5; this.isSpItemEdit = false; } public RemoveFromCart(e) { let item = this.P_Mast.SP_Items[e]; this.P_Mast.Inv_Amount -= item.Total; this.P_Mast.SP_Items.splice(e, 1); } public handleSearchEvents() { this.searchSubcription = this.searchEvent.pipe( debounceTime(400)).subscribe((x: any) => { console.log(x); if (x) { debugger; if (x.filter === 'income') { this.getAccount(x.event, 'Incom'); } if (x.filter === 'coustomer') { this.getAccount(x.event, 'CCS'); } if (x.filter === 'cashAndbank') { this.getAccount(x.event, 'CaB'); } if (x.filter === 'employee') { this.getAccount(x.event, 'E'); } if (x.filter === 'itemCode') { if (x.event !== '') { this.getItem(x.event); } else { this.sp_item = new Sale_PurchaseDetailViewModel(); this.avaQty = null; this.sp_item.VAT_Type = 'VAT ON'; this.sp_item.VAT_Value = 5; } } if (x.filter === 'vat') { //this.calculateVat(x.event); } if (x.filter === 'qty') { //this.calculateVat(this.sp_item.VAT_Value); if (this.sp_item.Item_Code && this.sp_item.Item_Type && this.avaQty >= this.sp_item.Qty && this.sp_item.Qty) { this.showAdd = true; } else { if (this.sp_item.Item_Type === 'Service') { this.showAdd = true; } else { this.showAdd = false; } } } } }); } }