import { Component, Injector, ViewChild, ViewEncapsulation } from '@angular/core'; import { ActivatedRoute, Router } from '@angular/router'; import { appModuleAnimation } from '@shared/animations/routerTransition'; import { AppComponentBase } from '@shared/common/app-component-base'; import { OrderServiceProxy, OrderListDto, LocationServiceProxy, LocationListDto, OrderTypesServiceProxy } from '@shared/service-proxies/service-proxies'; import { LazyLoadEvent } from 'primeng/components/common/lazyloadevent'; import { Paginator } from 'primeng/components/paginator/paginator'; import { Table } from 'primeng/components/table/table'; import { finalize } from 'rxjs/operators'; import * as moment from 'moment'; import { CreateOrderModalComponent } from './create-order-modal.component'; import { PrimengTableHelper } from '@shared/helpers/PrimengTableHelper'; import { FileDownloadService } from '@shared/utils/file-download.service'; import { HttpClient } from '@angular/common/http'; import { AppConsts } from '@shared/AppConsts'; import { FileUpload } from 'primeng/fileupload'; import * as signalR from '@aspnet/signalr'; import { ImportOrderTrackingComponent } from './import-order-tracking.component'; @Component({ templateUrl: './order.component.html', encapsulation: ViewEncapsulation.None, animations: [appModuleAnimation()], styleUrls: ['./order.component.less'] }) export class OrderComponent extends AppComponentBase { @ViewChild('dataTable', {static: true}) dataTable: Table; @ViewChild('paginator', {static: true}) paginator: Paginator; @ViewChild('createOrderModal', {static: false}) createOrderModal: CreateOrderModalComponent; @ViewChild('ExcelFileUpload', { static: true }) excelFileUpload: FileUpload; @ViewChild('importOrderTracking', {static: false}) importOrderTracking: ImportOrderTrackingComponent; //Filters filterText = undefined; locationFilter: LocationListDto = new LocationListDto(); filterDate: moment.Moment; maxResultCount = 1000; filteredOrder: any; filteredLocation: any; filteredOrderTypes: any; advancedFiltersAreShown: false; TimeCreated: any; deliveryEta: any; orderId: string; locationId: number; orderTypeId: number; isActiveFilter = -1; uploadUrl: string; orderImporting: boolean = false; private hubConnection: signalR.HubConnection; constructor( injector: Injector, private router: Router, private _orderAppService: OrderServiceProxy, private _locationAppService: LocationServiceProxy, private _fileDownloadService: FileDownloadService, private _orderTypeService: OrderTypesServiceProxy, private _httpClient: HttpClient ) { super(injector); this.uploadUrl = AppConsts.remoteServiceBaseUrl + '/Import/ImportFromExcel'; this.orderImporting = abp.features.isEnabled('App.OrderImporting'); console.log(this.orderImporting); } ngOnInit(){ $('.kt-select2').select2({ placeholder: "Select location.." }); this.filterLocation(); // this.filterOrder(); this.filterOrderTypes() this.backgroundProcessListener(); } getOrders(event?: LazyLoadEvent) { // this.orderId = Number((document.getElementById('OrderId')).value) ? Number((document.getElementById('OrderId')).value) : undefined; this.locationId = Number((document.getElementById('locations')).value) ? Number((document.getElementById('locations')).value) : undefined; this.orderTypeId = Number((document.getElementById('orderType')).value) ? Number((document.getElementById('orderType')).value) : undefined; if (this.primengTableHelper.shouldResetPaging(event)) { this.paginator.changePage(0); return; } //this.TimeCreated = moment(this.TimeCreated).format('YYYY-MM-DD'); this.spinnerService.show(); if (this.TimeCreated!=undefined){ this.filterDate = moment(this.TimeCreated); console.log(this.filterDate); } this._orderAppService.getOrders( undefined, this.filterText, undefined, undefined, this.locationId, undefined, this.filterDate, undefined, //moment(this.filterDate), this.orderTypeId, this.orderId, this.primengTableHelper.getSorting(this.dataTable), this.primengTableHelper.getMaxResultCount(this.paginator, event), this.primengTableHelper.getSkipCount(this.paginator, event) ).subscribe(result => { console.log(result) this.primengTableHelper.totalRecordsCount = result.totalCount; // result.items.forEach(item => { // let total = 0; // item.orderPackages.forEach(orderPackage => { // total = total + orderPackage.quantity; // }); // item.totalPackages = total; // }); this.primengTableHelper.records = result.items; this.spinnerService.hide(); }); } createOrder(): void { this.createOrderModal.show(); } reloadPage(): void { this.paginator.changePage(this.paginator.getPage()); } filterLocation(): void { this._locationAppService.getLocation( undefined, undefined, undefined, undefined, undefined, this.maxResultCount, undefined ).subscribe(result => { this.filteredLocation = result.items; console.log(this.filteredLocation); }); } filterOrderTypes(): void { this._orderTypeService.getAll( undefined, undefined, undefined, this.isActiveFilter, undefined, undefined, undefined, undefined, ).subscribe(result => { this.filteredOrderTypes = result.items; console.log(this.filteredOrderTypes); }); } deleteOrder(order: OrderListDto): void { this.message.confirm( this.l(''), (isConfirmed) => { if (isConfirmed) { this._orderAppService.deleteOrder(order.id) .subscribe(() => { this.reloadPage(); this.notify.success(this.l('SuccessfullyDeleted')); }); } } ); } createOrUpdate(url: any, id?: number){ if(id != null){ var myurl = `${url}/${id}`; }else{ var myurl = `${url}`; } console.log(myurl); this.router.navigateByUrl(myurl); } // filterOrder(){ // this._orderAppService.getOrders( // this.filterText, // undefined, // undefined, // undefined, // undefined, //this.filterText, // undefined, // undefined, // undefined, // undefined, // undefined, // this.maxResultCount, // undefined, // ).subscribe(result => { // this.filteredOrder = result.items; // console.log(this.filteredOrder); // this.spinnerService.hide(); // }); // } exportToExcel(): void { this._orderAppService.getOrdersForExcels( undefined, this.filterText, undefined, undefined, this.locationId, undefined, this.TimeCreated, undefined, this.orderTypeId, this.orderId, ) .subscribe(result => { this._fileDownloadService.downloadTempFile(result); }); } uploadExcel(data: { files: File }): void { this.importOrderTracking.clearMessages(); this.importOrderTracking.modal.show(); const formData: FormData = new FormData(); const file = data.files[0]; formData.append('file', file, file.name); formData.append('orders','orders'); // formData.append('entity','addresses','addresses'); this._httpClient .post(this.uploadUrl, formData) .subscribe(response => { if (response.success) { this.notify.success(this.l('ImportOrderStart')); } else if (response.error != null) { this.notify.error(this.l('ImportOrderFailed')); } }); } onUploadExcelError(): void { this.notify.error(this.l('ImportOrderFailed')); } backgroundProcessListener() { this.hubConnection = new signalR.HubConnectionBuilder() .withUrl(AppConsts.remoteServiceBaseUrl + '/signalr-background') .build(); this.hubConnection .start() .then(() => console.log('Connection started')) .catch(err => console.log('Error while starting connection: ' + err)); this.hubConnection.on('BackgroundMessage', (message) => { // if(message.match(/Route created/) || message.match(/Order created/)) { // this.importOrderTracking.logMessage(message, true); // } // else { if(message.match(/End Importing/)){ this.importOrderTracking.importing = false; }else{ this.importOrderTracking.logMessage(message); } // } }); } }