/** * @Component Name: DummyCheckComponent * @Module: Home * @Module Functionality : To display the customer dummy check information * @Creation Date: August 16,2018 * @Author: sbaireddy * @Version: 1.0 */ import { Component, Input, OnInit, OnChanges, OnDestroy, ViewChild } from '@angular/core'; import { NgForm } from '@angular/forms'; import Utils from '../../../shared/utils'; import CartUtils from '../../../shared/cart.utils'; import * as Constants from '../../../shared/constants'; 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 { UserService } from '../../../services/user.service'; import { CartService } from '../../../services/cart.service'; import { CustomerService } from '../../../services/customer.service'; @Component( { moduleId: module.id, selector: 'dummy-check-cmp', templateUrl: 'dummy-check.component.html', styleUrls: ['dummy-check.component.css'] }) export class DummyCheckComponent implements OnInit, OnChanges, OnDestroy { private COMPONENT_NAME: string = 'DummyCheckComponent'; karmaTestResult: any = {}; storeId: number; // update storeid @Input() creditType: string; @Input() customerModel: any; // getting the customer address info from customer component. @Input() customerForm: any; @ViewChild('conflictModel') conflictModel: any; creditDetails: any; dummyCheckModel: any = { creditClass: null, yesAuthorize: false}; user: User; cart: Cart; customer: Customer; subscription: any; hasItemsInCart: boolean = false; creditTypeNoCredit: string = Constants.CREDIT_TYPE_NC; constructor( public sharedService: SharedService, public userService: UserService, public cartService: CartService, public customerService: CustomerService) { } /** * 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, ID Types, user and cart informations * @return {void} */ ngOnInit(): void { const METHOD_NAME: string = 'ngOnInit()'; // Start-Switch Channel-Store id update this.sharedService.updateErrorMessage(null); 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(); this.subscription = this.cartService.hasUpdate.subscribe((item) => { if (item) { this.cart = this.cartService.getCart(); } }); if(this.cart && this.cart.packages) { if(this.creditType === Constants.CREDIT_TYPE_CC) { this.dummyCheckModel.creditClass = Constants.CREDIT_CLASS_C; }else if(this.creditType === Constants.CREDIT_TYPE_NC || CartUtils.isNCFPlanInCart(this.cart)) { this.dummyCheckModel.creditClass = Constants.CREDIT_CLASS_Q; }else{ this.dummyCheckModel.creditClass = Constants.CREDIT_CLASS_A; } }else{ this.dummyCheckModel.creditClass = Constants.CREDIT_CLASS_A; } this.creditDetails = this.customerService.getCreditDetails(); if(!this.creditDetails) this.initCreditDetails(); if (this.cart && this.cart.packages && this.cart.packages.length > 0) { this.hasItemsInCart = true; } } /** * This function is called whenever one or more data-bound input properties change * @return {void} */ ngOnChanges(): void { console.log('is customer form valid:' + this.customerForm.valid); } /** * 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.dummyCheckModel = null; this.karmaTestResult = null; this.customerModel = null; this.user = null; this.cart = null; this.customer = null; // unsubscribing the subscription this.subscription.unsubscribe(); } initCreditDetails():void{ const METHOD_NAME: string = 'initCreditDetails()'; this.sharedService.showSpinner(true); // spinner this.customerService.getCreditOptionDetails(this.storeId).subscribe(result => { this.karmaTestResult = 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); }); } /** * This function is used to check the credit eligibility of a customer based on their * credit history * @return void */ checkEligibility(): void { const METHOD_NAME: string = 'checkEligibility()'; if (this.customerForm.valid) { // checking parent form is valid first console.log('[start] - DummyCheckComponent.checkEligibility()>>checking eligibility.'); this.sharedService.addLogging(this.COMPONENT_NAME, METHOD_NAME, 'Checking dummy credit eligibility'); if (this.cart) { if (this.hasItemsInCart) { this.conflictModel.show(); } else { this.processDummyCheck(this.cart.orderId); } } else { console.log('Creating a cart for save the customer data.'); const cartInputData: any = CartUtils.createCartInput(this.storeId, this.user); this.sharedService.showSpinner(true); // spinner this.cartService.createCartDetails(this.storeId, cartInputData).subscribe(result => { this.karmaTestResult = result; this.sharedService.showSpinner(false); // spinner this.cart = new Cart(); this.cart = Object.assign(this.cart, result); //this.cartService.setCart(result); this.processDummyCheck(result.orderId); }, error => { this.sharedService.showSpinner(false); // spinner console.error('error=>' + error); this.sharedService.showErrorMessage(error, true); }); } console.log('[end] - DummyCheckComponent.checkEligibility()>>checking eligibility.'); } } /** * This function is used to perform the customer credit check through * creditCheck function in customer service * @param {number} orderId - order id to refer the order details * @return void */ processDummyCheck(orderId: number): void { const METHOD_NAME: string = 'processDummyCheck()'; console.log('DummyCheckComponent.processDummyCheck()>>checking dummy eligibility.'); if( CartUtils.canValidateAddress(this.customerModel) ) { this.sharedService.showSpinner(true); // spinner const inputData: any = CartUtils.validateAddressInput(this.customerModel); this.customerService.validateAddress(this.storeId, inputData).subscribe(result => { this.karmaTestResult = result; this.sharedService.showSpinner(false); // spinner if (result.statusCode === Constants.ORDER_STATUS_S) { this.customerModel.address1 = result.addressLine1, this.customerModel.address2 = result.addressLine2, this.customerModel.city = result.city, this.customerModel.state = result.stateCode, this.customerModel.zipCode = Utils.trimZipCode(result.zipCode), this.submitCustomerDetail( orderId ); } 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) }); }else{ this.submitCustomerDetail( orderId ); } } /** * This function is used for clearing the form elements * @returns void */ submitCustomerDetail(orderId: number):void{ const METHOD_NAME: string = 'submitCustomerDetail()'; let inputData: any = { orderId: this.cart.orderId, firstName: this.customerModel.firstName, lastName: this.customerModel.lastName, middleName: (this.customerModel.middleName)?this.customerModel.middleName:'', address1: this.customerModel.address1?this.customerModel.address1:'', address2: this.customerModel.address2?this.customerModel.address2:'', city: this.customerModel.city?this.customerModel.city:'', state: this.customerModel.state?this.customerModel.state : '', zipCode: this.customerModel.zipCode?this.customerModel.zipCode:'', phone1: Utils.nonFormatPhoneNumber(this.customerModel.phone1)?Utils.nonFormatPhoneNumber(this.customerModel.phone1):'', email1: this.customerModel.email1 } inputData.creditType = Constants.CREDIT_TYPE_CC; if(this.dummyCheckModel.creditClass) { if (this.dummyCheckModel.creditClass === Constants.CREDIT_CLASS_Q) { inputData.creditType = Constants.CREDIT_TYPE_NC; } inputData = Object.assign(inputData, Utils.getCreditProfile(this.dummyCheckModel.creditClass)); }else { 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.cart.customer = inputData; this.cart.customer.addressId = result.addressId; this.cartService.setCart( this.cart); if (this.cart.packages && this.cart.packages.length > 0) { this.removeConflicts(); } //Auto-Memo will indicate memo type: Credit Selection, and include: TOM Login, Date, Time, //Credit class selected, and text indicating Manual selection was completed if(this.dummyCheckModel && this.dummyCheckModel.creditClass){ this.cart.customer.orderNotes = { agentId: this.user.logonId, subject: 'Credit Selection Estimate', description: 'Credit class manually selected for use with Purchase Estimate, This order can only be used for generating a Purchase Estimate Email' // description: 'Credit Selection '+ this.dummyCheckModel.creditClass + ' and at a login of ' + //this.user.logonTime + 'Credit class selected, and text indicating Manual selection was completed ' }; this.sharedService.addOrderNotes(this.storeId, this.cart.orderId, this.cart.customer.orderNotes); } this.sharedService.updateCreditChoice(inputData.creditType); }, error => { this.sharedService.showSpinner(false); // spinner this.sharedService.showErrorMessage(error, true); console.error('error=>' + error); }); } /** * This function is used for clearing the form elements * @returns void */ resetDummyCheck(frm: NgForm): void { const METHOD_NAME: string = 'resetDummyCheck()'; //frm.form.reset(); frm.controls.creditClass.reset(); this.dummyCheckModel = { creditClass: null, yesAuthorize: false }; this.customerService.setCustomerModel(null); this.customerForm.reset(); } /** * This function is used to resolve the conflicted data after * the process credit check * @return void */ removeConflicts(): void { const METHOD_NAME: string = 'removeConflicts()'; const orderId = this.cart.orderId; this.sharedService.showSpinner(true); // spinner this.cartService.resolveConflicts(this.storeId, orderId, { 'resolveType': 'NEW' }).subscribe(result => { this.karmaTestResult = result; //this.sharedService.showSpinner(false); // spinner //this.sharedService.showSpinner(false); // spinner this.cartService.getCartSummary(this.storeId, orderId).subscribe(result => { this.sharedService.showSpinner(false); // spinner this.cart = this.cartService.getCart(); if(this.cart ){ this.cart = Object.assign(this.cart, result); this.cartService.setCart( this.cart); } }, error => { this.sharedService.showSpinner(false); // spinner this.sharedService.showErrorMessage(error, true); console.error('error=>' + error); }); }, error => { this.sharedService.showSpinner(false); // spinner console.error('error=>' + error); this.sharedService.showErrorMessage(error, true); }); } /** * This function is used to resolve conflicts * @returns void */ resolveConflicts(): void { const METHOD_NAME: string = 'resolveConflicts()'; this.hasItemsInCart = false; this.conflictModel.hide(); this.checkEligibility(); } }