/** * @Component Name: AdjustPriceComponent * @Module: Home * @Module Functionality : To display the adjust price * @Creation Date: November 29,2017 * @Author: sbaireddy * @Version: 1.0 */ import { Component, Input, OnInit, Output, ViewChild, EventEmitter, OnDestroy } from '@angular/core'; import { RT_ADJUST_PRICE_SOFTGOODS_REASONS } from '../../../../shared/form.constants'; import { SharedService } from '../../../../shared/shared.service'; import { CartService } from '../../../../services/cart.service'; import { UserService } from '../../../../services/user.service'; import { User } from '../../../../shared/modal/user'; declare var jQuery: any; @Component({ moduleId: module.id, selector: 'adjust-montly-cmp', templateUrl: 'adjust-montly.component.html', styleUrls: ['adjust-montly.component.css'] }) export class AdjustMonthlyComponent implements OnInit, OnDestroy { private COMPONENT_NAME: string = 'AdjustMonthlyComponent'; visible: boolean = false; visibleAnimate: boolean = false; karmaTestResult: any = {}; storeId: number; errorMessage: string = ''; adjPriceReasons: any[]; cart: any; user: User; @Input() orderItem: any; @Output() canRefreshCart = new EventEmitter(false); @ViewChild('adjustPriceForm') adjustPriceForm: any; reasonText: string; adjustPrice: any; finalAdjustPrice: number; showPercentUpdate: boolean = true; adjustPercent: number; constructor( public sharedService: SharedService, public userService: UserService, 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 {void} */ ngOnInit(): void { const METHOD_NAME: string = 'ngOnInit()'; this.user = this.userService.getUser(); this.adjPriceReasons = RT_ADJUST_PRICE_SOFTGOODS_REASONS.filter(adjPriceItem => adjPriceItem); } /** * 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.cart = null; this.user = null; this.orderItem = null; this.karmaTestResult = null; this.adjPriceReasons = null; this.adjustPrice = null; this.showPercentUpdate = null; this.adjustPercent = null } /** * This function used to display the object and also to reset the form. * @return {void} */ public show(): void { const METHOD_NAME: string = 'show()'; this.visible = true; setTimeout(() => this.visibleAnimate = true, 100); this.sharedService.storeId.subscribe((val: number) => { this.storeId = val; }); this.errorMessage = null; this.reasonText = null; this.adjustPrice = null; this.finalAdjustPrice = 0; this.cart = this.cartService.getCart(); // this.adjustPriceForm.reset(); this.showPercentUpdate = false; this.adjustPercent = null; } /** * 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 to adjust the price of a product. * @param {any} val - value that will be adjusted to the product, */ updateAdjustPrice(val: any) { if (val) { this.finalAdjustPrice = val; } } /** * This function is used to adjust the percentage of a product. * @param {any} val - value that will be adjusted to the product, */ updateAdjustPercent(val: any) { const origMonthlyPrice:number = this.orderItem.origMonthlyPrice; if (val && Number(val) > 0 && origMonthlyPrice > 0) { this.finalAdjustPrice = (origMonthlyPrice - ((val/100) * origMonthlyPrice)); } else { this.finalAdjustPrice = 0; } } /** * This function is used to update the price by calling adjustPrice api. * @returns void */ updatePrice(): void { const METHOD_NAME: string = 'updatePrice()'; this.errorMessage = null; this.sharedService.addLogging(this.COMPONENT_NAME, METHOD_NAME, 'Update the Adjust Price'); if (this.reasonText && this.finalAdjustPrice <= this.orderItem.origMonthlyPrice && this.finalAdjustPrice >= (this.orderItem.autoPayCreditAmount + this.orderItem.hybridBanCreditAmount + this.orderItem.nonSubscriberLineLevelBillCreditAmount)){ //(this.user.adjustPricingLimit >= (this.finalAdjustPrice - this.orderItem.origMonthlyPrice))) { const lines: Array = new Array(); lines.push({ orderItemId: this.orderItem.orderItemId, reasoncode: this.reasonText, price: '$' + this.finalAdjustPrice, currencyCode: 'USD' // financeType: this.orderItem.financeType }); const inputData: any = { 'lines': lines }; this.sharedService.showSpinner(true); // spinner this.cartService.adjustPrice(this.storeId, this.cart.orderId, inputData).subscribe(result => { this.karmaTestResult = result; this.sharedService.showSpinner(false); // spinner if(result && (!result.overridePrice || (result.overridePrice && result.overridePrice == 'false') )){ this.errorMessage = this.sharedService.getErrorMessage('_ERR_PRICE_UPDATE_FAILED'); }else{ this.canRefreshCart.emit(true); this.hide(); } }, error => { this.sharedService.showSpinner(false); // spinner this.errorMessage = this.sharedService.getErrorMessage(error); console.error('error=>' + error) }); } else { if (!this.reasonText) { this.errorMessage = this.sharedService.getErrorMessage('_ERR_ADJUST_REASON_EMPTY'); //} else if (this.user.adjustPricingLimit <= (this.finalAdjustPrice - this.orderItem.origMonthlyPrice)) { //this.errorMessage = this.sharedService.getErrorMessage('_ERR_ADJUST_PRICE_INVALID') + this.user.adjustPricingLimit; } else if (this.reasonText && this.finalAdjustPrice > this.orderItem.origMonthlyPrice) { this.errorMessage = this.sharedService.getErrorMessage('_ERR_ADJUST_PRICE_MIN_MAX'); } else { this.errorMessage = this.sharedService.getErrorMessage('_ERR_ADJUST_PRICE_EMPTY'); } } } }