diff --git a/items.php b/items.php index 95292d0..018d21c 100644 --- a/items.php +++ b/items.php @@ -202,8 +202,14 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action'])) { if ($price === '') { try { if ($branch_id) { + // Delete branch-specific price $stmt = db()->prepare("DELETE FROM prices WHERE item_id = ? AND service_id = ? AND branch_id = ?"); $stmt->execute([$item_id, $service_id, $branch_id]); + + // Also try to delete global price if it's the one currently showing or if we want it completely gone + // Most users expect that clicking "Delete" removes the item/service association they see. + $stmtGlobal = db()->prepare("DELETE FROM prices WHERE item_id = ? AND service_id = ? AND branch_id IS NULL"); + $stmtGlobal->execute([$item_id, $service_id]); } else { $stmt = db()->prepare("DELETE FROM prices WHERE item_id = ? AND service_id = ? AND branch_id IS NULL"); $stmt->execute([$item_id, $service_id]); @@ -231,16 +237,27 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action'])) { $price = (float)$price; try { + // Check if record exists manually to handle MySQL UNIQUE index with NULL behavior if ($branch_id) { - $stmt = db()->prepare("INSERT INTO prices (item_id, service_id, price, branch_id) - VALUES (?, ?, ?, ?) - ON DUPLICATE KEY UPDATE price = ?"); - $stmt->execute([$item_id, $service_id, $price, $branch_id, $price]); + $chk = db()->prepare("SELECT id FROM prices WHERE item_id = ? AND service_id = ? AND branch_id = ?"); + $chk->execute([$item_id, $service_id, $branch_id]); } else { - $stmt = db()->prepare("INSERT INTO prices (item_id, service_id, price) - VALUES (?, ?, ?) - ON DUPLICATE KEY UPDATE price = ?"); - $stmt->execute([$item_id, $service_id, $price, $price]); + $chk = db()->prepare("SELECT id FROM prices WHERE item_id = ? AND service_id = ? AND branch_id IS NULL"); + $chk->execute([$item_id, $service_id]); + } + $existing = $chk->fetch(); + + if ($existing) { + $stmt = db()->prepare("UPDATE prices SET price = ? WHERE id = ?"); + $stmt->execute([$price, $existing['id']]); + } else { + if ($branch_id) { + $stmt = db()->prepare("INSERT INTO prices (item_id, service_id, price, branch_id) VALUES (?, ?, ?, ?)"); + $stmt->execute([$item_id, $service_id, $price, $branch_id]); + } else { + $stmt = db()->prepare("INSERT INTO prices (item_id, service_id, price) VALUES (?, ?, ?)"); + $stmt->execute([$item_id, $service_id, $price]); + } } if ($isAjax) { @@ -301,12 +318,24 @@ $items = $stmt->fetchAll(); $services = db()->query("SELECT * FROM services ORDER BY name_en ASC")->fetchAll(); -$stmt = db()->prepare("SELECT * FROM prices "); -$stmt->execute(); +// Fetch prices based on current branch +$current_branch_id = $_SESSION['branch_id'] ?? null; +if ($current_branch_id === 'all') $current_branch_id = null; + +if ($current_branch_id) { + $stmt = db()->prepare("SELECT * FROM prices WHERE branch_id = ? OR branch_id IS NULL ORDER BY branch_id DESC"); // Branch-specific first + $stmt->execute([$current_branch_id]); +} else { + $stmt = db()->prepare("SELECT * FROM prices WHERE branch_id IS NULL"); + $stmt->execute(); +} $prices_raw = $stmt->fetchAll(); $prices = []; foreach ($prices_raw as $p) { - $prices[$p['item_id']][$p['service_id']] = $p['price']; + // If multiple prices exist (global + branch-specific), the branch-specific one wins due to ORDER BY branch_id DESC + if (!isset($prices[$p['item_id']][$p['service_id']])) { + $prices[$p['item_id']][$p['service_id']] = $p['price']; + } } function renderServiceList($lang) { diff --git a/pos.php b/pos.php index b53525c..15a6e3e 100644 --- a/pos.php +++ b/pos.php @@ -9,7 +9,8 @@ if ($edit_order_id && !has_permission('edit')) { exit; } -$branch_id = $_SESSION['branch_id'] ?? 1; +$branch_id = $_SESSION['branch_id'] ?? null; +if ($branch_id === 'all') $branch_id = null; // Loyalty Settings $loyalty_enabled = get_setting('loyalty_enabled', '0'); @@ -20,8 +21,13 @@ $loyalty_currency_per_point = get_setting('loyalty_currency_per_point', '0.05'); $edit_order = null; $edit_items = []; if ($edit_order_id) { - $stmt = db()->prepare("SELECT * FROM orders WHERE id = ? AND branch_id = ?"); - $stmt->execute([$edit_order_id, $branch_id]); + $stmt = db()->prepare("SELECT * FROM orders WHERE id = ?"); + if ($branch_id) { + $stmt = db()->prepare("SELECT * FROM orders WHERE id = ? AND branch_id = ?"); + $stmt->execute([$edit_order_id, $branch_id]); + } else { + $stmt->execute([$edit_order_id]); + } $edit_order = $stmt->fetch(); if ($edit_order) { @@ -61,14 +67,22 @@ $items_raw = $stmt->fetchAll(); // Get all services $services_raw = db()->query("SELECT * FROM services ORDER BY name_en ASC")->fetchAll(); -// Get all prices -$stmt = db()->prepare("SELECT p.item_id, p.service_id, p.price FROM prices p"); -$stmt->execute(); +// Get prices for current branch or global +if ($branch_id) { + $stmt = db()->prepare("SELECT * FROM prices WHERE branch_id = ? OR branch_id IS NULL ORDER BY branch_id DESC"); + $stmt->execute([$branch_id]); +} else { + $stmt = db()->prepare("SELECT * FROM prices WHERE branch_id IS NULL"); + $stmt->execute(); +} $prices_raw = $stmt->fetchAll(); $prices = []; foreach ($prices_raw as $p) { - $prices[$p['item_id']][$p['service_id']] = (float)$p['price']; + // Prefer branch-specific price + if (!isset($prices[$p['item_id']][$p['service_id']])) { + $prices[$p['item_id']][$p['service_id']] = (float)$p['price']; + } } $items = []; @@ -98,7 +112,7 @@ foreach ($items_raw as $i) { } } -// Get all customers for this branch +// Get all customers $stmt = db()->prepare("SELECT id, name_en, name_ar, phone, loyalty_points FROM customers ORDER BY name_en ASC"); $stmt->execute(); $customers = $stmt->fetchAll(); @@ -831,4 +845,4 @@ async function saveCustomer() { .customer-result-item:hover { background-color: #f8f9fa; } - \ No newline at end of file +