shelf_view.js 5.11 KB
Newer Older
Administrator committed
1 2 3 4 5 6 7 8
/*
Cette page affiche les détails des produits d'un rayon

Informations affichées :
- Quantité en stock théorique
*/

var parent_location = '/shelfs',
9 10
    shelf = null,
    table_products = null,
11
    search_chars = [];
Administrator committed
12 13 14 15 16

/* UTILS */

// Round a decimal value
function round(value, decimals) {
17
    return Number(Math.round(value+'e'+decimals)+'e-'+decimals);
Administrator committed
18 19 20
}

function back() {
21
    document.location.href = parent_location;
Administrator committed
22 23 24 25
}

// Set search field with product name when barcode is read
function select_product_from_bc(barcode) {
26 27 28 29 30 31
    $.each(shelf_products, function(i, e) {
        if (e.barcode == barcode) {
            $('#search_input').val(e.name);
            table_products.search(jQuery.fn.DataTable.ext.type.search.string(e.name)).draw();
        }
    });
Administrator committed
32 33 34 35 36 37
}

/* LIST HANDLING */

// Init Data & listeners
function initList() {
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
    // Init table for items to process
    table_products = $('#table_shelf_products').DataTable({
        data: shelf_products,
        columns: [
            {data:"id", title: "id", visible: false},
            {data:"name", title:"Produit"},
            {
                data:"qty_available",
                title:"Stock théorique",
                width: "10%",
                render: function (data, type, full) {
                    return round(data, 2) + '  ' + full.uom_id[1];
                }
            },
            {
                data:"last_inv_delta",
                title:"Delta (dernier inv.)",
                width: "10%",
                className:"dt-body-center",
57
                render: function (data, type) {
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
                    if (type == "sort" || type == 'type')
                        return data;

                    if (data == -99999999) {
                        return '/';
                    } else {
                        return data;
                    }
                }
            },
            {
                data:"last_inv_losses",
                title:"Pertes (dernier inv.)",
                width: "10%",
                className:"dt-body-center",
73
                render: function (data, type) {
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
                    if (type == "sort" || type == 'type')
                        return data;

                    if (data == -99999999) {
                        return '/';
                    } else {
                        return data + ' €';
                    }
                }
            }
        ],
        rowId : "id",
        order: [
            [
                0,
                "asc"
            ]
        ],
        paging: false,
        dom: 'lrtip', // Remove the search input from that table
        language: {url : '/static/js/datatables/french.json'}
    });


    /* Listeners on table & search input */

    // Search input for table
    $('#search_input').on('keyup', function () {
        table_products
            .search(jQuery.fn.DataTable.ext.type.search.string(this.value)) // search without accents (see DataTable plugin)
            .draw();
    });
Administrator committed
106 107 108 109 110 111 112
}


/* INIT */

// Get shelf data from server if not in local storage
function get_shelf_data() {
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
    $.ajaxSetup({ headers: { "X-CSRFToken": getCookie('csrftoken') } });
    $.ajax({
        type: 'GET',
        url: '../' + shelf.id,
        dataType:"json",
        traditional: true,
        contentType: "application/json; charset=utf-8",
        success: function(data) {
            shelf = data.res;
            init();
        },
        error: function(data) {
            if (typeof data.responseJSON != 'undefined') {
                console.log(data.responseJSON);
            }
            alert('Les données n\'ont pas pu être récupérées, réessayez plus tard.');
        }
    });
Administrator committed
131 132 133 134
}

// Init page : to be launched when shelf data is here
function init() {
135 136
    // Set shelf name in DOM
    $('#shelf_name').text(shelf.name);
Administrator committed
137

138 139
    // Products passed at page loading as 'shelf_products'
    initList();
Administrator committed
140

141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
    // Barcode reader: listen for 13 digits read in a very short time
    $('#search_input').keypress(function(e) {
        if (e.which >= 48 && e.which <= 57) {
            search_chars.push(String.fromCharCode(e.which));
        }
        if (search_chars.length >= 13) {
            var barcode = search_chars.join("");

            if (!isNaN(barcode)) {
                search_chars = [];
                setTimeout(function() {
                    select_product_from_bc(barcode);
                }, 300);
            }
        }
    });
Administrator committed
157 158 159 160
}


$(document).ready(function() {
161 162
    // Get Route parameter
    var pathArray = window.location.pathname.split('/');
Administrator committed
163

164
    shelf = {id: pathArray[pathArray.length-1]};
Administrator committed
165

166 167 168 169 170 171 172 173 174 175 176
    // Get shelf data from local storage
    if (Modernizr.localstorage) {
        var stored_shelf = JSON.parse(localStorage.getItem('shelf_' + shelf.id));

        if (stored_shelf != null) {
            shelf = stored_shelf;
            init();
        } else {
            // Get shelf info if not coming from shelves list
            get_shelf_data();
        }
Administrator committed
177
    } else {
178
        get_shelf_data();
Administrator committed
179
    }
180 181 182 183 184 185 186

    // listeners
    // Cancel search
    $('.cancel_search').on('click', function () {
        $('#search_input').val('');
        table_products.search('').draw();
    });
Administrator committed
187
});