38682-vm/api/tables.php
Flatlogic Bot bd73e23131 fix 4
2026-02-27 02:55:44 +00:00

47 lines
1.6 KiB
PHP

<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
header('Content-Type: application/json');
require_once __DIR__ . '/../db/config.php';
try {
$pdo = db();
$outlet_id = isset($_GET['outlet_id']) ? intval($_GET['outlet_id']) : 1;
// Fetch all tables with their area names, filtered by outlet_id
$sql = "
SELECT t.id, t.table_number as name, t.capacity, a.name AS area_name, t.status
FROM tables t
LEFT JOIN areas a ON t.area_id = a.id
WHERE a.outlet_id = :outlet_id AND t.is_deleted = 0
ORDER BY a.name ASC, t.table_number ASC
";
$stmt = $pdo->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) || $table['status'] === 'occupied';
}
echo json_encode(['success' => true, 'tables' => $tables]);
} catch (Exception $e) {
http_response_code(500);
echo json_encode(['success' => false, 'error' => $e->getMessage()]);
}