import { Component, EventEmitter, Injector, ViewChild, Output, Input } from '@angular/core'; import { AppComponentBase } from '@shared/common/app-component-base'; import { ModalDirective } from 'ngx-bootstrap'; import { ControllerRouteDetailServiceProxy, OrderServiceProxy, ControllerMoveOrderInput, ListResultDtoOfControllerGetAllAvailabelRouteDto, TimingServiceProxy, LocationServiceProxy, NameValueDto, PagedResultDtoOfNameValueDto, GetLocationNamesInputPage, GetControllerRouteLoadDateInputPage, ControllerRouteServiceProxy, GenerateInputDto, DateTimeServiceProxy, DriverListInput, OrderStatusListName } from '@shared/service-proxies/service-proxies'; import { PrimengTableHelper } from 'shared/helpers/PrimengTableHelper'; import { HFRouteDetailComponent } from './hf-route-detail.component'; import { finalize } from 'rxjs/operators'; import { Paginator } from 'primeng/components/paginator/paginator'; import { Table } from "primeng/table"; import * as moment from 'moment-timezone'; import { ActivatedRoute } from '@angular/router'; import { CommonLookupModalComponent } from '@app/shared/common/lookup/common-lookup-modal.component'; import { interval } from 'rxjs'; import { ProgressBarService } from '@app/shared/layout/progressbar/progressbar.service'; import { GenerateRoutesTrackingComponent } from '../routes/generate-routes-tracking.component'; import { THIS_EXPR } from '@angular/compiler/src/output/output_ast'; declare var $: any; // import * as DualListbox from 'dual-listbox'; @Component({ templateUrl: './move-order-modal.component.html', selector: 'MovePendingOrder', providers: [DateTimeServiceProxy] }) export class MovePendingOrder extends AppComponentBase { @ViewChild('movePendingOrderModal', { static: false }) modal: ModalDirective; @Output() reloadTable: EventEmitter = new EventEmitter(); @Output() reloadTablePackages: EventEmitter = new EventEmitter(); @Input() builderOrderIds: Array = new Array(); @Input('selectedRouteId') selectedRouteId: number; @ViewChild("paginator", { static: true }) paginator: Paginator; @ViewChild("dataTable", { static: true }) dataTable: Table; @ViewChild('locationList', { static: true }) locationList: CommonLookupModalComponent; @ViewChild('routesList', { static: true }) routesList: CommonLookupModalComponent; @ViewChild('optimizationList', { static: true }) optimizationList: CommonLookupModalComponent; @ViewChild('statusList', { static: true }) statusList: CommonLookupModalComponent; @ViewChild('driverList', { static: true }) driverList: CommonLookupModalComponent; @ViewChild('generateRoutesTracking', { static: false }) generateRoutesTracking: GenerateRoutesTrackingComponent; @Input() refreshRouteMove: Function @Input() refreshRouteBatchMove: Function // @Input() updateTable:Function locationFilter: PagedResultDtoOfNameValueDto = new PagedResultDtoOfNameValueDto(); toAddLocation: NameValueDto; reload: boolean = false; active = false; saving = false; loadDate: Date; location:any; timeId: any; orderId:any; orderIds: Array = new Array; routeId: any; routeDate: any routeList: ListResultDtoOfControllerGetAllAvailabelRouteDto = new ListResultDtoOfControllerGetAllAvailabelRouteDto(); moveInput: ControllerMoveOrderInput = new ControllerMoveOrderInput(); _isLab: any; _ukLocale: boolean; sample: any movefilteredLocation: any; selectedLocation: any; locationId: any; todaysDate: string; loadDateString: string; locationName: string; routeName: string; newRouteId: number; locationData: any; updateRoute: boolean = false; selectTz: string; routeOptimizationType: string routeOptimizationTypeCode: string routeIds: any enableDriver:boolean = false; driverName:string = ""; selectedDriverId:number; statusName:string = ""; selectedStatusId:number; constructor( injector: Injector, private _controllerRouteDetails: ControllerRouteDetailServiceProxy, private _controllerOrderService: OrderServiceProxy, private _timingService: TimingServiceProxy, private id: ActivatedRoute, private _locationAppService: LocationServiceProxy, private _controllerRouteService: ControllerRouteServiceProxy, private _progressBar: ProgressBarService, private _locationServiceProxy: LocationServiceProxy, private _dateTimeServiceProxy: DateTimeServiceProxy, private _orderServiceProxy: OrderServiceProxy ) { super(injector); this.locationId = localStorage.getItem('operatingLocationId') == null ? 0 :parseInt(localStorage.getItem('operatingLocationId')); this._isLab = abp.features.isEnabled('App.RouteDetails'); this.todaysDate = moment(Date.now()).format('MM/DD/YYYY'); if (localStorage.getItem('moveOrderLoadDate') == null) { this.todaysDate = moment(Date.now()).format('MM/DD/YYYY'); localStorage.setItem('moveOrderLoadDate', this.todaysDate); } } ngOnInit() { this._ukLocale = abp.features.isEnabled('App.DateFormatFeature.UKLocale'); this.timeId = localStorage.getItem('timeZoneId'); // moment.tz.setDefault(this.timeId); this.id.paramMap.subscribe(params => { this.routeId = Number(params.get('id')); }); this.filterLocation(); $('.kt-select2').select2(); let that = this; that.locationId = localStorage.getItem('operatingLocationId') == null ? 0 :parseInt(localStorage.getItem('operatingLocationId')); if (localStorage.getItem('moveOrderLoadDate') == null) { $(".RouteDate").val(moment(this.todaysDate).format('MM/DD/YYYY')); this.loadDate = moment(Date.now()).toDate(); }else { if(localStorage.getItem('moveOrderLoadDate').search('-') > 0) { var str = localStorage.getItem('moveOrderLoadDate').split('-'); var str1 = str[1] + '/' + str[2] + '/' + str[0]; localStorage.setItem('moveOrderLoadDate',str1); console.log("elseifinit") } $(".RouteDate").val(localStorage.getItem('moveOrderLoadDate')); this.loadDate = moment.tz(localStorage.getItem('moveOrderLoadDate'), this.timeId).toDate() } this.locationList.configure({ title: this.l('Select Location'), dataSource: (skipCount: number, maxResultCount: number, filter: string, tenantId?: number, locationId?: number) => { let input = new GetLocationNamesInputPage(); input.filter = filter; input.maxResultCount = maxResultCount; input.skipCount = skipCount; return this._locationAppService.assignLocationList(input); } }); this.routesList.configure({ title: this.l('Select Route'), dataSource: (skipCount: number, maxResultCount: number, filter: string, tenantId?: number, locationId?: number) => { let input = new GetControllerRouteLoadDateInputPage(); input.filter = filter; input.maxResultCount = maxResultCount; input.skipCount = skipCount; input.routeId = this.routeId; input.loadDateStart = moment.tz(localStorage.getItem('moveOrderLoadDate'), that.timeId); input.loadDateEnd = moment.tz(localStorage.getItem('moveOrderLoadDate'), that.timeId).endOf('day'); input.operatingLocationId = this.locationId; return this._controllerRouteDetails.assignAvailableRoute(input); } }); if(!this._isLab){ this.routeName = "To Be Generated" } this.optimizationList.configure({ title: this.l('SelectRouteOptimization'), dataSource: (skipCount: number, maxResultCount: number, filter: string, tenantId?: number, locationId?: number) => { return this._orderServiceProxy.getRouteOptimizationType(); } }); this.driverList.configure({ title: this.l('AssignDriver'), type:'assignDriver', 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 = this.locationId; return this._controllerRouteDetails.assignDriversList(input); } }); this.statusList.configure({ title: this.l('Select Status'), type:'Select Status', dataSource: (skipCount: number, maxResultCount: number, filter: string, tenantId?: number, locationId?: number) => { let input = new OrderStatusListName(); input.filter = filter; input.maxResultCount = maxResultCount; input.skipCount = skipCount; return this._orderServiceProxy.orderStatusListName(input); } }); // this.loadDateString = } filterLocation(): void { this._locationAppService.getLocationNames( undefined, undefined, undefined, undefined, undefined, undefined ).subscribe(result => { this.movefilteredLocation = result; }); } show(orderId?: number, routeId?: number, locationId? :any) { let that = this; if(orderId != null){ that.orderIds = []; that.orderIds.push(orderId); } if(this.builderOrderIds.length > 0){ that.orderIds = this.builderOrderIds; } if(this.orderIds.length == 0){ this.message.error('No orders selected'); } else{ that.locationId = localStorage.getItem('operatingLocationId') == null ? 0 : parseInt(localStorage.getItem('operatingLocationId')); this.location = localStorage.getItem('operatingLocationId') == null ? $("#selectedLocation").val() : parseInt(localStorage.getItem('operatingLocationId')); if (localStorage.getItem('moveOrderLoadDate') == null) { this.todaysDate = moment(Date.now()).format('MM/DD/YYYY'); localStorage.setItem('moveOrderLoadDate', this.todaysDate); } that.orderId = orderId == null ? undefined : orderId; that.active = true; that.modal.show(); let input = new GetLocationNamesInputPage(); input.filter = ""; input.maxResultCount = 10; input.skipCount = 0; input.id = that.locationId; this._dateTimeServiceProxy.getLocationTimeZone(that.locationId) .pipe() .subscribe(result => { this.selectTz = result.location }); this._locationAppService.assignLocationList(input).subscribe(result => { if(result.totalCount!=0){ this.locationName = result.items[0].name; // this.locationId = parseInt(this.locationData[0].value); } }); } } reloadList(){ var lang = abp.localization.currentLanguage.name; if(lang=="en-GB"){ var str = $('.RouteDate').val().toString().split('/'); var str1 = str[1] + '/' + str[0] + '/' + str[2]; localStorage.setItem('moveOrderLoadDate', str1); }else{ var str = $('.RouteDate').val().toString().split('/'); var str1 = str[0] + '/' + str[1] + '/' + str[2]; localStorage.setItem('moveOrderLoadDate', str1); } this.newRouteId = undefined; if(!this._isLab){ this.routeName = "To Be Generated" } else{ this.routeName = ""; } } onShown(): void { this.routeOptimizationTypeCode = '' this.routeOptimizationType = '' let that = this; if(this._isLab){ if (localStorage.getItem('moveOrderLoadDate') == null) { $(".RouteDate").val(moment(that.todaysDate).format('MM/DD/YYYY')); that.loadDate = moment(Date.now()).toDate(); }else { if(localStorage.getItem('moveOrderLoadDate').search('-') > 0) { var str = localStorage.getItem('moveOrderLoadDate').split('-'); var str1 = str[1] + '/' + str[2] + '/' + str[0]; localStorage.setItem('moveOrderLoadDate',str1); } $(".RouteDate").val(localStorage.getItem('moveOrderLoadDate')); that.loadDate = moment.tz(localStorage.getItem('moveOrderLoadDate'), that.timeId).toDate() } } else{ //for todays date upon show if (localStorage.getItem('moveOrderLoadDate') == null) { $(".RouteDate").val(moment(that.todaysDate).format('MM/DD/YYYY')); that.loadDate = moment(Date.now()).toDate(); }else { if(localStorage.getItem('moveOrderLoadDate').search('-') > 0) { var str = localStorage.getItem('moveOrderLoadDate').split('-'); var str1 = str[1] + '/' + str[2] + '/' + str[0]; localStorage.setItem('moveOrderLoadDate',str1); } // $(".RouteDate").val(localStorage.getItem('moveOrderLoadDate')); that.loadDate = moment(Date.now()).toDate(); localStorage.setItem('moveOrderLoadDate', this.todaysDate); // that.loadDate = moment.tz(localStorage.getItem('moveOrderLoadDate'), that.timeId).toDate() } } $('.kt-select2').select2(); $("#selectedLocation").on('change',function(e){ // that.reloadList(); }); this.locationId = localStorage.getItem('operatingLocationId') == null ? 0 : parseInt(localStorage.getItem('operatingLocationId')); this.location = localStorage.getItem('operatingLocationId') == null ? $("#selectedLocation").val() : parseInt(localStorage.getItem('operatingLocationId')); this.newRouteId = undefined if(!this._isLab){ this.routeName = "To Be Generated" } // if(this.orderIds === undefined || this.orderIds.length == 0) { // this.message.error('No orders selected'); // } } acceptOrderId($event) { this.orderIds = $event; } applyMove() { if (this.orderIds != null) { this.moveOrders(this.newRouteId, this.orderIds, this.routeId, this.updateRoute); this.routeName = ""; this.routeId = null; this.close(); } else { this.routeName = ""; this.routeId = null; this.notify.info(this.l('Select orders to move')); } } moveOrders(routeId: number, ids: any, originRouteId: number , updateRoute: boolean) { this.spinnerService.show(); this.moveInput.selectedRouteId = routeId; this.moveInput.originRouteId = originRouteId==0 || originRouteId==undefined ? undefined : originRouteId; this.moveInput.id = ids; this.moveInput.routeOptimizationType = this.routeOptimizationTypeCode this.moveInput.date = moment.tz(localStorage.getItem('moveOrderLoadDate'), this.selectTz) this.moveInput.locationId = this.locationId; this.moveInput.userId = this._isLab ? undefined : this.selectedDriverId; this.moveInput.statusId = this.selectedStatusId; this._dateTimeServiceProxy.getLocationTimeZone(this.locationId) .pipe() .subscribe(result => { this.selectTz = result.location }); this._controllerOrderService.moveOrder(this.moveInput) .pipe(finalize(() => this.saving = false)) .subscribe(() => { this.moveInput.id.forEach(element => { this.reloadTablePackages.emit(null); }); this.reloadTable.emit(null); this.notify.info(this.l('SavedSuccessfully')); if(!this._isLab){ if(this.routeOptimizationTypeCode === 'SOURCE' || this.routeOptimizationTypeCode === 'BOTH' || this.routeOptimizationTypeCode === 'TARGET' || routeId == undefined){ this.generateRoute() } } this.spinnerService.hide(); }, error =>{ this.spinnerService.hide(); }); } onChange(deviceValue) { } close(): void { this.active = false; this.routeName = ""; this.routeId = null; this.driverName = ""; this.statusName = ""; this.selectedStatusId = null; // this.orderIds=[] this.modal.hide(); } assignLocation(): void { this.locationList.show(); } selectLocation(item: NameValueDto): void { this.locationId = parseInt(item.value); this.locationName = item.name; this._dateTimeServiceProxy.getLocationTimeZone(parseInt(item.value)) .pipe() .subscribe(result => { this.selectTz = result.location }); } assignRoute(): void { this.routesList.show(); } assignDriver(): void { this.driverList.show(); } assignStatus(): void { this.statusList.show(); } selectRoute(item: NameValueDto): void { this.newRouteId = parseInt(item.value); this.routeName = item.name; if(!this._isLab){ this._controllerRouteDetails.getRouteInfo(this.newRouteId).subscribe(res => { this.enableDriver = true; this.driverName = res.driver; this.selectedDriverId = res.userId; }); } } selectDriver(item: NameValueDto): void { this.selectedDriverId = parseInt(item.value); this.driverName = item.name; } selectStatus(item: NameValueDto): void { this.selectedStatusId = parseInt(item.value); this.statusName = item.name; } assignOptimization(){ this.optimizationList.show(); } selectOptimizationType(item: NameValueDto): void { this.routeOptimizationTypeCode = item.value; this.routeOptimizationType = item.name; this.spinnerService.show console.log(this.orderIds) if(item.value === 'SOURCE' || item.value === 'BOTH'){ this._orderServiceProxy.getRouteIdOfOrder(this.orderIds) .pipe() .subscribe(result => { this.routeIds = result console.log(result) this.spinnerService.hide() }); } } clearRouteName(){ this.routeName = "To Be Generated" this.newRouteId = undefined } clearDriverName(){ this.driverName = ""; this.selectedDriverId = undefined; } clearStatusName(){ this.statusName = ""; this.selectedStatusId = undefined; } clearOptimization(){ this.routeOptimizationType = "" this.routeOptimizationTypeCode = '' } toggle(toggle: boolean) { this._progressBar.toggleProgressBar(toggle); } public async generateRoute() { console.log(this.selectedDriverId) let timeId = localStorage.getItem('timeZoneId'); let params = new GenerateInputDto({ locationId: this.locationId, loadDateEnd: moment.tz(localStorage.getItem('moveOrderLoadDate'), this.selectTz), loadDateStart: moment.tz(localStorage.getItem('moveOrderLoadDate'), this.selectTz), limit: 100, action: "RUN", jobId: "", timezone: "", isAuto: false, runEstimatedStartTime: true, sourceRouteIds: this.routeOptimizationTypeCode === 'SOURCE' || this.routeOptimizationTypeCode === 'BOTH' ? this.routeIds : undefined, targetRouteId: this.routeOptimizationTypeCode === 'TARGET' || this.routeOptimizationTypeCode === 'BOTH'? this.newRouteId : undefined, orderIds: undefined, driverUserId: this.selectedDriverId, visibleToDriver: true, tenantId: null, isDownloadOrder: false, driverAndOrderMapping: null }); this._locationServiceProxy.isHolidayOnLocation(params.loadDateEnd, params.locationId, undefined, undefined) .subscribe(result => { if (result) { this.message.info('Today is a holiday. Route and orders are not allowed to be generated') } else { this.genRoutes(params); this.selectedDriverId = undefined; } }); } public async genRoutes(params: GenerateInputDto) { let runResult = await this._controllerRouteService.generateRouteManual(params).toPromise(); if (runResult.success) { this.toggle(true); this.notify.info('Route generation is queued. Please wait'); if (runResult.jobId) { params.jobId = runResult.jobId; params.action = "TRACK"; let observable = interval(1500).subscribe((val) => { this._controllerRouteService.generateRouteManual(params).subscribe(watcher => { if (watcher.result == "DONE") { this.toggle(false); this.notify.success('Route generation is complete'); this.generateRoutesTracking.complete(); observable.unsubscribe(); window.location.reload(); } }); }); } } } // getOrderStatusList(){ // this. // } }