Complete port of all 163 API endpoints from Lucee/CFML to PHP 8.3. Shared helpers in api/helpers.php (DB, auth, request/response, security). PDO prepared statements throughout. Same JSON response shapes as CFML.
132 lines
5.2 KiB
PHP
132 lines
5.2 KiB
PHP
<?php
|
|
require_once __DIR__ . '/../helpers.php';
|
|
runAuth();
|
|
|
|
/**
|
|
* List Orders for KDS (Kitchen Display System)
|
|
* POST: { BusinessID: int, ServicePointID?: int, StationID?: int }
|
|
*/
|
|
|
|
$data = readJsonBody();
|
|
$BusinessID = (int) ($data['BusinessID'] ?? 0);
|
|
$ServicePointID = (int) ($data['ServicePointID'] ?? 0);
|
|
$StationID = (int) ($data['StationID'] ?? 0);
|
|
|
|
if ($BusinessID <= 0) {
|
|
apiAbort(['OK' => false, 'ERROR' => 'missing_params', 'MESSAGE' => 'BusinessID is required.']);
|
|
}
|
|
|
|
try {
|
|
// Build WHERE clause
|
|
$whereClauses = ['o.BusinessID = ?'];
|
|
$params = [$BusinessID];
|
|
|
|
if ($ServicePointID > 0) {
|
|
$whereClauses[] = 'o.ServicePointID = ?';
|
|
$params[] = $ServicePointID;
|
|
}
|
|
|
|
$whereClauses[] = 'o.StatusID >= 1';
|
|
$whereClauses[] = 'o.StatusID < 4';
|
|
|
|
$whereSQL = implode(' AND ', $whereClauses);
|
|
|
|
if ($StationID > 0) {
|
|
$stationParams = array_merge($params, [$StationID]);
|
|
$qOrders = queryTimed("
|
|
SELECT DISTINCT
|
|
o.ID, o.UUID, o.UserID, o.BusinessID, o.OrderTypeID, o.StatusID,
|
|
o.ServicePointID, o.Remarks,
|
|
DATE_FORMAT(o.SubmittedOn, '%Y-%m-%dT%H:%i:%sZ') AS SubmittedOn,
|
|
DATE_FORMAT(o.LastEditedOn, '%Y-%m-%dT%H:%i:%sZ') AS LastEditedOn,
|
|
sp.Name AS Name,
|
|
u.FirstName, u.LastName
|
|
FROM Orders o
|
|
LEFT JOIN ServicePoints sp ON sp.ID = o.ServicePointID
|
|
LEFT JOIN Users u ON u.ID = o.UserID
|
|
INNER JOIN OrderLineItems oli ON oli.OrderID = o.ID
|
|
INNER JOIN Items i ON i.ID = oli.ItemID
|
|
WHERE {$whereSQL}
|
|
AND (i.StationID = ? OR i.StationID IS NULL)
|
|
AND oli.IsDeleted = 0
|
|
ORDER BY SubmittedOn ASC, o.ID ASC
|
|
", $stationParams);
|
|
} else {
|
|
$qOrders = queryTimed("
|
|
SELECT
|
|
o.ID, o.UUID, o.UserID, o.BusinessID, o.OrderTypeID, o.StatusID,
|
|
o.ServicePointID, o.Remarks,
|
|
DATE_FORMAT(o.SubmittedOn, '%Y-%m-%dT%H:%i:%sZ') AS SubmittedOn,
|
|
DATE_FORMAT(o.LastEditedOn, '%Y-%m-%dT%H:%i:%sZ') AS LastEditedOn,
|
|
sp.Name AS Name,
|
|
u.FirstName, u.LastName
|
|
FROM Orders o
|
|
LEFT JOIN ServicePoints sp ON sp.ID = o.ServicePointID
|
|
LEFT JOIN Users u ON u.ID = o.UserID
|
|
WHERE {$whereSQL}
|
|
ORDER BY o.SubmittedOn ASC, o.ID ASC
|
|
", $params);
|
|
}
|
|
|
|
$orders = [];
|
|
foreach ($qOrders as $row) {
|
|
// Get line items for this order
|
|
$qLineItems = queryTimed("
|
|
SELECT
|
|
oli.ID, oli.ParentOrderLineItemID, oli.ItemID, oli.Price,
|
|
oli.Quantity, oli.Remark, oli.IsDeleted, oli.StatusID,
|
|
i.Name, i.ParentItemID, i.IsCheckedByDefault, i.StationID,
|
|
parent.Name AS ItemParentName
|
|
FROM OrderLineItems oli
|
|
INNER JOIN Items i ON i.ID = oli.ItemID
|
|
LEFT JOIN Items parent ON parent.ID = i.ParentItemID
|
|
WHERE oli.OrderID = ? AND oli.IsDeleted = 0
|
|
ORDER BY oli.ID
|
|
", [(int) $row['ID']]);
|
|
|
|
$lineItems = [];
|
|
foreach ($qLineItems as $li) {
|
|
$lineItems[] = [
|
|
'OrderLineItemID' => (int) $li['ID'],
|
|
'ParentOrderLineItemID' => (int) $li['ParentOrderLineItemID'],
|
|
'ItemID' => (int) $li['ItemID'],
|
|
'Price' => (float) $li['Price'],
|
|
'Quantity' => (int) $li['Quantity'],
|
|
'Remark' => $li['Remark'],
|
|
'Name' => $li['Name'],
|
|
'ParentItemID' => (int) $li['ParentItemID'],
|
|
'ItemParentName' => $li['ItemParentName'],
|
|
'IsCheckedByDefault' => (int) $li['IsCheckedByDefault'],
|
|
'StationID' => (int) ($li['StationID'] ?? 0),
|
|
'StatusID' => (int) $li['StatusID'],
|
|
];
|
|
}
|
|
|
|
$orderTypeName = match ((int) $row['OrderTypeID']) {
|
|
1 => 'Dine-In', 2 => 'Takeaway', 3 => 'Delivery', default => '',
|
|
};
|
|
|
|
$orders[] = [
|
|
'OrderID' => (int) $row['ID'],
|
|
'UUID' => $row['UUID'],
|
|
'UserID' => (int) $row['UserID'],
|
|
'BusinessID' => (int) $row['BusinessID'],
|
|
'OrderTypeID' => (int) $row['OrderTypeID'],
|
|
'OrderTypeName' => $orderTypeName,
|
|
'StatusID' => (int) $row['StatusID'],
|
|
'ServicePointID' => (int) ($row['ServicePointID'] ?? 0),
|
|
'Remarks' => $row['Remarks'],
|
|
'SubmittedOn' => $row['SubmittedOn'],
|
|
'LastEditedOn' => $row['LastEditedOn'],
|
|
'Name' => $row['Name'],
|
|
'FirstName' => $row['FirstName'],
|
|
'LastName' => $row['LastName'],
|
|
'LineItems' => $lineItems,
|
|
];
|
|
}
|
|
|
|
jsonResponse(['OK' => true, 'ERROR' => '', 'ORDERS' => $orders, 'STATION_FILTER' => $StationID]);
|
|
|
|
} catch (Exception $e) {
|
|
jsonResponse(['OK' => false, 'ERROR' => 'server_error', 'MESSAGE' => 'DB error loading orders for KDS', 'DETAIL' => $e->getMessage()]);
|
|
}
|