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'];
|
$service_id = $_POST['service_id'];
|
||||||
$price = $_POST['price'];
|
$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
|
// Handle deletion if price is empty
|
||||||
if ($price === '') {
|
if ($price === '') {
|
||||||
@ -224,27 +215,17 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action'])) {
|
|||||||
$price = (float)$price;
|
$price = (float)$price;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Check if record exists manually to handle MySQL UNIQUE index with NULL behavior
|
// Prices are globally shared
|
||||||
if ($branch_id) {
|
$chk = db()->prepare("SELECT id FROM prices WHERE item_id = ? AND service_id = ?");
|
||||||
$chk = db()->prepare("SELECT id FROM prices WHERE item_id = ? AND service_id = ? AND branch_id = ?");
|
$chk->execute([$item_id, $service_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]);
|
|
||||||
}
|
|
||||||
$existing = $chk->fetch();
|
$existing = $chk->fetch();
|
||||||
|
|
||||||
if ($existing) {
|
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']]);
|
$stmt->execute([$price, $existing['id']]);
|
||||||
} else {
|
} else {
|
||||||
if ($branch_id) {
|
$stmt = db()->prepare("INSERT INTO prices (item_id, service_id, price, branch_id) VALUES (?, ?, ?, NULL)");
|
||||||
$stmt = db()->prepare("INSERT INTO prices (item_id, service_id, price, branch_id) VALUES (?, ?, ?, ?)");
|
$stmt->execute([$item_id, $service_id, $price]);
|
||||||
$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) {
|
if ($isAjax) {
|
||||||
@ -302,21 +283,13 @@ $stmt = db()->prepare("SELECT * FROM items $where_sql ORDER BY name_en ASC");
|
|||||||
$stmt->execute($params);
|
$stmt->execute($params);
|
||||||
$items = $stmt->fetchAll();
|
$items = $stmt->fetchAll();
|
||||||
|
|
||||||
// Fetch prices
|
// Fetch prices (globally shared)
|
||||||
if ($current_branch_id && $current_branch_id !== 'all') {
|
$stmt = db()->prepare("SELECT * FROM prices");
|
||||||
$stmt = db()->prepare("SELECT * FROM prices WHERE branch_id = ? OR branch_id IS NULL ORDER BY branch_id DESC");
|
$stmt->execute();
|
||||||
$stmt->execute([$current_branch_id]);
|
|
||||||
} else {
|
|
||||||
$stmt = db()->prepare("SELECT * FROM prices WHERE branch_id IS NULL");
|
|
||||||
$stmt->execute();
|
|
||||||
}
|
|
||||||
$prices_raw = $stmt->fetchAll();
|
$prices_raw = $stmt->fetchAll();
|
||||||
$prices = [];
|
$prices = [];
|
||||||
foreach ($prices_raw as $p) {
|
foreach ($prices_raw as $p) {
|
||||||
// If multiple prices exist (global + branch-specific), the branch-specific one wins due to ORDER BY branch_id DESC
|
$prices[$p['item_id']][$p['service_id']] = $p['price'];
|
||||||
if (!isset($prices[$p['item_id']][$p['service_id']])) {
|
|
||||||
$prices[$p['item_id']][$p['service_id']] = $p['price'];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function renderCategoryList($lang) {
|
function renderCategoryList($lang) {
|
||||||
|
|||||||
16
pos.php
16
pos.php
@ -68,22 +68,14 @@ $items_raw = $stmt->fetchAll();
|
|||||||
// Get all services
|
// Get all services
|
||||||
$services_raw = db()->query("SELECT * FROM services WHERE is_deleted = 0 ORDER BY name_en ASC")->fetchAll();
|
$services_raw = db()->query("SELECT * FROM services WHERE is_deleted = 0 ORDER BY name_en ASC")->fetchAll();
|
||||||
|
|
||||||
// Get prices for current branch or global
|
// Get all prices (globally shared)
|
||||||
if ($branch_id) {
|
$stmt = db()->prepare("SELECT * FROM prices");
|
||||||
$stmt = db()->prepare("SELECT * FROM prices WHERE branch_id = ? OR branch_id IS NULL ORDER BY branch_id DESC");
|
$stmt->execute();
|
||||||
$stmt->execute([$branch_id]);
|
|
||||||
} else {
|
|
||||||
$stmt = db()->prepare("SELECT * FROM prices WHERE branch_id IS NULL");
|
|
||||||
$stmt->execute();
|
|
||||||
}
|
|
||||||
$prices_raw = $stmt->fetchAll();
|
$prices_raw = $stmt->fetchAll();
|
||||||
|
|
||||||
$prices = [];
|
$prices = [];
|
||||||
foreach ($prices_raw as $p) {
|
foreach ($prices_raw as $p) {
|
||||||
// Prefer branch-specific price
|
$prices[$p['item_id']][$p['service_id']] = (float)$p['price'];
|
||||||
if (!isset($prices[$p['item_id']][$p['service_id']])) {
|
|
||||||
$prices[$p['item_id']][$p['service_id']] = (float)$p['price'];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$items = [];
|
$items = [];
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user