import { Component, Input, Output, EventEmitter, OnInit, OnChanges, OnDestroy } from '@angular/core'; import * as Constants from '../../../../shared/constants'; import Utils from '../../../../shared/utils'; import CartUtils from '../../../../shared/cart.utils'; import CheckoutUtils from '../../../../shared/checkout.utils'; import { ADJUST_PRICE_REASONS, INVENTORY_STATUS } from '../../../../shared/form.constants'; import { IPackage, IAccessory, IAccessoryItem, IOrderItemAccessory } from '../../../../shared/modal/common'; import { IKeyValue } from '../../../../shared/modal/key.value'; import { Cart } from '../../../../shared/modal/cart'; import { Customer, ICreditProfile } from '../../../../shared/modal/customer'; import { SharedService } from '../../../../shared/shared.service'; import { UserService } from '../../../../services/user.service'; import { CartService } from '../../../../services/cart.service'; import { User } from '../../../../shared/modal/user'; @Component({ selector: 'accessory-cmp', moduleId: module.id, templateUrl: 'accessory.component.html' }) export class AccessoryComponent implements OnInit, OnChanges, OnDestroy { private COMPONENT_NAME: string = 'AccessoryComponent'; storeId: number; // update storeid rtStoreId: number = Constants.STORE_ID_RT; loanCrp: number = Constants.LOAN_CRP_DEFAULT; leaseCrp: number = Constants.LEASE_CRP_DEFAULT; financeTypeFull: string = Constants.FINANCE_TYPE_FRP; financeTypeLoan: string = Constants.FINANCE_TYPE_LOAN; financeTypeLease: string = Constants.FINANCE_TYPE_LEASE; // eipMinFinanceLimit:number=Constants.MIN_EIP_LIMIT; isEIPAccessoriesMinLimit: boolean = false; imageBaseUrl: string = Constants.IMAGE_BASE_URL; user: User; @Input() cart: Cart; @Input() package: IPackage; @Input() orderItem: IOrderItemAccessory; @Output() onItemChange = new EventEmitter(false); accessory: IAccessory; accessoryItem: IAccessoryItem; isFinanceOrder: boolean = false; isOnlyAccCart: boolean = false; isAALOrder: boolean = false; isNCCustomer: boolean = false; canFinance: boolean = true; isExchangeOrder: boolean = false; isNCFPlanInCart: boolean = false; financeType: string; options: any = []; tradeInDevice: boolean = false; tradeInSuccess: boolean = false; KarmaTestResult: any = {}; dormantFlag: any; 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 function is called whenever one or more data-bound input properties change * and also getAccessoriesByDevice function is called by catalog service to get accesories. * @returns void */ ngOnChanges(): void { const METHOD_NAME: string = 'ngOnChanges()'; this.sharedService.storeId.subscribe((val: number) => { this.storeId = val; }); // getting user info this.user = this.userService.getUser(); this.dormantFlag = this.sharedService.getDormantProperties(); // setting the flag for accessory only order and aal order if (this.cart) { this.isOnlyAccCart = CheckoutUtils.isAccOnlyOrder(this.cart); this.isFinanceOrder = CartUtils.isFinanceOrder(this.cart); this.isNCFPlanInCart = CartUtils.isNCFPlanInCart(this.cart); this.isExchangeOrder = this.cart.refType === Constants.REF_TYPE_EXCHANGE ? true : false; // setting the crp values based on customer profile if (this.cart.customer && this.cart.customer.creditProfile) { const creditProfile: ICreditProfile = this.cart.customer.creditProfile; this.loanCrp = creditProfile.loanCrp ? creditProfile.loanCrp : Constants.LOAN_CRP_DEFAULT; this.leaseCrp = creditProfile.leaseCrp ? creditProfile.leaseCrp : Constants.LEASE_CRP_DEFAULT; if (!this.isExchangeOrder && creditProfile.accountNumber) { this.isAALOrder = true; this.canFinance = (!creditProfile.isExtendedPaySchedule && !creditProfile.hideFinance) ? true : false; } if (this.isOnlyAccCart && this.isAALOrder && this.cart.customer.creditType === 'NC') { this.isNCCustomer = true; } } } if (this.orderItem) { // mae sure order item available this.financeType = this.orderItem.financeType; this.accessory = Utils.getAccessoryParent(this.orderItem.accessorySKU); this.isEIPAccessoriesMinLimit = (this.orderItem.financeType === Constants.FINANCE_TYPE_LOAN && this.orderItem.finalPrice < Constants.MIN_EIP_LIMIT) ? true : false; if (this.accessory) { const items: any = this.accessory.items.filter((item) => { return item.accessorySKU === this.orderItem.accessorySKU; }); if (items && items.length > 0) { this.options = new Array(); this.accessoryItem = items[0]; this.options.push({ key: Constants.FINANCE_TYPE_FRP, value: Constants.FULL_PRICE }); if (!this.isFinanceOrder) { if (this.accessoryItem.loan != null && this.accessoryItem.loan.length > 0) { this.options.push({ key: Constants.FINANCE_TYPE_LOAN, value: Constants.PRICING_OPTION }); } if (this.accessoryItem.lease != null && this.accessoryItem.lease.length > 0) { this.options.push({ key: Constants.FINANCE_TYPE_LEASE, value: Constants.JUMP_ON_DEMAND }); } } else { if (CartUtils.isEIPOnlyOrder(this.cart)) { if (this.accessoryItem.loan != null && this.accessoryItem.loan.length > 0) { this.options.push({ key: Constants.FINANCE_TYPE_LOAN, value: Constants.PRICING_OPTION }); } } else if (CartUtils.isJUMPOnlyOrder(this.cart)) { if (this.accessoryItem.lease != null && this.accessoryItem.lease.length > 0) { this.options.push({ key: Constants.FINANCE_TYPE_LEASE, value: Constants.JUMP_ON_DEMAND }); } } } } } } } /** * 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.accessory = null; this.accessoryItem = null; this.user = null; this.cart = null; this.package = null; this.orderItem = null; this.options = null; this.KarmaTestResult = null; } /** * This function is used to switch finance type by calling switchAccessoryFinanceInput function. * @returns void */ switchAccessoryFinance(): void { const METHOD_NAME: string = 'switchAccessoryFinance()'; if (this.orderItem.financeType !== this.financeType) { this.sharedService.addLogging(this.COMPONENT_NAME, METHOD_NAME, 'Updating accessory in shopping cart'); const packageType: string = this.package.packageType; const crp: number = this.financeType && this.financeType === Constants.FINANCE_TYPE_LOAN ? this.loanCrp : this.leaseCrp; const inputData: any = CartUtils.switchAccessoryFinanceInput(packageType, this.accessoryItem, this.financeType, crp); // setting original order item# inputData.orderItemId = this.orderItem.orderItemId; this.sharedService.showSpinner(true); // spinner this.cartService.updateAccessory(this.storeId, this.cart.orderId, this.package.packageId, this.package.lineId, this.orderItem.orderItemId, inputData ).subscribe(result => { this.KarmaTestResult = result; this.sharedService.showSpinner(false); // spinner console.log('Successfully updated the accessory.'); this.getCartSummary(); }, error => { this.financeType = this.orderItem.financeType; this.sharedService.showSpinner(false); // spinner this.sharedService.showErrorMessage(error, true); console.error('error=>' + error); }); } console.log('evt>>>>' + this.financeType); } /** * 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.sharedService.showSpinner(false); // spinner this.cart = CartUtils.replaceRequired(this.cart, result); // this.errorMessage = this.sharedService.showErrorDownPaymentAdjusted(this.cart.isDownPaymentAdjusted); this.cartService.setCart(this.cart); if (CartUtils.isFinanceOrder(this.cart)) { this.onItemChange.emit(true); } }, error => { this.sharedService.showSpinner(false); // spinner this.sharedService.showErrorMessage(error, true); }); } /** * This function is used to get the inventory status * @param {string} status - variable contains the item status * @return string */ getInventoryStatus(status: string): string { const METHOD_NAME: string = 'getInventoryStatus()'; const statusItems: IKeyValue[] = INVENTORY_STATUS.filter((item) => { return item.key === status; }); if (statusItems && statusItems.length > 0) { return statusItems[0].value; } return Constants.OUT_OF_STOCK; } }