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']]); } } ?> +