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, PagedResultDtoOfGetOrganizationUnitOrderNotificationForViewDto, NameValueDto, PagedResultDtoOfNameValueDto, GetControllerRouteLoadDateInputPage, GetLocationNamesInputPage, LocationServiceProxy, GenerateInputDto, ControllerRouteServiceProxy, DateTimeServiceProxy } from '@shared/service-proxies/service-proxies'; import { PrimengTableHelper } from 'shared/helpers/PrimengTableHelper'; import { finalize } from 'rxjs/operators'; import { Paginator } from 'primeng/components/paginator/paginator'; import { Table } from "primeng/table"; import * as jquery from 'jquery'; import * as moment from 'moment-timezone'; import { ActivatedRoute, Router } 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 '../controller/routes/generate-routes-tracking.component'; // import * as DualListbox from 'dual-listbox'; @Component({ templateUrl: './move-order-routebuilder.component.html', selector: 'MovePendingOrderRouteBuilder', providers: [DateTimeServiceProxy] }) export class MovePendingOrderRouteBuilder extends AppComponentBase { @ViewChild('movePendingOrderModal', { static: false }) modal: ModalDirective; @Output() refresh: EventEmitter = new EventEmitter(); @Output() reloadTablePackages: EventEmitter = new EventEmitter(); @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('generateRoutesTracking', { static: false }) generateRoutesTracking: GenerateRoutesTrackingComponent; @Input() refreshRouteMove: Function @Input() refreshRouteBatchMove: Function @Input() locationId: number; @Input() orderList: Array; @Input() routeDate: string; @Input() routeId: number; // @Input() updateTable:Function locationFilter: PagedResultDtoOfNameValueDto = new PagedResultDtoOfNameValueDto(); toAddLocation: NameValueDto; reload: boolean = false; active = false; saving = false; loadDate: any; location:any; timeId: any; orderId:any; orderIds: Array = new Array; routeList: ListResultDtoOfControllerGetAllAvailabelRouteDto = new ListResultDtoOfControllerGetAllAvailabelRouteDto(); moveInput: ControllerMoveOrderInput = new ControllerMoveOrderInput(); _isLab: any; _ukLocale: boolean; todaysDate: string; sample: any movefilteredLocation: any; selectedLocation: any; loadDateString: string; locationName: string; routeName: string; newRouteId: number; routeOptimizationType: string routeOptimizationTypeCode: string routeIds: any selectTz: string; constructor( injector: Injector, private _controllerRouteDetails: ControllerRouteDetailServiceProxy, private _controllerOrderService: OrderServiceProxy, private router: Router, private _timingService: TimingServiceProxy, private id: ActivatedRoute, private _locationAppService: LocationServiceProxy, private _progressBar: ProgressBarService, private _orderServiceProxy: OrderServiceProxy, private _locationServiceProxy: LocationServiceProxy, private _controllerRouteService: ControllerRouteServiceProxy, private _dateTimeServiceProxy: DateTimeServiceProxy ) { super(injector); 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('routeid')); }); 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); } $(".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; // 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); // } // $(".RouteDate").val(localStorage.getItem('moveOrderLoadDate')); // this.loadDate = moment.tz(localStorage.getItem('moveOrderLoadDate'), this.timeId).toDate(); // } input.loadDateStart = moment.tz(localStorage.getItem('moveOrderLoadDate'), this.timeId); input.loadDateEnd = moment.tz(localStorage.getItem('moveOrderLoadDate'), this.timeId).endOf('day'); input.operatingLocationId = this.locationId; return this._controllerRouteDetails.assignAvailableRoute(input); } }); this.optimizationList.configure({ title: this.l('SelectRouteOptimization'), dataSource: (skipCount: number, maxResultCount: number, filter: string, tenantId?: number, locationId?: number) => { return this._orderServiceProxy.getRouteOptimizationType(); } }); } show(builderList?: any, routeId?: number, locationId? :any) { this.orderIds = [] let e = this; let dateString = "" this.orderId = undefined; var lang = abp.localization.currentLanguage.name; var str1 = ""; this.location = (locationId == null ? undefined : parseInt(localStorage.getItem('operatingLocationId'))); if (localStorage.getItem('moveOrderLoadDate') == null) { this.todaysDate = moment(Date.now()).format('MM/DD/YYYY'); localStorage.setItem('moveOrderLoadDate', this.todaysDate); } // this.loadDate = moment.tz(localStorage.getItem('loadDate'), this.timeId).toDate(); this._controllerRouteDetails.getAvailableRoute(moment.tz(localStorage.getItem('moveOrderLoadDate'), this.timeId), moment.tz(localStorage.getItem('moveOrderLoadDate'), this.timeId).endOf('day'), localStorage.getItem('timeZone'), this.location, this.routeId, undefined) .subscribe(result => { this.routeList = result; }); this._dateTimeServiceProxy.getLocationTimeZone(e.locationId) .pipe() .subscribe(result => { this.selectTz = result.location }); this.active = true; this.modal.show(); if (builderList != null) { builderList.forEach(x => { this.orderIds.push(x.orderId); }); } this.routeOptimizationTypeCode='' } reloadList(){ // if(this._ukLocale) // { // this.loadDate = new Date(this.loadDate).toLocaleDateString('en-GB'); // } // else{ // this.loadDate = new Date(this.loadDate).toLocaleDateString('en-US'); // } // this.loadDate = new Date(new Date(Date.now()).toLocaleDateString(moment.locale())); var lang = abp.localization.currentLanguage.name; var str1 = ""; 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 = "" } // this.loadDate = moment(this.loadDate).toDate() // this._controllerRouteDetails.getAvailableRoute(moment.tz(localStorage.getItem('moveOrderLoadDate'), this.timeId), // moment.tz(localStorage.getItem('moveOrderLoadDate'), this.timeId).endOf('day'), // localStorage.getItem('timeZone'), this.location, this.routeId,undefined) // .subscribe(result => { // this.routeList = result; // }); } onShown(): void { this.routeOptimizationTypeCode = '' this.routeOptimizationType = '' let e = this; 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(); if(!this._isLab){ this.routeName = "To Be Generated" } this.newRouteId=undefined } acceptOrderId($event) { this.orderIds = $event; } applyMove() { if (this.orderIds != null) { // this.moveOrders(Number((document.getElementById('selectedRouteId')).value), this.orderIds, this.routeId); this.moveOrders(this.newRouteId, this.orderIds, this.routeId); this.notify.info(this.l('SavedSuccessfully')); this.close(); } else { this.notify.info(this.l('Select orders to move')); } } applyMoveGo(){ if (this.orderIds != null) { this.moveOrders(this.newRouteId, this.orderIds, this.routeId); this.notify.info(this.l('SavedSuccessfully')); this.routeName = ""; this.close(); this.router.navigateByUrl("app/sprintship/route-builder/"+this.newRouteId); } else { this.newRouteId = undefined; this.routeName = ""; this.notify.info(this.l('Select orders to move')); } } moveOrders(routeId: number, ids: any, originRouteId: number) { this.spinnerService.show(); this.moveInput.selectedRouteId = routeId; this.moveInput.originRouteId = originRouteId; this.moveInput.id = ids; this.moveInput.locationId = this.locationId; this._dateTimeServiceProxy.getLocationTimeZone(this.locationId) .pipe() .subscribe(result => { this.selectTz = result.location }); // if(!this._isLab){ // if(this.routeOptimizationTypeCode === 'SOURCE'){ // console.log("this is source") // console.log(this.routeIds) // } // if(this.routeOptimizationTypeCode === 'BOTH'){ // console.log("this is both") // console.log(this.routeIds) // console.log(this.newRouteId) // } // if(this.routeOptimizationTypeCode === 'TARGET'){ // console.log("this is target") // console.log(this.newRouteId) // } // } this._controllerOrderService.moveOrder(this.moveInput) .pipe(finalize(() => this.saving = false)) .subscribe(() => { // this.newRouteId = undefined; // this.routeName = ""; this.refresh.emit(); if(!this._isLab){ if(this.routeOptimizationTypeCode === 'SOURCE' || this.routeOptimizationTypeCode === 'BOTH' || this.routeOptimizationTypeCode === 'TARGET' || routeId == undefined){ this.generateRoute() } } }); } close(): void { this.active = false; 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(); } selectRoute(item: NameValueDto): void { this.newRouteId = parseInt(item.value); this.routeName = item.name; // this.applyMove(); } clearOptimization(){ this.routeOptimizationType = "" this.routeOptimizationTypeCode = '' } assignOptimization(){ this.optimizationList.show(); } selectOptimizationType(item: NameValueDto): void { this.routeOptimizationTypeCode = item.value; this.routeOptimizationType = item.name; this.spinnerService.show if(item.value === 'SOURCE' || item.value === 'BOTH'){ this._orderServiceProxy.getRouteIdOfOrder(this.orderIds) .pipe() .subscribe(result => { this.routeIds = result this.spinnerService.hide() }); } } clearRouteName(){ this.routeName = "To Be Generated" this.newRouteId = undefined } public async generateRoute() { // if(!this._isLab){ // if(this.routeOptimizationTypeCode === 'SOURCE'){ // console.log("this is source") // console.log(this.routeIds) // } // if(this.routeOptimizationTypeCode === 'BOTH'){ // console.log("this is both") // console.log(this.routeIds) // console.log(this.newRouteId) // } // if(this.routeOptimizationTypeCode === 'TARGET'){ // console.log("this is target") // console.log(this.newRouteId) // } // } 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: undefined, visibleToDriver: null, 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); } }); } 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(); } }); }); } } } toggle(toggle: boolean) { this._progressBar.toggleProgressBar(toggle); } }