'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; const SHOP_MAX_ORDER_QUANTITY = 1000; function shop_status_label(string $status): string { return SHOP_STATUSES[$status] ?? $status; } /** @return array> */ 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> */ 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> */ 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} */ 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']; } 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'); }