shop.js 54.3 KB
Newer Older
Administrator committed
1 2 3 4
var main_content = $('#main-content'),
    shop_section = $('section.shop'),
    orders_section = $('section.orders'),
    main_waiting_zone = $('#main-waiting-zone'),
5 6 7
    loading_img = $('#rotating_loader').clone()
        .removeAttr('id')
        .addClass('rotating_loader'),
Administrator committed
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
    product_template = $('#templates .product'),
    cart_elt_template = $('#templates .cart-elt'),
    categ_menu_template = $('#templates .category-menu'),
    alim_categ = $('#alim_categ'),
    non_alim_categ = $('#non_alim_categ'),
    cart = $('#cart'),
    cart_total = $('.cart-total'),
    valid_wrapper = $('#valid-wrapper'),
    valid_cart = $('#valid-cart'),
    skw = $('#search-input'),
    bday_sel = $('select[name="bday"]'),
    bday_change_sel = $('select[name="bday-change"]'),
    current_order_bdate = $('#current_order_bdate'),
    my_orders_wrap = $('#my-orders-sumup'),
    cart_destroy_msg = $('#templates .destroy-cart-msg'),
    modify_best_date_msg = $('#templates .modify-best-date-msg'),
    current_action = null,
    slots_pack_nb = $('[name="bhour"] option').length - 1,
    dragSrcEl = null,
    forbidden_slots = [],
    closing_dates = [],
    right_column = $('#right-column'),
    visit_mode = false,
31
    timer = null;
Administrator committed
32 33 34 35 36 37 38


/** --- UTILS --- **/
//$('.time-given-for-validation')

// Sort products div according to selected sort type
function sort_product_divs(divs_to_sort, sort_type) {
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
    let compare_price_uom = function (a, b) {
        // For products by unit : get uom price
        // For products by kg : get unit price (which is price of kg, we need to do this because some of them don't have a uom price)
        let a_price_uom = $(a).find("span.uom_price")
            .text() == 'Unité(s)' ? parseFloat($(a).find("span.uom_price")
                .text()) : parseFloat($(a).find("span.price")
                .text());
        let b_price_uom = $(b).find("span.uom_price")
            .text() == 'Unité(s)' ? parseFloat($(b).find("span.uom_price")
                .text()) : parseFloat($(b).find("span.price")
                .text());

        return a_price_uom > b_price_uom ? 1 : -1;
    };

54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
    var sorted_products = divs_to_sort.sort(function (a, b) {
        switch (sort_type) {
        case 'name_asc':
            return ($(a).find(".name")
                .text() > $(b).find(".name")
                .text()) ? 1 : -1;
        case 'name_desc':
            return ($(a).find(".name")
                .text() < $(b).find(".name")
                .text()) ? 1 : -1;
        case 'price_unit_asc':
            return parseFloat($(a).find("span.price")
                .text()) > parseFloat($(b).find("span.price")
                .text()) ? 1 : -1;
        case 'price_unit_desc':
            return parseFloat($(a).find("span.price")
                .text()) < parseFloat($(b).find("span.price")
                .text()) ? 1 : -1;
        case 'price_uom_asc':
73
            return compare_price_uom(a, b);
74
        case 'price_uom_desc':
75
            return compare_price_uom(b, a);
76 77 78 79 80 81
        default:
            return $(a).find(".name")
                .text() > $(b).find(".name")
                .text();
        }
    });
Administrator committed
82

83
    return sorted_products;
Administrator committed
84 85 86 87
}

function djLogError(e) {
    try {
88 89
        $.post('/shop/log_error', {error: JSON.stringify(e)});
    } catch (e) {
90
        console.log(e);
Administrator committed
91 92 93 94
    }
}

var french_date_and_time = function(dstring) {
95 96 97 98
    if (shop_mode == 'delivery') {
        return dstring;
    }

99
    var formatted = dstring.trim().replace(/-/g, "/"); // replace for Safari
Administrator committed
100
    //expected = YYYY-MM-DD HH:MM[:00]
101

Administrator committed
102
    try {
103 104 105 106 107 108
        const options = {weekday: 'short', month: 'long', day: 'numeric', hour: 'numeric', minute: 'numeric'};

        dt = new Date(formatted);
        formatted = dt.toLocaleString('fr-FR', options);
    } catch (e) {
        //no matter
Administrator committed
109
    }
110 111 112

    return formatted;
};
Administrator committed
113 114 115
/* ----------- */

var adjustCartHeight = function() {
116 117 118 119
    var max_height = window.innerHeight - 250 - 50;

    $('#cart-wrapper').css({'max-height': max_height + 'px'});
};
Administrator committed
120 121

var adjustSizes = function() {
122 123
    adjustCartHeight();
};
Administrator committed
124 125

var resetProgressBar = function() {
126 127 128 129
    $('#header_step_one').removeClass('step_one_active');
    $('#header_step_two').removeClass('step_two_active');
    $('#header_step_three').removeClass('step_three_active');
};
Administrator committed
130 131

function handleDragStart(e) {
132 133
    // Target (this) element is the source node.
    dragSrcEl = this;
Administrator committed
134

135 136
    e.dataTransfer.effectAllowed = 'move';
    e.dataTransfer.setData('text/html', this.outerHTML);
Administrator committed
137

138
    this.classList.add('dragElem');
Administrator committed
139 140
}
function handleDragOver(e) {
141 142 143 144
    if (e.preventDefault) {
        e.preventDefault(); // Necessary. Allows us to drop.
    }
    this.classList.add('over');
Administrator committed
145

146
    e.dataTransfer.dropEffect = 'move'; // See the section on the DataTransfer object.
Administrator committed
147

148
    return false;
Administrator committed
149 150 151
}

function handleDragLeave(e) {
152
    this.classList.remove('over'); // this / e.target is previous target element.
Administrator committed
153 154 155
}

function handleDrop(e) {
156 157 158 159 160 161 162
    // this/e.target is current target element.
    if (e.stopPropagation) {
        e.stopPropagation(); // Stops some browsers from redirecting.
    }
    var receiver = $(this);

    if (receiver.prop('draggable') == true) {
Administrator committed
163
    // Don't do anything if dropping the same column we're dragging.
164 165 166 167 168 169 170 171 172 173 174 175 176 177
        if (dragSrcEl != this) {
            var droped = $(e.dataTransfer.getData('text/html'));
            var nb = parseInt(receiver.find('td.nb').text(), 10) + parseInt(droped.find('td.nb').text(), 10);
            var amount = parseFloat(receiver.find('td.amount').text()) + parseFloat(droped.find('td.amount').text());

            receiver.find('td.nb').text(nb);
            receiver.find('td.amount').text(amount.toFixed(2));
            receiver.attr('data-addid', droped.data('id'));
            this.parentNode.removeChild(dragSrcEl);
            sendFusionCartProposition(receiver.data('id'), droped.data('id'), receiver.find('.bdate').text());
        }
        this.classList.remove('over');
    } else {
        console.log('Pas le droit');
Administrator committed
178 179 180 181
    }



182
    return false;
Administrator committed
183 184 185
}

function handleDragEnd(e) {
186 187
    // this/e.target is the source node.
    this.classList.remove('over');
Administrator committed
188 189 190 191

}

function addDnDHandlers(elem) {
192 193 194 195 196
    elem.addEventListener('dragstart', handleDragStart, false);
    elem.addEventListener('dragover', handleDragOver, false);
    elem.addEventListener('dragleave', handleDragLeave, false);
    elem.addEventListener('drop', handleDrop, false);
    elem.addEventListener('dragend', handleDragEnd, false);
Administrator committed
197 198 199 200

}

