From 078f28551df84933531682e6fbbb594ab81b17a9 Mon Sep 17 00:00:00 2001 From: Clemens Creutzburg Date: Tue, 14 Jul 2026 22:25:51 +0200 Subject: [PATCH] Teilnehmerauswertung auf Ledger-Lesemodell umstellen --- app/ledger.php | 54 ++++++ index.php | 492 +++++++++++++++++++++++++++---------------------- 2 files changed, 328 insertions(+), 218 deletions(-) diff --git a/app/ledger.php b/app/ledger.php index d29ba27..5868954 100644 --- a/app/ledger.php +++ b/app/ledger.php @@ -265,6 +265,60 @@ function ledger_fetch_participant_summary_by_legacy_id(PDO $pdo, int $tenantId, return $summaries[0] ?? null; } +function ledger_mirror_legacy_consumption(PDO $pdo, int $tenantId, int $legacyConsumptionId): void +{ + $stmt = $pdo->prepare( + "INSERT INTO ledger_entries + (tenant_id, participant_id, type, amount_cents, marks_count, + unit_price_cents, booked_at, source, note, legacy_table, legacy_id) + SELECT + p.tenant_id, + p.id, + 'consumption', + -CAST(ROUND(v.Kosten * 100) AS SIGNED), + v.AnzahlStriche, + CAST(ROUND(v.KostenproStrich * 100) AS SIGNED), + v.Datum, + CASE + WHEN v.Eintragsart = 2 THEN 'legacy_web' + ELSE 'legacy_manual' + END, + CASE + WHEN v.Eintragsart IS NULL THEN NULL + ELSE CONCAT('Legacy Eintragsart: ', v.Eintragsart) + END, + 'kl_Kaffeeverbrauch', + v.VerbrauchID + FROM kl_Kaffeeverbrauch v + JOIN participants p + ON p.tenant_id = ? + AND p.legacy_mitarbeiter_id = v.MitarbeiterID + WHERE v.VerbrauchID = ? + ON DUPLICATE KEY UPDATE + participant_id = VALUES(participant_id), + type = VALUES(type), + amount_cents = VALUES(amount_cents), + marks_count = VALUES(marks_count), + unit_price_cents = VALUES(unit_price_cents), + booked_at = VALUES(booked_at), + source = VALUES(source), + note = VALUES(note)" + ); + $stmt->execute([$tenantId, $legacyConsumptionId]); + + $check = $pdo->prepare( + "SELECT COUNT(*) + FROM ledger_entries + WHERE tenant_id = ? + AND legacy_table = 'kl_Kaffeeverbrauch' + AND legacy_id = ?" + ); + $check->execute([$tenantId, $legacyConsumptionId]); + if ((int)$check->fetchColumn() !== 1) { + throw new RuntimeException('Der Legacy-Strich konnte nicht ins Ledger gespiegelt werden.'); + } +} + /** * Options: * - participant_id: int diff --git a/index.php b/index.php index e0dda87..edb6503 100644 --- a/index.php +++ b/index.php @@ -1,242 +1,298 @@ [$emailNorm], + ]); + $participant = $summaries[0] ?? null; +} + +$settings = $hasAccess ? saas_fetch_tenant_settings($pdo, $tenantId) : null; +$entryMessage = null; +$entryError = null; + +function dashboard_money(int $cents): string +{ + return saas_format_money_cents($cents) . ' €'; +} + +function dashboard_date(string $value): string +{ + try { + return (new DateTimeImmutable($value))->format('d.m.Y'); + } catch (Throwable $e) { + return $value; + } +} + +function dashboard_current_name(?array $saasUser, ?array $participant, mixed $conn, string $mailadress): string +{ + $name = trim((string)($saasUser['display_name'] ?? '')); + if ($name !== '') { + return $name; + } + + $name = trim((string)($participant['display_name'] ?? '')); + if ($name !== '') { + return $name; + } + + return trim((string)getUserName($conn, $mailadress)); +} + +function dashboard_paypal_action(string $template, string $amount): string +{ + return trim($template) . $amount; +} + +/** + * @return array{ok: bool, message?: string, error?: string} + */ +function dashboard_record_legacy_self_entry(PDO $pdo, array $participant, array $settings, mixed $marksValue): array +{ + $legacyMitarbeiterId = $participant['legacy_mitarbeiter_id'] !== null + ? (int)$participant['legacy_mitarbeiter_id'] + : 0; + if ($legacyMitarbeiterId <= 0) { + return ['ok' => false, 'error' => 'Für diesen Teilnehmer ist die eigene Stricherfassung noch nicht verfügbar.']; + } + + if ((int)$settings['self_entry_enabled'] !== 1) { + return ['ok' => false, 'error' => 'Die eigene Stricherfassung ist aktuell deaktiviert.']; + } + + $marks = filter_var($marksValue, FILTER_VALIDATE_INT); + if ($marks !== 1 && $marks !== 2) { + return ['ok' => false, 'error' => 'Bitte einen oder zwei Striche auswählen.']; + } + + $unitPriceCents = (int)$settings['mark_price_cents']; + if ($unitPriceCents <= 0) { + return ['ok' => false, 'error' => 'Der Preis pro Strich ist nicht gültig konfiguriert.']; + } + + $bookedAt = date('Y-m-d H:i:s'); + $cost = (($marks * $unitPriceCents) / 100); + $unitPrice = ($unitPriceCents / 100); + + try { + $pdo->beginTransaction(); + $stmt = $pdo->prepare( + 'INSERT INTO kl_Kaffeeverbrauch + (MitarbeiterID, AnzahlStriche, Kosten, KostenproStrich, Datum, Eintragsart) + VALUES (?, ?, ?, ?, ?, 2)' + ); + $stmt->execute([$legacyMitarbeiterId, $marks, $cost, $unitPrice, $bookedAt]); + $legacyConsumptionId = (int)$pdo->lastInsertId(); + if ($legacyConsumptionId <= 0) { + throw new RuntimeException('Legacy-Strich konnte nicht angelegt werden.'); + } + + ledger_mirror_legacy_consumption($pdo, (int)$participant['tenant_id'], $legacyConsumptionId); + $pdo->commit(); + } catch (Throwable $e) { + if ($pdo->inTransaction()) { + $pdo->rollBack(); + } + + return ['ok' => false, 'error' => 'Die Stricheintragung konnte nicht gespeichert werden.']; + } + + return ['ok' => true, 'message' => 'Stricheintragung wurde erfolgreich eingetragen.']; +} + +function dashboard_render_payments(array $entries): void +{ + echo "

Letzte Einzahlungen

"; + echo ""; + if ($entries === []) { + echo ""; + } + foreach ($entries as $entry) { + echo ""; + echo ""; + echo ""; + echo ""; + } + echo "
DatumEinzahlung
Keine Einzahlungen vorhanden.
" . saas_html(dashboard_date((string)$entry['booked_at'])) . "" . saas_html(dashboard_money((int)$entry['amount_cents'])) . "
"; +} + +function dashboard_render_consumption(array $entries): void +{ + echo "

Letzte Striche

"; + echo ""; + if ($entries === []) { + echo ""; + } + foreach ($entries as $entry) { + echo ""; + echo ""; + echo ""; + echo ""; + echo ""; + } + echo "
DatumStricheKosten
Keine Striche vorhanden.
" . saas_html(dashboard_date((string)$entry['booked_at'])) . "" . number_format((int)($entry['marks_count'] ?? 0), 0, ',', '.') . "" . saas_html(dashboard_money(abs((int)$entry['amount_cents']))) . "
"; +} + +if ($_SERVER["REQUEST_METHOD"] === "POST") { + if ($hasAccess && $participant !== null && $settings !== null) { + $result = dashboard_record_legacy_self_entry($pdo, $participant, $settings, $_POST["anzahlStriche"] ?? null); + if ($result['ok']) { + $entryMessage = $result['message'] ?? null; + $participant = ledger_fetch_participant_summary($pdo, $tenantId, (int)$participant['participant_id']); + } else { + $entryError = $result['error'] ?? 'Die Stricheintragung konnte nicht gespeichert werden.'; + } + } else { + $entryError = 'Die Stricheintragung konnte nicht gespeichert werden.'; + } +} + +$paymentEntries = $participant !== null + ? ledger_fetch_recent_entries($pdo, $tenantId, [ + 'participant_id' => $participant['participant_id'], + 'type' => 'payment', + 'limit' => 100, + ]) + : []; +$consumptionEntries = $participant !== null + ? ledger_fetch_recent_entries($pdo, $tenantId, [ + 'participant_id' => $participant['participant_id'], + 'type' => 'consumption', + 'limit' => 100, + ]) + : []; + include "header.php"; include "headerline.php"; include "nav.php"; - - ?> + - +