import { Component, ViewEncapsulation, OnInit, OnDestroy } from '@angular/core'; import { Router, ActivatedRoute, Params } from '@angular/router'; import * as Constants from './quote.constants'; import { SharedService } from '../shared/shared.service'; import { UserService } from '../services/user.service'; declare var jQuery: any; @Component( { selector: 'quote-cmp', moduleId: module.id, encapsulation: ViewEncapsulation.None, templateUrl: './quote.component.html', styleUrls: ['./quote.component.css'] }) export class QuoteComponent implements OnInit, OnDestroy { errorMessage: string = 'Please fill the required fields.' today = new Date(); quote: any = new Object(); // this is final quote for print voicePlans: string[]; voiceOptList: any[] = Constants.VOICE_DROPDOWN_LIST; voiceOptLabel: string = Constants.VOICE_DROPDOWN_LIST[0]; beyondPlans: string[]; beyondOptList: any[] = Constants.BEYOND_DROPDOWN_LIST; beyondOptLabel: string = Constants.BEYOND_DROPDOWN_LIST[0]; services: string[] serviceOptList: any[] = Constants.SERVICE_DROPDOWN_LIST; serviceOptLabel: string = Constants.SERVICE_DROPDOWN_LIST[0]; reimbursements: string[]; reimburseOptList: any[] = Constants.REIMBURSE_DROPDOWN_LIST; reimburseOptLabel: string = Constants.REIMBURSE_DROPDOWN_LIST[0]; sskAndUses: string[]; sskUseOptList: any[] = Constants.SSKUSC_DROPDOWN_LIST; sskUseOptLabel: string = Constants.SSKUSC_DROPDOWN_LIST[0]; constructor( private route: ActivatedRoute, public router: Router, public sharedService: SharedService, public userService: UserService) {} ngOnInit() { this.initQuote(); } ngOnDestroy() { this.quote = null; this.voicePlans = null; this.voiceOptList = null; this.beyondOptList = null; this.services = null; this.serviceOptList = null; this.reimbursements = null; this.reimburseOptList = null; this.sskAndUses = null; this.sskUseOptList = null; this.beyondPlans = null; } initQuote(): void { this.errorMessage = null; this.quote = new Object(); this.quote.customerName = null; this.quote.customerEmail = null; this.quote.voicePlans = [ {item: {name: this.voiceOptLabel, price: null}, promo: {name: null, price: null}}]; this.quote.beyondPlans = [ {item: {name: this.beyondOptLabel, price: null}, promo: {name: null, price: null}}]; this.quote.totalPlans = 0; this.quote.devices = [ {item: {name: null, price: null}, promo: {name: null, price: null}}]; this.quote.totalDevice = 0; this.quote.services = [ {item: {name: this.serviceOptLabel, price: null}, promo: {name: null, price: null}}]; this.quote.totalService = 0; // for deposits this.quote.deposits = [ {item: {name: null, price: null}}]; this.quote.deviceDownPayments = [ {item: {name: null, price: null}}]; this.quote.accessories = [ {item: {name: null, price: null}}]; this.quote.simUscs = [ {item: {name : this.sskUseOptLabel, price: null}}]; this.quote.tradeIns = [ {item: {name: null, price: null}}]; this.quote.rabates = [ {item: {name: null, price: null}}]; this.quote.reimbursements = [ {item: {name: this.reimburseOptLabel, price: null}}]; this.quote.totalOutOfPocket = 0; this.quote.total = 0; this.quote.agentName = null; this.quote.agentEmail = null; this.quote.storeAddress = null; this.quote.storeNumber = null; this.quote.todayDate = null; jQuery('myPage').scrollTop(0); window.scrollTo(0, 0); } addVoicePlan(): void { if (!this.quote.voicePlans) { this.quote.voicePlans = new Array(); } this.quote.voicePlans.push( {item: {name: this.voiceOptLabel, price: null}, promo: {name: null, price: null}}); } removeVoicePlan(voicePlan: any, index: number): void { if ( this.quote.voicePlans && this.quote.voicePlans.length > 1) { this.quote.voicePlans.splice(index, 1); } } addBeyondPlan(): void { if (!this.quote.beyondPlans) { this.quote.beyondPlans = new Array(); } this.quote.beyondPlans.push( {item: {name: this.beyondOptLabel, price: null}, promo: {name: null, price: null}}); } removeBeyondPlan(beyondPlan: string, index: number): void { if (this.quote.beyondPlans && this.quote.beyondPlans.length > 1) { this.quote.beyondPlans.splice(index, 1); } } get totalPlansAmount(): number { let amount: number = 0; if (this.quote.voicePlans) { for (const voicePlan of this.quote.voicePlans) { if (voicePlan.item && voicePlan.item.price) { amount += parseFloat(voicePlan.item.price); } if (voicePlan.promo && voicePlan.promo.price) { amount += parseFloat(voicePlan.promo.price); } } } if (this.quote.beyondPlans) { for (const beyondPlan of this.quote.beyondPlans) { if (beyondPlan.item && beyondPlan.item.price) { amount += parseFloat(beyondPlan.item.price); } if (beyondPlan.promo && beyondPlan.promo.price) { amount += parseFloat(beyondPlan.promo.price); } } } this.quote.totalPlans = amount; return amount; } addDevice(): void { if (!this.quote.devices) { this.quote.devices = new Array(); } this.quote.devices.push( {item: {name: null, price: null}, promo: {name: null, price: null}}); } removeDevice(device: any, index: number): void { if (this.quote.devices && this.quote.devices.length > 1) { this.quote.devices.splice(index, 1); } } get totalDeviceAmount(): number { let amount: number = 0; if (this.quote.devices) { for (const item of this.quote.devices) { if (item.item && item.item.price) { amount += parseFloat(item.item.price); } if (item.promo && item.promo.price) { amount += parseFloat(item.promo.price); } } } this.quote.totalDevice = amount; return amount; } addService(): void { if (!this.quote.services) { this.quote.services = new Array(); } this.quote.services.push( {item: {name: this.serviceOptLabel, price: null}, promo: {name: null, price: null}}); } removeService(service: any, index: number): void { if (this.quote.services && this.quote.services.length > 1) { this.quote.services.splice(index, 1); } } get totalServiceAmount(): number { let amount: number = 0; if (this.quote.services) { for (const item of this.quote.services) { if (item.item && item.item.price) { amount += parseFloat(item.item.price); } if (item.promo && item.promo.price) { amount += parseFloat(item.promo.price); } } } this.quote.totalService = amount; return amount; } // this.quote.deposits = [ {item: {name:'Line', price:null}}]; addDeposit(): void { if (!this.quote.deposits) { this.quote.deposits = new Array(); } this.quote.deposits.push( {item: {name: null, price: null}}); } removeDeposit(item: any, index: number): void { if (this.quote.deposits && this.quote.deposits.length > 1) { this.quote.deposits.splice(index, 1); } } // this.quote.deviceDownPayments = [ {item: {name:'Line', price:null}}]; addDeviceDownpayment(): void { if (!this.quote.deviceDownPayments) { this.quote.deviceDownPayments = new Array(); } this.quote.deviceDownPayments.push( {item: {name: null, price: null}}); } removeDeviceDownpayment(item: any, index: number): void { if (this.quote.deviceDownPayments && this.quote.deviceDownPayments.length > 1) { this.quote.deviceDownPayments.splice(index, 1); } } // this.quote.accessories = [ {item: {name:null, price:null}}]; addAccessory(): void { if (!this.quote.accessories) { this.quote.accessories = new Array(); } this.quote.accessories.push( {item: {name: null, price: null}}); } removeAccessory(item: any, index: number): void { if (this.quote.accessories && this.quote.accessories.length > 1) { this.quote.accessories.splice(index, 1); } } // this.quote.simUscs = [ {item: {name:this.sskUseOptLabel, price:null}}]; addSimUsc(): void { if (!this.quote.simUscs) { this.quote.simUscs = new Array(); } this.quote.simUscs.push( {item: {name: this.sskUseOptLabel, price: null}}); } removeSimUsc(item: any, index: number): void { if (this.quote.simUscs && this.quote.simUscs.length > 1) { this.quote.simUscs.splice(index, 1); } } // this.quote.tradeIns = [ {item: {name:'Line', price:null}}]; addTradeIn(): void { if (!this.quote.tradeIns) { this.quote.tradeIns = new Array(); } this.quote.tradeIns.push( {item: {name: null, price: null}}); } removeTradeIn(item: any, index: number): void { if (this.quote.tradeIns && this.quote.tradeIns.length > 1) { this.quote.tradeIns.splice(index, 1); } } get totalOutOfPocketAmount(): number { let amount: number = 0; // this.quote.deposits = [ {item: {name:'Line', price:null}}]; if (this.quote.deposits) { for (const item of this.quote.deposits) { if (item.item && item.item.price) { amount += parseFloat(item.item.price); } } } // this.quote.deviceDownPayments = [ {item: {name:'Line', price:null}}]; if (this.quote.deviceDownPayments) { for (const item of this.quote.deviceDownPayments) { if (item.item && item.item.price) { amount += parseFloat(item.item.price); } } } // this.quote.accessories = [ {item: {name:null, price:null}}]; if (this.quote.accessories) { for (const item of this.quote.accessories) { if (item.item && item.item.price) { amount += parseFloat(item.item.price); } } } // this.quote.simUscs = [ {item: {name:this.sskUseOptLabel, price:null}}]; if (this.quote.simUscs) { for (const item of this.quote.simUscs) { if (item.item && item.item.price) { amount += parseFloat(item.item.price); } } } // this.quote.tradeIns = [ {item: {name:'Line', price:null}}]; if (this.quote.tradeIns) { for (const item of this.quote.tradeIns) { if (item.item && item.item.price) { amount += parseFloat(item.item.price); } } } // this.quote.rabates = [ {item: {name:null, price:null}}]; if (this.quote.rabates) { for (const item of this.quote.rabates) { if (item.item && item.item.price) { amount += parseFloat(item.item.price); } } } // this.quote.reimbursements = [ {item: {name:this.reimburseOptLabel, price:null}}]; if (this.quote.reimbursements) { for (const item of this.quote.reimbursements) { if (item.item && item.item.price) { amount += parseFloat(item.item.price); } } } this.quote.totalOutOfPocket = amount; return amount; } get totalQuoteAmount(): number { let amount = 0; // console.log('totalQuoteAmount.this.quote.totalPlans:'+ this.quote.totalPlans); if (this.quote.totalPlans) { amount += this.quote.totalPlans; } // console.log('totalQuoteAmount.this.quote.totalDevice:'+ this.quote.totalDevice); if (this.quote.totalDevice) { amount += this.quote.totalDevice; } // console.log('totalQuoteAmount.this.quote.totalService:'+ this.quote.totalService); if (this.quote.totalService) { amount += this.quote.totalService; } // console.log('totalQuoteAmount.this.quote.total:'+ amount); this.quote.total = amount; return amount; } resetQuote(): void { this.initQuote(); } validateQuote(): boolean { let valid: boolean = true; // checking customer name/email if (!this.quote.customerName) { this.errorMessage = 'Please fill the customer name.'; valid = false; } if (valid && !this.quote.customerEmail) { this.errorMessage = 'Please fill the customer email address.'; valid = false; } if (valid && this.quote.voicePlans) { for (const plan of this.quote.voicePlans) { if (plan.item && !plan.item.price) { this.errorMessage = 'Please fill the voice plan price.'; valid = false; break; } if (plan.promo && !plan.promo.name && plan.promo.price) { this.errorMessage = 'Please fill the voice line credit (AAL Promo) name.'; valid = false; break; } } } if (valid && this.quote.devices) { for (const device of this.quote.devices) { if (device.item) { if (!device.item.name) { this.errorMessage = 'Please fill the device name.'; valid = false; break; } if (!device.item.price) { this.errorMessage = 'Please fill the device price.'; valid = false; break; } } if (device.promo) { if (!device.promo.name && device.promo.price) { this.errorMessage = 'Please fill the device EIP promo name.'; valid = false; break; } } } } if (valid && this.quote.services) { for (const service of this.quote.services) { if (service.item && !service.item.price) { this.errorMessage = 'Please fill the service price.'; valid = false; break; } } } if (valid && !this.quote.agentName) { this.errorMessage = 'Please fill the agent name.'; valid = false; } if (valid && !this.quote.agentEmail) { this.errorMessage = 'Please fill the agent email.'; valid = false; } if (valid && !this.quote.storeAddress) { this.errorMessage = 'Please fill the store address.'; valid = false; } if (valid && !this.quote.storeNumber) { this.errorMessage = 'Please fill the store number.'; valid = false; } if (valid && !this.quote.todayDate) { this.errorMessage = 'Please fill the today date.'; valid = false; } if (!valid) { window.scrollTo(0, 0); } ; return valid; } sendQuoteToCustomer(): void { console.log('sending a quote to customer.'); if (this.validateQuote()) { this.userService.login(10553, this.quote).subscribe(result => { console.log('sending a quote to customer.' + result); }, error => { // this.showErrorMessage(error, null); console.error('error=>' + error); }); } console.log(this.quote); } }