var releaseTimeSlotOfCurrentOrder = function() {
201 202
    var c_id = order._id;

Administrator committed
203 204
    try {
        post_form(
205 206 207 208 209 210 211 212
            '/shop/delete_cart',
            {cart_id: c_id},
            function(err, result) {
                storeOrderForMigration();
                displayMsg("Le temps pour valider le panier s'est écoulé; le créneau a été libéré.<br/>Le contenu du panier a été sauvegardé, et vous devez maintenant réinitialiser une commande.");
                reset_home();
            }
        );
Administrator committed
213
    } catch (error) {
214
        djLogError({ctx: "release timeslot", msg:error});
Administrator committed
215
    }
216
};
Administrator committed
217 218 219
var countdown_timer = function() {
    try {
        if (typeof order.timer_end_date != "undefined") {
220 221 222
            const difference = +new Date(order.timer_end_date) - +new Date();

            let remaining = "Temps écoulé";
Administrator committed
223 224

            if (difference > 0) {
225
                const parts = {
Administrator committed
226 227 228
                    h: Math.floor((difference / (1000 * 60 * 60)) % 24),
                    mn: Math.floor((difference / 1000 / 60) % 60),
                    s: Math.floor((difference / 1000) % 60)
229
                };
Administrator committed
230

231
                remaining = Object.keys(parts)
Administrator committed
232
                    .map(part => {
233 234 235 236
                        val = parts[part];
                        if ((part == 'mn' || part == 's') && val < 10) val = '0' + val;

                        return `${val} ${part}`;
Administrator committed
237 238 239 240
                    })
                    .join(" ");

            } else {
241
                releaseTimeSlotOfCurrentOrder();
Administrator committed
242
            }
243
            document.getElementById("countdown").innerHTML = remaining;
Administrator committed
244 245 246
        } else {
            //order has been stored for migration or has been stored before countdown implementation
            if (typeof order.best_date != "undefined") {
247 248 249
                const difference = +new Date(order.best_date.replace(/-/g, "/")) - +new Date();

                if (difference/1000/3600 < 24) releaseTimeSlotOfCurrentOrder();
Administrator committed
250 251 252
            }
        }
    } catch (error) {
253
        djLogError({ctx: "countdown timer", msg:error});
Administrator committed
254
    }
255
};
Administrator committed
256 257

var launch_countdown_timer = function() {
258 259 260 261
    countdown_timer();
    if (timer) clearTimeout(timer);
    timer = setInterval(countdown_timer, 1000);
};
Administrator committed
262 263

var getMaxTimeBeforeValidation = function() {
264 265
    var max_time = 1;

Administrator committed
266
    try {
267 268 269 270
        const selected_day = $('.overlay-content [name="bday"]').val()
            .replace(/-/g, "/"); // replace for Safari
        const selected_hour = $('.overlay-content [name="bhour"]').val();

Administrator committed
271
        if (selected_hour.length > 0) {
272 273 274 275
            const difference = +new Date(selected_day + ' ' + selected_hour) - +new Date(); // unit = ms
            const h_before_pickup = difference/1000/3600;
            const delta = h_before_pickup - min_delay;

Administrator committed
276
            if (delta >= (hours_for_validation - 0.5)) {
277 278 279
                max_time = hours_for_validation;
            } else if (delta > 0.3) {
                max_time = delta;
Administrator committed
280
            } else {
281
                max_time = 0.3;
Administrator committed
282 283
            }
        } else {
284
            max_time = -1;
Administrator committed
285 286 287
        }

    } catch (error) {
288
        djLogError({ctx: "get maxtime bf valid", msg:error});
Administrator committed
289
    }
290 291 292

    return max_time;
};
Administrator committed
293 294

var adaptTimeGivenForValidationMsg = function() {
295 296
    var max_time_before_validation = getMaxTimeBeforeValidation();

Administrator committed
297
    if (!isNaN(max_time_before_validation) && max_time_before_validation > -1) {
298 299 300 301
        const h = Math.floor(max_time_before_validation % 24);
        var mn = Math.floor((max_time_before_validation - h) * 60);

        if (mn < 10) mn = '0' + mn;
Administrator committed
302

303 304
        $('.overlay-content .time-given-for-validation').text(h + ' h '+ mn + ' mn');
        $('.overlay-content .tv-msg').show();
Administrator committed
305
    } else {
306
        $('.overlay-content .tv-msg').hide();
Administrator committed
307
    }
308
};
Administrator committed
309 310 311

/** return javascript setted dates for time computation **/
var getStartTimeAndEndTimeForShortDay = function(short_day) {
312 313
    var periods = [];

Administrator committed
314 315
    try {
        for (i in opening[short_day]) {
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
            var [
                sh,
                sm
            ] = opening[short_day][i]['start'].split(':');
            var [
                eh,
                em
            ] = opening[short_day][i]['end'].split(':');
            var s = new Date();
            var e = new Date();

            s.setHours(sh);
            s.setMinutes(sm);
            e.setHours(eh);
            e.setMinutes(em);
            periods.push({start:s, end: e});
Administrator committed
332 333
        }
    } catch (error) {
334
        djLogError({ctx: "getStartTimeAndEndTimeForShortDay", msg:error});
Administrator committed
335
    }
336 337 338

    return periods;
};
Administrator committed
339
var getSlotsNumberForShorDay = function(short_day) {
340 341
    var slots_pack_nb = 0;

Administrator committed
342
    try {
343 344
        var periods = getStartTimeAndEndTimeForShortDay(short_day);

Administrator committed
345
        for (i in periods) {
346
            slots_pack_nb += (periods[i].end - periods[i].start)/1000/60/slot_size;
Administrator committed
347 348
        }
    } catch (error) {
349
        djLogError({ctx: "getSlotsNumberForShorDay", msg:error});
Administrator committed
350
    }
351 352 353

    return slots_pack_nb;
};
Administrator committed
354 355

var generateHourOptions = function() {
356 357 358 359 360
    var selected_day = $('.overlay-content [name^="bday"]').val();
    var hours_sel = $('.overlay-content [name^="bhour"]');
    var options = [{val: '', text: '---> Heure'}];
    var short_day = new Intl.DateTimeFormat('fr-FR', {weekday: 'short'}).format(new Date(selected_day));
    var periods = getStartTimeAndEndTimeForShortDay(short_day);
Administrator committed
361

362
    hours_sel.find('option').remove();
Administrator committed
363 364

    for (i in periods) {
365 366 367 368 369
        var start = periods[i].start;
        var end = periods[i].start;
        var d = periods[i].start;
        var nb_slots = (periods[i].end - periods[i].start)/1000/60/slot_size;

Administrator committed
370
        for (var j=0; j < nb_slots; j++) {
371 372 373 374 375 376 377 378 379 380 381
            if (j > 0) d = new Date(d.setMinutes(d.getMinutes() + slot_size));
            var h = d.getHours();
            var m = d.getMinutes();

            if (h < 10) h = '0' + h;
            if (m < 10) m = '0' + m;
            var hm = h + ':' + m;
            var slot = (selected_day + ' ' + hm).replace(/-/g, "/"); // replace for Safari
            var addit = true;
            const difference = (+new Date(slot) - +new Date())/1000/3600;

Administrator committed
382
            if (difference < 0 || (difference > 0 && min_delay > 0 && difference < min_delay)) {
383
                addit = false;
Administrator committed
384
            }
385 386 387 388
            forbidden_slots.forEach(function(s) {
                if (slot == s.replace(/-/g, "/")) addit = false;
            });
            if (addit == true) options.push({val: hm, text: hm});
Administrator committed
389 390 391
        }

    }
392 393 394 395 396
    $.each(options, function(i, e) {
        hours_sel.append($('<option>').val(e.val)
            .text(e.text));
    });
};
Administrator committed
397
var filterHourOptions = function() {
398 399
    var selected_day = $('.overlay-content [name^="bday"]').val();

Administrator committed
400
    if (typeof opening != "undefined" && (typeof opening_start_date == "undefined" || new Date(selected_day) >= opening_start_date)) {
401
        generateHourOptions();
Administrator committed
402
    } else { // for compatibility
403 404 405 406
        $('.overlay-content [name^="bhour"]').html($('#cart_creation_form [name^="bhour"]').html());
        var options = $('.overlay-content [name^="bhour"] option');

        options.each(function(i, o) {
Administrator committed
407
            try {
408
                $(o).show();
Administrator committed
409
                if ($(o).val().length > 1) {
410 411 412
                    var slot = (selected_day + ' ' + $(o).val()).replace(/-/g, "/"); // replace for Safari
                    const difference = +new Date(slot) - +new Date();

Administrator committed
413
                    if (difference/1000/3600 < min_delay) {
414
                        $(o).hide();
Administrator committed
415
                    } else {
416 417 418
                        forbidden_slots.forEach(function(s) {
                            if (slot == s.replace(/-/g, "/")) $(o).hide();
                        });
Administrator committed
419
                    }
420
                    $(o).prop('selected', false);
Administrator committed
421
                } else {
422
                    $(o).prop('selected', true); // if day has been changed
Administrator committed
423 424
                }
            } catch (error) {
425
                djLogError({ctx: "filter hours options", msg:error});
Administrator committed
426
            }
427
        });
Administrator committed
428
    }
429
    $('.overlay-content .tv-msg').hide();
Administrator committed
430

431
};
Administrator committed
432 433

