destocking.js 4.59 KB
Newer Older
Administrator committed
1 2 3 4 5 6 7 8 9
var active_table = null,
    loader = $('#main_content img.loading'),
    local_pdt_db = null,
    search_input = $('div.search input'),
    search_div = $('div.search'),
    search_result = $('#search_result'),
    bc_scanner = $('#barcode_scanner');

function getLocalSelected() {
10 11 12 13
    var local_in_process = localStorage.getItem("selected_products") || "[]";


    return JSON.parse(local_in_process);
Administrator committed
14 15 16
}

function setLocalSelected(lip) {
17
    localStorage.setItem("selected_products", JSON.stringify(lip));
Administrator committed
18 19 20
}

function getLocalPdtDB() {
21 22
    var local_pdt_db = JSON.parse(localStorage.getItem("products_db") || "{}");

Administrator committed
23
    if (typeof (local_pdt_db.list) == "undefined")
24 25 26
        local_pdt_db.list = [];

    return local_pdt_db;
Administrator committed
27 28 29
}

function setLocalPdtDB(lpdb) {
30
    localStorage.setItem("products_db", JSON.stringify(lpdb));
Administrator committed
31 32 33 34
}

function display_in_search_results(matching) {
    var ul = $('<ul>');
35 36 37 38 39 40 41 42

    $.each(matching, function(i, e) {
        var li = $('<li>').attr('data-index', e.index)
            .text(e.doc.barcode + ' ' + e.doc.display_name);

        ul.append(li);
    });
    search_result.html(ul);
Administrator committed
43 44 45 46
}

function search_in_pdt_list(kwords) {
    if (kwords.length > 3) {
47 48 49
        var matching = [];
        var is_bc = !isNaN(kwords);

Administrator committed
50
        if (!is_bc)
51 52
            kwords = jQuery.fn.DataTable.ext.type.search.string(kwords.toLowerCase());
        $.each(local_pdt_db.list, function(i, e) {
Administrator committed
53 54
            if (is_bc) {
                if (e.barcode && e.barcode.indexOf(kwords) === 0) {
55
                    matching.push({doc:e, index:i});
Administrator committed
56 57
                }

58 59 60
            } else {
                var p_name = jQuery.fn.DataTable.ext.type.search.string(e.display_name);

Administrator committed
61
                if (p_name.toLowerCase().indexOf(kwords) > -1)
62
                    matching.push({doc:e, index:i});
Administrator committed
63
            }
64
        });
Administrator committed
65
        if (matching.length > 0) {
66 67
            if (matching.length == 1 && is_bc == true) {
                add_product_to_table(matching[0].index);
Administrator committed
68
            } else {
69
                display_in_search_results(matching);
Administrator committed
70 71 72
            }
        }
    } else {
73
        search_result.html('');
Administrator committed
74
    }
75 76

}
Administrator committed
77 78

$(document).pos();
79 80 81 82 83 84 85 86 87 88 89 90 91
$(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);
        search_in_pdt_list(barcode);
    } else {
        console.log($(':focus').attr('type'));
        console.log(new Date().getTime() + ' -> '+barcode);
    }

});
Administrator committed
92 93

function destocking_record() {
94
    console.log('envoyer les informations à Django');
Administrator committed
95 96 97 98
}

function init_table_interface (data) {
    if (active_table)
99 100 101 102 103 104
        active_table.destroy();
    var cols = [
        {data: 'display_name', title: "Nom"},
        {data: 'qty', title: "Quantité", defaultContent: ""}

    ];
Administrator committed
105
    var action_btn = {
106 107 108 109 110 111 112 113 114 115 116
        text: 'Valider le déstockage',
        action : function(e, dt) {
            if (dt.rows().indexes().length > 0)
                openModal('Valider le déstockage', destocking_record, 'Enregistrer');
            else
                alert("Impossible, il n'y a aucun produit !");

        }
    };
    var params = {no_search: true};

Administrator committed
117 118 119 120
    active_table = coop_init_datatable(params, data, '.pdt_liste_to_transfert', cols, action_btn);
}
function add_product_to_table(p_index) {
    var local_selected = getLocalSelected();
121 122

    selected_p = local_pdt_db.list[p_index];
Administrator committed
123
    if (selected_p.uom_id[0] == 1)
124 125 126 127 128 129 130
        selected_p.qty = 1;
    console.log('p_index='+p_index);
    console.log(selected_p);
    local_selected.push(selected_p);
    setLocalSelected(local_selected);
    search_result.html('');
    init_table_interface(local_selected);
Administrator committed
131 132
}
var need_to_load_odoo_products = true;
133 134 135

local_pdt_db = getLocalPdtDB();
local_selected = getLocalSelected();
Administrator committed
136 137 138 139
if (local_pdt_db.list.length > 0) {
    if (new Date().getTime() - local_pdt_db.created_at < 7200000) {
        // if data have been retrieved less than 2h ago, no need to make ajax call
        need_to_load_odoo_products = false;
140 141
        loader.hide();
        search_div.show();
Administrator committed
142
    }
143 144


Administrator committed
145 146
}
if (need_to_load_odoo_products == true) {
147 148 149 150 151 152
    $.ajax('get_all_available_products').done(function(rData) {
        local_pdt_db.list = rData;
        local_pdt_db.created_at = new Date().getTime();
        setLocalPdtDB(local_pdt_db);
        loader.hide();
        search_div.show();
Administrator committed
153
    });
154 155 156 157 158
}

search_input.on('keyup focus', function() {
    search_in_pdt_list($(this).val());
});
Administrator committed
159

160 161
search_div.on('click', 'li', function() {
    var p_index = $(this).data('index');
Administrator committed
162

163 164
    add_product_to_table(p_index);
});
Administrator committed
165 166

if (local_selected.length > 0) {
167
    init_table_interface(local_selected);
Administrator committed
168
}