updating outlets

This commit is contained in:
Flatlogic Bot 2026-02-24 15:12:00 +00:00
parent 68b1e34fe9
commit 05a5289cfc
2 changed files with 92 additions and 21 deletions

View File

@ -18,6 +18,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action'])) {
$is_ratable = isset($_POST['is_ratable']) ? 1 : 0;
$commission_rate = (float)($_POST['commission_rate'] ?? 0);
$id = isset($_POST['id']) ? (int)$_POST['id'] : null;
$selected_outlets = $_POST['outlets'] ?? [];
$profile_pic = null;
if ($id) {
@ -43,6 +44,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action'])) {
$message = '<div class="alert alert-danger">Username is required.</div>';
} else {
try {
$pdo->beginTransaction();
if ($action === 'edit_user' && $id) {
if (!has_permission('users_edit') && !has_permission('users_add')) {
$message = '<div class="alert alert-danger">Access Denied: You do not have permission to edit users.</div>';
@ -58,6 +60,16 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action'])) {
$stmt = $pdo->prepare($sql);
$stmt->execute($params);
// Update outlets
$pdo->prepare("DELETE FROM user_outlets WHERE user_id = ?")->execute([$id]);
if (!empty($selected_outlets)) {
$stmt = $pdo->prepare("INSERT INTO user_outlets (user_id, outlet_id) VALUES (?, ?)");
foreach ($selected_outlets as $outlet_id) {
$stmt->execute([$id, (int)$outlet_id]);
}
}
$message = '<div class="alert alert-success">User updated successfully!</div>';
}
} elseif ($action === 'add_user') {
@ -67,10 +79,22 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action'])) {
$password = password_hash($_POST['password'] ?: '123456', PASSWORD_DEFAULT);
$stmt = $pdo->prepare("INSERT INTO users (username, password, full_name, full_name_ar, email, group_id, is_active, is_ratable, profile_pic, commission_rate) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
$stmt->execute([$username, $password, $full_name, $full_name_ar, $email, $group_id, $is_active, $is_ratable, $profile_pic, $commission_rate]);
$new_user_id = $pdo->lastInsertId();
// Update outlets
if (!empty($selected_outlets)) {
$stmt = $pdo->prepare("INSERT INTO user_outlets (user_id, outlet_id) VALUES (?, ?)");
foreach ($selected_outlets as $outlet_id) {
$stmt->execute([$new_user_id, (int)$outlet_id]);
}
}
$message = '<div class="alert alert-success">User created successfully!</div>';
}
}
$pdo->commit();
} catch (PDOException $e) {
$pdo->rollBack();
if ($e->getCode() == 23000) {
$message = '<div class="alert alert-danger">Username or Email already exists.</div>';
} else {
@ -98,6 +122,7 @@ if (isset($_GET['delete'])) {
}
$groups = $pdo->query("SELECT * FROM user_groups ORDER BY name ASC")->fetchAll();
$all_outlets = $pdo->query("SELECT * FROM outlets ORDER BY name ASC")->fetchAll();
$query = "SELECT u.*, g.name as group_name
FROM users u
@ -106,6 +131,13 @@ $query = "SELECT u.*, g.name as group_name
$users_pagination = paginate_query($pdo, $query);
$users = $users_pagination['data'];
// Fetch outlets for each user
foreach ($users as &$user) {
$stmt = $pdo->prepare("SELECT outlet_id FROM user_outlets WHERE user_id = ?");
$stmt->execute([$user['id']]);
$user['outlets'] = $stmt->fetchAll(PDO::FETCH_COLUMN);
}
include 'includes/header.php';
?>
@ -258,6 +290,22 @@ include 'includes/header.php';
<?php endforeach; ?>
</select>
</div>
<div class="mb-3">
<label class="form-label small fw-bold text-muted">ASSIGNED OUTLETS</label>
<div class="bg-light p-3 rounded-3 border-0" style="max-height: 150px; overflow-y: auto;">
<?php foreach ($all_outlets as $outlet): ?>
<div class="form-check">
<input class="form-check-input outlet-checkbox" type="checkbox" name="outlets[]" value="<?= $outlet['id'] ?>" id="outlet_<?= $outlet['id'] ?>">
<label class="form-check-label" for="outlet_<?= $outlet['id'] ?>">
<?= htmlspecialchars($outlet['name']) ?>
</label>
</div>
<?php endforeach; ?>
</div>
<small class="text-muted">Select outlets this user has access to.</small>
</div>
<div class="row g-3 mb-3">
<div class="col-md-12">
<label class="form-label small fw-bold text-muted">COMMISSION RATE (%)</label>
@ -307,6 +355,9 @@ function prepareAddForm() {
document.getElementById('userPassword').required = true;
document.getElementById('userImagePreviewContainer').style.display = 'none';
document.getElementById('userCommissionRate').value = '0.0';
// Uncheck all outlets
document.querySelectorAll('.outlet-checkbox').forEach(cb => cb.checked = false);
}
function prepareEditForm(user) {
@ -326,6 +377,11 @@ function prepareEditForm(user) {
document.getElementById('pwdLabel').style.display = 'none';
document.getElementById('pwdHint').style.display = 'block';
// Set outlets
document.querySelectorAll('.outlet-checkbox').forEach(cb => {
cb.checked = user.outlets && user.outlets.includes(cb.value);
});
if (user.profile_pic) {
const preview = document.getElementById('userImagePreview');
preview.src = '../' + user.profile_pic;
@ -377,4 +433,4 @@ document.getElementById('btnTranslate').addEventListener('click', function() {
</script>
<?php endif; ?>
<?php include 'includes/footer.php'; ?>
<?php include 'includes/footer.php'; ?>

55
pos.php
View File

@ -96,7 +96,7 @@ if (!$loyalty_settings) {
.product-card:active { transform: scale(0.95); }
.product-card:hover { border-color: #0d6efd !important; box-shadow: 0 4px 12px rgba(0,0,0,0.08) !important; }
.category-btn { text-align: left; border: none; background: none; padding: 12px 10px; width: 100%; display: block; border-radius: 12px; color: #64748b; font-weight: 700; transition: all 0.2s; }
.category-btn { text-align: left; border: none; background: none; padding: 10px 12px; width: 100%; display: flex; align-items: center; gap: 10px; border-radius: 12px; color: #64748b; font-weight: 700; transition: all 0.2s; }
.category-btn:hover { background-color: #f1f5f9; color: #0f172a; }
.category-btn.active { background-color: #0d6efd; color: white; box-shadow: 0 4px 6px -1px rgba(13, 110, 253, 0.3); }
.search-dropdown { position: absolute; width: 100%; z-index: 1000; max-height: 200px; overflow-y: auto; display: none; }
@ -150,16 +150,18 @@ if (!$loyalty_settings) {
</a>
<div class="ms-auto d-flex align-items-center gap-2">
<?php if (count($outlets) > 1): ?>
<div class="dropdown me-1 d-none d-md-block">
<button class="btn btn-outline-secondary btn-sm rounded-pill px-3 border-0 bg-light" type="button" data-bs-toggle="dropdown">
<i class="bi bi-shop me-1"></i> Switch
</button>
<ul class="dropdown-menu dropdown-menu-end shadow border-0 mt-2">
<?php foreach ($outlets as $o): ?>
<li><a class="dropdown-item <?= $o['id'] == $outlet_id ? 'active' : '' ?>" href="?outlet_id=<?= $o['id'] ?>"><?= htmlspecialchars($o['name']) ?></a></li>
<li><a class="dropdown-item <?= $o['id'] == $outlet_id ? 'active' : '' ?>" href="?outlet_id=<?= $o['id'] ?>&order_type=<?= $order_type ?>"><?= htmlspecialchars($o['name']) ?></a></li>
<?php endforeach; ?>
</ul>
</div>
<?php endif; ?>
<a href="kitchen.php" class="btn btn-outline-primary btn-sm rounded-pill px-2 px-sm-3 border-0"><i class="bi bi-fire me-1"></i> Kitchen</a>
<a href="admin/orders.php" class="btn btn-outline-secondary btn-sm rounded-pill px-2 px-sm-3 border-0"><i class="bi bi-receipt me-1"></i> Orders</a>
<button class="btn btn-outline-warning btn-sm rounded-pill px-2 px-sm-3 border-0" onclick="openRecallOrderModal()"><i class="bi bi-arrow-counterclockwise me-1"></i> Recall</button>
@ -187,19 +189,19 @@ if (!$loyalty_settings) {
<div class="row g-0 pos-layout">
<!-- Left Sidebar: Categories -->
<div class="col-md-2 col-auto pos-categories scrollable-y p-2" style="min-width: 120px;">
<button class="category-btn active mb-2 px-1 text-center" data-category="all">
<i class="bi bi-grid d-block fs-5 mb-1 text-inherit"></i>
<span style="font-size: 0.75rem;">All Items</span>
<div class="col-md-2 col-auto pos-categories scrollable-y p-2" style="max-width: 170px;">
<button class="category-btn active mb-2" data-category="all">
<i class="bi bi-grid fs-5 text-inherit"></i>
<span style="font-size: 0.85rem; font-weight: 700;">All Items</span>
</button>
<?php foreach ($categories as $cat): ?>
<button class="category-btn mb-2 px-1 text-center" data-category="<?= $cat['id'] ?>">
<button class="category-btn mb-2" data-category="<?= $cat['id'] ?>">
<?php if (!empty($cat['image_url'])): ?>
<img src="<?= htmlspecialchars($cat['image_url']) ?>" alt="<?= htmlspecialchars($cat['name']) ?>" class="rounded-3 d-block mb-1 mx-auto" style="width: 32px; height: 32px; object-fit: cover;">
<img src="<?= htmlspecialchars($cat['image_url']) ?>" alt="<?= htmlspecialchars($cat['name']) ?>" class="rounded-3" style="width: 24px; height: 24px; object-fit: cover; flex-shrink: 0;">
<?php else: ?>
<i class="bi bi-tag d-block fs-5 mb-1 text-inherit"></i>
<i class="bi bi-tag fs-5 text-inherit"></i>
<?php endif; ?>
<span class="d-block text-truncate" style="font-size: 0.75rem;"><?= htmlspecialchars($cat['name']) ?></span>
<span class="text-truncate" style="font-size: 0.85rem; font-weight: 700;"><?= htmlspecialchars($cat['name']) ?></span>
</button>
<?php endforeach; ?>
</div>
@ -208,11 +210,24 @@ if (!$loyalty_settings) {
<div class="col pos-products">
<!-- Search Bar -->
<div class="px-3 py-2 border-bottom bg-white">
<div class="position-relative">
<span class="position-absolute top-50 start-0 translate-middle-y ms-3 text-muted">
<i class="bi bi-search small"></i>
</span>
<input type="text" id="product-search" class="form-control form-control-sm ps-5 border-0 bg-light rounded-3" placeholder="Search products by name or SKU...">
<div class="row g-2 align-items-center">
<div class="col">
<div class="position-relative">
<span class="position-absolute top-50 start-0 translate-middle-y ms-3 text-muted">
<i class="bi bi-search small"></i>
</span>
<input type="text" id="product-search" class="form-control form-control-sm ps-5 border-0 bg-light rounded-3" placeholder="Search products by name or SKU...">
</div>
</div>
<?php if (count($outlets) > 1): ?>
<div class="col-auto">
<select class="form-select form-select-sm border-0 bg-light rounded-3 fw-bold text-primary" style="min-width: 120px;" onchange="location.href='?outlet_id=' + this.value + '&order_type=<?= $order_type ?>'">
<?php foreach ($outlets as $o): ?>
<option value="<?= $o['id'] ?>" <?= $o['id'] == $outlet_id ? 'selected' : '' ?>><?= htmlspecialchars($o['name']) ?></option>
<?php endforeach; ?>
</select>
</div>
<?php endif; ?>
</div>
</div>
@ -258,21 +273,21 @@ if (!$loyalty_settings) {
</div>
<!-- Right Sidebar: Cart -->
<div class="col-md-2 col-12 pos-cart shadow-sm">
<div class="col-md-3 col-12 pos-cart shadow-sm">
<div class="p-2 border-bottom bg-white">
<div class="btn-group w-100 mb-2" role="group">
<input type="radio" class="btn-check" name="order_type" id="ot-takeaway" value="takeaway" <?= $order_type === 'takeaway' ? 'checked' : '' ?>>
<label class="btn btn-outline-primary btn-sm py-1 rounded-start-pill" for="ot-takeaway" style="font-size: 0.7rem;">Takeaway</label>
<label class="btn btn-outline-primary btn-sm py-1 rounded-start-pill" for="ot-takeaway" style="font-size: 0.75rem; font-weight: 700;">Takeaway</label>
<input type="radio" class="btn-check" name="order_type" id="ot-dine-in" value="dine-in" <?= $order_type === 'dine-in' ? 'checked' : '' ?>>
<label class="btn btn-outline-primary btn-sm py-1" for="ot-dine-in" style="font-size: 0.7rem;">Dine-In</label>
<label class="btn btn-outline-primary btn-sm py-1" for="ot-dine-in" style="font-size: 0.75rem; font-weight: 700;">Dine-In</label>
<input type="radio" class="btn-check" name="order_type" id="ot-delivery" value="delivery" <?= $order_type === 'delivery' ? 'checked' : '' ?>>
<label class="btn btn-outline-primary btn-sm py-1 rounded-end-pill" for="ot-delivery" style="font-size: 0.7rem;">Delivery</label>
<label class="btn btn-outline-primary btn-sm py-1 rounded-end-pill" for="ot-delivery" style="font-size: 0.75rem; font-weight: 700;">Delivery</label>
</div>
<div class="position-relative">
<div class="input-group input-group-sm">
<span class="input-group-text bg-light border-0 rounded-start-pill ps-3"><i class="bi bi-person text-muted"></i></span>
<input type="text" class="form-control border-0 bg-light ps-1" id="customer-search" placeholder="Customer Name/Phone" autocomplete="off">
<input type="text" class="form-control border-0 bg-light ps-1" id="customer-search" placeholder="Customer Name/Phone" autocomplete="off" style="font-weight: 600;">
<button class="btn btn-light border-0 d-none" type="button" id="clear-customer"><i class="bi bi-x"></i></button>
<button class="btn btn-light border-0 rounded-end-pill pe-3 text-primary" type="button" data-bs-toggle="modal" data-bs-target="#addCustomerModal"><i class="bi bi-plus-lg"></i></button>
</div>