import { Component, Injector, OnInit, ViewChild, ViewEncapsulation, ElementRef, AfterViewInit, Renderer, Input } 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, RouteAnalyzerOrderDto, RouteAnalyzerServiceProxy, NameValueDto,RouteCodeListInputFilter, UpdateRoutetemplateInput, UpdateScheduleInput, LocationServiceProxy, LocationNameDto } 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 * as _ from 'lodash'; import { result } from 'lodash'; import { DeliveryRouteAnalyzerFlyoutComponent } from '@app/shared/layout/flyout/delivery-route-analyzer-flyout/route-analyzer-flyout.component'; import { log } from 'console'; import { FileDownloadService } from '@shared/utils/file-download.service'; import { numberBoxEditor } from 'devexpress-dashboard/designer/form-adapter/_form-adapter-editors'; import { EntityTypeHistoryModalComponent } from '@app/shared/common/entityHistory/entity-type-history-modal.component'; import { CommonLookupModalComponent } from '@app/shared/common/lookup/common-lookup-modal.component'; @Component({ templateUrl: 'route-analyzer.component.html', encapsulation: ViewEncapsulation.None, animations: [appModuleAnimation()], styleUrls: ['route-analyzer.component.less'] }) export class RouteAnalyzerDeliveryComponent extends AppComponentBase implements OnInit { advancedFiltersAreShown = false; dateContainer: any; dateData: any = {}; loadDate: any; locations: LocationNameDto[] = []; locationString: string; codeString: string; filterText: string; clinicAccountFilter: string; clinicNameFilter: string; checkAll: boolean = false; clinicList: any = {}; etaList: Array = new Array(); locationName:string; locationId: number; routeCode: string; @ViewChild('deliveryRouteAnalyzerFlyoutComponent', { static: false }) deliveryRouteAnalyzerFlyoutComponent: DeliveryRouteAnalyzerFlyoutComponent; @ViewChild('dataTable', { static: true }) dataTable: Table; @ViewChild('paginator', { static: true }) paginator: Paginator; @ViewChild('entityTypeHistoryModal', { static: true }) entityTypeHistoryModal: EntityTypeHistoryModalComponent; @ViewChild('locationList', { static: true }) locationList: CommonLookupModalComponent; @ViewChild('routeList', { static: true }) routeList: CommonLookupModalComponent; _entityTypeFullName = 'SprintTek.Laboratory.RouteScheduleDetail'; entityHistoryEnabled = false; constructor( injector: Injector, private _routeAnalyzerService: RouteAnalyzerServiceProxy, private _locationServiceProxy: LocationServiceProxy, private _fileDownloadService: FileDownloadService, private router: Router ) { super(injector); } ngOnInit(): void { this.setIsEntityHistoryEnabled(); // this.primengTableHelper.isLoading = true; this.dateContainer = []; this.etaList = []; // this.showFlyOut(); // this.routeAnalyzerFlyoutComponent.showFlyout(); // this.generateDate(); // this.getLocations(); this.generateHeaders(); if (localStorage.getItem('deliveryAnalyzerStartDate') == null) { this.primengTableHelper.isLoading = false; } this.locationList.configure({ title: this.l('Assign Location'), type:'assignLocation', dataSource: (skipCount: number, maxResultCount: number, filter: string) => { return this._locationServiceProxy.getLocationNameList(undefined,filter,maxResultCount,skipCount); } }); this.routeList.configure({ title: this.l('Select Route'), type:'selectRouteCode', dataSource: (skipCount: number, maxResultCount: number, filter: string) => { var data = new RouteCodeListInputFilter(); this.locationId = parseInt(localStorage.getItem('deliveryAnalyzerLocationId')); data.locationId = this.locationId; data.filter = filter; data.maxResultCount = maxResultCount; data.skipCount = skipCount; return this._routeAnalyzerService.routeCodesList(data); } }); } assignLocationModal(): void { this.locationList.show(); } deleteLocation(): void { this.locationId = null; this.locationName = ''; localStorage.removeItem('deliveryAnalyzerLocationString'); localStorage.removeItem('deliveryAnalyzerLocationId'); $('#locationSelectInput').val(""); } selectLocation(item: NameValueDto): void { console.log(item) this.locationId = parseInt(item.value); this.locationName = item.name; localStorage.setItem('deliveryAnalyzerLocationString', item.name); localStorage.setItem('deliveryAnalyzerLocationId', item.value); $('#locationSelectInput').val(item.name); } selectRouteModal(): void { if(localStorage.getItem('deliveryAnalyzerLocationId') != null || localStorage.getItem('deliveryAnalyzerLocationId') != undefined){ this.routeList.show(); } else { this.message.info("Please select a location","Select Location"); } } deleteRoute(): void { this.routeCode = ''; localStorage.removeItem('deliveryAnalyzerRouteCode'); $('#routeCodeInput').val(""); } selectRoute(item: NameValueDto): void { localStorage.setItem('deliveryAnalyzerRouteCode', item.name); $('#routeCodeInput').val(item.name); } generateHeaders() { if (localStorage.getItem('deliveryAnalyzerLocationString') != null && localStorage.getItem('deliveryAnalyzerLocationString') != "") { this.locationString = localStorage.getItem('deliveryAnalyzerLocationString'); } else { this.locationString = "Select Location"; // this.primengTableHelper.isLoading = false; } if (localStorage.getItem('deliveryAnalyzerRouteCode') != null) { this.codeString = localStorage.getItem('deliveryAnalyzerRouteCode').toString() == 'None' ? 'ALL' : localStorage.getItem('deliveryAnalyzerRouteCode').toString(); } } // getLocations() { // this._locationServiceProxy.getLocationNames(undefined, undefined, undefined, undefined, undefined, undefined) // .subscribe(result => { // if (result == null) { // $("#locationSelectInput").val(''); // } // else { // this.locations = result; // } // }); // } generateDate() { this.dateContainer = []; if (localStorage.getItem('deliveryAnalyzerStartDate') !== null) { if (localStorage.getItem('deliveryAnalyzerDayFilter') == 'ALL') { for (let index = 0; index < 7; index++) { var end = moment(localStorage.getItem('deliveryAnalyzerStartDate'), "MM-DD-YYYY").add(index, 'days'); this.dateData.date = end.format("MM/DD/YYYY"); this.dateData.day = end.day(); this.dateContainer.push({ date: end.format("MM/DD/YYYY"), day: end.day() }); } } else if (localStorage.getItem('deliveryAnalyzerDayFilter') == 'WEEKEND') { this.weekEnd(localStorage.getItem('deliveryAnalyzerStartDate')); } else if (localStorage.getItem('deliveryAnalyzerDayFilter') == 'WEEKDAY') { this.weekDay(localStorage.getItem('deliveryAnalyzerStartDate')); } } } weekDay(date) { this.dateContainer = []; var ctr = 0; while (this.dateContainer.length < 7) { var end = moment(date, "MM-DD-YYYY").add(ctr, 'days'); if (end.day() != 0 && end.day() != 6) { this.dateData.date = end.format("MM/DD/YYYY"); this.dateData.day = end.day(); this.dateContainer.push({ date: end.format("MM/DD/YYYY"), day: end.day() }); } ctr++; } } weekEnd(date) { this.dateContainer = []; var ctr = 0; if (moment(date, "MM-DD-YYYY").day() == 0) { while (this.dateContainer.length < 7) { var end = moment(date, "MM-DD-YYYY").add(ctr, 'days'); if (end.day() == 0) { this.dateData.date = end.format("MM/DD/YYYY"); this.dateData.day = end.day(); this.dateContainer.push({ date: end.format("MM/DD/YYYY"), day: end.day() }); } ctr++; } } if (moment(date, "MM-DD-YYYY").day() == 6) { while (this.dateContainer.length < 7) { var end = moment(date, "MM-DD-YYYY").add(ctr, 'days'); if (end.day() == 6) { this.dateData.date = end.format("MM/DD/YYYY"); this.dateData.day = end.day(); this.dateContainer.push({ date: end.format("MM/DD/YYYY"), day: end.day() }); } ctr++; } } } findObjectByKey(array, key, value) { for (var i = 0; i < array.length; i++) { if (array[i][key] === value) { return array[i]; } } return null; } getStopsByLocation(event?: LazyLoadEvent) { this.generateDate(); this.generateHeaders(); if (localStorage.getItem('deliveryAnalyzerLocationString') != null && localStorage.getItem('deliveryAnalyzerLocationId') != null && localStorage.getItem('deliveryAnalyzerLocationId') != "") { this.primengTableHelper.isLoading = true; var codes = localStorage.getItem('deliveryAnalyzerRouteCode') == null ? undefined : localStorage.getItem('deliveryAnalyzerRouteCode'); if (codes == "None") { codes = undefined; } if(localStorage.getItem('deliveryAnalyzerRouteCode') == null){ this.codeString = "ALL"; } this.loadDate = moment.tz(localStorage.getItem('deliveryAnalyzerStartDate'), localStorage.getItem('timeZoneId')); this._routeAnalyzerService.getStopsByLocation( parseInt(localStorage.getItem('deliveryAnalyzerLocationId')), this.loadDate, codes, this.filterText, this.clinicNameFilter, this.clinicAccountFilter, localStorage.getItem('deliveryAnalyzerDayFilter'), this.primengTableHelper.getSorting(this.dataTable), this.primengTableHelper.getSkipCount(this.paginator, event), this.primengTableHelper.getMaxResultCount(this.paginator, event)).subscribe(result => { this.primengTableHelper.records = result; this.primengTableHelper.totalRecordsCount = result.length; this.primengTableHelper.isLoading = false; }); } else { this.primengTableHelper.isLoading = false; this.codeString = ""; this.locationString = "Select Location"; this.primengTableHelper.records = null; this.primengTableHelper.totalRecordsCount = 0; } } updateStopsByETA(id: number, eta: any) { let dataArray = []; let data = new UpdateRoutetemplateInput(); data.routeTemplateId = id; data.endTime = eta; dataArray.push(data); this._routeAnalyzerService.updateRouteTemplate(dataArray).subscribe(result => { this.notify.info(this.l('Successfully Updated')); }); } showFlyOut(): void { this.deliveryRouteAnalyzerFlyoutComponent.showFlyout(this.locations); } filterRouteAnalyzer() { this.deliveryRouteAnalyzerFlyoutComponent.getStopsByLocation(); } selectAllClinics() { for (let index = 0; index < this.primengTableHelper.records.length; index++) { if (this.checkAll == true) { if (this.primengTableHelper.records[index].timeSDString != null) { this.primengTableHelper.records[index].selected = true; } } else { this.primengTableHelper.records[index].selected = false; } } } batchUpdate() { let dataArray = []; if (this.primengTableHelper.records != null) { for (let index = 0; index < this.primengTableHelper.records.length; index++) { if (this.primengTableHelper.records[index].selected == true) { dataArray.push({ routeTemplateId: this.primengTableHelper.records[index].routeTemplateId, endTime: this.primengTableHelper.records[index].timeAverageWithExclusion }); } } } if (dataArray.length > 0) { this._routeAnalyzerService.updateRouteTemplate(dataArray).subscribe(result => { this.notify.info(this.l('Successfully Updated')); }); } else { this.notify.info(this.l('Please Select Clinic to Update')); } } 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: RouteAnalyzerOrderDto): void { this.entityTypeHistoryModal.show({ entityId: role.routeScheduleDetailId.toString(), entityTypeFullName: this._entityTypeFullName, entityTypeDescription: "Route Analyzer" }); } exportToExcel() { if (localStorage.getItem('deliveryAnalyzerLocationString') != null && localStorage.getItem('deliveryAnalyzerLocationId') != null && localStorage.getItem('deliveryAnalyzerLocationId') != "") { this.primengTableHelper.isLoading = true; var codes = localStorage.getItem('deliveryAnalyzerRouteCode') == null ? undefined : localStorage.getItem('deliveryAnalyzerRouteCode'); this.loadDate = moment.tz(localStorage.getItem('deliveryAnalyzerStartDate'), localStorage.getItem('timeZoneId')); this._routeAnalyzerService.getAnalyedDataToExcel(parseInt(localStorage.getItem('deliveryAnalyzerLocationId')), this.loadDate, codes, localStorage.getItem('deliveryAnalyzerDayFilter')).subscribe(result => { this.primengTableHelper.isLoading = false; if(result!=undefined){ if(result.fileUrl!=undefined){ this.router.navigate([]).then(result1 => { window.open(result.fileUrl, '_blank'); }); this.spinnerService.hide(); }else{ this.spinnerService.hide(); } }else{ this.message.warn('', "No data to export"); this.spinnerService.hide(); } }); } } }