import { Component, Injector, OnInit, ViewChild, ViewEncapsulation, Input, OnDestroy, ElementRef, Renderer, QueryList, ViewChildren, ViewContainerRef, Output, EventEmitter } from '@angular/core'; import { ActivatedRoute, Router } from '@angular/router'; import { AppConsts } from '@shared/AppConsts'; import { appModuleAnimation } from '@shared/animations/routerTransition'; import { AppComponentBase } from '@shared/common/app-component-base'; import { RouteListDto, RouteServiceProxy, ControllerRouteServiceProxy, ControllerKpiCountServiceProxy, UpdateRouteInput, UpdateCompleteRouteInput, VehicleLogServiceProxy, ShippingPackageServiceProxy, RealTimeEtaServiceProxy, GetControllerRouteStatusInputDto, UpdateLockedRouteInput } from '@shared/service-proxies/service-proxies'; import * as moment from 'moment-timezone'; import { LazyLoadEvent } from 'primeng/components/common/lazyloadevent'; import { Paginator } from 'primeng/components/paginator/paginator'; import { Table } from 'primeng/components/table/table'; import { HttpClient } from '@angular/common/http'; import { finalize } from 'rxjs/operators'; import { Subscription } from 'rxjs'; import { CheckBoxComponent } from '@app/sprintship/core-components/checkbox/checkbox.component.ts'; import { StatusComponent } from '@app/sprintship/controller/status/status.component.ts'; import { ControllerRoutesService } from '@app/sprintship/controller/routes/croutes.service'; import * as _ from 'lodash'; import * as status_icon from '@app/sprintship/controller/status/status_icon.json'; import { KPIBoxComponent } from '@app/sprintship/controller/kpi-box/kpi-box.component'; import { TimerService } from '@app/sprintship/core-components/timer/timer.service'; import { RouteDetailModal } from '@app/sprintship/controller/route-detail/route-detail-modal.component'; import { MarkAsCompleteModalComponent } from '../routes/mark-as-complete-modal.component'; import { analyzeAndValidateNgModules } from '@angular/compiler'; declare var $: any; declare var jquery: any; @Component({ selector: 'table-routes-laboratory-details', templateUrl: 'table-routes-laboratory-details.component.html', encapsulation: ViewEncapsulation.None, styleUrls: ['./table-routes-laboratory-details.component.less', './tables.checkbox.component.less'], animations: [appModuleAnimation()], providers: [VehicleLogServiceProxy, RealTimeEtaServiceProxy] }) export class TableRoutesLaboratoryComponents extends AppComponentBase implements OnInit, OnDestroy { @ViewChild('dataTable', { static: true }) dataTable: Table; @ViewChild('paginator', { static: true }) paginator: Paginator; @ViewChildren('CheckBoxComponent') chkBoxList: QueryList < any > ; @ViewChildren(KPIBoxComponent) tKpiBoxComponents: KPIBoxComponent; @ViewChild(StatusComponent, { static: false }) statusDiv: StatusComponent; @ViewChild('routeDetailModal', { static: true }) routeDetailModal: RouteDetailModal; @ViewChild('markAsCompleteModalComponent', { static: true }) markAsCompleteModalComponent: MarkAsCompleteModalComponent; /**** ELEMENTS ****/ @ViewChild('dataTable', { read: ElementRef, static: true }) dTable: ElementRef; @Input() filters: { locationFilter: number; statusFilter: number; typeFilter: number; startDate: Date; endDate: Date; datePicked: string; filterTxt: string, routeId: string, routeName: string, vehicleName: string, driverName: string, completed: boolean, orderType: Array, osName: string, routeIds: Array, } = < any > {}; device_type: string; routeIds: any; routeIdsStops: GetControllerRouteStatusInputDto = new GetControllerRouteStatusInputDto(); attr: any; respkey: any; saving = false; completion: any; route: UpdateRouteInput = new UpdateRouteInput(); updateCompletedInput = new UpdateCompleteRouteInput(); updateLockedInput = new UpdateLockedRouteInput(); totalMileage: number; given: number; convert: any; hours: number; etaHours: any; minutes: number; routeIdStatus: any; etaMinutes: any; routeIdsForEta: any; routeIdsToLab: any; routeIdsToClinics: any; key2: any; packages: Array < { id: number, name: string } > = new Array(); filterFlyout:{}; filterFlyoutHolder = []; finalFilterResult = [] sprintIds: Array = new Array(); sprintIdsList = []; selectAll: boolean = false; checkboxes_: boolean = false; @Input('search_string') search: string; subscription: Subscription; private cBox: CheckBoxComponent; constructor( injector: Injector, private _routeService: RouteServiceProxy, private _controllerRouteService: ControllerRouteServiceProxy, private _cRoutesService: ControllerRoutesService, private _cKpiService: ControllerKpiCountServiceProxy, private _vehicleLogsService: VehicleLogServiceProxy, private _controllerRoutePackages: ShippingPackageServiceProxy, private _timerService: TimerService, private router: Router, private renderer: Renderer, private elem: ElementRef, private _realTimeEtaService: RealTimeEtaServiceProxy, private viewContainerRef: ViewContainerRef, ) { super(injector); this.subscription = _cRoutesService.setStatisticsObj.subscribe(details => { }); this.subscription = _timerService.tmrMediator.subscribe(details => { }); } ngOnInit(): void { this.sprintIdsList = []; localStorage.setItem('routeIds', JSON.stringify(this.sprintIdsList)); } setFilter(value: string) { this.search = value; } ngOnDestroy(): void { } formatDate(date) { var d = new Date(date), month = '' + (d.getMonth() + 1), day = '' + d.getDate(), year = d.getFullYear(); if (month.length < 2) month = '0' + month; if (day.length < 2) day = '0' + day; return [year, month, day].join('-'); } populateTable(event ? : LazyLoadEvent): void { let timeId = localStorage.getItem('timeZoneId'); // moment.tz.setDefault(timeId); var that = this; var key, arrResult = [], resp; this.attr = []; that.routeIds = []; that.routeIdStatus = []; that.routeIdsForEta = []; that.routeIdsToLab = []; that.routeIdsToClinics = []; that.spinnerService.show(); if (this.filters.locationFilter !== undefined) { this.device_type = 'IOS'; // that.primengTableHelper.defaultRecordsCountPerPage = 50; if (this.primengTableHelper.shouldResetPaging(event)) { this.paginator.changePage(0); return; } let routeIdObj = {}; this.packages = []; this.filterFlyoutHolder = []; this.finalFilterResult = []; this._controllerRouteService.getFullListLaboratoryEdition(undefined, this.filters.locationFilter, undefined, this.filters.routeId, this.filters.vehicleName, this.filters.routeName, this.filters.driverName, moment.tz(localStorage.getItem('loadDate'), timeId), moment.tz(localStorage.getItem('loadDate'), timeId), this.filters.filterTxt, (this.filters.osName=="null") ? undefined : this.filters.osName, this.filters.completed, this.filters.routeIds, this.filters.orderType, this.primengTableHelper.getSorting(this.dataTable), this.primengTableHelper.getMaxResultCount(this.paginator, event), this.primengTableHelper.getSkipCount(this.paginator, event)) .subscribe(result => { that.spinnerService.hide(); this._controllerRoutePackages.getShippingPackage(undefined, undefined, undefined, undefined, undefined, undefined).subscribe(resultPackages => { let finalResult = {}; let fResult = { "result": "OK", "total_vehicles": 0, "total_packages": 0 }; let vehicles = []; this.primengTableHelper.totalRecordsCount = result.length; this.primengTableHelper.records = result; if(result.length!=0){ for (key in result) { if (result[key].driverStatus == "Clocked_in") { that.routeIdStatus.push({"RouteId" : result[key].routeId, "Status" : result[key].driverStatus }); } that.routeIds.push(result[key].routeId); result[key]['completionstr'] = moment(result[key]['completion']).tz(timeId).format('h:mm a'); result[key]['Status'] = 'NONE'; result[key]['TotalPackages'] = 0; vehicles[result[key].vehicleId] = result[key].vehicleId; let fullName = result[key].fullName ? "/" +result[key].fullName : ""; this.filterFlyout = { 'route_id': result[key].routeId, 'routeName': result[key].routeTemplateName +fullName, } this.filterFlyoutHolder.push(this.filterFlyout); } this.getParentComponent().getRouteAndDriver(this.filterFlyoutHolder); that.packages = []; resultPackages.items.forEach(element => { if (element.name !== 'Default') { that.packages.push(element); } }); fResult.total_vehicles = Object.keys(vehicles).length; this.routeIdsStops.routeIds = that.routeIds; that._controllerRouteService.stopsStats(that.routeIdsStops) .subscribe(resultStatus => { let total_packages = 0; if (resultStatus == null) { that.spinnerService.hide(); } else { if (Object.keys(resultStatus).length > 0) { var resp; var rStatus = []; for (key in resultStatus) { rStatus[resultStatus[key].routeId] = []; resp = that.filterArrayKeys(result, 'routeId', resultStatus[key].routeId.toString()); if (resp !== null) { result[resp]['Status'] = resultStatus[key].statusCode; result[resp]['TotalPackages'] = resultStatus[key].totalPackages; result[resp]['TotalPickedUp'] = resultStatus[key].totalPickedUp; result[resp]['TotalCallin'] = resultStatus[key].totalCallIn; result[resp]['TotalCallinPickedUp'] = resultStatus[key].totalCallInPickedUp; result[resp]['TotalWillCall'] = resultStatus[key].totalWillCall; } else { result[resp]['Status'] = 'NONE'; } for ( that.key2 = 0; that.key2 < that.routeIdStatus.length; that.key2++ ) { if (resultStatus[key].totalPackages == resultStatus[key].totalPickedUp && resultStatus[key].routeId == that.routeIdStatus[that.key2].RouteId) { that.routeIdsToLab.push(resultStatus[key].routeId) } else if (resultStatus[key].totalPackages != resultStatus[key].totalPickedUp && resultStatus[key].routeId == that.routeIdStatus[that.key2].RouteId) { that.routeIdsToClinics.push(resultStatus[key].routeId); } } this.dTable.nativeElement.querySelector('#packageStatus' + resultStatus[key].routeId).innerHTML = `
`; if (resultStatus[key].totalPickedUp > 0) { resultStatus[key].totalPickedUp = resultStatus[key].totalPickedUp > resultStatus[key].totalPackages ? resultStatus[key].totalPackages : resultStatus[key].totalPickedUp; this.dTable.nativeElement.querySelector('#packageStatistics' + resultStatus[key].routeId).innerHTML = ` ` + resultStatus[key].totalPickedUp + ` / ` + resultStatus[key].totalPackages + ` `; } else { this.dTable.nativeElement.querySelector('#packageStatistics' + resultStatus[key].routeId).innerHTML = ` 0 / ` + resultStatus[key].totalPackages + ` `; } let noteIcon = ''; if(resultStatus[key].totalWillCall > 0) { // noteIcon = ''; noteIcon = ""; } this.dTable.nativeElement.querySelector("#willCallStatus" + resultStatus[key].routeId).innerHTML = noteIcon + resultStatus[key].totalCallInPickedUp + " / " + resultStatus[key].totalCallIn; that.attr[resultStatus[key].routeId] = resultStatus[key].statusCode; total_packages = total_packages + resultStatus[key].totalPackages; } if (that.routeIdsToLab.length > 0) { that._realTimeEtaService.getRouteRealTimeEtaLab(that.routeIdsToLab, undefined).subscribe(result => { for (var a in result) { if (result[a].realTimeETA >= 60) { that.given = parseInt(result[a].realTimeETA.toFixed(2)); that.convert = (that.given / 60).toString().split('.'); that.hours = parseInt(that.convert[0]); that.etaHours = that.hours > 1 ? that.hours + ' hrs' : that.hours + ' hr'; that.minutes = (parseInt(that.convert[1].substring(0, 2)) * 60) / 100; that.minutes = Math.round(that.minutes); that.etaMinutes = that.minutes > 1 ? that.minutes + ' mins' : that.minutes + ' min'; $('#eta' + result[a].routeId).html(that.etaHours + ' ' + that.etaMinutes); (that.etaHours + ' ' + that.etaMinutes); } else { $('#eta' + result[a].routeId).html(result[a].realTimeETA > 1 ? result[a].realTimeETA + ' mins' : result[a].realTimeETA + ' min '); } } $.each($('.etaSpinner'), function(key, val) { if ($(val).children().hasClass('routeloader')) { $(val).html(' - '); } }); }); } if (that.routeIdsToClinics.length > 0) { that._realTimeEtaService.getRealTimeEtaClinics(undefined, that.routeIdsToClinics).subscribe(result => { for (var a in result) { if (result[a].realTimeETA >= 60) { that.given = parseInt(result[a].realTimeETA.toFixed(2)); that.convert = (that.given / 60).toString().split('.'); that.hours = parseInt(that.convert[0]); that.etaHours = that.hours > 1 ? that.hours + ' hrs' : that.hours + ' hr'; that.minutes = (parseInt(that.convert[1].substring(0, 2)) * 60) / 100; that.minutes = Math.round(that.minutes); that.etaMinutes = that.minutes > 1 ? that.minutes + ' mins' : that.minutes + ' min'; $('#eta' + result[a].routeId).html(that.etaHours + ' ' + that.etaMinutes); (that.etaHours + ' ' + that.etaMinutes); } else { $('#eta' + result[a].routeId).html(result[a].realTimeETA > 1 ? result[a].realTimeETA + ' mins' : result[a].realTimeETA + ' min '); } } $.each($('.etaSpinner'), function(key, val) { if ($(val).children().hasClass('routeloader')) { $(val).html(' - '); } }); }); } var packageStatObj = this.dTable.nativeElement.querySelectorAll('.packageStatus'); $.each($('.packageStatus'), function(key, val) { if ($(val).children().hasClass('routeloader')) { $(val).html(' '); } }); $.each($('.packageStatistics'), function(key, val) { if ($(val).children().hasClass('routeloader')) { $(val).html(' '); } }); $.each($('.mileage'), function(key, val) { if ($(val).children().hasClass('routeloader')) { $(val).html(' '); } }); } // END ONE } // END OF ELSE fResult.total_packages = total_packages; that._cRoutesService.getTableStatistics(fResult); }); that._controllerRouteService.getCountRouteDetails(that.routeIds) .subscribe(resultCount => { var fCount = JSON.parse(JSON.stringify(resultCount)); var fStatus = []; var x; for (key in fCount) { x = $('#statsCountRoute' + fCount[key].routeId + fCount[key].packageId).data('total'); x = x + parseInt(fCount[key].totalPackages); $('#statsCountRoute' + fCount[key].routeId + fCount[key].packageId).data('total', x); if ($('#statsCountRoute' + fCount[key].routeId + fCount[key].packageId).data('total') !== undefined) { $('#statsCountRoute' + fCount[key].routeId + fCount[key].packageId).data('total', x); $('#statsCountRoute' + fCount[key].routeId + fCount[key].packageId).html(x); $('.totalRoute' + fCount[key].routeId).html(parseInt($('.totalRoute' + fCount[key].routeId).html()) + parseInt(fCount[key].totalPackages)); } } $.each($('.statsCountRoute'), function(key, val) { if ($(val).children().hasClass('routeloader')) { $(val).html('-'); } }); }); that._vehicleLogsService.getTotalMileageWithMultipleRoutes(that.routeIds).subscribe(result => { for (key in result) { this.dTable.nativeElement.querySelector('#mileage' + result[key].routeId).innerHTML = result[key].totalMileage + ' mi'; if ((result[key].totalMileage) > 0) { } else { $('#mileage' + result[key].routeId).css('display', 'none'); } } }); } }); }); // END FULL LIST } } tickBox() { this.cBox.tickBox(); } getSelected() { } filterArray(array: any, key: any, value: any) { var i, arrList = []; for (i in array) { if (array[i][key] == value) { arrList.push(array[i]); } } return arrList; } filterArrayKeys(array: any, key: any, value: any) { var i, keys = 0; for (i in array) { if (array[i][key] == value) { keys = i; } } return keys; } openMap(userId, routeId) { // window.open('/app/sprintship/locator/'+routeId+'/'+this.filters); this.router.navigate([]).then(result => { window.open('/app/sprintship/locator/' + userId + '/' + routeId, '_blank'); }); } navigateDetail(id) { this.router.navigate(['/app/sprintship/controller-routes-details/', id]); } updateRoute() { this._routeService.updateRoute(this.route) .pipe(finalize(() => this.saving = false)) .subscribe(() => { this.notify.info(this.l('SavedSuccessfully')); }); } setDeparture(route: any) { if (!this.route.locked) { if (this.route.departure) { this.route.departure = null; } else { this.route.departure = moment(new Date()); } this.route.vehicleId = route.vehicleId; this.route.loadDate = route.loadDate; this.route.locked = route.locked; this.route.locationId = parseInt(localStorage.getItem('operatingLocationId')); this.route.id = route.routeId; this.updateRoute(); } else { this.notify.info(this.l('Unlocked Loadlist First')); } } // setCompleted(record) { // if (!record.locked) { // if (record.completion) { // record.completion = undefined; // this.updateCompletedInput.dateCompleted = record.completion; // this.updateCompletedInput.id = record.routeId; // this._controllerRouteService.updateCompletedRoute(this.updateCompletedInput) // .pipe(finalize(() => { this.saving = false; })) // .subscribe(() => { // this.notify.info(this.l('Saved Successfully')); // }); // } else { // record.completion = moment.utc(moment(new Date(Date.now())).format('YYYY-MM-DD h:m a')).toDate(); // } // } else { // this.notify.info(this.l('Unlocked Loadlist First')); // } // } setCompleted(record) { this.saving = true; if (!record.locked) { this.updateCompletedInput.id = record.routeId; if (record.completion) { this.updateCompletedInput.completion = false; this._controllerRouteService.updateCompletedRoute(this.updateCompletedInput) .pipe(finalize(() => { this.saving = false; })) .subscribe(() => { this.notify.info(this.l('Saved Successfully')); record.completion = false; }); } else { this.updateCompletedInput.completion = true; this._controllerRouteService.updateCompletedRoute(this.updateCompletedInput) .pipe(finalize(() => { this.saving = false; })) .subscribe(() => { this.notify.info(this.l('Saved Successfully')); record.completion = true; }); } } else { this.notify.info(this.l('Unlocked Loadlist First')); } } setLocked(record) { this.updateLockedInput.routeId = record.routeId; if (record.locked==true) { this.updateLockedInput.locked = false; this._controllerRouteService.updateLockedRoute(this.updateLockedInput) .pipe(finalize(() => { this.saving = false; })) .subscribe(() => { this.notify.info(this.l('Route Unlocked Successfully')); }); record.locked = false; } else { this.updateLockedInput.locked = true; this._controllerRouteService.updateLockedRoute(this.updateLockedInput) .pipe(finalize(() => { this.saving = false; })) .subscribe(() => { this.notify.info(this.l('Route Locked Successfully')); }); record.locked = true; } } getParentComponent() { return this.viewContainerRef[ '_data' ].componentView.component.viewContainerRef[ '_view' ].component } selectRoute(id: number) { if (this.sprintIdsList.indexOf(id) == -1) { this.sprintIdsList.push(id); } else { this.sprintIdsList.splice(this.sprintIdsList.indexOf(id), 1); } localStorage.setItem('routeIds', JSON.stringify(this.sprintIdsList)); if(this.sprintIdsList.length==this.routeIds.length){ this.selectAll = true; }else{ this.selectAll = false; } } CheckAllOptions() { if (!this.selectAll){ this.primengTableHelper.records.forEach(val => { val.selectedRoute = false }); this.sprintIdsList = []; localStorage.setItem('routeIds', JSON.stringify(this.sprintIdsList)); } else this.primengTableHelper.records.forEach(val => { val.selectedRoute = true; this.sprintIdsList.push(val.routeId)}); // this.sprintIdsList = this.routeIds; localStorage.setItem('routeIds', JSON.stringify(this.sprintIdsList)); } }