/** * @Component Name: OrderListComponent * @Module: Home * @Module Functionality : To display the order search results * @Creation Date: August 12,2017 * @Author: sbaireddy * @Version: 1.0 */ import { Component, Input, OnInit, OnDestroy } from '@angular/core'; import { Router } from '@angular/router'; import * as Constants from '../../../shared/constants'; import Utils from '../../../shared/utils'; import { STORES, ORDER_STATUS, ORDER_STATUS_DROP_DOWN_LIST, ORDER_SEARCH_REFTYPE } from '../../../shared/form.constants'; import { IKeyValue } from '../../../shared/modal/key.value'; import { IOrderList, IOrder } from '../../../shared/modal/common'; import { Customer } from '../../../shared/modal/customer'; import { User } from '../../../shared/modal/user'; import { Cart } from '../../../shared/modal/cart'; import { SharedService } from '../../../shared/shared.service'; import { UserService } from '../../../services/user.service'; import { LookupService } from '../../../services/lookup.service'; import { CartService } from '../../../services/cart.service'; import { CustomerService } from '../../../services/customer.service'; @Component({ selector: 'order-list-cmp', moduleId: module.id, templateUrl: 'order-list.component.html' }) export class OrderListComponent implements OnInit, OnDestroy { private COMPONENT_NAME: string = 'OrderListComponent'; orderStatusItems: any[]; orderStatusDrpItems: any[]; orderStoresItems: any[]; headerRow: string[] = ['Order ID', 'Customer Name', 'Channel', 'Type', 'Created Date', 'Status']; headerRowMobile: string[] = ['Order ID', 'Status']; orderList: IOrderList; user: User; cart: Cart; ordersPerPage: number = 25; selectedStatus: any; // pagination allItems: any[]; // array of all items to be paged pager: any = {}; // pager object pagedItems: any[]; // paged items storeId: number; // update storeid orderSearchModel: any = {}; // for sorting isDesc: boolean = false; searchText: string = ''; column: string = 'fullOrderId'; direction: number; showOrderSummaryData: any = {}; customerProfileResponse: any = {} constructor( public router: Router, public sharedService: SharedService, public userService: UserService, public lookupService: LookupService, public cartService: CartService, public customerService: CustomerService) { } /** * This function is used to initialize the directive/component after Angular * first displays the data-bound properties.It is invoked only once when the * directive is instantiated. * @returns void */ ngOnInit(): void { const METHOD_NAME: string = 'ngOnInit()'; this.sharedService.updateErrorMessage(null); // Start-Switch Channel-Store id update this.sharedService.storeId.subscribe((val: number) => {this.storeId = val; }); this.user = this.userService.getUser(); if (this.user && this.user.favorites && this.user.favorites.ordersPerPage) { this.ordersPerPage = this.user.favorites.ordersPerPage; } // End-Switch Channel-Store id update this.orderStatusItems = ORDER_STATUS.filter(orderStatusItem => orderStatusItem); this.orderStatusDrpItems = ORDER_STATUS_DROP_DOWN_LIST.filter(orderStatusItem => orderStatusItem); this.orderStoresItems = STORES.filter(orderStoreItem => orderStoreItem); this.orderSearchModel = this.lookupService.getOrderSearchModel(); this.orderList = this.lookupService.getOrderList(); if (this.orderSearchModel) { for (let i = 0; i < this.orderStatusDrpItems.length; i++) { if (this.orderSearchModel && this.orderStatusDrpItems[i].key === this.orderSearchModel.status) { this.selectedStatus = this.orderStatusDrpItems[i].value; } } if (this.orderSearchModel.status == null) { this.selectedStatus = Constants.SELECTED_STATUS; } } if (this.orderList && this.orderList.orders) { // formatting for display this.sortOrders(); } else { this.sharedService.showErrorMessage('_ERR_EMPTY_RESULT_FOUND', false); } } /** * Cleanup just before Angular destroys the directive/component. * Unsubscribe Observables and detach event handlers to avoid memory leaks. * @returns void */ ngOnDestroy(): void { const METHOD_NAME: string = 'ngOnDestroy()'; this.orderList = null; this.cart = null; this.pager = null; this.pagedItems = null; this.showOrderSummaryData = null; this.customerProfileResponse = null; this.orderStatusItems = null; this.orderStoresItems = null; this.headerRow = null; this.headerRowMobile = null; this.selectedStatus = null; this.allItems = null; this.orderSearchModel = null; } /** * This function is used to convert date string to date object. * @param {string} dateString * @returns Date */ getDateLable(dateString: string): Date { const METHOD_NAME: string = 'getDateLable()'; const newDate = new Date(dateString); return newDate; } /** * This function is used to get channel label from order stores object. * @param {number} storeId * @returns string */ getChannelLabel(storeId: number): string { const METHOD_NAME: string = 'getChannelLabel()'; for (const item of this.orderStoresItems) { if (item.key === storeId) { return item.value; } } return 'Not Found'; } /** * This function is used to get the value of status label from key value pair. * @param {string} status - key/shorthand notation of the order status. * @returns string */ getStatusLabel(status: string): string { const METHOD_NAME: string = 'getStatusLabel()'; for (const item of this.orderStatusItems) { if (item.key === status) { return item.value; } } return 'Not Found'; } /** * This function is used to get the value of status label from key value pair. * @param {string} status - key/shorthand notation of the order status. * @returns string */ getStatusDrpLabel(status: string): string { const METHOD_NAME: string = 'getStatusLabel()'; for (const item of this.orderStatusDrpItems) { if (item.key === status) { return item.value; } } return 'Not Found'; } /** * This function is used to set and apply paging for devices. * @param {number} page contains valid number. * @returns void */ setPage(page: number) { const METHOD_NAME: string = 'setPage()'; if (page < 1 || page > this.pager.totalPages) { return; } if (this.allItems) { // get pager object from service this.pager = Utils.getPager(this.allItems.length, page, this.ordersPerPage); // get current page of items this.pagedItems = this.allItems.slice(this.pager.startIndex, this.pager.endIndex + 1); } else { this.pagedItems = null; } } /** * This function is used to get overall summary of the order. * @param {number} orderId - orderId of the user. * @returns void */ showOrderSummary(orderId: number): void { const METHOD_NAME: string = 'showOrderSummary()'; this.sharedService.addLogging(this.COMPONENT_NAME, METHOD_NAME, 'Display order summary'); console.log('OrderListComponent.showOrderSummary()>>Start.'); // clearing order details from local storage this.lookupService.setOrder(null); this.sharedService.showSpinner(true); // spinner this.cartService.getCartSummaryInclCancelled(this.storeId, orderId, true).subscribe(result => { this.showOrderSummaryData = result; this.sharedService.showSpinner(false); // spinner this.lookupService.setOrder(result); this.router.navigate(['home/search/order-detail'], { queryParams: { orderId: result.fullOrderId} }); }, error => { this.sharedService.showSpinner(false); // spinner this.sharedService.showErrorMessage(error, true); console.error('error=>' + error) }); } /** * This function is used to sort the orders * @returns void */ sortOrders(): void { const METHOD_NAME: string = 'sortOrders()'; console.log('this.searchText:' + this.searchText); this.pager = {};  // pager object this.pagedItems = []; this.allItems = new Array(); for (const order of this.orderList.orders) { const item: any = Object.assign({}, order); item.name = ''; if (order.firstName && order.lastName) { item.name = order.firstName + ' ' + order.lastName; } else if (order.firstName) { item.name = order.firstName + ' ' + order.lastName; } else if (order.lastName) { item.name = order.lastName; } item.channel = this.getChannelLabel(order.storeId); if (this.orderSearchModel && this.orderSearchModel.status && (this.orderSearchModel.status === 'P8' || this.orderSearchModel.status === 'P14')) { item.status = this.getStatusDrpLabel(this.orderSearchModel.status); this.allItems.push(item); } else { if(item.storeId == Constants.STORE_ID_RT && item.status == Constants.ORDER_STATUS_C){ item.status = Constants.PURCHASE_EST_SENT; this.allItems.push(item); }else{ // Checking order status entered by agent item.status = this.getStatusLabel(order.status); this.allItems.push(item); // if(!this.selectedStatus){ // this.allItems.push(item); // } else if(this.selectedStatus === item.status || this.selectedStatus === Constants.SELECTED_STATUS){ // this.allItems.push(item); // } } } } if (this.searchText && this.searchText.length > 0) { this.pager = {};  // pager object this.pagedItems = []; this.allItems = this.allItems.filter((item) => { if (item.fullOrderId && item.fullOrderId.indexOf(this.searchText.toUpperCase()) > -1) { return 1; } else if (item.name && item.name.toUpperCase().indexOf(this.searchText.toUpperCase()) > -1) { return 1; } else if (item.channel && item.channel.toUpperCase().indexOf(this.searchText.toUpperCase()) > -1) { return 1; } else if (item.createdDate && item.createdDate.toUpperCase().indexOf(this.searchText.toUpperCase()) > -1) { return 1; } else if (item.status && item.status.toUpperCase().indexOf(this.searchText.toUpperCase()) > -1) { return 1; } else { return 0 }; }); } // initialize to page 1 this.setPage(1); } sort(property) { const METHOD_NAME: string = 'sort()'; this.isDesc = !this.isDesc; // change the direction this.column = property; this.direction = this.isDesc ? 1 : -1; }; /** * This function is used to sort the order list * @param {string} columnName - column name for sorting * @param {number} direction - sorting direction * @returns void */ orderBy(columnName: string, direction: number): void { const METHOD_NAME: string = 'orderBy()'; this.allItems = this.allItems.sort(function (a, b) { if (a[columnName] < b[columnName]) { return -1 * direction; } else if (a[columnName] > b[columnName]) { return 1 * direction; } else { return 0; } }); // initialize to page 1 this.setPage(1); } /** * This function is used to get the value of ref type label from key value pair. * @param {string} refType - order reference type. * @returns string */ getRefType(refType: string): string { const METHOD_NAME: string = 'getRefType()'; for (const item of ORDER_SEARCH_REFTYPE) { if (item.key === refType) { return item.value; } } return Constants.NOT_FOUND; } }