false]; try { $UserID = (int) ($_GET['UserID'] ?? 0); $BusinessID = (int) ($_GET['BusinessID'] ?? 0); if ($UserID <= 0) { $response['ERROR'] = 'missing_user'; $response['MESSAGE'] = 'UserID is required'; jsonResponse($response); } if ($BusinessID <= 0) { $response['ERROR'] = 'missing_business'; $response['MESSAGE'] = 'BusinessID is required'; jsonResponse($response); } $qOrders = queryTimed(" SELECT o.ID, o.UUID, o.OrderTypeID, o.StatusID, o.SubmittedOn, o.ServicePointID, sp.Name AS Name, b.Name AS BizName, (SELECT COALESCE(SUM(oli.Price * oli.Quantity), 0) FROM OrderLineItems oli WHERE oli.OrderID = o.ID AND oli.IsDeleted = 0 AND oli.ParentOrderLineItemID = 0) AS Subtotal FROM Orders o LEFT JOIN ServicePoints sp ON sp.ID = o.ServicePointID LEFT JOIN Businesses b ON b.ID = o.BusinessID WHERE o.UserID = ? AND o.BusinessID = ? AND o.StatusID IN (1, 2, 3) ORDER BY o.SubmittedOn DESC LIMIT 5 ", [$UserID, $BusinessID]); $orders = []; foreach ($qOrders as $row) { $statusName = match ((int) $row['StatusID']) { 1 => 'Submitted', 2 => 'Preparing', 3 => 'Ready for Pickup', default => '', }; $orderTypeName = match ((int) $row['OrderTypeID']) { 1 => 'Dine-In', 2 => 'Takeaway', 3 => 'Delivery', default => '', }; $orders[] = [ 'OrderID' => (int) $row['ID'], 'UUID' => $row['UUID'], 'OrderTypeID' => (int) $row['OrderTypeID'], 'OrderTypeName' => $orderTypeName, 'StatusID' => (int) $row['StatusID'], 'StatusName' => $statusName, 'SubmittedOn' => toISO8601($row['SubmittedOn']), 'ServicePointID' => (int) ($row['ServicePointID'] ?? 0), 'Name' => trim($row['Name'] ?? '') !== '' ? $row['Name'] : '', 'Subtotal' => (float) $row['Subtotal'], ]; } $response['OK'] = true; $response['ORDERS'] = $orders; $response['HAS_PENDING'] = count($orders) > 0; } catch (Exception $e) { $response['ERROR'] = 'server_error'; $response['MESSAGE'] = $e->getMessage(); } jsonResponse($response);