Commit 7b94ae6c authored by Malcolm Blaney's avatar Malcolm Blaney

Purchase module switch timestamp ranges to calendar date start and

end, to make it clear that when updates are made they are only made
to purchases on the same day.
parent 35277fd7
Pipeline #35349099 passed with stage
in 1 minute and 10 seconds
......@@ -850,8 +850,8 @@ class Purchase extends Base {
}
else {
// Look for purchases over a range of time.
$start = $timestamp - 43200;
$end = $timestamp + 43200;
$start = strtotime(date('F j Y 00:00:00', $timestamp));
$end = $start + 86399;
$query = 'SELECT name, quantity, timestamp FROM purchase WHERE ' .
'user = "' . $user . '" AND timestamp >= ' . $start . ' AND ' .
'timestamp <= ' . $end;
......@@ -969,14 +969,15 @@ class Purchase extends Base {
// the start of the day (midnight), otherwise find last week's date.
$start = (int)strtotime($co_op_day);
if ($start > time()) $start = (int)strtotime('last ' . $co_op_day);
$end = $start + 86400;
$end = $start + 86399;
// Calculate quantities to deduct from stock for this order.
$products = [];
$query = 'SELECT name, supplier, SUM(quantity) AS quantity FROM purchase ' .
'WHERE user = "' . $user . '" AND timestamp > ' . $start . ' AND ' .
'timestamp < ' . $end . ' AND name != "surcharge" AND quantity != 0 AND '.
'purchase_group = "' . $this->user->group . '" GROUP BY name, supplier';
'WHERE user = "' . $user . '" AND timestamp >= ' . $start . ' AND ' .
'timestamp <= ' . $end . ' AND name != "surcharge" AND quantity != 0 ' .
'AND purchase_group = "' . $this->user->group . '" ' .
'GROUP BY name, supplier';
if ($result = $mysqli->query($query)) {
while ($purchase = $result->fetch_assoc()) {
$supplier = $purchase['supplier'];
......@@ -992,10 +993,11 @@ class Purchase extends Base {
$stock->Decrease($products);
$query = 'INSERT INTO purchase (user, timestamp, name, supplier, grower, ' .
'quantity, price, base_price, volunteer) SELECT user, ' . $timestamp.', '.
'name, supplier, grower, quantity, price, base_price, volunteer FROM ' .
'purchase WHERE user = "' . $user . '" AND timestamp > ' . $start.' AND '.
'timestamp < ' . $end . ' AND name != "surcharge" AND quantity != 0 AND '.
'quantity, price, base_price, volunteer) SELECT user, ' .
$timestamp . ', name, supplier, grower, quantity, price, base_price, ' .
'volunteer FROM purchase WHERE user = "' . $user . '" AND ' .
'timestamp >= ' . $start . ' AND timestamp <= ' . $end . ' AND ' .
'name != "surcharge" AND quantity != 0 AND '.
'purchase_group = "' . $this->user->group . '"';
if (!$mysqli->query($query)) {
$this->Log('Purchase->PreOrder 3: ' . $mysqli->error);
......@@ -1533,11 +1535,11 @@ class Purchase extends Base {
$mysqli = connect_db();
$products = [];
$name_query = '';
$start = $next_co_op - 86400;
$end = $next_co_op + 86400;
$start = $next_co_op;
$end = $next_co_op + 86399;
$query = 'SELECT name, supplier, SUM(quantity) AS quantity FROM purchase ' .
'WHERE purchase_group = "' . $this->user->group . '" AND ' .
'timestamp > ' . $start . ' AND timestamp < ' . $end .
'timestamp >= ' . $start . ' AND timestamp <= ' . $end .
' GROUP BY name, supplier';
if ($result = $mysqli->query($query)) {
while ($purchase = $result->fetch_assoc()) {
......@@ -1567,8 +1569,8 @@ class Purchase extends Base {
$name_query = '(' . $name_query . ') AND ';
}
$query = 'DELETE FROM purchase WHERE ' . $name_query .
'purchase_group = "' . $this->user->group . '" AND timestamp > ' . $start.
' AND timestamp < ' . $end;
'purchase_group = "' . $this->user->group . '" AND ' .
'timestamp >= ' . $start . ' AND timestamp <= ' . $end;
if (!$mysqli->query($query)) {
$this->Log('Purchase->RemoveAllPurchases 3: ' . $mysqli->error);
}
......@@ -2074,7 +2076,7 @@ class Purchase extends Base {
$us_user_index = json_decode($_POST['userIndex'], true);
$us_new_sales = json_decode($_POST['newSales'], true);
$start = strtotime(date('F j Y 00:00:00'));
$end = $start + 86400;
$end = $start + 86399;
$current = $this->AllData($start, $end, false, false, false);
$payment = new Payment($this->user, $this->owner);
$organiser = new Organiser($this->user, $this->owner);
......@@ -2281,7 +2283,7 @@ class Purchase extends Base {
else {
$start = strtotime($co_op_day);
}
$end = $start + 86400;
$end = $start + 86399;
if ($start > time() || $end < time()) return false;
// Purchasing is available, but also need to check if this group should
......@@ -2623,7 +2625,7 @@ class Purchase extends Base {
$object['data'] = [$this->user->name => $this->Data($next_co_op)];
$object['total'] = [$this->user->name =>
$this->Total($this->user->name, $next_co_op,
$next_co_op + 86400, true)];
$next_co_op + 86399, true)];
$user_query = ' AND user != "' . $this->user->name . '"';
}
// Also look up quantities for other users so quotas can be calculated.
......@@ -2667,11 +2669,9 @@ class Purchase extends Base {
}
else {
$object['products'] = $stock->AvailableProducts();
// Otherwise in normal purchasing mode, only show purchases from the last
// 12 hours, as these are the only ones that can be changed in UpdateData
// below. (12 hours is used so that purchases from the previous day are
// not displayed for groups that meet on consecutive days.)
$start = strtotime('-12 hours');
// Otherwise in normal purchasing mode, only show purchases from today's
// date onwards, as only these can be changed in UpdateData.
$start = strtotime(date('F j Y 00:00:00'));
$end = 0;
// An end timestamp is required when pre-order is true, otherwise
// orders for next week will be shown when purchasing this week.
......@@ -2842,9 +2842,9 @@ class Purchase extends Base {
// associated with the item, and only keep the most recent one.
// A further complication however is that at some point the new data
// shouldn't be considered a duplicate, but a new purchase. Make this
// cut-off 12 hours either side of the given purchase date.
$start = $timestamp - 43200;
$end = $timestamp + 43200;
// cut-off the same day as the given timestamp.
$start = strtotime(date('F j Y 00:00:00', $timestamp));
$end = $start + 86399;
$user_not_found = false;
$current = $this->AllData($start, $end, false, false, false);
$us_data = json_decode($_POST['data'], true);
......
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