/** * @Component Name: FulcartComponent * @Module: Home * @Module Functionality : To display the full cart section * @Creation Date: August 12,2017 * @Author: sbaireddy * @Version: 1.0 */ import { Component, Input, OnInit, OnDestroy, ViewChild, Output, EventEmitter } from '@angular/core'; import { Router, ActivatedRoute } from '@angular/router'; import { NgClass } from '@angular/common'; import * as Constants from '../../../shared/constants'; import Utils from '../../../shared/utils'; import CartUtils from '../../../shared/cart.utils'; import { ERROR_LIST } from '../../../shared/error.constants'; import { ADJUST_PRICE_REASONS } from '../../../shared/form.constants'; import { IPackage, IDeviceItem, IAccessoryItem, IPromotion } from '../../../shared/modal/common'; import { User } from '../../../shared/modal/user'; import { Cart } from '../../../shared/modal/cart'; import { Customer } from '../../../shared/modal/customer'; import { SharedService } from '../../../shared/shared.service'; import { UserService } from '../../../services/user.service'; import { CartService } from '../../../services/cart.service'; import { CustomerService } from '../../../services/customer.service'; import { TradeinService } from '../../../services/tradein.service'; import { MarketingService } from '../../../services/marketing.service'; import { PayExtraService } from './../../../shared/pay-extra-modal.service'; import { restUrl } from 'app/rest.url'; import { error } from 'selenium-webdriver'; @Component({ selector: 'roomy-cmp', moduleId: module.id, templateUrl: 'roomy.component.html', styleUrls: ['roomy.component.css'] }) export class RoomyComponent implements OnInit, OnDestroy { private COMPONENT_NAME: string = 'RoomyComponent'; storeId: number; // update storeid showMinicart: boolean = false; packageTypes: string[] = [ Constants.PACKAGE_TYPE_VOICE, Constants.PACKAGE_TYPE_DATA, Constants.PACKAGE_TYPE_PREPAID, Constants.PACKAGE_TYPE_ACCESSORY ]; refTypeExchange: string = Constants.REF_TYPE_EXCHANGE; user: User; cart: any; customer: Customer; subscription: any; promotions: IPromotion[]; promoModel: any = { 'promoCode': '' }; isFinanceOrder: boolean = false; isValidCart: boolean = false; isEIPOnlyOrder: boolean = false; isValidPromo: boolean = false; karmaTestResult: any = {}; errorMessage: string; switchView: boolean = false; @Output() onViewChange = new EventEmitter(); @ViewChild('payExtraPopup') payExtraPopup: any; rtStoreId: number = Constants.STORE_ID_RT; // canOverrideRemorse constructor( public router: Router, public sharedService: SharedService, public userService: UserService, public cartService: CartService, public tradeinService: TradeinService, public customerService: CustomerService, public marketingService: MarketingService, private payExtraService: PayExtraService) { } /** * 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.addLogging(this.COMPONENT_NAME, METHOD_NAME, 'Display shopping cart'); this.sharedService.updateErrorMessage(null); this.sharedService.storeId.subscribe((val: number) => { this.storeId = val; }); this.sharedService.showMincartStatus.subscribe((val: boolean) => { this.showMinicart = val; }); // getting user info this.user = this.userService.getUser(); this.cart = this.cartService.getCart(); this.customer = (this.cart && this.cart.customer) ? this.cart.customer: null; this.subscription = this.cartService.hasUpdate.subscribe((item) => { this.cart = Object.assign(this.cart, this.cartService.getCart()); this.isFinanceOrder = CartUtils.isFinanceOrder(this.cart); this.isEIPOnlyOrder = CartUtils.isEIPOnlyOrder(this.cart); }); if (this.cart) { // if(CartUtils.hasTaxCalculated(this.cart)) { // this.cart.taxRate= CartUtils.getTaxRate(this.cart); // } if (this.cart.packages) { for (const item of this.cart.packages) { item.showPackage = false; } this.cart.packages[0].showPackage = true; // making first element as expandable } this.isFinanceOrder = CartUtils.isFinanceOrder(this.cart); this.isEIPOnlyOrder = CartUtils.isEIPOnlyOrder(this.cart); // validating cart either it vaild or not this.calculateDiscounts(); // getting the applied promotions this.getAppliedPromos(this.cart.orderId); if (CartUtils.isTradeinCart(this.cart)) { let allTradeInfoList = this.tradeinService.getTradeInInfo(); if (allTradeInfoList.length === 0) { this.sharedService.getAllTradeinInfo(this.storeId, this.cart.orderId); } } else { this.tradeinService.saveTradeInInfo(null, null); } } } /** * Cleanup just before Angular destroys the directive/component. * Unsubscribe Observables and detach event handlers to avoid memory leaks. * @returns void */ ngOnDestroy() { const METHOD_NAME: string = 'ngOnDestroy()'; this.cart = null; this.user = null; this.subscription.unsubscribe(); this.karmaTestResult = null; this.packageTypes = null; this.promotions = null; this.promoModel = 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 validate cart and updates the finance and discounts * @param {number} orderId - order number * @returns {void} */ calculateDiscounts(): void { const METHOD_NAME: string = 'calculateDiscounts()'; let reqParams: any = { validateCreditCheck:true }; if(this.storeId == Constants.STORE_ID_RT) { reqParams.validateCreditCheck = false; } this.sharedService.showSpinner(true); // spinner this.cartService.validateCart(this.storeId, this.cart.orderId, reqParams).subscribe(result => { this.karmaTestResult = result; this.sharedService.showSpinner(false); // spinner if (result && result.readyForCheckout) { this.isValidCart = true; let inputData: any = { actions: 'SYNCELIGIBLEDISCOUNTS' }; if (!this.customer && this.cart && this.cart.customer) { this.customer = this.cart.customer; } if (this.isFinanceOrder && this.customer && this.customer.creditProfile && this.customer.creditProfile.creditClass) { inputData = { actions: 'SYNCELIGIBLEDISCOUNTS,EIPESTIMATE' }; } this.sharedService.showSpinner(true); // spinner this.cartService.calculate(this.storeId, this.cart.orderId, inputData).subscribe(calculationResult => { this.karmaTestResult = calculationResult; this.sharedService.showSpinner(false); // spinner this.getCartSummary(); }, error => { this.sharedService.showSpinner(false); // spinner this.sharedService.showErrorMessage(error, true); console.error('error=>' + error); }); } else { this.refreshCartSummary(); } }, error => { this.sharedService.showSpinner(false); // spinner this.sharedService.showErrorMessage(error, true); console.error('error=>' + error); }); } /** * This function is used to update cart details as when cart is updated * @param {number} orderId - orderId * @returns void */ refreshCartSummary(): void { const METHOD_NAME: string = 'refreshCartSummary()'; if (this.isFinanceOrder && this.isValidCart) { this.sharedService.showSpinner(true); // spinner // this.errorMessage = this.sharedService.showErrorDownPaymentAdjusted(this.cart.isDownPaymentAdjusted); if (this.cart.isDownPaymentAdjusted) { this.sharedService.showErrorMessage('_ERR_ERROR_ECALIMIT', false); } this.cartService.calculate(this.storeId, this.cart.orderId, { actions: 'EIPESTIMATE' }).subscribe(result => { this.karmaTestResult = result; this.sharedService.showSpinner(false); // spinner this.getCartSummary(); }, error => { this.sharedService.showSpinner(false); // spinner // this.sharedService.showErrorMessage(error, true); console.error('error=>' + error); }); } else { this.getCartSummary(); } } isPromoValid(): void { if (this.promoModel.promoCode && this.promoModel.promoCode.length > 0) { this.isValidPromo = true; } else { this.isValidPromo = false; } } /** * This function is used to get cart details * @param {number} orderId - orderId * @returns void */ getCartSummary(): void { this.sharedService.showSpinner(true); // spinner this.cartService.getCartSummary(this.storeId, this.cart.orderId).subscribe(result => { this.karmaTestResult = result; this.sharedService.showSpinner(false); // spinner this.cart = CartUtils.replaceRequired(this.cart, result); // this.errorMessage = this.sharedService.showErrorDownPaymentAdjusted(this.cart.isDownPaymentAdjusted); if (this.cart.isDownPaymentAdjusted) { this.sharedService.showErrorMessage('_ERR_ERROR_ECALIMIT', false); } this.cartService.setCart(this.cart); }, error => { this.sharedService.showSpinner(false); // spinner this.sharedService.showErrorMessage(error, true); }); } /** * This function is used to apply promo code to avail offer on products * @returns void */ applyPromotion(): void { const METHOD_NAME: string = 'applyPromotion()'; this.sharedService.addLogging(this.COMPONENT_NAME, METHOD_NAME, 'Applying promotion'); if (this.promoModel.promoCode && this.promoModel.promoCode.length > 0) { if (this.cart && this.cart.packages) { const inputData: any = { orderId: this.cart.orderId, promoCode: this.promoModel.promoCode }; this.sharedService.showSpinner(true); // spinner this.cartService.applyPromotion(this.storeId, this.cart.orderId, inputData).subscribe(result => { if (result.promoCode) { this.isValidPromo = false; } else { this.isValidPromo = true; } this.karmaTestResult = result; this.sharedService.showSpinner(false); // spinner this.promoModel.promoCode = ''; this.refreshCartSummary(); this.getAppliedPromos(this.cart.orderId); }, error => { this.sharedService.showSpinner(false); // spinner this.sharedService.showErrorMessage(error, true); console.error('error=>' + error) }); } else { this.sharedService.showErrorMessage('_ERR_INVAID_CART', false); } } else { this.sharedService.showErrorMessage('_ERR_INVALID_PROMO_CODE', false); } } /** * This function is used to delete promo code applied * @returns void */ deletePromotion(promoCode: string): void { const METHOD_NAME: string = 'deletePromotion()'; this.sharedService.addLogging(this.COMPONENT_NAME, METHOD_NAME, 'Delete promotion'); if (this.cart && this.cart.packages) { this.sharedService.showSpinner(true); // spinner this.cartService.deletePromotion(this.storeId, this.cart.orderId, promoCode).subscribe(result => { this.karmaTestResult = result; this.sharedService.showSpinner(false); // spinner this.promoModel.promoCode = ''; this.refreshCartSummary(); this.getAppliedPromos(this.cart.orderId); }, error => { this.sharedService.showSpinner(false); // spinner this.sharedService.showErrorMessage(error, true); console.error('error=>' + error) }); } else { this.sharedService.showErrorMessage('_ERR_INVAID_CART', false); } } /** * This function is used to get list of promos applied on the products. * @param {number} orderId - orderId of the user * @returns void */ getAppliedPromos(orderId: number): void { const METHOD_NAME: string = 'getAppliedPromos()'; console.log('OrderDetailComponent.showOrderPromo()>>Start.'); this.sharedService.showSpinner(true); // spinner this.cartService.getCartPromo(this.storeId, orderId).subscribe(result => { this.sharedService.showSpinner(false); // spinner this.promotions = result; }, error => { this.sharedService.showSpinner(false); // spinner this.sharedService.showErrorMessage(error, true); console.error('error=>' + error) }); } /** * This function is used to checkout the existing cart. * @returns void */ checkoutNow(): void { const METHOD_NAME: string = 'checkoutNow()'; console.log('MinicartComponent.showCartSummary()>>checkoutNow.'); if (this.isValidCart && this.sharedService.getTradeInValidate()) { this.sharedService.addLogging(this.COMPONENT_NAME, METHOD_NAME, 'Checkout from shopping cart:' + this.cart.orderId); let inputData: any = { actions: 'SYNCELIGIBLEDISCOUNTS' }; if (!this.customer && this.cart && this.cart.customer){ this.customer = this.cart.customer; } if (this.isFinanceOrder && this.customer && this.customer.creditProfile && this.customer.creditProfile.creditClass) { inputData = { actions: 'SYNCELIGIBLEDISCOUNTS,EIPESTIMATE' }; } this.sharedService.showSpinner(true); // spinner this.cartService.calculate(this.storeId, this.cart.orderId, inputData).subscribe(result => { this.sharedService.showSpinner(false); // spinner const hostName = window.location.hostname; if (!this.isFinanceOrder || (hostName != null && hostName.includes('training')) || (inputData && inputData.actions && inputData.actions === 'SYNCELIGIBLEDISCOUNTS')) { if(this.storeId === Constants.STORE_ID_RT) { this.router.navigate(['estimate']); } else { this.router.navigate(['checkout']); } } else { const output: any = result.output; if (output && output['EIPESTIMATE.status'] === Constants.EIP_ESTIMATE_STATUS) { if (this.storeId === Constants.STORE_ID_RT) { this.router.navigate(['estimate']); } else { this.router.navigate(['checkout']); } } else { this.sharedService.showSpinner(true); // spinner this.cartService.convertFullPrice(this.storeId, this.cart.orderId, {}).subscribe(convertFullPriceResult => { this.karmaTestResult = convertFullPriceResult; this.sharedService.showSpinner(false); // spinner this.sharedService.showErrorMessage('_ERR_EIP_ESTIMATE_ERROR', false); this.isFinanceOrder = false; this.refreshCartSummary(); }, error => { this.sharedService.showSpinner(false); // spinner this.sharedService.showErrorMessage(error, true); }); } } }, error => { const hostName = window.location.hostname; if (!this.isFinanceOrder || (hostName != null && hostName.includes('training'))) { if (this.storeId === Constants.STORE_ID_RT) { this.router.navigate(['estimate']); } else { this.router.navigate(['checkout']); } } this.sharedService.showSpinner(false); // spinner this.sharedService.showErrorMessage(error, true); console.error('error=>' + error) }); } else { if (!this.sharedService.getTradeInValidate()) { this.sharedService.showErrorMessage('_ERR_SERACH_QUATION_TRADEIN', false); } else { this.sharedService.showErrorMessage('_ERR_INVAID_CART', false); } } } /** * this function is used to calculate the total products due today. * @returns number */ get totalProductsDueToday(): number { return CartUtils.totalProductsDueToday(this.cart); } /** * this function is used to calculate the total service due monthly. * @returns number */ get totalServicesDueMonthly(): number { return CartUtils.totalServicesDueMonthly(this.cart); } /** * this function is used to calculate the total finance due monthly. * @returns number */ get totalFinancesDueMonthly(): number { return CartUtils.totalFinancesDueMonthly(this.cart); } /** * this function is used to calculate the total discount due monthly. * @returns number */ get totalDiscountsDueMonthly(): number { return CartUtils.totalDiscountsDueMonthly(this.cart, true); } /** * this function is used to calculate the total discounts due today. * @returns number */ get totalDiscountsDueToday(): number { return CartUtils.totalDiscountsDueToday(this.cart); } /** * this function is used to calculate the total deposit due today. * @returns number */ get totalDepositsDueToday(): number { return CartUtils.totalDepositsDueToday(this.cart); } /** * this function is used to calculate the total shipping due today. * @returns number */ get totalShippingDueToday(): number { return CartUtils.totalShippingDueToday(this.cart); } /** * this function is used to calculate the estimated tax. * @returns number */ get estTotalTaxDueToday(): number { const METHOD_NAME: string = 'estTotalTaxDueToday()'; return CartUtils.estTotalTaxDueToday(this.cart, this.cart.taxRate); } /** * this function is used to calculate the total due monthly. * @returns number */ get totalDueMonthly(): number { return CartUtils.totalDueMonthly(this.cart, true); } /** * this function is used to calculate the total due today including taxes. * @returns number */ get estTotalDueToday(): number { const METHOD_NAME: string = 'estTotalDueToday()'; return CartUtils.estTotalDueToday(this.cart); } /** * This function is used to get line name based on line type. * @param {string} type - line type * @returns Object */ getLineName(pack: IPackage): Object { const METHOD_NAME: string = 'getLineName()'; return CartUtils.getLineName(pack); } doubleEIPEcl(): void { const METHOD_NAME: string = 'doubleEIPEcl()'; this.sharedService.showSpinner(true); // spinner this.customerService.doubleEIPEcl(this.storeId, this.cart.orderId, {}).subscribe(result => { this.karmaTestResult = result; this.sharedService.showSpinner(false); // spinner if (this.cart && this.cart.customer) { this.cart.customer = Object.assign(this.cart.customer, result); this.cartService.setCart(this.cart); } }, error => { this.sharedService.showSpinner(false); // spinner this.sharedService.showErrorMessage(error, true); }); } /** * This function is used to clear products added to cart. * @param {any} clearProductsModal - Popup that is displayed when clear products link is clicked * @returns void */ clearProducts(clearProductsModal: any): void { const METHOD_NAME: string = 'clearProducts()'; this.sharedService.addLogging(this.COMPONENT_NAME, METHOD_NAME, 'Clearig the products from cart:' + this.cart.orderId); this.sharedService.showSpinner(true); // spinner this.cartService.clearProducts(this.storeId, this.cart.orderId).subscribe(result => { this.karmaTestResult = result; // this.sharedService.showSpinner(false);//spinner clearProductsModal.hide(); // this.sharedService.showSpinner(true);//spinner this.cartService.getCartSummary(this.storeId, this.cart.orderId).subscribe(getCartSummaryResult => { this.sharedService.showSpinner(false); // spinner this.cart = Object.assign(this.cart, getCartSummaryResult); this.cartService.setCart(this.cart); this.router.navigate(['home']); }, error => { this.sharedService.showSpinner(false); // spinner this.sharedService.showErrorMessage(error, true); }); }, error => { this.sharedService.showSpinner(false); // spinner this.sharedService.showErrorMessage(error, true); }); } sendQuoteToCustomer(): void { if (this.cart && this.cart.customer && this.cart.customer.email1) { const inputData: any = {}; inputData.orderId = this.cart.orderId; inputData.logonId = this.user.logonId; inputData.email = this.cart.customer.email1; inputData.customerName = this.cart.customer.firstName ? this.cart.customer.firstName : 'Customer'; this.sharedService.showSpinner(true); // spinner this.marketingService.sendQuote(this.storeId, inputData).subscribe(result => { this.sharedService.showSpinner(false); // spinner }, error => { this.sharedService.showSpinner(false); // spinner this.sharedService.showErrorMessage(error, true); console.error('error=>' + error) }); } else { this.sharedService.showErrorMessage('_ERR_EMAIL_NO_FOUND', false); } } payExtraModalToggle(status: string){ this.payExtraService.cartInfo = this.cart; this.payExtraService.isValidCart = this.isValidCart; this.payExtraService.setMessage(status); } }