UNPKG

rm-components

Version:

The default blueprint for ember-cli addons.

177 lines (154 loc) 5.76 kB
import Ember from 'ember'; import EmberCPM from 'ember-cpm'; import rmAlert from 'rm-components/utils/rm-alert'; import layout from './template'; const { Macros: { product, sum, difference } } = EmberCPM; const { Component, computed } = Ember; export default Component.extend({ layout, i18n: Ember.inject.service(), smallTotal: product('pricing.smallEvents', 'pricing.smallEventMonthlyPriceInCents'), mediumTotal: product('pricing.mediumEvents', 'pricing.mediumEventMonthlyPriceInCents'), largeTotal: product('pricing.largeEvents', 'pricing.largeEventMonthlyPriceInCents'), subtotal: sum( product('pricing.smallEvents', 'pricing.smallEventMonthlyPriceInCents'), product('pricing.mediumEvents', 'pricing.mediumEventMonthlyPriceInCents'), product('pricing.largeEvents', 'pricing.largeEventMonthlyPriceInCents'), product('pricing.proUsers', 'pricing.proUserMonthlyPriceInCents') ), previousSmallEvents: 0, previousMediumEvents: 0, previousLargeEvents: 0, previousProUsers: 0, additionalSmallEvents: difference('pricing.smallEvents', 'previousSmallEvents'), additionalMediumEvents: difference('pricing.mediumEvents', 'previousMediumEvents'), additionalLargeEvents: difference('pricing.largeEvents', 'previousLargeEvents'), additionalProUsers: difference('pricing.proUsers', 'previousProUsers'), /** * Calculate the total percentage discount based on what the user has spent * * How this is calcualted: * - Discounts are calculated in tiers. * - Each tier is defiend by a threshold and a discout associated with that threshold * * @method spendingDiscount * @public */ spendingDiscount: computed('subtotal', function() { // const parsing = this.get('pricing'); const discountTiers = [{ threshold: 100000, discount: 0.1 }, { threshold: 150000, discount: 0.15 }, { threshold: 200000, discount: 0.2 }, { threshold: 250000, discount: 0.25 }, { threshold: 300000, discount: 0.3 }]; const subtotal = this.get('subtotal'); let discount = 0; discountTiers.forEach(function(tier) { if (subtotal >= tier.threshold) { discount = tier.discount; } }); return discount; }), calculatedSpendingDiscount: computed('subtotal', 'spendingDiscount', function() { const subtotal = this.get('subtotal'); const spendingDiscount = this.get('spendingDiscount'); return subtotal * spendingDiscount; }), implementationFee: computed('pricing.implementationFeeInCents', 'pricing.hasPaidImplementationFee', function() { if (this.get('pricing.hasPaidImplementationFee')) { return 0; } else { return this.get('pricing.implementationFeeInCents'); } }), discount: computed('pricing.proUsers', 'pricing.smallEvents', function() { if (this.get('pricing.smallEvents') > 0 && this.get('pricing.proUsers') > 0) { return this.get('pricing.smallEventMonthlyPriceInCents'); } else { return 0; } }), subtotalWithDiscount: computed('subtotal', 'discount', function() { return this.get('subtotal') - this.get('discount'); // the difference near threshold might change discount threshold }), total: sum(product('subtotalWithDiscount', difference(1, 'spendingDiscount')), 'implementationFee'), // total: product( difference( sum('subtotal','implementationFee'),'discount'),difference(1, 'spendingDiscount')), settingTotal: computed('total', function() { this.set('pricing.total', this.get('total')); return this.get('total'); }), stringifyBill: computed('subtotal', function() { const i18n = this.get('i18n'); const eventStrings = [ i18n.t('event', { size: 'small', count: this.get('pricing.smallEvents') }), i18n.t('event', { size: 'medium', count: this.get('pricing.mediumEvents') }), i18n.t('event', { size: 'large', count: this.get('pricing.largeEvents') }) ]; return eventStrings.join(', '); }), _updateNewNumbers() { this.set('previousSmallEvents', this.get('pricing.smallEvents')); this.set('previousMediumEvents', this.get('pricing.mediumEvents')); this.set('previousLargeEvents', this.get('pricing.largeEvents')); this.set('previousProUsers', this.get('pricing.proUsers')); }, didReceiveAttrs() { this._updateNewNumbers(); }, actions: { paymentPreflight() { this.attrs.updatePricing(this.get('pricing')).then((data) => { this.set('hasError', false); const pricingId = data.pricing.id; this.set('pricingId', pricingId); if (!data.pricing.has_setup_payment) { // jscs:disable requireCamelCaseOrUpperCaseIdentifiers this.$('.stripe-checkout').click(); } else { rmAlert('Payment Updated', 'The changes to your subscription will be active on your next billing cycle', 'success'); } this.set('previousSmallEvents', this.get('pricing.smallEvents')); this.set('previousMediumEvents', this.get('pricing.mediumEvents')); this.set('previousLargeEvents', this.get('pricing.largeEvents')); this.set('previousProUsers', this.get('pricing.proUsers')); }, () => { this.set('hasError', true); }); }, processStripeToken(token) { this.attrs.sendPricing(this.get('pricingId'), token); }, resetNumbers() { this.set('pricing.smallEvents', this.get('previousSmallEvents')); this.set('pricing.mediumEvents', this.get('previousMediumEvents')); this.set('pricing.largeEvents', this.get('previousLargeEvents')); this.set('pricing.proUsers', this.get('previousProUsers')); } } });