Warehouse history: search, in/out filter and sorting (server-side, kept across pages)
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
parent
276f80bd7b
commit
11d8e8d819
2 changed files with 114 additions and 20 deletions
|
|
@ -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<int, mixed>}
|
||||
*/
|
||||
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<int, array<string, mixed>>
|
||||
*/
|
||||
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();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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';
|
|||
<div class="warehouse-head">
|
||||
<h2>Verlauf<?= $filterItem ? ': ' . e((string) $filterItem['name']) : '' ?></h2>
|
||||
<?php if ($filterItem): ?>
|
||||
<a href="/warehouse.php#verlauf">Alle Gegenstände anzeigen</a>
|
||||
<a href="<?= e($historyUrl(['item' => '']) . '#verlauf') ?>">Alle Gegenstände anzeigen</a>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
|
||||
<form class="list-toolbar" id="warehouse-history-filter" method="get" action="/warehouse.php#verlauf">
|
||||
<?php if ($filterItemId !== null): ?><input type="hidden" name="item" value="<?= (int) $filterItemId ?>"><?php endif; ?>
|
||||
<input type="hidden" name="dir" value="<?= e($historyDirection) ?>">
|
||||
<input type="hidden" name="sort" value="<?= e($historySort) ?>">
|
||||
<label class="search-field">
|
||||
<?= icon('search') ?>
|
||||
<input type="search" name="q" value="<?= e($historyQuery) ?>" placeholder="Gegenstand, Person oder Bemerkung suchen …" aria-label="Verlauf durchsuchen">
|
||||
</label>
|
||||
<div class="list-toolbar-row">
|
||||
<div class="members-chips" role="group" aria-label="Nach Art filtern">
|
||||
<?php foreach (['' => 'Alle', 'in' => 'Eingelagert', 'out' => 'Entnommen'] as $value => $label): ?>
|
||||
<button type="submit" name="dir" value="<?= e($value) ?>" class="members-chip<?= $historyDirection === $value ? ' active' : '' ?>" aria-pressed="<?= $historyDirection === $value ? 'true' : 'false' ?>"><?= e($label) ?></button>
|
||||
<?php endforeach; ?>
|
||||
</div>
|
||||
<div class="list-toolbar-meta">
|
||||
<span class="members-filter-label"><?= icon('sort') ?> Sortieren</span>
|
||||
<div class="members-segment" role="group" aria-label="Sortieren">
|
||||
<?php foreach (WAREHOUSE_HISTORY_SORTS as $value => [$label]): ?>
|
||||
<button type="submit" name="sort" value="<?= e($value) ?>" class="<?= $historySort === $value ? 'active' : '' ?>"><?= e($label) ?></button>
|
||||
<?php endforeach; ?>
|
||||
</div>
|
||||
<span class="list-search-count text-muted">
|
||||
<?= number_format($totalMovements, 0, ',', '.') ?> <?= $totalMovements === 1 ? 'Eintrag' : 'Einträge' ?>
|
||||
<?php if ($historyFiltered): ?>
|
||||
· <a href="<?= e($historyUrl(['q' => '', 'dir' => '']) . '#verlauf') ?>">Filter zurücksetzen</a>
|
||||
<?php endif; ?>
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
<?php if (empty($movements)): ?>
|
||||
<p class="text-muted">Noch keine Einträge.</p>
|
||||
<p class="text-muted"><?= $historyFiltered ? 'Kein Eintrag passt zu Suche und Filter.' : 'Noch keine Einträge.' ?></p>
|
||||
<?php else: ?>
|
||||
<div class="table-scroll">
|
||||
<table class="table-actions">
|
||||
|
|
@ -244,7 +294,7 @@ include __DIR__ . '/includes/header.php';
|
|||
Einträge <?= ($page - 1) * $perPage + 1 ?>–<?= min($page * $perPage, $totalMovements) ?>
|
||||
von <?= number_format($totalMovements, 0, ',', '.') ?>
|
||||
</span>
|
||||
<?php render_pagination('/warehouse.php?' . ($filterItemId !== null ? 'item=' . $filterItemId . '&' : ''), $page, $pageCount, '#verlauf'); ?>
|
||||
<?php render_pagination($historyUrl(), $page, $pageCount, '#verlauf'); ?>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
|
|
|
|||
Loading…
Reference in a new issue