Members may now request more than is in stock (or items that are sold out); only their points have to cover the full request. When approving, the manager can approve fewer than requested. The quantity field is prefilled with what is in stock, the cost updates as you type, and only the approved part is charged and taken from stock. The order keeps the requested quantity, so the member sees e.g. "3 von 10 bewilligt". The order list also shows the current stock and flags when it isn't enough. The column upgrade check now covers every table that has upgrades, instead of only users and roles. Tables that are about to be created fresh are skipped. The dashboard gets a card with your Lumipunkte and your rank. In the sidebar, the points move from under your name into a gold box above the profile. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
222 lines
7.9 KiB
PHP
222 lines
7.9 KiB
PHP
<?php
|
||
declare(strict_types=1);
|
||
|
||
/**
|
||
* Lumishop: Mitglieder tauschen Lumipunkte gegen Gegenstaende.
|
||
*
|
||
* Ablauf einer Bestellung:
|
||
* pending - Mitglied hat beantragt. Punkte und Bestand sind vorgemerkt, aber noch nicht abgezogen.
|
||
* approved - bewilligt: Punkte abgezogen (negative Vergabe der Kategorie "shop"), Bestand
|
||
* verringert bzw. aus dem Lagerhaus entnommen. Wartet auf die Uebergabe im Spiel.
|
||
* delivered - im Spiel uebergeben.
|
||
* rejected - abgelehnt (nichts wurde abgezogen).
|
||
* cancelled - vom Mitglied zurueckgezogen oder nach der Bewilligung storniert (Punkte und
|
||
* Bestand zurueck).
|
||
*
|
||
* Bestand eines Gegenstands (stock_mode):
|
||
* unlimited - unbegrenzt
|
||
* manual - eigene Zahl im Shop (shop_items.stock)
|
||
* warehouse - der Bestand des verknuepften Lagerhaus-Gegenstands; bei der Bewilligung wird
|
||
* automatisch eine Entnahme an das Mitglied gebucht.
|
||
*/
|
||
|
||
require_once __DIR__ . '/contributions.php';
|
||
require_once __DIR__ . '/warehouse.php';
|
||
|
||
const SHOP_STATUSES = [
|
||
'pending' => 'Beantragt',
|
||
'approved' => 'Bewilligt – Übergabe ausstehend',
|
||
'delivered' => 'Übergeben',
|
||
'rejected' => 'Abgelehnt',
|
||
'cancelled' => 'Storniert',
|
||
];
|
||
|
||
const SHOP_STOCK_MODES = [
|
||
'manual' => 'Eigener Bestand im Shop',
|
||
'warehouse' => 'Aus dem Lagerhaus (automatisch)',
|
||
'unlimited' => 'Unbegrenzt',
|
||
];
|
||
|
||
const SHOP_MAX_PRICE = 1000000;
|
||
const SHOP_MAX_STOCK = 1000000;
|
||
// Nur gegen Tippfehler - der Bestand begrenzt Antraege nicht, erst die Bewilligung.
|
||
const SHOP_MAX_ORDER_QUANTITY = 1000000;
|
||
|
||
function shop_status_label(string $status): string
|
||
{
|
||
return SHOP_STATUSES[$status] ?? $status;
|
||
}
|
||
|
||
/** @return array<int, array<string, mixed>> */
|
||
function shop_categories(): array
|
||
{
|
||
return db()->query('SELECT id, name, sort_order FROM shop_categories ORDER BY sort_order, name')->fetchAll();
|
||
}
|
||
|
||
/**
|
||
* Gegenstaende mit Kategorie, Bild, Lagerbestand und vorgemerkter Menge (offene Antraege).
|
||
*
|
||
* @return array<int, array<string, mixed>>
|
||
*/
|
||
function shop_items(bool $onlyActive): array
|
||
{
|
||
$rows = db()->query(
|
||
"SELECT s.*, c.name AS category_name, c.sort_order AS category_sort,
|
||
w.name AS warehouse_name, w.icon,
|
||
(SELECT COALESCE(SUM(CASE WHEN m.direction = 'in' THEN m.quantity ELSE -m.quantity END), 0)
|
||
FROM warehouse_movements m WHERE m.item_id = s.warehouse_item_id) AS warehouse_stock,
|
||
(SELECT COALESCE(SUM(o.quantity), 0) FROM shop_orders o
|
||
WHERE o.item_id = s.id AND o.status = 'pending') AS reserved
|
||
FROM shop_items s
|
||
LEFT JOIN shop_categories c ON c.id = s.category_id
|
||
LEFT JOIN warehouse_items w ON w.id = s.warehouse_item_id
|
||
" . ($onlyActive ? 'WHERE s.active = 1' : '') . "
|
||
ORDER BY c.sort_order IS NULL, c.sort_order, c.name, s.name"
|
||
)->fetchAll();
|
||
|
||
foreach ($rows as &$row) {
|
||
$row['available'] = shop_available($row);
|
||
}
|
||
unset($row);
|
||
return $rows;
|
||
}
|
||
|
||
function shop_item(int $id): ?array
|
||
{
|
||
$stmt = db()->prepare(
|
||
'SELECT s.*, w.name AS warehouse_name, w.icon FROM shop_items s
|
||
LEFT JOIN warehouse_items w ON w.id = s.warehouse_item_id WHERE s.id = ?'
|
||
);
|
||
$stmt->execute([$id]);
|
||
return $stmt->fetch() ?: null;
|
||
}
|
||
|
||
/** Bestand vor Abzug der offenen Antraege; null = unbegrenzt. */
|
||
function shop_stock(array $item): ?int
|
||
{
|
||
switch ($item['stock_mode']) {
|
||
case 'unlimited':
|
||
return null;
|
||
case 'warehouse':
|
||
if (empty($item['warehouse_item_id'])) {
|
||
return 0;
|
||
}
|
||
return array_key_exists('warehouse_stock', $item)
|
||
? (int) $item['warehouse_stock']
|
||
: warehouse_item_stock((int) $item['warehouse_item_id']);
|
||
default:
|
||
return (int) $item['stock'];
|
||
}
|
||
}
|
||
|
||
/** Was noch beantragt werden kann (Bestand minus offene Antraege); null = unbegrenzt. */
|
||
function shop_available(array $item): ?int
|
||
{
|
||
$stock = shop_stock($item);
|
||
if ($stock === null) {
|
||
return null;
|
||
}
|
||
if (!array_key_exists('reserved', $item)) {
|
||
$stmt = db()->prepare("SELECT COALESCE(SUM(quantity), 0) FROM shop_orders WHERE item_id = ? AND status = 'pending'");
|
||
$stmt->execute([(int) $item['id']]);
|
||
$item['reserved'] = (int) $stmt->fetchColumn();
|
||
}
|
||
return max(0, $stock - (int) $item['reserved']);
|
||
}
|
||
|
||
/** Punkte, die in offenen Antraegen eines Mitglieds vorgemerkt sind. */
|
||
function shop_reserved_points(int $userId): int
|
||
{
|
||
$stmt = db()->prepare("SELECT COALESCE(SUM(total_price), 0) FROM shop_orders WHERE user_id = ? AND status = 'pending'");
|
||
$stmt->execute([$userId]);
|
||
return (int) $stmt->fetchColumn();
|
||
}
|
||
|
||
function shop_pending_count(): int
|
||
{
|
||
return (int) db()->query("SELECT COUNT(*) FROM shop_orders WHERE status = 'pending'")->fetchColumn();
|
||
}
|
||
|
||
/**
|
||
* Bestellungen, neueste zuerst.
|
||
*
|
||
* @param string[] $statuses leer = alle
|
||
* @return array<int, array<string, mixed>>
|
||
*/
|
||
function shop_orders(array $statuses, ?int $userId, int $limit, int $offset): array
|
||
{
|
||
[$where, $params] = shop_orders_where($statuses, $userId);
|
||
$stmt = db()->prepare(
|
||
"SELECT o.*, w.icon, s.stock_mode, s.warehouse_item_id,
|
||
COALESCE(NULLIF(u.display_name, ''), u.username) AS buyer,
|
||
COALESCE(NULLIF(du.display_name, ''), du.username) AS decider,
|
||
COALESCE(NULLIF(vu.display_name, ''), vu.username) AS deliverer
|
||
FROM shop_orders o
|
||
JOIN users u ON u.id = o.user_id
|
||
LEFT JOIN shop_items s ON s.id = o.item_id
|
||
LEFT JOIN warehouse_items w ON w.id = s.warehouse_item_id
|
||
LEFT JOIN users du ON du.id = o.decided_by
|
||
LEFT JOIN users vu ON vu.id = o.delivered_by
|
||
$where
|
||
ORDER BY o.created_at " . (in_array('pending', $statuses, true) && count($statuses) === 1 ? 'ASC' : 'DESC') . ', o.id DESC
|
||
LIMIT ' . (int) $limit . ' OFFSET ' . (int) $offset
|
||
);
|
||
$stmt->execute($params);
|
||
return $stmt->fetchAll();
|
||
}
|
||
|
||
function shop_order_count(array $statuses, ?int $userId): int
|
||
{
|
||
[$where, $params] = shop_orders_where($statuses, $userId);
|
||
$stmt = db()->prepare("SELECT COUNT(*) FROM shop_orders o $where");
|
||
$stmt->execute($params);
|
||
return (int) $stmt->fetchColumn();
|
||
}
|
||
|
||
/** @return array{0: string, 1: array<int, mixed>} */
|
||
function shop_orders_where(array $statuses, ?int $userId): array
|
||
{
|
||
$conditions = [];
|
||
$params = [];
|
||
if (!empty($statuses)) {
|
||
$conditions[] = 'o.status IN (' . implode(',', array_fill(0, count($statuses), '?')) . ')';
|
||
array_push($params, ...$statuses);
|
||
}
|
||
if ($userId !== null) {
|
||
$conditions[] = 'o.user_id = ?';
|
||
$params[] = $userId;
|
||
}
|
||
return [$conditions ? 'WHERE ' . implode(' AND ', $conditions) : '', $params];
|
||
}
|
||
|
||
/** Bild eines Shop-Gegenstands (das des verknuepften Lagerhaus-Gegenstands oder Platzhalter). */
|
||
function shop_icon_html(array $item, int $size): string
|
||
{
|
||
return warehouse_icon_html($item['icon'] ?? null, (string) ($item['name'] ?? $item['item_name'] ?? ''), $size);
|
||
}
|
||
|
||
/** "3× Name" bzw. nur "Name" bei einem Stueck. */
|
||
function shop_order_label(array $order): string
|
||
{
|
||
return ((int) $order['quantity'] > 1 ? (int) $order['quantity'] . '× ' : '') . $order['item_name'];
|
||
}
|
||
|
||
/** Urspruenglich beantragte Anzahl (aeltere Antraege ohne eigene Spalte: die Anzahl selbst). */
|
||
function shop_requested_quantity(array $order): int
|
||
{
|
||
return max((int) ($order['requested_quantity'] ?? 0), (int) $order['quantity']);
|
||
}
|
||
|
||
function shop_format_availability(?int $available): string
|
||
{
|
||
if ($available === null) {
|
||
return 'Unbegrenzt verfügbar';
|
||
}
|
||
return $available > 0 ? number_format($available, 0, ',', '.') . ' verfügbar' : 'Ausverkauft';
|
||
}
|
||
|
||
/** Jetzt in deutscher Zeit - wie die Zeitpunkte, die im Lagerhaus und bei Vergaben eingetragen werden. */
|
||
function shop_now(): string
|
||
{
|
||
return (new DateTimeImmutable('now', new DateTimeZone('Europe/Berlin')))->format('Y-m-d H:i:s');
|
||
}
|