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
/*
Cette page affiche les détails des produits d'un rayon
Informations affichées :
- Quantité en stock théorique
*/
var parent_location = '/shelfs',
shelf = null,
table_products = null,
search_chars = [];
/* UTILS */
// Round a decimal value
function round(value, decimals) {
return Number(Math.round(value+'e'+decimals)+'e-'+decimals);
}
function back() {
document.location.href = parent_location;
}
// Set search field with product name when barcode is read
function select_product_from_bc(barcode) {
$.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();
}
});
}
/* LIST HANDLING */
// Init Data & listeners
function initList() {
// 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",
render: function (data, type) {
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",
render: function (data, type) {
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();
});
}
/* INIT */
// Get shelf data from server if not in local storage
function get_shelf_data() {
$.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.');
}
});
}
// Init page : to be launched when shelf data is here
function init() {
// Set shelf name in DOM
$('#shelf_name').text(shelf.name);
// Products passed at page loading as 'shelf_products'
initList();
// 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);
}
}
});
}
$(document).ready(function() {
// Get Route parameter
var pathArray = window.location.pathname.split('/');
shelf = {id: pathArray[pathArray.length-1]};
// 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();
}
} else {
get_shelf_data();
}
// listeners
// Cancel search
$('.cancel_search').on('click', function () {
$('#search_input').val('');
table_products.search('').draw();
});
});