shelfs_admin.js 20.6 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616
var main_content = $('#main-content'),
    main_table_wrap = $('#main-table-wrap').html(),
    shelfs_table = null,
    create_form = $('#create_form'),
    shelf_id_input = create_form.find('input[name="shelf_id"]'),
    shelf_sort_order = create_form.find('input[name="sort_order"]'),
    shelf_name = create_form.find('input[name="name"]'),
    description = create_form.find('textarea[name="description"]'),
    eye = '<i class="fas fa-eye"></i>',
    delete_icon = '<i class="fas fa-trash p_action_icon"></i>',
    print_icon = '<i class="fas fa-print p_action_icon"></i>',
    add_icon = '<i class="fas fa-plus-circle"></i>',
    edit_icon = '<i class="fas fa-edit"></i>',
    download_icon = '<i class="fas fa-download"></i>',
    destroy_shelf_msg = $('#destroy-shelf-msg'),
    adding_pdts_tpl = $('#adding-products').clone()
        .removeAttr('id'),
    active_phase = 'main',
    add_to_shelf_product_ids = [],
    barcodes = null;


var deleteShelf = function() {
    var clicked = $(this);

    if (is_time_to('delete_shelf', 15000)) { // prevent double click or browser hic up bug
        var data = rowGetData(clicked);
        var msg = destroy_shelf_msg.clone();

        msg.find('span.shelf').text(data.name);
        openModal(
            msg.html(),
            function() {
                // Confirm button callback
                post_form(
                    '/shelfs/admin/delete',
                    {id: data.id},
                    function(err, result) {
                        if (!err) {
                            if (typeof result.res !== "undefined" && result.res == true) {
                                shelfs_table.row(clicked.parents('tr')).remove()
                                    .draw();

                                alert("Enregistrement détruit");
                            } else {
                                console.log(result);
                            }

                        } else {
                            console.log(err);
                        }
                    }
                );
            },
            'Détruire'
        );
    }
};

var create = function() {
    if (is_time_to('create_shelf', 5000)) { // prevent double click or browser hic up bug
        var shelf_name = modal.find('input[name="name"]'),
            sort_order = modal.find('input[name="sort_order"]'),
            description = modal.find('textarea[name="description"]');

        if (shelf_name.val().length == 0 && sort_order.val().length > 0)
            shelf_name.val(sort_order.val());
        if (shelf_name.val().length > 0 && sort_order.val().length > 0) {
            $('.mconfirm .btns').hide();
            box_load.show();
            post_form(
                '/shelfs/admin/create',
                {name: shelf_name.val(), description: description.val(), sort_order: sort_order.val()},
                function(err, rData) {
                    if (typeof(rData.res.id) !== "undefined") {
                        rData.res.p_nb = 0;
                        if (shelfs_table)
                            shelfs_table.row.add(rData.res).draw();
                        else init_and_fill_selfs_list(); // first shelf
                        closeModal();
                    } else {
                        msg = rData.res.error;
                        if (msg.indexOf("Num must be unique !") > -1) {
                            msg = "Ce numéro de rayon est déjà utilisé !";
                        }
                        alert(msg);
                    }
                    $('.mconfirm .btns').show();
                    box_load.hide();
                }

            );
        } else {
            alert("Champs obligatoires non remplis");
        }
    }
};

var open_create_form = function() {
    shelf_sort_order.attr('value', '');
    shelf_name.attr('value', '');
    description.text('');
    box_load.hide();

    openModal(create_form.html(), create, 'Confirmer', false);
};


var update_shelf = function() {
    if (is_time_to('update_shelf', 5000)) { // prevent double click or browser hic up bug
        var shelf_id = modal.find('input[name="shelf_id"]'),
            shelf_name = modal.find('input[name="name"]'),
            sort_order = modal.find('input[name="sort_order"]'),
            description = modal.find('textarea[name="description"]');

        if (shelf_name.val().length == 0 && sort_order.val().length > 0)
            shelf_name.val(sort_order.val());
        if (shelf_name.val().length > 0 && sort_order.val().length > 0) {
            $('.mconfirm .btns').hide();
            box_load.show();

            post_form(
                '/shelfs/admin/update',
                {
                    id: shelf_id.val(),
                    name: shelf_name.val(),
                    description: description.val(),
                    sort_order: sort_order.val()
                },
                function(err, rData) {
                    if (typeof(rData.res.id) !== "undefined") {
                        shelfs_table.rows().every(function () {
                            var data = this.data();

                            if (data.id == rData.res.id) {
                                rData.res.p_nb = data.p_nb;
                                this.data(rData.res).draw();
                            }

                            return 1;
                        });
                        closeModal();
                    } else alert(rData.res.error);

                    $('.mconfirm .btns').show();
                    box_load.hide();
                }
            );
        } else {
            alert("Champs obligatoires non remplis");
        }
    }
};

