Add getWashesInTimeRange method to orders_o for fetching orders within specified daily time range
- Introduce filtering by time range and optional department ID. - Validate inputs and ensure robust exception handling for invalid ranges. - Optimize SQL queries for performance with joins and distinct order retrieval.
This commit is contained in:
@@ -1566,4 +1566,62 @@ class orders_o extends db
|
||||
}
|
||||
return $order_item_objects;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \DateTime|string $daily_start E.g. 2025-01-01 08:00:00
|
||||
* @param \DateTime|string $daily_end E.g. 2025-01-01 18:00:00
|
||||
* $options array An optional array of options to filter the results. Supported options:
|
||||
* - department_id (int): Filter orders by a specific department ID.
|
||||
* @return orders_o[] An array of orders that have washes within the specified daily time range, regardless of the date. The time range is specified in "H:i:s" format (e.g. "08:00:00" for 8 AM and "18:00:00" for 6 PM).
|
||||
* @throws Exception
|
||||
*/
|
||||
public function getWashesInTimeRange(\DateTime|string $daily_start, \DateTime|string $daily_end, array $options = []): array
|
||||
{ global /** @var db $db */
|
||||
$db;
|
||||
// Convert daily_start and daily_end to time strings if they are DateTime objects
|
||||
if ($daily_start instanceof \DateTime) {
|
||||
$daily_start = $daily_start->format('Y-m-d H:i:s');
|
||||
}
|
||||
if ($daily_end instanceof \DateTime) {
|
||||
$daily_end = $daily_end->format('Y-m-d H:i:s');
|
||||
}
|
||||
// Validate the time range
|
||||
if (strtotime($daily_start) === false || strtotime($daily_end) === false) {
|
||||
throw new Exception('Invalid time range provided');
|
||||
}
|
||||
if (strtotime($daily_start) > strtotime($daily_end)) {
|
||||
throw new Exception('The start time cannot be after the end time');
|
||||
}
|
||||
// Prepare the SQL query to find orders with washes in the daily time range
|
||||
$daily_start = $db->escape_string($daily_start);
|
||||
$daily_end = $db->escape_string($daily_end);
|
||||
// Options for filtering by department ID
|
||||
$department_filter = '';
|
||||
if (isset($options['department_id']) && is_int($options['department_id'])) {
|
||||
$department_id = $options['department_id'];
|
||||
$department_filter = "AND o.department_id = $department_id";
|
||||
}
|
||||
|
||||
$sql = "SELECT DISTINCT o.id
|
||||
FROM $this->table o
|
||||
JOIN order_items oi ON oi.order_id = o.id
|
||||
JOIN products p ON p.id = oi.product_id
|
||||
WHERE TIME(o.created_at) BETWEEN TIME('$daily_start') AND TIME('$daily_end')
|
||||
AND o.created_at BETWEEN '$daily_start' AND '$daily_end'
|
||||
AND o.deleted_at IS NULL
|
||||
AND p.is_wash = 1
|
||||
$department_filter";
|
||||
//echo "Executing SQL query to find washes in time range: $sql\n"; // Debug log to check the generated SQL query
|
||||
$result = $db->query($sql);
|
||||
if ($result->num_rows === 0) {
|
||||
return []; // No washes found in the time range
|
||||
}
|
||||
$orders = [];
|
||||
while ($row = $result->fetch_assoc()) {
|
||||
$order = new orders_o();
|
||||
$order->select((int)$row['id']);
|
||||
$orders[] = $order;
|
||||
}
|
||||
return $orders;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user