shelfs_index.js
11 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
var shelfs_table = null,
default_inventory_start_datetime = "0001-01-01 00:00:00";
function init_datatable() {
return $('#shelfs').DataTable({
data: shelfs, // data passed at page loading
rowId: 'id',
columns:[
{data: "id", title:"id", "visible": false},
{
data:"sort_order",
title:"Numéro",
width: "10%",
className:"dt-body-center clickable"
},
{data:"name", title:"Nom", className:"clickable"},
// {data:"description", title:"Description", orderable: false},
{
data:"ongoing_inv_start_datetime",
title:"Début inventaire en cours",
className:"dt-body-center clickable",
render: function (data, type) {
// Sort on data, not rendering
if (type == "sort" || type == 'type')
return data;
if (data == '0001-01-01 00:00:00')
return "";
else {
var date = new Date(data);
// Get local timezone offset in minutes
const offset = date.getTimezoneOffset();
// Add offset to saved time.
// offset is negative if the local time zone is ahead of UTC.
date.setTime(date.getTime() - offset * 60 * 1000);
return `${date.toLocaleDateString('fr-FR')} ${date.toLocaleTimeString('fr-FR')}`;
}
}
},
{
data:"date_last_product_added",
title:"Dernier ajout produit",
className:"dt-body-center clickable",
render: function (data, type) {
// Sort on data, not rendering
if (type == "sort" || type == 'type')
return data;
if (data == '0001-01-01')
return "";
else {
var date = new Date(data);
return date.toLocaleDateString('fr-FR');
}
}
},
{
data:"date_last_inventory",
title:"Dernier inventaire",
className:"dt-body-center clickable",
render: function (data, type) {
// Sort on data, not rendering
if (type == "sort" || type == 'type')
return data;
if (data == '0001-01-01')
return "Ce rayon n'a jamais été inventorié !";
else {
var date = new Date(data);
return date.toLocaleDateString('fr-FR');
}
}
},
{data:"p_nb", title:"Nombre de réfs", width: "5%", className:"dt-body-center clickable"},
{
data:"shelf_value",
title:"Valeur théorique du rayon",
visible: false,
name: "shelf_value",
render: function (data, type) {
if (type == "sort" || type == 'type')
return data;
if (data == -1) { // Code: server send empty field -> loading
return '<i class="fas fa-spinner fa-spin"></i>';
} else if (data == -2) { // Code: error getting data from server
return '/';
} else {
return data + ' €';
}
},
width: "5%",
className:"dt-body-center clickable"
},
/* NOT IN USE */
// {
// data:"last_inv_delta_percentage",
// title:"Delta (dernier inv.)",
// width: "5%",
// 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_percentage",
// title:"Pertes (dernier inv.)",
// width: "5%",
// className:"dt-body-center",
// render: function (data, type) {
// if (type == "sort" || type == 'type')
// return data;
// if (data == -99999999) {
// return '/';
// } else {
// return data + ' %';
// }
// }
// },
{
data:"inventory_status",
title:"Inventaire à faire",
className:"dt-body-center",
width: "15%",
render: function (data) {
if (data == '')
return "<button class='btn--primary do_shelf_inventory'>Inventaire en rayon</button>";
else
return "<button class='btn--success do_shelf_inventory'>Inventaire en réserve</button>";
}
},
{
data:"id",
title:"Supprimer inventaire en cours",
className:"dt-body-center",
width: "5%",
render: function () {
return `<i class="fas fa-trash delete_ongoing_inv_icon"></i>`;
}
}
],
dom: 'rtip',
order: [
[
1,
"asc"
]
],
iDisplayLength: 25,
language: {url : '/static/js/datatables/french.json'}
});
}
function get_shelfs_extra_data() {
try {
$.ajax({
type: 'GET',
url: '/shelfs/get_shelves_extra_data',
dataType:"json",
traditional: true,
contentType: "application/json; charset=utf-8",
success: function(data) {
for (item of data.res) {
var row_data = shelfs_table.row('#'+item.id).data();
row_data.shelf_value = item.shelf_value;
shelfs_table
.row('#'+item.id)
.data(row_data)
.draw();
}
},
error: function(data) {
if (typeof data.responseJSON != 'undefined') {
console.log(data.responseJSON);
}
set_null_to_extra_data();
}
});
} catch (e) {
console.log(e);
set_null_to_extra_data();
}
}
function set_null_to_extra_data() {
shelfs_table.rows().every(function () {
var d = this.data();
d.shelf_value = -2;
this.invalidate(); // invalidate the data DataTables has cached for this row
return 1;
});
shelfs_table.draw();
}
var getRowData = function(clicked) {
var row = shelfs_table.row(clicked.parents('tr'));
return row.data();
};
function go_to_shelf_inventory() {
openModal();
var clicked = $(this);
var row_data = getRowData(clicked);
// Use local storage to pass data to next page
if (Modernizr.localstorage) {
var stored_shelf = JSON.parse(localStorage.getItem('shelf_' + row_data.id));
// Set local storage if key doesn't exist
if (stored_shelf == null) {
localStorage.setItem("shelf_" + row_data.id, JSON.stringify(row_data));
}
}
document.location.href = "shelf_inventory/" + row_data.id;
}
function pre_delete_ongoing_inventory() {
let clicked = $(this);
let row_data = getRowData(clicked);
let template = $("#modal_delete_ongoing_inv");
template.find(".shelf_name").text(row_data.name);
openModal(
template.html(),
() => {
delete_ongoing_inventory(row_data);
},
"Valider",
false
);
}
function delete_ongoing_inventory(row_data) {
openModal();
$.ajax({
type: 'POST',
url: `/shelfs/${row_data.id}/delete_ongoing_inv_data`,
dataType:"json",
traditional: true,
contentType: "application/json; charset=utf-8",
success: function() {
row_data.inventory_status = '';
row_data.ongoing_inv_start_datetime = default_inventory_start_datetime;
shelfs_table
.row('#'+row_data.id)
.data(row_data)
.draw();
// Delete shelf data from localstorage
if (Modernizr.localstorage) {
localStorage.removeItem('shelf_' + row_data.id);
}
closeModal();
},
error: function(data) {
if (typeof data.responseJSON != 'undefined') {
console.log(data.responseJSON);
}
closeModal();
alert("Une erreur est survenue...");
}
});
}
function go_to_shelf_view() {
openModal();
var clicked = $(this);
var row_data = getRowData(clicked);
// Use local storage to pass data to next page
if (Modernizr.localstorage) {
var stored_shelf = JSON.parse(localStorage.getItem('shelf_' + row_data.id));
// Set local storage if key doesn't exist
if (stored_shelf == null) {
localStorage.setItem("shelf_" + row_data.id, JSON.stringify(row_data));
}
}
document.location.href = "shelf_view/" + row_data.id;
}
$(document).ready(function() {
$.ajaxSetup({ headers: { "X-CSRFToken": getCookie('csrftoken') } });
// Check if local data is outdated
for (let shelf of shelfs) {
let stored_shelf = JSON.parse(localStorage.getItem('shelf_' + shelf.id));
if (
stored_shelf !== null
&& stored_shelf.ongoing_inv_start_datetime !== undefined
&& shelf.ongoing_inv_start_datetime !== stored_shelf.ongoing_inv_start_datetime
) {
localStorage.removeItem('shelf_' + shelf.id);
}
}
shelfs_table = init_datatable();
$(document).on('click', 'button.do_shelf_inventory', go_to_shelf_inventory);
$(document).on('click', 'tbody td .delete_ongoing_inv_icon', pre_delete_ongoing_inventory);
$('#shelfs').on('click', 'tbody td.clickable', go_to_shelf_view);
// Search input
$('#search_input').on('keyup', function () {
shelfs_table
.search(jQuery.fn.DataTable.ext.type.search.string(this.value))
.draw();
});
$(document).on('click', '#show_and_compute_shelf_value', function () {
shelfs_table.column('shelf_value:name').visible(true);
get_shelfs_extra_data();
});
});