import { Component, OnInit } from '@angular/core'; import { KGridHelper } from '../../../_helpers/k-grid.class'; import { Subject, Subscription } from 'rxjs'; import { DepreciationViewModel, ItemViewModel } from '../store-model'; import { StoreService } from '../store.services'; import { NotificationService } from '../../../_services/notification.service'; import { ActivatedRoute, Router } from '@angular/router'; import { LocalService } from '../../../_services/local.service'; import { SortDescriptor, orderBy } from '@progress/kendo-data-query'; import { PageChangeEvent } from '@progress/kendo-angular-grid'; import { debounceTime } from 'rxjs/operators'; @Component({ selector: 'app-item-depreciation', templateUrl: './item-depreciation.component.html' }) export class ItemDepreciationComponent implements OnInit { public kGrid: KGridHelper = new KGridHelper(); public searchEvent = new Subject(); public searchSubcription: Subscription = null; public saving = false; public deleting = false; public maxDate = new Date(); public createOrUpdateDialog = false; public deleteConfirmationDialog = false; public _isEdit = false; public itemsList: any[] = []; public stockAccountList: any[] = []; public expenseAccountList: any[] = []; public vm: DepreciationViewModel = new DepreciationViewModel(); constructor(public _storeService: StoreService, public _notificationService: NotificationService, private activatedRoute: ActivatedRoute, private router: Router, public _localService: LocalService) { } ngOnInit() { this.initializeProps(); this.getAccount(' ', 'E'); this.getAccount(' ', 'Exp'); this.getAccount(' ', 'St'); this.getItem(' '); this.handleSearchEvents(); this.getList(); this.maxDate = new Date(); } // DepriAmount = Current Value * percentage / 100 // Percentage = DeprAmount * 100 / CurrentAmount // New Value = Curentvlaue - Depri Amount depriciationChanged(e) { if (this.vm.DepreciationAmount !== 0) { this.vm.Percentage = (this.vm.DepreciationAmount * 100) / this.vm.CurrentValue; } this.currentValChanged(e); } percentageChanged(e) { if (this.vm.Percentage !== 0) { this.vm.DepreciationAmount = (this.vm.CurrentValue * this.vm.Percentage) / 100; } this.currentValChanged(e); } currentValChanged(e) { this.vm.NewValue = this.vm.CurrentValue - this.vm.DepreciationAmount; } CreateOrUpdateDialog() { this.vm = new DepreciationViewModel(); this.vm.TransactionDate = new Date(); this._isEdit = false; this.createOrUpdateDialog = true; } getList() { this.kGrid.loading = true; this._storeService.ItemDepreciationList().subscribe( (response: any) => { console.log(response); this.kGrid.data = []; this.kGrid.data = response; this.kGrid.totalRecords = response.length; this.kGrid.gridView = { data: this.kGrid.data, total: this.kGrid.totalRecords }; this.kGrid.loading = false; console.log(this.kGrid.gridView); }, err => this.handleError(err) ); this.maxDate.setHours(this.maxDate.getHours() + 1); } public getItem(x: string) { this._storeService.GetDepItemBySearch(x).subscribe((res: any) => { if (res) { this.itemsList = res; console.log(res); } }, err => { this._notificationService.notify('danger', err.msg); }); } OnSubmit() { this.saving = true; this._storeService.SaveItemDepreciation(this.vm).subscribe((res) => { this._notificationService.notify('success', 'Saved Successfully'); this.saving = false; this.close(); this.getList(); }, (err) => { this._notificationService.notify('danger', 'Something Went Wrong'); this.saving = false; }); this.maxDate.setHours(this.maxDate.getHours() + 1); } EditDialog(item) { this.vm = item; this.vm.TransactionDate = new Date(item.TransactionDate); this._isEdit = true; this.createOrUpdateDialog = true; console.log(item); this.depriciationChanged(null); let code = this.vm.FK_ItemId.Name; this.vm.ItemCode = code.split('-')[0]; } DeleteDialog(item) { this.vm = item; this.deleteConfirmationDialog = true; } OnDelete() { this.deleting = true; this._storeService.DeleteItemDepreciation(this.vm.Id).subscribe((res:any) => { this._notificationService.notify('success', 'Deleted Successfully'); this.deleting = false; this.close(); this.getList(); }, (err) => { this._notificationService.notify('danger', 'Something Went Wrong'); this.deleting = false; }); } // public getFIle(event) { // this.vm.file = event.target.files[0]; // } public close() { this.createOrUpdateDialog = false; this.deleteConfirmationDialog = false; } private initializeProps() { this.kGrid.loading = false; this.kGrid.pageSize = 20; this.kGrid.pageSizes = [20, 50, 100]; } public sortChange(sort: SortDescriptor[]): void { if (sort[0].field == 'asd') { return; } this.kGrid.sort = sort; this.sortData(); } private sortData() { this.kGrid.gridView = { data: orderBy(this.kGrid.data, this.kGrid.sort), total: this.kGrid.totalRecords }; } public pageChange(event: PageChangeEvent): void { this.kGrid.skip = event.skip; this.getList(); } private handleError(err: any) { this.kGrid.loading = false; // if (err.status == 403) { // this._authenticationService.logout(); // } } onBinClick(aid: number) { alert(aid); } public changePagesize(value: any) { this.kGrid.pageSize = +value; this.kGrid.skip = 0; this.getList(); } public getAccount(x: string, event: string) { this._storeService.getAccountBySearch(x, event).subscribe((res: any) => { if (res) { if (event === 'St') { this.stockAccountList = res; } if (event === 'Exp') { this.expenseAccountList = res; } } }, err => { this._notificationService.notify('danger', err.msg); }); } public dropdownValueChanged = (value, filter) => { if (!value) { this.vm.FK_ItemId = new ItemViewModel(0, ''); this.vm.CurrentValue = 0; this.vm.FK_StockAccount = null;//new ItemViewModel(0, ''); return; } if (filter === 'item') { var item = this.itemsList.find(x => x.Code === value); if (value !== ' ') { console.log(item); this.vm.FK_ItemId = new ItemViewModel(item.Id, item.Name); this.vm.CurrentValue = item.CostOfGoodSold == null ? 0 : item.CostOfGoodSold; this.vm.FK_StockAccount = new ItemViewModel(item.FK_PayableAccount_Id, item.FK_PayableAccount_IdName); this.currentValChanged(null); } else { this.vm.FK_ItemId = new ItemViewModel(0, ''); this.vm.CurrentValue = 0; this.vm.FK_StockAccount = new ItemViewModel(0, ''); } } } public handleSearchEvents() { this.searchSubcription = this.searchEvent.pipe( debounceTime(400)).subscribe((x: any) => { console.log(x); if (x) { if (x.event !== '') { if (x.filter === 'item') { this.getItem(x.event); } if (x.filter === 'stock') { this.getAccount(x.event, 'St'); } if (x.filter === 'expense') { this.getAccount(x.event, 'Exp'); } } } }); } }