This commit is contained in:
Flatlogic Bot 2026-02-27 03:10:56 +00:00
parent e2cbec0383
commit fb1aa34d97
3 changed files with 146 additions and 175 deletions

View File

@ -71,7 +71,7 @@ $query = "SELECT t.*, a.name as area_name
FROM tables t
LEFT JOIN areas a ON t.area_id = a.id
WHERE t.is_deleted = 0
ORDER BY a.name ASC, t.table_number ASC";
ORDER BY a.name ASC, table_number ASC";
$tables_pagination = paginate_query($pdo, $query);
$tables = $tables_pagination['data'];
@ -98,219 +98,168 @@ $baseUrl = $protocol . $host . $project_root;
<?= $message ?>
<div class="card border-0 shadow-sm">
<div class="card border-0 shadow-sm rounded-3">
<div class="card-body p-0">
<!-- Pagination Controls -->
<div class="p-3 border-bottom bg-light">
<?php render_pagination_controls($tables_pagination); ?>
</div>
<?php render_pagination_controls($tables_pagination); ?>
<div class="table-responsive">
<table class="table table-hover align-middle mb-0">
<thead class="bg-light">
<tr>
<th class="ps-4">ID</th>
<th>Table Number</th>
<th class="ps-4">QR Code</th>
<th>Table #</th>
<th>Area</th>
<th>Capacity</th>
<th>Status</th>
<th>QR Code</th>
<th class="text-end pe-4">Actions</th>
</tr>
</thead>
<tbody>
<?php if (empty($tables)): ?>
<tr>
<td colspan="6" class="text-center py-5">
<div class="text-muted mb-3">
<i class="bi bi-info-circle fs-1"></i>
</div>
<h6>No tables found</h6>
<p class="small">Add your first table to get started.</p>
</td>
</tr>
<?php endif; ?>
<?php foreach ($tables as $table): ?>
<tr>
<td class="ps-4 fw-medium">#<?= $table['id'] ?></td>
<td class="ps-4">
<?php
$qr_url = $baseUrl . "/qorder.php?table=" . $table['id'];
$qr_api = "https://api.qrserver.com/v1/create-qr-code/?size=100x100&data=" . urlencode($qr_url);
?>
<div class="d-flex align-items-center gap-3">
<a href="<?= $qr_url ?>" target="_blank" class="d-inline-block border rounded p-1">
<img src="<?= $qr_api ?>" alt="QR" width="50" height="50">
</a>
<div class="small text-muted">
<a href="<?= $qr_api ?>&size=500x500" download="table_<?= $table['table_number'] ?>.png" class="text-decoration-none">
<i class="bi bi-download me-1"></i>Download
</a>
</div>
</div>
</td>
<td class="fw-bold"><?= htmlspecialchars($table['table_number']) ?></td>
<td><span class="badge bg-light text-dark border"><?= htmlspecialchars($table['area_name'] ?: 'None') ?></span></td>
<td><?= $table['capacity'] ?> Persons</td>
<td>
<?php if ($table['status'] === 'available'): ?>
<span class="badge bg-success-subtle text-success px-3">Available</span>
<?php elseif ($table['status'] === 'occupied'): ?>
<span class="badge bg-danger-subtle text-danger px-3">Occupied</span>
<?php else: ?>
<span class="badge bg-secondary-subtle text-secondary px-3"><?= ucfirst($table['status']) ?></span>
<?php endif; ?>
<span class="badge bg-secondary bg-opacity-10 text-secondary border border-secondary border-opacity-25">
<?= htmlspecialchars($table['area_name'] ?? 'No Area') ?>
</span>
</td>
<td>
<button type="button" class="btn btn-sm btn-outline-info"
onclick="showTableQR('<?= $table['id'] ?>', '<?= htmlspecialchars($table['table_number']) ?>')">
<i class="bi bi-qr-code"></i> View QR
</button>
<i class="bi bi-people me-1 text-muted"></i>
<?= htmlspecialchars((string)$table['capacity']) ?>
</td>
<td>
<?php
$status_class = 'bg-success';
if ($table['status'] === 'occupied') $status_class = 'bg-danger';
if ($table['status'] === 'reserved') $status_class = 'bg-warning';
if ($table['status'] === 'inactive') $status_class = 'bg-secondary';
?>
<span class="badge <?= $status_class ?> bg-opacity-10 text-<?= str_replace('bg-', '', $status_class) ?> border border-<?= str_replace('bg-', '', $status_class) ?> border-opacity-25">
<?= ucfirst($table['status']) ?>
</span>
</td>
<td class="text-end pe-4">
<?php if (has_permission('tables_add')): ?>
<button type="button" class="btn btn-sm btn-outline-primary me-1"
data-bs-toggle="modal" data-bs-target="#tableModal"
onclick='prepareEditForm(<?= htmlspecialchars(json_encode($table), ENT_QUOTES, "UTF-8") ?>)' title="Edit"><i class="bi bi-pencil"></i></button>
<?php endif; ?>
<?php if (has_permission('tables_del')): ?>
<a href="?delete=<?= $table['id'] ?>" class="btn btn-sm btn-outline-danger" onclick="return confirm('<?= t('are_you_sure') ?>')"><i class="bi bi-trash"></i></a>
<?php endif; ?>
<div class="btn-group btn-group-sm">
<?php if (has_permission('tables_add')): ?>
<button class="btn btn-outline-primary" onclick='editTable(<?= json_encode($table) ?>)'>
<i class="bi bi-pencil"></i>
</button>
<?php endif; ?>
<?php if (has_permission('tables_del')): ?>
<a href="?delete=<?= $table['id'] ?>" class="btn btn-outline-danger" onclick="return confirm('Are you sure you want to remove this table?')">
<i class="bi bi-trash"></i>
</a>
<?php endif; ?>
</div>
</td>
</tr>
<?php endforeach; ?>
<?php if (empty($tables)): ?>
<tr>
<td colspan="7" class="text-center py-4 text-muted">No tables found.</td>
</tr>
<?php endif; ?>
</tbody>
</table>
</div>
<!-- Bottom Pagination -->
<div class="p-3 border-top bg-light">
<?php render_pagination_controls($tables_pagination); ?>
</div>
<?php render_pagination_controls($tables_pagination); ?>
</div>
</div>
<!-- Table Modal -->
<?php if (has_permission('tables_add')): ?>
<div class="modal fade" id="tableModal" tabindex="-1" aria-hidden="true">
<!-- Add/Edit Table Modal -->
<div class="modal fade" id="tableModal" tabindex="-1">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header bg-primary text-white">
<h5 class="modal-title" id="tableModalTitle">Add New Table</h5>
<button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
<div class="modal-content border-0 shadow">
<div class="modal-header">
<h5 class="modal-title fw-bold" id="modalTitle">Add New Table</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
</div>
<form method="POST" id="tableForm">
<form action="tables.php" method="POST">
<input type="hidden" name="action" id="formAction" value="add_table">
<input type="hidden" name="id" id="tableId">
<div class="modal-body">
<input type="hidden" name="action" id="tableAction" value="add_table">
<input type="hidden" name="id" id="tableId">
<div class="mb-3">
<label class="form-label">Table Number <span class="text-danger">*</span></label>
<input type="text" name="table_number" id="tableNumber" class="form-control" required placeholder="e.g. T1">
<label class="form-label fw-bold small text-uppercase">Table Number/Name</label>
<input type="text" name="table_number" id="tableNumber" class="form-control" required placeholder="e.g. Table 1, Booth A">
</div>
<div class="mb-3">
<label class="form-label">Area <span class="text-danger">*</span></label>
<select name="area_id" id="tableAreaId" class="form-select" required>
<label class="form-label fw-bold small text-uppercase">Area / Section</label>
<select name="area_id" id="areaId" class="form-select" required>
<option value="">Select Area</option>
<?php foreach ($areas as $area): ?>
<option value="<?= $area['id'] ?>"><?= htmlspecialchars($area['name']) ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="mb-3">
<label class="form-label">Capacity (Persons)</label>
<input type="number" name="capacity" id="tableCapacity" class="form-control" value="2" min="1">
</div>
<div class="mb-3">
<label class="form-label">Status</label>
<select name="status" id="tableStatus" class="form-select">
<option value="available">Available</option>
<option value="occupied">Occupied</option>
<option value="reserved">Reserved</option>
<option value="inactive">Inactive</option>
</select>
<div class="row">
<div class="col-md-6 mb-3">
<label class="form-label fw-bold small text-uppercase">Capacity</label>
<input type="number" name="capacity" id="tableCapacity" class="form-control" min="1" value="4" required>
</div>
<div class="col-md-6 mb-3">
<label class="form-label fw-bold small text-uppercase">Status</label>
<select name="status" id="tableStatus" class="form-select">
<option value="available">Available</option>
<option value="occupied">Occupied</option>
<option value="reserved">Reserved</option>
<option value="inactive">Inactive</option>
</select>
</div>
</div>
</div>
<div class="modal-footer">
<div class="modal-footer bg-light border-0">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-primary">Save Table</button>
<button type="submit" class="btn btn-primary px-4">Save Table</button>
</div>
</form>
</div>
</div>
</div>
<?php endif; ?>
<!-- QR Modal -->
<div class="modal fade" id="qrModal" tabindex="-1" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Table QR Code</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body text-center p-5">
<h4 id="qrTableNumber" class="mb-4 fw-bold"></h4>
<div id="qrcode" class="mb-4 d-flex justify-content-center"></div>
<p class="text-muted small">Scan this QR to order from this table</p>
<div class="d-grid gap-2">
<button type="button" class="btn btn-outline-primary" onclick="downloadQR()">
<i class="bi bi-download"></i> Download QR
</button>
<button type="button" class="btn btn-outline-secondary" onclick="printQR()">
<i class="bi bi-printer"></i> Print
</button>
</div>
</div>
</div>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/qrcodejs/1.0.0/qrcode.min.js"></script>
<script>
let qrGenerator = null;
const baseUrl = '<?= $baseUrl ?>';
function prepareAddForm() {
document.getElementById('tableModalTitle').innerText = 'Add New Table';
document.getElementById('tableAction').value = 'add_table';
document.getElementById('modalTitle').innerText = 'Add New Table';
document.getElementById('formAction').value = 'add_table';
document.getElementById('tableId').value = '';
document.getElementById('tableNumber').value = '';
document.getElementById('tableAreaId').value = '';
document.getElementById('tableCapacity').value = '2';
document.getElementById('areaId').value = '';
document.getElementById('tableCapacity').value = '4';
document.getElementById('tableStatus').value = 'available';
}
function prepareEditForm(table) {
document.getElementById('tableModalTitle').innerText = 'Edit Table';
document.getElementById('tableAction').value = 'edit_table';
function editTable(table) {
document.getElementById('modalTitle').innerText = 'Edit Table';
document.getElementById('formAction').value = 'edit_table';
document.getElementById('tableId').value = table.id;
document.getElementById('tableNumber').value = table.table_number || '';
document.getElementById('tableAreaId').value = table.area_id || '';
document.getElementById('tableCapacity').value = table.capacity || '2';
document.getElementById('tableStatus').value = table.status || 'available';
}
function showTableQR(id, number) {
document.getElementById('qrTableNumber').innerText = 'Table ' + number;
const qrContainer = document.getElementById('qrcode');
qrContainer.innerHTML = '';
document.getElementById('tableNumber').value = table.table_number;
document.getElementById('areaId').value = table.area_id;
document.getElementById('tableCapacity').value = table.capacity;
document.getElementById('tableStatus').value = table.status;
// Fixed: using table_id as parameter name to match qorder.php expectation
const url = baseUrl + '/qorder.php?table_id=' + id;
qrGenerator = new QRCode(qrContainer, {
text: url,
width: 256,
height: 256,
colorDark: "#000000",
colorLight: "#ffffff",
correctLevel: QRCode.CorrectLevel.H
});
new bootstrap.Modal(document.getElementById('qrModal')).show();
}
function downloadQR() {
const img = document.querySelector('#qrcode img');
if (img) {
const link = document.createElement('a');
link.download = 'table-qr.png';
link.href = img.src;
link.click();
}
}
function printQR() {
const img = document.querySelector('#qrcode img');
if (!img) return;
const win = window.open('', '_blank');
win.document.write('<html><body style="text-align:center;padding:50px;">');
win.document.write('<h1>' + document.getElementById('qrTableNumber').innerText + '</h1>');
win.document.write('<img src="' + img.src + '" style="width:300px;">');
win.document.write('<p style="font-family:sans-serif;margin-top:20px;">Scan to Order</p>');
win.document.write('</body></html>');
win.document.close();
win.print();
win.close();
new bootstrap.Modal(document.getElementById('tableModal')).show();
}
</script>
<?php include 'includes/footer.php'; ?>
<?php include 'includes/footer.php'; ?>
<?php // Actually, footer.php is usually included at the end. Let me fix that. ?>

View File

@ -11,11 +11,11 @@ try {
// 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
SELECT t.id, 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
ORDER BY a.name ASC, table_number ASC
";
$stmt = $pdo->prepare($sql);
$stmt->execute(['outlet_id' => $outlet_id]);

View File

@ -94,25 +94,43 @@ function paginate_query($pdo, $query, $params = [], $default_limit = 20) {
// If limit is -1, fetch all
if ($limit == -1) {
$stmt = $pdo->prepare($query);
$stmt->execute($params);
$data = $stmt->fetchAll();
return [
'data' => $data,
'total_rows' => count($data),
'total_pages' => 1,
'current_page' => 1,
'limit' => -1
];
try {
$stmt = $pdo->prepare($query);
$stmt->execute($params);
$data = $stmt->fetchAll();
return [
'data' => $data,
'total_rows' => count($data),
'total_pages' => 1,
'current_page' => 1,
'limit' => -1
];
} catch (PDOException $e) {
die("Pagination Query Error (All): " . $e->getMessage() . "\nQuery: " . $query);
}
}
// Count total rows using a subquery to handle complex queries safely
// Strip ORDER BY from the query for the count to avoid SQL errors and improve performance
$count_query = preg_replace('/ORDER\s+BY.*$/is', '', $query);
// Use a more robust regex that handles potential trailing semicolons or whitespace
$count_query = preg_replace('/ORDER\s+BY.*?(?=;|$)/is', '', $query);
$count_sql = "SELECT COUNT(*) FROM ($count_query) as count_table";
$stmt = $pdo->prepare($count_sql);
$stmt->execute($params);
$total_rows = $stmt->fetchColumn();
try {
$stmt = $pdo->prepare($count_sql);
$stmt->execute($params);
$total_rows = $stmt->fetchColumn();
} catch (PDOException $e) {
// If stripping ORDER BY failed or caused issues, try with the original query in subquery
try {
$count_sql_fallback = "SELECT COUNT(*) FROM ($query) as count_table";
$stmt = $pdo->prepare($count_sql_fallback);
$stmt->execute($params);
$total_rows = $stmt->fetchColumn();
} catch (PDOException $e2) {
die("Pagination Count Error: " . $e2->getMessage() . "\nSQL: " . $count_sql);
}
}
$total_pages = ceil($total_rows / $limit);
if ($page > $total_pages && $total_pages > 0) $page = $total_pages;
@ -124,9 +142,13 @@ function paginate_query($pdo, $query, $params = [], $default_limit = 20) {
// Add LIMIT and OFFSET
$query_with_limit = $query . " LIMIT " . (int)$limit . " OFFSET " . (int)$offset;
$stmt = $pdo->prepare($query_with_limit);
$stmt->execute($params);
$data = $stmt->fetchAll();
try {
$stmt = $pdo->prepare($query_with_limit);
$stmt->execute($params);
$data = $stmt->fetchAll();
} catch (PDOException $e) {
die("Pagination Data Error: " . $e->getMessage() . "\nSQL: " . $query_with_limit);
}
return [
'data' => $data,
@ -444,4 +466,4 @@ function trigger_auto_backup() {
$stmt->execute();
}
}
}
}