/** * @Component Name: CustomerInfoComponent * @Module: Checkout * @Module Functionality : To display the template info section * @Creation Date: August 12,2018 * @Author: sbaireddy * @Version: 1.0 */ import { Component, Input, ViewChild, OnInit, OnDestroy } from '@angular/core'; import { Router } from '@angular/router'; import * as Constants from '../../shared/constants' import { TEMPLATE_TYPES, CUSTOMER_INTENT } from '../../shared/form.constants' import Utils from '../../shared/utils'; import CartUtils from '../../shared/cart.utils'; 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 { CheckoutService } from '../../services/checkout.service'; import { MarketingService } from '../../services/marketing.service'; @Component({ selector: 'template-info-cmp', moduleId: module.id, templateUrl: 'template-info.component.html', styleUrls: ['template-info.component.css'] }) export class TemplateInfoComponent implements OnInit, OnDestroy { private COMPONENT_NAME: string = 'TemplateInfoComponent'; storeId: number; // update storeid templateTypes: any[]; custIntents: any[]; creditDetails: any; creditType: string; user: User; cart: Cart; customer: Customer; templateModel: any = { languageOption: true, tempName: null, custIntent: null, //avgMonthlyBill: 0, estMonthlyBill: 0, exptMonthlyDiff: 0 }; isAALOrder: boolean = false; enrollAutopay: boolean = false; disableFields: boolean = false; diffAmount: number = 0; constructor( public router: Router, public sharedService: SharedService, public userService: UserService, public cartService: CartService, public customerService: CustomerService, public checkoutService: CheckoutService, public marketingService: MarketingService) { } /** * 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 the user profile, cart and customer information * @return {void} */ ngOnInit(): void { const METHOD_NAME: string = 'ngOnInit()'; this.sharedService.updateErrorMessage(null); this.templateTypes = TEMPLATE_TYPES.filter(tempItem => tempItem); this.custIntents = CUSTOMER_INTENT.filter(intentItem => intentItem); this.sharedService.storeId.subscribe((val: number) => { this.storeId = val; }); this.sharedService.creditChoice.subscribe((val: string) => { this.creditType = val; }); this.user = this.userService.getUser(); this.cart = this.cartService.getCart(); if (this.cart && this.user) { this.customer = this.cart.customer ? this.cart.customer : null;; if (this.user && this.user.taxAreaId) { this.calculateTax(this.cart.orderId); } else { this.getCartSummary(this.cart.orderId); } } this.creditDetails = this.customerService.getCreditDetails(); if (!this.creditDetails) this.initCreditDetails(); this.initCustomerInfo(); } /** * Cleanup just before Angular destroys the directive/component * Unsubscribe Observables and detach event handlers to avoid memory leaks. * @return {void} */ ngOnDestroy(): void { const METHOD_NAME: string = 'ngOnDestroy()'; this.cart = null; this.customer = null; } initCreditDetails(): void { const METHOD_NAME: string = 'initCreditDetails()'; this.sharedService.showSpinner(true); // spinner this.customerService.getCreditOptionDetails(this.storeId).subscribe(result => { this.sharedService.showSpinner(false); // spinner if (result && result.length > 0) { this.creditDetails = result; this.customerService.setCreditDetails(result); } }, error => { this.sharedService.showSpinner(false); // spinner console.error('error=>' + error); this.sharedService.showErrorMessage(error, true); }); } initCustomerInfo(): void { if (this.cart) { this.templateModel.estMonthlyBill = Utils.formatDecimal(String(CartUtils.totalDueMonthly(this.cart, true))); if (this.cart.customer) { if (this.cart.customer.creditProfile && (this.cart.customer.creditProfile.accountNumber || (this.cart.customer.creditType && this.cart.customer.creditProfile.creditClass))) { this.disableFields = true; this.isAALOrder = this.cart.customer.creditProfile.accountNumber ? true : false; } const billingAddress: any = { addressId: this.cart.customer.addressId, firstName: this.cart.customer.firstName, middleName: this.cart.customer.middleName, lastName: this.cart.customer.lastName, // address1: this.cart.customer.address1, // address2: this.cart.customer.address2, // city: this.cart.customer.city, // state: this.cart.customer.state, // state: (this.cart.customer.state == null) ? '' : this.cart.customer.state , // zipCode: this.cart.customer.zipCode, phone1: this.cart.customer.phone1, // phone2: null, email1: this.cart.customer.email1, // email2: null }; this.templateModel = Object.assign(this.templateModel, billingAddress); this.templateModel.phone1 = Utils.formatPhoneNumber(this.templateModel.phone1); } // this.templateModel.dealerCode = this.cart.dealerCode; // this.templateModel.validDealerCode = (this.cart.dealerCode) ? true : this.templateModel.validDealerCode; // this.templateModel.webOrderId = this.cart.webOrderId; // this.templateModel.retailStoreId = this.cart.retailStoreId; } } /** * This function is used to get the cart summary through * getCartSummary function in cart service * @param {number} orderId - order id to refer the order details * @return {void} */ getCartSummary(orderId: number): void { const METHOD_NAME: string = 'getCartSummary()'; // getting current cart updates this.sharedService.showSpinner(true); // spinner this.cartService.getCartSummary(this.storeId, orderId).subscribe(result => { this.sharedService.showSpinner(false); // spinner this.cart = Object.assign({}, result); this.cartService.setCart(this.cart); for (const pkg of this.cart.packages) { if (!pkg.showPackage) { pkg.showPackage = false; } } // expanding the first package this.cart.packages[0].showPackage = true; // getting the customer profile this.getCustomerProfile(this.cart.orderId); }, error => { this.sharedService.showSpinner(false); // spinner this.sharedService.showErrorMessage(error, true); // console.error('error=>' + error) }); } /** * This function is used to get the customer profile through * getCustomerProfile function in customer service * @param {number} orderId - order id to refer the order details * @return {void} */ getCustomerProfile(orderId: number): void { const METHOD_NAME: string = 'getCustomerProfile()'; console.log('CustomerInfoComponent.getCustomerProfile()>>Start.'); this.sharedService.showSpinner(true); // spinner this.customerService.getCustomerProfile(this.storeId, orderId, null).subscribe(result => { this.sharedService.showSpinner(false); // spinner this.customer = result; this.cart.customer = result; this.cartService.setCart(this.cart); this.initCustomerInfo(); }, error => { this.sharedService.showSpinner(false); // spinner this.sharedService.showErrorMessage(error, true); // console.error('error=>' + error); }); } calculateTax(orderId: number): void { const METHOD_NAME: string = 'calculateTax()'; console.log('CustomerInfoComponent.calculateTax()>>Start.'); let inputData: any = {}; inputData.taxAreaId = this.user.taxAreaId; inputData.address1 = this.user.address1; inputData.address2 = this.user.address2; inputData.city = this.user.city; inputData.state = this.user.state; inputData.zipCode = Utils.trimZipCode(this.user.zipCode); this.sharedService.showSpinner(true); // spinner this.cartService.calculateTax(this.storeId, orderId, inputData).subscribe(result => { this.sharedService.showSpinner(false); // spinner this.cart = Object.assign(this.cart, result); this.cartService.setCart(this.cart); this.getCustomerProfile(this.cart.orderId); if (this.cart.status != Constants.ORDER_STATUS_P) { this.sharedService.showErrorMessage('_ERR_TAX_ESTIMATE_FAILED', false); } }, error => { this.sharedService.showSpinner(false); // spinner this.sharedService.showErrorMessage('_ERR_TAX_ESTIMATE_FAILED', false); // console.error('error=>' + error); }); } sendQuoteToCustomer(): void { const nonFormatPhone1 = Utils.nonFormatPhoneNumber(this.templateModel.phone1); if (this.customer.firstName != this.templateModel.firstName || this.customer.middleName != this.templateModel.middleName || this.customer.lastName != this.templateModel.lastName || this.customer.email1 != this.templateModel.email1 || this.customer.phone1 != nonFormatPhone1) { let inputData: any = {}; if (this.cart && this.cart.customer && this.cart.customer.creditProfile && !this.cart.customer.creditProfile.accountNumber && !this.cart.customer.creditProfile.creditClass) { inputData.orderId = this.cart.orderId; inputData.firstName = this.templateModel.firstName; inputData.lastName = this.templateModel.lastName; inputData.middleName = this.templateModel.middleName; inputData.address1 = ''; inputData.address2 = ''; inputData.city = ''; inputData.state = ''; inputData.zipCode = ''; inputData.phone1 = nonFormatPhone1; inputData.email1 = this.templateModel.email1; inputData.creditType = Constants.CREDIT_TYPE_CC; if (this.creditType === Constants.CREDIT_CHECK) { inputData = Object.assign(inputData, Utils.getCreditProfile(Constants.CREDIT_CLASS_C)); } else if (this.creditType === Constants.NOCREDIT_CHECK || CartUtils.isNCFPlanInCart(this.cart)) { inputData.creditType = Constants.CREDIT_TYPE_NC; inputData = Object.assign(inputData, Utils.getCreditProfile(Constants.CREDIT_CLASS_Q)); } else { inputData = Object.assign(inputData, Utils.getCreditProfile(Constants.CREDIT_CLASS_A)); } this.sharedService.showSpinner(true); this.customerService.createCustomer(this.storeId, inputData).subscribe(result => { this.sharedService.showSpinner(false); // spinner this.customer = inputData; this.customer.addressId = result.addressId; this.cart.customer = this.customer; this.cartService.setCart(this.cart); this.sendPurchaseEstimate(); }, error => { this.sharedService.showSpinner(false); // spinner this.sharedService.showErrorMessage(error, true); console.error('error=>' + error); }); } else { inputData.addressId = this.customer.addressId; if (this.customer.firstName != this.templateModel.firstName) { inputData.firstName = this.templateModel.firstName; } if (this.customer.middleName != this.templateModel.middleName) { inputData.middleName = this.templateModel.middleName; } if (this.customer.lastName != this.templateModel.lastName) { inputData.lastName = this.templateModel.lastName; } if (this.customer.email1 != this.templateModel.email1) { inputData.email1 = this.templateModel.email1; } if (this.customer.phone1 != nonFormatPhone1) { inputData.phone1 = nonFormatPhone1; } const billing: any = { billingAddress: inputData }; this.sharedService.showSpinner(true); // spinner this.checkoutService.updateCustomerInfo(this.storeId, this.cart.orderId, billing).subscribe(result => { this.sharedService.showSpinner(false); // spinner this.customer = Object.assign(this.customer, inputData); this.cart.customer = this.customer; console.log('Success'); this.sendPurchaseEstimate(); }, error => { this.sharedService.showSpinner(false); // spinner this.sharedService.showErrorMessage(error, true); console.error('error=>' + error); }); } } else { this.sendPurchaseEstimate(); } } sendPurchaseEstimate(): void { this.user.email1 = (this.user.email1 === "") ? this.customer.email1 : this.user.email1; this.user.phone1 = (this.user.phone1 === "") ? this.customer.phone1 : this.user.phone1; if (this.customer && this.customer.email1) { const inputData: any = { cart: this.cart, userProfile: this.user, emailTemplate: this.templateModel.tempName, customerIntent: this.templateModel.custIntent, languagePreference: this.templateModel.languageOption ? 'en' : 'es', managerEmail: this.templateModel.storeEmail, currentTotalDueMonthly: this.templateModel.avgMonthlyBill ? this.templateModel.avgMonthlyBill : 0 }; inputData.cart.customerInfo = Object.assign({}, this.cart.customer); inputData.cart.customer = null; inputData.userProfile.salesStats = null; inputData.userProfile.notifications = null; this.sharedService.showSpinner(true); // spinner let isAALOrder: boolean = CartUtils.isAALOrder(this.cart); this.checkoutService.sendPurchaseEstimate(this.storeId, this.cart.orderId, isAALOrder, inputData).subscribe(result => { //this.sharedService.showSpinner(false); // spinner //this.sharedService.showSpinner(true); // spinner this.cartService.updateCartDetails(this.storeId, this.cart.orderId, { status: 'C' }).subscribe(result => { this.sharedService.showSpinner(false); // spinner let orderNotes = { agentId: this.user.logonId, subject: 'Estimate Email sent', description: 'Purchase Estimate was sent to : ' + this.templateModel.email1 }; this.sharedService.addOrderNotes(this.storeId, this.cart.orderId, orderNotes); this.router.navigate(['/estimate/email-confirm']); }, error => { this.sharedService.showSpinner(false); // spinner this.sharedService.showErrorMessage(error, true); console.error('error=>' + error) }); }, error => { this.sharedService.showSpinner(false); // spinner this.sharedService.showErrorMessage(error, true); console.error('error=>' + error) }); } else { this.sharedService.showErrorMessage('_ERR_EMAIL_NO_FOUND', false); } } get rateplanTypes(): any[] { if (this.templateModel.custIntent && this.templateModel.custIntent == 'BASE') { return this.sharedService.rateplanTypes; } return TEMPLATE_TYPES.filter(tempItem => tempItem); } get estimateEmailType(): any[] { if (this.templateModel.tempName && this.templateModel.tempName == 'MILITARY') { return this.sharedService.estimateEmailType; } return CUSTOMER_INTENT.filter(intentItem => intentItem); } diffMonthlyBill(val: any) { if (val) { if (Number(val) > Number(this.templateModel.estMonthlyBill)) { this.diffAmount = Number(val) - Number(this.templateModel.estMonthlyBill); } else { this.diffAmount = 0; } } } }