var is_possible_day = function(date, short_day) {
434 435 436
    var answer = true;

    var dsfn = 0; // day slots forbidden number
Administrator committed
437

438 439 440 441 442
    $.each(forbidden_slots, function(i, e) {
        var dh = e.split(' ');

        if (dh.length == 2 && dh[0] == date) dsfn += 1;
    });
Administrator committed
443 444 445 446
    if (typeof opening != "undefined") {

        if (typeof opening[short_day] != "undefined") {
            // compute slots_pack_nb value
447
            slots_pack_nb = getSlotsNumberForShorDay(short_day);
Administrator committed
448 449 450


        } else {
451
            answer = false;
Administrator committed
452 453
        }
    }
454
    if (dsfn == slots_pack_nb) answer = false;
Administrator committed
455

456 457
    return answer;
};
Administrator committed
458 459

var fillBDayOptions = function(select) {
460 461 462 463 464 465 466 467 468 469 470 471
    select.find('option').remove();
    select.append($('<option>').attr("value", "")
        .text("---> Jour"));
    date_options = {weekday:'long', day: 'numeric', month: 'long'};
    var opening_days = [
        'mar.',
        'mer.',
        'jeu.',
        'ven.',
        'sam.'
    ]; // default for previous code compat.

Administrator committed
472
    if (typeof opening != "undefined") {
473
        opening_days = Object.keys(opening);
Administrator committed
474
    }
475 476 477
    var start = new Date();
    var d = new Date();

Administrator committed
478
    for (var i=0; i<10; i++) {
479 480 481 482 483 484 485 486 487 488 489 490 491
        if (i > 0) d = new Date(d.setDate(d.getDate() + 1));

        if (!closing_dates.includes(d.toISOString().slice(0, 10))) {
            short_day = new Intl.DateTimeFormat('fr-FR', {weekday: 'short'}).format(d);
            if (opening_days.indexOf(short_day) > -1) {
                var date_text = d.toLocaleDateString('fr-FR', date_options);
                var date = d.toISOString().slice(0, 10);
                // Adding it only if slots are left

                if (is_possible_day(date, short_day))
                    select.append($('<option>').attr("value", date)
                        .text(date_text));
            }
Administrator committed
492 493
        }
    }
494
};
Administrator committed
495 496 497


var getStoredOrder = function() {
498 499
    var stored = null;

Administrator committed
500
    try {
501
        stored = JSON.parse(localStorage.getItem(current_order_name));
502
    } catch (e) {
Administrator committed
503 504
        //WARNING : In this case, make sure the user haven't got any order initialized
        //TODO : make a request to retrieve it
505
        alert("Votre navigateur ne permet pas de mémoriser durablement les paniers (localStorage)");
Administrator committed
506
    }
507 508 509

    return stored;
};
Administrator committed
510 511

var getStoredOrderForMigration = function() {
512 513
    var stored = null;

Administrator committed
514
    try {
515
        stored = JSON.parse(localStorage.getItem(saved_order_name));
516 517
    } catch (e) {
        djLogError({ctx: "get stored for mig", msg:e});
Administrator committed
518
    }
519 520 521

    return stored;
};
Administrator committed
522 523
var storeOrderForMigration = function() {
    try {
524 525 526 527 528 529
        var to_save = order;

        delete to_save._id;
        delete to_save._rev;
        delete to_save.timer_end_date;
        delete to_save.best_date;
530
        localStorage.setItem(saved_order_name, JSON.stringify(to_save));
531 532 533 534
        order = to_save;
        storeOrder();
    } catch (e) {
        djLogError({ctx: "store for mig", msg:e});
Administrator committed
535
    }
536
};
Administrator committed
537 538
var storeOrder = function() {
    try {
539
        localStorage.setItem(current_order_name, JSON.stringify(order));
540 541
    } catch (e) {
        djLogError({ctx: "store order", msg:e});
Administrator committed
542
    }
543
};
Administrator committed
544 545

var getStoredCatElts = function() {
546 547
    var stored = null;

Administrator committed
548 549
    try {
        stored = JSON.parse(localStorage.getItem('catElts'));
550
    } catch (e) {
Administrator committed
551 552
        //no matter
    }
553 554 555

    return stored;
};
Administrator committed
556 557 558

var storeCatElts = function() {
    try {
559 560 561
        localStorage.setItem("catElts", JSON.stringify(category_elts));
    } catch (e) {
        djLogError({ctx: "store cat elts", msg:e});
Administrator committed
562
    }
563
};
Administrator committed
564
var putLoadingImgOn = function(target) {
565 566
    target.append(loading_img);
};
Administrator committed
567
var removeLoadingImg = function() {
568 569
    $('.rotating_loader').remove();
};
Administrator committed
570 571

var putAlimCategData = function() {
572 573 574 575 576 577 578 579 580 581 582 583
    [
        'epicerie',
        'liquide',
        'produits_frais',
        'surgeles'
    ].forEach(function(k) {
        var div = categ_menu_template.clone().attr('data-id', categories[k].id);

        div.find('.dropbtn').text(categories[k].label);
        alim_categ.append(div);
    });
};
Administrator committed
584 585

var putNonAlimCategData = function() {
586 587 588 589 590 591 592 593 594 595 596
    [
        'bazar',
        'droguerie',
        'parfumerie'
    ].forEach(function(k) {
        var div = categ_menu_template.clone().attr('data-id', categories[k].id);

        div.find('.dropbtn').text(categories[k].label);
        non_alim_categ.append(div);
    });
};
Administrator committed
597 598 599 600 601

var pass2step2 = function() {
    try {
        if (order.products.length > 0 && order.state == "init") {
            // a cart in process has been stored, render it
602
            renderStoredCart();
Administrator committed
603
        }
604 605
    } catch (e) {
        djLogError({ctx: "pass2step2", msg:e});
Administrator committed
606
    }
607 608 609 610
    main_waiting_zone.hide();
    $('h1').show();
    var recup = order.best_date || 'non défini';

611 612
    if (shop_mode == 'shop')
        current_order_bdate.html("Récupération : <strong>" + french_date_and_time(recup) + "</strong>");
Administrator committed
613
    if (visit_mode == true) {
614 615 616
        $('.product .choice').hide();
        right_column.hide();
        $('.arrow-block').css('visibility', 'hidden');
Administrator committed
617 618

    } else {
619 620 621
        $('.product .choice').show();
        right_column.show();
        $('.arrow-block').css('visibility', 'visible');
Administrator committed
622 623

    }
624 625 626
    orders_section.hide();
    main_content.show();
    shop_section.show();
Administrator committed
627

628 629
    $('#header_step_one').addClass('step_one_active');
};
Administrator committed
630 631 632 633

