/** * @Component Name: ShippingComponent * @Module: Checkout * @Module Functionality : To display the customer information * @Creation Date: August 12,2017 * @Author: sbaireddy * @Version: 1.0 */ import { Component, Output, EventEmitter, ViewChild, OnInit, Input, OnChanges, AfterViewChecked, OnDestroy } from '@angular/core'; import { NgForm } from '@angular/forms'; import Utils from '../../../shared/utils'; 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 { Cart } from '../../../shared/modal/cart'; import { SharedService } from '../../../shared/shared.service'; import { CustomerService } from '../../../services/customer.service'; import { CheckoutService } from '../../../services/checkout.service'; import { CartService } from '../../../services/cart.service'; @Component({ selector: 'shipping-cmp', moduleId: module.id, templateUrl: 'shipping.component.html', styleUrls: ['shipping.component.css'] }) export class ShippingComponent implements OnInit, OnChanges, AfterViewChecked, OnDestroy { private COMPONENT_NAME: string = 'ShippingComponent'; stateItems: any[]; shippingModel: any = { state: '' }; @ViewChild('shippingForm') shippingForm: NgForm; @Output() onFormChange = new EventEmitter(); @Input() cart: Cart; @Output() onShipModesUpdate = new EventEmitter(null); storeId: number; // update storeid customerInfo: any; karmaTestResult: any = {}; shippingInfo: IShipping; constructor( public sharedService: SharedService, public customerService: CustomerService, public cartService: CartService, public checkoutService: CheckoutService) { console.log('[start]-ShippingComponent.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 all the states, payment methods and card types * @returns void */ ngOnInit(): void { const METHOD_NAME: string = 'ngOnInit()'; console.log('[start]-ShippingComponent.ngOnInit()'); this.stateItems = STATES.filter(stateItem => stateItem); // Start-Switch Channel-Store id update this.sharedService.storeId.subscribe((val: number) => { this.storeId = val; }); // getting the customer info updates this.customerInfo = this.checkoutService.getCustomerInfo(); /*SDD changes - set sameAsBilling in customerInfo to use it in billing.component as a filter*/ if (this.customerInfo && this.customerInfo.sameAsBilling === false) { this.shippingModel.sameAsBilling = this.customerInfo.sameAsBilling; this.shippingModel.isValidShipping = this.customerInfo.shippingAddress.isValidShipping; this.setShippingAddress(this.customerInfo.shippingAddress); this.getShippingModes(this.customerInfo.shippingAddress.zipCode); } else { this.getShippingAddress(this.cart.orderId); } console.log('[end]-ShippingComponent.ngOnInit()'); } /** * This function is called whenever one or more data-bound input properties change * @returns void */ ngOnChanges(): void { const METHOD_NAME: string = 'ngOnChanges()'; console.log('Changes the objects initilized.'); if (this.cart && this.cart.shippingInfo && this.cart.shippingInfo.address) { this.shippingModel = Object.assign({}, this.cart.shippingInfo.address); this.shippingModel.sameAsBilling = false; if (this.cart.customer && this.cart.customer.addressId) { if (this.cart.customer.addressId === this.shippingModel.addressId) { this.shippingModel.sameAsBilling = true; this.shippingModel.state = ''; } } // added for sdd refresh if (this.shippingModel.sameAsBilling === false) { this.getShippingModes(this.shippingModel.zipCode); } } } /** * This function is called after view checked * @returns void */ ngAfterViewChecked(): void { this.onFormChange.emit(this.shippingForm); } /** * 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()'; console.log('Destroying the objects initilized.'); this.shippingModel = null; this.cart = null; this.customerInfo = null; this.karmaTestResult = null; this.stateItems = null; this.shippingInfo = null; } /** * This function is used to validate the address entered by user. * @param {any} element contains address fields entered by user. * @returns void */ validateAddress(element: any): void { const METHOD_NAME: string = 'validateAddress()'; // this.cart.shippingInfo.address if (element && element.valid && (!this.shippingModel || !this.cart.shippingInfo.address || !this.cart.shippingInfo.address.addressId || this.shippingModel['address1'] !== this.cart.shippingInfo.address.address1 || this.shippingModel['address2'] !== this.cart.shippingInfo.address.address2)) { if (this.shippingModel.address1 && this.shippingModel.city && this.shippingModel.state && this.shippingModel.zipCode) { this.shippingModel.isValidShipping = false; this.customerInfo.shippingAddress = null; this.customerInfo.isValidShipping = this.shippingModel.isValidShipping; this.checkoutService.setCustomerInfo(this.customerInfo); const inputData: any = CartUtils.validateAddressInput(this.shippingModel); // this.sharedService.showSpinner(true);// spinner this.customerService.validateAddress(this.storeId, inputData).subscribe(result => { this.karmaTestResult = result; // this.sharedService.showSpinner(false);// spinner if (result.statusCode === Constants.ORDER_STATUS_S) { this.shippingModel.isValidShipping = true; this.customerInfo.isValidShipping = this.shippingModel.isValidShipping; this.shippingModel.address1 = result.addressLine1; this.shippingModel.address2 = result.addressLine2; this.shippingModel.city = result.city; this.shippingModel.state = result.stateCode; this.shippingModel.zipCode = Utils.trimZipCode(result.zipCode); this.customerInfo.shippingAddress = this.shippingModel; this.getShippingModes(this.shippingModel.zipCode); this.checkoutService.setCustomerInfo(this.customerInfo); // this.onFormChange.emit(this.shippingForm); } else { this.sharedService.showErrorMessage('_ERR_INVALID_SHIPPING_ADDRESS', false); } }, error => { // this.sharedService.showSpinner(false);// spinner this.sharedService.showErrorMessage(error, true); // console.error('validate shipping address error=>'+ error) }); } } } /** * This function is used to get the shipping information including shipping address through * getCartShipping function in cart service * @param {number} orderId - order id to refer the order details * @return {void} */ getShippingAddress(orderId: number): void { const METHOD_NAME: string = 'getShippingAddress()'; console.log('CustomerInfoComponent.getShippingAddress()>>Start.'); this.sharedService.showSpinner(true); // spinner this.cartService.getCartShipping(this.storeId, orderId).subscribe(result => { this.sharedService.showSpinner(false); // spinner this.shippingInfo = result; this.cart.shippingInfo = result; this.setShippingAddressDefault(); if (result.address) { this.getShippingModes(result.address.zipCode); } else if (this.cart.customer && this.cart.customer.zipCode) { this.getShippingModes(this.cart.customer.zipCode); } else { this.getShippingModes(null); } }, error => { this.sharedService.showSpinner(false); // spinner this.sharedService.showErrorMessage(error, true); // console.error('error=>' + error); // getting the usable shipping modes if (this.cart.customer && this.cart.customer.zipCode) { this.getShippingModes(this.cart.customer.zipCode); } else { this.getShippingModes(null); } }); } /** * This function is used to set shipping to Billing Or shipping or not to refresh shipping methods * whenever same shipping address flag is checked or unchecked. * @returns void */ setShippingAddressDefault(): void { const METHOD_NAME: string = 'setShippingAddressDefault()'; if (this.cart.shippingInfo && this.cart.shippingInfo.address && this.cart.customer.addressId !== this.cart.shippingInfo.address.addressId) { this.shippingModel.sameAsBilling = false; this.setShippingAddress(this.cart.shippingInfo.address); } else { this.shippingModel.sameAsBilling = true; } this.shippingModel.isValidShipping = (this.cart.shippingInfo && this.cart.shippingInfo.address && this.cart.shippingInfo.address.addressId) ? true : this.shippingModel.sameAsBilling; } /** * SDD changes - This function is used to modified to refresh shipping methods * whenever same shipping address flag is checked or unchecked. * @returns void */ switchShippingAddress(event): void { const METHOD_NAME: string = 'switchShippingAddress()'; this.shippingModel = {}; this.sharedService.addLogging(this.COMPONENT_NAME, METHOD_NAME, 'Switching shipping address'); this.shippingModel.sameAsBilling = event.target.checked; // !this.shippingModel.sameAsBilling; this.customerInfo.sameAsBilling = this.shippingModel.sameAsBilling; if (this.shippingModel.sameAsBilling === true) { const add = (this.cart && this.cart.shippingInfo && this.cart.shippingInfo.address) ? this.cart.shippingInfo.address : this.cart.customer; this.setShippingAddress(add); this.shippingModel.isValidShipping = true; this.customerInfo.isValidShipping = this.shippingModel.isValidShipping; this.getShippingModes(this.cart.customer.zipCode); } else { if (this.cart.shippingInfo && this.cart.shippingInfo.address && this.cart.shippingInfo.address.addressId && this.cart.customer && this.cart.customer.addressId && this.cart.customer.addressId !== this.cart.shippingInfo.address.addressId) { this.setShippingAddress(this.cart.shippingInfo); this.shippingModel.isValidShipping = true; this.customerInfo.isValidShipping = this.shippingModel.isValidShipping; this.getShippingModes(this.shippingModel.zipCode); } else { const add = (this.customerInfo.shippingAddress) ? this.customerInfo.shippingAddress : null; this.setShippingAddress(add); this.shippingModel.isValidShipping = (add && add.isValidShipping) ? add.isValidShipping : false; this.customerInfo.isValidShipping = this.shippingModel.isValidShipping; } } this.checkoutService.setCustomerInfo(this.customerInfo); // this.onFormChange.emit(this.shippingForm); } // /** // * This function is used to update the shipping address using checkout service // * if the address is valid // * @returns void // */ // updateShippingAddress(): void { // const METHOD_NAME: string = "updateShippingAddress()"; // this.sharedService.addLogging(this.COMPONENT_NAME, METHOD_NAME, 'Updating shipping address'); // if (this.shippingModel.isValidShipping === true) { // let inputData: any = { shippingAddress: CartUtils.createAddressInput(this.shippingModel) }; // this.sharedService.showSpinner(true);// spinner // this.checkoutService.updateCustomerInfo(this.storeId, this.cart.orderId, inputData).subscribe(result => { // this.karmaTestResult = result; // this.sharedService.showSpinner(false);// spinner // // this.customerInfo.shippingAddress = null; // this.getShippingModes(this.shippingModel.zipCode); // }, error => { // this.sharedService.showSpinner(false);// spinner // this.sharedService.showErrorMessage(error, true); // // console.error('updating the shipping address error=>'+ error) // }); // } else { // this.sharedService.showErrorMessage("_ERR_INVALID_SHIPPING_ADDRESS", false); // } // } /** * This function is used get the shipping information from getUsableShippingInfo * of checkoutservice when zipcode is valid. * @param {string} zipCode contains valid zipcode. * * this.getShippingAddress(this.cart.orderId); * @returns void */ getShippingModes(zipCode: string): void { const METHOD_NAME: string = 'getShippingModes()'; if (this.storeId && this.cart && this.cart.orderId) { 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( this.usableShipping); }, error => { this.sharedService.showSpinner(false); // spinner this.sharedService.showErrorMessage(error, true); // console.error('error=>'+ error); }); } } /** * This function is used set the shipping information from cart shipping details * of checkoutservice when zipcode is valid. * @param {object} address contains valid shipping address. * @returns void */ setShippingAddress(val): void { const METHOD_NAME: string = 'setShippingAddress()'; this.shippingModel.state = (val && val.state) ? val.state : ''; this.shippingModel.address2 = (val && val.address2) ? val.address2 : ''; this.shippingModel.address1 = (val && val.address1) ? val.address1 : ''; this.shippingModel.city = (val && val.city) ? val.city : ''; this.shippingModel.zipCode = (val && val.zipCode) ? val.zipCode : ''; this.shippingModel.addressId = (val && val.addressId) ? val.addressId : null; } }