LuminaWebsite/warehouse.php
Maaxxs 3e025b811e Lagerhaus: stock and history of the legion's items
A new page records items handed in (item, quantity, when, and from whom -
a website account or a free-text name) and taken out (when, now or entered
by hand, and optionally to whom). Stock is always computed from the
movements, withdrawals can't exceed it, and a deposit can only be deleted
once nothing of it has been taken out. Stock is shown as cards with the item
picture and quantity, name and category below, plus search and a category
filter; the history lists every movement with who entered it.

The item list holds 2,301 weapons, armor pieces, accessories and consumables
read from the Aion 2 wiki's MediaWiki API, with each item's icon downscaled
to 128px under assets/images/warehouse/. It is loaded into the database on
first visit and again whenever the JSON changes. Unknown names can be added
as custom items after a confirmation, since the wiki lists no materials.

warehouse.view and warehouse.manage are new permissions. Missing core
permissions are now created automatically and granted to the Administrator
group, and the two tables are created like the other schema additions.

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

324 lines
16 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 = 50;
$page = max(1, (int) ($_GET['page'] ?? 1));
$movements = warehouse_movements($filterItemId, $perPage + 1, ($page - 1) * $perPage);
$hasMore = count($movements) > $perPage;
$movements = array_slice($movements, 0, $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">
<?php $base = '/warehouse.php?' . ($filterItemId !== null ? 'item=' . $filterItemId . '&' : ''); ?>
<?php if ($page > 1): ?>
<a href="<?= e($base . 'page=' . ($page - 1)) ?>#verlauf">← Neuere</a>
<?php endif; ?>
<?php if ($hasMore): ?>
<a href="<?= e($base . 'page=' . ($page + 1)) ?>#verlauf">Ältere →</a>
<?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'; ?>