var updateCartTotal = function(products_nb, total) {

    if (products_nb == 0) {
634 635 636 637 638
        valid_wrapper.hide();
        cart.find('.msg').show();
        cart_total.find('span').text('');
    } else {
        cart_total.find('span').text('Total : ' + total.toFixed(2) + ' € TTC');
Administrator committed
639
    }
640
};
Administrator committed
641 642

var addProductToOrder = function(pdt, max_qty, callback) {
643
    answer = {product: pdt};
Administrator committed
644 645

    // Insert or update pdt data
646 647 648 649 650
    var p_index = null;

    $.each(order.products, function(i, e) {
        if (e.id == pdt.id) p_index = i;
    });
Administrator committed
651
    if (p_index !== null) {
652 653 654
        var p = order.products[p_index];
        var qty = p.qty + pdt.qty;

Administrator committed
655
        if (pdt.unit != "U") {
656
            qty = parseFloat(p.qty) + parseFloat(pdt.qty);
Administrator committed
657
        }
658 659
        var total = parseFloat(p.total) + parseFloat(pdt.total);

Administrator committed
660
        if (qty > max_qty) {
661
            answer.warning = "max_qty";
Administrator committed
662
        }
663 664 665 666
        order.products[p_index].qty = qty;
        order.products[p_index].total = parseFloat(total).toFixed(2);
        answer.product = order.products[p_index];
        answer.refresh = true;
Administrator committed
667
    } else {
668
        order.products.push(pdt);
Administrator committed
669 670
    }

671 672 673 674 675
    order.total += parseFloat(pdt.total);
    updateCartTotal(order.products.length, order.total);
    storeOrder();
    callback(answer);
};
Administrator committed
676 677

var removeProductFromCart = function() {
678 679
    var cart_elt = $(this).closest('.cart-elt');
    var pid = cart_elt.data('pid');
Administrator committed
680
    //console.log('On va supprimer le produit ' + pid)
681 682 683
    var p_index = null;

    $.each(order.products, function(i, e) {
Administrator committed
684
        if (e.id == pid) {
685 686
            p_index = i;
            order.total -= parseFloat(e.total);
Administrator committed
687
        }
688
    });
Administrator committed
689
    if (p_index !== null) {
690 691 692 693
        order.products.splice(p_index, 1);
        updateCartTotal(order.products.length, order.total);
        cart_elt.remove();
        storeOrder();
Administrator committed
694
    }
695
};
Administrator committed
696
var renderProductInCart = function(elt_div, pdt) {
697 698 699 700 701 702 703
    elt_div.find('.name').text(pdt.name);
    elt_div.find('.qty').text(pdt.qty);
    elt_div.find('.price').text(pdt.price);
    elt_div.find('.total').text(pdt.total);
    cart.prepend(elt_div);
    elt_div.find('img').click(removeProductFromCart);
};
Administrator committed
704 705

var addProductToCart = function() {
706 707 708
    var p_div = $(this).closest('.product');
    var qty = p_div.find('input[name="qty"]').val();
    var available_qty = parseFloat(p_div.find('.available_qty').text());
Administrator committed
709

710 711 712
    cart.find('.msg').hide();
    valid_cart.show();
    valid_wrapper.show();
Administrator committed
713 714 715 716
    try {
        if (order.state == "init") {

            if (qty > 0) {
717 718 719
                var msg = "";
                var too_much = "Vous avez pris plus de produit que le stock indicatif.\nVous n'aurez peut-être pas toute la quantité.";

Administrator committed
720
                if (parseFloat(qty) > available_qty) {
721
                    msg = too_much;
Administrator committed
722
                }
723 724 725
                var u = p_div.find('.unit').text()
                    .substring(0, 1);

Administrator committed
726
                if (u == "U") {
727
                    qty = parseInt(qty, 10);
Administrator committed
728
                } else {
729
                    qty = parseFloat(qty).toFixed(3);
Administrator committed
730 731 732
                }

                var pdt = {id: p_div.data('pid'),
733 734 735 736 737 738 739 740 741 742
                    name: p_div.find('.name').text(),
                    qty : qty,
                    price : parseFloat(p_div.find('span.price').text()).toFixed(2),
                    unit: u
                };

                pdt.total = pdt.qty * pdt.price;
                pdt.total = parseFloat(pdt.total).toFixed(2);

                addProductToOrder(pdt, available_qty, function(answer) {
Administrator committed
743 744 745
                    // console.log('réponse')
                    // console.log(answer)
                    if (typeof answer.error === "undefined") {
746 747
                        var cart_elt_div = null;

Administrator committed
748
                        if (answer.refresh) {
749
                            cart_elt_div = cart.find('[data-pid="'+pdt.id+'"]');
Administrator committed
750
                        } else {
751
                            cart_elt_div = cart_elt_template.clone().attr('data-pid', pdt.id);
Administrator committed
752 753
                        }
                        if (cart_elt_div && cart_elt_div.length > 0) {
754
                            renderProductInCart(cart_elt_div, answer.product);
Administrator committed
755
                        } else {
756
                            djLogError("Problème insertion de l'article dans le panier");
Administrator committed
757 758 759 760
                        }
                    }
                    if (typeof answer.warning !== "undefined") {
                        if (answer.warning == "max_qty")
761
                            msg = too_much;
Administrator committed
762
                    }
763
                });
Administrator committed
764 765

                if (msg.length > 0) {
766
                    alert(msg);
Administrator committed
767 768
                }
            }
769 770
        } else if (order.state == "validating") {
            alert("Le panier est en cours d'envoi. Impossible d'ajouter un article.");
Administrator committed
771
        }
772 773
    } catch (e) {
        djLogError({ctx: "add product to cart", msg:e});
Administrator committed
774 775
    }

776
};
Administrator committed
777 778

var isChoosenSlotValid = function(slot) {
779 780 781
    var answer = true;
    var cause = '';

782 783 784 785 786 787 788 789 790 791 792 793 794 795 796
    // For delivery, no slot validation: return true
    if (shop_mode == 'shop') {
        forbidden_slots.forEach(function(e) {
            if (slot == e) {
                answer = false; cause = 'full';
            }
        });
        //Does it respect min delay ?
        var now = new Date();
        var min_date = new Date();
        var slot_date = new Date(slot.replace(/-/g, "/")); // replace for Safari

        min_date = new Date(min_date.setHours(now.getHours() + min_delay));
        if (slot_date - min_date < 0) {
            answer = false; cause = 'delay';
797 798 799 800 801
        }
    }

    return {res: answer, reason: cause};
};
Administrator committed
802 803

var showForbiddenSlots = function() {
804 805 806
    if (forbidden_slots.length > 0) {
        var fb_slots = $('.mconfirm .forbidden-slots');
        var ul = fb_slots.find('ul');
Administrator committed
807

808 809 810 811 812 813 814 815 816 817
        ul.empty();
        forbidden_slots.forEach(function(e) {
            var li = $('<li>').text(e);

            ul.append(li);
        });
        fb_slots.show();
    }

};
Administrator committed
818
var closeForbiddenList = function() {
819 820 821 822
    var fb_slots = $('.mconfirm .forbidden-slots');

    fb_slots.hide();
};
Administrator committed
823 824 825


