-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
325 lines (285 loc) · 10.4 KB
/
script.js
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
const batchSelection = document.getElementById("batchSelection");
const flexContainer = document.querySelector(".flex-container");
const fileInput = document.getElementById("fileInput");
const batchIndicator = document.getElementById("batchIndicator");
const body = document.getElementById("body");
const excelProducts = [];
let products = "";
fileInput.addEventListener("change", async function () {
const excelData = await readXlsxFile(fileInput.files[0]);
let fileName = fileInput.files[0].name;
for (let i = 1; i < excelData.length; i++) {
const product = {
"Product Code": excelData[i][0],
Quantity: excelData[i][1],
Placed: excelData[i][2],
"Not Placed": excelData[i][3],
"Destination List": excelData[i][4],
};
excelProducts.push(product);
}
products = [...excelProducts];
initialCount();
createOptionsList();
createTableContent();
batchIndicator.innerHTML = `<p style="color:#888;">Working with: '${fileName}' batch.</p>`;
body.classList.replace("body--backImage", "body--backColor");
batchSelection.style.display = "none";
flexContainer.style.display = "flex";
});
const form1 = document.getElementById("form1");
const searcher = document.getElementById("searcher");
const button1 = document.getElementById("button1");
const button2 = document.getElementById("button2");
const button3 = document.getElementById("button3");
const button4 = document.getElementById("button4");
const button5 = document.getElementById("button5");
const productDetails = document.getElementById("productDetails");
const noProduct = {
"Product Code": "Unregistered product",
Quantity: "------",
Placed: "------",
"Not Placed": "------",
"Destination List": "------",
};
const initialProduct = `
<ul>
<li><b>Product:</b> Select a product to see its values.</li>
<li><b>Quantity: ------</b></li>
<li><b>Placed: ------</b></li>
<li><b>Not Placed: ------</b></li>
<li><b>Destination List: ------</b></li>
</ul>`;
productDetails.innerHTML = initialProduct;
const disableButtons = () => {
button1.disabled = true;
button2.disabled = true;
button3.disabled = true;
button4.disabled = true;
button1.classList.add("disabled");
button2.classList.add("disabled");
button3.classList.add("disabled");
button4.classList.add("disabled");
};
disableButtons();
const enableButtons = () => {
button1.disabled = false;
button2.disabled = false;
button3.disabled = false;
button4.disabled = false;
button1.classList.remove("disabled");
button2.classList.remove("disabled");
button3.classList.remove("disabled");
button4.classList.remove("disabled");
};
const disableSubtractButton = (selectedProduct) => {
if (selectedProduct["Placed"] <= 0) {
button2.disabled = true;
button2.classList.add("disabled");
}
};
const disableInput = () => {
searcher.disabled = true;
searcher.classList.add("disabled");
};
const initialCount = () => {
products.forEach(function (product) {
if (product["Placed"] === null) {
product["Placed"] = 0;
product["Not Placed"] = product["Quantity"];
}
});
};
const countToZeroAll = () => {
products.forEach(function (product) {
product["Placed"] = 0;
product["Not Placed"] = product["Quantity"];
});
};
const checkSelectedP = (searcherValue) => {
let selectedProduct;
for (let i = 0; i < products.length; i++) {
if (products[i]["Product Code"] == searcherValue) {
selectedProduct = products[i];
enableButtons();
break;
} else {
selectedProduct = noProduct;
disableButtons();
}
}
return selectedProduct;
};
const alertCompleted = document.querySelector(".alert--fully-placed");
const alertSurpassed = document.querySelector(".alert--overstock");
const alertCount = (selectedProduct) => {
if (selectedProduct["Not Placed"] <= 0) {
alertCompleted.style.display = "inline-block";
} else {
alertCompleted.style.display = "none";
}
if (selectedProduct["Not Placed"] < 0) {
alertSurpassed.style.display = "inline-block";
} else {
alertSurpassed.style.display = "none";
}
disableSubtractButton(selectedProduct);
};
const resetCount = (searcherValue) => {
let confirmation = confirm(`The counting of all products will be restarted.
Do you want to continue?`);
if (confirmation) {
countToZeroAll();
showProduct(searcher.value);
for (let i = 0; i < products.length; i++) {
let iD = "product" + i;
let selectedRow = document.getElementById(iD);
selectedRow.innerHTML = `
<td>${products[i]["Product Code"]}</td>
<td>${products[i]["Quantity"]}</td>
<td>${products[i]["Placed"]}</td>
<td>${products[i]["Not Placed"]}</td>
<td>${products[i]["Destination List"]}</td>`;
}
}
};
const resetProduct = (searcherValue) => {
let confirmation = confirm(`The counting of this product will be restarted.
Do you want to continue?`);
if (confirmation) {
let selectedProduct = checkSelectedP(searcherValue);
selectedProduct["Placed"] = 0;
selectedProduct["Not Placed"] = selectedProduct["Quantity"];
showProduct(searcher.value);
}
};
const addProduct = (searcherValue) => {
let selectedProduct = checkSelectedP(searcherValue);
selectedProduct["Placed"] += 1;
selectedProduct["Not Placed"] -= 1;
showProduct(searcher.value);
};
const subtractProduct = (searcherValue) => {
let selectedProduct = checkSelectedP(searcherValue);
selectedProduct["Placed"] -= 1;
selectedProduct["Not Placed"] += 1;
showProduct(searcher.value);
};
const checkSelectedPi = (searcherValue) => {
for (let i = 0; i < products.length; i++) {
if (products[i]["Product Code"] == searcherValue) {
return i;
}
}
};
const replaceRow = (searcherValue) => {
let i = checkSelectedPi(searcherValue);
let iD = "product" + i;
if (i != undefined) {
let selectedRow = document.getElementById(iD);
if (products[i]["Not Placed"] == 0) {
selectedRow.innerHTML = `
<td>${products[i]["Product Code"]}</td>
<td>${products[i]["Quantity"]}</td>
<td>${products[i]["Placed"]}</td>
<td style="color:#ffff00; background:#ff0000; font-weight:bolder; font-family:sans-serif">${products[i]["Not Placed"]}</td>
<td>${products[i]["Destination List"]}</td>`;
} else if (products[i]["Not Placed"] < 0) {
selectedRow.innerHTML = `
<td>${products[i]["Product Code"]}</td>
<td>${products[i]["Quantity"]}</td>
<td>${products[i]["Placed"]}</td>
<td style="color:#f3f3f3; background:#0000ff; font-weight:bolder; font-family:sans-serif">${products[i]["Not Placed"]}</td>
<td>${products[i]["Destination List"]}</td>`;
} else {
selectedRow.innerHTML = `
<td>${products[i]["Product Code"]}</td>
<td>${products[i]["Quantity"]}</td>
<td>${products[i]["Placed"]}</td>
<td>${products[i]["Not Placed"]}</td>
<td>${products[i]["Destination List"]}</td>`;
}
}
};
const showProduct = (searcherValue) => {
let selectedProduct = checkSelectedP(searcherValue);
productDetails.innerHTML = `
<ul>
<li><b>Product:</b> <b style="color:red">
${selectedProduct["Product Code"]}</b></li>
<li><b>Quantity:</b> ${selectedProduct["Quantity"]}</li>
<li><b>Placed:</b> ${selectedProduct["Placed"]}</li>
<li><b>Not Placed:</b> ${selectedProduct["Not Placed"]}</li>
<li><b>Destination List:</b> <b style="color:blue;
font-family:sans-serif">${selectedProduct["Destination List"]}</b></li>
</ul>`;
alertCount(selectedProduct);
replaceRow(searcherValue);
};
const newSearch = () => {
if (searcher.value) {
searcher.value = "";
}
};
const createOptionsList = () => {
const dataList = document.getElementById("optionsList");
const dataListFragment = document.createDocumentFragment();
for (let i = 0; i < products.length; i++) {
const item = document.createElement("OPTION");
item.setAttribute("value", `${products[i]["Product Code"]}`);
dataListFragment.appendChild(item);
}
dataList.appendChild(dataListFragment);
};
const createTableContent = () => {
const table = document.getElementById("table");
const tableFragment = document.createDocumentFragment();
for (let i = 0; i < products.length; i++) {
const rowItem = document.createElement("TR");
let iD = "product" + i;
rowItem.setAttribute("id", iD);
if (products[i]["Not Placed"] == 0) {
rowItem.innerHTML = `
<td>${products[i]["Product Code"]}</td>
<td>${products[i]["Quantity"]}</td>
<td>${products[i]["Placed"]}</td>
<td style="color:#ffff00; background:#ff0000; font-weight:bolder; font-family:sans-serif">${products[i]["Not Placed"]}</td>
<td>${products[i]["Destination List"]}</td>`;
} else if (products[i]["Not Placed"] < 0) {
rowItem.innerHTML = `
<td>${products[i]["Product Code"]}</td>
<td>${products[i]["Quantity"]}</td>
<td>${products[i]["Placed"]}</td>
<td style="color:#f3f3f3; background:#0000ff; font-weight:bolder; font-family:sans-serif">${products[i]["Not Placed"]}</td>
<td>${products[i]["Destination List"]}</td>`;
} else {
rowItem.innerHTML = `
<td>${products[i]["Product Code"]}</td>
<td>${products[i]["Quantity"]}</td>
<td>${products[i]["Placed"]}</td>
<td>${products[i]["Not Placed"]}</td>
<td>${products[i]["Destination List"]}</td>`;
}
tableFragment.appendChild(rowItem);
}
table.appendChild(tableFragment);
};
form1.addEventListener("input", function () {
showProduct(searcher.value);
});
searcher.addEventListener("click", newSearch);
button1.addEventListener("click", function () {
addProduct(searcher.value);
});
button2.addEventListener("click", function () {
subtractProduct(searcher.value);
});
button3.addEventListener("click", function () {
resetCount(searcher.value);
});
button4.addEventListener("click", function () {
resetProduct(searcher.value);
});
button5.addEventListener("click", function () {
window.print();
});