/** * @Component Name: MIDevicesComponent * @Module: Home * @Module Functionality : To display the mi devices * @Creation Date: August 12,2017 * @Author: sbaireddy * @Version: 1.0 */ import { Component, ChangeDetectionStrategy, OnInit, OnDestroy , ViewChild } from '@angular/core'; import Utils from '../../../shared/utils'; import CartUtils from '../../../shared/cart.utils'; import * as Constants from '../../../shared/constants'; import { SORT_BY_OPTIONS, INVENTORY_STATUS } from '../../../shared/form.constants'; import { IKeyValue } from '../../../shared/modal/key.value'; import { IDevice, IDeviceItem, IFilterList, IFilter, IPackage } from '../../../shared/modal/common'; import { User } from '../../../shared/modal/user'; import { Cart } from '../../../shared/modal/cart'; import { Customer, ICreditProfile } from '../../../shared/modal/customer'; import { SharedService } from '../../../shared/shared.service'; import { CartService } from '../../../services/cart.service'; import { CustomerService } from '../../../services/customer.service'; import { CatalogService } from '../../../services/catalog.service'; import { UserService } from '../../../services/user.service'; import { CheckoutService } from '../../../services/checkout.service'; import { MarketingService } from '../../../services/marketing.service'; import { CompareService } from '../../../shared/compare-modal.service'; import { MoreDetailsService } from './../../../shared/more-details-modal.service'; declare var jQuery: any; @Component( { moduleId: module.id, selector: 'mi-devices-cmp', templateUrl: 'mi-devices.component.html', styleUrls: ['mi-devices.component.css'], // changeDetection: ChangeDetectionStrategy.OnPush }) export class MIDevicesComponent implements OnInit, OnDestroy { private COMPONENT_NAME: string = 'MIDevicesComponent'; storeId: number; // update storeid categoryId: number = Constants.CATEGORY_ID_DEVICE; creditType: string = Constants.DEFAULT_CREDIT; 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; groupByCount: number = jQuery(window).width() <= 991 ? 2 : 3; cardsPerPage: number = Constants.CARDS_PER_PAGE; // filters expand and collapse flags showBrandFilter: boolean = false; showDeviceTypeFilter: boolean = false; showFeatureFilter: boolean = false; // header section veriables financeType: string = Constants.FINANCE_TYPE_FRP; // for finance type slider searchText: string = ''; // search text miSuggestions: Array = []; suggestionItems: Array = []; sortByKey: string = ''; // sort by key sortByOptions: IKeyValue[]; // sort by price and alphabet // body section veriables imageBaseUrl: string = Constants.IMAGE_BASE_URL; devices: IDevice[]; filters: IFilterList[]; deviceBrandFilters: IFilter[]; deviceTypeFilters: IFilter[]; deviceFeatureFilters: IFilter[]; // sorted accessories for display sortedDevices: Array>; // pagination allItems: any[]; // array of all items to be paged pager: any = {};  // pager object pagedItems: any[]; // paged items // added for expand and collapse expandedProductId: number; // adding item to cart functionality packageType: string = Constants.PACKAGE_TYPE_DATA; customer: Customer; // getting customer infomation creditProfile: ICreditProfile; subscription: any; user: User; cart: Cart; // getting active package item package: IPackage; disableAddToCart: boolean = false; isFinanceOrder: boolean = false; isEIPOnlyOrder: boolean = false; isJUMPOnlyOrder: boolean = false; isNCFPlanInCart: boolean = false; isBYOSOnlyOrder: boolean = false; isAALOrder: boolean = false; isExchangeOrder: boolean = false; canFinance: boolean = true; compareProducts: Array = new Array(); bannerName: string = Constants.BANNER_MI_DEVICE; deviceConflicts:any; @ViewChild('deviceConflictExchangeModal') deviceConflictExchangeModal: any; rtStoreId: number = Constants.STORE_ID_RT; activeSIMNumber: string; constructor( public sharedService: SharedService, public cartService: CartService, public customerService: CustomerService, public catalogService: CatalogService, public userService: UserService, public checkoutService: CheckoutService, public marketingService: MarketingService, public compareService: CompareService, public moreDetailsService: MoreDetailsService) { } /** * 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.Here it is initializing all the states, payment methods and card types * @returns void */ ngOnInit(): void { const METHOD_NAME: string = 'ngOnInit()'; // this.sharedService.addLogging(this.COMPONENT_NAME, METHOD_NAME, 'Loading MI devices'); // Start-Switch Channel-Store id update this.sharedService.updateErrorMessage(null); this.sortByOptions = SORT_BY_OPTIONS; if (this.sortByOptions) { this.sortByKey = this.sortByOptions[0].key; } this.sharedService.storeId.subscribe((val: number) => { this.storeId = val; if(this.storeId == this.rtStoreId) this.sortByKey = 'price-desc'; }); this.sharedService.creditChoice.subscribe((val: string) => { this.creditType = val; if ( this.creditType && this.creditType === Constants.NOCREDIT_CHECK) { this.financeType = Constants.FINANCE_TYPE_FRP; } this.updateActivePackage(); }); this.groupByCount = Utils.groupByProductCount(); this.cardsPerPage = Utils.totalCardsPerPage(); this.user = this.userService.getUser(); // getting current cart/customer this.customer = this.customerService.getCustomer(); this.cart = this.cartService.getCart(); // getting cart changes... this.subscription = this.cartService.hasUpdate.subscribe((item) => { if (item) { this.cart = this.cartService.getCart(); } this.updateActivePackage(); }); console.log('getting the master devices'); this.devices = this.catalogService.getMIDevices(); if (!this.devices) { console.log('MIDevicesComponent.ngOnInit()>>Getting the master devices.'); this.sharedService.initializeCatalog(); } else { this.devices.forEach(device => { device.itemIndex = 0; if (device.items) { device.items[device.itemIndex].sim = null; } }); } console.log('getting the master device filters'); this.filters = this.catalogService.getMIDeviceFiletrs(); if (!this.filters) { console.log('MIDevicesComponent.ngOnInit()>>Getting the master device filetrs.'); this.sharedService.initializeCatalog(); } else { this.resetDeviceFilters(); } // select active package this.updateActivePackage(); // filter based on rateplan this.filterByRateplan(); jQuery(document).click(function (e) { if (e.target.id === 'search1') { jQuery('#suggestions').show(); } else { jQuery('#suggestions').hide(); } }); } /** * 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.devices = null; this.filters = null; this.customer = null; this.cart = null; this.package = null; this.pager = null; this.deviceBrandFilters = null; this.deviceTypeFilters = null; this.deviceFeatureFilters = null; this.suggestionItems = null; this.sortByOptions = null; this.sortedDevices = null; this.allItems = null; this.pagedItems = null; this.creditProfile = null; this.user = null; this.compareProducts = null; this.subscription.unsubscribe(); } /** * This function is used to get and update active package. * @returns void */ updateActivePackage(): void { const METHOD_NAME: string = 'updateActivePackage()'; // getting active package from cart this.package = CartUtils.getActivePackage(this.cart, this.packageType); if (this.package) { // setting the active package device finance type. this.financeType = (this.package.device && this.package.device.financeType) ? this.package.device.financeType : this.financeType; // console.log('package#' + this.package.packageId + '>>line#' + this.package.lineId); // console.log('active package#' + this.cart.activePackageId + '>>line#' + this.cart.activeLineId); } else { // if there are no active voice package avaible, select first one. this.package = CartUtils.getFirstPackage(this.cart, this.packageType); if (this.package) { this.cart.activePackageId = this.package.packageId; this.cart.activeLineId = this.package.lineId; } } //finding active SIM card number this.activeSIMNumber = null; if (this.package && this.package.device && this.package.device.isSimOnly) { this.activeSIMNumber = this.package.device.sim; } // initilizing local veriables this.isFinanceOrder = CartUtils.isFinanceOrder(this.cart); this.isEIPOnlyOrder = CartUtils.isEIPOnlyOrder(this.cart); this.isJUMPOnlyOrder = CartUtils.isJUMPOnlyOrder(this.cart); this.isNCFPlanInCart = CartUtils.isNCFPlanInCart(this.cart); this.isBYOSOnlyOrder = CartUtils.isBYOSOnlyOrder(this.cart); this.isExchangeOrder = (this.cart && this.cart.refType === Constants.REF_TYPE_EXCHANGE) ? true : false; // getting the customer if (!this.customer && this.cart && this.cart.customer) { this.customer = this.cart.customer; } if (this.customer && this.customer.creditProfile) { this.creditProfile = this.customer.creditProfile; this.loanCrp = this.creditProfile.loanCrp ? this.creditProfile.loanCrp : Constants.LOAN_CRP_DEFAULT; this.leaseCrp = this.creditProfile.leaseCrp ? this.creditProfile.leaseCrp : Constants.LEASE_CRP_DEFAULT; } else { if (this.creditType) { this.loanCrp = Utils.getLoanCrp(this.creditType); this.leaseCrp = Utils.getLeaseCrp(this.creditType); } } if (this.creditProfile && this.creditProfile.accountNumber) { this.isAALOrder = !this.isExchangeOrder ? true : false; this.canFinance = (!this.creditProfile.isExtendedPaySchedule && !this.creditProfile.hideFinance) ? true : false; if (this.creditProfile.accountNumber && !CartUtils.hasSubscribersInAccount(this.customer)) { if (this.cart) { if (this.cart.refType !== Constants.REF_TYPE_EXCHANGE) { this.devices = null; this.disableAddToCart = true; this.sharedService.showErrorMessage('_ERR_SUBSCRIBERS_EMPTY', false); } } else { this.devices = null; this.disableAddToCart = true; this.sharedService.showErrorMessage('_ERR_SUBSCRIBERS_EMPTY', false); } } // checking the enable/diable add to cart button for aal path if (this.creditProfile.taxTreatment === Constants.MIXED_TAX_TREATMENT || this.creditProfile.isDelinquent || this.creditProfile.effectiveDate || (this.creditProfile.accountType === Constants.ACCOUNT_TYPE_I && this.creditProfile.accountSubType === Constants.ACCOUNT_SUB_TYPE_S)) { this.devices = null; this.disableAddToCart = true; if ((this.creditProfile.accountType === Constants.ACCOUNT_TYPE_I && this.creditProfile.accountSubType === Constants.ACCOUNT_SUB_TYPE_S) ) { this.sharedService.showErrorMessage('_ERR_SOLE_PROPRIETOR', false); } else if (this.creditProfile.isDelinquent) { this.sharedService.showErrorMessage('_ERR_DELINQUENT_CUSTOMER', false); } else if (this.creditProfile.taxTreatment === Constants.MIXED_TAX_TREATMENT || this.creditProfile.effectiveDate) { this.sharedService.showErrorMessage('_ERR_FUTURE_DATED_TAX_TREATMENT', false); } } } // applying the custom sorting this.applyCustomSorting(); } /** * This function is used to add device to cart. * @param {IDevice} device * @param {number} index * @returns void */ addDevice(device: IDevice): void { const METHOD_NAME: string = 'addDevice()'; this.searchText = ''; console.log('[start]- adding device to cart.'); // checking preprocess checks this.addItemPreprocess(device); // adding product into cart/package if (this.cart && this.package) { if(this.isExchangeOrder && this.package.returnOrderItemId) { this.checkForDeviceConflicts( device); }else if (device.items[device.itemIndex].isByos) { this.addByosDevice( device); } else { this.storeDevice( device); } } //console.log('[end]- adding device to cart.'); } /** * this function is for preprocess of adding item to cart * @param {IDevice} device contains valid device * @returns void */ addItemPreprocess(device: IDevice): void { const METHOD_NAME: string = 'addItemPreprocess()'; console.log('[start]- preprocess of adding item to cart.'); if (!this.cart) { // making sure there was cart in progress this.sharedService.addLogging(this.COMPONENT_NAME, METHOD_NAME, 'Creating a cart'); const inputData: any = CartUtils.createCartInput(this.storeId, this.user); this.sharedService.showSpinner(true); // spinner this.cartService.createCartDetails(this.storeId, inputData).subscribe(result => { this.sharedService.showSpinner(false); // spinner this.cart = result; // calling back add to device method again... this.addDevice(device); }, error => { this.sharedService.showSpinner(false); // spinner this.sharedService.showErrorMessage(error, true); }); }else if(this.cart && !this.cart.customer && this.customer) { this.sharedService.addLogging(this.COMPONENT_NAME, METHOD_NAME, 'Associating the customer to cart:' + this.cart.orderId); //adding order notes if (this.customer.orderNotes) { this.sharedService.addOrderNotes(this.storeId, this.cart.orderId, this.customer.orderNotes); this.customer.orderNotes = null; } this.customer.orderId = this.cart.orderId; this.sharedService.showSpinner(true); // spinner this.customerService.createCustomer(this.storeId, this.customer).subscribe(result => { this.sharedService.showSpinner(false); // spinner this.cart.customer = Object.assign({}, this.customer); this.cart.customer.addressId = result.addressId; this.customer = null; this.customerService.setCustomer(null); this.cartService.setCart(this.cart); // calling back add to device method again... this.addDevice( device); }, error => { this.sharedService.showSpinner(false); // spinner this.sharedService.showErrorMessage(error, true); }); }else if (this.cart && !this.package) { // making sure there was active package this.sharedService.addLogging(this.COMPONENT_NAME, METHOD_NAME, 'Creating a package to cart:' + this.cart.orderId); const inputData = CartUtils.createPackageInput(this.packageType, 1, null); this.sharedService.showSpinner(true); // spinner this.cartService.createPackageDetails(this.storeId, this.cart.orderId, inputData).subscribe(result => { this.sharedService.showSpinner(false); // spinner const packages: IPackage[] = result; for (const pkg of packages) { pkg.packageType = this.packageType; } // setting active package#&line# this.package = packages[0]; this.cart = CartUtils.addPackage(this.cart, packages); this.cart.activePackageId = this.package.packageId; this.cart.activeLineId = this.package.lineId; this.cartService.setCart(this.cart); // calling back to add to device again this.addDevice(device); }, error => { this.sharedService.showSpinner(false); // spinner this.sharedService.showErrorMessage(error, true); }); } } /** * This function is for adds the byos device item to cart * @param {IDevice} device contains valid device details. * @returns void */ addByosDevice(device: IDevice): void { const METHOD_NAME: string = 'addByosDevice()'; const simNumber: string = device.items[device.itemIndex].sim; // checking either we can add byos device into cart if (CartUtils.canAddByosToCart(this.cart)) { if(CartUtils.isDuplicateSIMNumber(this.cart, simNumber)){ this.sharedService.showErrorMessage('_ERR_BYOS_SIM_DUPLICATE', false); }else if (simNumber && simNumber.length >= Constants.SIM_LENGTH) { // checking serial number length this.sharedService.addLogging(this.COMPONENT_NAME, METHOD_NAME, 'Validating the SIM'); this.sharedService.showSpinner(true); // spinner this.checkoutService.validateSIMNumber(this.storeId, this.cart.fullOrderId, simNumber).subscribe(result => { this.sharedService.showSpinner(false); // spinner if (result && result.status === 'valid') { this.storeDevice( device); } else { this.sharedService.showErrorMessage('SIMVALIDATION_ERROR_CODE_2007', false); } }, error => { this.sharedService.showSpinner(false); // spinner this.sharedService.showErrorMessage(error, true); }); } else { this.sharedService.showErrorMessage('_ERR_INVAID_SIM_LENGTH', false); } } else { this.sharedService.showErrorMessage('_ERR_NON_BYOS_ITEMS_IN_CART', false); } } /** * This function is used to checks the device conflicts * @param {IDevice} device contains valid device details. * @returns void */ checkForDeviceConflicts(device: IDevice): void { const METHOD_NAME: string = 'checkForDeviceConflicts()'; this.sharedService.addLogging(this.COMPONENT_NAME, METHOD_NAME, 'Checking SOC conflicts for device'); //check soc conflict first before to add any device const deviceItem: any = device.items[device.itemIndex]; let inputData: any = { 'deviceSKU': deviceItem.deviceSKU, 'categoryId': '2', 'productId': device.productId , 'originalOrderItemId': this.package.returnOrderItemId }; this.sharedService.showSpinner(true); this.cartService.deviceFeatureConflicts(this.storeId, this.cart.orderId,inputData).subscribe(result => { this.sharedService.showSpinner(false); this.deviceConflicts = result; this.deviceConflicts.device = Object.assign(this.deviceConflicts,device); if(this.deviceConflicts && this.deviceConflicts.isPHPConflict) { this.deviceConflictExchangeModal.show(this.deviceConflicts); }else{ this.storeDevice(device); } }, error => { this.sharedService.showSpinner(false); // spinner this.sharedService.showErrorMessage(error, true); }); } /** * This function is to store device to cart once preprocess is done * @param {IDevice} device contains valid device * @returns void */ storeDevice(device: IDevice): void { const METHOD_NAME: string = 'updateDevice()'; const crp: number = (this.financeType && this.financeType === this.financeTypeLoan) ? this.loanCrp : this.leaseCrp; const inputData: any = CartUtils.createDeviceInput(this.packageType, device, this.financeType, crp); if (this.package.returnOrderItemId) { inputData.originalOrderItemId = this.package.returnOrderItemId; inputData.orderRefType = Constants.REF_TYPE_EXCHANGE; } this.sharedService.addLogging(this.COMPONENT_NAME, METHOD_NAME, 'Adding a device to cart:' + this.cart.orderId); // adding non byos device into cart this.sharedService.showSpinner(true); // spinner this.cartService.createDevice(this.storeId, this.cart.orderId, this.package.packageId, this.package.lineId, inputData ).subscribe(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 to update product to cart * @param {IDevice} device contains valid device * @param {number} index contains valid number * @returns void */ updateDevice(device: IDevice): void { const METHOD_NAME: string = 'updateDevice()'; this.sharedService.addLogging(this.COMPONENT_NAME, METHOD_NAME, 'Updating MI device to cart:' + this.cart.orderId); console.log('[start]- updating product to cart.'); this.searchText = ''; // updating product into cart/package if (this.cart && this.package && this.package.device) { if (!device.items[device.itemIndex].isByos) { const crp: number = (this.financeType && this.financeType === this.financeTypeLoan) ? this.loanCrp : this.leaseCrp; const inputData: any = CartUtils.createDeviceInput(this.packageType, device, this.financeType, crp); inputData.orderItemId = this.package.device.orderItemId; this.sharedService.showSpinner(true); // spinner this.cartService.updateDevice(this.storeId, this.cart.orderId, this.package.packageId, this.package.lineId, this.package.device.orderItemId, inputData ).subscribe(result => { // this.sharedService.showSpinner(false);//spinner // this.cart = CartUtils.replaceRequired(this.cart, result); // this.cartService.setCart(this.cart); // console.log('Device updated successfully.'); this.cartService.getCartSummary(this.storeId, this.cart.orderId).subscribe(getCartSummaryResult => { this.sharedService.showSpinner(false); // spinner this.cart = CartUtils.replaceRequired(this.cart, getCartSummaryResult); this.cartService.setCart(this.cart); // this.selectSuggestionItem(null); }, error => { this.sharedService.showSpinner(false); // spinner this.sharedService.showErrorMessage(error, true); }); }, error => { this.sharedService.showSpinner(false); // spinner this.sharedService.showErrorMessage(error, true); }); } else { this.sharedService.showErrorMessage('_ERR_NON_BYOS_ITEMS_IN_CART', false); } } console.log('[end]- updating product to cart.'); } /** * This function is used for removing product from cart. * @param {IDevice} device contains valid device details. * @returns void */ removeDevice(device: IDevice): void { const METHOD_NAME: string = 'removeDevice()'; this.sharedService.addLogging(this.COMPONENT_NAME, METHOD_NAME, 'Removing MI device from cart:' + this.cart.orderId); console.log('[start]- removing product from cart.'); this.searchText = ''; // adding product into cart/package if (this.cart && this.package && this.package.device) { this.sharedService.showSpinner(true); // spinner this.cartService.deleteDevice(this.storeId, this.cart.orderId, this.package.packageId, this.package.lineId, this.package.device.orderItemId ).subscribe(result => { this.sharedService.showSpinner(false); // spinner this.cart = CartUtils.replaceRequired(this.cart, result); this.cartService.setCart(this.cart); // this.selectSuggestionItem(null); // this.buttonState(device.items[index]); }, error => { this.sharedService.showSpinner(false); // spinner this.sharedService.showErrorMessage(error, true); }); } console.log('[end]- removing product from cart.'); } /** * This function is used filter devices by rateplan using getDevicesByRateplan() from catalog service. * @returns void */ filterByRateplan(): void { const METHOD_NAME: string = 'filterByRateplan()'; if (this.disableAddToCart) { return; } const rateplanSOC = (this.package && this.package.plan && this.package.plan.rateplanSOC) ? this.package.plan.rateplanSOC : (this.package && this.package.origRatePlanSoc)?this.package.origRatePlanSoc :null; if (this.package && rateplanSOC) { // calling device filter this.sharedService.showSpinner(true); // spinner this.catalogService.getDevicesByRateplan( this.storeId, rateplanSOC ).subscribe(result => { this.sharedService.showSpinner(false); // spinner if (result) { this.devices = this.catalogService.getGSMDevices().concat(this.catalogService.getMIDevices()); this.devices = this.devices.filter((product) => { return result.includes(product.productId); }); this.resetDeviceFilters(); this.applyCustomSorting(); // applying the custom sorting } }, error => { this.sharedService.showSpinner(false); // spinner this.sharedService.showErrorMessage(error, true); }); } else { let zipCode: string = Constants.DEFAULT_ZIP_CODE; if (this.customer && this.customer.zipCode) { zipCode = this.customer.zipCode; } else if (this.cart && this.cart.customer && this.cart.customer.zipCode) { zipCode = this.cart.customer.zipCode; } // calling device filter this.sharedService.showSpinner(true); // spinner this.catalogService.getDevicesByZipCode(this.storeId, zipCode, Constants.PRODUCT_TYPE_DATA).subscribe(result => { this.sharedService.showSpinner(false); // spinner if (result) { this.devices = this.catalogService.getMIDevices(); this.devices = this.devices.filter((product) => { return result.includes(product.productId); }); this.resetDeviceFilters(); // applying the custom sorting this.applyCustomSorting(); } }, error => { this.sharedService.showSpinner(false); // spinner this.sharedService.showErrorMessage(error, true); }); } } /** * This function is used to display devices. * @returns void */ applyCustomSorting(): void { const METHOD_NAME: string = 'applyCustomSorting()'; if (this.devices && this.devices.length > 0) { // support products for full price/loan/lease const crp: number = this.financeType === Constants.FINANCE_TYPE_LOAN ? this.loanCrp : this.leaseCrp; let sortList: any = Utils.keepFinanceItems(this.devices, this.financeType, crp); // let sortList:any = Utils.sortByFinance(this.devices,this.financeType, crp); if (this.isBYOSOnlyOrder) { sortList = sortList.filter((product) => { product.items[0].sim = (this.package.device)? product.items[0].sim : null; // remove the pooled line sim number if only one device sim is added. return product.items[0].isByos === this.isBYOSOnlyOrder; }); } else { if(this.isExchangeOrder) { //if (!this.isAALOrder && !this.isExchangeOrder) { sortList = sortList.filter((product) => { return !product.items[0].isByos; }); } } sortList = Utils.serachFilter(sortList, this.filters); sortList = Utils.serachSortBy(sortList, this.sortByKey); // US291720:update default active item index to show device color based on priority - InStock/BOA/OOS sortList.forEach(device => { device.itemIndex = Utils.getItemIndex(device); }); sortList = Utils.serachProducts(sortList, this.searchText); this.allItems = Utils.serachProducts(sortList, this.searchText); // initilizing the compare products and expand product# if (this.allItems && this.allItems.length > 0) { this.expandedProductId = null; // this.compareProducts = new Array(); if (this.compareProducts.length < 1) { for (const product of this.allItems) { product.canCompare = false; } } } // initialize to page 1 this.setPage(1); } } /** * This function is used to apply paging for list of devices. * @param {number} page contains valid number. * @returns void */ setPage(page: number): void { const METHOD_NAME: string = 'setPage()'; jQuery('.main-panel').scrollTop(0); if (page < 1 || page > this.pager.totalPages) { return; } if (this.allItems && this.allItems.length > 0) { // get pager object from service this.pager = Utils.getPager(this.allItems.length, page, this.cardsPerPage); // get current page of items this.pagedItems = this.allItems.slice(this.pager.startIndex, this.pager.endIndex + 1); // grouping 3 plans per row this.sortedDevices = Utils.groupProducts(this.pagedItems, this.groupByCount); } else { this.sortedDevices = null; } } /** * This function is used to return the state of button for device. * @param {IDeviceItem} deviceItem contains valid device details. * @returns string */ buttonState(deviceItem: IDeviceItem): string { const METHOD_NAME: string = 'buttonState()'; let state: string = Constants.BTN_DISABLE; // console.log('deviceItem.status>>>'+ deviceItem.status); if (deviceItem && deviceItem.status && deviceItem.status !== Constants.OUTOFSTOCK_CODE && !this.disableAddToCart) { state = Constants.BTN_ADD; if (this.cart && this.package && this.package.device) { state = Constants.BTN_UPDATE; if (this.financeType === this.package.device.financeType) { if (this.package.device.deviceSKU === deviceItem.deviceSKU) { deviceItem.sim = this.package.device.sim ? this.package.device.sim : null; } state = this.package.device.deviceSKU === deviceItem.deviceSKU ? Constants.BTN_REMOVE : Constants.BTN_UPDATE; } } } return state; } /** * This function is used to get key,valye pairs of inventory status. * @param {IDeviceItem} device item * @returns boolean */ hasOffer(deviceItem: IDeviceItem): boolean { return Utils.hasOffer( deviceItem ); } /** * This function is used to get key,valye pairs of inventory status. * @param {string} status * @returns 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 '-'; } /** * This function is used to resetting the device filters * @returns void */ resetDeviceFilters(): void { const METHOD_NAME: string = 'resetDeviceFilters()'; if (this.filters) { this.filters.forEach((element) => { if (element.type === Constants.DEVICE_GRP_NAME_MNFR) { this.deviceBrandFilters = element.items; } if (element.type === Constants.DEVICE_GRP_NAME_TYPE) { this.deviceTypeFilters = element.items; } if (element.type === Constants.DEVICE_GRP_NAME_FEATURE) { this.deviceFeatureFilters = element.items; } }); if (this.devices) { this.filters = new Array(); this.deviceBrandFilters = Utils.matchFiltersProducts(this.deviceBrandFilters, this.devices); if (this.deviceBrandFilters) { this.filters.push( { type: Constants.DEVICE_GRP_NAME_MNFR, items: this.deviceBrandFilters }); } this.deviceTypeFilters = Utils.matchFiltersProducts(this.deviceTypeFilters, this.devices); if (this.deviceTypeFilters) { this.filters.push( { type: Constants.DEVICE_GRP_NAME_TYPE, items: this.deviceTypeFilters }); } this.deviceFeatureFilters = Utils.matchFiltersProducts(this.deviceFeatureFilters, this.devices); if (this.deviceFeatureFilters) { this.filters.push( { type: Constants.DEVICE_GRP_NAME_FEATURE, items: this.deviceFeatureFilters }); } } } } /** * This function is used to check check whether atleast once device checked or not. * @returns boolean */ atleastOneChecked(): boolean { const METHOD_NAME: string = 'atleastOneChecked()'; if (this.filters && this.filters.length > 0) { for (const filter of this.filters) { if (filter && filter.items) { for (const item of filter.items) { if (item.isChecked) { return true; } } } } } return false; } /** * this function is used to clear all filters applied to devices. * @returns void */ clearAllFilters(): void { const METHOD_NAME: string = 'clearAllFilters()'; for (const filter of this.filters) { for (const item of filter.items) { item.isChecked = false; } } if (this.sortByOptions) { this.sortByKey = this.sortByOptions[0].key; } this.searchText = ''; this.financeType = Constants.FINANCE_TYPE_FRP; this.applyCustomSorting(); } /** * This function is used to display more details of selected device. * @param {number} productId contains valid device id. * @returns void */ expandDetails(productId: number): void { const METHOD_NAME: string = 'expandDetails()'; if (this.expandedProductId && this.expandedProductId === productId) { this.expandedProductId = null; } else { this.expandedProductId = productId; } } /** * This function is used to compare products. * @param {boolean} isChecked indicates whether the product selected or not. * @param {IDevice} product contains valid device. * @returns void */ updateCompareProducts(isChecked: boolean, product: IDevice): void { const METHOD_NAME: string = 'updateCompareProducts()'; this.sharedService.addLogging(this.COMPONENT_NAME, METHOD_NAME, 'Update compare products'); if (isChecked) { if (this.compareProducts) { if (this.compareProducts.length < Constants.MAXIMUM_COMPARE) { product.canCompare = true; this.compareProducts.push(product); } else { product.canCompare = false; // console.log('show excceed model'); } } else { this.compareProducts = new Array(); this.compareProducts.push(product); } jQuery('.checked-status').addClass('visible'); } else { product.canCompare = false; if (this.compareProducts) { // removing item from compare product list const index = this.compareProducts.indexOf(product, 0); if (index > -1) { this.compareProducts.splice(index, 1); } } } } /** * This function is used to disable compare option for device * @param {IDevice} product contains valid device * @returns boolean */ disableCompare(product: IDevice): boolean { const METHOD_NAME: string = 'disableCompare()'; if (!product.canCompare) { if (this.compareProducts && this.compareProducts.length === Constants.MAXIMUM_COMPARE) { return true; } } return false; } /** * This function is used to get list of matching devices based on search text * @returns void */ getSuggestionItems(): void { if (this.searchText && this.searchText.length > 0) { this.miSuggestions = this.marketingService.getMIDeviceSuggestions(this.storeId); if (this.miSuggestions && this.miSuggestions.length > 0) { this.suggestionItems = this.miSuggestions.filter((item) => { return item.toUpperCase().indexOf(this.searchText.toUpperCase(), 0) > -1; }); } } else { this.suggestionItems = []; } } /** * This function is used to display list of devices based on search text * @param {string} item * @returns void */ selectSuggestionItem(item: string): void { this.searchText = item; this.suggestionItems = []; // this.applyCustomSorting(); if (this.searchText && this.searchText.length > 0) { this.applyCustomSorting(); } } sendCompareModalInfo(show: string){ this.compareService.compareProducts = this.compareProducts; this.compareService.leaseCRP = this.leaseCrp; this.compareService.loanCRP = this.loanCrp; this.compareService.deviceFeatureFilters = this.deviceFeatureFilters; this.compareService.setMessage(show); } sendMoreDetailsModalInfo(show: string, productItem: any){ this.moreDetailsService.product = productItem; this.moreDetailsService.setMessage(show); } }