var initCart = function () {
826 827 828 829 830 831 832
    current_action = 'init_cart';
    var bd = $('.mconfirm select[name="bday"]').val();
    var bh = $('.mconfirm select[name="bhour"]').val();
    var no_accept_msg = $('.mconfirm .no-accept-reason');
    var allowed_slot = isChoosenSlotValid(bd + ' '+ bh);

    no_accept_msg.hide();
Administrator committed
833 834

    if (allowed_slot.res == false || bd.length == 0 || bh.length == 0) {
835 836 837 838 839
        var day_zone = $('.mconfirm span.ask-day');
        var delay_msg = $('.mconfirm span.delay24h');

        day_zone.css({'border': 'none', 'background-color': 'white'});
        delay_msg.css({'background-color': 'white', 'padding': '0'});
Administrator committed
840

841 842
        if (allowed_slot.res == false && allowed_slot.reason == 'full') showForbiddenSlots();
        if (allowed_slot.reason == 'delay') delay_msg.css({'padding': '2px', 'background-color': 'black'});
Administrator committed
843

844 845 846
        no_accept_msg.show();
        setTimeout(function() {
            modal.css("width", "100%"); //modal div is closed after callback has been triggered
Administrator committed
847
            if (allowed_slot == true && (bd.length == 0 || bh.length == 0))
848 849
                day_zone.css({'border':'1px solid red', 'background-color':'#fcbbf4'});
        }, 500);
Administrator committed
850 851 852 853


    } else {
        if (is_time_to('init_cart', 15000)) { // prevent double click or browser hic up bug
854 855
            order.state = 'init';
            order.best_date = bd + " " + bh;
Administrator committed
856

857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885
            make_user_wait('Enregistrement des informations de réservation...');
            post_form(
                '/shop/cart_init', {order: JSON.stringify(order)},
                function(err, result) {
                    if (!err) {
                        main_content.show();
                        if (typeof result.res.cart != "undefined" && result.res.cart.length == 2) {
                            var storedOrderFM = getStoredOrderForMigration();

                            if (storedOrderFM) {
                                order.products = storedOrderFM.products;
                                order.total = storedOrderFM.total;
                                localStorage.removeItem('saved_order');
                            }
                            order._id = result.res.cart[0];
                            if (hours_for_validation > 0) {
                                var limit = new Date();
                                const time_to_add = getMaxTimeBeforeValidation();
                                const h_to_add = Math.floor(time_to_add % 24);
                                const mn_to_add = Math.floor((time_to_add - h_to_add) * 60);

                                limit.setHours(limit.getHours() + h_to_add);
                                limit.setMinutes(limit.getMinutes() + mn_to_add);
                                order.timer_end_date = limit;
                                launch_countdown_timer();
                            }

                            storeOrder();
                            pass2step2();
Administrator committed
886 887 888


                        } else {
889 890 891 892 893 894 895 896 897 898
                            order.state = 'error_on_submit';
                            message = "Problème d'enregistrement";
                            if (typeof result.res.ts_respect != "undefined") {
                                if (result.res.ts_respect == false)
                                    message += "\nLe créneau horaire choisi n'est pas correct.";
                            } else {
                                message += "\nSi cela persiste, merci de nous contacter.";
                            }
                            alert(message);
                            djLogError({ctx: "cart init ajax response 1", msg:result});
Administrator committed
899

900 901 902 903
                        }
                    } else {
                        djLogError({ctx: "cart init ajax response 2", msg:err});
                    }
Administrator committed
904 905 906 907
                }
            );
        }
    }
908
};
Administrator committed
909 910 911 912 913



var validCart = function() {

914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948
    valid_cart.hide();
    valid_wrapper.append(loading_img);
    $('#header_step_two').addClass('step_two_active');
    order.state = 'validating';
    order.accept_substitution = $('.mconfirm input[name="accept_substitution"]').prop('checked');
    order.comment = $('textarea[name="cart_comment"]').val();
    post_form(
        '/shop/cart', {order: JSON.stringify(order)},
        function(err, result) {
            if (!err) {
                try {
                    if (typeof result.res.cart != "undefined" && result.res.cart != null) {
                        if (typeof result.res.cart._rev != "undefined") {
                            clearCart();
                            $('#header_step_three').addClass('step_three_active');
                            reset_home();
                            main_waiting_zone.find('h2').text('Merci pour votre commande !');
                            main_waiting_zone.find('li.survey').css('display', 'block');
                            display_msg_box("Un email vient d'être envoyé avec des informations supplémentaires et le récapitulatif (il peut être dans les spams).");
                        } else {
                            result.browser_error = "Pas de ._rev pour la commande !";
                            djLogError({ctx: "Valid cart 1", msg:result});
                        }
                    } else {
                        storeOrderForMigration();
                        var msg = "Le pré-enregistrement de la commande, avec la date, n'a pas pu être retrouvé.\n";

                        msg += "Les produits du panier ont été sauvegardés.\n";
                        alert(msg);
                        reset_home();
                        djLogError({ctx: "Valid cart 2", msg:result});
                    }
                    valid_wrapper.find('.rotating_loader').remove();
                } catch (e) {
                    djLogError({ctx: "Valid cart 3", msg:result, msg2:e});
Administrator committed
949
                }
950 951 952 953
            } else {
                djLogError({ctx: "Valid cart 4", msg:err});
            }
        }
Administrator committed
954
    );
955
};
Administrator committed
956 957

var clearCart = function() {
958
    order = { products: [], total: 0.00, type: shop_mode};
959 960 961 962 963
    storeOrder();
    cart.find('.cart-elt').remove();
    updateCartTotal(0, 0);
    valid_wrapper.find('.rotating_loader').remove();
};
Administrator committed
964 965

var renderStoredCart = function() {
966 967 968 969 970 971 972 973 974 975 976 977
    cart.find('.cart-elt').remove();
    cart.find('.msg').hide();
    valid_wrapper.show();
    $.each(order.products, function(i, e) {
        var cart_elt_div = cart_elt_template.clone().attr('data-pid', e.id);

        renderProductInCart(cart_elt_div, e);

    });
    if (order.products.length > 0) valid_cart.show();
    updateCartTotal(order.products.length, order.total);
};
Administrator committed
978 979 980


var appendProductsToGrid = function (grid, pdts, sort = true) {
981 982 983 984 985 986 987 988 989
    var product_divs = [];

    $.each(pdts, function (i, pdt) {
        var p_div = product_template.clone().attr('data-pid', pdt.id);
        var qty = p_div.find('input[name="qty"]');

        p_div.find('.name').text(pdt.name);
        if (pdt.image_small != false)
            p_div.find('img').attr('src', 'data:image/jpeg;base64,' + pdt.image_small);
Administrator committed
990
        //p_div.find('img').remove()
991 992
        p_div.find('span.price').text(pdt.list_price);
        p_div.find('.unit').text(pdt.uom_id[1]);
Administrator committed
993
        if (pdt.price_weight_net != "") {
994
            p_div.find('span.uom_price').text(pdt.price_weight_net + ' €');
Administrator committed
995
        } else if (pdt.price_volume != "") {
996
            p_div.find('span.uom_price').text(pdt.price_volume + ' €');
Administrator committed
997
        } else if (pdt.price_weight_net == "" && pdt.price_volume == "" && pdt.uom_id[0] == 1) {
998
            p_div.find('span.uom_price').text('non renseigné');
Administrator committed
999 1000
        }
        if (pdt.uom_id[0] == 1) {
1001
            p_div.find('div.uom_price').show();
Administrator committed
1002
        }
1003 1004
        p_div.find('.available_qty').text(pdt.qty_available);
        p_div.find('.incoming_qty').text(pdt.incoming_qty);
Administrator committed
1005 1006

        if (pdt.uom_id[1].indexOf('U') == 0) {
1007
            qty.attr('oninput', "this.value=this.value.replace(/[^0-9]/g,'');");
Administrator committed
1008
        } else {
1009
            qty.attr('min', 0).attr('step', 0.1);
Administrator committed
1010
        }
1011
        qty.val(1);
Administrator committed
1012

1013 1014
        product_divs.push(p_div);
    });
Administrator committed
1015 1016

    if (product_divs.length > 0) {
1017
        if (sort) {
Administrator committed
1018
        // Sort by selected sort type and display
1019 1020 1021
            var sort_type = grid.parent().find('select.products_sort option:selected')
                .val();
            var sorted_products = sort_product_divs(product_divs, sort_type);
Administrator committed
1022

1023 1024 1025 1026
            grid.html(sorted_products);
        } else {
            grid.html(product_divs);
        }
Administrator committed
1027

1028 1029
        grid.parent().find('div.products_sort_container')
            .show();
Administrator committed
1030
    }
1031
};
Administrator committed
1032 1033

