45 lines
1.4 KiB
PHP
45 lines
1.4 KiB
PHP
<?php
|
|
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.name, t.capacity, a.name AS area_name
|
|
FROM tables t
|
|
LEFT JOIN areas a ON t.area_id = a.id
|
|
WHERE a.outlet_id = :outlet_id
|
|
ORDER BY a.name ASC, t.name 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);
|
|
}
|
|
|
|
echo json_encode(['success' => true, 'tables' => $tables]);
|
|
|
|
} catch (Exception $e) {
|
|
http_response_code(500);
|
|
echo json_encode(['success' => false, 'error' => $e->getMessage()]);
|
|
} |