From 11d8e8d819da0cc72e5c08fef427cddbf0a5de8c Mon Sep 17 00:00:00 2001 From: Maaxxs <61059039+MaaxxsDev@users.noreply.github.com> Date: Mon, 21 Sep 2026 21:16:07 +0200 Subject: [PATCH] Warehouse history: search, in/out filter and sorting (server-side, kept across pages) Co-Authored-By: Claude Opus 5 --- includes/warehouse.php | 74 +++++++++++++++++++++++++++++++++--------- warehouse.php | 60 +++++++++++++++++++++++++++++++--- 2 files changed, 114 insertions(+), 20 deletions(-) diff --git a/includes/warehouse.php b/includes/warehouse.php index 387e68d..6b78b70 100644 --- a/includes/warehouse.php +++ b/includes/warehouse.php @@ -163,39 +163,83 @@ function warehouse_find_item(string $name): ?array return $row ?: null; } -/** Anzahl der Bewegungen (fuer die Seitenzahlen im Verlauf), optional nur fuer einen Gegenstand. */ -function warehouse_movement_count(?int $itemId): int +/** Sortierungen fuer den Verlauf: Schluessel => [Beschriftung, ORDER BY]. */ +const WAREHOUSE_HISTORY_SORTS = [ + 'new' => ['Neueste', 'm.occurred_at DESC, m.id DESC'], + 'old' => ['Älteste', 'm.occurred_at ASC, m.id ASC'], + 'qty' => ['Menge', 'm.quantity DESC, m.occurred_at DESC, m.id DESC'], + 'item' => ['Gegenstand', 'i.name ASC, m.occurred_at DESC, m.id DESC'], +]; + +/** + * WHERE-Teil fuer den Verlauf. Filter: item (ID), direction ('in'/'out') und q (Suche in + * Gegenstand, Von/An, Bemerkung und "Eingetragen von"). + * + * @param array{item?: ?int, direction?: string, q?: string} $filter + * @return array{0: string, 1: array} + */ +function warehouse_history_where(array $filter): array { - if ($itemId === null) { - return (int) db()->query('SELECT COUNT(*) FROM warehouse_movements')->fetchColumn(); + $conditions = []; + $params = []; + if (($filter['item'] ?? null) !== null) { + $conditions[] = 'm.item_id = ?'; + $params[] = (int) $filter['item']; } - $stmt = db()->prepare('SELECT COUNT(*) FROM warehouse_movements WHERE item_id = ?'); - $stmt->execute([$itemId]); + if (in_array($filter['direction'] ?? '', ['in', 'out'], true)) { + $conditions[] = 'm.direction = ?'; + $params[] = $filter['direction']; + } + $q = trim((string) ($filter['q'] ?? '')); + if ($q !== '') { + $like = '%' . addcslashes($q, '%_\\') . '%'; + $conditions[] = '(i.name LIKE ? OR m.counterpart_name LIKE ? OR m.note LIKE ? + OR cu.display_name LIKE ? OR cu.username LIKE ? OR au.display_name LIKE ? OR au.username LIKE ?)'; + array_push($params, $like, $like, $like, $like, $like, $like, $like); + } + return [$conditions ? 'WHERE ' . implode(' AND ', $conditions) : '', $params]; +} + +/** Gemeinsame Joins fuer Verlauf und Zaehlung (die Suche braucht die Namen). */ +const WAREHOUSE_HISTORY_FROM = 'FROM warehouse_movements m + JOIN warehouse_items i ON i.id = m.item_id + LEFT JOIN users cu ON cu.id = m.counterpart_user_id + LEFT JOIN users au ON au.id = m.created_by'; + +/** + * Anzahl der Bewegungen (fuer die Seitenzahlen im Verlauf). + * + * @param array{item?: ?int, direction?: string, q?: string} $filter + */ +function warehouse_movement_count(array $filter = []): int +{ + [$where, $params] = warehouse_history_where($filter); + $stmt = db()->prepare('SELECT COUNT(*) ' . WAREHOUSE_HISTORY_FROM . ' ' . $where); + $stmt->execute($params); return (int) $stmt->fetchColumn(); } /** - * Letzte Bewegungen, optional nur fuer einen Gegenstand. + * Bewegungen fuer den Verlauf, gefiltert und sortiert (siehe WAREHOUSE_HISTORY_SORTS). * + * @param array{item?: ?int, direction?: string, q?: string} $filter * @return array> */ -function warehouse_movements(?int $itemId, int $limit, int $offset): array +function warehouse_movements(array $filter, int $limit, int $offset, string $sort = 'new'): array { - $where = $itemId !== null ? 'WHERE m.item_id = ?' : ''; + [$where, $params] = warehouse_history_where($filter); + $order = (WAREHOUSE_HISTORY_SORTS[$sort] ?? WAREHOUSE_HISTORY_SORTS['new'])[1]; $stmt = db()->prepare( "SELECT m.id, m.item_id, m.direction, m.quantity, m.occurred_at, m.note, m.created_at, m.counterpart_name, i.name AS item_name, i.category, i.icon, COALESCE(NULLIF(cu.display_name, ''), cu.username) AS counterpart_user, COALESCE(NULLIF(au.display_name, ''), au.username) AS author - FROM warehouse_movements m - JOIN warehouse_items i ON i.id = m.item_id - LEFT JOIN users cu ON cu.id = m.counterpart_user_id - LEFT JOIN users au ON au.id = m.created_by + " . WAREHOUSE_HISTORY_FROM . " $where - ORDER BY m.occurred_at DESC, m.id DESC + ORDER BY $order LIMIT " . (int) $limit . ' OFFSET ' . (int) $offset ); - $stmt->execute($itemId !== null ? [$itemId] : []); + $stmt->execute($params); return $stmt->fetchAll(); } diff --git a/warehouse.php b/warehouse.php index f591eaa..24d8c82 100644 --- a/warehouse.php +++ b/warehouse.php @@ -37,11 +37,30 @@ if ($filterItemId !== null) { $filterItemId = null; } } +// Suche, Art (eingelagert/entnommen) und Sortierung - alles in der Adresse, damit die Seiten mitwandern. +$historyQuery = mb_substr(trim((string) ($_GET['q'] ?? '')), 0, 100); +$historyDirection = in_array($_GET['dir'] ?? '', ['in', 'out'], true) ? (string) $_GET['dir'] : ''; +$historySort = isset(WAREHOUSE_HISTORY_SORTS[$_GET['sort'] ?? '']) ? (string) $_GET['sort'] : 'new'; +$historyFilter = ['item' => $filterItemId, 'direction' => $historyDirection, 'q' => $historyQuery]; +$historyFiltered = $historyQuery !== '' || $historyDirection !== ''; + +/** Link auf den Verlauf mit den aktuellen Filtern, einzelne Werte ueberschrieben ('' = weglassen). */ +$historyUrl = static function (array $overrides = []) use ($filterItemId, $historyQuery, $historyDirection, $historySort): string { + $params = array_merge([ + 'item' => $filterItemId !== null ? (string) $filterItemId : '', + 'q' => $historyQuery, + 'dir' => $historyDirection, + 'sort' => $historySort !== 'new' ? $historySort : '', + ], $overrides); + $params = array_filter($params, static fn ($value): bool => $value !== '' && $value !== null); + return '/warehouse.php?' . ($params ? http_build_query($params) . '&' : ''); +}; + $perPage = 25; -$totalMovements = warehouse_movement_count($filterItemId); +$totalMovements = warehouse_movement_count($historyFilter); $pageCount = max(1, (int) ceil($totalMovements / $perPage)); $page = min($pageCount, max(1, (int) ($_GET['page'] ?? 1))); -$movements = warehouse_movements($filterItemId, $perPage, ($page - 1) * $perPage); +$movements = warehouse_movements($historyFilter, $perPage, ($page - 1) * $perPage, $historySort); // Fuer die Formulare: alle Gegenstaende (Vorschlagsliste) und die Konten der Website. $allItems = $canManage ? db()->query('SELECT name FROM warehouse_items ORDER BY name')->fetchAll(PDO::FETCH_COLUMN) : []; @@ -179,12 +198,43 @@ include __DIR__ . '/includes/header.php'; +
+ + + + +
+
+ 'Alle', 'in' => 'Eingelagert', 'out' => 'Entnommen'] as $value => $label): ?> + + +
+
+ Sortieren +
+ [$label]): ?> + + +
+ + + + · Filter zurücksetzen + + +
+
+
+ -

Noch keine Einträge.

+

@@ -244,7 +294,7 @@ include __DIR__ . '/includes/header.php'; Einträge – von - +