// Set update form in modal
var open_update_form = function() {
    var clicked = $(this);

    var shelf_data = rowGetData(clicked);

    shelf_id_input.attr('value', shelf_data.id);
    shelf_sort_order.attr('value', shelf_data.sort_order);
    shelf_name.attr('value', shelf_data.name);
    description.text(shelf_data.description);
    box_load.hide();

    openModal(create_form.html(), update_shelf, 'Mettre à jour', false);
};

var downloadInventoryReport = function() {
    if (is_time_to('download_inv_report', 3000)) { // prevent double click or browser hic up bug
        var clicked = $(this);

        var shelf_data = rowGetData(clicked);

        if (shelf_data['last_inventory_id'] != 0) {
            $.ajax({
                type: "GET",
                url: "/shelfs/"+ shelf_data['id'] +"/last_inventory_report",
                xhrFields: {
                    responseType: 'blob'
                },
                success: function (data, textStatus, request) {
                    var a = document.createElement('a');
                    var url = window.URL.createObjectURL(data);

                    a.href = url;
                    // Assuming headers are correctly set :
                    // -> Content-Disposition : attachment; filename="xxx"
                    a.download = request.getResponseHeader('Content-Disposition').split("\"")[1];
                    document.body.append(a);
                    a.click();
                    a.remove();
                    window.URL.revokeObjectURL(url);
                },
                error: function(data) {
                    if (typeof data.responseJSON != 'undefined' && typeof data.responseJSON.error != 'undefined') {
                        console.log(data.responseJSON.error);
                    }
                    alert('Le fichier n\'a pas pu être récupéré, réessayez plus tard ou bien allez le chercher dans l\'inventaire Odoo.');
                }
            });
        }
    }
};

// TODO put datatable common methods such as following in a file useable for all modules
var rowGetData = function(clicked) {
    var row = shelfs_table.row(clicked.parents('tr'));


    return row.data();
};

function coop_init_datatable(params, data, domsel, cols) {
    var buttons = [];
    var columns = [];


    $.each(cols, function(i, e) {
        columns.push(e);
    });

    columns.push({
        data: null,
        defaultContent: add_icon,
        title: "Ajout produits",
        className: 'products',
        orderable: false,
        targets:   0
    });

    columns.push({
        data: null,
        defaultContent: edit_icon,
        title: "Modifier le rayon",
        className: 'action',
        orderable: false,
        targets:   0
    });

    columns.push({
        data: null,
        defaultContent: delete_icon,
        orderable: false,
        className: 'action',
        targets:   0
    });

    var settings = {
        dom: '<lf<t>ip><"clear"><B>',
        lengthMenu : [
            [
                50,
                100,
                150,
                200,
                -1
            ],
            [
                50,
                100,
                150,
                200,
                'Tout'
            ]
        ],
        pageLength : 50,
        buttons: buttons,

        columns: columns,
        //select: select ,
        rowId : "id",
        data : data,
        language: {url : '/static/js/datatables/french.json'},
        initComplete: function() {
            /*
                                if (! coop_is_connected())
                                    $('#main_content input[type="search"]').attr('disabled','disabled')
                                */
        }
    };

    if (params) {
        if (params.page) {
            settings.displayStart = params.page.start;
        }
        if (params.ordering) {
            settings.order = params.ordering;
        }
    }

    return main_content.find('table'+domsel).DataTable(settings);
}
var init_and_fill_selfs_list = function() {
    try {
        $.ajax({
            url :'/shelfs/all',
            dataType: 'json'
        })
            .done(function(rData) {
                if (rData.res && rData.res.length > 0) {
                    var cols = [
                        {data: 'sort_order', title: "Numéro"},
                        {data: 'name', title: "Nom"},
                        {data: 'description', title: "Description"},
                        {data: 'p_nb', title: "Nb pdts", className: "p_nb"},
                        {
                            data:"last_inventory_id",
                            title:"Rapport dernier inventaire",
                            className: "action",
                            render: function (data) {
                                if (typeof data != "undefined" && data != 0) {
                                    return download_icon;
                                } else {
                                    return "";
                                }
                            }
                        }

                    ];

                    if (shelfs_table)
                        shelfs_table.destroy();
                    shelfs_table = coop_init_datatable(null, rData.res, '.shelfs', cols);
                }
                //console.log(rData.res)

            });
    } catch (e) {
        console.log(e);
    }
};
var deleteBarcodeFromList = function () {
    let clicked = $(this);
    let new_pids_list = [];
    let tr_to_remove = clicked.closest('tr');
    let pid_to_remove = tr_to_remove.data('id');

    $.each(add_to_shelf_product_ids, function(idx, pid) {
        if (pid != pid_to_remove) new_pids_list.push(pid);
    });
    add_to_shelf_product_ids = new_pids_list;
    tr_to_remove.remove();
};
var is_product_in_shelf_adding_queue_list = function(testing_pid) {
    let found = false;

    $.each(add_to_shelf_product_ids, function(idx, pid) {
        if (pid == testing_pid) found = true;
    });

    return found;
};

