You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
194 lines
6.3 KiB
JavaScript
194 lines
6.3 KiB
JavaScript
/*
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
*/
|
|
|
|
function addItem(name, code, price) {
|
|
if ($(".list-group-item[data-code='" + code + "']").length) {
|
|
updateQty($(".list-group-item[data-code='" + code + "']").find(".qty-plus"), 1);
|
|
return;
|
|
}
|
|
price = (price * 1.0).toFixed(2);
|
|
$("#pos-lines-box").append('<div class="list-group-item" data-code="' + code + '">'
|
|
+ '<div class="d-flex w-100 justify-content-between mb-2">'
|
|
+ '<h5 class="item-name">'
|
|
+ name
|
|
+ '</h5>'
|
|
+ '<h5>'
|
|
+ '<small class="item-code mr-1">' + code + '</small>'
|
|
+ '<span class="badge badge-light">'
|
|
+ '$<span class="line-total">'
|
|
+ price
|
|
+ '</span>'
|
|
+ '</span>'
|
|
+ '</h5>'
|
|
+ '</div>'
|
|
+ '<div class="d-inline-flex">'
|
|
+ '<div class="input-group qty-control">'
|
|
+ '<div class="input-group-prepend">'
|
|
+ '<span class="input-group-text pr-1"><b>$</b></span>'
|
|
+ '</div>'
|
|
+ '<input type="money" class="form-control item-price" value="' + price + '"/>'
|
|
+ '<div class="input-group-prepend">'
|
|
+ '<span class="input-group-text px-2"><i class="fas fa-times"></i></span>'
|
|
+ '<button class="btn btn-red qty-minus" type="button"><i class="fas fa-trash"></i></button>'
|
|
+ '</div>'
|
|
+ '<input type="number" class="form-control item-qty px-2" value="1" />'
|
|
+ '<div class="input-group-append">'
|
|
+ '<button class="btn btn-light-green qty-plus" type="button"><i class="fas fa-plus"></i></button>'
|
|
+ '</div>'
|
|
+ '</div>'
|
|
+ '</div>'
|
|
+ '</div>');
|
|
}
|
|
|
|
function findItem(q) {
|
|
function decodeThenAddItem(item) {
|
|
var code = item.code1;
|
|
console.log(code);
|
|
if (code == "" && item["code2"] != "") {
|
|
code = item["code2"];
|
|
} else if (code == "") {
|
|
code = "---";
|
|
}
|
|
var price = item['price'];
|
|
if (price == null || price == "" || price == 0) {
|
|
if (!$(".list-group-item[data-code='" + code + "']").length) {
|
|
bsprompt("Enter Price",
|
|
"No price set. Enter a price for this item:",
|
|
"Add Item",
|
|
"Cancel",
|
|
"number",
|
|
function (result) {
|
|
addItem(item['name'], code, result);
|
|
});
|
|
return;
|
|
}
|
|
}
|
|
addItem(item['name'], code, price);
|
|
$("#barcode").val("");
|
|
}
|
|
if (q == "") {
|
|
return;
|
|
}
|
|
$.get("action.php", {
|
|
action: "itemsearch",
|
|
q: q
|
|
}, function (data) {
|
|
if (data['items'].length == 1) {
|
|
decodeThenAddItem(data['items'][0]);
|
|
} else if (data['items'].length > 1) {
|
|
var options = [];
|
|
for (var i = 0; i < data['items'].length; i++) {
|
|
var text = data['items'][i]['name'];
|
|
if (data['items'][i]['price'] != null) {
|
|
text += " ($" + data['items'][i]['price'] + ")";
|
|
}
|
|
options.push({"text": text, "val": data['items'][i]['id']});
|
|
}
|
|
bschoices(
|
|
"Multiple Results",
|
|
"More than one item match the query. Pick the correct one:",
|
|
options,
|
|
"Cancel",
|
|
function (result) {
|
|
for (var i = 0; i < data['items'].length; i++) {
|
|
if (data['items'][i]['id'] == result) {
|
|
decodeThenAddItem(data['items'][i]);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
);
|
|
}
|
|
}).fail(function () {
|
|
alert("Error");
|
|
});
|
|
}
|
|
|
|
function removezero() {
|
|
$("#pos-lines-box .list-group-item").each(function () {
|
|
var qty = $(".item-qty", this).val() * 1.0;
|
|
if (qty == 0) {
|
|
$(this).remove();
|
|
}
|
|
});
|
|
}
|
|
|
|
function recalculate() {
|
|
removezero();
|
|
var total = 0.0;
|
|
$("#pos-lines-box .list-group-item").each(function () {
|
|
var each = $(".item-price", this).val() * 1.0;
|
|
var qty = $(".item-qty", this).val() * 1.0;
|
|
var line = each * qty;
|
|
$(".line-total", this).text(line.toFixed(2));
|
|
$(".item-price", this).val(each.toFixed(2));
|
|
console.log(each.toFixed(2));
|
|
total += line;
|
|
});
|
|
$(".grand-total").text(total.toFixed(2));
|
|
}
|
|
|
|
function updateQty(btn, diff) {
|
|
var qtybox = $(btn).parent().parent().find(".item-qty");
|
|
var qty = parseInt(qtybox.val());
|
|
qty += diff;
|
|
if (qty > 0) {
|
|
qtybox.val(qty);
|
|
var minbtn = $(btn).parent().parent().find(".qty-minus");
|
|
if (qty == 1) {
|
|
minbtn.html("<i class=\"fas fa-trash\"></i>");
|
|
} else {
|
|
minbtn.html("<i class=\"fas fa-minus\"></i>");
|
|
}
|
|
} else {
|
|
qtybox.closest(".list-group-item").remove();
|
|
}
|
|
recalculate();
|
|
}
|
|
|
|
$("#pos-lines-box").on("click", ".qty-minus", function () {
|
|
updateQty(this, -1);
|
|
});
|
|
|
|
$("#pos-lines-box").on("click", ".qty-plus", function () {
|
|
updateQty(this, 1);
|
|
});
|
|
|
|
$("#pos-lines-box").on("change", ".item-qty,.item-price", function () {
|
|
recalculate();
|
|
});
|
|
|
|
$("#pos-lines-box").on("keypress", ".item-qty,.item-price", function (e) {
|
|
if (e.which === 13) {
|
|
recalculate();
|
|
}
|
|
});
|
|
|
|
$("#barcode").on('keypress', function (e) {
|
|
if (e.which === 13) {
|
|
findItem($("#barcode").val());
|
|
}
|
|
});
|
|
|
|
$("#barcodebtn").on("click", function () {
|
|
findItem($("#barcode").val());
|
|
});
|
|
|
|
$("body").on("keypress", "input[type=money],input[type=number]", function (e) {
|
|
//console.log(String.fromCharCode(e.which) + "|" + e.which);
|
|
var c = String.fromCharCode(e.which);
|
|
var k = e.which;
|
|
if (/[0-9]|[\.]/.test(c)) {
|
|
// Numbers and period
|
|
} else if (k == 0 || k == 8) {
|
|
// Delete, backspace, etc
|
|
} else {
|
|
e.preventDefault();
|
|
return false;
|
|
}
|
|
});
|
|
|
|
$("#barcode").focus(); |