prepare($sql); $stmt->execute(['outlet_id' => $outlet_id]); $tables = $stmt->fetchAll(PDO::FETCH_ASSOC); // Fetch currently occupied table IDs // Orders that are NOT completed and NOT cancelled are considered active $occupiedSql = " SELECT DISTINCT table_id FROM orders WHERE status NOT IN ('completed', 'cancelled') AND table_id IS NOT NULL AND outlet_id = :outlet_id "; $occupiedStmt = $pdo->prepare($occupiedSql); $occupiedStmt->execute(['outlet_id' => $outlet_id]); $occupiedTableIds = $occupiedStmt->fetchAll(PDO::FETCH_COLUMN); // Mark tables as occupied foreach ($tables as &$table) { $table['is_occupied'] = in_array($table['id'], $occupiedTableIds); } echo json_encode(['success' => true, 'tables' => $tables]); } catch (Exception $e) { http_response_code(500); echo json_encode(['success' => false, 'error' => $e->getMessage()]); }