var fasoon = fasoon || {}; (function (ns, $) { "use strict"; ns.QuotePriceStep6 = function () { this.construct.apply(this, arguments); }; var proto = ns.QuotePriceStep6.prototype; proto.construct = function (options) { this.options = $.extend(true, { form_id: undefined, ajax_url: undefined, payment_method_el_name: undefined, _loyalty_card_id_el_name: undefined, loyalty_card_id_el_name: undefined, loyalty_card_value_el_name: undefined, loyalty_card_apply_el_name: undefined, loyalty_card_remove_el_name: undefined, loyalty_card_required_msg: undefined, vat: undefined, order_total: undefined, gtm_data: {}, currency: 'CHF' }, options); this.form = $('#' + this.options.form_id); if (!this.form.length) { alert('Form #' + this.options.form_id + ' is not found.'); return; } this.initLoyaltyCardElements(); this.initPaymentMethodElements(); fasoon_gtm.company_foundation(6, "Initiated", this.options.gtm_data); this.updateLoyaltyCardState(); this.setActivePaymentMethod(true); this.form.find('#back').on('click', $.proxy(function () { var data = this.collectGtmData(); data.click_name = "Back"; fasoon_gtm.company_foundation(6, "Complete", data); return true; }, this)); this.form.on('submit', $.proxy(function() { this.showAjaxOverlay(); var data = this.collectGtmData(); data.click_name = 'Submit Order'; fasoon_gtm.company_foundation(6, "Complete", data); return true; }, this)); }; proto.collectGtmData = function () { var data = this.options.gtm_data; var price = $('.fasoon_form1_summary_price').data('price'); price = parseFloat(price); if (isNaN(price)) { price = 0.00; } var discount = 0.00; $('.summary-partners li .discount').each(function () { var _disctoun = parseFloat($(this).data('discount')); if (isNaN(_disctoun)) { _disctoun = 0.00; } discount += _disctoun; }); var products = 0.00; $('.summary-products li').each(function () { var price = $(this).data('price'); if (isNaN(price)) { price = 0.0; } products += price; }); var tax = $('.summary-tax .tax').data('tax'); if (isNaN(tax)) { tax = 0.00; } var fee = $('.payment-method-fee').data('fee'); if (isNaN(fee)) { fee = 0.00; } var loyalty_card_value = this.loyalty_card_value.val(); if (isNaN(loyalty_card_value)) { loyalty_card_value = 0.00; } data.category_price = { "Foundation": price, "Partner": discount, "AdditionalServices": products + tax + fee, "Voucher": loyalty_card_value }; data.total_price = data.category_price.Foundation - data.category_price.Partner + data.category_price.AdditionalServices - data.category_price.Voucher; return data; }; proto.serviceGtmEvent = function (action, service) { fasoon_gtm.push({ "event": "company_foundation", "step_number": "6", "step_progress": "In Progress", "item_category": service.category, "item_action": action, "item_name": service.title, "item_currency": this.options.currency, "item_price": service.price, }); }; proto.showAjaxOverlay = function () { $('.ajax_overlay').addClass('shown'); $('body').addClass('ajax_overlay_shown'); }; proto.hideAjaxOverlay = function () { $('.ajax_overlay').removeClass('shown'); $('body').removeClass('ajax_overlay_shown'); }; proto.initPaymentMethodElements = function () { this.payment_method = this.form.find('[name=' + this.options.payment_method_el_name + ']'); this.payment_method.on('click', $.proxy(function () { this.setActivePaymentMethod(true); }, this)); }; proto.initLoyaltyCardElements = function () { this._loyalty_card_id = this.form.find('[name="' + this.options._loyalty_card_id_el_name + '"]'); this.loyalty_card_id = this.form.find('[name="' + this.options.loyalty_card_id_el_name + '"]'); this.loyalty_card_value = this.form.find('[name="' + this.options.loyalty_card_value_el_name + '"]'); this.loyalty_card_apply = this.form.find('[name="' + this.options.loyalty_card_apply_el_name + '"]'); this.loyalty_card_apply.on('click', $.proxy(this.loyaltyCardApply, this)); this.loyalty_card_remove = this.form.find('[name="' + this.options.loyalty_card_remove_el_name + '"]'); this.loyalty_card_remove.on('click', $.proxy(this.loyaltyCardRemove, this)); this.summary_loyalty_card = this.form.find('.summary-loyalty-card'); this._loyalty_card_id.mask('0000 0000 0000 0000 000'); }; proto.setActivePaymentMethod = function (callGtmEvent) { var obj = this; this.payment_method.each(function () { var $this = $(this); var $label = $('label[for=' + $this.attr('id') + ']'); if ($this.prop('checked')) { var vat = parseFloat(obj.options.order_total.VAT); var pm_value = $this.val(); var pm_title = $this.data('method-title'); var pm_fee = parseFloat($this.data('fee')); var pm_tax = 0; var $spm = $('.summary-payment-method'); // products total & tax var products_total = 0.0; var products_tax = 0.0; var $products = $('.summary-products ul>li'); if ($products.length) { $products.each(function () { var price = parseFloat($(this).data('price')); if (!isNaN(price) && price > 0) { products_total += price; } }); } if (products_total > 0) { products_tax = obj.calculateTax(products_total, vat); } var total = parseFloat(obj.options.order_total.total); if (0 == total && 0 == products_total) { $('.payment_method_container').addClass('hidden'); obj.payment_method.prop('disabled', true); pm_fee = 0; } else { $('.payment_method_container').removeClass('hidden'); obj.payment_method.prop('disabled', false); } // payment fee $spm.find('.payment-method-name').html(pm_title); $spm.find('.payment-method-fee').attr('data-fee', pm_fee); $spm.find('.payment-method-fee span').html(obj.formatPrice(pm_fee)); // calculate tax var $stx = $('.summary-tax'); var tax = parseFloat(obj.options.order_total.tax); if (pm_fee > 0) { pm_tax = obj.calculateTax(pm_fee, vat); } $stx.find('.tax').attr('data-tax', tax + pm_tax + products_tax); $stx.find('.tax span').html(obj.formatPrice(tax + pm_tax + products_tax)); // calculate total var amount = total + tax + pm_fee + pm_tax + products_total + products_tax; // loyalty card discount var loyalty_card_value = parseFloat(obj.loyalty_card_value.val()); if (!isNaN(loyalty_card_value) && loyalty_card_value > 0) { if (loyalty_card_value > amount) { obj.summary_loyalty_card.find('li').data('price', amount); obj.summary_loyalty_card.find('.price span').text(obj.formatPrice(amount)); amount = 0; } else { obj.summary_loyalty_card.find('li').data('price', loyalty_card_value); obj.summary_loyalty_card.find('.price span').text(obj.formatPrice(loyalty_card_value)); amount -= loyalty_card_value; } } $('.summary-total .total').attr('data-total', amount); $('.summary-total .total span').html(obj.formatPrice(amount)); $label.addClass('active'); if (callGtmEvent) { obj.serviceGtmEvent('Add', { "category": "Additional Services", "title": pm_value, "price": pm_fee }); } } else { $label.removeClass('active'); } }); }; proto.calculateTax = function(sum, vat) { var tax = sum * vat / 100; tax = tax * 100; tax = Math.ceil(tax); tax = tax / 100; return tax; } proto.formatPrice = function (price) { var price_f = parseFloat(price); var price_i = parseInt(price); if (price_f === price_i) { price = '' + price_i + '.-'; } else { price = '' + price_f.toFixed(2); } return price; }; proto.loyaltyCardApply = function () { if (!this._loyalty_card_id.val()) { alert(this.options.loyalty_card_required_msg); return false; } this.showAjaxOverlay(); var data = { action: 'apply_loyalty_card', loyalty_card_id: this._loyalty_card_id.val(), }; $.post(this.options.ajax_url, data, $.proxy(this.loyaltyCardApplySuccess, this), 'json'); }; proto.loyaltyCardApplySuccess = function (response) { this.hideAjaxOverlay(); if (response.is_success) { this.loyalty_card_id.val(response.loyalty_card_id); this.loyalty_card_value.val(response.loyalty_card_value); this.updateLoyaltyCardState(); this.setActivePaymentMethod(); // update summary this.serviceGtmEvent('Add', { "category": "Voucher", "title": 'Voucher', "price": response.loyalty_card_value }); } else { alert(response.error); } }; proto.loyaltyCardRemove = function () { this.serviceGtmEvent('Remove', { "category": "Voucher", "title": 'Voucher', "price": this.loyalty_card_id.val() }); this.loyalty_card_id.val(''); this.loyalty_card_value.val(''); this._loyalty_card_id.val(''); this.summary_loyalty_card.find('li').data('price', 0); this.updateLoyaltyCardState(); this.setActivePaymentMethod(); // update summary }; proto.updateLoyaltyCardState = function () { if (this.loyalty_card_id.val()) { var value = parseFloat(this.loyalty_card_value.val()); this.summary_loyalty_card.find('li').data('price', value); this.summary_loyalty_card.find('.price span').text(this.formatPrice(value)); this.summary_loyalty_card.removeClass('hidden'); this._loyalty_card_id.prop('disabled', true); this.loyalty_card_apply.addClass('hidden'); this.loyalty_card_remove.removeClass('hidden'); } else { this.summary_loyalty_card.addClass('hidden'); this.summary_loyalty_card.find('li').data('price', 0); this.summary_loyalty_card.find('.price span').text(''); this._loyalty_card_id.prop('disabled', false); this.loyalty_card_remove.addClass('hidden'); this.loyalty_card_apply.removeClass('hidden'); } } proto.formatPrice = function (price) { var price_f = parseFloat(price); var price_i = parseInt(price); if (price_f === price_i) { price = '' + price_i + '.-'; } else { price = '' + price_f.toFixed(2); } return price; }; })(fasoon, jQuery);