/** * @Component Name: BillingComponent * @Module: Checkout * @Module Functionality : To display the customer information * @Creation Date: August 12,2017 * @Author: sbaireddy * @Version: 1.0 */ // import { Component, Input, OnInit, OnChanges, OnDestroy } from '@angular/core'; import { Component, Input, Output, EventEmitter, ViewChild, OnInit, OnChanges, AfterViewChecked, OnDestroy, DoCheck } from '@angular/core'; import { NgForm } from '@angular/forms'; import CartUtils from '../../../shared/cart.utils'; import { STATES } from '../../../shared/form.constants'; import * as Constants from '../../../shared/constants'; import { IShipping, IAddress, IUsableShipping } from '../../../shared/modal/common'; import CheckoutUtils from '../../../shared/checkout.utils'; import { Customer } from '../../../shared/modal/customer'; import { User } from '../../../shared/modal/user'; import { Cart } from '../../../shared/modal/cart'; import { SharedService } from '../../../shared/shared.service'; import { CustomerService } from '../../../services/customer.service'; import { CheckoutService } from '../../../services/checkout.service'; import { UserService } from '../../../services/user.service'; import { CartService } from '../../../services/cart.service'; import Utils from '../../../shared/utils'; @Component({ selector: 'billing-cmp', moduleId: module.id, templateUrl: 'billing.component.html', styleUrls: ['billing.component.css'] }) export class BillingComponent implements OnInit, OnChanges, AfterViewChecked, OnDestroy { private COMPONENT_NAME: string = 'BillingComponent'; stateItems: any[]; billingModel: any = { state: '' }; @ViewChild('billingForm') billingForm: NgForm; @Output() onFormChange = new EventEmitter(); disableFields: boolean = false; storeId: number; // update storeid user: User; @Input() cart: Cart; /*SDD changes - added onShipModesUpdate EventEmitter to refresh shipping methods if applicable*/ @Output() onShipModesUpdate = new EventEmitter(null); customerInfo: any; isValidBilling: boolean = false; isOnlyAccCart: boolean = false; isAALOrder: boolean = false; billingAddress: IAddress; refTypeExchange: string = Constants.REF_TYPE_EXCHANGE; karmaTestResult: any = {}; isBDStore: boolean = false; isExchangeOrder: boolean = false; isEmailAvailable: boolean = false; constructor( public sharedService: SharedService, public checkoutService: CheckoutService, public customerService: CustomerService, public cartService: CartService, public userService: UserService) { console.log('[start]-CustomerComponent.constructor()'); } /** * 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 and customer information * @return {void} */ ngOnInit(): void { const METHOD_NAME: string = 'ngOnInit()'; console.log('[start]-CustomerComponent.ngOnInit()'); this.stateItems = STATES.filter(stateItem => stateItem); // Start-Switch Channel-Store id update this.sharedService.storeId.subscribe((val: number) => { this.storeId = val; }); // getting user profile. this.user = this.userService.getUser(); // getting the customer info updates this.customerInfo = this.checkoutService.getCustomerInfo(); console.log('[end]-CustomerComponent.ngOnInit()'); if (!(this.user.permissions.isSupervisor || this.user.permissions.isWTS)) { this.billingModel.validDealerCode = true; } else if (this.cart.dealerCode || this.isAALOrder || this.isExchangeOrder) { this.billingModel.validDealerCode = true; } else { this.billingModel.validDealerCode = false; } this.updateBillingAddress(); this.isBDStore = this.storeId === Constants.STORE_ID_BD ? true : false; this.isExchangeOrder = (this.cart && this.cart.refType === this.refTypeExchange) ? true : false; // calling to check if email id already exists in case of AAL flow. else force agent to enter email id this.isEmailExists(); } /** * This function is called whenever one or more data-bound input properties change * @return {void} */ ngOnChanges(): void { const METHOD_NAME: string = 'ngOnChanges()'; if (this.cart) { if (this.cart.customer) { this.isExchangeOrder = (this.cart.refType === this.refTypeExchange) ? true : false; 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.isExchangeOrder && this.cart.customer.creditProfile.accountNumber) ? true : false; } this.billingAddress = { 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 == "PR") ? '' : this.cart.customer.state, zipCode: this.cart.customer.zipCode, phone1: this.cart.customer.phone1, phone2: null, email1: this.cart.customer.email1, email2: null }; this.billingModel = Object.assign({}, this.billingAddress); this.billingModel.isValidBilling = true; this.billingModel.phone1 = Utils.formatPhoneNumber(this.billingModel.phone1); if (this.isAALOrder && !this.billingAddress.email1) { this.billingModel.isValid = false; } if(this.isAALOrder && !this.billingAddress.state){ this.billingModel.isValidBilling = false; } } this.isOnlyAccCart = CheckoutUtils.isAccOnlyOrder(this.cart); this.billingModel.dealerCode = this.cart.dealerCode; this.billingModel.validDealerCode = (this.cart.dealerCode) ? true : this.billingModel.validDealerCode; this.billingModel.webOrderId = this.cart.webOrderId; this.billingModel.retailStoreId = this.cart.retailStoreId; } } /** * This function is called after view checked * @returns void */ ngAfterViewChecked(): void { this.onFormChange.emit(this.billingForm); } /** * 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()'; console.log('BillingComponent.ngOnDestroy()'); this.billingModel = null; this.user = null; this.cart = null; this.customerInfo = null; this.billingAddress = null; this.karmaTestResult = null; this.stateItems = null; } /** * This function is used to validate the billing address by calling validateAddress * function in customer service. * @param {any} element - contains validate address element * @return {void} */ validateAddress(element : any): void { const METHOD_NAME: string = 'validateAddress()'; if ((!element) || (element.valid && (!this.billingAddress || this.billingAddress[element.name] !== element.value))) { if (this.billingModel.firstName && this.billingModel.lastName && this.billingModel.address1 && this.billingModel.city && this.billingModel.state && this.billingModel.zipCode) { this.billingModel.isValidBilling = false; // this.updateAddress(null); const inputData: any = CartUtils.validateAddressInput(this.billingModel); this.sharedService.showSpinner(true); // spinner this.customerService.validateAddress(this.storeId, inputData).subscribe(result => { this.karmaTestResult = result; this.sharedService.showSpinner(false); // spinner console.log('validate address success'); if (result.statusCode === Constants.ORDER_STATUS_S) { this.billingModel.isValidBilling = true; this.billingModel.address1 = result.addressLine1; this.billingModel.address2 = result.addressLine2; this.billingModel.city = result.city; this.billingModel.state = result.stateCode; if(result.stateCode ==="PR") { this.sharedService.showErrorMessage('DISCOURAGE_PUERTO_RICO_CUSTOMERS',false); this.billingModel.state = ''; this.billingModel.isValidBilling = false; } this.billingModel.zipCode = Utils.trimZipCode(result.zipCode); this.updateBillingAddress(); this.sharedService.invalidAdress = false; /*SDD changes - refresh shipping methods if billing address is same as shipping address*/ // this.getShippingModes(this.billingModel.zipCode); if (this.customerInfo.sameAsBilling === true) { this.getShippingModes(this.billingModel.zipCode); } this.checkoutService.setCustomerInfo(this.customerInfo); } else { this.sharedService.showErrorMessage('_ERR_INVALID_ADDRESS', false); } }, error => { this.sharedService.showSpinner(false); // spinner this.sharedService.showErrorMessage(error, true); console.error('validate address error=>' + error) }); } } } /** * This function is used to validate the dealer code by calling validateDealerCode function in user service * Billing address will be updated if the dealer code is valid. * @param {any} element - contains validate dealer code element * @return {void} */ validateDealerCode(element: any) { const METHOD_NAME: string = 'validateDealerCode()'; this.sharedService.addLogging(this.COMPONENT_NAME, METHOD_NAME, 'Validating dealer code'); if (element.value && element.valid && element.value.length === 8 && Utils.checkDealerCode(element.value)) { const inputData: any = { dealerCode: element.value }; const userId = this.userService.getUser().userId; this.sharedService.showSpinner(true); // spinner this.userService.validateDealerCode(this.storeId, userId, inputData).subscribe(result => { this.karmaTestResult = result; this.sharedService.showSpinner(false); // spinner if (!result.isDealerValid) { this.sharedService.showErrorMessage('_ERR_DEALER_CODE_NOT_FOUND', false); } else { console.log('valid dealer code.'); this.billingModel.validDealerCode = true; this.updateBillingAddress(); } }, error => { this.sharedService.showSpinner(false); // spinner this.sharedService.showErrorMessage(error, true); console.error('error=>' + error); }); } else { this.sharedService.showErrorMessage('VALIDATEDEALER_ERROR_CODE_2009', false); } } /** * This function is used to update the billing address * @return {void} */ updateBillingAddress(): void { const METHOD_NAME: string = 'updateBillingAddress()'; // this.sharedService.addLogging(this.COMPONENT_NAME, METHOD_NAME, 'Updating billing address'); this.customerInfo.billingAddress = this.billingModel; // this.customerInfo.validDealerCode = this.billingModel.validDealerCode; this.customerInfo.validDealerCode = this.customerInfo.validDealerCode ? this.customerInfo.validDealerCode : this.billingModel.validDealerCode; this.billingModel.isValid = ((this.billingModel.firstName && this.billingModel.firstName.length > 0) || (this.billingModel.lastName && this.billingModel.lastName.length > 0) || (this.billingModel.address1 && this.billingModel.address1.length > 0) || (this.billingModel.city && this.billingModel.city.length > 0) || (this.billingModel.state && this.billingModel.state.length > 0) || (this.billingModel.zipCode && this.billingModel.zipCode.length > 0) ) && (this.billingModel.email1 && this.billingModel.email1.length > 0); console.log('isvalid of billing model: ' + this.billingModel.isValid); } /** * This function is used to get the shipping modes * @param {string} zipCode - Zipcode of the customer * @return {void} */ getShippingModes(zipCode: string): void { const METHOD_NAME: string = 'getShippingModes()'; this.sharedService.showSpinner(true); // spinner this.checkoutService.getUsableShippingInfo(this.storeId, this.cart.orderId, zipCode).subscribe(result => { this.karmaTestResult = result; this.sharedService.showSpinner(false); // spinner this.onShipModesUpdate.emit(result); console.log('Success'); // console.log( this.usableShipping); }, error => { this.sharedService.showSpinner(false); // spinner this.sharedService.showErrorMessage(error, true); console.error('error=>' + error); }); } /** * This function is used to check if email already exists in the billingInfo AAL * @return {void} */ isEmailExists(): void { this.isEmailAvailable = this.billingModel.email1 ? true : false; } }