/** * @Component Name: OrderPaymentComponent * @Module: Home * @Module Functionality : To display the order payment section * @Creation Date: August 12,2017 * @Author: sbaireddy * @Version: 1.0 */ import { Component, Input, OnDestroy, OnInit, OnChanges } from '@angular/core'; import { MAX_PED_RESEND_DAYS } from '../../../../shared/constants'; import { TEMPLATE_TYPES, CUSTOMER_INTENT } from '../../../../shared/form.constants'; import { User } from '../../../../shared/modal/user'; import { Cart } from '../../../../shared/modal/cart'; import { Customer } from '../../../../shared/modal/customer'; import CartUtils from '../../../../shared/cart.utils'; import { SharedService } from '../../../../shared/shared.service'; import { UserService } from '../../../../services/user.service'; import { CartService } from '../../../../services/cart.service'; import { LookupService } from '../../../../services/lookup.service'; import { CheckoutService } from '../../../../services/checkout.service'; @Component({ selector: 'purchase-estimate-cmp', moduleId: module.id, templateUrl: 'purchase-estimate.component.html', styleUrls: ['purchase-estimate.component.css'] }) export class PurchaseEstimateComponent implements OnInit, OnDestroy { private COMPONENT_NAME: string = 'PurchaseEstimateComponent'; storeId: number; templateTypes: any[]; custIntents: any[]; user: User; @Input() cart: Cart; customer: Customer; emailSent: boolean = false; showPurchaseEst: boolean = false templateModel: any = { languageOption: true, tempName: null, custIntent: null, storeEmail: null, customerEmail: null }; enableResendBtn: boolean = true; constructor( public sharedService: SharedService, public userService: UserService, public cartService: CartService, public lookupService: LookupService, public checkoutService: CheckoutService ) { } /** * 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. * @returns void */ ngOnInit(): void { const METHOD_NAME: string = 'ngOnInit()'; this.templateTypes = TEMPLATE_TYPES.filter(tempItem => tempItem); this.custIntents = CUSTOMER_INTENT.filter(intentItem => intentItem); this.sharedService.storeId.subscribe((val: number) => { this.storeId = val; }); this.user = this.userService.getUser(); this.cart = this.lookupService.getOrder(); if (this.cart && this.cart.submittedByDate) { let submittedDate = new Date(this.cart.submittedByDate); if (submittedDate) { const timeDiff = Math.abs(submittedDate.getTime() - new Date().getTime()); const elapsedDays = Math.ceil(timeDiff / (1000 * 3600 * 24)); console.log("Total Elapsed Days:" + elapsedDays); if (elapsedDays > MAX_PED_RESEND_DAYS) { this.enableResendBtn = false; } } } this.customer = this.cart && this.cart.customer ? this.cart.customer : null; if (this.customer && this.customer.email1) { this.templateModel.customerEmail = this.customer.email1; } } /** * 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.cart = null; } sendQuoteToCustomer(): void { if (this.customer && this.customer.email1 !== this.templateModel.customerEmail) { let inputData: any = { addressId: this.customer.addressId }; if (this.customer.email1 !== this.templateModel.email1) { inputData.email1 = this.templateModel.customerEmail; } 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('Customer:' + this.customer); this.sendPurchaseEstimate(); }, error => { this.sharedService.showSpinner(false); // spinner this.sharedService.showErrorMessage(error, true); console.error('error=>' + error); }); } else { this.sendPurchaseEstimate(); } } sendPurchaseEstimate(): void { if (this.customer && this.customer.email1) { this.emailSent = false; 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 this.emailSent = true; this.cart.customer.orderNotes = { agentId: this.user.logonId, subject: 'Estimate Email sent', description: 'Purchase Estimate was sent to : ' + this.templateModel.customerEmail }; this.sharedService.addOrderNotes(this.storeId, this.cart.orderId, this.cart.customer.orderNotes); }, 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); } }