-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
456 lines (400 loc) · 17.5 KB
/
index.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
<!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 active" href="#">Sales</a>
</li>
<li class="nav-item">
<a class="nav-link" href="./product.html">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">
Sales
</h2>
</div>
<div class="col-lg-4 mb-3">
<div id="reader"></div>
</div>
<div class="col-lg-8 mb-3">
<div class="card" id="table_sales_card">
<div class="card-body p-3">
<div id="receipt_ref" style="display:none"></div>
<div class="table-responsive">
<table class="table table-sm table-striped mb-0" id="tableSalesData">
<thead>
<tr>
<th style="width:75%">Product</th>
<th style="text-align:end">Qty</th>
<th style="text-align:end">Price</th>
<th style="text-align:end">Total</th>
<th id="table_sales_operation_th">Operation</th>
</tr>
</thead>
<tbody id="tableSales"></tbody>
<tfoot class="bg-info" id="totalPrice"></tfoot>
</table>
</div>
</div>
</div>
<div class="card mt-3 mb-3">
<div class="card-body p-3">
<div>
<button type="button" class="btn btn-info" data-toggle="tooltip" data-placement="top"
title="Search product from list" onclick="onSearchClicked()">
<i class="fa-solid fa-magnifying-glass"></i>
</button>
<button type="button" class="btn btn-primary" data-toggle="tooltip" data-placement="top" title="Clear"
onclick="clearSalesTable()">
<i class="fas fa-redo"></i>
</button>
<button type="button" class="btn btn-success" data-toggle="tooltip" data-placement="top" title="Save transaction"
onclick="saveSalesTransaction()">
<i class="fas fa-save"></i>
</button>
<button type="button" class="btn btn-warning" data-toggle="tooltip" data-placement="top"
title="Print receipt" onclick="printTableSales()">
<i class="fa fa-print"></i>
</button>
</div>
<div>
<div class="mt-2" id="seach_product_form" style="display: none;">
<div>
<form class="form-inline mb-2">
<div class="form-group">
<label for=""></label>
<input type="text" placeholder="Product Code" class="form-control" name="product_code_search" id="product_code_search"
onkeypress="handleEnterKeyPressOnSearchProductCode(event)" />
</div>
<div class="form-group ml-1" style="align-self: end;">
<button type="button" class="btn btn-info" onclick="onSearchProduct()">
<i class="fa-solid fa-magnifying-glass"></i>
</button>
</div>
</form>
</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>Product</th>
<th style="text-align:end">Price</th>
<th style="text-align:center">Add</th>
</tr>
</thead>
<tbody id="tableProduct"></tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- Custom Alert -->
<div id="customAlert"></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>
<!-- Scanner -->
<script src="https://unpkg.com/html5-qrcode" type="text/javascript"></script>
<script type="text/javascript">
function onScanSuccess(decodedText, decodedResult) {
html5QrcodeScanner.pause()
setTimeout(onNextScan, 1500)
onScannerFound(decodedText)
}
function onScannerFound(decodedText) {
lsDB = newSesionDB()
var products = lsDB.queryAll("products", {
query: { product_code: decodedText }
})
if (products.length != 1) {
alertE("product not found or corrupted")
setTimeout(alertClose, 1000)
return
}
onAddToCart(products[0].ID, products[0].product_code, products[0].product_name, products[0].product_price, 1)
}
function onNextScan() {
html5QrcodeScanner.resume()
}
let qrboxFunction = function (viewfinderWidth, viewfinderHeight) {
let minEdgePercentage = 0.7 // 70%
let minEdgeSize = Math.min(viewfinderWidth, viewfinderHeight)
let qrboxSize = Math.floor(minEdgeSize * minEdgePercentage)
return {
width: qrboxSize,
height: qrboxSize
}
}
let config = { fps: 10, qrbox: qrboxFunction }
let html5QrcodeScanner = new Html5QrcodeScanner("reader", config, /* verbose= */ false)
html5QrcodeScanner.render(onScanSuccess)
</script>
<!-- Manual Add Product -->
<script type="text/javascript">
function handleEnterKeyPressOnSearchProductCode(e) {
if (e.keyCode === 13) {
e.preventDefault()
onSearchProduct()
}
}
function onSearchClicked() {
var formSearchProduct = document.getElementById("seach_product_form")
if (formSearchProduct.style.display === "none") {
formSearchProduct.style.display = "block"
} else {
formSearchProduct.style.display = "none"
}
}
function onSearchProduct() {
document.getElementById('tableProduct').innerHTML = ``
var input = document.getElementById('product_code_search').value
if (input.length < 5) {
alertE("Please type atleast 5 character")
return
}
lsDB = newSesionDB()
var searchQuery = function (row) {
if (row.product_code.toLowerCase().match(input.toLowerCase())) {
return true
} else {
return false
}
}
var searchProduct = lsDB.queryAll("products", {
query: searchQuery
})
var tableProduct = ``
if (searchProduct.length != 0) {
searchProduct.forEach(function (value) {
var singleData = Object.values(value)
var singleDataRow = ``
singleData.forEach(function (va, i) {
if (i+1 == singleData.length) {
singleDataRow += `<td style="text-align:end">${va}</td>`
} else{
singleDataRow += `<td>${va}</td>`
}
})
tableProduct += "<tr>" + singleDataRow + `<td style="text-align:center"><button type="button" class="btn btn-sm btn-success" onclick="onAddToCart(${singleData[0]}, '${singleData[1]}', '${singleData[2]}', '${singleData[3]}', 1)">
<i class="fa fa-plus"></i>
</button></td></tr>`
})
}
document.getElementById('tableProduct').innerHTML = tableProduct
}
</script>
<!-- Sales/Transaction Table -->
<script type="text/javascript">
function handleEnterKeyPressOnPaymentSave(e) {
if (e.keyCode === 13) {
e.preventDefault()
onTotalPaymentSave()
}
}
function onAddToCart(id, productCode, productName, productPrice, qtyValue) {
var pid = "pid_" + id
var pidName = "pid_name_" + productCode
var pidPrice = "pid_price_" + productCode
var pidQty = "pid_qty_" + id
var pidSubtotalPrice = "pid_subtotal_" + id
var tr = "<tr id=" + pid + ">"
var tdProductName = `<td id="${pidName}">${productName}</td>`
var tdProductPrice = `<td id="${pidPrice}" style='text-align:end'>${productPrice}</td>`
var tdOP = `<td id="table_sales_operation_button_${id}" style="text-align:center">
<button type="button" class="btn btn-sm btn-success" onclick="onAddToCart(${id}, '${productCode}', '${productName}', '${productPrice}', 1)">
<i class="fa fa-plus"></i>
</button>
<button type="button" class="btn btn-sm btn-danger" onclick="onAddToCart(${id}, '${productCode}', '${productName}', '${productPrice}', -1)">
<i class="fa fa-minus"></i>
</button>
</td>`
if (document.getElementById(pid) == null) {
var tdQty = `<td id="${pidQty}" style='text-align:end'>1</td>`
var tdSubTotalPrice = `<td id="${pidSubtotalPrice}" style='text-align:end'>${productPrice}</td>`
document.getElementById("tableSales").innerHTML += tr + tdProductName + tdQty + tdProductPrice + tdSubTotalPrice + tdOP + "</tr>"
calculateTotal()
} else {
var qty = parseInt(document.getElementById(pidQty).innerHTML) || 0
qty += qtyValue
if (qty == 0) {
document.getElementById(pid).remove()
calculateTotal()
return
}
productPriceInt = parseInt(productPrice) || 0
var subTotalPrice = qty * productPriceInt
var tdQty = `<td id="${pidQty}" style='text-align:end'>${qty}</td>`
var tdSubTotalPrice = `<td id="${pidSubtotalPrice}" style='text-align:end'>${subTotalPrice}</td>`
document.getElementById(pid).innerHTML = tr + tdProductName + tdQty + tdProductPrice + tdSubTotalPrice + tdOP + "</tr>"
calculateTotal()
}
}
function calculateTotal() {
var subTotal = Array.from(document.querySelectorAll("[id^='pid_subtotal_']"))
if (subTotal.length != 0) {
var total = 0
subTotal.forEach(function (value) {
var subTotalInt = parseInt(value.innerHTML) || 0
total += subTotalInt
})
document.getElementById("totalPrice").innerHTML = `<tr>
<th>Total</th>
<th colspan="3" id="totalPriceTH" style='text-align:end'>${total}</th>
<th id="table_sales_operation_tfoot_1"></th>
</tr>
<tr>
<th>Payment</th>
<th <th colspan="3" id="totalPayment" style='text-align:end'>${total}</th>
<th id="table_sales_operation_tfoot_2" style="text-align:center">
<button class="btn btn-sm btn-warning" onclick="onTotalPaymentEdit()">
<i class="fa fa-edit"></i>
</button>
</th>
</tr>
<tr>
<th>Change</th>
<th <th colspan="3" id="totalChange" style='text-align:end'>0</th>
<th id="table_sales_operation_tfoot_4"></th>
</tr>`
} else {
document.getElementById("totalPrice").innerHTML = ""
}
}
function onTotalPaymentEdit() {
var paymentBefore = document.getElementById("totalPayment").innerHTML
document.getElementById("totalPayment").innerHTML = `<input id="totalPaymentEdit" type="number" value=${paymentBefore} onkeypress="handleEnterKeyPressOnPaymentSave(event)"></input>`
document.getElementById("table_sales_operation_tfoot_2").innerHTML = `
<button class="btn btn-sm btn-primary" onclick="onTotalPaymentSave()">
<i class="fas fa-save"></i>
</button>`
}
function onTotalPaymentSave() {
var paymentAfter = document.getElementById("totalPaymentEdit").value
document.getElementById("totalPayment").innerHTML = `<th id="totalPayment">${paymentAfter}</th>`
document.getElementById("table_sales_operation_tfoot_2").innerHTML = `
<button class="btn btn-sm btn-warning" onclick="onTotalPaymentEdit()">
<i class="fa fa-edit"></i>
</button>`
calculatePayment()
}
function calculatePayment() {
var totalPrice = parseInt(document.getElementById("totalPriceTH").innerHTML) || 0
var totalPayment = parseInt(document.getElementById("totalPayment").innerHTML) || 0
document.getElementById("totalPayment").innerHTML = totalPayment
document.getElementById("totalChange").innerHTML = totalPayment - totalPrice
}
function clearSalesTable() {
document.getElementById("receipt_ref").innerHTML = ""
document.getElementById("tableSales").innerHTML = ""
document.getElementById("totalPrice").innerHTML = ""
}
function saveSalesTransaction() {
var tableCard = document.getElementById("table_sales_card").innerHTML
var tableSales = document.getElementById("tableSales").innerHTML
if (tableSales == "") {
alertE("Please add atleast 1 product to save this sales transaction")
return
}
var pBuyQty = Array.from(document.querySelectorAll("[id^='pid_qty_']"))
var pBuyPrice = Array.from(document.querySelectorAll("[id^='pid_price_']"))
var pBuyPriceTotal = Array.from(document.querySelectorAll("[id^='pid_subtotal_']"))
var pBuyName = Array.from(document.querySelectorAll("[id^='pid_name_']"))
var pTotal = document.getElementById("totalPriceTH").innerHTML
var pPayment = document.getElementById("totalPayment").innerHTML
var pChange = document.getElementById("totalChange").innerHTML
trxData = []
pBuyName.forEach(function (value, i) {
let data = {
product_code: value.id.replace("pid_name_", ""),
product_name: value.innerHTML,
product_price: pBuyPrice[i].innerHTML,
product_qty: pBuyQty[i].innerHTML,
product_price_total: pBuyPriceTotal[i].innerHTML
}
trxData.push(data)
})
var newDate = new Date();
var dateTime = newDate.toLocaleString('sv-SE') // Sweden format seems like common format. i want something like yyyy-mm-dd hh:ii:ss
var newQuery = function (row) {
if (row['datetime'].match(dateTime.substring(0, 10))) {
return true
} else {
return false
}
}
var trxRef = document.getElementById("receipt_ref").innerHTML
if (trxRef == "") {
var listSalesTrx = lsDB.queryAll("sales", {
query: newQuery
})
trxRef = `ST/${queryConfig("merchant_code")}/${queryConfig("pic_code")}/${String(listSalesTrx.length + 1).padStart(4, "0")}`
}
var trx = {
reference: trxRef,
timestamp: newDate / 1000 | 0, // only for backup, when someone want to convert to other format
datetime: dateTime,
data: trxData,
total: pTotal,
payment: pPayment,
change: pChange
}
document.getElementById("receipt_ref").innerHTML = trxRef
lsDB.insertOrUpdate("sales", {reference: trxRef}, trx)
lsDB.commit()
}
</script>
<!-- Table Printer -->
<script type="text/javascript">
function printTableSales() {
saveSalesTransaction() // save trx 1st then print it
var uri = window.location.pathname.replace("index.html", "receipt.html")
if (!(uri.includes("receipt.html"))) {
uri += "receipt.html"
}
var trxRef = document.getElementById("receipt_ref").innerHTML
var printWindow = window.open(uri + '?ref=' + trxRef, 'Print', 'left=200, top=200, width=950, height=500, toolbar=0, resizable=0');
printWindow.addEventListener('load', function(){
printWindow.print();
setTimeout(function(){
printWindow.close();
}, 500);
}, true);
html5QrcodeScanner.render(onScanSuccess)
}
</script>
</body>
</html>