var printProduct = function () {
    let clicked = $(this);
    let tr_to_print = clicked.closest('tr');
    let barcode = tr_to_print.data('bc');

    openModal();
    try {
        $.ajax({
            url: '/products/get_product_data',
            data: {'barcode': barcode}
        })
            .done(function(res) {
                var product = res.product;
                var product_tmpl_id = product.product_tmpl_id[0];

                $.ajax({
                    url: '/products/label_print/' + product_tmpl_id
                })
                    .done(function(res_print) {
                        closeModal();
                        if ("error" in res_print.res) {
                            console.log(res_print.res);
                            alert('Une erreur est survenue...');
                        } else {
                            alert('Impression lancée');
                        }
                    });
            });
    } catch (e) {
        closeModal();
        alert('Une erreur est survenue...');
    }

};

var addProductToList = async function(barcode) {
    if (barcodes == null) barcodes = await init_barcodes(); // May appens (after inactivity?)
    //Get Odoo corresponding barcode
    //(May be different due to weight encoded barcode)
    //It could also be a wrong reading one

    odoo_product = barcodes.get_corresponding_odoo_product(barcode);
    if (odoo_product === null) {
        alert(barcode + " : ce code-barre est inconnu, merci d'apporter le produit à un salarié.");
    } else {
        if (is_product_in_shelf_adding_queue_list(odoo_product.data[barcodes.keys.id])) {
            alert("Produit déjà présent dans la liste.");
        } else {
            add_to_shelf_product_ids.push(odoo_product.data[4]);
            var pdt_line = $('<tr>').attr('data-id', odoo_product.data[barcodes.keys.id])
                .attr('data-bc', odoo_product.barcode)
                .addClass('obc');

            $('<td>').text(barcode)
                .appendTo(pdt_line);
            $('<td>').text(odoo_product.barcode)
                .appendTo(pdt_line);
            $('<td>').text(odoo_product.data[barcodes.keys.name])
                .appendTo(pdt_line);
            $('<td>').html(odoo_product.data[barcodes.keys.list_price] + " €")
                .appendTo(pdt_line);
            $('<td>').html(delete_icon + " " + print_icon)
                .appendTo(pdt_line);
            adding_pdts_tpl.find('#added_products tbody').append(pdt_line);
            main_content.find('.add-products').css('display', 'block');
        }
    }
};

var addProducts = async function() {
    var clicked = $(this);
    var data = rowGetData(clicked);

    if (barcodes == null) barcodes = await init_barcodes();
    add_to_shelf_product_ids = [];
    adding_pdts_tpl.find('.shelf').text(data.name + ' (num = ' + data.sort_order+')')
        .attr('data-shelfid', data.id);
    adding_pdts_tpl.find('#added_products tbody').empty();

    main_content.html(adding_pdts_tpl);
    active_phase = "adding_products";
    main_content.find('.add-products').css('display', 'none');
    if (admin_ids.find(id => id == getCookie("uid"))) $('.add-search').show();

};

