import { Component, Injector, OnInit, ViewChild, ViewEncapsulation, EventEmitter, Output, Input } from '@angular/core'; import { ActivatedRoute, Router } from '@angular/router'; import { appModuleAnimation } from '@shared/animations/routerTransition'; import { AppComponentBase } from '@shared/common/app-component-base'; import { ControllerRouteDetailServiceProxy, ShippingPackageServiceProxy, ControllerScheduleDetailDto, GetCompaniesListItemDto, OrderPackageListDto, OrderStatusAttachmentServiceProxy, UpdateOrderSorting, UpdateOrderSortingInput, UpdateRouteTemplateAddressSorting, RouteTemplateAddressServiceProxy, RemoveOrderInput, RemoveOrderList, OrderServiceProxy, LockedOrderInput, ControllerPrintRouteSheetServiceProxy, UpdateRouteInput, ControllerRouteServiceProxy, ControllerRouteSheetDto, RouteSheetList, GetCountryListOnlyDto } from '@shared/service-proxies/service-proxies'; import { AppSessionService } from '@shared/common/session/app-session.service'; import { Table } from "primeng/table"; import { LazyLoadEvent } from "primeng/primeng"; import { Paginator } from 'primeng/components/paginator/paginator'; import { finalize, repeat } from 'rxjs/operators'; import { Location } from '@angular/common'; import * as _ from 'lodash'; declare var $: any; declare var jquery: any; import * as moment from 'moment-timezone'; import { AppConsts } from '@shared/AppConsts'; import jsPDF from 'jspdf'; import 'jspdf-autotable' import { sample } from 'lodash'; @Component({ selector: 'route-sheet-laboratory', templateUrl: 'route-sheet-laboratory.component.html', styleUrls: ['./route-sheet-laboratory.css'], encapsulation: ViewEncapsulation.None, animations: [appModuleAnimation()], providers: [ControllerPrintRouteSheetServiceProxy] }) export class RouteSheetLaboratoryComponent extends AppComponentBase implements OnInit { routeId: number = null; packages: Array<{ id: number, name: string }> = new Array(); packageCount: Array<{ id: number, quantity: number }> = new Array(); filterText = ''; accountNoFilter: any; clinicFilter: any; advancedFiltersAreShown = false; now: Date; arrayNotes: Array = new Array(); dNote: string; resultCount: number; saving = false; newRemoveOrderInput: Array = []; sprintIds: Array<{}> = new Array(); sprintIdsList: any[]; checkAll: boolean; records: any; loadDate: Date; driverName: string = ""; routeCode: string = ""; route: UpdateRouteInput = new UpdateRouteInput(); vehicleName: string; locationName: string; routeSheet: RouteSheetList[] = []; // data: Array []; @ViewChild("dataTable", { static: true }) dataTable: Table @ViewChild("paginator", { static: true }) paginator: Paginator newDto: ControllerScheduleDetailDto = new ControllerScheduleDetailDto(); newPackage: OrderPackageListDto = new OrderPackageListDto(); lockedEtaInput: LockedOrderInput = new LockedOrderInput(); packagesList: Array = new Array(); totalCount: any; flag = false; tbodyHolder: any; orderList: Array = []; routeTemplateAddressList: Array = []; timeId: any; localRouteIds = []; retrieving: boolean = false; public getRouteDetailFunction: Function; noData: boolean = false; head = [['', 'Account', 'Customer', 'Phone', 'Addresses']]; data = []; dataRow = []; dataAutos = []; dataWillCall = []; loadDateString:string; constructor( injector: Injector, private id: ActivatedRoute, private _controllerPrintRouteDetails: ControllerPrintRouteSheetServiceProxy, private _controllerRouteDetails: ControllerRouteDetailServiceProxy, private _controllerRoutePackages: ShippingPackageServiceProxy, private _location: Location, private router: Router ) { super(injector); this.timeId = localStorage.getItem('timeZoneId'); moment.tz.setDefault(this.timeId); this.routeSheet.length = 1; this.localRouteIds = JSON.parse(localStorage.getItem('routeIds')); if (this.localRouteIds.length == 0) { window.close(); } } ngOnInit() { this.now = new Date(); function printContent() { let popupWin = window.open('', '_blank', 'width=1080,height=595'); let printContents = document.getElementById("container-print").innerHTML; popupWin.document .write(` ${$('head').clone().html()}`); popupWin.document .write(`${printContents}`); $('head').append(''); popupWin.document .write(``); popupWin.document.close(); } $('#btn').click(function (e) { printContent(); }); this.getLabPackages(); this.getRouteDetails(); } printContent() { let popupWin = window.open('', '_blank', 'width=1080,height=595'); let printContents = document.getElementById("container-print").innerHTML; popupWin.document .write(` ${$('head').clone().html()}`); popupWin.document .write(`${printContents}`); $('head').append(''); popupWin.document .write(``); popupWin.document.close(); } ngAfterViewInit() { } getLabPackages() { this.packages = []; this._controllerRoutePackages.getShippingPackageListOptions(undefined, undefined, undefined, undefined, undefined, undefined).subscribe(result => { result.forEach(element => { if (element.name !== 'Default') { this.packages.push(element); this.head[0].push(element.name); } }); this.head[0].push("Total"); this.head[0].push("ETA/Cutoff"); console.log(this.head); }); } getTotal(array) { var data = array.map(i => i.quantity); data.reduce((a, b) => a + b, 0); return data; } getRouteDetails(event?: LazyLoadEvent) { var locationId = parseInt(localStorage.getItem('operatingLocationId')); let timeId = localStorage.getItem('timeZoneId'); this.localRouteIds = JSON.parse(localStorage.getItem('routeIds')); if (this.localRouteIds.length == 0) { window.close(); } this.spinnerService.show(); this.retrieving = false; this._controllerPrintRouteDetails.getRouteSheetLab(locationId, moment.tz(localStorage.getItem('loadDate'), timeId), this.localRouteIds).subscribe(result => { if (result != undefined) { this.spinnerService.hide(); this.routeSheet = result; this.retrieving = true; // this.routeCode = result[0].routeCode; this.loadDateString = result[0].loadDate; this.locationName = result[0].location; // this.notify.success(this.l('Exported Successfully')); // window.open(AppConsts.remoteServiceBaseUrl + '/File/DownloadTempFile?fileType=' + result.fileType + '&fileToken=' + result.fileToken + '&fileName=' + result.fileName, '_blank'); } else { this.spinnerService.hide(); this.retrieving = true; this.notify.error(this.l('No Route Sheet')); } }); } onShown() { } ConvertToInt(val) { return parseInt(val); } goBack() { this._location.back(); } exportToExcel() { this.spinnerService.show(); var locationId = parseInt(localStorage.getItem('operatingLocationId')); let timeId = localStorage.getItem('timeZoneId'); this.spinnerService.show(); this._controllerPrintRouteDetails.getRouteSheetToExcel(locationId, moment.tz(localStorage.getItem('loadDate'), timeId), this.localRouteIds).subscribe(result => { 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(); } }); } printPdf() { var locationId = parseInt(localStorage.getItem('operatingLocationId')); let timeId = localStorage.getItem('timeZoneId'); // this.spinnerService.show(); // this._controllerPrintRouteDetails.getRouteSheetLab(locationId, moment.tz(localStorage.getItem('loadDate'), timeId), this.localRouteIds).subscribe(result => { if (this.routeSheet != undefined) { var img = new Image(); img.src = 'assets/images/AntechLogo.jpg' const doc = new jsPDF('l', '', 'letter'); var count = 0; for (var x in this.routeSheet) { var route = this.routeSheet[x].routeCode; var loadDate = this.routeSheet[x].loadDate; if (count == 0) { this.data = []; this.dataWillCall = []; this.dataAutos = []; var countAuto = 1; var countWillCall = 1; if(this.routeSheet[x].routeSchedAutosList.length!=0){ this.dataRow = []; this.dataRow.push(''); this.dataRow.push('Autos'); this.dataRow.push(''); this.dataRow.push(''); this.dataRow.push(''); this.dataRow.push(''); this.dataRow.push(''); this.dataRow.push(''); this.dataRow.push(''); this.dataRow.push(''); this.data.push(this.dataRow); } for (var y in this.routeSheet[x].routeSchedAutosList) { this.dataRow = []; var total = 0; this.dataRow.push(countAuto); this.dataRow.push(this.routeSheet[x].routeSchedAutosList[y].accountNumber); this.dataRow.push(this.routeSheet[x].routeSchedAutosList[y].customerName); this.dataRow.push(this.routeSheet[x].routeSchedAutosList[y].phoneNumber); this.dataRow.push(this.routeSheet[x].routeSchedAutosList[y].addresses); if(this.routeSheet[x].routeSchedAutosList[y].orderPackages!=null){ for(var data in this.packages){ var getPackage = this.routeSheet[x].routeSchedAutosList[y].orderPackages.filter(o => o.packageId == this.packages[data].id); var getCount = getPackage==null ? 0 : getPackage.length==0 ? 0 : getPackage.map(a => a.quantity).reduce(function(acc, cur) { return acc + cur }); this.dataRow.push(getCount==0? "-" : getCount.toString()); total = total + getCount; } }else{ for(var data in this.packages){ this.dataRow.push("-"); } } this.dataRow.push(total==0? "-" : total.toString()); this.dataRow.push(this.routeSheet[x].routeSchedAutosList[y].eta); this.data.push(this.dataRow); countAuto = countAuto + 1; } if(this.routeSheet[x].routeSchedWillCallList.length!=0){ this.dataRow = []; this.dataRow.push(''); this.dataRow.push('Will Calls'); this.dataRow.push(''); this.dataRow.push(''); this.dataRow.push(''); this.dataRow.push(''); this.dataRow.push(''); this.dataRow.push(''); this.dataRow.push(''); this.dataRow.push(''); this.data.push(this.dataRow); } for (var y in this.routeSheet[x].routeSchedWillCallList) { this.dataRow = []; var totalResw1 = 0; this.dataRow.push(countWillCall); this.dataRow.push(this.routeSheet[x].routeSchedWillCallList[y].accountNumber); this.dataRow.push(this.routeSheet[x].routeSchedWillCallList[y].customerName); this.dataRow.push(this.routeSheet[x].routeSchedWillCallList[y].phoneNumber); this.dataRow.push(this.routeSheet[x].routeSchedWillCallList[y].addresses); if(this.routeSheet[x].routeSchedWillCallList[y].orderPackages!=null){ for(var data in this.packages){ var getPackage = this.routeSheet[x].routeSchedWillCallList[y].orderPackages.filter(o => o.packageId==this.packages[data].id); var getCount = getPackage==null ? 0 : getPackage.length==0 ? 0 : getPackage.map(a => a.quantity).reduce(function(acc, cur) { return acc + cur }); this.dataRow.push(getCount==0? "-" : getCount.toString()); totalResw1 = totalResw1 + getCount; } }else{ for(var data in this.packages){ this.dataRow.push("-"); } } this.dataRow.push(totalResw1==0? "-" : totalResw1.toString()); this.dataRow.push(this.routeSheet[x].routeSchedWillCallList[y].eta); this.data.push(this.dataRow); countWillCall = countWillCall + 1; } (doc as any).autoTable({ head: this.head, body: this.data, theme: 'grid', didDrawCell: data => { }, columnStyles: { halign: 'center' }, styles: {textColor: [0, 0, 0], lineColor: [0, 0, 0], fontSize: 8}, bodyStyles : {fontSize: 7, halign: 'center'}, headStyles: {fillColor: [255, 255, 255], lineColor: [0, 0, 0], halign: 'center'}, margin: { top: 21 }, didDrawPage : function (data) { doc.addImage(img, 'png', 16, 5, 25, 15); doc.setFontSize(12); doc.text(263, 10, route, 'right'); doc.text(263, 15, loadDate, 'right'); } }); count++; } else { doc.addPage('letter', 'l'); this.data = []; this.dataWillCall = []; this.dataAutos = []; var countAuto = 1; var countWillCall = 1; if(this.routeSheet[x].routeSchedAutosList.length!=0){ this.dataRow = []; this.dataRow.push(''); this.dataRow.push('Autos'); this.dataRow.push(''); this.dataRow.push(''); this.dataRow.push(''); this.dataRow.push(''); this.dataRow.push(''); this.dataRow.push(''); this.dataRow.push(''); this.dataRow.push(''); this.data.push(this.dataRow); } for (var y in this.routeSheet[x].routeSchedAutosList) { this.dataRow = []; var totalRes2 = 0; this.dataRow.push(countAuto); this.dataRow.push(this.routeSheet[x].routeSchedAutosList[y].accountNumber); this.dataRow.push(this.routeSheet[x].routeSchedAutosList[y].customerName); this.dataRow.push(this.routeSheet[x].routeSchedAutosList[y].phoneNumber); this.dataRow.push(this.routeSheet[x].routeSchedAutosList[y].addresses); if(this.routeSheet[x].routeSchedAutosList[y].orderPackages!=null){ if(this.routeSheet[x].routeSchedAutosList[y].orderPackages!=null){ for(var data in this.packages){ var getPackage = this.routeSheet[x].routeSchedAutosList[y].orderPackages.filter(o => o.packageId == this.packages[data].id); var getCount = getPackage==null ? 0 : getPackage.length==0 ? 0 : getPackage.map(a => a.quantity).reduce(function(acc, cur) { return acc + cur }); this.dataRow.push(getCount==0? "-" : getCount.toString()); totalRes2 = totalRes2 + getCount; } } }else{ for(var data in this.packages){ this.dataRow.push("-"); } } this.dataRow.push(totalRes2==0? "-" : totalRes2.toString()); this.dataRow.push(this.routeSheet[x].routeSchedAutosList[y].eta); this.data.push(this.dataRow); countAuto = countAuto + 1; } if(this.routeSheet[x].routeSchedWillCallList.length!=0){ this.dataRow = []; this.dataRow.push(''); this.dataRow.push('Will Calls'); this.dataRow.push(''); this.dataRow.push(''); this.dataRow.push(''); this.dataRow.push(''); this.dataRow.push(''); this.dataRow.push(''); this.dataRow.push(''); this.dataRow.push(''); this.data.push(this.dataRow); } for (var y in this.routeSheet[x].routeSchedWillCallList) { this.dataRow = []; var totalResw2=0; this.dataRow.push(countWillCall); this.dataRow.push(this.routeSheet[x].routeSchedWillCallList[y].accountNumber); this.dataRow.push(this.routeSheet[x].routeSchedWillCallList[y].customerName); this.dataRow.push(this.routeSheet[x].routeSchedWillCallList[y].phoneNumber); this.dataRow.push(this.routeSheet[x].routeSchedWillCallList[y].addresses); if(this.routeSheet[x].routeSchedWillCallList[y].orderPackages!=null){ for(var data in this.packages){ var getPackage = this.routeSheet[x].routeSchedWillCallList[y].orderPackages.filter(o => o.packageId==this.packages[data].id); var getCount = getPackage==null ? 0 : getPackage.length==0 ? 0 : getPackage.map(a => a.quantity).reduce(function(acc, cur) { return acc + cur }); this.dataRow.push(getCount==0? "-" : getCount.toString()); totalResw2 = totalResw2 + getCount; } }else{ for(var data in this.packages){ this.dataRow.push("-"); } } this.dataRow.push(totalResw2==0? "-" : totalResw2.toString()); this.dataRow.push(this.routeSheet[x].routeSchedWillCallList[y].eta); this.data.push(this.dataRow); countWillCall = countWillCall + 1; } doc.setFontSize(11); (doc as any).autoTable({ head: this.head, body: this.data, theme: 'grid', didDrawCell: data => { }, columnStyles: { halign: 'center' }, styles: {textColor: [0, 0, 0], lineColor: [0, 0, 0], fontSize: 8}, bodyStyles : {fontSize: 7, halign: 'center'}, headStyles: {fillColor: [255, 255, 255], lineColor: [0, 0, 0], halign: 'center'}, margin: { top: 21 }, didDrawPage : function (data) { doc.addImage(img, 'png', 16, 5, 25, 15); doc.setFontSize(12); doc.text(263, 10, route, 'right'); doc.text(263, 15, loadDate, 'right'); } }); } } doc.setProperties({ title: "Route Sheet - " +this.routeSheet[0].loadDate +".pdf" }); // doc.addImage(img, 'png', 5, 5, 20, 15); doc.output('save', 'Route Sheet - ' +this.routeSheet[0].loadDate +'.pdf') ; // doc.output('dataurlnewwindow'); } else { this.spinnerService.hide(); this.notify.error(this.l('No Route Sheet')); } // }); // doc.setDisplayMode('fullpage', 'single', 'UseOutlines'); // doc.save("a4.pdf"); } }