modify pos
This commit is contained in:
parent
84add80342
commit
57a6e7ed99
49
items.php
49
items.php
@ -183,16 +183,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action'])) {
|
||||
$service_id = $_POST['service_id'];
|
||||
$price = $_POST['price'];
|
||||
|
||||
// Determine branch_id from session (if available)
|
||||
$branch_id = $_SESSION['branch_id'] ?? null;
|
||||
if ($branch_id === 'all') $branch_id = null;
|
||||
|
||||
// Final sanity check for branch_id to avoid FK errors if session has stale ID
|
||||
if ($branch_id) {
|
||||
$chk = db()->prepare("SELECT id FROM branches WHERE id = ?");
|
||||
$chk->execute([$branch_id]);
|
||||
if (!$chk->fetch()) $branch_id = null;
|
||||
}
|
||||
|
||||
|
||||
// Handle deletion if price is empty
|
||||
if ($price === '') {
|
||||
@ -224,27 +215,17 @@ 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) {
|
||||
$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 {
|
||||
$chk = db()->prepare("SELECT id FROM prices WHERE item_id = ? AND service_id = ? AND branch_id IS NULL");
|
||||
$chk->execute([$item_id, $service_id]);
|
||||
}
|
||||
// Prices are globally shared
|
||||
$chk = db()->prepare("SELECT id FROM prices WHERE item_id = ? AND service_id = ?");
|
||||
$chk->execute([$item_id, $service_id]);
|
||||
$existing = $chk->fetch();
|
||||
|
||||
if ($existing) {
|
||||
$stmt = db()->prepare("UPDATE prices SET price = ? WHERE id = ?");
|
||||
$stmt = db()->prepare("UPDATE prices SET price = ?, branch_id = NULL 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]);
|
||||
}
|
||||
$stmt = db()->prepare("INSERT INTO prices (item_id, service_id, price, branch_id) VALUES (?, ?, ?, NULL)");
|
||||
$stmt->execute([$item_id, $service_id, $price]);
|
||||
}
|
||||
|
||||
if ($isAjax) {
|
||||
@ -302,21 +283,13 @@ $stmt = db()->prepare("SELECT * FROM items $where_sql ORDER BY name_en ASC");
|
||||
$stmt->execute($params);
|
||||
$items = $stmt->fetchAll();
|
||||
|
||||
// Fetch prices
|
||||
if ($current_branch_id && $current_branch_id !== 'all') {
|
||||
$stmt = db()->prepare("SELECT * FROM prices WHERE branch_id = ? OR branch_id IS NULL ORDER BY branch_id DESC");
|
||||
$stmt->execute([$current_branch_id]);
|
||||
} else {
|
||||
$stmt = db()->prepare("SELECT * FROM prices WHERE branch_id IS NULL");
|
||||
$stmt->execute();
|
||||
}
|
||||
// Fetch prices (globally shared)
|
||||
$stmt = db()->prepare("SELECT * FROM prices");
|
||||
$stmt->execute();
|
||||
$prices_raw = $stmt->fetchAll();
|
||||
$prices = [];
|
||||
foreach ($prices_raw as $p) {
|
||||
// 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'];
|
||||
}
|
||||
$prices[$p['item_id']][$p['service_id']] = $p['price'];
|
||||
}
|
||||
|
||||
function renderCategoryList($lang) {
|
||||
|
||||
16
pos.php
16
pos.php
@ -68,22 +68,14 @@ $items_raw = $stmt->fetchAll();
|
||||
// Get all services
|
||||
$services_raw = db()->query("SELECT * FROM services WHERE is_deleted = 0 ORDER BY name_en ASC")->fetchAll();
|
||||
|
||||
// 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();
|
||||
}
|
||||
// Get all prices (globally shared)
|
||||
$stmt = db()->prepare("SELECT * FROM prices");
|
||||
$stmt->execute();
|
||||
$prices_raw = $stmt->fetchAll();
|
||||
|
||||
$prices = [];
|
||||
foreach ($prices_raw as $p) {
|
||||
// Prefer branch-specific price
|
||||
if (!isset($prices[$p['item_id']][$p['service_id']])) {
|
||||
$prices[$p['item_id']][$p['service_id']] = (float)$p['price'];
|
||||
}
|
||||
$prices[$p['item_id']][$p['service_id']] = (float)$p['price'];
|
||||
}
|
||||
|
||||
$items = [];
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user