import { Component, Injector, OnInit, ViewChild, ViewEncapsulation, Input, TemplateRef, 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 { ControllerRouteDetailServiceProxy, UpdateRouteInput,VehicleInput,UpdateRouteByStopInput, RouteServiceProxy, NameValueDto,ShippingPackageServiceProxy,CreateOptimizeInputDto, DriverListInput, Route4MeServiceProxy, RouteOptimizationServiceProxy, OptimizeRouteInput, GoogleDirectionOptimizationServiceProxy, OptimizeByRouteIdInput, ControllerGetDriverListDto, UpdateEtaInput } from '@shared/service-proxies/service-proxies'; import { RouteDetailModal } from './route-detail-modal.component'; import { MovePendingOrder } from './move-order-modal.component'; import { CreateIncidentsModal } from './create-incidents-modal.component'; import { AppSessionService } from '@shared/common/session/app-session.service'; import * as moment from 'moment'; import { Table } from "primeng/table"; import { LazyLoadEvent } from "primeng/primeng"; import { Paginator } from 'primeng/components/paginator/paginator'; import { HttpClient } from '@angular/common/http'; import { finalize } from 'rxjs/operators'; import * as _ from 'lodash'; import { MessageService } from 'primeng/api'; import * as signalR from '@aspnet/signalr'; import {Location} from '@angular/common'; import { LabRouteDetailComponent } from './lab-route-detail.component'; import { FBXRouteDetailComponent } from './fbx-route-detail.component'; import { CommonLookupModalComponent } from '@app/shared/common/lookup/common-lookup-modal.component'; import { AssignStopModalComponent } from '@app/shared/common/lookup/assignStop-modal.component'; import { EntityTypeHistoryModalComponent } from '@app/shared/common/entityHistory/entity-type-history-modal.component'; declare var $: any; declare var jquery: any; @Component({ templateUrl: 'route-detail.component.html', encapsulation: ViewEncapsulation.None, styleUrls: ['./route-detail.component.less'], animations: [appModuleAnimation()], selector: 'routeDetailComponent', providers: [MessageService] }) export class RouteDetailComponent extends AppComponentBase implements OnInit { saving = false; routeId: number = null; details: boolean = false; _isLab: boolean = false; _isFbx: boolean = false; routeCode: string; vehicleName: string; loadDate: string; locationName: string; reload: any; route: UpdateRouteInput = new UpdateRouteInput(); routeDetailTenant:string = 'STANDARD'; totalItems: any; packages: Array<{id: number, name: string}> = new Array(); OptimizeInput = new CreateOptimizeInputDto(); tableDetails:any; driverListInput:any; data:any; stopInput:any; userId: any; routeName: any; updateOnly = false; updateRouteId: number = 0; updateLocationId: number = 0; updateRouteTemplateId: number = 0; @ViewChild('driversList', {static: true}) driversList: CommonLookupModalComponent; @ViewChild('stopList', {static: true}) stopList: AssignStopModalComponent; @ViewChild('routeDetailModal', { static: true }) routeDetailModal: RouteDetailModal; @ViewChild('movePendingOrderModal', { static: true }) movePendingOrderModal: MovePendingOrder; @ViewChild('createIncidentsModal', { static: true }) createIncidentsModal: CreateIncidentsModal; @ViewChild('labRouteDetail' , { static: false}) public labRouteDetail: LabRouteDetailComponent; @ViewChild('hfRouteDetail' , { static: false}) public hfRouteDetail: LabRouteDetailComponent; @ViewChild('fbxRouteDetail' , { static: false}) public fbxRouteDetail: FBXRouteDetailComponent; // @Input() hfRouteDetails: TemplateRef; @Input() labRouteDetails: any; @ViewChild("dataTable", { static: true }) dataTable: Table @ViewChild("paginator", { static: true }) paginator: Paginator @Output() reloadTable: EventEmitter = new EventEmitter(); @ViewChild('entityTypeHistoryModal', { static: true }) entityTypeHistoryModal: EntityTypeHistoryModalComponent; _entityTypeFullName = 'SprintTek.Shipping.Routes.Route'; entityHistoryEnabled = false; labRouteRefresh: Function; localRouteIds = []; optimizer: boolean = false; locationId:any; toll: boolean = false; highways: boolean = false;; ferries: boolean = false; indoor: boolean = false;; // locations: LocationListDto[] = []; constructor( injector: Injector, private id: ActivatedRoute, private _controllerRouteDetails: ControllerRouteDetailServiceProxy, private _controllerRoutePackages: ShippingPackageServiceProxy, private _controllerRoute: RouteServiceProxy, private _appSessionService: AppSessionService, private messageService: MessageService, private _location:Location, private _route4me : Route4MeServiceProxy, private router: Router, private optimization: RouteOptimizationServiceProxy, private _googleOptimization: GoogleDirectionOptimizationServiceProxy // private _routeService: RouteServiceProxy ) { super(injector); this._isLab = abp.features.isEnabled('App.RouteDetails'); this._isFbx = abp.features.isEnabled('App.FBXFeature'); if(this._isLab){ this.routeDetailTenant = 'LAB'; } if(this._isFbx){ this.routeDetailTenant = 'FBX'; } this.id.paramMap.subscribe(params => { this.routeId = Number(params.get('id')); }); } ngOnInit(): void { this.setIsEntityHistoryEnabled(); this.routeName = localStorage.getItem('RouteName') //this.primengTableHelper.showLoadingIndicator(); // this.getDetails(); // const connection = new signalR.HubConnectionBuilder() // .configureLogging(signalR.LogLevel.Information) // .withUrl("http://localhost:65302/location") // .build(); // connection.start().then(function () { // console.log('Connecteddfsdfsdf!'); // }).catch(function (err) { // return console.error(err.toString()); // }); // connection.on("GetLocation", (latitude: string,longitude: string) => { // this.messageService.add({ severity: 'success', summary: 'Success Message', detail: 'Notificiation' }); // }); this.locationId = parseInt(localStorage.getItem('operatingLocationId')); this.driversList.configure({ title: this.l('Assign Driver'), dataSource: (skipCount: number, maxResultCount: number, filter: string, tenantId?: number, locationId?:number) => { let input = new DriverListInput(); input.filter = filter; input.maxResultCount = maxResultCount; input.skipCount = skipCount; input.tenantId = tenantId; input.locationId = parseInt(localStorage.getItem('operatingLocationId')); return this._controllerRouteDetails.assignDriversList(input); } }); this.stopList.configure({ title: this.l('Select Delivery Address'), dataSource: (skipCount: number, maxResultCount: number, filter: string, tenantId?: number, locationId?:number) => { // let input = any; // input.filter = filter; // input.maxResultCount = maxResultCount; // input.skipCount = skipCount; // input.tenantId = tenantId; // input.locationId = parseInt(localStorage.getItem('operatingLocationId')); return this._controllerRouteDetails.getAllStopsList(tenantId,parseInt(localStorage.getItem('operatingLocationId')),maxResultCount,skipCount,filter); } }); } private setIsEntityHistoryEnabled(): void { let customSettings = (abp as any).custom; this.entityHistoryEnabled = customSettings.EntityHistory && customSettings.EntityHistory.isEnabled && _.filter(customSettings.EntityHistory.enabledEntities, entityType => entityType === this._entityTypeFullName).length === 1; } showHistory(role: UpdateRouteInput): void { this.entityTypeHistoryModal.show({ entityId: role.id.toString(), entityTypeFullName: this._entityTypeFullName, entityTypeDescription: "Route - " +this.routeCode }); } ngAfterViewInit() { $('.kt-select2').select2(); $('.selectpicker').selectpicker(); this.getDetails(); } getDetails() { this._controllerRouteDetails.getControllerRouteDetail(this.routeId) .subscribe(result => { this.routeCode = result.routeTemplate.name; this.route.vehicleId = result.vehicleId; this.route.completion = result.completion; this.route.departure = result.departure; this.route.loadDate = result.loadDate; this.route.locked = result.locked; this.route.routeTemplateId = result.routeTemplateId; this.route.locationId = result.locationId; this.route.id = this.routeId; this.locationName = result.location.name; this.userId = result.vehicle==undefined ? 0 : result.vehicle.user==undefined ? 0 : result.vehicle.userId if (result.vehicle) { this.vehicleName = result.vehicle.user.name + " " + result.vehicle.user.surname; } this.data = result; this.loadDate = localStorage.getItem('loadDate'); this.labRouteDetails = result.routeTemplate.name; this.checkOptimizer(this.route.locationId); }); } checkOptimizer(locationId){ if(!this._isLab){ this._googleOptimization.getRuleByLocation(locationId,undefined,undefined) .subscribe(res=>{ if(res != null && res != undefined){ this.optimizer = true this.toll = res[0].tolls; this.highways = res[0].highways; this.ferries = res[0].ferries; this.indoor = res[0].indoor; } }); }else{ this.updateOnly = true; } } updateETAOnly(locationId, routeTemplateId, routeId){ this.message.confirm( 'Are you sure you want to update the ETA of this route?', '', (isConfirmed) => { if (isConfirmed) { this.spinnerService.show(); let input = new UpdateEtaInput(); input.locationId = locationId; input.routeId = routeId; input.routeTemplateId = routeTemplateId this.optimization.updateEta(input).subscribe(r=>{ let x = new OptimizeRouteInput({routeId : routeId}); this.optimization.reSortByEtaLab(x).subscribe(result=>{ this.refreshPage(); this.notify.info(this.l('SavedSuccessfully')); }); }); } } ); } refreshPage(){ if(this._isLab){ this.labRouteDetail.getLabPackages(); this.labRouteDetail.getRouteDetails(); } else if(this._isFbx) { this.fbxRouteDetail.getRouteDetails(); } else{ this.hfRouteDetail.getRouteDetails(); } } updateRoute() { this._controllerRoute.updateRoute(this.route) .pipe(finalize(() => this.saving = false)) .subscribe(() => { this.refreshPage(); this.notify.info(this.l('SavedSuccessfully')); }); } setDeparture() { if (!this.route.locked) { if (this.route.departure) { this.route.departure = null; } else { this.route.departure = moment.utc(moment(new Date()).format('YYYY-MM-DD h:m a')); } this.updateRoute(); } else { this.notify.info(this.l('Unlocked Loadlist First')); } } setCompletion() { if (!this.route.locked) { if (this.route.completion) { this.route.completion = null; } else { this.route.completion = moment.utc(moment(new Date()).format('YYYY-MM-DD h:m a')); } this.updateRoute(); } else { this.notify.info(this.l('Unlocked Loadlist First')); } } optimizeRoute(routeID){ this.spinnerService.show(); if(abp.features.isEnabled('App.GoogleDirectionApi.GoogleDirectionApi') && this.optimizer == true){ let input = new OptimizeByRouteIdInput(); input.routeId = routeID; input.ferry = this.ferries; input.highway = this.highways; input.indoor = this.indoor; input.toll = this.toll; input.departureTime = undefined; this._googleOptimization.optimizeByRouteId(input).subscribe((result) => { this.notify.info(this.l('Route has been re-optimized')); this.refreshPage(); }); }else{ if(this._isLab){ let x = new OptimizeRouteInput({routeId : routeID}); this.optimization.reSortByEtaLab(x).subscribe(result=>{ this.refreshPage(); this.notify.info(this.l('Route has been re-optimized')); }); }else{ let x = new OptimizeRouteInput({routeId : routeID}); this.optimization.reSortByEtaStandard(x).subscribe(result=>{ this.refreshPage(); this.notify.info(this.l('Route has been re-optimized')); }); } } } loadTable($event) { this.reload = $event; } goBack(){ this._location.back(); } onShown() { } removeStops() { this.message.confirm( '', '', (isConfirmed) => { if (isConfirmed) { if(this._isLab){ this.labRouteDetail.removeStop(); } if(this._isFbx){ this.fbxRouteDetail.removeStop(); } } } ); } printRouteSheet(){ if(localStorage.getItem('routeIds')==null){ this.localRouteIds = []; this.localRouteIds.push(this.routeId) localStorage.setItem('routeIds', JSON.stringify(this.localRouteIds)); }else{ localStorage.removeItem('routeIds'); this.localRouteIds = []; this.localRouteIds.push(this.routeId) localStorage.setItem('routeIds', JSON.stringify(this.localRouteIds)); } if(this.routeDetailTenant == 'LAB'){ window.open('/app/sprintship/route-sheet-print', '_blank'); }else{ window.open('/app/sprintship/route-print', '_blank'); } } assignDriverModal(record:any):void{ this.driversList.tenantId = record.id; this.driversList.show(); } updateRouteByVehicle(item:NameValueDto):void{ let input = new VehicleInput(); input.driverId = parseInt(item.value); input.routeId = this.routeId; this._controllerRouteDetails.updateRouteByVehicleId(input).subscribe(result=>{ this.getDetails(); }); } newIncident():void{ window.open('app/sprintship/create-incidents', '_blank'); } markAsDeliveredModal(record:any):void{ this.stopList.tenantId = record.id; this.stopList.show(); } updateRouteByStop(item:NameValueDto):void{ let input = new UpdateRouteByStopInput(); input.contactId = parseInt(item.value); input.routeId = this.routeId; input.loadDate = moment($('input[name=completionDate]').val()); this._controllerRouteDetails.updateRouteByStop(input).subscribe(result=>{ this.refreshPage(); }); } openMap(userId, routeId) { // window.open('/app/sprintship/locator/'+routeId+'/'+this.filters); this.router.navigate([]).then(result => { window.open('/app/sprintship/locator/' + userId + '/' + routeId, '_blank'); }); } }