var loadAllAvailableBoughtProducts = function() {
1034 1035 1036 1037
    var msg_cont = $('#content1_msg');

    putLoadingImgOn(content1);
    msg_cont.html($('#loading_bought_products_msg').html());
Administrator committed
1038
    try {
1039 1040 1041 1042 1043 1044
        $.ajax({
            url :'/shop/get_all_available_bought_products',
            dataType: 'json'
        })
            .done(function(rData) {
                removeLoadingImg();
Administrator committed
1045 1046 1047

                //console.log(rData)
                if (rData.res && rData.res.data && rData.res.data.pdts && rData.res.data.pdts.length > 0) {
1048 1049 1050 1051
                    msg_cont.remove();
                    var grid = content1.find('section');

                    appendProductsToGrid(grid, rData.res.data.pdts, false);
Administrator committed
1052
                } else {
1053
                    msg_cont.html('Aucun produit trouvé');
Administrator committed
1054
                }
1055
            });
Administrator committed
1056

1057 1058
    } catch (e) {
        alert('Impossible de récupérer les produits que vous avez déjà achetés');
Administrator committed
1059
    }
1060
};
Administrator committed
1061 1062

var appendChildrenCatToMenu = function (catdiv, children) {
1063 1064 1065
    var ul = catdiv.find('ul');

    $.each(children, function(i, e) {
Administrator committed
1066
        if (excluded_cat.indexOf(e.id) < 0) {
1067
            var li = $('<li>').addClass("nav-item");
1068 1069 1070 1071 1072 1073 1074 1075 1076

            // Remove TVA in cat name
            let name = e.name;

            name = name.replaceAll(' TVA 20%', '');
            name = name.replaceAll(' TVA 5,5%', '');
            name = name.replaceAll(' 20%', '');
            name = name.replaceAll(' 5,5%', '');

1077
            var span = $('<span>').attr('data-id', e.id)
1078
                .text(name);
1079 1080 1081

            li.append(span);
            ul.append(li);
Administrator committed
1082 1083
        }

1084 1085
    });
};
Administrator committed
1086 1087

var getCategChildren = function() {
1088 1089 1090 1091
    var clicked = $(this);
    var cat_id = clicked.data('id');
    var li_nb = clicked.find('li').length;

Administrator committed
1092 1093 1094
    if (typeof category_elts[cat_id] == "undefined") {
        try {
            $.ajax({
1095 1096 1097 1098
                //url :'/shop/get_categ_products',
                url : '/shop/get_cat_children',
                data: {id: cat_id},
                dataType: 'json'
Administrator committed
1099
            })
1100 1101 1102 1103 1104 1105 1106
                .done(function(rData) {
                    if (typeof rData.res.data != "undefined" && rData.res.data.length > 0) {
                        category_elts[cat_id] = rData.res.data;
                        storeCatElts();
                        appendChildrenCatToMenu(clicked, category_elts[cat_id]);
                    }
                });
Administrator committed
1107

1108
        } catch (e) {
Administrator committed
1109 1110 1111
            //alert('Impossible de récupérer les catégories')
        }
    } else if (li_nb == 0) {
1112
        appendChildrenCatToMenu(clicked, category_elts[cat_id]);
Administrator committed
1113
    }
1114
};
Administrator committed
1115 1116

var getCategProducts = function() {
1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132
    var clicked = $(this);
    var cat_id = clicked.data('id');
    var tab = clicked.closest('li.tab');
    var msg_cont = tab.find('.msg');
    var content = tab.find('.content');
    var grid = content.find('section');
    var msg = $('#loading_categ_products_msg').clone()
        .removeAttr('id');

    grid.empty();
    putLoadingImgOn(content);
    var menu_ul = tab.find('ul');

    menu_ul.hide(); /** needed to make visitor known retrival is in process **/
    msg.find('span').text(clicked.text());
    msg_cont.html(msg);
Administrator committed
1133
    try {
1134 1135 1136 1137 1138 1139
        $.ajax({
            url :'/shop/get_categ_products',
            data: {id: cat_id},
            dataType: 'json'
        })
            .done(function(rData) {
Administrator committed
1140 1141
                if (typeof rData.res.data != "undefined" && typeof rData.res.data.pdts != "undefined") {
                    //console.log(rData)
1142
                    appendProductsToGrid(grid, rData.res.data.pdts);
Administrator committed
1143 1144 1145
                } else {
                    // ? what to do
                }
1146 1147 1148 1149
                removeLoadingImg();
                msg_cont.empty();
                menu_ul.removeAttr('style'); /** Needed to enable dropdown menu again **/
            });
Administrator committed
1150

1151 1152
    } catch (e) {
        alert('Impossible de récupérer les articles de cette catégorie');
Administrator committed
1153
    }
1154
};
Administrator committed
1155

1156
var initPromotionTabs = function() {
Administrator committed
1157
    if (promoted_pdts.length > 0) {
1158 1159 1160 1161
        $('.tab.promote').css('display', 'block');
        appendProductsToGrid($('#tab-content0').find('section'), promoted_pdts);
        $('input[name="tabs"]').removeAttr('checked');
        $('#tab0').prop('checked', true);
Administrator committed
1162 1163
    }
    if (discounted_pdts.length > 0) {
1164 1165
        $('.tab.discount').css('display', 'block');
        appendProductsToGrid($('#tab-content-1').find('section'), discounted_pdts);
Administrator committed
1166
    }
1167
};
Administrator committed
1168 1169

var search_product = function() {
1170 1171
    var kw = skw.val().trim();

Administrator committed
1172 1173
    if (kw.length > 0) {
        if (is_time_to('search_product', 1000)) { // prevent double click or browser hic up bug
1174 1175 1176 1177 1178 1179
            var grid = content4.find('section');
            var msg_cont = content4.find('.msg');

            grid.empty();
            putLoadingImgOn(content4);
            msg_cont.show();
Administrator committed
1180
            try {
1181 1182 1183 1184 1185 1186
                $.ajax({
                    url :'/shop/search_product',
                    data: {kw: kw},
                    dataType: 'json'
                })
                    .done(function(rData) {
Administrator committed
1187 1188
                        if (typeof rData.res.data != "undefined" && typeof rData.res.data.pdts != "undefined") {
                            //console.log(rData)
1189
                            appendProductsToGrid(grid, rData.res.data.pdts);
Administrator committed
1190 1191 1192
                        } else {
                            // ? what to do
                        }
1193 1194 1195
                        removeLoadingImg();
                        msg_cont.hide();
                    });
Administrator committed
1196

1197 1198
            } catch (e) {
                alert('Impossible de récupérer les articles de cette catégorie');
Administrator committed
1199 1200 1201 1202
            }
        }
    }

1203 1204
    return false; //prevent page reload
};
Administrator committed
1205 1206

