|
@@ -4,309 +4,462 @@
|
4
|
4
|
Organization : Centro de Desarrollo y Consultoria Computacional
|
5
|
5
|
Project : OPASO Material Registry
|
6
|
6
|
File : inventory.js
|
7
|
|
- Description : Fetch lab inventory */
|
8
|
|
-
|
9
|
|
-/* globals */
|
10
|
|
-var editing,form_is_active,inventory,new_inventory,text;
|
11
|
|
-
|
12
|
|
-/* get_inventory() - fetch lab inventory from server */
|
13
|
|
-function get_inventory(){
|
14
|
|
- var laboratory = get_arg("lab"); /* get lab id from url */
|
15
|
|
- $.post("/scripts/opaso.php",{query: 4,laboratory: laboratory},function(data){
|
16
|
|
- console.log(data);
|
17
|
|
- try{
|
18
|
|
- var response = JSON.parse(data); /* parse response as json */
|
19
|
|
- switch(response["status"]){
|
20
|
|
- case "success": /* on success, generate table body */
|
21
|
|
- /* update inventory header */
|
22
|
|
- $(".header").text(response["laboratory"]);
|
23
|
|
- /* extract inventory from response and generate table body */
|
24
|
|
- inventory = response["inventory"];
|
25
|
|
- var tbody = create_element("tbody","inventory-body");
|
26
|
|
- var row_index = 1;
|
27
|
|
- for(var row in inventory){
|
28
|
|
- /* generate option buttons */
|
29
|
|
- var edit = create_button("Edit","fas fa-pen","btn options","edit");
|
30
|
|
- var copy = create_button("Copy","fas fa-copy","btn options","copy");
|
31
|
|
- var remove = create_button("Remove","fas fa-minus-square","btn options","remove");
|
32
|
|
- /* append buttons to cell */
|
33
|
|
- var options = create_element("td","options-cell");
|
34
|
|
- options.append(edit,copy,remove);
|
35
|
|
- /* generate table row */
|
36
|
|
- var index = create_element("td","index",row_index);
|
37
|
|
- var tr = create_element("tr","inventory-row");
|
38
|
|
- tr.setAttribute("id",row);
|
39
|
|
- tr.append(options,index);
|
40
|
|
- /* generate inventory columns */
|
41
|
|
- for(var col in inventory[row]){
|
42
|
|
- var cell = create_element("td","inventory-cell",inventory[row][col]);
|
43
|
|
- cell.setAttribute("val",col); /* identify cell field */
|
44
|
|
- tr.append(cell);
|
45
|
|
- }
|
46
|
|
- tbody.append(tr);
|
47
|
|
- row_index += 1;
|
48
|
|
- }
|
49
|
|
- $(".table").append(tbody);
|
50
|
|
- break;
|
51
|
|
- case "error": /* on error, display message */
|
52
|
|
- display_error(response["error"]);
|
53
|
|
- break;
|
54
|
|
- default: /* undefined status */
|
55
|
|
- display_error("Undefined status.");
|
56
|
|
- break;
|
|
7
|
+ Description : Generate inventory table */
|
57
|
8
|
|
58
|
|
- }
|
59
|
|
- set_listeners(); /* listen for cell/button clicks */
|
60
|
|
- loading_screen(false); /* hide loading screen */
|
61
|
|
- }
|
62
|
|
- catch(error){
|
63
|
|
- console.log(error.message);
|
64
|
|
- display_error(error.message);
|
65
|
|
- }
|
66
|
|
- });
|
|
9
|
+/* save inventory data globally */
|
|
10
|
+var editing,inventory,form,text,timeout,i,next;
|
|
11
|
+var sort_key = "material";
|
|
12
|
+
|
|
13
|
+/* fetch inventory */
|
|
14
|
+$(document).ready(function(){
|
|
15
|
+ link_gen();
|
|
16
|
+ fetch_inventory();
|
|
17
|
+});
|
|
18
|
+
|
|
19
|
+/* display_form() - animate form */
|
|
20
|
+function display_form(){
|
|
21
|
+ $(".form-shader").addClass("fade-in");
|
|
22
|
+ $(".edit-form").addClass("slide-down");
|
67
|
23
|
}
|
68
|
|
-/* set_listeners() - set listeners for cell/button clicks */
|
|
24
|
+
|
|
25
|
+/* hide_form() - hide form */
|
|
26
|
+function hide_form(){
|
|
27
|
+ $(".edit-form").removeClass("slide-down");
|
|
28
|
+ setTimeout(function(){
|
|
29
|
+ $(".form-shader").removeClass("fade-in");
|
|
30
|
+ },250);
|
|
31
|
+}
|
|
32
|
+
|
|
33
|
+/* set_listeners() - listen for events */
|
69
|
34
|
function set_listeners(){
|
70
|
|
- /* set listener for option buttons */
|
71
|
|
- $(".options").click(function(){
|
|
35
|
+ /* option queries */
|
|
36
|
+ $(".action").click(function(){
|
72
|
37
|
/* extract args */
|
73
|
38
|
var option = $(this).attr("id");
|
74
|
|
- var row = $(this).parent().parent().attr("id"); /* row id */
|
|
39
|
+ i = $(this).parent().parent().attr("id");
|
|
40
|
+
|
|
41
|
+ /* handle by option */
|
75
|
42
|
switch(option){
|
76
|
|
- case "edit": /* edit inventory row (fallthrough) */
|
77
|
|
- case "copy": /* copy inventory row */
|
78
|
|
- $(".form-header").text(option + " entry");
|
79
|
|
- set_form(row); /* populate form */
|
80
|
|
- $(".submit").attr("id",option);
|
81
|
|
- $(".submit").attr("val",row);
|
82
|
|
- $(".close-form").attr("val",row);
|
|
43
|
+ case "edit": /* edit row, populate row */
|
|
44
|
+ set_form();
|
83
|
45
|
break;
|
84
|
|
- case "remove": /* delete row form inventory */
|
85
|
|
- /* extract arg */
|
86
|
|
- var laboratory = get_arg("lab");
|
87
|
|
- $.post("/scripts/opaso.php",{query: 8,laboratory: laboratory,eid: row},function(data){ /* send row to be deleted */
|
88
|
|
- // console.log(data);
|
89
|
|
- try{
|
90
|
|
- var response = JSON.parse(data); /* parse response as json */
|
91
|
|
- switch(response["status"]){
|
92
|
|
- case "success": /* on success, generate table body */
|
93
|
|
- break;
|
94
|
|
- case "error": /* on error, display message */
|
95
|
|
- display_error(response["error"]);
|
96
|
|
- break;
|
97
|
|
- default: /* undefined status */
|
98
|
|
- display_error("Undefined status.");
|
99
|
|
- break;
|
100
|
|
- }
|
101
|
|
- }
|
102
|
|
- catch(error){
|
103
|
|
- display_error(error.message);
|
104
|
|
- }
|
105
|
|
- });
|
|
46
|
+
|
|
47
|
+ case "remove": /* delete row, confirm */
|
|
48
|
+ set_confirm("Do you wish to delete entry from inventory?",option);
|
106
|
49
|
break;
|
107
|
|
- default: /* undefined option */
|
108
|
|
- display_error("Undefined option.");
|
|
50
|
+
|
|
51
|
+ default: /* undefined, display alert */
|
|
52
|
+ set_alert("error","Invalid option")
|
109
|
53
|
break;
|
110
|
54
|
}
|
111
|
55
|
});
|
|
56
|
+
|
|
57
|
+ /* submit query */
|
|
58
|
+ $("#yes").click(function(){
|
|
59
|
+ hide_confirm();
|
|
60
|
+ $.post("/scripts/opaso",{query: 8,lab_id: get_arg("lab_id"),data: JSON.stringify(inventory[i])},function(data){
|
|
61
|
+ // console.log(data);
|
|
62
|
+ /* extract response */
|
|
63
|
+ let response = JSON.parse(data);
|
|
64
|
+ let status = response["status"];
|
|
65
|
+ /* handle by status */
|
|
66
|
+ switch(status){
|
|
67
|
+ case "expired": /* on expired, redirect to index */
|
|
68
|
+ window.location.href = "/?error=session_expired";
|
|
69
|
+ break;
|
|
70
|
+
|
|
71
|
+ case "success": /* on success, generate table body */
|
|
72
|
+ $(`#${i}`).remove();
|
|
73
|
+ next -= 1;
|
|
74
|
+ $(".table-total").text(`Total: ${next}`);
|
|
75
|
+
|
|
76
|
+ default: /* on success/error, display message */
|
|
77
|
+ set_alert(status,response["message"]);
|
|
78
|
+ break;
|
|
79
|
+ }
|
|
80
|
+ });
|
|
81
|
+ });
|
|
82
|
+
|
|
83
|
+ /* cancel query */
|
|
84
|
+ $("#no").click(hide_confirm);
|
|
85
|
+
|
112
|
86
|
/* set listener for form submit button */
|
113
|
87
|
$(".submit").click(function(){
|
114
|
|
- /* extract args */
|
115
|
|
- var option = $(this).attr("id");
|
116
|
|
- var row = $(this).attr("val"); /* row id */
|
117
|
|
- var laboratory = get_arg("lab");
|
118
|
|
- /* set query for option */
|
119
|
|
- var query = null;
|
120
|
|
- switch(option){
|
121
|
|
- case "edit": /* edit row */
|
122
|
|
- query = 6;
|
123
|
|
- break;
|
124
|
|
- case "copy": /* copy row */
|
125
|
|
- query = 7;
|
126
|
|
- break;
|
127
|
|
- default: /* undefined option */
|
128
|
|
- display_error("Undefined option.");
|
129
|
|
- break;
|
130
|
|
- }
|
131
|
|
- /* get and validate form fields */
|
132
|
|
- var form = get_form(row);
|
133
|
|
- if(valid(form) && form_is_active){
|
134
|
|
- reset_form(); /* reset form */
|
135
|
|
- /* submit query for row */
|
136
|
|
- console.log(JSON.stringify(form));
|
137
|
|
- $.post("/scripts/opaso.php",{query: query,laboratory: laboratory,eid: row, fields: JSON.stringify(form)},function(data){
|
138
|
|
- console.log(data);
|
139
|
|
- /* parse response as json and display result of query */
|
140
|
|
- try{
|
141
|
|
- var response = JSON.parse(data); /* parse response as json */
|
142
|
|
- switch(response["status"]){
|
143
|
|
- case "success": /* on success, generate table body */
|
144
|
|
- break;
|
145
|
|
- case "error": /* on error, display message */
|
146
|
|
- display_error(response["error"]);
|
147
|
|
- break;
|
148
|
|
- default: /* undefined status */
|
149
|
|
- display_error("Undefined status.");
|
150
|
|
- break;
|
151
|
|
- }
|
152
|
|
- }
|
153
|
|
- /* handle error */
|
154
|
|
- catch(error){
|
155
|
|
- display_error(error.message);
|
|
88
|
+ if(get_form()){
|
|
89
|
+ /* submit query */
|
|
90
|
+ hide_form();
|
|
91
|
+ $.post("/scripts/opaso",{query: 6,lab_id: get_arg("lab_id"),mat_id: inventory[i]["material"]["mat_id"],man_id: inventory[i]["manufacturer"]["man_id"],uom: inventory[i]["uom"],capacity: inventory[i]["capacity"],data: JSON.stringify(form)},function(data){
|
|
92
|
+ // console.log(data);
|
|
93
|
+ /* extract response */
|
|
94
|
+ let response = JSON.parse(data);
|
|
95
|
+ let status = response["status"];
|
|
96
|
+
|
|
97
|
+ /* handle by status */
|
|
98
|
+ switch(status){
|
|
99
|
+ case "expired": /* on expired, redirect to index */
|
|
100
|
+ window.location.href = "/?error=session_expired";
|
|
101
|
+ break;
|
|
102
|
+
|
|
103
|
+ case "success": /* on success, update inventory */
|
|
104
|
+ // console.log(inventory[i],form);
|
|
105
|
+ for(var field in form){
|
|
106
|
+ if(field === "mat_name"){
|
|
107
|
+ inventory[i]["material"][field] = form[field];
|
|
108
|
+ }
|
|
109
|
+
|
|
110
|
+ else if(field === "man_name"){
|
|
111
|
+ inventory[i]["manufacturer"][field] = form[field];
|
|
112
|
+ }
|
|
113
|
+
|
|
114
|
+ else{
|
|
115
|
+ inventory[i][field] = form[field];
|
|
116
|
+ }
|
|
117
|
+
|
|
118
|
+ if(field === "sds"){
|
|
119
|
+ $(`#${i}`).find(`td[value=${field}]`).find("span").attr("href",form[field]);
|
|
120
|
+ }
|
|
121
|
+ else{
|
|
122
|
+ $(`#${i}`).find(`td[value=${field}]`).text(form[field]);
|
|
123
|
+ }
|
|
124
|
+ }
|
|
125
|
+ // console.log(inventory[i]);
|
|
126
|
+
|
|
127
|
+ default: /* on success/error, display message */
|
|
128
|
+ set_alert(status,response["message"]);
|
|
129
|
+ break;
|
156
|
130
|
}
|
157
|
131
|
});
|
158
|
132
|
}
|
159
|
|
- /* non-valid form, display warning */
|
160
|
|
- else{
|
161
|
|
- $(".fields-required").show();
|
|
133
|
+ /* invalid form, display warning */
|
|
134
|
+ else{
|
|
135
|
+ set_alert("error","All fields required");
|
162
|
136
|
}
|
163
|
137
|
});
|
164
|
|
- /* set listener for form close buttons */
|
165
|
|
- $(".close-form").click(function(){
|
166
|
|
- reset_form(); /* reset form */
|
167
|
|
- });
|
168
|
|
- /* set listener for double clicks on cells */
|
169
|
|
- $(".inventory-cell").dblclick(function(){
|
170
|
|
- /* generate text area if user isn't editing */
|
|
138
|
+
|
|
139
|
+ /* edit cell */
|
|
140
|
+ $(".entry-cell").dblclick(function(){
|
|
141
|
+ /* verify if user is editing */
|
171
|
142
|
if(!editing){
|
172
|
|
- $(this).height($(this).height()); /* avoid cell from collapsing */
|
173
|
|
- $(this).css("padding","0px"); /* remove padding */
|
174
|
|
- /* generate textarea with text from clicked cell */
|
175
|
|
- text = $(this).text();
|
176
|
|
- var textarea = create_element("textarea","edit-text",text);
|
177
|
|
- textarea.style.width = $(this).width() + "px";
|
178
|
|
- textarea.style.height = $(this).height() + "px";
|
179
|
|
- /* insert text area into DOM */
|
|
143
|
+ $(this).height($(this).height());
|
|
144
|
+ $(this).css("padding","0px");
|
|
145
|
+
|
|
146
|
+ if($(this).attr("value") === "sds"){
|
|
147
|
+ text = $(this).find("span").attr("href");
|
|
148
|
+ }
|
|
149
|
+ else{
|
|
150
|
+ text = $(this).text();
|
|
151
|
+ }
|
|
152
|
+
|
|
153
|
+ i = $(this).parent().attr("id");
|
|
154
|
+ var textarea = element_gen("textarea",{class: "edit",text: text});
|
180
|
155
|
$(this).html(textarea);
|
181
|
|
- editing = true; /* user is editing */
|
|
156
|
+ editing = true;
|
182
|
157
|
}
|
183
|
158
|
});
|
184
|
|
- /* set listener for clicks on body */
|
185
|
|
- $("body").click(function(element){
|
186
|
|
- /* extract class of clicked element */
|
187
|
|
- var target = $(element.target).attr("class");
|
188
|
|
- if(editing && target != "edit-text"){
|
189
|
|
- /* extract text from textarea and field and row id */
|
190
|
|
- var delta_text = $(".edit-text").val();
|
191
|
|
- var field = $(".edit-text").parent().val();
|
192
|
|
- var row = $(".edit-text").parent().parent().attr("id");
|
193
|
|
- /* reset styling and insert text to cell */
|
194
|
|
- $(".edit-text").parent().removeAttr("style");
|
195
|
|
- $(".edit-text").parent().html(delta_text);
|
196
|
|
- editing = false; /* deactivate editing */
|
197
|
|
- /* submit row changes to inventory */
|
198
|
|
- if(text != delta_text){
|
199
|
|
- /* extract arg */
|
200
|
|
- var laboratory = get_arg("lab");
|
201
|
|
- /* send new field text */
|
202
|
|
- $.post("/scripts/opaso.php",{query: 5,laboratory: laboratory,eid: row,text: delta_text, field: field},function(data){
|
203
|
|
- console.log(data);
|
204
|
|
- try{
|
205
|
|
- var response = JSON.parse(data); /* parse response as json */
|
206
|
|
- switch(response["status"]){
|
207
|
|
- case "success": /* on success, generate table body */
|
208
|
|
- break;
|
209
|
|
- case "error": /* on error, display message */
|
210
|
|
- display_error(response["error"]);
|
211
|
|
- break;
|
212
|
|
- default: /* undefined status */
|
213
|
|
- display_error("Undefined status.");
|
214
|
|
- break;
|
215
|
|
- }
|
216
|
|
- }
|
217
|
|
- catch(error){
|
218
|
|
- display_error(error.message);
|
219
|
|
- }
|
220
|
|
- });
|
221
|
|
- }
|
|
159
|
+
|
|
160
|
+ /* submit cell edit */
|
|
161
|
+ $("body").click(function(event){
|
|
162
|
+ var t = $(event.target).attr("class");
|
|
163
|
+ if(editing && t !== "edit"){
|
|
164
|
+ edit_field();
|
222
|
165
|
}
|
223
|
|
- /* reset form on exit */
|
224
|
|
- else if(form_is_active && target == "form-window"){
|
225
|
|
- reset_form(); /* reset form */
|
|
166
|
+ });
|
|
167
|
+
|
|
168
|
+ /* submit cell edit */
|
|
169
|
+ $(document).on("keypress",function(event){
|
|
170
|
+ if(event.which == 13 && editing){
|
|
171
|
+ event.preventDefault();
|
|
172
|
+ edit_field();
|
226
|
173
|
}
|
227
|
174
|
});
|
228
|
|
- /* set listener for dropdown menu */
|
229
|
|
- $( ".navbar-wrapper" ).hover(function(){}, /* on hover in, do nothing */
|
230
|
|
- /* on hover out, hide dropdown menu */
|
231
|
|
- function(){
|
232
|
|
- /* check if menu is visible */
|
233
|
|
- if($(".dropdown-menu").is(':visible')){
|
234
|
|
- $(".dropdown-menu").removeClass("show"); /* hide menu */
|
235
|
|
- }
|
|
175
|
+
|
|
176
|
+ /* hide form */
|
|
177
|
+ $(".close-form").click(function(e){
|
|
178
|
+ if(e.target === e.currentTarget){
|
|
179
|
+ hide_form();
|
236
|
180
|
}
|
237
|
|
- );
|
238
|
|
-}
|
239
|
|
-/* reset_form() - reset form and its styling */
|
240
|
|
-function reset_form(){
|
241
|
|
- /* reset styling (label) */
|
242
|
|
- $(".required-field").each(function(){
|
243
|
|
- $(this).removeClass("required-field");
|
244
|
|
- $(this).text($(this).text().replace("*",""));
|
245
|
181
|
});
|
246
|
|
- /* reset styling (input) */
|
247
|
|
- $(".empty-field").each(function(){
|
248
|
|
- $(this).removeClass("empty-field");
|
|
182
|
+
|
|
183
|
+ /* hide confirm alert */
|
|
184
|
+ $(".hide-confirm").click(function(e){
|
|
185
|
+ if(e.target === e.currentTarget){
|
|
186
|
+ hide_confirm();
|
|
187
|
+ }
|
249
|
188
|
});
|
250
|
|
- /* hide from */
|
251
|
|
- $(".form-window").hide();
|
252
|
|
- $(".fields-required").hide();
|
253
|
|
- form_is_active = false; /* deactivate form */
|
254
|
189
|
}
|
255
|
|
-/* valid(form: dictionary) - check for invalid form entries */
|
256
|
|
-function valid(form){
|
257
|
|
- var valid = true;
|
258
|
|
- for(var field in form){
|
259
|
|
- /* highlight empty fields */
|
260
|
|
- if(!form[field]){
|
261
|
|
- /* if not currently highlighted */
|
262
|
|
- if($("[for=" + field + "]").attr("class") != "required-field"){
|
263
|
|
- /* highlight label */
|
264
|
|
- $("[for=" + field + "]").text($("[for=" + field + "]").text() + "*");
|
265
|
|
- $("[for=" + field + "]").addClass("required-field");
|
266
|
|
- /* highlight field */
|
267
|
|
- $("[name=" + field + "]").addClass("empty-field");
|
268
|
|
- }
|
269
|
|
- valid = false;
|
|
190
|
+
|
|
191
|
+/* edit_field() - update material field */
|
|
192
|
+function edit_field(){
|
|
193
|
+ /* extract text */
|
|
194
|
+ var delta = $(".edit").val();
|
|
195
|
+ var field = $(".edit").parent().attr("value");
|
|
196
|
+
|
|
197
|
+ if(text != delta){
|
|
198
|
+
|
|
199
|
+ // console.log(inventory,i,inventory[i]);
|
|
200
|
+
|
|
201
|
+ let data = {mat_id: inventory[i]["material"]["mat_id"],man_id: inventory[i]["manufacturer"]["man_id"],capacity: inventory[i]["capacity"], uom: inventory[i]["uom"]};
|
|
202
|
+
|
|
203
|
+ if(field === "capacity"){
|
|
204
|
+ data["prev_capacity"] = text;
|
|
205
|
+ data["new_capacity"] = delta;
|
|
206
|
+ total = inventory[i]["total"];
|
|
207
|
+ data["quantity"] = Math.ceil(total/delta);
|
|
208
|
+ }
|
|
209
|
+
|
|
210
|
+ else if(field === "total"){
|
|
211
|
+ data["total"] = delta;
|
|
212
|
+ data["quantity"] = Math.ceil(delta/data["capacity"]);
|
|
213
|
+ }
|
|
214
|
+
|
|
215
|
+ else if(field === "quantity"){
|
|
216
|
+ data["quantity"] = delta;
|
|
217
|
+ data["total"] = delta * data["capacity"];
|
|
218
|
+ }
|
|
219
|
+
|
|
220
|
+ else if(field === "uom"){
|
|
221
|
+ data["prev_uom"] = text;
|
|
222
|
+ data["new_uom"] = delta;
|
270
|
223
|
}
|
271
|
|
- /* remove styling for non empty fields */
|
|
224
|
+
|
272
|
225
|
else{
|
273
|
|
- /* if currently highlighted... */
|
274
|
|
- if($("[for=" + field + "]").attr("class") == "required-field"){
|
275
|
|
- /* reset styling for label */
|
276
|
|
- $("[for=" + field + "]").text($("[for=" + field + "]").text().replace("*",""));
|
277
|
|
- $("[for=" + field + "]").removeClass("required-field");
|
278
|
|
- /* reset styling for field */
|
279
|
|
- $("[name=" + field + "]").removeClass("empty-field");
|
|
226
|
+ data[field] = delta;
|
|
227
|
+ }
|
|
228
|
+
|
|
229
|
+ /* update field */
|
|
230
|
+ $.post("/scripts/opaso",{query: 5,lab_id: get_arg("lab_id"),data: JSON.stringify(data),field: field},function(data){
|
|
231
|
+ // console.log(data);
|
|
232
|
+ /* extract response */
|
|
233
|
+ let response = JSON.parse(data);
|
|
234
|
+ let status = response["status"];
|
|
235
|
+ /* handle by status */
|
|
236
|
+ switch(status){
|
|
237
|
+ case "expired": /* on expired, redirect to index */
|
|
238
|
+ window.location.href = "/?error=session_expired";
|
|
239
|
+ break;
|
|
240
|
+
|
|
241
|
+ case "success": /* on success, update entry */
|
|
242
|
+ if(field === "mat_name"){
|
|
243
|
+ inventory[i]["material"][field] = delta;
|
|
244
|
+ }
|
|
245
|
+ else if(field === "man_name"){
|
|
246
|
+ inventory[i]["manufacturer"][field] = delta;
|
|
247
|
+ }
|
|
248
|
+ else if(field === "total"){
|
|
249
|
+ inventory[i]["quantity"] = Math.ceil(delta/inventory[i]["capacity"]);
|
|
250
|
+ $(`#${i}`).find("td[value=quantity]").text(inventory[i]["quantity"]);
|
|
251
|
+
|
|
252
|
+ }
|
|
253
|
+
|
|
254
|
+ else if(field === "capacity"){
|
|
255
|
+ total = inventory[i]["total"];
|
|
256
|
+ inventory[i]["quantity"] = Math.ceil(total/delta);
|
|
257
|
+ $(`#${i}`).find("td[value=quantity]").text(inventory[i]["quantity"]);
|
|
258
|
+ }
|
|
259
|
+
|
|
260
|
+ else if(field === "quantity"){
|
|
261
|
+ inventory[i]["total"] = delta * inventory[i]["capacity"];
|
|
262
|
+ $(`#${i}`).find("td[value=total]").text(inventory[i]["total"]);
|
|
263
|
+ }
|
|
264
|
+
|
|
265
|
+ if(field === "sds"){
|
|
266
|
+ var icon = element_gen("i",{class: "fas fa-link"});
|
|
267
|
+ var link = element_gen("span",{class: "sds",href: decodeURI(delta),target: "_blank",rel: "noopener noreferrer",childs: {icon}});
|
|
268
|
+ $(".edit").parent().html(link);
|
|
269
|
+ }
|
|
270
|
+
|
|
271
|
+ else{
|
|
272
|
+ $(".edit").parent().html(delta);
|
|
273
|
+ }
|
|
274
|
+
|
|
275
|
+ if(field !== "mat_name" && field !== "man_name"){
|
|
276
|
+ inventory[i][field] = delta;
|
|
277
|
+ }
|
|
278
|
+
|
|
279
|
+ $(".edit").parent().removeAttr("style");
|
|
280
|
+
|
|
281
|
+
|
|
282
|
+
|
|
283
|
+ default: /* on error, display message */
|
|
284
|
+ if(field === "sds"){
|
|
285
|
+ var icon = element_gen("i",{class: "fas fa-link"});
|
|
286
|
+ var link = element_gen("span",{class: "sds",href: decodeURI(text),target: "_blank",rel: "noopener noreferrer",childs: {icon}});
|
|
287
|
+ $(".edit").parent().html(link);
|
|
288
|
+ }
|
|
289
|
+
|
|
290
|
+ else{
|
|
291
|
+ $(".edit").parent().html(text);
|
|
292
|
+ }
|
|
293
|
+
|
|
294
|
+ /* reset styling */
|
|
295
|
+ $(".edit").parent().removeAttr("style");
|
|
296
|
+ set_alert(status,response["message"]);
|
|
297
|
+ break;
|
280
|
298
|
}
|
|
299
|
+ });
|
|
300
|
+ }
|
|
301
|
+ else{
|
|
302
|
+ if(field === "sds"){
|
|
303
|
+ var icon = element_gen("i",{class: "fas fa-link"});
|
|
304
|
+ var link = element_gen("span",{class: "sds",href: decodeURI(text),target: "_blank",rel: "noopener noreferrer",childs: {icon}});
|
|
305
|
+ $(".edit").parent().html(link);
|
281
|
306
|
}
|
|
307
|
+ else{
|
|
308
|
+ $(".edit").parent().html(text);
|
|
309
|
+ $(".edit").parent().removeAttr("style");
|
|
310
|
+ }
|
282
|
311
|
}
|
|
312
|
+
|
|
313
|
+ editing = false;
|
|
314
|
+}
|
|
315
|
+
|
|
316
|
+/* get_form(row: string) - get form data from row */
|
|
317
|
+function get_form(){
|
|
318
|
+ form = {};
|
|
319
|
+ var valid = true;
|
|
320
|
+
|
|
321
|
+ $(".material-input").each(function(){
|
|
322
|
+ var id = $(this).attr("id");
|
|
323
|
+ // var s = `.${id}`;
|
|
324
|
+ var field = $(this).attr("name");
|
|
325
|
+
|
|
326
|
+ /* highlight */
|
|
327
|
+ if(!is_valid($(this).val())){
|
|
328
|
+ // $(s).show();
|
|
329
|
+ valid = false;
|
|
330
|
+ $(this).addClass("invalid");
|
|
331
|
+ }
|
|
332
|
+
|
|
333
|
+ /* unhighlight */
|
|
334
|
+ else{
|
|
335
|
+ // $(s).hide();
|
|
336
|
+ $(this).removeClass("invalid");
|
|
337
|
+ }
|
|
338
|
+
|
|
339
|
+ form[field] = $(this).val();
|
|
340
|
+ });
|
|
341
|
+
|
283
|
342
|
return valid;
|
284
|
343
|
}
|
285
|
|
-/* get_form(row: string) - get form data */
|
286
|
|
-function get_form(row){
|
287
|
|
- var form = {};
|
288
|
|
- for(var col in inventory[row]){
|
289
|
|
- form[col] = $("[name=" + col + "]").val();
|
|
344
|
+/* set_form() - populate form using inventory data */
|
|
345
|
+function set_form(){
|
|
346
|
+ for(var field in inventory[i]){
|
|
347
|
+ if(field === "material"){
|
|
348
|
+ var s = "mat_name";
|
|
349
|
+ var val = inventory[i][field][s];
|
|
350
|
+ }
|
|
351
|
+
|
|
352
|
+ else if(field === "manufacturer"){
|
|
353
|
+ var s = "man_name";
|
|
354
|
+ var val = inventory[i][field][s]
|
|
355
|
+ }
|
|
356
|
+
|
|
357
|
+ else{
|
|
358
|
+ var s = field;
|
|
359
|
+ var val = inventory[i][field];
|
|
360
|
+ }
|
|
361
|
+
|
|
362
|
+ $(`input[name=${s}]`).val(val);
|
290
|
363
|
}
|
291
|
|
- return form;
|
|
364
|
+
|
|
365
|
+ display_form();
|
292
|
366
|
}
|
293
|
|
-/* set_form(row: string) - populate form using inventory data */
|
294
|
|
-function set_form(row){
|
295
|
|
- /* fill form input by name */
|
296
|
|
- for(var col in inventory[row]){
|
297
|
|
- $("[name=" + col + "]").val(inventory[row][col]);
|
|
367
|
+
|
|
368
|
+/* table_gen(inventory: dictionary) - generate table body */
|
|
369
|
+function table_gen(data){
|
|
370
|
+ /* extract entries */
|
|
371
|
+ for(var d in data){
|
|
372
|
+ /* generate table row */
|
|
373
|
+ let tr = element_gen("tr",{class: "entry-row",id: d});
|
|
374
|
+ for(var field in data[d]){
|
|
375
|
+ switch(field){
|
|
376
|
+ case "material": /* material, extract id */
|
|
377
|
+ var link = element_gen("a",{class: "mat-link",text: data[d][field]["mat_name"],href: `materials?mat_id=${data[d][field]["mat_id"]}`,target: "_blank",rel: "noopener noreferrer"});
|
|
378
|
+ var attributes = {class: "entry-cell",value: "mat_name",mat_id: data[d][field]["mat_id"],childs: {link}};
|
|
379
|
+ break;
|
|
380
|
+
|
|
381
|
+ case "manufacturer": /* manufacturer, extract id */
|
|
382
|
+ var attributes ={class: "entry-cell",value: "man_name",man_id: data[d][field]["man_id"],text: data[d][field]["man_name"]};
|
|
383
|
+ break;
|
|
384
|
+
|
|
385
|
+ case "sds": /* generate sds link */
|
|
386
|
+ if(!data[d][field]){
|
|
387
|
+ data[d][field] = "-";
|
|
388
|
+ }
|
|
389
|
+ var icon = element_gen("i",{class: "fas fa-link"});
|
|
390
|
+ var link = element_gen("span",{class: "sds",href: decodeURI(data[d][field]),target: "_blank",rel: "noopener noreferrer",childs: {icon}});
|
|
391
|
+ var attributes = {class: "entry-cell",value: field,childs: {link}};
|
|
392
|
+ break;
|
|
393
|
+
|
|
394
|
+ default: /* default */
|
|
395
|
+ if(!data[d][field]){
|
|
396
|
+ data[d][field] = "-";
|
|
397
|
+ }
|
|
398
|
+ var attributes = {class: "entry-cell",value: field,text: data[d][field]};
|
|
399
|
+ break;
|
|
400
|
+ }
|
|
401
|
+
|
|
402
|
+ var td = element_gen("td",attributes);
|
|
403
|
+ tr.append(td);
|
|
404
|
+ }
|
|
405
|
+
|
|
406
|
+ /* generate actions cell */
|
|
407
|
+ let actions = {edit:{class: "t-icon material-icons",icon: "notes"},remove: {class: "t-icon material-icons",icon: "close"}};
|
|
408
|
+ var td = element_gen("td",{class: "actions-cell"});
|
|
409
|
+
|
|
410
|
+ /* generate actions */
|
|
411
|
+ for(var action in actions){
|
|
412
|
+ var icon = element_gen("i",{class: actions[action]["class"],text: actions[action]["icon"]});
|
|
413
|
+ var button = element_gen("button",{class: "button action",id: action,value: d,title: action,childs: {icon}});
|
|
414
|
+ td.append(button);
|
|
415
|
+ }
|
|
416
|
+
|
|
417
|
+ /* append */
|
|
418
|
+ tr.append(td);
|
|
419
|
+ $("tbody").append(tr);
|
298
|
420
|
}
|
299
|
|
- /* display form */
|
300
|
|
- $(".form-window").show();
|
301
|
|
- /* form is active */
|
302
|
|
- form_is_active = true;
|
|
421
|
+
|
|
422
|
+ /* display inventory */
|
|
423
|
+ $(".processing").hide();
|
|
424
|
+ setTimeout(function(){
|
|
425
|
+ set_listeners();
|
|
426
|
+ $(".main-wrapper").show();
|
|
427
|
+ },250);
|
303
|
428
|
}
|
304
|
|
-/* Load navbar and footer into document and fetch inventory */
|
305
|
|
-$(document).ready(function(){
|
306
|
|
- $(".navbar-wrapper").load("/navbar.html",function(){
|
307
|
|
- $(".footer-wrapper").load("/footer.html",function(){
|
308
|
|
- current(); /* highlight current navbar link */
|
309
|
|
- get_inventory(); /* fetch inventory from server */
|
|
429
|
+
|
|
430
|
+/* fetch_inventory() - fetch lab inventory */
|
|
431
|
+function fetch_inventory(){
|
|
432
|
+ /* extract lab id */
|
|
433
|
+ let lab_id = get_arg("lab_id");
|
|
434
|
+ if(is_valid(lab_id)){
|
|
435
|
+ $.post("/scripts/opaso",{query: 4,lab_id: lab_id},function(data){
|
|
436
|
+ // console.log(data);
|
|
437
|
+ /* extract response */
|
|
438
|
+ response = JSON.parse(data);
|
|
439
|
+ status = response["status"];
|
|
440
|
+
|
|
441
|
+ /* handle by status */
|
|
442
|
+ switch(status){
|
|
443
|
+ case "success": /* on success, generate table */
|
|
444
|
+ inventory = response["lab"]["inventory"];
|
|
445
|
+
|
|
446
|
+ /* generate inventory table */
|
|
447
|
+ table_gen(inventory);
|
|
448
|
+ next = Object.keys(inventory).length;
|
|
449
|
+ $(".table-total").text(`Total: ${next}`);
|
|
450
|
+ break;
|
|
451
|
+ case "expired": /* on expired, redirect */
|
|
452
|
+ window.location.href = "/?error=session_expired";
|
|
453
|
+ break;
|
|
454
|
+
|
|
455
|
+ default: /* on error, display message */
|
|
456
|
+ set_alert(status,response["message"]);
|
|
457
|
+ break;
|
|
458
|
+ }
|
310
|
459
|
});
|
311
|
|
- });
|
312
|
|
-});
|
|
460
|
+ }
|
|
461
|
+ /* missing args */
|
|
462
|
+ else{
|
|
463
|
+ set_alert("error","One or more arguments missing");
|
|
464
|
+ }
|
|
465
|
+}
|