var Order = function(orderConfig){
var $this = this, $window, $document;
$this.NAME = 'Order';
$this.config = orderConfig || {
singleSubscriptionTypeTemplateId: 0,
orderStep: 1
};
var initHandler = function(){
$window = $(window);
$document = $(document);
$(document).on('sendAjax', $this.sendAjaxHandler);
//$(document).trigger( 'sendAjax', {target: $classHandler, action: 'actionName', params: {param1: 1, param2: 2} } );
resizeHandler();
initOrder();
};
var initOrder = function(){
if( $this.config.orderStep == 2 ) {
var urlParams = new URLSearchParams(window.location.search);
if( urlParams.get('st') !== null ){
$('body,html').scrollTop( $(".form-subscription").offset().top );
}
$this.prepareMagazinesForm();
}else if( $this.config.orderStep == 3 ){
if( $('#checkbox-invoice').is(':checked') ){
$('#condition-invoice').slideDown(100);
}
}
};
$this.prepareMagazinesForm = function(){
if( $this.config.singleSubscriptionTypeTemplateId == 1 ) { // pojedynczy numer
$('.form-group-repeater').on('change', 'input[name="copies[]"]', $this.calculatePrice);
$('#btnAdd').on('click', function(){
$('select[name="magazinesId[]"]').off('change').on('change', function(){
$(document).trigger( 'sendAjax', {target: $(this).closest('.product'), action: 'getNumbersList', params: {magazinesId: $(this).val()} } );
});
$('select[name="numbersId[]"]').off('change').on('change', function(){
$(this).closest('.product').data('price', $(this).find('option:selected').data('price') );
$this.calculatePrice();
});
});
$('.form-group-repeater').on('DOMNodeRemoved', function(){
$this.calculatePrice();
});
$('select[name="magazinesId[]"]').on('change', function(){
$(document).trigger( 'sendAjax', {target: $(this).closest('.product'), action: 'getNumbersList', params: {magazinesId: $(this).val()} } );
});
$('select[name="numbersId[]"]').on('change', function(){
$(this).closest('.product').data('price', $(this).find('option:selected').data('price') );
$this.calculatePrice();
});
$('input[name="copies[]"]').on('change', $this.calculatePrice);
}else if( $this.config.singleSubscriptionTypeTemplateId == 2 ){ // krajowa
$('select[name="magazinesId"]').on('change', function(){
$(document).trigger( 'sendAjax', {target: $(this).closest('.product'), action: 'getNumbersList', params: {magazinesId: $(this).val()} } );
});
$('select[name="numbersIdStart"]').on('change', $this.calculatePrice);
$('select[name="numbersIdEnd"]').on('change', $this.calculatePrice);
$('input[name="copies"]').on('change', $this.calculatePrice);
}else if( $this.config.singleSubscriptionTypeTemplateId == 3 ){ // zagraniczna
}else if( $this.config.singleSubscriptionTypeTemplateId == 4 ){ // rabat 25%
$('select[name="magazinesId"]').on('change', function(){
$(document).trigger( 'sendAjax', {target: $(this).closest('.product'), action: 'getNumbersList', params: {magazinesId: $(this).val()} } );
});
$('select[name="numbersIdStart"]').on('change', $this.calculatePrice);
$('select[name="numbersIdEnd"]').on('change', $this.calculatePrice);
$('input[name="copies"]').on('change', $this.calculatePrice);
}else if( $this.config.singleSubscriptionTypeTemplateId == 5 ){ // 2 za 50%
$('select[name="magazinesId[]"]').on('change', function(){
$(document).trigger( 'sendAjax', {target: $(this).closest('.product'), action: 'getNumbersList', params: {magazinesId: $(this).val()} } );
});
$('select[name="numbersIdStart[]"]').on('change', $this.calculatePrice);
$('select[name="numbersIdEnd[]"]').on('change', $this.calculatePrice);
$('input[name="copies"]').on('change', $this.calculatePrice);
$('select[name="numbersIdStart[]"]:eq(1)').on('change', function () {
var $product1 = $('.product').eq(0);
var $product2 = $('.product').eq(1);
var $start = $product1.find('select[name="numbersIdStart[]"] option:selected');
var $end = $product1.find('select[name="numbersIdEnd[]"] option:selected');
var $min = Math.min($start.index(), $end.index());
var $max = Math.max($start.index(), $end.index());
var len = $max - $min;
var startIndex = $product2.find('select[name="numbersIdStart[]"] option:selected').index();
var endIndex = startIndex + len;
startIndex = startIndex - len;
var $options = $product2.find('select[name="numbersIdEnd[]"] option');
$options.attr('disabled', 'disabled');
for( var index=startIndex; index<=endIndex; index++ ) {
$options.eq(index).removeAttr('disabled');
}
$numbesList = $product2.find('select[name="numbersIdEnd[]"]');
$numbesList.selectbox("detach");
$numbesList.val(0);
$numbesList.selectbox("attach");
});
}else if( $this.config.singleSubscriptionTypeTemplateId == 6 ){ // 3 za 2
$('select[name="magazinesId[]"]').on('change', function(){
$(document).trigger( 'sendAjax', {target: $(this).closest('.product'), action: 'getNumbersList', params: {magazinesId: $(this).val()} } );
});
$('select[name="numbersIdStart[]"]').on('change', $this.calculatePrice);
$('select[name="numbersIdEnd[]"]').on('change', $this.calculatePrice);
$('input[name="copies"]').on('change', $this.calculatePrice);
$('select[name="numbersIdStart[]"]:eq(1)').on('change', function () {
var $product1 = $('.product').eq(0);
var $product2 = $('.product').eq(1);
var $start = $product1.find('select[name="numbersIdStart[]"] option:selected');
var $end = $product1.find('select[name="numbersIdEnd[]"] option:selected');
var $min = Math.min($start.index(), $end.index());
var $max = Math.max($start.index(), $end.index());
var len = $max - $min;
var startIndex = $product2.find('select[name="numbersIdStart[]"] option:selected').index();
var endIndex = startIndex + len;
startIndex = startIndex - len;
var $options = $product2.find('select[name="numbersIdEnd[]"] option');
$options.attr('disabled', 'disabled');
for( var index=startIndex; index<=endIndex; index++ ) {
$options.eq(index).removeAttr('disabled');
}
$numbesList = $product2.find('select[name="numbersIdEnd[]"]');
$numbesList.selectbox("detach");
$numbesList.val(0);
$numbesList.selectbox("attach");
});
$('select[name="numbersIdStart[]"]:eq(2)').on('change', function () {
var $product1 = $('.product').eq(0);
var $product2 = $('.product').eq(2);
var $start = $product1.find('select[name="numbersIdStart[]"] option:selected');
var $end = $product1.find('select[name="numbersIdEnd[]"] option:selected');
var $min = Math.min($start.index(), $end.index());
var $max = Math.max($start.index(), $end.index());
var len = $max - $min;
var startIndex = $product2.find('select[name="numbersIdStart[]"] option:selected').index();
var endIndex = startIndex + len;
startIndex = startIndex - len;
var $options = $product2.find('select[name="numbersIdEnd[]"] option');
$options.attr('disabled', 'disabled');
for( var index=startIndex; index<=endIndex; index++ ) {
$options.eq(index).removeAttr('disabled');
}
$numbesList = $product2.find('select[name="numbersIdEnd[]"]');
$numbesList.selectbox("detach");
$numbesList.val(0);
$numbesList.selectbox("attach");
});
}
};
$this.calculatePrice = function(){
var totalPrice = 0;
var discount = 0;
var $price = $('.price');
var $copies = $('input[name="copies"]');
var cnt = 0;
var productPrice = 0;
var $start, $end, $min, $max;
var $product1, $product2, $product3;
var price1, price2, price3;
if( $this.config.singleSubscriptionTypeTemplateId == 1 ){ // pojedynczy numer
$('input[name="copies[]"]').each(function(){
productPrice = $(this).closest('.product').data('price');
if( productPrice != undefined && productPrice != null && productPrice != '' ) {
totalPrice += parseInt($(this).val(), 10) * parseFloat(productPrice);
}
});
}else if( $this.config.singleSubscriptionTypeTemplateId == 2 ) { // krajowa
$start = $('select[name="numbersIdStart"] option:selected');
$end = $('select[name="numbersIdEnd"] option:selected');
if( typeof $start.data('price') != 'undefined' && typeof $end.data('price') != 'undefined' ) {
$min = Math.min($start.index(), $end.index());
$max = Math.max($start.index(), $end.index());
var $options = $start.closest('select').find('option');
for( cnt=$min; cnt<=$max; cnt++ ) {
totalPrice += parseFloat( $options.eq(cnt).data('price') );
}
totalPrice = totalPrice * parseInt($copies.val(), 10);
}
}else if( $this.config.singleSubscriptionTypeTemplateId == 3 ) { // zagraniczna
}else if( $this.config.singleSubscriptionTypeTemplateId == 4 ) { // rabat 25%
$start = $('select[name="numbersIdStart"] option:selected');
$end = $('select[name="numbersIdEnd"] option:selected');
if( typeof $start.data('price') != 'undefined' && typeof $end.data('price') != 'undefined' ) {
$min = Math.min($start.index(), $end.index());
$max = Math.max($start.index(), $end.index());
var $options = $start.closest('select').find('option');
for( cnt=$min; cnt<=$max; cnt++ ) {
totalPrice += parseFloat( $options.eq(cnt).data('price') );
}
discount = totalPrice * parseInt($copies.val(), 10) * 0.25;
totalPrice = totalPrice * parseInt($copies.val(), 10) * 0.75;
}
}else if( $this.config.singleSubscriptionTypeTemplateId == 5 ) { // 2 za 50%
$product1 = $('.product').eq(0);
$product2 = $('.product').eq(1);
price1 = 0;
price2 = 0;
$start = $product1.find('select[name="numbersIdStart[]"] option:selected');
$end = $product1.find('select[name="numbersIdEnd[]"] option:selected');
if( typeof $start.data('price') != 'undefined' && typeof $end.data('price') != 'undefined' ) {
$min = Math.min($start.index(), $end.index());
$max = Math.max($start.index(), $end.index());
var $options = $start.closest('select').find('option');
for( cnt=$min; cnt<=$max; cnt++ ) {
price1 += parseFloat( $options.eq(cnt).data('price') );
}
}
$start = $product2.find('select[name="numbersIdStart[]"] option:selected');
$end = $product2.find('select[name="numbersIdEnd[]"] option:selected');
if( typeof $start.data('price') != 'undefined' && typeof $end.data('price') != 'undefined' ) {
$min = Math.min($start.index(), $end.index());
$max = Math.max($start.index(), $end.index());
var $options = $start.closest('select').find('option');
for( cnt=$min; cnt<=$max; cnt++ ) {
price2 += parseFloat( $options.eq(cnt).data('price') );
}
}
if( price1 < price2 ){
price1 = 0.5 * price1;
discount = price1;
}else{
price2 = 0.5 * price2;
discount = price2;
}
discount = discount * parseInt($copies.val(), 10);
totalPrice = ( price1 + price2 ) * parseInt($copies.val(), 10);
}else if( $this.config.singleSubscriptionTypeTemplateId == 6 ) { // 3 za 2
$product1 = $('.product').eq(0);
$product2 = $('.product').eq(1);
$product3 = $('.product').eq(2);
price1 = 0;
price2 = 0;
price3 = 0;
$start = $product1.find('select[name="numbersIdStart[]"] option:selected');
$end = $product1.find('select[name="numbersIdEnd[]"] option:selected');
if( typeof $start.data('price') != 'undefined' && typeof $end.data('price') != 'undefined' ) {
$min = Math.min($start.index(), $end.index());
$max = Math.max($start.index(), $end.index());
var $options = $start.closest('select').find('option');
for( cnt=$min; cnt<=$max; cnt++ ) {
price1 += parseFloat( $options.eq(cnt).data('price') );
}
}
$start = $product2.find('select[name="numbersIdStart[]"] option:selected');
$end = $product2.find('select[name="numbersIdEnd[]"] option:selected');
if( typeof $start.data('price') != 'undefined' && typeof $end.data('price') != 'undefined' ) {
$min = Math.min($start.index(), $end.index());
$max = Math.max($start.index(), $end.index());
var $options = $start.closest('select').find('option');
for( cnt=$min; cnt<=$max; cnt++ ) {
price2 += parseFloat( $options.eq(cnt).data('price') );
}
}
$start = $product3.find('select[name="numbersIdStart[]"] option:selected');
$end = $product3.find('select[name="numbersIdEnd[]"] option:selected');
if( typeof $start.data('price') != 'undefined' && typeof $end.data('price') != 'undefined' ) {
$min = Math.min($start.index(), $end.index());
$max = Math.max($start.index(), $end.index());
var $options = $start.closest('select').find('option');
for( cnt=$min; cnt<=$max; cnt++ ) {
price3 += parseFloat( $options.eq(cnt).data('price') );
}
}
discount = Math.min(price1, price2, price3) * parseInt($copies.val(), 10);
totalPrice = ( price1 + price2 + price3 - Math.min(price1, price2, price3) ) * parseInt($copies.val(), 10);
}
discount = (discount / parseFloat($price.data('exchangerate'))).toFixed(2);
totalPrice = (totalPrice / parseFloat($price.data('exchangerate'))).toFixed(2);
$price.html( totalPrice + " " + $price.data('currency') );
$('.price-discount').html( (parseFloat(totalPrice) + parseFloat(discount)).toFixed(2) + " " + $price.data('currency') );
};
$this.setNumbersList = function(data, $target){
if( $this.config.singleSubscriptionTypeTemplateId == 1 ) { // pojedynczy numer
$numbesList = $target.find('select[name="numbersId[]"]');
$numbesList.find('option:not(:first)').remove();
var options = '';
for (var key in data.data) {
if( data.data[key].canOrder == 1 && data.data[key].printed == 1 && data.data[key].price === Object(data.data[key].price) && Object.prototype.toString.call(data.data[key].price) !== '[object Array]' ) {
options += '';
}
}
$numbesList.append(options);
$numbesList.selectbox("detach");
$numbesList.val(0);
$numbesList.selectbox("attach");
$target.data('price', data.price);
}else if( $this.config.singleSubscriptionTypeTemplateId == 2 ){ // krajowa
$numbesList = $target.find('select[name="numbersIdStart"]');
$numbesList.find('option:not(:first)').remove();
var options = '';
for (var key in data.data) {
console.log( key, data.data[key].price );
if( data.data[key].canOrder == 1 && data.data[key].price === Object(data.data[key].price) && Object.prototype.toString.call(data.data[key].price) !== '[object Array]' ) {
options += '';
}else{
break;
}
}
$numbesList.append(options);
$numbesList.selectbox("detach");
$numbesList.val(0);
$numbesList.selectbox("attach");
$numbesList = $target.find('select[name="numbersIdEnd"]');
$numbesList.find('option:not(:first)').remove();
var options = '';
for (var key in data.data) {
if( data.data[key].canOrder == 1 && data.data[key].price === Object(data.data[key].price) && Object.prototype.toString.call(data.data[key].price) !== '[object Array]' ) {
options += '';
}else{
break;
}
}
$numbesList.append(options);
$numbesList.selectbox("detach");
$numbesList.val(0);
$numbesList.selectbox("attach");
$target.data('price', data.price);
}else if( $this.config.singleSubscriptionTypeTemplateId == 3 ){ // zagraniczna
}else if( $this.config.singleSubscriptionTypeTemplateId == 4 ){ // rabat 25%
$numbesList = $target.find('select[name="numbersIdStart"]');
$numbesList.find('option:not(:first)').remove();
var options = '';
for (var key in data.data) {
if( data.data[key].canOrder == 1 && data.data[key].price === Object(data.data[key].price) && Object.prototype.toString.call(data.data[key].price) !== '[object Array]' ) {
options += '';
}else{
break;
}
}
$numbesList.append(options);
$numbesList.selectbox("detach");
$numbesList.val(0);
$numbesList.selectbox("attach");
$numbesList = $target.find('select[name="numbersIdEnd"]');
$numbesList.find('option:not(:first)').remove();
var options = '';
for (var key in data.data) {
if( data.data[key].canOrder == 1 && data.data[key].price === Object(data.data[key].price) && Object.prototype.toString.call(data.data[key].price) !== '[object Array]' ) {
options += '';
}else{
break;
}
}
$numbesList.append(options);
$numbesList.selectbox("detach");
$numbesList.val(0);
$numbesList.selectbox("attach");
$target.data('price', data.price);
}else if( $this.config.singleSubscriptionTypeTemplateId == 5 ){ // 2 za 50%
$numbesList = $target.find('select[name="numbersIdStart[]"]');
$numbesList.find('option:not(:first)').remove();
var options = '';
for (var key in data.data) {
if( data.data[key].canOrder == 1 && data.data[key].price === Object(data.data[key].price) && Object.prototype.toString.call(data.data[key].price) !== '[object Array]' ) {
options += '';
}else{
break;
}
}
$numbesList.append(options);
$numbesList.selectbox("detach");
$numbesList.val(0);
$numbesList.selectbox("attach");
$numbesList = $target.find('select[name="numbersIdEnd[]"]');
$numbesList.find('option:not(:first)').remove();
var options = '';
for (var key in data.data) {
if( data.data[key].canOrder == 1 && data.data[key].price === Object(data.data[key].price) && Object.prototype.toString.call(data.data[key].price) !== '[object Array]' ) {
options += '';
}else{
break;
}
}
$numbesList.append(options);
$numbesList.selectbox("detach");
$numbesList.val(0);
$numbesList.selectbox("attach");
$target.data('price', data.price);
}else if( $this.config.singleSubscriptionTypeTemplateId == 6 ){ // 3 za 2
$numbesList = $target.find('select[name="numbersIdStart[]"]');
$numbesList.find('option:not(:first)').remove();
var options = '';
for (var key in data.data) {
if( data.data[key].canOrder == 1 && data.data[key].price === Object(data.data[key].price) && Object.prototype.toString.call(data.data[key].price) !== '[object Array]' ) {
options += '';
}else{
break;
}
}
$numbesList.append(options);
$numbesList.selectbox("detach");
$numbesList.val(0);
$numbesList.selectbox("attach");
$numbesList = $target.find('select[name="numbersIdEnd[]"]');
$numbesList.find('option:not(:first)').remove();
var options = '';
for (var key in data.data) {
if( data.data[key].canOrder == 1 && data.data[key].price === Object(data.data[key].price) && Object.prototype.toString.call(data.data[key].price) !== '[object Array]' ) {
options += '';
}else{
break;
}
}
$numbesList.append(options);
$numbesList.selectbox("detach");
$numbesList.val(0);
$numbesList.selectbox("attach");
}
$this.calculatePrice();
};
$this.sendAjaxHandler = function(e, eData){
var target = eData.target;
$.ajax({
type: 'POST',
url: '',
dataType: 'json',
data: {action: eData.action, params: eData.params},
headers: {
'X-CSRF-TOKEN' : $('meta[name="X-CSRF-TOKEN"]').attr('content')
}
}).done(function(data){
var i = 0;
var l = data.length;
for( i; i');
}else{
$(this).find('input[name="X-CSRF-TOKEN"]').val( token );
}
});
};
var resizeHandler = function(){
};
var loadHandler = function(){
resizeHandler();
};
return {
init: initHandler,
resize: resizeHandler,
load: loadHandler
};
};
var Order = new Order(orderConfig);
$(document).ready(Order.init);
$(window).load(Order.load);
$(window).resize(Order.resize);