var displaySentOrders = function() {
1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223
    var waiting_msg = orders_section.find('.waiting_msg');

    $('.arrow-block').css('visibility', 'hidden');
    $('h1').hide();

    shop_section.hide();

    main_content.show();
    orders_section.show();
    make_user_wait('Recherche de commandes envoyées....');
    my_orders_wrap.hide();
    var no_action_available_msg = orders_section.find('.no-action-available-msg');

    no_action_available_msg.hide();
    var tbody = orders_section.find('tbody');

    tbody.empty();
Administrator committed
1224
    try {
1225 1226 1227 1228 1229
        $.ajax({
            url :'/shop/my_orders',
            dataType: 'json'
        })
            .done(function(rData) {
Administrator committed
1230 1231
                if (typeof rData.res.error != "undefined") {
                    if (rData.res.error == "Authentification non valide") {
1232
                        window.location.href = '/';
Administrator committed
1233
                    } else {
1234 1235
                        alert("Impossible de récupérer les données.");
                        djLogError({ctx: "get orders", msg:rData});
Administrator committed
1236
                    }
1237
                } else if (typeof rData.res.data.orders != "undefined") {
Administrator committed
1238
                    if (rData.res.data.orders.length > 0) {
1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257
                        var eye = '<i class="fas fa-eye fl"></i>';
                        var delete_icon = '<i class="fas fa-trash fr"></i>';
                        var edit = '<i class="fas fa-edit"></i>';
                        var show_no_action_available_msg = false;

                        $.each(rData.res.data.orders, function(i, o) {
                            var bdate_content = "<span>" + o.best_date + "</span>";

                            if (o.state == "init" || o.state == "validating") bdate_content += " " + edit;
                            var actions_content = "";

                            if (o.state != "validating") show_no_action_available_msg = true;
                            if (o.submitted_time) ctime = parseInt(o.submitted_time*1000, 10);
                            else ctime = parseInt(o.init_time*1000, 10);

                            var date = format_date_to_sortable_string(new Date(ctime));
                            var tr = $('<tr>').attr('data-id', o._id)
                                .attr('data-rev', o._rev);

Administrator committed
1258
                            if (o.state == "validating" || o.state == "init") {
1259 1260
                                actions_content = delete_icon;
                                tr.prop('draggable', true);
Administrator committed
1261
                            }
1262 1263 1264 1265 1266 1267 1268 1269
                            var td1 = $('<td>').addClass('date create')
                                .text(date);
                            var td2 = $('<td>').addClass('date bdate')
                                .html(bdate_content);
                            var td3 = $('<td>').addClass('nb')
                                .text(o.products.length);
                            var td4 = $('<td>').addClass('amount')
                                .text(parseFloat(o.total).toFixed(2));
Administrator committed
1270
                            //var td5 = $('<td>').addClass('actions').html(eye + ' ' + delete_icon)
1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284
                            var td5 = $('<td>').addClass('actions')
                                .html(actions_content);

                            tr.append(td1);
                            tr.append(td2);
                            tr.append(td3);
                            tr.append(td4);
                            tr.append(td5);
                            tbody.append(tr);
                            addDnDHandlers(tr.get(0));
                        });
                        if (show_no_action_available_msg == true) no_action_available_msg.show();

                        my_orders_wrap.show();
Administrator committed
1285
                    } else {
1286 1287
                        waiting_msg.show();
                        waiting_msg.html('<h3 style="text-align:center;">Aucune commande en cours.</h3>');
Administrator committed
1288
                    }
1289
                    main_waiting_zone.hide();
Administrator committed
1290
                }
1291
            });
Administrator committed
1292

1293 1294
    } catch (e) {
        alert('Impossible de récupérer les commandes en cours');
Administrator committed
1295
    }
1296
};
Administrator committed
1297 1298 1299 1300

/** Couchdb stored cart action **/
//change date methods will be useless when cart modifications will be possible since cart process validation is already done
var validBDayChange = function(cart_id) {
1301 1302 1303
    var bd = $('.mconfirm select[name="bday-change"]').val();
    var bh = $('.mconfirm select[name="bhour-change"]').val();
    var allowed_slot = isChoosenSlotValid(bd + ' '+ bh);
Administrator committed
1304 1305

    if (allowed_slot.res == false || bd.length == 0 || bh.length == 0) {
1306 1307 1308 1309 1310 1311 1312 1313 1314 1315
        var day_zone = $('.mconfirm span.ask-day');
        var delay_msg = $('.mconfirm span.delay24h');

        day_zone.css({'border': 'none', 'background-color': 'white'});
        delay_msg.css({'background-color': 'white', 'padding': '0'});

        if (allowed_slot.res == false && allowed_slot.reason == 'full') showForbiddenSlots();
        if (allowed_slot.reason == 'delay') delay_msg.css({'padding': '2px', 'background-color': 'black'});
        setTimeout(function() {
            modal.css("width", "100%"); //modal div is closed after callback has been triggered
Administrator committed
1316
            if (allowed_slot == true && (bd.length == 0 || bh.length == 0))
1317 1318
                day_zone.css({'border':'1px solid red', 'background-color':'#fcbbf4'});
        }, 500);
Administrator committed
1319 1320 1321 1322


    } else {
        if (is_time_to('change_date', 15000)) { // prevent double click or browser hic up bug
1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349
            var waiting_msg = $('<p>').append(loading_img);

            waiting_msg.append("<br/>Traitement de la demande de changement de date en cours....");
            setTimeout(function() {
                displayMsg(waiting_msg);
            }, 200); // delay needed because of closeModal called by confirm click
            post_form(
                '/shop/cart/' + cart_id + '/change_date',
                {new_date: bd + " " + bh},
                function(err, result) {
                    if (typeof result.res.changed != "undefined" && typeof result.res.changed[0] != "undefined" && result.res.changed[0].length == 3) {
                        alert("Nouvelle date enregistrée !");
                        displaySentOrders();
                    } else {

                        message = "Problème d'enregistrement";
                        if (typeof result.res.ts_respect != "undefined") {
                            if (result.res.ts_respect == false)
                                message += "\nLe créneau horaire choisi n'est pas correct.";
                        } else {
                            message += "\nSi cela persiste, merci de nous contacter.";
                        }
                        alert(message);
                    }
                    closeModal();
                }
            );
Administrator committed
1350 1351
        }
    }
1352
};
Administrator committed
1353
var changeBestDate = function() {
1354
    var clicked = $(this);
Administrator committed
1355 1356
    var clicked_tr = clicked.closest('tr'),
        id = clicked_tr.data('id'),
1357 1358 1359
        msg = modify_best_date_msg;

    msg.find('.current-bdate').text(clicked_tr.find('.bdate span').text());
Administrator committed
1360
    //copy hours from cart validation and slots constraints div to this msg content
1361
    var cart_vform = $('#cart_creation_form');
Administrator committed
1362

1363 1364
    msg.find('select[name="bhour-change"]').html(cart_vform.find('select[name="bhour"]').html());
    msg.find('.slots-constraints').html(cart_vform.find('.slots-constraints').html());
1365 1366
    if (shop_mode == 'shop')
        fillBDayOptions(bday_change_sel);
1367 1368 1369 1370 1371 1372

    updateUnavailableSlots(function() {
        openModal(msg.html(), function() {
            validBDayChange(id);
        }, 'Enregistrer');
    });
Administrator committed
1373

1374 1375

};
Administrator committed
1376 1377

var destroySentCart = function() {
1378
    var clicked = $(this);
Administrator committed
1379 1380
    var clicked_tr = clicked.closest('tr'),
        id = clicked_tr.data('id'),
1381
        msg = cart_destroy_msg.clone();
Administrator committed
1382

1383 1384
    msg.find('.date').text(clicked_tr.find('.create').text());
    msg.find('.bdate').text(clicked_tr.find('.bdate span').text());
Administrator committed
1385
    openModal(
1386 1387
        msg.html(),
        function() {
Administrator committed
1388
        // Confirm button callback
1389 1390 1391 1392 1393 1394 1395 1396 1397 1398
            var waiting_msg = $('<p>').append(loading_img);

            waiting_msg.append("<br/>Traitement de la demande de suppression en cours....");
            setTimeout(function() {
                displayMsg(waiting_msg);
            }, 200); // delay needed because of closeModal called by confirm click
            post_form(
                '/shop/delete_cart',
                {cart_id: id},
                function(err, result) {
Administrator committed
1399 1400
                    if (!err) {
                        if (typeof result.res !== "undefined" && typeof result.res.del_action !== "undefined") {
1401
                            clicked_tr.remove();
Administrator committed
1402
                            // remove from browser stored data if it was the current one
1403 1404
                            if (order._id == id) clearCart();
                            alert("Commande détruite");
Administrator committed
1405
                        } else {
1406
                            djLogError({ctx: "destroy", msg: result});
Administrator committed
1407 1408 1409
                        }

                    } else {
1410
                        djLogError({ctx: "destroy", msg:err});
Administrator committed
1411
                    }
1412 1413 1414 1415 1416 1417
                    closeModal();
                }
            );
        },
        'Détruire'
    );
Administrator committed
1418

1419
};
Administrator committed
1420 1421

