-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproduct.html
490 lines (434 loc) · 18.6 KB
/
product.html
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
<!DOCTYPE html>
<html lang="en">
<head>
<title>POS Lite</title>
<link rel="icon" type="image/png" sizes="16x16"
href="https://cdn0.iconfinder.com/data/icons/kameleon-free-pack-rounded/110/Shop-16.png">
<link rel="icon" type="image/png" sizes="32x32"
href="https://cdn0.iconfinder.com/data/icons/kameleon-free-pack-rounded/110/Shop-32.png">
<link rel="icon" type="image/png" sizes="96x96"
href="https://cdn0.iconfinder.com/data/icons/kameleon-free-pack-rounded/110/Shop-96.png">
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<meta name="Description" content="POS Lite" />
<meta name="author" content="POS Lite">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.0.0/dist/css/bootstrap.min.css"
integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.1/css/all.min.css"
integrity="sha512-KfkfwYDsLkIlwQp6LFnl8zNdLGxu9YAA1QvwINks4PhcElQSvqcyVLLD9aMhXd13uQjoXtEKNosOWaZqXgel0g=="
crossorigin="anonymous" referrerpolicy="no-referrer" />
</head>
<body>
<div class="container-fluid">
<div class="row">
<div class="col-lg-12">
<ul class="nav nav-pills justify-content-end p-2" style="width:100%; background-color:aliceblue">
<li class="nav-item">
<a class="nav-link" href="./index.html">Sales</a>
</li>
<li class="nav-item">
<a class="nav-link active" href="#">Product</a>
</li>
<li class="nav-item">
<a class="nav-link" href="./config.html">Config</a>
</li>
</ul>
</div>
<div class="col-lg-12 mt-3">
<h2 class="alert alert-primary text-center">
Product
</h2>
</div>
<div class="col-lg-12">
<div class="card">
<div class="card-body p-3">
<div class="row">
<div class="col-lg-4 mb-2">
<div class="row no-gutters">
<div class="col mr-1">
<button type="button" class="btn btn-info btn-block" onclick="onDownloadProduct()">Download Product</button>
</div>
<div class="col">
<button type="button" class="btn btn-info btn-block" onclick="onUploadProduct()">Upload Product</button>
</div>
</div>
<div class="input-group-sm mt-2 mb-2" style="display:none" id="product_upload_form">
<input type="file" class="form-control" id="product_file">
<div class="input-group-append">
<button class="btn btn-secondary" type="button" data-toggle="modal" data-target="#exampleModal">Upload</button>
</div>
</div>
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Warning</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
This action will not change the current transaction record <br />
Do you really want to replace all products?
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary" data-dismiss="modal" onclick="onUploadProductProcess()">Save changes</button>
</div>
</div>
</div>
</div>
</div>
<div class="col-lg-4 offset-lg-4 mb-2">
<div class="input-group">
<input id="filter_value" type="text" class="form-control" placeholder="Filter Value"
aria-label="Filter Value" aria-describedby="basic-addon2">
<div class="input-group-append">
<select id="filter_type" class="custom-select" id="inputGroupSelect04">
<option value="default" selected>Choose filter</option>
<option value="product_code">Code</option>
<option value="product_name">Name</option>
</select>
<button class="btn btn-info btn-rounded" onclick="onSearchProduct()">
<i class="fa-solid fa-magnifying-glass"></i>
</button>
</div>
</div>
</div>
</div>
<div class="table-responsive">
<table id="datatable" class="table table-sm table-striped mb-0">
<thead>
<tr>
<th>ID</th>
<th>Code</th>
<th>Name</th>
<th style="text-align:end">Price</th>
<th style="text-align:center">Operation</th>
</tr>
</thead>
<tbody id="product_list"></tbody>
<tfoot id="new_product" class="bg-info">
<tr>
<td></td>
<td>
<input id="new_product_code" type="text" class="form-control">
</td>
<td>
<input id="new_product_name" type="text" class="form-control">
</td>
<td>
<input id="new_product_price" type="text" class="form-control" style="text-align:end">
</td>
<td style="text-align:center"><button type="button" class="btn btn-success btn-rounded btn-sm my-0"
style="border-color:antiquewhite" onclick="onAddProduct()">
<i class="fas fa-plus"></i>
</button></td>
</tr>
</tfoot>
</table>
</div>
</div>
</div>
</div>
</div>
<!-- Custom Alert -->
<div id="customAlert"></div>
</div>
<!-- Import JS -->
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"
integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN"
crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.12.9/dist/umd/popper.min.js"
integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q"
crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.0.0/dist/js/bootstrap.min.js"
integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl"
crossorigin="anonymous"></script>
<script src="js/localstoragedb.min.js"></script>
<script src="js/localstoragedbcustom.js"></script>
<script src="js/customalert.js"></script>
<!-- Custom JS -->
<script type="text/javascript">
allProducts() // init page
function allProducts() {
var listProduct = lsDB.queryAll("products")
if (listProduct.length == 0) {
return
}
productList = ``
listProduct.forEach(function (value) {
var singleDataValues = Object.values(value)
var singleDataKeys = Object.keys(value)
var singleDataRow = ``
singleDataValues.forEach(function (va, i) {
if (i + 1 == singleDataValues.length) { // indicating last row that i need to move text to right side
singleDataRow += `<td style="text-align:end" id="${singleDataKeys[i]}_${singleDataValues[0]}">${va}</td>`
} else {
singleDataRow += `<td id="${singleDataKeys[i]}_${singleDataValues[0]}">${va}</td>`
}
})
productList += `
<tr id="product_data_${singleDataValues[0]}">
${singleDataRow}
<td style="text-align:center">
<button class="btn btn-sm btn-secondary" onclick="onEditProduct(${singleDataValues[0]})">
<i class="fa fa-edit"></i>
</button>
<button class="btn btn-sm btn-danger" onclick="onDeleteProduct(${singleDataValues[0]})">
<i class="fas fa-trash"></i>
</button>
</td>
</tr>`
})
document.getElementById("product_list").innerHTML = productList
}
function onAddProduct() {
var productCode = document.getElementById("new_product_code").value
var productName = document.getElementById("new_product_name").value
var productPrice = document.getElementById("new_product_price").value
if (productCode == "" || productName == "" || productPrice == "") {
alertE("Product code, Product Name or Price should not empty")
return
}
var regex = /^.{5,}$/
if (!(regex.test(productCode)) || !(regex.test(productName))) {
alertE("Product code or product name should more than 5 character")
return
}
var regex = /^\d+$/
if (!(regex.test(productPrice))) {
alertE("Price should be numeric format")
return
}
var registeredProduct = lsDB.queryAll("products", {
query: { product_code: productCode }
})
if (registeredProduct.length > 0) {
alertE("Product code already used")
return
}
lsDB.insert("products", {
product_code: productCode,
product_name: productName,
product_price: productPrice,
})
lsDB.commit()
allProducts()
document.getElementById("new_product_name").value = ""
document.getElementById("new_product_code").value = ""
document.getElementById("new_product_price").value = ""
}
function onEditProduct(id) {
var currentProductID = document.getElementById("ID_" + id).innerHTML
var currentProductCode = document.getElementById("product_code_" + id).innerHTML
var currentProductName = document.getElementById("product_name_" + id).innerHTML
var currentProductPrice = document.getElementById("product_price_" + id).innerHTML
document.getElementById("product_data_" + id).innerHTML = `
<td id="ID_${id}">${currentProductID}</td>
<td id="product_code_${id}" contenteditable=true style="border:groove">${currentProductCode}</td>
<td id="product_name_${id}" contenteditable=true style="border:groove">${currentProductName}</td>
<td id="product_price_${id}" contenteditable=true style="border:groove; text-align:end">${currentProductPrice}</td>
<td style="text-align:center">
<button class="btn btn-sm btn-primary" onclick="onSaveProduct(${id})">
<i class="fa fa-save"></i>
</button>
<button class="btn btn-sm btn-danger" onclick="onDeleteProduct(${id})">
<i class="fas fa-trash"></i>
</button>
</td>`
}
function onSaveProduct(id) {
var changeProductID = document.getElementById("ID_" + id).innerHTML
var changeProductCode = document.getElementById("product_code_" + id).innerHTML
var changeProductName = document.getElementById("product_name_" + id).innerHTML
var changeProductPrice = document.getElementById("product_price_" + id).innerHTML
if (changeProductCode == "" || changeProductName == "" || changeProductPrice == "") {
alertE("Product code, product name or price should not empty")
return
}
var regex = /^.{5,}$/
if (!(regex.test(changeProductCode)) || !(regex.test(changeProductName))) {
alertE("Product code or product name should more than 5 character")
return
}
var regex = /^\d+$/
if (!(regex.test(changeProductPrice))) {
alertE("Price should be numeric format")
return
}
var registeredProduct = lsDB.queryAll("products", {
query: { product_code: changeProductCode }
})
if (registeredProduct.length > 0) {
if (registeredProduct[0].product_code == changeProductCode && registeredProduct[0].ID != id) {
alertE("Product code already used")
return
}
}
document.getElementById("product_data_" + id).innerHTML = `
<td id="ID_${id}">${changeProductID}</td>
<td id="product_code_${id}">${changeProductCode}</td>
<td id="product_name_${id}">${changeProductName}</td>
<td id="product_price_${id}" style="text-align:end">${changeProductPrice}</td>
<td style="text-align:center">
<button class="btn btn-sm btn-secondary" onclick="onEditProduct(${id})">
<i class="fa fa-edit"></i>
</button>
<button class="btn btn-sm btn-danger" onclick="onDeleteProduct(${id})">
<i class="fas fa-trash"></i>
</button>
</td>`
lsDB.update("products", { ID: id }, function (row) {
row.product_code = changeProductCode
row.product_name = changeProductName
row.product_price = changeProductPrice
return row
})
lsDB.commit()
}
function onDeleteProduct(id) {
lsDB.deleteRows("products", { ID: id })
lsDB.commit()
document.getElementById("product_data_" + id).remove()
}
function onSearchProduct() {
var filterType = document.getElementById("filter_type").value
var filterValue = document.getElementById("filter_value").value
var newQuery = {}
if (filterType == "default" || filterValue == "") {
document.getElementById("filter_value").value = ""
allProducts()
return
} else {
newQuery = function (row) {
if (row[filterType].toLowerCase().match(filterValue.toLowerCase())) {
return true
} else {
return false
}
}
}
var listProduct = lsDB.queryAll("products", {
query: newQuery
})
if (listProduct.length == 0) {
document.getElementById("product_list").innerHTML = ""
return
}
productList = ``
listProduct.forEach(function (value) {
var singleDataValues = Object.values(value)
var singleDataKeys = Object.keys(value)
var singleDataRow = ``
singleDataValues.forEach(function (va, i) {
if (i + 1 == singleDataValues.length) { // indicating last row that i need to move text to right side
singleDataRow += `<td style="text-align:end" id="${singleDataKeys[i]}_${singleDataValues[0]}">${va}</td>`
} else {
singleDataRow += `<td id="${singleDataKeys[i]}_${singleDataValues[0]}">${va}</td>`
}
})
productList += `
<tr id="product_data_${singleDataValues[0]}">
${singleDataRow}
<td style="text-align:center">
<button class="btn btn-sm btn-secondary" onclick="onEditProduct(${singleDataValues[0]})">
<i class="fa fa-edit"></i>
</button>
<button class="btn btn-sm btn-danger" onclick="onDeleteProduct(${singleDataValues[0]})">
<i class="fas fa-trash"></i>
</button>
</td>
</tr>`
})
document.getElementById("product_list").innerHTML = productList
}
function onDownloadProduct() {
var listProduct = lsDB.queryAll("products")
if (listProduct.length == 0) {
return
}
var header = Object.keys(listProduct[0]).join(",") + "\n"
var productCSV = ""
listProduct.forEach(function (value, i) {
var {ID, ...valueWithoutID} = value
if (i == 0) { // first iteration
productCSV += Object.keys(valueWithoutID).join(",") + "\n"
productCSV += Object.values(valueWithoutID).join(",") + "\n"
}
else if (i + 1 == listProduct.length) { // last iteration
productCSV += Object.values(valueWithoutID).join(",")
} else {
productCSV += Object.values(valueWithoutID).join(",") + "\n"
}
})
var csv_file, download_link
csv_file = new Blob([productCSV], { type: "text/csv" })
download_link = document.createElement("a")
download_link.download = "product.csv"
download_link.href = window.URL.createObjectURL(csv_file)
download_link.style.display = "none"
document.body.appendChild(download_link)
download_link.click()
}
function onUploadProduct() {
var formUploadProduct = document.getElementById("product_upload_form")
if (formUploadProduct.style.display === "none") {
formUploadProduct.style.display = "flex"
} else {
formUploadProduct.style.display = "none"
}
}
function onUploadProductProcess() {
var productFile = document.getElementById("product_file")
var regex = /^([a-zA-Z0-9\s_\\.\-:])+(.csv|.txt)$/
if (regex.test(productFile.value.toLowerCase())) {
if (typeof (FileReader) != "undefined") {
var reader = new FileReader()
reader.onload = function (e) {
var productJSON = csvToJSON(e.target.result)
if (productJSON) {
lsDB.truncate("products")
productJSON.forEach(function (value) {
lsDB.insert("products", value)
})
lsDB.commit()
allProducts()
} else {
alertE("Error, please re-validate your file");
}
}
reader.readAsText(productFile.files[0]);
} else {
alertE("This browser does not support HTML5");
}
} else {
alertE("Please upload a valid CSV file");
}
}
function csvToJSON(csv) {
var lines = csv.split("\n");
var result = [];
var headers = lines[0].split(",");
for (var i = 1; i < lines.length; i++) {
var obj = {};
var currentline = lines[i].split(",")
if (currentline.length > 3) {
return false
}
for (var j = 0; j < headers.length; j++) {
obj[headers[j]] = currentline[j]
}
result.push(obj)
}
const unique = new Set()
const anyDuplicateProductCode = result.some(element => unique.size === unique.add(element.product_code).size)
if (anyDuplicateProductCode) {
return false
}
return result
}
</script>
</body>
</html>