Members request items from the shop. Their points and the stock are earmarked but not taken until someone with shop.manage approves. Approving books a negative "Lumishop" entry in the Lumipunkte history and takes the stock. For warehouse-backed items it also books a withdrawal to the member in the warehouse. Approved orders are then marked as handed over in the game, or can be reversed, which restores points, stock and the warehouse withdrawal. The management page handles categories and items (price, description, stock). Stock is either the shop's own count, unlimited, or live from a linked warehouse item. Items in stock can be imported from the warehouse, and an item's own stock can be moved into the warehouse to link it there. Warehouse cards show when an item is sold in the shop. Shop bookings can't be deleted from the Lumipunkte page, only reversed in the shop, so orders and points stay in sync. The new permissions go to the Administrator group only, so the shop stays hidden until it is assigned. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
255 lines
12 KiB
PHP
255 lines
12 KiB
PHP
<?php
|
|
require_once __DIR__ . '/includes/bootstrap.php';
|
|
require_once __DIR__ . '/includes/shop.php';
|
|
require_permission('shop.manage');
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
|
redirect('/shop_manage.php');
|
|
}
|
|
verify_csrf();
|
|
|
|
$action = (string) ($_POST['action'] ?? '');
|
|
$userId = (int) current_user()['id'];
|
|
$pdo = db();
|
|
|
|
/** Zurueck in den Dialog, mit Fehlermeldung und den bisherigen Eingaben. */
|
|
function shop_item_fail(string $message): void
|
|
{
|
|
if (db()->inTransaction()) {
|
|
db()->rollBack();
|
|
}
|
|
$_SESSION['shop_item_form'] = [
|
|
'id' => (int) ($_POST['item_id'] ?? 0),
|
|
'name' => (string) ($_POST['name'] ?? ''),
|
|
'category_id' => (string) ($_POST['category_id'] ?? ''),
|
|
'description' => (string) ($_POST['description'] ?? ''),
|
|
'price' => (string) ($_POST['price'] ?? ''),
|
|
'stock_mode' => (string) ($_POST['stock_mode'] ?? 'manual'),
|
|
'stock' => (string) ($_POST['stock'] ?? '0'),
|
|
'warehouse_item' => (string) ($_POST['warehouse_item'] ?? ''),
|
|
'active' => !empty($_POST['active']),
|
|
];
|
|
set_flash('error', $message);
|
|
redirect('/shop_manage.php');
|
|
}
|
|
|
|
function shop_manage_done(string $type, string $message, string $anchor = ''): void
|
|
{
|
|
set_flash($type, $message);
|
|
redirect('/shop_manage.php' . $anchor);
|
|
}
|
|
|
|
/** Kategorie-ID aus dem Formular, null = ohne Kategorie; unbekannte IDs zaehlen als ohne. */
|
|
function shop_read_category(): ?int
|
|
{
|
|
$id = (int) ($_POST['category_id'] ?? 0);
|
|
if ($id <= 0) {
|
|
return null;
|
|
}
|
|
$stmt = db()->prepare('SELECT id FROM shop_categories WHERE id = ?');
|
|
$stmt->execute([$id]);
|
|
return $stmt->fetchColumn() !== false ? $id : null;
|
|
}
|
|
|
|
function shop_find_warehouse_item(string $name): ?array
|
|
{
|
|
if (trim($name) === '') {
|
|
return null;
|
|
}
|
|
$stmt = db()->prepare('SELECT id, name, category FROM warehouse_items WHERE name = ?');
|
|
$stmt->execute([trim($name)]);
|
|
return $stmt->fetch() ?: null;
|
|
}
|
|
|
|
$itemId = (int) ($_POST['item_id'] ?? 0);
|
|
|
|
switch ($action) {
|
|
// ---- Kategorie anlegen / umbenennen ----
|
|
case 'category_save':
|
|
$name = trim((string) ($_POST['name'] ?? ''));
|
|
if ($name === '' || mb_strlen($name) > 80) {
|
|
shop_manage_done('error', 'Bitte einen Kategorienamen mit höchstens 80 Zeichen angeben.');
|
|
}
|
|
$sort = (int) ($_POST['sort_order'] ?? 0);
|
|
$categoryId = (int) ($_POST['category_id'] ?? 0);
|
|
if ($categoryId > 0) {
|
|
$pdo->prepare('UPDATE shop_categories SET name = ?, sort_order = ? WHERE id = ?')->execute([$name, $sort, $categoryId]);
|
|
shop_manage_done('success', 'Kategorie „' . $name . '“ gespeichert.');
|
|
}
|
|
$pdo->prepare('INSERT INTO shop_categories (name, sort_order) VALUES (?, ?)')->execute([$name, $sort]);
|
|
shop_manage_done('success', 'Kategorie „' . $name . '“ angelegt.');
|
|
|
|
case 'category_delete':
|
|
$pdo->prepare('DELETE FROM shop_categories WHERE id = ?')->execute([(int) ($_POST['category_id'] ?? 0)]);
|
|
shop_manage_done('success', 'Kategorie gelöscht.');
|
|
|
|
// ---- Gegenstand anlegen / bearbeiten ----
|
|
case 'item_save':
|
|
$name = trim((string) ($_POST['name'] ?? ''));
|
|
if ($name === '' || mb_strlen($name) > 150) {
|
|
shop_item_fail('Bitte einen Namen mit höchstens 150 Zeichen angeben.');
|
|
}
|
|
$description = trim((string) ($_POST['description'] ?? ''));
|
|
if (mb_strlen($description) > 500) {
|
|
shop_item_fail('Die Beschreibung darf höchstens 500 Zeichen lang sein.');
|
|
}
|
|
$price = filter_var($_POST['price'] ?? '', FILTER_VALIDATE_INT);
|
|
if ($price === false || $price < 0 || $price > SHOP_MAX_PRICE) {
|
|
shop_item_fail('Bitte einen Preis zwischen 0 und ' . number_format(SHOP_MAX_PRICE, 0, ',', '.') . ' Lumipunkten angeben.');
|
|
}
|
|
$mode = (string) ($_POST['stock_mode'] ?? 'manual');
|
|
if (!isset(SHOP_STOCK_MODES[$mode])) {
|
|
$mode = 'manual';
|
|
}
|
|
$stock = 0;
|
|
if ($mode === 'manual') {
|
|
$stock = filter_var($_POST['stock'] ?? '', FILTER_VALIDATE_INT);
|
|
if ($stock === false || $stock < 0 || $stock > SHOP_MAX_STOCK) {
|
|
shop_item_fail('Bitte eine Stückzahl zwischen 0 und ' . number_format(SHOP_MAX_STOCK, 0, ',', '.') . ' angeben.');
|
|
}
|
|
}
|
|
|
|
// Lagerhaus-Verknuepfung: ausdruecklich angegeben oder - fuer das Bild - gleicher Name.
|
|
$warehouseName = trim((string) ($_POST['warehouse_item'] ?? ''));
|
|
$warehouse = shop_find_warehouse_item($warehouseName !== '' ? $warehouseName : $name);
|
|
if ($warehouseName !== '' && !$warehouse) {
|
|
shop_item_fail('„' . $warehouseName . '“ gibt es im Lagerhaus nicht. Bitte einen Namen aus der Vorschlagsliste wählen.');
|
|
}
|
|
if ($mode === 'warehouse' && !$warehouse) {
|
|
shop_item_fail('Für den Bestand aus dem Lagerhaus bitte angeben, welcher Lagerhaus-Gegenstand gemeint ist.');
|
|
}
|
|
if ($mode === 'warehouse') {
|
|
$dup = $pdo->prepare("SELECT name FROM shop_items WHERE warehouse_item_id = ? AND stock_mode = 'warehouse' AND id <> ?");
|
|
$dup->execute([(int) $warehouse['id'], $itemId]);
|
|
$dupName = $dup->fetchColumn();
|
|
if ($dupName !== false) {
|
|
shop_item_fail('„' . $warehouse['name'] . '“ aus dem Lagerhaus ist schon mit „' . $dupName . '“ im Shop verknüpft.');
|
|
}
|
|
}
|
|
|
|
$values = [
|
|
shop_read_category(), $name, $description !== '' ? $description : null, (int) $price, $mode, (int) $stock,
|
|
$warehouse ? (int) $warehouse['id'] : null, !empty($_POST['active']) ? 1 : 0,
|
|
];
|
|
if ($itemId > 0) {
|
|
$stmt = $pdo->prepare(
|
|
'UPDATE shop_items SET category_id = ?, name = ?, description = ?, price = ?, stock_mode = ?, stock = ?,
|
|
warehouse_item_id = ?, active = ?, updated_at = NOW() WHERE id = ?'
|
|
);
|
|
$stmt->execute([...$values, $itemId]);
|
|
shop_manage_done('success', '„' . $name . '“ gespeichert.');
|
|
}
|
|
$pdo->prepare(
|
|
'INSERT INTO shop_items (category_id, name, description, price, stock_mode, stock, warehouse_item_id, active, created_by)
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)'
|
|
)->execute([...$values, $userId]);
|
|
shop_manage_done('success', '„' . $name . '“ angelegt.');
|
|
|
|
case 'item_toggle':
|
|
$pdo->prepare('UPDATE shop_items SET active = 1 - active, updated_at = NOW() WHERE id = ?')->execute([$itemId]);
|
|
$item = shop_item($itemId);
|
|
shop_manage_done('success', $item ? '„' . $item['name'] . '“ ' . ((int) $item['active'] ? 'ist wieder im Angebot.' : 'aus dem Angebot genommen.') : 'Gespeichert.');
|
|
|
|
case 'item_delete':
|
|
$item = shop_item($itemId);
|
|
if (!$item) {
|
|
shop_manage_done('error', 'Diesen Gegenstand gibt es nicht mehr.');
|
|
}
|
|
// Offene Antraege brauchen den Gegenstand noch (Bestand beim Bewilligen/Stornieren).
|
|
$open = $pdo->prepare("SELECT COUNT(*) FROM shop_orders WHERE item_id = ? AND status IN ('pending', 'approved')");
|
|
$open->execute([$itemId]);
|
|
if ((int) $open->fetchColumn() > 0) {
|
|
shop_manage_done('error', 'Für „' . $item['name'] . '“ gibt es noch offene oder nicht übergebene Anträge. Bitte erst diese abschließen - oder den Gegenstand nur aus dem Angebot nehmen.');
|
|
}
|
|
$pdo->prepare('DELETE FROM shop_items WHERE id = ?')->execute([$itemId]);
|
|
shop_manage_done('success', '„' . $item['name'] . '“ gelöscht.');
|
|
|
|
// ---- Lagerhaus -> Shop: Gegenstaende mit Lagerbestand uebernehmen ----
|
|
case 'import_warehouse':
|
|
$ids = array_values(array_unique(array_filter(array_map('intval', (array) ($_POST['warehouse_ids'] ?? [])))));
|
|
if (empty($ids)) {
|
|
shop_manage_done('error', 'Bitte mindestens einen Gegenstand auswählen.');
|
|
}
|
|
$prices = (array) ($_POST['prices'] ?? []);
|
|
$categoryId = shop_read_category();
|
|
|
|
$pdo->beginTransaction();
|
|
$find = $pdo->prepare("SELECT id, name FROM warehouse_items WHERE id = ? AND category <> 'currency'");
|
|
$linked = $pdo->prepare("SELECT 1 FROM shop_items WHERE warehouse_item_id = ? AND stock_mode = 'warehouse'");
|
|
$insert = $pdo->prepare(
|
|
"INSERT INTO shop_items (category_id, name, price, stock_mode, warehouse_item_id, active, created_by)
|
|
VALUES (?, ?, ?, 'warehouse', ?, 1, ?)"
|
|
);
|
|
$added = [];
|
|
foreach ($ids as $warehouseId) {
|
|
$price = filter_var($prices[$warehouseId] ?? '', FILTER_VALIDATE_INT);
|
|
$find->execute([$warehouseId]);
|
|
$warehouse = $find->fetch();
|
|
if (!$warehouse) {
|
|
continue;
|
|
}
|
|
if ($price === false || $price < 0 || $price > SHOP_MAX_PRICE) {
|
|
$pdo->rollBack();
|
|
shop_manage_done('error', 'Bitte für „' . $warehouse['name'] . '“ einen Preis zwischen 0 und ' . number_format(SHOP_MAX_PRICE, 0, ',', '.') . ' angeben.');
|
|
}
|
|
$linked->execute([$warehouseId]);
|
|
if ($linked->fetchColumn()) {
|
|
continue; // schon im Shop
|
|
}
|
|
$insert->execute([$categoryId, $warehouse['name'], (int) $price, $warehouseId, $userId]);
|
|
$added[] = $warehouse['name'];
|
|
}
|
|
$pdo->commit();
|
|
|
|
shop_manage_done('success', count($added) === 1
|
|
? '„' . $added[0] . '“ in den Shop übernommen.'
|
|
: count($added) . ' Gegenstände in den Shop übernommen.');
|
|
|
|
// ---- Shop -> Lagerhaus: eigenen Bestand einlagern und kuenftig aus dem Lagerhaus fuehren ----
|
|
case 'to_warehouse':
|
|
$pdo->beginTransaction();
|
|
$stmt = $pdo->prepare('SELECT * FROM shop_items WHERE id = ? FOR UPDATE');
|
|
$stmt->execute([$itemId]);
|
|
$item = $stmt->fetch();
|
|
if (!$item || $item['stock_mode'] !== 'manual') {
|
|
$pdo->rollBack();
|
|
shop_manage_done('error', 'Dieser Gegenstand führt keinen eigenen Bestand im Shop.');
|
|
}
|
|
|
|
// Verknuepften Lagerhaus-Gegenstand nehmen, sonst einen mit gleichem Namen, sonst neu anlegen.
|
|
$warehouseId = (int) ($item['warehouse_item_id'] ?? 0);
|
|
if ($warehouseId === 0) {
|
|
$warehouse = shop_find_warehouse_item((string) $item['name']);
|
|
if ($warehouse) {
|
|
$warehouseId = (int) $warehouse['id'];
|
|
} else {
|
|
$pdo->prepare("INSERT INTO warehouse_items (name, category, source, created_by) VALUES (?, 'other', 'custom', ?)")
|
|
->execute([$item['name'], $userId]);
|
|
$warehouseId = (int) $pdo->lastInsertId();
|
|
}
|
|
}
|
|
$dup = $pdo->prepare("SELECT name FROM shop_items WHERE warehouse_item_id = ? AND stock_mode = 'warehouse' AND id <> ?");
|
|
$dup->execute([$warehouseId, $itemId]);
|
|
$dupName = $dup->fetchColumn();
|
|
if ($dupName !== false) {
|
|
$pdo->rollBack();
|
|
shop_manage_done('error', 'Dieser Lagerhaus-Gegenstand ist schon mit „' . $dupName . '“ im Shop verknüpft.');
|
|
}
|
|
|
|
$stock = (int) $item['stock'];
|
|
if ($stock > 0) {
|
|
$pdo->prepare(
|
|
"INSERT INTO warehouse_movements (item_id, direction, quantity, occurred_at, counterpart_name, note, created_by)
|
|
VALUES (?, 'in', ?, ?, 'Lumishop', 'Bestand aus dem Lumishop übertragen', ?)"
|
|
)->execute([$warehouseId, $stock, shop_now(), $userId]);
|
|
}
|
|
$pdo->prepare("UPDATE shop_items SET stock_mode = 'warehouse', stock = 0, warehouse_item_id = ?, updated_at = NOW() WHERE id = ?")
|
|
->execute([$warehouseId, $itemId]);
|
|
$pdo->commit();
|
|
|
|
shop_manage_done('success', '„' . $item['name'] . '“ ist jetzt mit dem Lagerhaus verknüpft'
|
|
. ($stock > 0 ? ' - ' . $stock . ' Stück dort eingelagert.' : '.'));
|
|
|
|
default:
|
|
redirect('/shop_manage.php');
|
|
}
|