var sendFusionCartProposition = function(main_id, addid, date) {
1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434
    openModal(
        'Enregistrer la fusion des 2 commandes<br/>Récupération le '+ french_date_and_time(date),
        function() {
            var waiting_msg = $('<p>').append(loading_img);

            waiting_msg.append("<br/>Traitement de la demande de fusion en cours....");
            setTimeout(function() {
                displayMsg(waiting_msg);
            }, 200); // delay needed because of closeModal called by confirm click
            post_form(
                '/shop/fusion_carts',
                {id: main_id, add_id: addid},
                function(err, result) {
Administrator committed
1435 1436 1437
                    if (!err) {
                        try {
                            if (typeof result.res !== "undefined" && typeof result.res.del_action !== "undefined") {
1438
                                alert("Commandes fusionnées");
Administrator committed
1439 1440

                            } else {
1441
                                djLogError({ctx: "fusion", msg: result});
Administrator committed
1442
                            }
1443 1444
                        } catch (e1) {
                            djLogError({ctx: "fusion", msg: e1});
Administrator committed
1445 1446 1447
                        }

                    } else {
1448
                        djLogError({ctx: "fusion", msg:err});
Administrator committed
1449
                    }
1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460
                    closeModal();
                }
            );
        },
        'Envoyer',
        true,
        true,
        displaySentOrders // callback if canceled

    );
};
Administrator committed
1461 1462 1463 1464

// Get full slots and closing dates
var updateUnavailableSlots = function(callback) {
    try {
1465 1466 1467 1468 1469 1470
        $.ajax({
            url :'/shop/full_slots',
            dataType: 'json'
        })
            .done(function(rData) {
                forbidden_slots = [];
Administrator committed
1471
                if (typeof rData.res.full_slots != "undefined" && rData.res.full_slots.length > 0) {
1472
                    forbidden_slots = rData.res.full_slots;
Administrator committed
1473 1474
                }

1475
                closing_dates = [];
Administrator committed
1476
                if (typeof rData.res.closing_dates != "undefined" && rData.res.closing_dates.length > 0) {
1477
                    closing_dates = rData.res.closing_dates;
Administrator committed
1478 1479
                }

1480 1481
                callback();
            });
Administrator committed
1482

1483 1484
    } catch (e) {
        djLogError({ctx: "update unavailable slots", msg:e});
Administrator committed
1485
    }
1486
};
Administrator committed
1487 1488

var launch_init_form = function() {
1489 1490 1491
    make_user_wait('Préparation du formulaire...');
    current_action = 'init_form';
    updateUnavailableSlots(function() {
1492 1493
        if (shop_mode == 'shop')
            fillBDayOptions(bday_sel);
1494 1495 1496
        openModal($('#cart_creation_form').html(), initCart, 'Commencer la commande');
    });
};
Administrator committed
1497 1498

var ask_user_for_action = function(msg) {
1499 1500 1501 1502
    main_waiting_zone.show();
    main_waiting_zone.find('.msg').html(msg);
    main_waiting_zone.find('.loader').hide();
};
Administrator committed
1503 1504

var make_user_wait = function(msg) {
1505 1506 1507 1508
    main_waiting_zone.show();
    main_waiting_zone.find('.msg').html(msg);
    main_waiting_zone.find('.loader').show();
};
Administrator committed
1509 1510 1511 1512

var init_shop = function() {
    if (typeof order._id == "undefined") {
        if (order.products.length > 0) {
1513
            storeOrderForMigration();
Administrator committed
1514
        }
1515
        ask_user_for_action($('#templates .after-login-msg').html());
Administrator committed
1516 1517

    } else {
1518 1519
        launch_countdown_timer();
        pass2step2();
Administrator committed
1520 1521 1522
    }


1523
};
Administrator committed
1524 1525

var reset_home = function() {
1526 1527 1528 1529 1530 1531 1532 1533
    window.scrollTo(0, 0);
    $('.arrow-block').css('visibility', 'visible');
    main_content.hide();
    main_waiting_zone.show();
    orders_section.hide();
    orders_section.find('tbody').empty();
    init_shop();
};
Administrator committed
1534 1535

var justVisit = function() {
1536 1537 1538
    visit_mode = true;
    pass2step2();
};
Administrator committed
1539

1540 1541 1542
var current_order_name = shop_mode == 'shop' ? 'currentShopOrder' : 'currentDeliveryOrder';
var saved_order_name = shop_mode == 'shop' ? 'saved_shop_order' : 'saved_delivery_order';
var order = getStoredOrder() || { products: [], total: 0.00, type: shop_mode};
1543 1544 1545
var content1 = $('#tab-content1');
var content4 = $('#tab-content4');
var category_elts = getStoredCatElts() || {};
Administrator committed
1546 1547

/** init first render **/
1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558
adjustCartHeight();
putAlimCategData();
putNonAlimCategData();
initPromotionTabs();
init_shop();

//cart_validation_form
valid_cart.click(function() {
    if (is_time_to('valid_cart', 1000)) { // prevent double click or browser hic up bug
        openModal($('#cart_validation_form').html(), validCart, 'Valider la commande');
        $('.mconfirm .pickup_date span').text(french_date_and_time(order.best_date));
Administrator committed
1559
    }
1560
});
Administrator committed
1561 1562


1563 1564 1565 1566 1567 1568 1569 1570 1571
$('#get_my_bought_products').click(loadAllAvailableBoughtProducts);
$(document).on('change', '[name^="bday"]', filterHourOptions);
$(document).on('change', '[name="bhour"]', adaptTimeGivenForValidationMsg);
$(document).on('click', '#alim_categ > div, #non_alim_categ > div', getCategChildren);
$(document).on('click', '#alim_categ ul li span, #non_alim_categ ul li span', getCategProducts);
$(document).on('click', '.product button', addProductToCart);
$(document).on('click', '.forbidden-slots .fs-close', closeForbiddenList);
$(document).on('click', 'td.date .fa-edit', changeBestDate);
$(document).on('click', 'td.actions .fa-trash', destroySentCart);
Administrator committed
1572

1573 1574 1575 1576
$(document).on(
    'click', '.new-order',
    function() {
        visit_mode = false;
Administrator committed
1577
        if (typeof order._id == "undefined") {
1578 1579
            reset_home();
            clearCart();
Administrator committed
1580
            resetProgressBar();
1581 1582
            main_content.hide();
            launch_init_form();
Administrator committed
1583
        } else {
1584 1585
            djLogError({msg: "Une commande est déjà en cours", order:order});
            pass2step2();
Administrator committed
1586
        }
1587 1588
    }
);
Administrator committed
1589

1590 1591
$(document).on('click', '.my-orders', displaySentOrders);
$(document).on('click', '.visit', justVisit);
Administrator committed
1592

1593 1594 1595 1596 1597
$('#deconnect').click(function() {
    $.ajax("/website/deconnect").done(function() {
        window.location.reload();
    });
});
Administrator committed
1598

1599 1600 1601 1602
$('#go_to_top').click(function() {
    window.scrollTo(0, 0);
});
$('.back-to-home').click(reset_home);
Administrator committed
1603 1604 1605

/* Listener on product sorting selector */
$("select.products_sort").change(function () {
1606 1607 1608 1609 1610
    var clicked = $(this);
    var tab = clicked.closest('li.tab');
    var content = tab.find('.content');
    var grid = content.find('section');
    var products = grid.find('div.product');
Administrator committed
1611

1612 1613
    var sort_type = clicked.children("option:selected").val();
    var sorted_products = sort_product_divs(products, sort_type);
Administrator committed
1614

1615 1616
    grid.html(sorted_products);
});
Administrator committed
1617

1618 1619 1620
window.onresize = adjustSizes;
$(document).on("closemodal", function() {
    if (current_action == "init_form") reset_home();
Administrator committed
1621

1622
});