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 FROM tables t
LEFT JOIN areas a ON t.area_id = a.id LEFT JOIN areas a ON t.area_id = a.id
WHERE t.is_deleted = 0 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_pagination = paginate_query($pdo, $query);
$tables = $tables_pagination['data']; $tables = $tables_pagination['data'];
@ -98,219 +98,168 @@ $baseUrl = $protocol . $host . $project_root;
<?= $message ?> <?= $message ?>
<div class="card border-0 shadow-sm"> <div class="card border-0 shadow-sm rounded-3">
<div class="card-body p-0"> <div class="card-body p-0">
<!-- Pagination Controls --> <?php render_pagination_controls($tables_pagination); ?>
<div class="p-3 border-bottom bg-light">
<?php render_pagination_controls($tables_pagination); ?>
</div>
<div class="table-responsive"> <div class="table-responsive">
<table class="table table-hover align-middle mb-0"> <table class="table table-hover align-middle mb-0">
<thead class="bg-light"> <thead class="bg-light">
<tr> <tr>
<th class="ps-4">ID</th> <th class="ps-4">QR Code</th>
<th>Table Number</th> <th>Table #</th>
<th>Area</th> <th>Area</th>
<th>Capacity</th> <th>Capacity</th>
<th>Status</th> <th>Status</th>
<th>QR Code</th>
<th class="text-end pe-4">Actions</th> <th class="text-end pe-4">Actions</th>
</tr> </tr>
</thead> </thead>
<tbody> <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): ?> <?php foreach ($tables as $table): ?>
<tr> <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 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> <td>
<?php if ($table['status'] === 'available'): ?> <span class="badge bg-secondary bg-opacity-10 text-secondary border border-secondary border-opacity-25">
<span class="badge bg-success-subtle text-success px-3">Available</span> <?= htmlspecialchars($table['area_name'] ?? 'No Area') ?>
<?php elseif ($table['status'] === 'occupied'): ?> </span>
<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; ?>
</td> </td>
<td> <td>
<button type="button" class="btn btn-sm btn-outline-info" <i class="bi bi-people me-1 text-muted"></i>
onclick="showTableQR('<?= $table['id'] ?>', '<?= htmlspecialchars($table['table_number']) ?>')"> <?= htmlspecialchars((string)$table['capacity']) ?>
<i class="bi bi-qr-code"></i> View QR </td>
</button> <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>
<td class="text-end pe-4"> <td class="text-end pe-4">
<?php if (has_permission('tables_add')): ?> <div class="btn-group btn-group-sm">
<button type="button" class="btn btn-sm btn-outline-primary me-1" <?php if (has_permission('tables_add')): ?>
data-bs-toggle="modal" data-bs-target="#tableModal" <button class="btn btn-outline-primary" onclick='editTable(<?= json_encode($table) ?>)'>
onclick='prepareEditForm(<?= htmlspecialchars(json_encode($table), ENT_QUOTES, "UTF-8") ?>)' title="Edit"><i class="bi bi-pencil"></i></button> <i class="bi bi-pencil"></i>
<?php endif; ?> </button>
<?php endif; ?>
<?php if (has_permission('tables_del')): ?> <?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> <a href="?delete=<?= $table['id'] ?>" class="btn btn-outline-danger" onclick="return confirm('Are you sure you want to remove this table?')">
<?php endif; ?> <i class="bi bi-trash"></i>
</a>
<?php endif; ?>
</div>
</td> </td>
</tr> </tr>
<?php endforeach; ?> <?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> </tbody>
</table> </table>
</div> </div>
<!-- Bottom Pagination --> <?php render_pagination_controls($tables_pagination); ?>
<div class="p-3 border-top bg-light">
<?php render_pagination_controls($tables_pagination); ?>
</div>
</div> </div>
</div> </div>
<!-- Table Modal --> <!-- Add/Edit Table Modal -->
<?php if (has_permission('tables_add')): ?> <div class="modal fade" id="tableModal" tabindex="-1">
<div class="modal fade" id="tableModal" tabindex="-1" aria-hidden="true">
<div class="modal-dialog"> <div class="modal-dialog">
<div class="modal-content"> <div class="modal-content border-0 shadow">
<div class="modal-header bg-primary text-white"> <div class="modal-header">
<h5 class="modal-title" id="tableModalTitle">Add New Table</h5> <h5 class="modal-title fw-bold" id="modalTitle">Add New Table</h5>
<button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button> <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
</div> </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"> <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"> <div class="mb-3">
<label class="form-label">Table Number <span class="text-danger">*</span></label> <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. T1"> <input type="text" name="table_number" id="tableNumber" class="form-control" required placeholder="e.g. Table 1, Booth A">
</div> </div>
<div class="mb-3"> <div class="mb-3">
<label class="form-label">Area <span class="text-danger">*</span></label> <label class="form-label fw-bold small text-uppercase">Area / Section</label>
<select name="area_id" id="tableAreaId" class="form-select" required> <select name="area_id" id="areaId" class="form-select" required>
<option value="">Select Area</option> <option value="">Select Area</option>
<?php foreach ($areas as $area): ?> <?php foreach ($areas as $area): ?>
<option value="<?= $area['id'] ?>"><?= htmlspecialchars($area['name']) ?></option> <option value="<?= $area['id'] ?>"><?= htmlspecialchars($area['name']) ?></option>
<?php endforeach; ?> <?php endforeach; ?>
</select> </select>
</div> </div>
<div class="mb-3"> <div class="row">
<label class="form-label">Capacity (Persons)</label> <div class="col-md-6 mb-3">
<input type="number" name="capacity" id="tableCapacity" class="form-control" value="2" min="1"> <label class="form-label fw-bold small text-uppercase">Capacity</label>
</div> <input type="number" name="capacity" id="tableCapacity" class="form-control" min="1" value="4" required>
<div class="mb-3"> </div>
<label class="form-label">Status</label> <div class="col-md-6 mb-3">
<select name="status" id="tableStatus" class="form-select"> <label class="form-label fw-bold small text-uppercase">Status</label>
<option value="available">Available</option> <select name="status" id="tableStatus" class="form-select">
<option value="occupied">Occupied</option> <option value="available">Available</option>
<option value="reserved">Reserved</option> <option value="occupied">Occupied</option>
<option value="inactive">Inactive</option> <option value="reserved">Reserved</option>
</select> <option value="inactive">Inactive</option>
</select>
</div>
</div> </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="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> </div>
</form> </form>
</div> </div>
</div> </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> <script>
let qrGenerator = null;
const baseUrl = '<?= $baseUrl ?>';
function prepareAddForm() { function prepareAddForm() {
document.getElementById('tableModalTitle').innerText = 'Add New Table'; document.getElementById('modalTitle').innerText = 'Add New Table';
document.getElementById('tableAction').value = 'add_table'; document.getElementById('formAction').value = 'add_table';
document.getElementById('tableId').value = ''; document.getElementById('tableId').value = '';
document.getElementById('tableNumber').value = ''; document.getElementById('tableNumber').value = '';
document.getElementById('tableAreaId').value = ''; document.getElementById('areaId').value = '';
document.getElementById('tableCapacity').value = '2'; document.getElementById('tableCapacity').value = '4';
document.getElementById('tableStatus').value = 'available'; document.getElementById('tableStatus').value = 'available';
} }
function prepareEditForm(table) { function editTable(table) {
document.getElementById('tableModalTitle').innerText = 'Edit Table'; document.getElementById('modalTitle').innerText = 'Edit Table';
document.getElementById('tableAction').value = 'edit_table'; document.getElementById('formAction').value = 'edit_table';
document.getElementById('tableId').value = table.id; document.getElementById('tableId').value = table.id;
document.getElementById('tableNumber').value = table.table_number || ''; document.getElementById('tableNumber').value = table.table_number;
document.getElementById('tableAreaId').value = table.area_id || ''; document.getElementById('areaId').value = table.area_id;
document.getElementById('tableCapacity').value = table.capacity || '2'; document.getElementById('tableCapacity').value = table.capacity;
document.getElementById('tableStatus').value = table.status || 'available'; document.getElementById('tableStatus').value = table.status;
}
function showTableQR(id, number) {
document.getElementById('qrTableNumber').innerText = 'Table ' + number;
const qrContainer = document.getElementById('qrcode');
qrContainer.innerHTML = '';
// Fixed: using table_id as parameter name to match qorder.php expectation new bootstrap.Modal(document.getElementById('tableModal')).show();
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();
} }
</script> </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 // Fetch all tables with their area names, filtered by outlet_id
$sql = " $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 FROM tables t
LEFT JOIN areas a ON t.area_id = a.id LEFT JOIN areas a ON t.area_id = a.id
WHERE a.outlet_id = :outlet_id AND t.is_deleted = 0 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 = $pdo->prepare($sql);
$stmt->execute(['outlet_id' => $outlet_id]); $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 is -1, fetch all
if ($limit == -1) { if ($limit == -1) {
$stmt = $pdo->prepare($query); try {
$stmt->execute($params); $stmt = $pdo->prepare($query);
$data = $stmt->fetchAll(); $stmt->execute($params);
return [ $data = $stmt->fetchAll();
'data' => $data, return [
'total_rows' => count($data), 'data' => $data,
'total_pages' => 1, 'total_rows' => count($data),
'current_page' => 1, 'total_pages' => 1,
'limit' => -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 // 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 // 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"; $count_sql = "SELECT COUNT(*) FROM ($count_query) as count_table";
$stmt = $pdo->prepare($count_sql);
$stmt->execute($params); try {
$total_rows = $stmt->fetchColumn(); $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); $total_pages = ceil($total_rows / $limit);
if ($page > $total_pages && $total_pages > 0) $page = $total_pages; 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 // Add LIMIT and OFFSET
$query_with_limit = $query . " LIMIT " . (int)$limit . " OFFSET " . (int)$offset; $query_with_limit = $query . " LIMIT " . (int)$limit . " OFFSET " . (int)$offset;
$stmt = $pdo->prepare($query_with_limit); try {
$stmt->execute($params); $stmt = $pdo->prepare($query_with_limit);
$data = $stmt->fetchAll(); $stmt->execute($params);
$data = $stmt->fetchAll();
} catch (PDOException $e) {
die("Pagination Data Error: " . $e->getMessage() . "\nSQL: " . $query_with_limit);
}
return [ return [
'data' => $data, 'data' => $data,
@ -444,4 +466,4 @@ function trigger_auto_backup() {
$stmt->execute(); $stmt->execute();
} }
} }
} }