/** * @Component Name: ExchangeComponent * @Module: Home * @Module Functionality : To display the minicart * @Creation Date: October 18,2017 * @Author: sbaireddy * @Version: 1.0 */ import { Component, Input, OnInit, OnDestroy, OnChanges } from '@angular/core'; import { Router, ActivatedRoute } from '@angular/router'; import { ERROR_LIST } from '../../../shared/error.constants'; import { IKeyValue } from '../../../shared/modal/key.value'; import CartUtils from '../../../shared/cart.utils'; import * as Constants from '../../../shared/constants'; import { Cart } from '../../../shared/modal/cart'; import { Customer } from '../../../shared/modal/customer'; import { IPackage } from '../../../shared/modal/common'; import { SharedService } from '../../../shared/shared.service'; import { UserService } from '../../../services/user.service'; import { CustomerService } from '../../../services/customer.service'; import { CartService } from '../../../services/cart.service'; @Component( { moduleId: module.id, selector: 'exchange-cmp', templateUrl: 'exchange.component.html', styleUrls: ['exchange.component.css'] }) export class ExchangeComponent implements OnInit, OnChanges, OnDestroy { private COMPONENT_NAME: string = 'ExchangeComponent'; packageTypes: string[] = [ Constants.PACKAGE_TYPE_VOICE, Constants.PACKAGE_TYPE_DATA, Constants.PACKAGE_TYPE_PREPAID, Constants.PACKAGE_TYPE_ACCESSORY ]; refTypeExchange: string = Constants.REF_TYPE_EXCHANGE; taxRate: number = 0; expandPackage: boolean = false; @Input() cart: Cart; returnCart: Cart; storeId: number; // update storeid deleteOption: boolean = false; karmaTestResult: any = {}; constructor( public router: Router, public sharedService: SharedService, public userService: UserService, public customerService: CustomerService, 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. * @returns void */ ngOnInit(): void { const METHOD_NAME: string = 'ngOnInit()'; this.sharedService.storeId.subscribe((val: number) => { this.storeId = val; }); } /** * This function is called whenever one or more data-bound input properties change * @returns void */ ngOnChanges(): void { const METHOD_NAME: string = 'ngOnChanges()'; this.sharedService.storeId.subscribe((val: number) => { this.storeId = val; }); if (this.cart && this.cart.refType === Constants.REF_TYPE_EXCHANGE && this.cart.relatedOrderId) { if (CartUtils.hasTaxCalculated(this.cart)) { this.cart.taxRate = CartUtils.getTaxRate(this.cart); } this.returnCart = this.cartService.getReturnCart(); if (!this.returnCart) { this.sharedService.showSpinner(true); // spinner this.cartService.getCartSummary(this.storeId, this.cart.relatedOrderId).subscribe(result => { this.sharedService.showSpinner(false); // spinner this.returnCart = result; this.cartService.setReturnCart(this.returnCart); }, error => { this.sharedService.showSpinner(false); // spinner this.sharedService.showErrorMessage(error, true); console.error('error=>' + error); }); } } } /** * 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.returnCart = null; this.karmaTestResult = null; this.packageTypes = null; } get isFullCartPage(): boolean { const METHOD_NAME: string = 'isFullCartPage()'; if (window.location.pathname.indexOf('fullcart') > -1) { return true; } return false; } /** * This function is used to display current finance type. * @param {string} financeType - contains Loan or Lease type of finance * @returns any */ getDisplayFinanceType(financeType: string): any { const METHOD_NAME: string = 'getDisplayFinanceType()'; if (financeType && financeType === Constants.FINANCE_TYPE_LOAN) { return '-EIP'; } else if (financeType && financeType === Constants.FINANCE_TYPE_LEASE) { return '-JOD'; } return null; } /** * This function is used to get list of packages that matches package type. * @param {string} type - package type * @returns IPackage - list of packages that matches package type */ getPackages(type: string): IPackage[] { const METHOD_NAME: string = 'getPackages()'; return this.cart.packages.filter((pkg) => { return (pkg.packageType === type) }); } /** * This function is used to get list of packages that matches package type. * @param {string} type - package type * @returns IPackage - list of packages that matches package type */ getReturnPackages(type: string): IPackage[] { const METHOD_NAME: string = 'getReturnPackages()'; return this.returnCart.packages.filter((pkg) => { return (pkg.packageType === type) }); } /** * This function is used to get package that is active in the cart. * @param {number} packageId - packageId of the package. * @param {number} lineId - lineId of the package. */ activePackage(packageId: number, lineId: number) { const METHOD_NAME: string = 'activePackage()'; this.cart.activePackageId = packageId; this.cart.activeLineId = lineId; this.cartService.setCart(this.cart); } /** * This function is used to get package that is active in the cart. * @param {number} packageId - packageId of the package. * @param {number} lineId - lineId of the package. */ activeReturnPackage(packageId: number, lineId: number) { const METHOD_NAME: string = 'activeReturnPackage()'; this.returnCart.activePackageId = packageId; this.returnCart.activeLineId = lineId; } /** * This function is used to determine if a package is active in the cart. * @param {number} packageId - packageId of the package. * @param {number} lineId - lineId of the package. * @returns boolean - if a package is active in the cart. */ isActivePackage(packageId: number, lineId: number): boolean { const METHOD_NAME: string = 'isActivePackage()'; let isActive: boolean = false; if (this.cart) { // console.log('active package1: '+ cart.activePackageId +'>>line: '+ cart.activeLineId); if (this.cart.activePackageId === null || this.cart.activeLineId === null) { this.cart.activePackageId = this.cart.packages[0].packageId; this.cart.activeLineId = this.cart.packages[0].lineId; this.cartService.setCart(this.cart); } // console.log('active package2: '+ cart.activePackageId +'>>line: '+ cart.activeLineId); isActive = this.cart.activePackageId === packageId && this.cart.activeLineId === lineId; // console.log('isActive>>>1>: '+ isActive) if (!isActive) { const packages: IPackage[] = this.cart.packages.filter((item) => { return item.packageId === this.cart.activePackageId && item.lineId === this.cart.activeLineId }); if (!packages || packages.length === 0) { this.cart.activePackageId = this.cart.packages[0].packageId; this.cart.activeLineId = this.cart.packages[0].lineId; this.cartService.setCart(this.cart); } isActive = this.cart.activePackageId === packageId && this.cart.activeLineId === lineId; // console.log('isActive>>>2>: '+ isActive) } } return isActive; } isReturnActivePackage(packageId: number, lineId: number): boolean { const METHOD_NAME: string = 'isReturnActivePackage()'; let isActive: boolean = false; // console.log('is active this package: ' + packageId + '>>line: ' + lineId); if (this.returnCart) { // console.log('active package1: '+ cart.activePackageId +'>>line: '+ cart.activeLineId); if (this.returnCart.activePackageId === null || !this.returnCart.activeLineId === null) { this.returnCart.activePackageId = this.returnCart.packages[0].packageId; this.returnCart.activeLineId = this.returnCart.packages[0].lineId; } // console.log('active package2: '+ cart.activePackageId +'>>line: '+ cart.activeLineId); isActive = this.returnCart.activePackageId === packageId && this.returnCart.activeLineId === lineId; // console.log('isActive>>>1>: '+ isActive) if (!isActive) { const packages: IPackage[] = this.returnCart.packages.filter((item) => { return item.packageId === this.returnCart.activePackageId && item.lineId === this.returnCart.activeLineId }); if (!packages || packages.length === 0) { this.returnCart.activePackageId = this.returnCart.packages[0].packageId; this.returnCart.activeLineId = this.returnCart.packages[0].lineId; } isActive = this.returnCart.activePackageId === packageId && this.returnCart.activeLineId === lineId; // console.log('isActive>>>2>: '+ isActive) } } return isActive; } /** * This function is used to detrmine if package added to cart is valid. * @param {IPackage} packge - contains package information * @returns boolean - if package is valid. */ isValidPackage(packge: IPackage): boolean { const METHOD_NAME: string = 'isValidPackage()'; if (packge && packge.packageType) { if (packge.packageType === Constants.PACKAGE_TYPE_VOICE || packge.packageType === Constants.PACKAGE_TYPE_DATA) { if (this.cart.refType === Constants.REF_TYPE_EXCHANGE) { return !packge.device; } else { return (!packge.plan || !packge.device); } } else if (packge.packageType === Constants.PACKAGE_TYPE_PREPAID) { return (!packge.device); } if (packge.packageType === Constants.PACKAGE_TYPE_ACCESSORY) { return (!packge.accessories); } } return true; } /** * this function is used to calculate the due monthly. * @returns number */ get totalDueMonthly(): number { const METHOD_NAME: string = 'totalDueMonthly()'; return CartUtils.totalDueMonthly(this.cart, true); } /** * this function is used to calculate the estimated today. * @returns number */ get totalProductsDueToday(): number { const METHOD_NAME: string = 'totalProductsDueToday()'; return CartUtils.totalProductsDueToday(this.cart); } /** * this function is used to calculate the estimated tax. * @returns number */ get estTotalTaxDueToday(): number { const METHOD_NAME: string = 'estTotalTaxDueToday()'; return (this.cart) ? CartUtils.estTotalTaxDueToday(this.cart, this.cart.taxRate) : 0; } /** * this function is used to calculate the estimated total due today. * @returns number */ get estTotalDueToday(): number { const METHOD_NAME: string = 'estTotalDueToday()'; return CartUtils.estTotalDueToday(this.cart); } /** * This function is used to delete the device . * @param {number} packageId - packageId of the package. * @param {number} lineId - lineId of the package * @param {number} orderItemId - orderItemId of the package. * @returns void */ deleteDevice(packageId: number, lineId: number, orderItemId: number): void { const METHOD_NAME: string = 'deleteDevice()'; this.sharedService.showSpinner(true); // spinner this.sharedService.addLogging(this.COMPONENT_NAME, METHOD_NAME, 'Deleting a device fromm cart: ' + this.cart.orderId); this.cartService.deleteDevice( this.storeId, this.cart.orderId, packageId, lineId, orderItemId ).subscribe(result => { this.karmaTestResult = result; this.sharedService.showSpinner(false); // spinner this.cart = CartUtils.replaceRequired(this.cart, result); this.cartService.setCart(this.cart); }, error => { this.sharedService.showSpinner(false); // spinner this.sharedService.showErrorMessage(error, true); }); } /** * This function is used to delete the accessory . * @param {number} packageId - packageId of the package. * @param {number} lineId - lineId of the package * @param {number} orderItemId - orderItemId of the package. * @returns void */ deleteAccessory(packageId: number, lineId: number, orderItemId: number): void { const METHOD_NAME: string = 'deleteAccessory()'; this.sharedService.addLogging(this.COMPONENT_NAME, METHOD_NAME, 'Deleting a accessory fromm cart: ' + this.cart.orderId); this.sharedService.showSpinner(true); // spinner this.cartService.deleteAccessory( this.storeId, this.cart.orderId, packageId, lineId, orderItemId ).subscribe(result => { this.karmaTestResult = result; this.sharedService.showSpinner(false); // spinner this.cart = CartUtils.replaceRequired(this.cart, result); this.cartService.setCart(this.cart); }, error => { this.sharedService.showSpinner(false); // spinner this.sharedService.showErrorMessage(error, true); }); } /** * This function is used to sync the cart updates * @param {number} orderId - orderId of the user * @returns void */ syncCartUpdates(orderId: number): void { const METHOD_NAME: string = 'syncCartUpdates()'; this.sharedService.showSpinner(true); // spinner this.cartService.getCartSummary(this.storeId, orderId).subscribe(result => { this.sharedService.showSpinner(false); // spinner this.karmaTestResult = result; // making sure active package/line shouldn't be remove let cart: Cart = result; if (cart && cart.packages) { const activePackageId = this.cart.activePackageId; const activeLineId = this.cart.activeLineId; if (activePackageId && activeLineId) { let activePackage: IPackage; cart.packages.forEach((item) => { if (item.packageId === activePackageId && item.lineId === activeLineId) { activePackage = item; } }); if (activePackage) { cart.activePackageId = activePackage.packageId; cart.activeLineId = activePackage.lineId; cart = CartUtils.updatePackage(cart, activePackage); } } } this.cartService.setCart(cart); }, error => { this.sharedService.showSpinner(false); // spinner this.sharedService.showErrorMessage(error, true); }); } /** * This function is used to update cart summary. * @param {number} orderId - orderId of the user * @returns void */ updateCartSummary(orderId: number): void { const METHOD_NAME: string = 'updateCartSummary()'; this.sharedService.showSpinner(true); // spinner this.cartService.getCartSummary(this.storeId, orderId).subscribe(result => { this.sharedService.showSpinner(false); // spinner this.karmaTestResult = result; this.cart = CartUtils.replaceRequired(this.cart, result); this.cartService.setCart(this.cart); }, error => { this.sharedService.showSpinner(false); // spinner this.sharedService.showErrorMessage(error, true); }); } convertFullPrice(orderId: number): void { const METHOD_NAME: string = 'convertFullPrice()'; this.sharedService.showSpinner(true); // spinner this.cartService.convertFullPrice(this.storeId, orderId, {}).subscribe(result => { this.karmaTestResult = result; this.sharedService.showSpinner(false); // spinner this.updateCartSummary(orderId); }, error => { this.sharedService.showSpinner(false); // spinner this.sharedService.showErrorMessage(error, true); }); } /** * This function is used to checkout existing cart. * @returns void */ checkoutNow(): void { const METHOD_NAME: string = 'checkoutNow()'; console.log('MinicartComponent.checkoutNow().'); if (this.cart && this.cart.packages) { this.sharedService.addLogging(this.COMPONENT_NAME, METHOD_NAME, 'Checkout from mini cart: ' + this.cart.orderId); // clearing order details from local storage this.sharedService.showSpinner(true); // spinner this.cartService.validateCart(this.storeId, this.cart.orderId, {}).subscribe(result => { this.karmaTestResult = result; this.sharedService.showSpinner(false); // spinner if (result.readyForCheckout) { // clearing order details from local storage this.sharedService.showSpinner(true); // spinner const isFinanceOrder: boolean = CartUtils.isFinanceOrder(this.cart); if (isFinanceOrder) { const inputData: any = {actions: 'EIPESTIMATE'}; // tslint:disable-next-line:no-shadowed-variable this.cartService.calculate(this.storeId, this.cart.orderId, inputData).subscribe(result => { this.karmaTestResult = result; this.sharedService.showSpinner(false); // spinner const hostName = window.location.hostname; if (!isFinanceOrder || (hostName != null && hostName.includes('training'))) { this.router.navigate(['checkout']); } else { const output: any = result.output; if (output && output.EIPESTIMATE && output.EIPESTIMATE.status) { if (output.EIPESTIMATE.status === Constants.EIP_ESTIMATE_STATUS) { this.router.navigate(['checkout']); } else { this.sharedService.showErrorMessage('_ERR_EIP_ESTIMATE_ERROR', false); this.convertFullPrice(this.cart.orderId); } } else { this.router.navigate(['checkout']); } } }, error => { const hostName = window.location.hostname; if (!isFinanceOrder || (hostName != null && hostName.includes('training'))) { this.router.navigate(['checkout']); } this.sharedService.showSpinner(false); // spinner this.sharedService.showErrorMessage(error, true); }); } else { this.router.navigate(['checkout']); } } else { const errorMessages: any = ERROR_LIST.filter((errorCode) => { return errorCode.key === result.failureReason; }); if (errorMessages && errorMessages.length > 0) { this.sharedService.showErrorMessage(errorMessages[0].key, false); } else { this.sharedService.showErrorMessage('_ERR_INVAID_CART', false); } } }, error => { this.sharedService.showSpinner(false); // spinner this.sharedService.showErrorMessage(error, true); }); } else { this.sharedService.showErrorMessage('_ERR_INVAID_CART', false); } } /** * This function is used to display shopping cart if a package exists in cart. * @returns void */ showShoppingCart(): void { const METHOD_NAME: string = 'showShoppingCart()'; if (this.cart) { this.router.navigate(['/home/fullcart']); } else { this.sharedService.showErrorMessage('_ERR_EMPTY_CART', false); } } }