LuminaWebsite/warehouse.php
Maaxxs 2f74c92b04 Lagerhaus: page numbers for the history
The history shows 25 entries per page with "Einträge 51–75 von 130" and a
bar of page links (first, last and two around the current one, gaps as "…")
instead of only newer/older links. The item filter is kept while paging and
a page number past the end shows the last page.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-21 09:03:51 +02:00

345 lines
18 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
require_once __DIR__ . '/includes/bootstrap.php';
require_once __DIR__ . '/includes/warehouse.php';
require_permission('warehouse.view');
$canManage = user_has_permission('warehouse.manage');
warehouse_sync_catalog();
$stock = warehouse_stock();
$stockTotal = array_sum(array_map(static fn (array $row): int => (int) $row['quantity'], $stock));
// Verlauf, optional nur fuer einen Gegenstand (Klick auf den Namen im Bestand).
$filterItemId = isset($_GET['item']) ? (int) $_GET['item'] : null;
$filterItem = null;
if ($filterItemId !== null) {
$stmt = db()->prepare('SELECT id, name FROM warehouse_items WHERE id = ?');
$stmt->execute([$filterItemId]);
$filterItem = $stmt->fetch() ?: null;
if (!$filterItem) {
$filterItemId = null;
}
}
$perPage = 25;
$totalMovements = warehouse_movement_count($filterItemId);
$pageCount = max(1, (int) ceil($totalMovements / $perPage));
$page = min($pageCount, max(1, (int) ($_GET['page'] ?? 1)));
$movements = warehouse_movements($filterItemId, $perPage, ($page - 1) * $perPage);
// 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) : [];
$members = $canManage ? db()->query(
"SELECT id, COALESCE(NULLIF(display_name, ''), username) AS name FROM users
WHERE status = 'active' ORDER BY name"
)->fetchAll() : [];
// Eingaben nach einem Fehler wieder ins Formular (siehe warehouse_action.php).
$old = $_SESSION['warehouse_form'] ?? [];
unset($_SESSION['warehouse_form']);
$openForm = (string) ($old['form'] ?? '');
$categoriesInStock = [];
foreach ($stock as $row) {
$categoriesInStock[$row['category']] = warehouse_category_label((string) $row['category']);
}
asort($categoriesInStock);
/** Von wem / an wen: Konto oder frei eingetragener Name. */
function warehouse_counterpart(array $movement): string
{
if (!empty($movement['counterpart_user'])) {
return (string) $movement['counterpart_user'];
}
return (string) ($movement['counterpart_name'] ?? '');
}
/** Auswahl "Konto oder Name" fuer beide Formulare. */
function warehouse_render_counterpart(string $prefix, array $members, array $old, bool $required): void
{
$selectedUser = (string) ($old['counterpart_user'] ?? '');
?>
<div class="warehouse-counterpart">
<div>
<label for="<?= e($prefix) ?>-user">Mitglied der Website</label>
<select id="<?= e($prefix) ?>-user" name="counterpart_user">
<option value="">– kein Konto / Name eintragen –</option>
<?php foreach ($members as $member): ?>
<option value="<?= (int) $member['id'] ?>" <?= $selectedUser === (string) $member['id'] ? 'selected' : '' ?>><?= e((string) $member['name']) ?></option>
<?php endforeach; ?>
</select>
</div>
<div>
<label for="<?= e($prefix) ?>-name">oder Name</label>
<input type="text" id="<?= e($prefix) ?>-name" name="counterpart_name" maxlength="100"
value="<?= e((string) ($old['counterpart_name'] ?? '')) ?>" placeholder="z.B. Spielername ohne Website-Konto">
</div>
</div>
<div class="field-hint"><?= $required ? 'Ein Konto auswählen oder einen Namen eintragen.' : 'Optional - leer lassen, wenn es an niemand Bestimmten ging.' ?></div>
<?php
}
$pageTitle = 'Lagerhaus';
$containerWide = true;
include __DIR__ . '/includes/header.php';
?>
<div class="panel">
<div class="warehouse-head">
<div>
<h1>Lagerhaus</h1>
<p class="text-muted">
<?= count($stock) ?> verschiedene Gegenstände, <?= number_format($stockTotal, 0, ',', '.') ?> Stück insgesamt.
</p>
</div>
<?php if ($canManage): ?>
<div class="actions">
<button type="button" class="button-success" data-open-modal="warehouse-in-modal">+ Einlagern</button>
<button type="button" class="button-secondary" data-open-modal="warehouse-out-modal" <?= empty($stock) ? 'disabled' : '' ?>>− Entnehmen</button>
</div>
<?php endif; ?>
</div>
<div class="warehouse-filters">
<input type="search" id="warehouse-search" placeholder="Gegenstand suchen …" autocomplete="off">
<select id="warehouse-category">
<option value="">Alle Kategorien</option>
<?php foreach ($categoriesInStock as $key => $label): ?>
<option value="<?= e((string) $key) ?>"><?= e($label) ?></option>
<?php endforeach; ?>
</select>
</div>
<?php if (empty($stock)): ?>
<p class="text-muted">Das Lager ist leer.<?= $canManage ? ' Mit „Einlagern“ trägst du den ersten Gegenstand ein.' : '' ?></p>
<?php else: ?>
<div class="warehouse-grid" id="warehouse-stock">
<?php foreach ($stock as $row): ?>
<div class="warehouse-card" data-name="<?= e(mb_strtolower((string) $row['name'])) ?>" data-category="<?= e((string) $row['category']) ?>">
<a class="warehouse-card-link" href="/warehouse.php?item=<?= (int) $row['id'] ?>#verlauf"
title="<?= e((string) $row['name']) ?> - zuletzt bewegt <?= e(warehouse_format_time($row['last_movement'])) ?>. Klicken für den Verlauf.">
<span class="warehouse-card-image">
<?= warehouse_icon_html($row['icon'] ?? null, (string) $row['name'], 72) ?>
<span class="warehouse-card-qty"><?= number_format((int) $row['quantity'], 0, ',', '.') ?></span>
</span>
<span class="warehouse-card-name"><?= e((string) $row['name']) ?></span>
<span class="warehouse-card-category"><?= e(warehouse_category_label((string) $row['category'])) ?></span>
</a>
<?php if ($canManage): ?>
<div class="warehouse-card-actions">
<button type="button" class="icon-button" title="Entnehmen" aria-label="Entnehmen"
data-open-modal="warehouse-out-modal" data-item-id="<?= (int) $row['id'] ?>">−</button>
<button type="button" class="icon-button" title="Mehr einlagern" aria-label="Mehr einlagern"
data-open-modal="warehouse-in-modal" data-item-name="<?= e((string) $row['name']) ?>">+</button>
</div>
<?php endif; ?>
</div>
<?php endforeach; ?>
</div>
<p class="text-muted warehouse-no-match" id="warehouse-no-match" hidden>Kein Gegenstand passt zur Suche.</p>
<?php endif; ?>
</div>
<div class="panel" id="verlauf">
<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>
<?php endif; ?>
</div>
<?php if (empty($movements)): ?>
<p class="text-muted">Noch keine Einträge.</p>
<?php else: ?>
<div class="table-scroll">
<table class="table-actions">
<thead>
<tr>
<th>Zeitpunkt</th>
<th>Art</th>
<th>Gegenstand</th>
<th class="num">Menge</th>
<th>Von / An</th>
<th>Bemerkung</th>
<th>Eingetragen von</th>
<th><?= $canManage ? 'Aktionen' : '' ?></th>
</tr>
</thead>
<tbody>
<?php foreach ($movements as $movement): ?>
<?php $isIn = $movement['direction'] === 'in'; ?>
<tr>
<td><?= e(warehouse_format_time($movement['occurred_at'])) ?></td>
<td><span class="badge <?= $isIn ? 'badge-active' : 'badge-disabled' ?>"><?= $isIn ? 'Eingelagert' : 'Entnommen' ?></span></td>
<td>
<a class="warehouse-item-inline" href="/warehouse.php?item=<?= (int) $movement['item_id'] ?>#verlauf">
<?= warehouse_icon_html($movement['icon'] ?? null, (string) $movement['item_name'], 32) ?>
<span><?= e((string) $movement['item_name']) ?></span>
</a>
</td>
<td class="num"><?= $isIn ? '+' : '−' ?><?= number_format((int) $movement['quantity'], 0, ',', '.') ?></td>
<td>
<?php $who = warehouse_counterpart($movement); ?>
<?php if ($who !== ''): ?>
<span class="text-muted"><?= $isIn ? 'von' : 'an' ?></span> <?= e($who) ?>
<?php else: ?>
<span class="text-muted">-</span>
<?php endif; ?>
</td>
<td><?= e((string) ($movement['note'] ?? '')) ?></td>
<td><?= e((string) ($movement['author'] ?? '-')) ?></td>
<td>
<?php if ($canManage): ?>
<form class="inline-form" method="post" action="/warehouse_action.php"
onsubmit="return confirm('Diesen Eintrag löschen? Der Bestand wird entsprechend angepasst.');">
<?= csrf_field() ?>
<input type="hidden" name="action" value="delete">
<input type="hidden" name="movement_id" value="<?= (int) $movement['id'] ?>">
<button type="submit" class="icon-button icon-button-danger" title="Eintrag löschen" aria-label="Eintrag löschen"><?= icon('trash') ?></button>
</form>
<?php endif; ?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<div class="warehouse-pages">
<span class="text-muted">
Einträge <?= ($page - 1) * $perPage + 1 ?>–<?= min($page * $perPage, $totalMovements) ?>
von <?= number_format($totalMovements, 0, ',', '.') ?>
</span>
<?php if ($pageCount > 1): ?>
<?php $base = '/warehouse.php?' . ($filterItemId !== null ? 'item=' . $filterItemId . '&' : ''); ?>
<nav class="pagination" aria-label="Seiten des Verlaufs">
<?php if ($page > 1): ?>
<a class="page-link" href="<?= e($base . 'page=' . ($page - 1)) ?>#verlauf" title="Neuere Einträge">‹ Zurück</a>
<?php else: ?>
<span class="page-link disabled">‹ Zurück</span>
<?php endif; ?>
<?php foreach (warehouse_page_numbers($page, $pageCount) as $number): ?>
<?php if ($number === null): ?>
<span class="page-gap">…</span>
<?php elseif ($number === $page): ?>
<span class="page-link active" aria-current="page"><?= $number ?></span>
<?php else: ?>
<a class="page-link" href="<?= e($base . 'page=' . $number) ?>#verlauf"><?= $number ?></a>
<?php endif; ?>
<?php endforeach; ?>
<?php if ($page < $pageCount): ?>
<a class="page-link" href="<?= e($base . 'page=' . ($page + 1)) ?>#verlauf" title="Ältere Einträge">Weiter ›</a>
<?php else: ?>
<span class="page-link disabled">Weiter ›</span>
<?php endif; ?>
</nav>
<?php endif; ?>
</div>
<?php endif; ?>
</div>
<?php if ($canManage): ?>
<?php $oldIn = $openForm === 'in' ? $old : []; $oldOut = $openForm === 'out' ? $old : []; ?>
<!-- Einlagern -->
<div class="modal-overlay" id="warehouse-in-modal" <?= $openForm === 'in' ? '' : 'hidden' ?>>
<div class="modal">
<button type="button" class="modal-close icon-button" title="Schließen"><?= icon('x') ?></button>
<h2>Einlagern</h2>
<form method="post" action="/warehouse_action.php">
<?= csrf_field() ?>
<input type="hidden" name="action" value="deposit">
<div>
<label for="in-item">Gegenstand</label>
<input type="text" id="in-item" name="item" list="warehouse-item-list" required autocomplete="off"
value="<?= e((string) ($oldIn['item'] ?? '')) ?>" placeholder="Namen eintippen, Vorschläge erscheinen">
<datalist id="warehouse-item-list">
<?php foreach ($allItems as $name): ?>
<option value="<?= e((string) $name) ?>">
<?php endforeach; ?>
</datalist>
</div>
<?php if (!empty($oldIn['unknown_item'])): ?>
<div class="warehouse-new-item">
<label>
<input type="checkbox" name="create_item" value="1" checked>
„<?= e((string) $oldIn['item']) ?>“ als neuen Gegenstand anlegen
</label>
<select name="create_category">
<?php foreach (WAREHOUSE_CUSTOM_CATEGORIES as $key): ?>
<option value="<?= e($key) ?>" <?= ($oldIn['create_category'] ?? 'material') === $key ? 'selected' : '' ?>><?= e(warehouse_category_label($key)) ?></option>
<?php endforeach; ?>
</select>
</div>
<?php endif; ?>
<div class="warehouse-row">
<div>
<label for="in-quantity">Anzahl</label>
<input type="number" id="in-quantity" name="quantity" min="1" max="<?= WAREHOUSE_MAX_QUANTITY ?>" required
value="<?= e((string) ($oldIn['quantity'] ?? '1')) ?>">
</div>
<div>
<label for="in-time">Überreicht am</label>
<input type="datetime-local" id="in-time" name="occurred_at" required
value="<?= e((string) ($oldIn['occurred_at'] ?? warehouse_now_input())) ?>">
</div>
</div>
<label>Überreicht von</label>
<?php warehouse_render_counterpart('in', $members, $oldIn, true); ?>
<div>
<label for="in-note">Bemerkung <span class="text-muted">(optional)</span></label>
<input type="text" id="in-note" name="note" maxlength="255" value="<?= e((string) ($oldIn['note'] ?? '')) ?>">
</div>
<div class="actions">
<button type="submit" class="button-success">Einlagern</button>
</div>
</form>
</div>
</div>
<!-- Entnehmen -->
<div class="modal-overlay" id="warehouse-out-modal" <?= $openForm === 'out' ? '' : 'hidden' ?>>
<div class="modal">
<button type="button" class="modal-close icon-button" title="Schließen"><?= icon('x') ?></button>
<h2>Entnehmen</h2>
<form method="post" action="/warehouse_action.php">
<?= csrf_field() ?>
<input type="hidden" name="action" value="withdraw">
<div>
<label for="out-item">Gegenstand</label>
<select id="out-item" name="item_id" required>
<option value="">– aus dem Bestand wählen –</option>
<?php foreach ($stock as $row): ?>
<option value="<?= (int) $row['id'] ?>" data-max="<?= (int) $row['quantity'] ?>"
<?= (string) ($oldOut['item_id'] ?? '') === (string) $row['id'] ? 'selected' : '' ?>>
<?= e((string) $row['name']) ?> (<?= number_format((int) $row['quantity'], 0, ',', '.') ?> auf Lager)
</option>
<?php endforeach; ?>
</select>
</div>
<div class="warehouse-row">
<div>
<label for="out-quantity">Anzahl</label>
<input type="number" id="out-quantity" name="quantity" min="1" required
value="<?= e((string) ($oldOut['quantity'] ?? '1')) ?>">
</div>
<div>
<label for="out-time">Entnommen am</label>
<input type="datetime-local" id="out-time" name="occurred_at" required
value="<?= e((string) ($oldOut['occurred_at'] ?? warehouse_now_input())) ?>">
</div>
</div>
<label>Überreicht an <span class="text-muted">(optional)</span></label>
<?php warehouse_render_counterpart('out', $members, $oldOut, false); ?>
<div>
<label for="out-note">Bemerkung <span class="text-muted">(optional)</span></label>
<input type="text" id="out-note" name="note" maxlength="255" value="<?= e((string) ($oldOut['note'] ?? '')) ?>">
</div>
<div class="actions">
<button type="submit">Entnehmen</button>
</div>
</form>
</div>
</div>
<?php endif; ?>
<script src="/assets/js/warehouse.js"></script>
<?php include __DIR__ . '/includes/footer.php'; ?>