diff --git a/action.php b/action.php index 9e14918..82605bf 100644 --- a/action.php +++ b/action.php @@ -312,10 +312,45 @@ END; 'notes' => $VARS['notes'] ]; + $customerid = null; if ($insert) { $database->insert('customers', $data); + $customerid = $database->id(); } else { $database->update('customers', $data, ['customerid' => $VARS['id']]); + $customerid = $VARS['id']; + } + + if (!is_null($customerid)) { + $custprices = $VARS['pricing']; + $newcustprices = []; + $oldcustprices = $database->select('customer_pricing', ['itemid (item)', 'price'], ['customerid' => $customerid]); + foreach ($custprices as $cp) { + if (!$binstack->has('items', ['itemid' => $cp['item']])) { + continue; + } + if (!is_numeric($cp['price'])) { + continue; + } + $newcustprices[] = $cp; + $oldcustprices = array_filter($oldcustprices, function ($var) { + if ($cp['item'] == $var['item']) { + return false; + } + return true; + }); + } + + foreach ($oldcustprices as $cp) { + $database->delete('customer_pricing', ['AND' => ['itemid' => $cp['item'], 'customerid' => $customerid]]); + } + foreach ($newcustprices as $cp) { + if ($database->has('customer_pricing', ['AND' => ['itemid' => $cp['item'], 'customerid' => $customerid]])) { + $database->update('customer_pricing', ['price' => $cp['price']], ['AND' => ['itemid' => $cp['item'], 'customerid' => $customerid]]); + } else { + $database->insert('customer_pricing', ['price' => $cp['price'], 'itemid' => $cp['item'], 'customerid' => $customerid]); + } + } } returnToSender("customer_saved"); diff --git a/lang/en_us.php b/lang/en_us.php index fa9d610..30c782f 100644 --- a/lang/en_us.php +++ b/lang/en_us.php @@ -62,4 +62,14 @@ define("STRINGS", [ "save" => "Save", "customer saved" => "Customer saved.", "invalid customer id" => "Invalid customer ID", + "customer pricing" => "Customer Pricing", + "item" => "Item", + "cost" => "Cost", + "normal price" => "Normal Price", + "customer price" => "Customer Price", + "add price" => "Add Price", + "add customer price" => "Add Customer Price", + "delete" => "Delete", + "cancel" => "Cancel", + "price" => "Price" ]); \ No newline at end of file diff --git a/pages.php b/pages.php index e319538..1446141 100644 --- a/pages.php +++ b/pages.php @@ -19,6 +19,7 @@ define("PAGES", [ "static/css/pos.css", ], "scripts" => [ + "static/js/input_type_money.js", "static/js/bsalert.js", "static/js/pos_items.js", "static/js/pos_customer.js", @@ -43,7 +44,14 @@ define("PAGES", [ "editcustomer" => [ "title" => "edit customer", "navbar" => false, + "styles" => [ + "static/css/datatables.min.css", + "static/css/tables.css" + ], "scripts" => [ + "static/js/input_type_money.js", + "static/js/bsalert.js", + "static/js/datatables.min.js", "static/js/editcustomer.js" ] ], diff --git a/pages/editcustomer.php b/pages/editcustomer.php index ece9dfd..a9d2cb2 100644 --- a/pages/editcustomer.php +++ b/pages/editcustomer.php @@ -36,10 +36,69 @@ if (!empty($VARS['id']) && !is_empty($VARS['id'])) { // customer id is invalid, redirect to a version of the page that won't // cause an error when pressing Save header('Location: app.php?page=editcustomer'); + die(); + } +} + +$pricing = []; + +if ($editing) { + $pricing = $database->select('customer_pricing', ['itemid', 'price'], ['customerid' => $custdata['id']]); + for ($i = 0; $i < count($pricing); $i++) { + $pricing[$i]['item'] = $binstack->get('items', ['itemid', 'name', 'code1', 'code2', 'cost', 'price'], ['itemid' => $pricing[$i]['itemid']]); } } ?> + +

@@ -78,6 +137,55 @@ if (!empty($VARS['id']) && !is_empty($VARS['id'])) {

+
+ +
+
+
+
+ +
+
+ + + + + + + + + + + + + + + + + + + + + + + +
+
+
+ + + + + +
+
+ diff --git a/static/js/editcustomer.js b/static/js/editcustomer.js index 0b0deec..8948fa8 100644 --- a/static/js/editcustomer.js +++ b/static/js/editcustomer.js @@ -7,4 +7,137 @@ $('#name').on('input propertychange paste', function () { $('#name_title').text($('#name').val()); -}); \ No newline at end of file +}); + +var pricingtable = $('#pricingtable').DataTable({ + responsive: { + details: { + display: $.fn.dataTable.Responsive.display.modal({ + header: function (row) { + var data = row.data(); + return " " + data[2]; + } + }), + renderer: $.fn.dataTable.Responsive.renderer.tableAll({ + tableClass: 'table' + }), + type: "column" + } + }, + columnDefs: [ + { + targets: 0, + className: 'control', + orderable: false + }, + { + targets: 1, + orderable: false + } + ], + order: [ + [2, 'asc'] + ] +}); + +$("#pricingtable").on("click", ".deletepricebtn", function () { + pricingtable.row($("#pricingtable tr[data-itemid=" + $(this).data("itemid") + "]")).remove(); + pricingtable.draw(); +}); + +function findItem(q) { + function decodeThenSetItem(item) { + var name = item['name']; + var id = item['id']; + var price = item['price']; + var cost = item['price']; + + $("#pricemodal").data("itemid", id); + $("#pricemodalitemname").text(name); + $("#pricemodalitemcost").text(cost); + $("#pricemodalitemprice").text(price); + $("#pricemodalcustomerprice").val(price); + } + if (q == "") { + return; + } + $.get("action.php", { + action: "itemsearch", + q: q + }, function (data) { + if (data['items'].length == 1) { + decodeThenSetItem(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) { + decodeThenSetItem(data['items'][i]); + break; + } + } + } + ); + } + }).fail(function () { + alert("Error"); + }); +} + +$("#pricemodalitem").on('keypress', function (e) { + if (e.which === 13) { + findItem($("#pricemodalitem").val()); + $("#pricemodalitem").val(""); + } +}); + +$("#pricemodalsearch").on("click", function () { + findItem($("#pricemodalitem").val()); + $("#pricemodalitem").val(""); +}); + +$("#addcustomerpricebtn").click(function () { + $("#pricemodal").data("itemid", ""); + $("#pricemodal").modal(); +}); + +$("#pricemodalsave").click(function () { + var itemid = $("#pricemodal").data("itemid"); + var name = $("#pricemodalitemname").text(); + var cost = $("#pricemodalitemcost").text(); + var price = $("#pricemodalitemprice").text(); + var custprice = $("#pricemodalcustomerprice").val(); + var rownumber = $("#pricingtable tbody tr").length; + var html = '' + + '' + + ' ' + + ' ' + + '
Delete
' + + ' ' + + ' ' + + ' ' + + ' ' + name + + ' ' + + ' ' + cost + '' + + ' ' + price + '' + + ' ' + + ' ' + + ' ' + custprice + + ' ' + + ''; + pricingtable.row.add($(html)); + pricingtable.draw(); + $("#pricemodal").modal("hide"); +}) \ No newline at end of file diff --git a/static/js/input_type_money.js b/static/js/input_type_money.js new file mode 100644 index 0000000..1b7ab7b --- /dev/null +++ b/static/js/input_type_money.js @@ -0,0 +1,20 @@ +/* + * 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/. + */ + + +$("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; + } +}); \ No newline at end of file diff --git a/static/js/pos.js b/static/js/pos.js index fc043ae..7fab73d 100644 --- a/static/js/pos.js +++ b/static/js/pos.js @@ -53,21 +53,6 @@ function recalculate() { $("#receiptchange").text(change.toFixed(2)); } - -$("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; - } -}); - // Make sure the session doesn't expire setInterval(function () { $.get("action.php", {action: "session_keepalive"});