import { Component, OnInit } from '@angular/core'; import { KGridHelper } from '../../../_helpers/k-grid.class'; import { Subject, Subscription } from 'rxjs'; import { NotificationService } from '../../../_services/notification.service'; import { AdvancesService } from '../advances-services'; import { ActivatedRoute } from '@angular/router'; import { SortDescriptor } from '@progress/kendo-data-query'; import { PageChangeEvent } from '@progress/kendo-angular-grid'; import { debounceTime } from 'rxjs/operators'; import { StoreService } from '../../store/store.services'; import { ItemViewModel } from '../../store/store-model'; import { AdvanceViewModel, AdvanceInstallmentViewModel } from '../advances-class'; import { LocalService } from '../../../_services/local.service'; @Component({ selector: 'app-advance-management', templateUrl: './advance-management.component.html' }) export class AdvanceManagementComponent implements OnInit { public openAddAdvacneDialog = false; public openAddAdvacneInstallmentDialog = false; private _advancesList: AdvanceViewModel[] = []; public kGrid: KGridHelper = new KGridHelper(); public searchEvent = new Subject(); public searchSubcription: Subscription = null; public expAccountList: any[] = []; public openDilogConfirmation = false; public _advanceModel: AdvanceViewModel; public _advanceReturnModel: AdvanceInstallmentViewModel; public employeeAccountList: any[] = []; public title = ''; public saving = false; public nowDate = new Date(); public maxDate = new Date(); constructor(public _notificationService: NotificationService, public _advancesService: AdvancesService, public _storeService: StoreService, private activatedRoute: ActivatedRoute, public _localService: LocalService) { } ngOnInit() { this.initializeProps(); this.getAccount(' ', 'E'); this.getAdvancesList(); // this.getAdvancesList(); this.handleSearchEvents(); } private initializeProps() { this.kGrid.loading = false; this.kGrid.pageSize = 20; this.kGrid.pageSizes = [20, 50, 100]; } public getAdvancesList() { this.kGrid.loading = true; this._advancesService.getAdvanceList().subscribe((res: any) => { console.log(res); this._advancesList = res; this.kGrid.data = []; this.kGrid.data = res; this.kGrid.totalRecords = this.kGrid.data.length; this.kGrid.gridView = { data: this.kGrid.data, total: this.kGrid.totalRecords }; this.kGrid.loading = false; }, (err) => { this._notificationService.notify('danger', 'Something went wrong'); }); } AddAdvance() { this.title = 'Add'; this._advanceModel = new AdvanceViewModel(); this._advanceModel.CreatedDate = new Date(); this.validateAdvanceCode('-1'); this.maxDate = new Date(); this.maxDate.setHours(this.maxDate.getHours() + 2); this.openAddAdvacneDialog = true; this._isAdvanceEdit = false; } public installmentList = []; public openAdvanceInstallmentListDialog = false; GetDetails(item) { console.log(item); this._advancesService.GetAdvanceInstallmentListByMastId(item.Id).subscribe((res: any) => { this.installmentList = res; this.openAdvanceInstallmentListDialog = true; console.log(res); }, (err) => { this._notificationService.notify('danger', 'Something Went Wrong'); }); } Close() { this.openAdvanceInstallmentListDialog = false; } public _isAdvanceEdit = false; public _minInstallments = 0; public _minAdvanceAmount = 0; EditAdvance(item: AdvanceViewModel) { this.title = 'Edit'; this._advanceModel = item; this._advanceModel.CreatedDate = new Date(item.CreatedDate); this._isAdvanceEdit = true; this._minInstallments = item.TotalInstalment;//(item.PaidInstalement + 1); this._minAdvanceAmount = item.PaidAmount; this.openAddAdvacneDialog = true; } public _maxAdvanceReturnAmount = 0; public remainingInstallments = 0; AddReturn(item) { this._advanceReturnModel = new AdvanceInstallmentViewModel(); this._advanceReturnModel.CreatedDate = new Date(); this.maxDate = new Date(); this.maxDate.setHours(this.maxDate.getHours() + 2); let _remainingAmount = (item.AdvanceAmount - item.PaidAmount); let _remainingInstalments = item.TotalInstalment - item.PaidInstalement; this._advanceReturnModel.InstallmentAmount = _remainingAmount / _remainingInstalments; this._advanceReturnModel.InstallmentNo = item.PaidInstalement + 1; this._advanceReturnModel.Fk_EmployeAccount = new ItemViewModel(item.Fk_EmployeAccount.Id, item.Fk_EmployeAccount.Name); this._advanceReturnModel.Fk_AdvanceMaster = new ItemViewModel(item.Id); this._maxAdvanceReturnAmount = _remainingAmount; this.remainingInstallments = _remainingInstalments; this.validateAdvanceInstallmentCode('-1'); this.openAddAdvacneInstallmentDialog = true; } BulkAdvanceReturn() { } OnSubmitAdvance() { this.saving = true; this._advancesService.saveAdvanceMaster(this._advanceModel).subscribe((res) => { this.openAddAdvacneDialog = false; this._isAdvanceEdit = false; this.saving = false; this.getAdvancesList(); this._notificationService.notify('success', 'Successfully Saved'); }, (err) => { this._notificationService.notify('danger', 'Something Went Wrong'); }); } OnSubmitAddAdvanceInstallment() { this.saving = true; this._advancesService.saveAdvanceInstallment(this._advanceReturnModel).subscribe((res) => { this.openAddAdvacneInstallmentDialog = false; this.saving = false; this.getAdvancesList(); this._notificationService.notify('success', 'Successfully Saved'); }, (err) => { this._notificationService.notify('danger', 'Something Went Wrong'); }); } validateAdvanceCode(code) { this._advancesService.validateAdvanceCode(code).subscribe((res: any) => { if (res.msg === 'Valid') { this._advanceModel.RefId = res.code; } }, (err) => { this._notificationService.notify('danger', 'Something Went Wrong'); }); } validateAdvanceInstallmentCode(code) { this._advancesService.validateAdvanceInstallmentCode(code).subscribe((res: any) => { if (res.msg === 'Valid') { this._advanceReturnModel.RefId = res.code; } }, (err) => { this._notificationService.notify('danger', 'Something Went Wrong'); }); } public openDeleteConfirmationDialog = false; public deleting = false; CloseDeleteDialog() { this.openDeleteConfirmationDialog = false; } OpenDeleteConfirmationDialog(item) { this._advanceModel = item; this.openDeleteConfirmationDialog = true; } ConfirmDelete() { this.deleting = true; this._advancesService.deleteAdvanceMaster(this._advanceModel.Id).subscribe((res: any) => { this._notificationService.notify('success', 'Deleted Successfully'); this.deleting = false; this._advanceModel = new AdvanceViewModel(); this.getAdvancesList(); this.openDeleteConfirmationDialog = false; }, (err) => { this._notificationService.notify('danger', 'Something Went Wrong'); }); } public getAccount(x: string, event: string) { this._storeService.getAccountBySearch(x, event).subscribe((res: any) => { if (res) { if (event === 'E') { this.employeeAccountList = 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) { if (x.filter == 'Fk_EmployeAccount') { this.getAccount(x.event, 'E'); } } }); } public close(status) { this.openAddAdvacneDialog = false; this.openAddAdvacneInstallmentDialog = false; } public changePagesize(value: any) { this.kGrid.pageSize = +value; this.kGrid.skip = 0; // this.getCategory(); } 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.getCategory(); } }