/** * @Component Name: OrderNotesComponent * @Module: Home * @Module Functionality : To display the order notes * @Creation Date: August 12,2017 * @Author: sbaireddy * @Version: 1.0 */ import { Component, Input, OnInit, OnChanges, OnDestroy } from '@angular/core'; import Utils from '../../shared/utils'; import * as Constants from '../../shared/constants'; import { IKeyValue } from '../../shared/modal/key.value'; import { ORDER_NOTES_REASONS } from '../../shared/form.constants'; import { Cart } from '../../shared/modal/cart'; import { User } from '../../shared/modal/user'; import { SharedService } from '../../shared/shared.service'; import { LookupService } from '../../services/lookup.service'; import { CartService } from '../../services/cart.service'; import { UserService } from '../../services/user.service'; declare var jQuery: any; @Component({ moduleId: module.id, selector: 'order-notes-cmp', templateUrl: 'order-notes.component.html', styleUrls: ['order-notes.component.css'] }) export class OrderNotesComponent implements OnInit, OnChanges, OnDestroy { private COMPONENT_NAME: string = 'OrderNotesComponent'; visible: boolean = false; visibleAnimate: boolean = false; storeId: number = Constants.STORE_ID_CD; orderNotesReasons: any[]; user: User; cart: Cart; subscription: any; orderNotesModel: any = {subject: null, description: ''}; orderNotes: any = null; showOrderNotes: boolean = true; KarmaTestResult: any = {}; submitted: boolean = false; constructor( public sharedService: SharedService, public cartService: CartService, public lookupService: LookupService, public userService: UserService) { } /** * 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 { this.orderNotesModel.submitted = false; const METHOD_NAME: string = 'ngOnInit()'; console.log('[start]-OrderNotesComponent.ngOnInit()'); this.sharedService.storeId.subscribe((val: number) => { this.storeId = val; }); this.user = this.userService.getUser(); // getting cart changes... this.cart = this.cartService.getCart(); this.subscription = this.cartService.hasUpdate.subscribe((item) => { if (item) { this.cart = this.cartService.getCart(); } if (!this.cart) { this.cart = this.lookupService.getOrder(); } }); if (!this.cart) { this.cart = this.lookupService.getOrder(); } this.orderNotesReasons = ORDER_NOTES_REASONS.filter(notesReason => notesReason); this.orderNotesModel.subject = 'Other'; this.orderNotesModel.description = 'Test'; console.log('[end]-OrderNotesComponent.ngOnInit()'); } /** * 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. * @returns void */ ngOnDestroy(): void { const METHOD_NAME: string = 'ngOnDestroy()'; this.user = null; this.cart = null; this.KarmaTestResult = null; this.orderNotes = null; this.orderNotesModel = null; this.orderNotesReasons = null; this.subscription.unsubscribe(); } /** * This function used to display the object. * @return {void} */ public show(): void { const METHOD_NAME: string = 'show()'; this.cart = (this.cartService.getCart() || this.lookupService.getOrder()); if (this.cart) { this.visible = true; setTimeout(() => this.visibleAnimate = true, 100); this.showOrderNotes = true; this.orderNotesModel = {subject: null, description: '', submitted: 'true'}; this.loadOrderNotes(); // loading order notes } } /** * This function is used to hide the object. * @returns void */ public hide(): void { const METHOD_NAME: string = 'hide()'; this.visibleAnimate = false; setTimeout(() => this.visible = false, 300); } /** * This function is used to hide the object when nouse event performed * @param {MouseEvent} event contains valid mouse event * @returns void */ public onContainerClicked(event: MouseEvent): void { const METHOD_NAME: string = 'onContainerClicked()'; if ((event.target).classList.contains('modal')) { this.hide(); } } /** * This function is used get Order notes through getOrderNotes function of lookup service. * @returns void */ public loadOrderNotes(): void { const METHOD_NAME: string = 'loadOrderNotes()'; if (this.cart && this.cart.orderId) { // this.sharedService.showSpinner(true);//spinner this.lookupService.getOrderNotes(this.storeId, this.cart.orderId).subscribe(result => { this.KarmaTestResult = result; // this.sharedService.showSpinner(false);//spinner this.orderNotes = result.orderNotes; this.searchMemoNotes(); // (this.cart.customer && this.cart.customer.creditProfile && this.cart.customer.creditProfile.accountNumber)? this.searchOrderNotes():''; }, error => { // this.sharedService.showSpinner(false);//spinner // this.sharedService.showErrorMessage(error, null); console.error('error=>' + error); }); } } /** * This function is used get Order notes through getOrderNotes function of lookup service. * @returns void */ public searchMemoNotes(): void { const METHOD_NAME: string = 'loadOrderNotes()'; let accountNumber: any ; if (this.cart.customer && this.cart.customer.creditProfile && this.cart.customer.creditProfile.accountNumber) { accountNumber = this.cart.customer.creditProfile.accountNumber; } else if (this.cart.customer && this.cart.customer.creditProfile && this.cart.customer.creditProfile.tentativeBAN) { accountNumber = this.cart.customer.creditProfile.tentativeBAN; } if (this.cart && this.cart.orderId && accountNumber) { // this.sharedService.showSpinner(true);//spinner // this.lookupService.searchMemoNotes(this.storeId, this.cart.orderId, 955037100).subscribe(result => { this.lookupService.searchMemoNotes(this.storeId, this.cart.orderId, accountNumber, this.cart.createdByDate,this.cart.refType).subscribe(result => { this.KarmaTestResult = result; // this.sharedService.showSpinner(false);//spinner if (Number(result.statusCode) === 100) { this.orderNotes = this.orderNotes.concat(result.memoList); } }, error => { // this.sharedService.showSpinner(false);//spinner // this.sharedService.showErrorMessage(error, null); console.error('error=>' + error); }); } } /** * This function is used to add new Order notes through createOrderNotes function of lookup Service. * @returns void */ orderAddNote(): void { const METHOD_NAME: string = 'orderAddNote()'; this.sharedService.addLogging(this.COMPONENT_NAME, METHOD_NAME, 'Adding order notes'); console.log('OrderNotesComponent.orderAddNote()>>Start.', this.orderNotesModel); if ( this.orderNotesModel.subject && this.orderNotesModel.description) { this.sharedService.showSpinner(true); // spinner this.orderNotesModel.submitted = true; const requestParams: any = { agentId: this.user.logonId, subject: this.orderNotesModel.subject, description: this.orderNotesModel.description } this.sharedService.showSpinner(true); // spinner this.lookupService.createOrderNotes(this.storeId, this.cart.orderId, requestParams).subscribe(result => { this.KarmaTestResult = result; this.sharedService.showSpinner(false); // spinner console.log('created order note is>>>>' + result); this.orderNotesModel = {subject: null, description: '', submitted: 'true'}; this.loadOrderNotes(); // loading order notes. }, error => { this.sharedService.showSpinner(false); // spinner this.sharedService.showErrorMessage(error, true); console.error('error=>' + error); }); console.log('OrderNotesComponent.orderAddNote()>>End.', requestParams); } else { this.orderNotesModel.submitted = false; } } }