(function ($) { $(function () { var script_vars = $.extend(true, { container_id: undefined, ajax_url: undefined, gtm_data: {}, step: undefined, partner_ajax_action: undefined, currency: 'CHF', summary_title_class: undefined, summary_price_class: undefined, summary_partners_class: undefined, summary_partner_tpl_class: undefined, summary_total_class: undefined, banks_container_class: 'banks-container' }, fasoon_alt_summary_box_vars); var $container = $('#' + script_vars.container_id); if (!$container.length) { alert('Element #' + script_vars.container_id + ' is not found.'); return; } var $summary_price = $container.find('.' + script_vars.summary_price_class); if (!$summary_price.length) { alert('Element .' + script_vars.summary_price_class + ' is not found.'); return; } var summary_partner_tpl = $container.find('.' + script_vars.summary_partner_tpl_class)[0].outerHTML; if (!summary_partner_tpl) { alert('Summary partner template is not found.'); return; } $container.find('.' + script_vars.summary_partner_tpl_class).remove(); var $summary_partners = $container.find('.' + script_vars.summary_partners_class); if (!$summary_partners.length) { alert('Element .' + script_vars.summary_partners_class + ' is not found.'); return; } if (!$summary_partners.find('ul>li').length) { $summary_partners.hide(); } var $summary_selected_partners = $summary_partners.find('.partner-intro'); if (!$summary_selected_partners.length) { alert('Element .partner-intro is not found.'); return; } $summary_total = $container.find('.' + script_vars.summary_total_class); if (!$summary_total.length) { alert('Element .' + script_vars.summary_total_class + ' is not found.'); return; } $banks_container = $('.' + script_vars.banks_container_class); initPartners(); function initPartners() { if ($banks_container.length) { $banks_container.find('.bank').each(function (i, el) { $('.add_btn', el).on('click', onBankAdd); $('.remove_btn', el).on('click', onPartnerRemove); }); // якщо вже є вибраний банк, то нічого робити не треба // якщо немає, то треба перевірити чи є банки з "related partners" if (!$banks_container.find('.add_btn.hidden').length) { $banks_container.find('.bank').each(function (i, el) { var $add_btn = $(el).find('.add_btn'); var related_partner = $add_btn.data('related-partner'); if (related_partner && $summary_partners.find('li[data-id=' + related_partner + ']').length) { $add_btn.trigger('click', [ true ] ); return false; } }); } } $summary_partners.find('ul>li').each($.proxy(function (i, el) { var li = $(el); li.find('.remove_btn').on('click', onPartnerRemove); }, this)); $summary_selected_partners.on('click', toggleSelectedPartners); updateSummary(); } function toggleSelectedPartners() { if ($summary_selected_partners.hasClass('open')) { $summary_selected_partners.removeClass('open'); $summary_selected_partners.addClass('close'); $summary_partners.find('.summary-partners-list').slideUp('fast'); } else { $summary_selected_partners.removeClass('close'); $summary_selected_partners.addClass('open'); $summary_partners.find('.summary-partners-list').slideDown('fast'); } } function partnerGtmEvent(action, partner) { fasoon_gtm.push({ "event": "company_foundation", "step_number": script_vars.step, "step_progress": "In Progress", "item_category": "Regional Partner", "item_action": action, "item_name": partner.title, "item_currency": script_vars.currency, "item_discount": partner.discount, }); } function onBankAdd(ev, is_manual_call) { var target_bank_ID = $(ev.currentTarget).data('id'); // якщо вже доданий інший банк, то треба видалити його з партнерів (може бути доданий тільки один банк) $banks_container.find('.bank').each(function (i, el) { var bank_ID = $('.add_btn', el).data('id'); $('ul > li', $summary_partners).each(function (i, el) { var $rm_btn = $('.remove_btn', el); if ($rm_btn.data('id') == bank_ID) { $rm_btn.trigger('click'); } }); // видаляються ті банки які мають "related partners" $banks_container.find('.bank').each(function (i, el) { var $rm_btn = $('.remove_btn', el); var bank_ID = $rm_btn.data('id'); if (target_bank_ID != bank_ID) { $rm_btn.trigger('click'); } }); }); return onPartnerAdd(ev, is_manual_call); } function onPartnerAdd(ev, is_manual_call) { var target = $(ev.currentTarget); var partner_id = target.data('id'); var partner_apiname = target.data('apiname'); var related_partner = target.data('related-partner'); if (!related_partner || !$summary_partners.find('li[data-id=' + related_partner + ']').length) { var partner_title = target.data('title'); var partner_discounts = target.data('discounts'); var li = $(summary_partner_tpl).clone(); li.removeClass(script_vars.summary_partner_tpl_class); li.find('input[type=hidden]').val(partner_id); li.find('.remove_btn').on('click', onPartnerRemove); li.find('.remove_btn').attr('data-id', partner_id); li.find('.title').html(partner_title); li.attr('data-id', partner_id); li.attr('data-type', 'bank'); li.attr('data-discounts', JSON.stringify(partner_discounts)); li.attr('data-apiname', partner_apiname); $('ul', $summary_partners).append(li); $summary_partners.show(); } $banks_container.find('.bank .add_btn[data-id=' + partner_id + ']').addClass('hidden'); $banks_container.find('.bank .remove_btn[data-id=' + partner_id + ']').removeClass('hidden'); updateSummary(); if (!is_manual_call) { partnerGtmEvent('Add', { title: partner_apiname, discount: $summary_partners.find('li[data-id=' + partner_id + ']').find('.discount').data('discount'), }); } } function onPartnerRemove(ev) { var partner_id = $(ev.currentTarget).data('id'); var li = $summary_partners.find('li[data-id=' + partner_id + ']'); if (!li.length) { return; } $banks_container.find('.bank .remove_btn[data-id=' + partner_id + ']').addClass('hidden'); $banks_container.find('.bank .add_btn[data-id=' + partner_id + ']').removeClass('hidden'); partnerGtmEvent('Remove', { title: li.data('apiname'), discount: li.find('.discount').data('discount'), }); li.remove(); if (!$summary_partners.find('ul>li').length) { $summary_partners.hide(); } updateSummary(); } function updateSummary() { var price = $summary_price.data('price'); var entity_id = $summary_price.data('entityid'); var backing_id = $summary_price.data('backingid'); var total = price; var total_without_bank = price; var partners_discount = 0; $summary_partners.find('ul>li').each(function (i, el) { var li = $(el); var partner_discount = 0.0; var discounts = li.data('discounts'); var hide_discount = li.data('hide-discount'); if (hide_discount) { li.find('.discount').addClass('hidden'); } if (discounts) { if (backing_id) { if (discounts.hasOwnProperty(entity_id) && discounts[entity_id].hasOwnProperty(backing_id)) { partner_discount = discounts[entity_id][backing_id]; } } else { if (discounts.hasOwnProperty(entity_id)) { partner_discount = discounts[entity_id]; } } } var discount = price * partner_discount / 100; if (discount > total) { discount = total; } li.find('.discount').attr('data-discount', discount); li.find('.discount').html('- ' + script_vars.currency + ' ' + formatPrice(discount)); total -= discount; if ('bank' != li.data('type')) { total_without_bank -= discount; if (total_without_bank < 0) { total_without_bank = 0; } } if (total < 0) { total = 0; } partners_discount += discount; }); $summary_selected_partners.find('.discount').text(script_vars.currency + ' ' + formatPrice(partners_discount)); $summary_total.data('total', total); $summary_total.text(script_vars.currency + ' ' + formatPrice(total)); updatePartners(total, total_without_bank); } function formatPrice(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; } function updatePartners(total, total_without_bank) { if ($banks_container.length) { var price = $summary_price.data('price'); var entity_id = $summary_price.data('entityid'); var backing_id = $summary_price.data('backingid'); $banks_container.find('.bank').each(function (i, el) { // alt title / description var alt_elems = ['title', 'description']; for (var i in alt_elems) { var elem = alt_elems[i]; var $alt_elem = $('.alt_' + elem, el); if ($alt_elem.length) { $alt_price_val = parseFloat($alt_elem.data('altpriceval')); if (!isNaN($alt_price_val) && $alt_price_val > 0 && total < $alt_price_val) { $('.main_' + elem, el).addClass('hidden'); $alt_elem.removeClass('hidden'); } else { $alt_elem.addClass('hidden'); $('.main_' + elem, el).removeClass('hidden'); } } } // bank discount var $this = $(this); var discount = 0.0; var discounts = $this.find('.add_btn').data('discounts'); if (discounts) { if (backing_id) { if (discounts.hasOwnProperty(entity_id) && discounts[entity_id].hasOwnProperty(backing_id)) { discount = discounts[entity_id][backing_id]; } } else { if (discounts.hasOwnProperty(entity_id)) { discount = discounts[entity_id]; } } } if (total_without_bank > 0 && discount > 0) { var discount = price * discount / 100; if (discount > total_without_bank) { discount = total_without_bank; } $this.find('.bank-price .price').html(script_vars.currency + ' ' + formatPrice(discount)); $this.find('.bank-price').removeClass('hidden'); } else { $this.find('.bank-price .price').html(script_vars.currency + ' 0.-'); $('.bank-price', $this).addClass('hidden'); } }); } } }); })(jQuery);