var recordProductsAddedShelf = function() {
    var to_add = adding_pdts_tpl.find('tr.obc');

    if (to_add.length > 0) {
        var barcodes = [];
        var id = main_content.find('.shelf').data('shelfid');

        to_add.each(function(i, e) {
            barcodes.push($(e).data('bc'));
        });

        if (is_time_to('add_pdts_to_shelf', 5000)) { // prevent double click or browser hic up bug
            openModal(); // loading on

            post_form(
                '/shelfs/admin/add_products',
                {bc: JSON.stringify(barcodes), shelf_id: id},
                function(err, rData) {
                    let msg = 'Echec';

                    if (typeof rData.res.added != "undefined") {
                        msg = "Ajout des produits réussi.";

                        if (typeof rData.res.missing != "undefined") {
                            msg += "\nSauf pour :";
                            rData.res.missing.forEach(function(bc) {
                                msg += "\n" + bc;
                            });
                        }
                        closeModal();

                        alert(msg);
                        backToMain();
                    } else {
                        if (typeof rData.res.error != "undefined")
                            msg = rData.res.error;
                        else if (typeof rData.res.msg != "undefined")
                            msg = rData.res.msg;
                        alert(msg);
                        main_content.find('.add-products').show();
                        closeModal();
                    }

                }
            );

        }
    }
};

var showProductsList = function() {
    var clicked = $(this);
    var data = rowGetData(clicked);

    if (is_time_to('add_pdts_to_shelf', 5000)) { // prevent double click or browser hic up bug

        try {
            $.ajax({
                url :'/shelfs/' + data.id + '/products',
                dataType: 'json'
            })
                .done(function(rData) {
                    if (typeof rData.res.data != "undefined" && rData.res.data.length > 0) {
                        var table = $('<table>').attr('border', '1');

                        $.each(rData.res.data, function(i, pdt) {
                            var tr = $('<tr>');
                            var td1 = $('<td>').css('width', '100px')
                                .text(pdt.barcode);
                            var td2 = $('<td>').css({'text-align':'left', 'padding-left':'10px'})
                                .text(pdt.name);

                            tr.append(td1);
                            tr.append(td2);
                            table.append(tr);
                        });
                        displayMsg(table.html());
                    }

                });
        } catch (e) {
            console.log(e);
        }

    }
};

var addSearchDisplay = function(action) {
    var isw = $('.input-search-wrapper');
    var open = $('.add-search .fa-eye');
    var close = $('.add-search .fa-eye-slash');

    if (action == 'show') {
        isw.show();
        open.hide();
        close.show();
    } else {
        isw.hide();
        open.show();
        close.hide();
    }
};
var processAddSearchInput = function() {
    //Basic process for now : append text as barcode to list
    addProductToList($('.input-search-wrapper input[name="kw"]').val());
};

var backToMain = function () {
    active_phase = 'main';
    main_content.html(main_table_wrap);
    init_and_fill_selfs_list();
};

$(document).ready(function() {
    if (coop_is_connected()) {
        $('#content_wrapper').show();
        $('#need_connect').hide();
        main_content.html(main_table_wrap);
        init_and_fill_selfs_list();
        /* Make search accent insensitive */

        $(document).on('keyup', '#main_content input[type="search"]', function() {
            shelfs_table
                .search(jQuery.fn.DataTable.ext.type.search.string(this.value))
                .draw();
        });

        $(document).on('click', 'button.create', open_create_form);
        $(document).on('click', '.shelfs .fa-edit', open_update_form);
        $(document).on('click', '.shelfs .fa-trash', deleteShelf);
        $(document).on('click', '.shelfs .fa-download', downloadInventoryReport);
        $(document).on('click', '.obc .fa-trash', deleteBarcodeFromList);
        $(document).on('click', '.obc .fa-print', printProduct);
        $(document).on('click', 'td.products .fa-plus-circle', addProducts);
        $(document).on('click', '#main-content .add-products', recordProductsAddedShelf);
        $(document).on('click', 'td.p_nb', showProductsList);
        try {
            if (admin_ids.find(id => id == getCookie("uid"))) {
                $(document).on('click', '.add-search .fa-eye', function() {
                    addSearchDisplay('show');
                });
                $(document).on('click', '.add-search .fa-eye-slash', function() {
                    addSearchDisplay('hide');
                });
                $(document).on('click', '.input-search-wrapper button', processAddSearchInput);
            }
        } catch (e) {
            console.log(e);
        }

        $(document).pos();
        $(document).on('scan.pos.barcode', function(event) {
            //access `event.code` - barcode data
            var barcode = event.code;

            if (barcode.length >=13) {
                barcode = barcode.substring(barcode.length-13);
                //console.log(new Date().getTime() + ' ' + barcode)
            } else if (barcode.length == 12 && barcode.indexOf('0') !== 0) {
            // User may use a scanner which remove leading 0
                barcode = '0' + barcode;
            } else if (barcode.length >= 8) {
                // For EAN8
                barcode = barcode.substring(barcode.length-8);
            }


            if (active_phase == "adding_products") {
                addProductToList(barcode);
            }

        });

    }
});