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
var shelfs_table = null;
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"
},
{data:"name", title:"Nom"},
{data:"description", title:"Description", orderable: false},
{
data:"date_last_inventory",
title:"Date dernier inventaire",
render: function (data, type, full, meta) {
// 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"},
{
data:"shelf_value",
title:"Valeur théorique du rayon",
render: function (data, type, full, meta) {
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"
},
{
data:"last_inv_delta_percentage",
title:"Delta (dernier inv.)",
width: "5%",
className:"dt-body-center",
render: function (data, type, full, meta) {
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, full, meta) {
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, type, full, meta) {
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>";
}
}
],
dom: 'rtip',
order: [
[
1,
"asc"
]
],
iDisplayLength: 25,
language: {url : '/static/js/datatables/french.json'}
});
}
function get_shelfs_extra_data() {
try {
$.ajaxSetup({ headers: { "X-CSRFToken": getCookie('csrftoken') } });
$.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 (rowIdx, tableLoop, rowLoop) {
var d = this.data();
d.shelf_value = -2;
this.invalidate(); // invalidate the data DataTables has cached for this row
});
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 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() {
shelfs_table = init_datatable();
get_shelfs_extra_data();
$(document).on('click', 'button.do_shelf_inventory', go_to_shelf_inventory);
$('#shelfs').on('click', 'tbody td', go_to_shelf_view);
// Search input
$('#search_input').on('keyup', function () {
shelfs_table
.search(jQuery.fn.DataTable.ext.type.search.string(this.value))
.draw();
});
});