Commit dac84e86 authored by Mal's avatar Mal

Lots of bug fixes and improvements to the purchase module.

Small fix for displaying the browser module.
parent 51c4e4ba
......@@ -263,7 +263,7 @@ class Browser extends Base {
// Private functions below here ////////////////////////////////////////////
private function ImageContent($src, $image) {
return '<span class="thumbnail">'.$src.
return '<span class="thumbnail hidden">'.$src.
'<span class="filename hidden">'.$image.'</span><br>'.
'<button class="select hidden">select</button>'.
'<button class="remove hidden"></button>'.
......
......@@ -470,7 +470,7 @@ class Cart extends Base {
$mysqli = connect_db();
$query = 'SELECT name, image, short, full, weight, price, variable, '.
'minimum, download, item_order, available FROM cart_items WHERE user="'.
$this->owner.'" ORDER BY name';
$this->owner.'" ORDER BY item_order, name';
if ($result = $mysqli->query($query)) {
while ($cart_items = $result->fetch_assoc()) {
$object[] = $cart_items;
......
......@@ -49,6 +49,7 @@ class Purchase extends Base {
// Return milliseconds for use with javascript Date object.
$object["date"] = time() * 1000;
$object["nextWeek"] = [];
$object["processed"] = [];
}
else if ($action == "time") {
// Return milliseconds for use with javascript Date object.
......@@ -78,6 +79,19 @@ class Purchase extends Base {
}
// This array should be passed to a banking module to do the update.
$us_next_week = json_decode($_POST["nextWeek"], true);
$object["done"] = true;
}
else if ($action == "remove") {
$user = $mysqli->escape_string($_POST["user"]);
$name = $mysqli->escape_string($_POST["name"]);
// Timestamps are in milliseconds in javascript, convert to seconds.
$timestamp = (int)$mysqli->escape_string($_POST["timestamp"]) / 1000;
$query = 'DELETE FROM purchase WHERE user="'.$user.'" AND timestamp="'.
$timestamp.'" AND name="'.$name.'"';
if (!$mysqli->query($query)) {
error_log('Callback: '.$mysqli->error);
}
$object["done"] = true;
}
$mysqli->close();
return $object;
......@@ -140,7 +154,7 @@ class Purchase extends Base {
'</div>'.
'<form id="purchase-form">'.
'<div class="order hidden">You are currently taking an order for '.
'<span class="name"></span>, current total is: $'.
'<span class="name"></span>, current total for this week is: $'.
'<span class="total">0.00</span><br>'.
'<label for="purchase-next-week-input">'.
'Please ask them if they are coming next week? </label>'.
......@@ -189,7 +203,7 @@ class Purchase extends Base {
'user VARCHAR(50) NOT NULL,'.
'timestamp INT(10) UNSIGNED NOT NULL,'.
'name VARCHAR(100) NOT NULL,'.
'quantity INT UNSIGNED NOT NULL,'.
'quantity DECIMAL(8,3) NOT NULL,'.
'price DECIMAL(8,2) NOT NULL,'.
'PRIMARY KEY(user, timestamp, name)'.
') ENGINE=MyISAM';
......@@ -244,15 +258,24 @@ class Purchase extends Base {
if (!is_array($object[$purchase["user"]])) {
$object[$purchase["user"]] = array();
}
// The grid module requires this data as an indexed array, but
// when updating the data it's easier to do the comparison with
// an associative array keyed on item names.
// The grid module requires this data as an indexed array, but when
// updating the data it's easier to do the comparison with an
// associative array keyed on item names. The 'server' value is so
// that the client knows to tell the server when an item is removed.
if ($array) {
$quantity = (float)$purchase["quantity"];
$price = (float)$purchase["price"];
$total = $quantity * $price;
$total = number_format($total, 2, ".", "");
$price = number_format($price, 2, ".", "");
$timestamp = (int)$purchase["timestamp"] * 1000;
$object[$purchase["user"]][] =
array("date" => (int)$purchase["timestamp"],
array("date" => $timestamp,
"name" => $purchase["name"],
"quantity" => (float)$purchase["quantity"],
"price" => (float)$purchase["price"]);
"quantity" => $quantity,
"price" => $price,
"total" => $total,
"server" => true);
}
else {
$object[$purchase["user"]][$purchase["name"]] =
......@@ -272,36 +295,39 @@ class Purchase extends Base {
private function UpdateData($user, $us_values, $current) {
$mysqli = connect_db();
$query = 'INSERT INTO purchase VALUES ';
$values = "";
// Create a list of items that need updating.
for ($i = 0; $i < count($us_values); $i++) {
$date = $mysqli->escape_string($us_values[$i]["date"]);
// Timestamps are in milliseconds in javascript, convert to seconds.
$date = (int)$mysqli->escape_string($us_values[$i]["date"]) / 1000;
$name = $mysqli->escape_string($us_values[$i]["name"]);
$quantity = $mysqli->escape_string($us_values[$i]["quantity"]);
$price = $mysqli->escape_string($us_values[$i]["price"]);
$quantity = (float)$mysqli->escape_string($us_values[$i]["quantity"]);
$price = (float)$mysqli->escape_string($us_values[$i]["price"]);
if ($i > 0) $query .= ',';
if ($values !== "") $values .= ",";
$item = $current[$name];
if (is_array($item)) {
// This item already exists, so keep the most recent.
if ($date > $item["date"]) {
$query .= '("'.$user.'","'.$date.'","'.$name.'","'.$quantity.'","'.
$values .= '("'.$user.'","'.$date.'","'.$name.'","'.$quantity.'","'.
$price.'")';
$delete = 'DELETE FROM purchase WHERE user="'.$user.'" AND name="'.
$query = 'DELETE FROM purchase WHERE user="'.$user.'" AND name="'.
$name.'" AND timestamp="'.$item["date"].'"';
if (!$mysqli->query($delete)) {
if (!$mysqli->query($query)) {
error_log('UpdateData 1: '.$mysqli->error);
}
}
}
else {
// The item doesn't exist yet, so add it.
$query .= '("'.$user.'","'.$date.'","'.$name.'","'.$quantity.'","'.
$values .= '("'.$user.'","'.$date.'","'.$name.'","'.$quantity.'","'.
$price.'")';
}
}
if (!$mysqli->query($query)) {
error_log('UpdateData 2: '.$mysqli->error);
if ($values !== "") {
if (!$mysqli->query('INSERT INTO purchase VALUES '.$values)) {
error_log('UpdateData 2: '.$mysqli->error);
}
}
$mysqli->close();
}
......
......@@ -192,7 +192,9 @@ class Stock extends Base {
$this->AppendScript($path, "dobrado.stock.js", false);
}
public function AvailableProducts() {
// Private functions below here ////////////////////////////////////////////
private function AvailableProducts() {
$object = array();
$mysqli = connect_db();
$query = 'SELECT name, user, unit, price, category FROM stock '.
......@@ -214,8 +216,6 @@ class Stock extends Base {
return $object;
}
// Private functions below here ////////////////////////////////////////////
private function Products() {
$object = array();
$mysqli = connect_db();
......
......@@ -23,7 +23,7 @@ if (!this.dobrado.browser) {
zIndex: 11000
});
$(".upload").button().click(upload);
$(".thumbnail").click(highlight);
$(".thumbnail").show().click(highlight);
$(".thumbnail .select").button().click(select);
$(".thumbnail .remove").button({ icons: { primary: "ui-icon-trash" },
text: false }).click(remove);
......
......@@ -910,7 +910,7 @@ $special.draginit = $special.dragstart = $special.dragend = drag;
var year = d.getFullYear();
var month = d.getMonth() + 1;
var day = d.getDate();
return day + "/" + month + "/" + year;
return year + "-" + month + "-" + day;
}
})(jQuery);
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment