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 { ActivatedRoute, Router } from '@angular/router'; import { LocalService } from '../../../_services/local.service'; import { NgForm } from '@angular/forms'; import { FormCanDeactivate } from '../../../_guards/form-can-deactivate'; @Component({ selector: 'app-purchase-form', templateUrl: './purchase-form.component.html', }) export class PurchaseFormComponent 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 supaccountList = []; public itemList = []; public oldrate = 0; public nowDate = new Date(); public isEdit = false; public isSpItemEdit = false; constructor(public _storeService: StoreService, public _notificationService: NotificationService, private activatedRoute: ActivatedRoute, private router: Router, private _localService: LocalService) { super(); } ngOnInit() { this.activatedRoute.queryParams.subscribe(params => { const accId = params['id']; if (accId != null) { this.getOdrrDtl(accId); } else { this.InitialzedProp(); } }); this.nowDate.setHours(this.nowDate.getHours() + 5); this.handleSearchEvents(); } batchesOfSelectedItems = []; isBatchNoValid = true; validateBatchCode() { let contains = this.batchesOfSelectedItems.find(x => x.spBatch === this.sp_item.Batch); this.isBatchNoValid = (contains === undefined) ? true : false; } public onSubmit() { this.saving = true; this.nowDate = new Date(); let emp: any = Object.assign({}, this.P_Mast); if (this.P_Mast.SP_Date) { emp.SP_Date = this.P_Mast.SP_Date.toDateString(); } this._storeService.savePurchaseInvoice(emp).subscribe((res: any) => { this._notificationService.notify('success', 'Invoice Save Successfully'); this.InitialzedProp(); this.saving = false; }, err => { this._notificationService.notify('danger', err.error); //this.InitialzedProp(); this.saving = false; }); } public InitialzedProp() { this.P_Mast = new Sale_PurchaseMastViewModel(); this.P_Mast.SP_Items = []; this.P_Mast.SP_Date = new Date(); this.P_Mast.FK_Inv_Type = new ItemViewModel(); this.P_Mast.FK_Inv_Type.Id = 2; this.sp_item.VAT_Type = 'VAT ON'; this.sp_item.VAT_Value = 5; this.P_Mast.SP_Type = 'Exp'; this.P_Mast.PaymentType = 'Net Paid'; this.nowDate = new Date(); this.nowDate.setHours(this.nowDate.getHours() + 3); } public ClearForm() { this.InitialzedProp(); } public attachfile(event) { this.P_Mast.file = event.target.files[0]; } public ClearEditForm() { 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.nowDate = new Date(); this.nowDate.setHours(this.nowDate.getHours() + 3); this.isSpItemEdit = false; } public getAccount(x: string, event: string) { debugger; this.nowDate = new Date(); this._storeService.getAccountBySearch(x, event).subscribe((res: any) => { if (res) { if (event == 'Exp' || event == 'St') { this.accountList = res; } else { this.supaccountList = res; } } }, err => { this._notificationService.notify('danger', err.msg); }); } PaymentTypeChanged() { this.P_Mast.FK_Sup_Cradit_Acc = null; this.supaccountList = []; } ExpTypeChanged() { this.P_Mast.FK_Dabit_Acc = null; this.accountList = []; } public getOdrrDtl(x) { this.nowDate = new Date(); this._storeService.getOderDtl(x).subscribe((res: any) => { console.log(res); this.P_Mast = res.item; this.P_Mast.PaymentType = res.item.PaymentType; this.isEdit = true; if (this.P_Mast.PaymentType == 'Credit') { this.getAccount(' ', 'SCS'); } if (this.P_Mast.PaymentType == 'Net Paid') { this.getAccount(' ', 'CaB'); } if (this.P_Mast.SP_Type == 'Exp') { this.getAccount('', 'Exp'); } if (this.P_Mast.SP_Type == 'St') { this.getAccount('', 'St'); } 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); this.P_Mast.FK_Dabit_Acc = new ItemViewModel(res.item.FK_Dabit_Acc.Id, res.item.FK_Dabit_Acc.Name); this.P_Mast.FK_Sup_Cradit_Acc = new ItemViewModel(res.item.FK_Sup_Cradit_Acc.Id, res.item.FK_Sup_Cradit_Acc.Name); //P_Mast.FK_Sup_Cradit_Acc //P_Mast.FK_Dabit_Acc }, err => { this._notificationService.notify('danger', err); }); } public getItem(x: string) { this.nowDate = new Date(); this._storeService.getItemBySearch(x).subscribe((res: any) => { if (res) { console.log(res); console.log(this.P_Mast.SP_Type); if (this.P_Mast.SP_Type === 'Exp') { this.itemList = res.filter(ri => ri.Item_Type === 'Service'); this.sp_item.Item_Type = 'Service'; } if (this.P_Mast.SP_Type === 'St') { this.itemList = res.filter(ri => ri.Item_Type === 'Product'); this.sp_item.Item_Type = 'Product'; } console.log(this.itemList) } }, err => { this._notificationService.notify('danger', err.msg); }); } public dropdownValueChanged = (value, filter) => { this.nowDate = new Date(); if (!value) { return; } if (filter == 'dabitAcc') { 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.Qty = this.itemList.find(x => x.Code == value).Qty; 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.sp_item.Batch = this.itemList.find(x => x.Code == value).Batch; this.batchesOfSelectedItems = this.itemList.find(x => x.Code == value).ExistingBatches; } } } public AddToCart() { this.nowDate = new Date(); 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.sp_item.VAT_Type = 'VAT ON'; this.sp_item.VAT_Value = 5; } 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; } EditItem(item) { console.log(item); //P_Mast.FK_Sup_Cradit_Acc //P_Mast.FK_Dabit_Acc this.sp_item = item; this.P_Mast.Inv_Amount -= this.sp_item.Total; this.isSpItemEdit = true; } public calculateVat(x) { this.nowDate = new Date(); debugger; 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; } var 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; var 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(); } } public RemoveFromCart(e) { this.nowDate = new Date(); var 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.nowDate = new Date(); this.searchSubcription = this.searchEvent.pipe( debounceTime(400)).subscribe((x: any) => { console.log(x); if (x) { debugger; if (x.filter == 'FK_Dabit_Acc') { this.getAccount(x.event, this.P_Mast.SP_Type); } if (x.filter == 'FK_Cradit') { this.getAccount(x.event, 'SCS'); } if (x.filter == 'FK_Payment') { this.getAccount(x.event, 'CaB') } if (x.filter == 'itemCode') { if (x.event != '') { this.getItem(x.event); } else { this.sp_item = new Sale_PurchaseDetailViewModel(); 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); // } } }); } }