LuminaWebsite/includes/contributions.php
Maaxxs ebc9e2367e Lumishop: trade Lumipunkte for items, with approval
Members request items from the shop. Their points and the stock are
earmarked but not taken until someone with shop.manage approves. Approving
books a negative "Lumishop" entry in the Lumipunkte history and takes the
stock. For warehouse-backed items it also books a withdrawal to the member
in the warehouse. Approved orders are then marked as handed over in the
game, or can be reversed, which restores points, stock and the warehouse
withdrawal.

The management page handles categories and items (price, description,
stock). Stock is either the shop's own count, unlimited, or live from a
linked warehouse item. Items in stock can be imported from the warehouse,
and an item's own stock can be moved into the warehouse to link it there.
Warehouse cards show when an item is sold in the shop.

Shop bookings can't be deleted from the Lumipunkte page, only reversed in
the shop, so orders and points stay in sync. The new permissions go to the
Administrator group only, so the shop stays hidden until it is assigned.

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

158 lines
5.4 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
declare(strict_types=1);
/**
* Lumipunkte: Punkte, die Mitglieder fuer Raids und andere Aktivitaeten bekommen.
*
* Eine Vergabe (contribution_awards) ist ein Anlass mit Grund und Datum, an dem ein oder
* mehrere Mitglieder beteiligt sind (contribution_entries, je Mitglied die Punkte). Der
* Punktestand wird nicht gespeichert, sondern aus der Summe berechnet - so kann er nie von
* der Historie abweichen, und spaeter lassen sich Punkte auch ausgeben (negative Eintraege).
*/
const CONTRIBUTION_CATEGORIES = [
'raid' => 'Raid',
'event' => 'Event',
'other' => 'Sonstiges',
];
// Anlaesse, die nur das System anlegt (nicht im Formular waehlbar). Vergaben dieser Art
// lassen sich nicht auf der Lumipunkte-Seite loeschen, sondern nur dort, wo sie entstanden sind.
const CONTRIBUTION_SYSTEM_CATEGORIES = [
'shop' => 'Lumishop',
];
// So viele Plaetze der Rangliste sieht jeder mit contributions.view - den Rest nur mit
// contributions.view_all (oder contributions.manage).
const CONTRIBUTION_PUBLIC_RANKS = 25;
// Obergrenze pro Mitglied und Vergabe - schuetzt vor Tippfehlern wie 10000 statt 100.
const CONTRIBUTION_MAX_POINTS = 10000;
function contribution_category_label(string $key): string
{
return CONTRIBUTION_CATEGORIES[$key] ?? CONTRIBUTION_SYSTEM_CATEGORIES[$key] ?? $key;
}
/** Das Muenz-Symbol der Lumipunkte. */
function contribution_coin(int $size = 20): string
{
return '<img class="contribution-coin" src="/assets/images/lumipunkt.svg" alt="Lumipunkte" '
. 'width="' . $size . '" height="' . $size . '">';
}
/** Punkte mit Tausenderpunkt, Abzuege mit echtem Minuszeichen. */
function contribution_format(int $points, bool $withSign = false): string
{
$formatted = number_format(abs($points), 0, ',', '.');
if ($points < 0) {
return '−' . $formatted;
}
return ($withSign && $points > 0 ? '+' : '') . $formatted;
}
/**
* Punktestand aller aktiven Mitglieder (auch 0), hoechster zuerst.
*
* @return array<int, array<string, mixed>>
*/
function contribution_balances(): array
{
return db()->query(
"SELECT u.id, u.username, u.display_name, u.avatar_filename,
COALESCE(SUM(e.points), 0) AS points,
COUNT(e.id) AS award_count
FROM users u
LEFT JOIN contribution_entries e ON e.user_id = u.id
WHERE u.status = 'active'
GROUP BY u.id
ORDER BY points DESC, COALESCE(NULLIF(u.display_name, ''), u.username)"
)->fetchAll();
}
function contribution_user_balance(int $userId): int
{
$stmt = db()->prepare('SELECT COALESCE(SUM(points), 0) FROM contribution_entries WHERE user_id = ?');
$stmt->execute([$userId]);
return (int) $stmt->fetchColumn();
}
function contribution_award_count(?int $userId): int
{
if ($userId === null) {
return (int) db()->query('SELECT COUNT(*) FROM contribution_awards')->fetchColumn();
}
$stmt = db()->prepare('SELECT COUNT(*) FROM contribution_entries WHERE user_id = ?');
$stmt->execute([$userId]);
return (int) $stmt->fetchColumn();
}
/**
* Vergaben mit ihren Empfaengern, neueste zuerst. Mit $userId nur die Vergaben, an denen
* dieses Mitglied beteiligt war.
*
* @return array<int, array<string, mixed>>
*/
function contribution_awards(?int $userId, int $limit, int $offset): array
{
$join = $userId !== null ? 'JOIN contribution_entries mine ON mine.award_id = a.id AND mine.user_id = ?' : '';
$stmt = db()->prepare(
"SELECT a.id, a.category, a.title, a.note, a.raid_id, a.awarded_at,
COALESCE(NULLIF(cu.display_name, ''), cu.username) AS author
FROM contribution_awards a
$join
LEFT JOIN users cu ON cu.id = a.created_by
ORDER BY a.awarded_at DESC, a.id DESC
LIMIT " . (int) $limit . ' OFFSET ' . (int) $offset
);
$stmt->execute($userId !== null ? [$userId] : []);
$awards = [];
foreach ($stmt->fetchAll() as $row) {
$row['entries'] = [];
$awards[(int) $row['id']] = $row;
}
if (empty($awards)) {
return [];
}
$ids = array_keys($awards);
$placeholders = implode(',', array_fill(0, count($ids), '?'));
$entryStmt = db()->prepare(
"SELECT e.id, e.award_id, e.user_id, e.points,
COALESCE(NULLIF(u.display_name, ''), u.username) AS name
FROM contribution_entries e
JOIN users u ON u.id = e.user_id
WHERE e.award_id IN ($placeholders)
ORDER BY name"
);
$entryStmt->execute($ids);
foreach ($entryStmt->fetchAll() as $entry) {
$awards[(int) $entry['award_id']]['entries'][] = $entry;
}
return array_values($awards);
}
/** Jetzt in deutscher Zeit fuer ein datetime-local-Feld. */
function contribution_now_input(): string
{
return (new DateTimeImmutable('now', new DateTimeZone('Europe/Berlin')))->format('Y-m-d\TH:i');
}
/** Zeitpunkt aus einem datetime-local-Feld, null bei ungueltiger Eingabe oder weit in der Zukunft. */
function contribution_parse_time(string $value): ?string
{
$tz = new DateTimeZone('Europe/Berlin');
$time = DateTimeImmutable::createFromFormat('Y-m-d\TH:i', trim($value), $tz);
if ($time === false || $time > new DateTimeImmutable('+1 day', $tz)) {
return null;
}
return $time->format('Y-m-d H:i:s');
}
function contribution_format_time(?string $value): string
{
$time = $value ? strtotime($value) : false;
return $time ? date('d.m.Y H:i', $time) : '-';
}