38682-vm/api/tables.php
2026-02-22 11:09:10 +00:00

40 lines
1.2 KiB
PHP

<?php
header('Content-Type: application/json');
require_once __DIR__ . '/../db/config.php';
try {
$pdo = db();
// Fetch all tables with their area names
$sql = "
SELECT t.id, t.name, t.capacity, a.name AS area_name
FROM tables t
LEFT JOIN areas a ON t.area_id = a.id
ORDER BY a.name ASC, t.name ASC
";
$stmt = $pdo->query($sql);
$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
";
$occupiedStmt = $pdo->query($occupiedSql);
$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()]);
}