import { Component, Input, OnInit, OnDestroy } from '@angular/core'; import * as Constants from '../../../shared/constants'; import { Customer, ICreditProfile } from '../../../shared/modal/customer'; import { Cart } from '../../../shared/modal/cart'; import { SharedService } from '../../../shared/shared.service'; import { UserService } from '../../../services/user.service'; import { CustomerService } from '../../../services/customer.service'; import { CartService } from '../../../services/cart.service'; import { ICreditLine, IOrder, ISubscriber } from '../../../shared/modal/common'; @Component({ selector: 'customer-detail-cmp', moduleId: module.id, templateUrl: 'customer-detail.component.html', styleUrls: ['customer-detail.component.css'] }) export class CustomerDetailComponent implements OnInit, OnDestroy { private COMPONENT_NAME: string = 'CustomerDetailComponent'; storeId: number; // update storeid @Input() customer: Customer; KarmaTestResult: any = {}; constructor( 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.Here it is initializing all the states, payment methods and card types * @returns void */ ngOnInit(): void { const METHOD_NAME: string = 'ngOnInit()'; // this.sharedService.updateErrorMessage(null); console.log('Loading the customer details.'); this.sharedService.storeId.subscribe((val: number) => { this.storeId = val; }); if (this.customer && this.customer.creditProfile) { const creditProfile: ICreditProfile = this.customer.creditProfile; if (creditProfile.accountNumber) { if (creditProfile.ecl === null || creditProfile.ecl === 0 || creditProfile.eclb === null || creditProfile.eclb === 0) { console.log('calling credit line summary for account:' + creditProfile.accountNumber); this.getCreditLineDeatils(); } if (!creditProfile.hasOwnProperty('onTimePayment')) { console.log('calling preferred priceing for account:' + creditProfile.accountNumber); this.getPreferredPrice(creditProfile.accountNumber, creditProfile.creditClass); } if (!this.customer.orders) { console.log('calling order list for account:' + creditProfile.accountNumber); this.getOrderHistory(creditProfile.accountNumber); } // making sure, there are credit line info let canCallSubscribers: boolean = true; if (this.customer.subscribers) { for (const sub of this.customer.subscribers) { if (sub.rateplanSOC && sub.deviceSKU) { canCallSubscribers = false; break; } } } if (canCallSubscribers) { console.log('calling subscriber list for account:' + creditProfile.accountNumber); this.getSubscribers(creditProfile.accountNumber); } else { this.updateCustomer(this.customer); } } else { console.log('there was no account# accosiated for customer.') } } } /** * 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.customer = null; this.KarmaTestResult = null; } /** * This function is used to get creditline details from customerService * @returns void */ getCreditLineDeatils(): void { const METHOD_NAME: string = 'getCreditLineDeatils()'; console.log('CustomerComponent.getCreditLineDeatils()>>Start.'); // this.sharedService.showSpinner(true);//spinner(don't need to block the user) this.customerService.getCreditLine( this.storeId, this.customer.creditProfile.accountNumber, this.customer.creditProfile.planType, this.customer.creditProfile.creditClass, this.customer.creditProfile.accountType, this.customer.creditProfile.accountSubType, this.customer.creditProfile.accountStatus, this.customer.creditProfile.accountTenure, this.customer.creditProfile.behaviorScore, this.customer.creditProfile.onyxReferenceNumber, this.customer.creditProfile.loanCrp, this.customer.creditProfile.leaseCrp, this.customer.state, this.customer.totalActiveSubscribers, this.customer.creditProfile.activeDataLines, this.customer.creditProfile.activeVoiceLines).subscribe(result => { this.KarmaTestResult = result; // this.sharedService.showSpinner(false);//spinner(don't need to block the user) if (this.customer) { this.customer.creditProfile.ecla = result.ecla; this.customer.creditProfile.ecl = result.ecl; this.customer.creditProfile.eclb = result.eclb; this.customer.creditProfile.lca = result.lca; this.customer.creditProfile.mpec = result.mpec; this.customer.creditProfile.financeEligibleCount = result.financeEligibleCount; this.customer.creditProfile.hideFinance = result.hideFinance; this.updateCustomer(this.customer); } }, error => { // this.sharedService.showSpinner(false);//spinner(don't need to block the user) // this.sharedService.showErrorMessage(error, null); console.error('error=>' + error); }); } /** * This function is used to get order list from customer service. * @param {number} accountNumber contains valid account number. * @returns void */ getOrderHistory(accountNumber: number): void { const METHOD_NAME: string = 'getOrderHistory()'; console.log('CustomerComponent.getOrderHistory()>>Start.'); // this.sharedService.showSpinner(true);//spinner(don't need to block the user) this.customerService.getOrderList(this.storeId, accountNumber).subscribe(result => { this.KarmaTestResult = result; // this.sharedService.showSpinner(false);//spinner(don't need to block the user) if (this.customer) { this.customer.orders = []; this.customer.orders = result; this.getAssociatedOrder(); this.updateCustomer(this.customer); } }, error => { // this.sharedService.showSpinner(false);//spinner(don't need to block the user) // this.sharedService.showErrorMessage(error, null); console.error('error=>' + error); }); } /** * This function is used to get list of subscribers from customer service. * @param {number} accountNumber contains valid account number. * @returns void */ getSubscribers(accountNumber: number): void { const METHOD_NAME: string = 'getSubscribers()'; console.log('CustomerComponent.getSubscribers()>>Start.'); let hasDuplicateLines: boolean = false; const phoneNumbers: string = null; // avoiding to call account details service. // if(this.customer && this.customer.subscribers) { // for(let sub of this.customer.subscribers) { // if(sub.phoneNumber) phoneNumbers += sub.phoneNumber +','; // } // } if (this.customer && this.customer.existingDuplicateLines && this.customer.existingDuplicateLines > 0) { hasDuplicateLines = true; } this.sharedService.showSpinner(true); // spinner this.customerService.getSubscriberList(this.storeId, accountNumber, phoneNumbers, hasDuplicateLines).subscribe(result => { this.KarmaTestResult = result; this.sharedService.showSpinner(false); // spinner if (result && result.length > 0) { const subscribers = this.customer.subscribers.map(x => Object.assign({}, x)); this.customer.subscribers = result.map(val => { return Object.assign({}, subscribers.filter(v => v.phoneNumber === val.phoneNumber)[0], val); }); this.updateCustomer(this.customer); } else { this.sharedService.showErrorMessage('_ERR_SUBSCRIBERS_EMPTY', false); } }, error => { this.sharedService.showSpinner(false); // spinner // this.sharedService.showErrorMessage(error, null); this.sharedService.showErrorMessage('_ERR_SUBSCRIBERS_EMPTY', false); console.error('error=>' + error); }); } /** * This function is used to get preferred price from customer service. * @param {number} accountNumber contains valid customer account number * @param {string} creditClass contains valid credit class. * @returns void */ getPreferredPrice(accountNumber: number, creditClass: string): void { const METHOD_NAME: string = 'getPreferredPrice()'; console.log('CustomerComponent.getPreferredPrice()>>Start.'); // this.sharedService.showSpinner(true);//spinner(don't need to block the user) this.customerService.getPreferredPrice(this.storeId, accountNumber, creditClass).subscribe(result => { this.KarmaTestResult = result; // this.sharedService.showSpinner(false);//spinner(don't need to block the user) this.customer.creditProfile.reasonCode = result.reasonCode ? result.reasonCode : 0; this.customer.creditProfile.onTimePayment = result.onTimePayment ? result.onTimePayment : 0; this.customer.creditProfile.remainingPayment = result.remainingPayment ? result.remainingPayment : 0; this.customer.creditProfile.eligibleDate = result.eligibleDate ? result.eligibleDate : null; this.updateCustomer(this.customer); }, error => { // this.sharedService.showSpinner(false);//spinner(don't need to block the user) // this.sharedService.showErrorMessage(error, null); console.error('error=>' + error); }); } /** * This function is used to get and update customer using customer service. * @param {Customer} cust * @returns void */ updateCustomer(cust: Customer): void { const cart: Cart = this.cartService.getCart(); let customer: Customer = this.customerService.getCustomer(); if (cust.subscribers && cust.creditProfile) { for (const sub of cust.subscribers) { if (sub.isGSM && sub.isPooledPlan) { cust.creditProfile.virtualRatePlan = sub.rateplanSOC; break; } } } if (cart && cart.customer) { cart.customer = Object.assign(cart.customer, cust); this.cartService.setCart(cart); } else if (customer) { customer = Object.assign(customer, cust); this.customerService.setCustomer(customer); } } /** * This function is used to get the order assosiated order details. * @returns List Orders */ getAssociatedOrder(): any { const METHOD_NAME: string = 'getAssociatedOrder()'; if (this.customer && this.customer.orders && this.customer.orders.length > 0) { // let Orders: any = this.customer.orders; // this.customer.orders = []; for (let order of this.customer.orders) { if (order && order.fullOrderId) { this.showBlindAssociatedOrders(order.fullOrderId); } } } } /** * This function is used to get the order assosiated order details. * @param {number} orderId - orderId of the user. * @returns void */ showAssociatedOrders(orderId: number): void { const METHOD_NAME: string = 'showAssociatedOrders()'; // this.sharedService.showSpinner(true);// spinner this.cartService.getAssociatedOrders(this.storeId, orderId).subscribe(result => { // this.sharedService.showSpinner(false);// spinner if (result && result.associatedOrders) { for (let i = 0; i < result.associatedOrders.length; i++) { this.customer.orders.push(result.associatedOrders[i]); } } }, error => { // this.sharedService.showSpinner(false);// spinner // this.sharedService.showErrorMessage(error, true); console.error('Error occurred while loading the associated orders for order:' + orderId); }); } /** * This function is used to get the sap blind order details. * @param {number} fullOrderId - fullOrderId * @returns void */ showBlindAssociatedOrders(fullOrderId: any): void { const METHOD_NAME: string = 'showAssociatedOrders()'; // this.sharedService.showSpinner(true);// spinner this.cartService.getSapOrderList(this.storeId, fullOrderId).subscribe(result => { // this.sharedService.showSpinner(false);// spinner if (result && result.associatedOrders) { for (let associatedOrder of result.associatedOrders) { if(associatedOrder.isSAPReturnOrder) this.customer.orders.push(associatedOrder); } } }, error => { // this.sharedService.showSpinner(false);// spinner // this.sharedService.showErrorMessage(error, true); console.error('Error occurred while loading the blind associated orders for order:' + fullOrderId); }); } }