import { Component, OnInit } from '@angular/core'; import { KGridHelper } from '../../../_helpers/k-grid.class'; import { NotificationService } from '../../../_services/notification.service'; import { ReportingService } from '../reporting.service'; import { AuthenticationService } from '../../../_services/authentication.service'; import { State, SortDescriptor, orderBy, process } from '@progress/kendo-data-query'; import { DataStateChangeEvent, PageChangeEvent } from '@progress/kendo-angular-grid'; import { Subject, Subscription } from 'rxjs'; import { debounceTime } from 'rxjs/operators'; import { StoreService } from '../../store/store.services'; import { LocalService } from '../../../_services/local.service'; @Component({ selector: 'app-ladger-report', templateUrl: './ladger-report.component.html', }) export class LadgerReportComponent implements OnInit { public kGrid: KGridHelper = new KGridHelper(); public accountList: any[] = []; public openDilog = false; public geting = false; public AccountTItel = ''; public PH = ''; public H1 = ''; public H2 = ''; public Natuer = ''; public FromDate = null; public ToDate = null; public TDr = 0; public TCr = 0; public OB = 0; public CB = 0; public searchEvent = new Subject(); public searchSubcription: Subscription = null; public selectedAccount: any; constructor(public _notificationService: NotificationService, public _reportingServices: ReportingService, public _storeService: StoreService, public _authenticationService: AuthenticationService, private _localService: LocalService) { } public state: State = { skip: 0, take: 5, // Initial filter descriptor filter: { logic: 'and', filters: [] } }; ngOnInit() { this.initializeProps(); this.kGrid.toDate = new Date(); let today = new Date(); let fDate = new Date(); this.kGrid.fromDate = new Date(fDate.setDate(today.getDate() - 15)); this.handleSearchEvents(); } private initializeProps() { this.kGrid.loading = false; this.kGrid.pageSize = 20; this.kGrid.pageSizes = [20, 50, 100]; } public getEmployeelist() { debugger; this.kGrid.data = []; this.TDr = 0; this.TCr = 0; this.OB = 0; this.CB = 0; this.kGrid.loading = true; var td = null; var fd = null; if (this.kGrid.toDate != null) { td = this.kGrid.toDate.toDateString(); } if (this.kGrid.fromDate != null) { fd = this.kGrid.fromDate.toDateString(); } this._reportingServices.getLadgerReport(this.kGrid.skip, this.kGrid.pageSize, this.kGrid.searchQuery, this.kGrid.hfId, fd, td, this.selectedAccount.Id).subscribe( (response: any) => { debugger; console.log(response); if (response.DtlList.length == 0) { this.kGrid.data = []; this._notificationService.notify('danger', 'No Record Found'); this.kGrid.loading = false; return; } this.kGrid.data = []; this.kGrid.data = this.parseData(response.DtlList); console.log(this.kGrid.data); this.kGrid.totalRecords = response.TotalCount; this.kGrid.gridView = process(this.kGrid.data, this.state); this.kGrid.gridView = { data: this.kGrid.data, total: this.kGrid.totalRecords }; this.kGrid.loading = false; console.log(this.kGrid.gridView); this.CB = Math.abs(this.TCr - this.TDr); }, err => this.handleError(err) ); } parseData(list) { console.log(list) if (list.length > 0) { this.AccountTItel = list[0].Account; this.OB = Math.abs(this.OB); this.OB = list[0].OpningBalance; this.PH = list[0].PH; this.H1 = list[0].H1; this.H2 = list[0].H2; this.Natuer = list[0].Natuer; this.FromDate = new Date(list[0].CreationDate); this.ToDate = new Date(list[0].CreationDate); } for (let i = 0; i < list.length; i++) { if (list[i].Type == 'D') { this.TDr += list[i].Amount; } if (list[i].Type == 'C') { this.TCr += list[i].Amount; } debugger; if (i == 0) { if (list[i].Type == 'D') { list[i].ClosingBalance = 0 - list[i].Amount; } else { list[i].ClosingBalance = list[i].Amount; } } else { if (list[i].Type == 'D') { if (list[i - 1].Status == 'Dr') { list[i].ClosingBalance = -list[i - 1].ClosingBalance - list[i].Amount; } else { list[i].ClosingBalance = list[i - 1].ClosingBalance - list[i].Amount; } } else { if (list[i - 1].Status == 'Dr') { list[i].ClosingBalance = -list[i - 1].ClosingBalance + list[i].Amount; } else { list[i].ClosingBalance = list[i - 1].ClosingBalance + list[i].Amount; } } } if (list[i].ClosingBalance) { if (list[i].ClosingBalance < 0) { list[i].ClosingBalance = Math.abs(list[i].ClosingBalance); list[i].Status = 'Dr'; } else { list[i].Status = 'Cr'; } } if (list[i].OpningBalance) { if (list[i].OpningBalance < 0) { list[i].OpningBalance = Math.abs(list[i].OpningBalance); } } if (list[i].CreationDate != null) { list[i].CreationDate = new Date(list[i].CreationDate); } } return list; } public ClearFilter() { this.kGrid.fromDate = null; this.kGrid.toDate = null; } public dataStateChange(state: DataStateChangeEvent): void { debugger; this.state = state; this.kGrid.gridView = process(this.kGrid.data, this.state); } 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.getEmployeelist(); } 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.getEmployeelist(); } public columns: string[] = [ 'Title', 'Name', 'StartDate', 'EndDate', 'NoOfsolutions', 'ShiftHour', 'ContrectReate' ]; public columnsLabel: string[] = [ 'Title', 'Coustomer', 'Start Date', 'End Date', 'Solutions', 'Shift Hour', 'Contrect Rate' ]; public close(status) { this.getEmployeelist(); this.openDilog = false; } public hiddenColumns: string[] = []; public isHidden(columnName: string): boolean { var c = this.columns[this.columnsLabel.indexOf(columnName)]; return this.hiddenColumns.indexOf(c) > -1; } public isHiddenGrid(columnName: string): boolean { return this.hiddenColumns.indexOf(columnName) > -1; } public isDisabled(columnName: string): boolean { var c = this.columns[this.columnsLabel.indexOf(columnName)] return this.columns.length - this.hiddenColumns.length === 1 && !this.isHidden(columnName); } public hideColumn(columnName: string): void { var c = this.columns[this.columnsLabel.indexOf(columnName)] const hiddenColumns = this.hiddenColumns; if (!this.isHidden(columnName)) { hiddenColumns.push(c); } else { hiddenColumns.splice(hiddenColumns.indexOf(c), 1); } } public getAccount(x: string, event: string) { this._storeService.getAccountBySearch(x, event).subscribe((res: any) => { if (res) { this.accountList = 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 == 'income') { this.getAccount(x.event, 'A'); } } }); } }