import { Component, Injector, OnInit, ViewChild, ViewEncapsulation, Input, ViewContainerRef } from '@angular/core'; import { ActivatedRoute } 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, ControllerKpiCountServiceProxy } from '@shared/service-proxies/service-proxies'; import * as moment from 'moment'; 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 { ControllerRoutesService } from '@app/sprintship/controller/routes/croutes.service'; import { format, parse } from 'path'; import { TimerService } from '@app/sprintship/core-components/timer/timer.service'; @Component({ selector: 'kpi-box', templateUrl: './kpi-box.component.html', encapsulation: ViewEncapsulation.None, styleUrls: ['./kpi-box.component.less'], animations: [appModuleAnimation()] }) export class KPIBoxComponent extends AppComponentBase implements OnInit { hide : boolean; isLoading : boolean; @Input('statistics') statistics: { total_vehicles: number, total_packages: number }; @Input ('total_packages') total_packages: number; @Input ('total_vehicles') total_vehicles: number; @Input() filters: { locationFilter: number; statusFilter: number; typeFilter: number; startDate: Date; endDate: Date; routeIds: Array; completed: boolean, }; searchFilters: { route: string, vehicleName: string, driverName: string, showCompletion: boolean, routeIds: Array, orderType: Array, deviceType: string, completed: boolean, } = {}; todaysDate = moment().format('MM/DD/YYYY'); subscription: Subscription; currentKPI: boolean; active: string; constructor( injector: Injector, private _routeService: RouteServiceProxy, private _cRoutesService: ControllerRoutesService, private _timerService: TimerService, private _cKpiCountService: ControllerKpiCountServiceProxy, private viewContainerRef: ViewContainerRef, ) { super(injector); var that = this; this.subscription = _cRoutesService.StatisticsObj.subscribe(details=> { that.total_packages = details.total_packages; that.total_vehicles = details.total_vehicles; }); this.subscription = _timerService.tmrMediator.subscribe(details=> { }); this.currentKPI = false; this.active = 'NONE'; } ngOnInit(): void { // this.total_packages = this.statistics.total_packages; } tryThis(): void { } getStatisticsStandard(result: any) { // $('#total_packages').html(result.total_stops); // $('#delivered').html(result.total_delivered); // if(result.total_delivered === 0) // { // $('#completion_rate').html('0%'); // } // else // { // let totalCompletion = (parseFloat(result.total_delivered) / parseFloat(result.total_stops)) * 100; // $('#completion_rate').html(totalCompletion.toFixed(2) + '%'); // } } getFilters(){ if (localStorage.getItem('searchFilter') == null) { this.searchFilters.route = ''; this.searchFilters.driverName = ''; this.searchFilters.showCompletion = false; this.searchFilters.vehicleName = ''; } else { this.searchFilters = JSON.parse(localStorage.getItem('searchFilter')); } } getKpiCount(): void { this.getFilters(); $('#total_packages').html('0'); $('#delivered').html('0'); $('#completion_rate').html('0%'); this.isLoading = true; let locationFilter = this.filters.locationFilter; let timeId = localStorage.getItem('timeZoneId'); moment.tz.setDefault(timeId); if(localStorage.getItem('loadDate').search('-') > 0) { var str = localStorage.getItem('loadDate').split('-'); var str1 = str[1] + '/' + str[2] + '/' + str[0]; localStorage.setItem('loadDate',str1); } this._cKpiCountService.getRefreshTime(locationFilter) .subscribe(result=>{ this.getParentComponent().lastRefresh = sessionStorage.setItem('last-refresh', result); }); this._cKpiCountService.getKPICount( undefined, locationFilter, moment.tz(localStorage.getItem('loadDate'), timeId), moment.tz(localStorage.getItem('loadDate'), timeId).endOf('day'), this.searchFilters.showCompletion, undefined, undefined, undefined ).subscribe( result => { var key; let unassigned = 0; let remaining_pickup = 0; let remaining_deliveries = 0; let with_issues = 0; let picked_up = 0; let delivered = 0; let completion_rate = 0; this.isLoading = false; // if(result.result == null) // { // GetControllerVehicleCount // } for (key in result) { switch(result[key].code) { case "ARRIVED" : picked_up = picked_up + result[key].orderId break; case "WAITING_PICKUP" : remaining_pickup = remaining_pickup + result[key].orderId break; case "PICKED_UP" : picked_up = picked_up + result[key].orderId break; case "IN_TRANSIT" : picked_up = picked_up + result[key].orderId break; case "DELIVERED" : delivered = delivered + result[key].orderId break; case "PICKUP_IN_TRANSIT" : remaining_pickup = remaining_pickup + result[key].orderId break; case "ARRIVED_PICKUP" : remaining_pickup = remaining_pickup + result[key].orderId break; case "DELIVERY_IN_TRANSIT" : picked_up = picked_up + result[key].orderId break; case "ARRIVED_DELIVERY" : picked_up = picked_up + result[key].orderId break; case "DELIVERY_ISSUE" : with_issues = with_issues + result[key].orderId break; case "WAREHOUSE_IN_TRANSIT" : picked_up = picked_up + result[key].orderId break; case "WAREHOUSE" : remaining_pickup = remaining_pickup + result[key].orderId break; } } this._cKpiCountService.getUnassignedPackagesCount( undefined, locationFilter, moment.tz(localStorage.getItem('loadDate'), timeId), moment.tz(localStorage.getItem('loadDate'), timeId).endOf('day'), this.searchFilters.showCompletion, undefined, undefined, undefined ).subscribe(result => { let that = this; if (result > 0) { unassigned = unassigned + result; locationFilter = this.filters.locationFilter; $('#box-unassigned').click(function(){ var startDate = moment.tz(moment(localStorage.getItem('loadDate')).format("YYYY-MM-DD"),timeId); var loadDate = startDate['_i']; window.open('app/sprintship/unassigned-packages/', '_blank'); }); } else { $('#box-unassigned').unbind('click'); } $('#unassigned').html(unassigned.toString()); $('#remaining_pickup').html((remaining_pickup + unassigned).toString()); $('#remaining_deliveries').html((picked_up + remaining_pickup + unassigned).toString()); $('#with_issues').html(with_issues.toString()); $('#picked_up').html((delivered + picked_up).toString()); $('#delivered').html(delivered.toString()); switch(abp.session.tenantId) { case 2 : //antech completion rate $('#completion_rate').html(((picked_up + delivered)/(picked_up + remaining_pickup + with_issues + delivered + unassigned) * 100 || 0 ).toFixed(2) + '%'); break; case 4 : //hellofresh completion rate $('#completion_rate').html((delivered/(picked_up + remaining_pickup + with_issues + delivered + unassigned) * 100 || 0 ).toFixed(2) + '%'); break; } $('#completion_rate').html((delivered/(picked_up + remaining_pickup + with_issues + delivered + unassigned) * 100 || 0 ).toFixed(2) + '%'); $('#total_packages').html((picked_up + remaining_pickup + with_issues + delivered + unassigned).toString()); }); }); this._cKpiCountService.getControllerVehicleCount( undefined, undefined, this.filters.locationFilter, moment.tz(localStorage.getItem('loadDate'), timeId), moment.tz(localStorage.getItem('loadDate'), timeId).endOf('day'), this.searchFilters.showCompletion, this.filters.routeIds, undefined, undefined, undefined ).subscribe(result => { $('#total_vehicles').html('0'); if (result > 0 ) { $('#total_vehicles').html(result.toString()); } }); } getParentComponent() { return this.viewContainerRef[ '_data' ].componentView.component.viewContainerRef[ '_view' ].component } remainingPickUp(){ if($('.filter-right-generic-remaining').hasClass('active')) { $('.filter-right-generic').removeClass('active'); this.getParentComponent().staticTabs.tabs[0].active = true; this.getParentComponent().filters.orderStatusCode = "NONE"; this.getParentComponent().loadOrderListComponent('NONE'); } else { $('.filter-right-generic').removeClass('active'); $('.filter-right-generic-remaining').addClass('active'); this.getParentComponent().staticTabs.tabs[1].active = true; this.getParentComponent().filters.orderStatusCode = "REMAINING_PICKUP"; this.getParentComponent().loadOrderListComponent('KPI_REMAINING_STOPS'); } } remainingDeliveries(){ // $('.filter-right-generic').removeClass('active'); // $('.filter-right-generic-rdeliveries').addClass('active'); // this.getParentComponent().staticTabs.tabs[2].active = true; // this.getParentComponent().filters.orderStatusCode = "REMAINING_DELIVERIES"; // this.getParentComponent().loadOrderPackagesListComponent(); if($('.filter-right-generic-rdeliveries').hasClass('active')) { $('.filter-right-generic').removeClass('active'); this.getParentComponent().staticTabs.tabs[0].active = true; this.getParentComponent().filters.orderStatusCode = "NONE"; this.getParentComponent().loadOrderListComponent('NONE'); } else { $('.filter-right-generic').removeClass('active'); $('.filter-right-generic-rdeliveries').addClass('active'); this.getParentComponent().staticTabs.tabs[1].active = true; this.getParentComponent().filters.orderStatusCode = "REMAINING_DELIVERIES"; this.getParentComponent().loadOrderListComponent('KPI_REMAINING_DELIVERIES'); } } pickedUp(){ // this.getParentComponent().staticTabs.tabs[2].active = true; // this.getParentComponent().filters.orderStatusCode = "PICKED_UP"; // this.getParentComponent().loadOrderPackagesListComponent(); if($('.filter-right-generic-pickedup').hasClass('active')) { $('.filter-right-generic').removeClass('active'); this.getParentComponent().staticTabs.tabs[0].active = true; this.getParentComponent().filters.orderStatusCode = "NONE"; this.getParentComponent().loadOrderListComponent('NONE'); } else { $('.filter-right-generic').removeClass('active'); $('.filter-right-generic-pickedup').addClass('active'); this.getParentComponent().staticTabs.tabs[1].active = true; this.getParentComponent().filters.orderStatusCode = "PICKED_UP"; this.getParentComponent().loadOrderListComponent('KPI_PICKED_UP'); } } delivered(){ if($('.filter-right-generic-delivered').hasClass('active')) { $('.filter-right-generic').removeClass('active'); this.getParentComponent().staticTabs.tabs[0].active = true; this.getParentComponent().filters.orderStatusCode = "NONE"; this.getParentComponent().loadOrderListComponent('NONE'); } else { $('.filter-right-generic').removeClass('active'); $('.filter-right-generic-delivered').addClass('active'); this.getParentComponent().staticTabs.tabs[1].active = true; this.getParentComponent().filters.orderStatusCode = "DELIVERED"; this.getParentComponent().loadOrderListComponent('KPI_DELIVERED'); } // this.getParentComponent().staticTabs.tabs[2].active = true; // this.getParentComponent().filters.orderStatusCode = "DELIVERED"; // this.getParentComponent().loadOrderPackagesListComponent(); } filterDriver() { if($('.filter-right-generic-vehicle').hasClass('active')) { $('.filter-right-generic').removeClass('active'); this.getParentComponent().staticTabs.tabs[0].active = true; } else { $('.filter-right-generic').removeClass('active'); $('.filter-right-generic-vehicle').addClass('active'); this.getParentComponent().staticTabs.tabs[3].active = true; } } unassingedStops() { if($('.filter-right-generic-unassigned').hasClass('active')) { $('.filter-right-generic').removeClass('active'); this.getParentComponent().staticTabs.tabs[0].active = true; this.getParentComponent().filters.orderStatusCode = "NONE"; this.getParentComponent().loadOrderListComponent('NONE'); } else { $('.filter-right-generic').removeClass('active'); $('.filter-right-generic-unassigned').addClass('active'); this.getParentComponent().staticTabs.tabs[1].active = true; this.getParentComponent().filters.orderStatusCode = "UNASSIGNED"; this.getParentComponent().loadOrderListComponent('KPI_UNASSIGNED'); } } packageWithIssue(){ if($('.filter-right-generic-issues').hasClass('active')) { $('.filter-right-generic').removeClass('active'); this.getParentComponent().staticTabs.tabs[0].active = true; this.getParentComponent().filters.orderStatusCode = "NONE"; this.getParentComponent().loadOrderListComponent('NONE'); } else { $('.filter-right-generic').removeClass('active'); $('.filter-right-generic-issues').addClass('active'); this.getParentComponent().staticTabs.tabs[1].active = true; this.getParentComponent().filters.orderStatusCode = "ISSUE"; this.getParentComponent().loadOrderListComponent('KPI_DELIVERY_ISSUE'); } // this.getParentComponent().staticTabs.tabs[2].active = true; // this.getParentComponent().filters.orderStatusCode = "PACKAGE_ISSUES"; // this.getParentComponent().loadOrderPackagesListComponent(); } filterKpiBox(filter: string) { this.getParentComponent().staticTabs.tabs[0].active = true; $('.filter-right-generic').removeClass('active'); // if(this.lastAttempt == filter) // { // } // else // { // filter = 'ALL'; // this.lastAttempt = filter; // } // alert(this.active + ' ' + this.currentKPI); if(this.active != filter && this.currentKPI == true) { this.currentKPI = false; } if(this.currentKPI == true) { this.currentKPI = false; filter = 'NEW'; } else { this.currentKPI = true; switch (filter) { case 'NEW': $('.filter-right-generic-all').addClass('active'); this.active = filter; break; case 'ASSIGNED': $('.filter-right-generic-assigned').addClass('active'); this.active = filter; break; case 'UNASSIGNED': $('.filter-right-generic-unassigned').addClass('active'); this.active = filter; break; case 'MODIFICATION': $('.filter-right-helpdesk-modification').addClass('active'); this.active = filter; break; case 'CLOSED': $('.filter-right-helpdesk-close').addClass('active'); this.active = filter; break; case 'ISSUEORBUG': $('.filter-right-helpdesk-issue').addClass('active'); this.active = filter; break; case 'NEWFEATURE': $('.filter-right-helpdesk-new-feature').addClass('active'); this.active = filter; break; } } // this.getParentComponent().filterKpiBox(filter); let timeId = localStorage.getItem('timeZoneId'); moment.tz.setDefault(timeId); this._cKpiCountService.getKPICountList( this.filters.locationFilter, moment.tz(localStorage.getItem('loadDate'), timeId), moment.tz(localStorage.getItem('loadDate'), timeId).endOf('day'), filter, undefined, undefined, undefined ).subscribe( result => { console.log('RESULT'); console.log(result); var key; var arr = []; for(key in result) { arr.push(result[key].routeIds); } this.getParentComponent().filters.routeIds = arr; this.getParentComponent().filterRoutes(); } ); } }