import { Component, OnInit } from '@angular/core'; import { KGridHelper } from '../../../_helpers/k-grid.class'; import { NotificationService } from '../../../_services/notification.service'; import { ExpenseServices } from '../expense-services'; import { AuthenticationService } from '../../../_services/authentication.service'; import { SortDescriptor, orderBy } from '@progress/kendo-data-query'; import { PageChangeEvent } from '@progress/kendo-angular-grid'; import { InstalmentVewModel } from '../../store/store-model'; import { Subject, Subscription } from 'rxjs'; import { StoreService } from '../../store/store.services'; import { debounceTime } from 'rxjs/operators'; import { TraficFinePayment } from '../expense-class'; import { ItemViewModel } from '../../stock/MedStock.class'; import { LocalService } from '../../../_services/local.service'; @Component({ selector: 'app-trafic-fine-invoice', templateUrl: './trafic-fine-invoice.component.html', }) export class TraficFineInvoiceComponent implements OnInit { public kGrid: KGridHelper = new KGridHelper(); public payableList: any[] = []; public drAccountList: any[] = []; public crAccountList: any[] = []; public searchEvent = new Subject(); public searchSubcription: Subscription = null; public obj = new TraficFinePayment(); public saving = false; public nowDate = new Date(); constructor(public _notificationService: NotificationService, public _expenseServices: ExpenseServices, public _storeService: StoreService, public _authenticationService: AuthenticationService, public _localService: LocalService) { } ngOnInit() { this.obj.trns = new InstalmentVewModel(); this.obj.trns.SP_Date = new Date(); this.obj.trns.Amount = 0; this.initializeProps(); this.getExpenseList(); this.handleSearchEvents(); this.getAccount(' ', 'Mis'); this.nowDate = new Date(); this.nowDate.setHours(this.nowDate.getHours() + 5); } private initializeProps() { this.kGrid.loading = false; this.kGrid.pageSize = 20; this.kGrid.pageSizes = [20, 50, 100]; } public getExpenseList() { this.kGrid.loading = true; this._expenseServices.getUnPaidTraficFineList(this.kGrid.skip, this.kGrid.pageSize, this.kGrid.searchQuery, this.kGrid.hfId, this.kGrid.geoLvlCode, this.kGrid.hfType, this.kGrid.stockId).subscribe( (response: any) => { this.kGrid.data = []; this.kGrid.data = response.DtlList; this.kGrid.totalRecords = response.TotalCount; this.kGrid.gridView = { data: this.kGrid.data, total: this.kGrid.totalRecords }; this.kGrid.loading = false; console.log(this.kGrid.gridView); }, err => this.handleError(err) ); } 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.getExpenseList(); } 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.getExpenseList(); } public AddToPayment(e, data, index) { debugger; this.getAccount(' ','Mis'); if (e == true) { var i = data; this.obj.selection.push(i); this.obj.trns.Amount += data.ExpAmount; } else { this.obj.selection.splice(this.payableList.findIndex(x => x.Id == data.Id), 1); this.obj.trns.Amount -= data.ExpAmount; } } public onSubmit() { this.saving = true; this._expenseServices.saveTraficFInePayment(this.obj).subscribe((res: any) => { this._notificationService.notify("success", "Save Successfully!"); this.saving = false; this.obj = new TraficFinePayment(); this.obj.trns = new InstalmentVewModel(); this.obj.trns.SP_Date = new Date(); this.obj.trns.Amount = 0; this.getExpenseList(); setTimeout(() => { window.location.reload(); }, 5000); }, err => { this._notificationService.notify("danger", err.msg); this.saving = false; }); } public attachfile(event) { this.obj.trns.file = event.target.files[0]; } public getAccount(x: string, event: string) { this._storeService.getAccountBySearch(x, event).subscribe((res: any) => { if (res) { if (event == 'Mis') { debugger; this.drAccountList = res; var drAccount = res.filter(x => x.Name.split('-')[1] == 'Traffic Fine Collections')[0]; if (drAccount !== undefined) { this.obj.trns.FK_Account = new ItemViewModel(drAccount.Id, drAccount.Name); } } if (event == 'CaB') { this.crAccountList = 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) { debugger; if (x.filter == "FK_Dr_Account") { this.getAccount(x.event, "Mis"); } if (x.filter == "FK_Cr_Account") { this.getAccount(x.event, "CaB"); } } }); } }