/** * @Component Name: AssociatedOrdersComponent * @Module: Home * @Module Functionality : To display the associated orders details * @Creation Date: August 12,2017 * @Author: sbaireddy * @Version: 1.0 */ import { Component, Input, Output, OnInit, EventEmitter, OnChanges, OnDestroy } from '@angular/core'; import { Router } from '@angular/router'; import CartUtils from '../../../../shared/cart.utils'; import { User } from '../../../../shared/modal/user'; import { Cart } from '../../../../shared/modal/cart'; import * as Constants from '../../../../shared/constants'; import { IPackage } from '../../../../shared/modal/common'; import { RETURN_ITEM_REASONS } from '../../../../shared/form.constants' import { STORES, ORDER_STATUS, ORDER_REFTYPE } from '../../../../shared/form.constants'; import { SharedService } from '../../../../shared/shared.service'; import { LookupService } from '../../../../services/lookup.service'; import { CartService } from '../../../../services/cart.service'; import { UserService } from '../../../../services/user.service'; @Component({ selector: 'associated-orders-cmp', moduleId: module.id, templateUrl: 'associated-orders.component.html', styleUrls: ['associated-orders.component.css'] }) export class AssociatedOrdersComponent implements OnInit, OnChanges, OnDestroy { private COMPONENT_NAME: string = 'AssociatedOrdersComponent'; headerRow: string[] = ['Order ID', 'Created By', 'Channel', 'Type', 'Created Date', 'Status']; headerRowMobile: string[] = ['Order ID', 'Status']; orderStatusItems: any[]; orderStoresItems: any[]; showAssociatedInfo: boolean = false; storeId: number; // update storeid user: User; @Input() associatedOrders: any; @Output() canRefreshCart: EventEmitter = new EventEmitter(); constructor( public router: Router, public sharedService: SharedService, public userService: UserService, public lookupService: LookupService, public cartService: CartService) { } /** * 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. * @return string */ ngOnInit(): void { const METHOD_NAME: string = 'ngOnInit()'; this.sharedService.storeId.subscribe((val: number) => { this.storeId = val; console.log('updated storeid' + this.storeId); }); this.orderStatusItems = ORDER_STATUS.filter(orderStatusItem => orderStatusItem); this.orderStoresItems = STORES.filter(orderStoreItem => orderStoreItem); } /** * This function is called whenever one or more data-bound input properties change * @returns void */ ngOnChanges(): void { const METHOD_NAME: string = 'ngOnChanges()'; } /** * Cleanup just before Angular destroys the directive/component. * Unsubscribe Observables and detach event handlers to avoid memory leaks. * @return string */ ngOnDestroy(): void { const METHOD_NAME: string = 'ngOnDestroy()'; this.user = null; this.associatedOrders = null; this.headerRow = null; this.headerRowMobile = null; this.orderStatusItems = null; this.orderStoresItems = 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 Constants.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 Constants.NOT_FOUND; } /** * 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_REFTYPE) { if (item.key === refType) { return item.value; } } return Constants.NOT_FOUND; } /** * 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'); // 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.sharedService.showSpinner(false); // spinner this.lookupService.setOrder(result); this.canRefreshCart.emit(true); }, error => { this.sharedService.showSpinner(false); // spinner this.sharedService.showErrorMessage(error, true); console.error('error=>' + error); }); } /** * This function is used to get overall summary of the order. * @param {number} orderId - orderId of the user. * @returns void */ showSAPOrderSummary(orderId: number): void { const METHOD_NAME: string = 'showSAPOrderSummary()'; this.sharedService.addLogging(this.COMPONENT_NAME, METHOD_NAME, 'Display sap order summary'); // clearing order details from local storage let phoneNumber1: any = null; let crid: any = null; if (this.lookupService.getOrder() && this.lookupService.getOrder().customer) { phoneNumber1 = this.lookupService.getOrder().customer.phone1; crid = this.lookupService.getOrder().customer.creditProfile.onyxReferenceNumber; } this.lookupService.setOrder(null); this.sharedService.showSpinner(true); // spinner this.cartService.getSapItemDetails(this.storeId, orderId).subscribe(result => { this.sharedService.showSpinner(false); // spinner let modifiedCustomerCart: any = result; modifiedCustomerCart['customer'] = modifiedCustomerCart['customerInfo']; if (phoneNumber1) { modifiedCustomerCart.customer.phone1 = phoneNumber1; modifiedCustomerCart.customer.creditProfile.onyxReferenceNumber = crid; } delete modifiedCustomerCart['customerInfo']; this.lookupService.setOrder(modifiedCustomerCart); //set this value only for lease order this.cartService.getCartSummary(this.storeId,result.orderId).subscribe(cartSummary =>{ for(let frwdPak of cartSummary.packages) { for(let blindPack of result.packages) { if(blindPack.device && blindPack.device.financeType == 'LEASE' && Number(blindPack.device.orderItemId) === Number(frwdPak.device.orderItemId)) { blindPack.device.suggestedPrice = frwdPak.device.suggestedPrice; blindPack.device.finalPrice = frwdPak.device.suggestedPrice; blindPack.device.fullRetailPrice = frwdPak.device.suggestedPrice; result.packages.device = blindPack.device; } } } this.lookupService.setOrder(result); }, error => { this.sharedService.showSpinner(false); // spinner this.sharedService.showErrorMessage('_ERR_RESULT_NOT_FOUND', false); console.error('error=>' + error); }); this.canRefreshCart.emit(true); }, error => { this.sharedService.showSpinner(false); // spinner this.sharedService.showErrorMessage(error, true); console.error('error=>' + error); }); } }