import { Component, OnInit } from '@angular/core'; import { AdvanceViewModel, AdvanceInstallmentViewModel } from '../advances-class'; import { KGridHelper } from '../../../_helpers/k-grid.class'; import { Subject, Subscription } from 'rxjs'; import { NotificationService } from '../../../_services/notification.service'; import { AdvancesService } from '../advances-services'; import { StoreService } from '../../store/store.services'; import { ActivatedRoute, Router } from '@angular/router'; import { ItemViewModel } from '../../store/store-model'; import { debounceTime } from 'rxjs/operators'; import { PageChangeEvent } from '@progress/kendo-angular-grid'; import { SortDescriptor } from '@progress/kendo-data-query'; import { LocalService } from '../../../_services/local.service'; @Component({ selector: 'app-advance-bulk-entries', templateUrl: './advance-bulk-entries.component.html' }) export class AdvanceBulkEntriesComponent 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 saving = false; public nowDate = new Date(); public maxDate = new Date(); public transactionDate = new Date(); public _isFormValid = true; constructor(public _notificationService: NotificationService, public _advancesService: AdvancesService, public _storeService: StoreService, private activatedRoute: ActivatedRoute, private router: Router, private _localService: LocalService) { } ngOnInit() { this.initializeProps(); this.getAccount(' ', 'E'); this.getAdvancesList(); // this.getAdvancesList(); this.handleSearchEvents(); } checkFormValidity() { let things = this.kGrid.gridView.data.filter(d => d.InstallmentAmount > d.remainingAmount); console.log(things); if (things.length > 0) { this._isFormValid = false; }else { this._isFormValid = true; } console.log(this._isFormValid); } private initializeProps() { this.kGrid.loading = false; this.kGrid.pageSize = 20; this.kGrid.pageSizes = [20, 50, 100]; } public getAdvancesList() { this.kGrid.loading = true; this._advancesService.getAdvanceBulkList().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.parseList(this.kGrid.data), total: this.kGrid.totalRecords }; this.kGrid.loading = false; }, (err) => { this._notificationService.notify('danger', 'Something went wrong'); }); } AddAdvance() { 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; } _isAdvanceEdit = false; _minInstallments = 0; _minAdvanceAmount = 0; EditAdvance(item: AdvanceViewModel) { this._advanceModel = item; this._advanceModel.CreatedDate = new Date(item.CreatedDate); this._isAdvanceEdit = true; this._minInstallments = (item.PaidInstalement + 1); this._minAdvanceAmount = item.PaidAmount; this.openAddAdvacneDialog = true; } 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.validateAdvanceInstallmentCode('-1'); this.openAddAdvacneInstallmentDialog = true; } intiElement(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; item.remainingAmount = _remainingAmount; item.remainingInstallments = _remainingInstalments; item.InstallmentAmount = _remainingAmount / _remainingInstalments; item.InstallmentNo = item.PaidInstalement + 1; item.Fk_EmployeAccount = new ItemViewModel(item.Fk_EmployeAccount.Id, item.Fk_EmployeAccount.Name); item.Fk_AdvanceMaster = new ItemViewModel(item.Id); } parseList(list) { list.forEach(el => { this.intiElement(el); }); return list; } BulkAdvanceReturn() { } ClearForm() { } onSubmitBulkAdvances() { let itemList = []; this.kGrid.gridView.data.forEach(item => { item.CreatedDate = this.transactionDate; itemList.push(item); }); // console.log(itemList); this.saving = true; this._advancesService.saveAdvanceInstallmentAuto(itemList).subscribe((res: any) => { // console.log(res); this._notificationService.notify('success', 'Successfully Saved!'); // this.getAdvancesList(); this.saving = false; this.router.navigate(['/advances/advance-list']); }, (err) => { console.log(err); this._notificationService.notify('danger', 'Something Went Wrong'); this.saving = false; }); } RemoveItem(item) { this.kGrid.gridView.data.splice(this.kGrid.gridView.data.indexOf(item), 1); } OnSubmitAdvance() { this._advancesService.saveAdvanceMaster(this._advanceModel).subscribe((res) => { this.openAddAdvacneDialog = false; this.getAdvancesList(); this._notificationService.notify('success', 'Successfully Saved'); }, (err) => { this._notificationService.notify('danger', 'Something Went Wrong'); }); } OnSubmitAddAdvanceInstallment() { this._advancesService.saveAdvanceInstallment(this._advanceReturnModel).subscribe((res) => { this.openAddAdvacneInstallmentDialog = 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 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(); } }