Erste drei Bausteine der Haertung: - Security-Headers (X-Content-Type-Options, X-Frame-Options, Referrer- Policy, Permissions-Policy, HSTS bei HTTPS) laufen automatisch ueber app_send_security_headers() am Ende von app/bootstrap.php fuer jede dynamische Seite; landing.php war als einzige Seite ganz ohne PHP und bekam einen minimalen Bootstrap-Aufruf. Bewusst kein CSP, da die bestehenden Templates durchgaengig auf Inline-style-Attribute setzen. - DB-gestuetzte Rate-Limits (neue Tabelle rate_limit_attempts) fuer Login (10/15min je E-Mail, 20/15min je IP), Registrierung (5/h je IP) und Passwort-Reset-Anfrage (5/h je E-Mail, 10/h je IP); bei ausgereiztem Reset-Limit erscheint dieselbe generische Meldung wie im Erfolgsfall, um kein Konto-Enumeration-Signal zu geben. - Zentrales Audit-Log (neue Tabelle audit_log) fuer Mitgliederverwaltung, Zugangsvergabe/-entzug, Storno, Mandant-Einstellungen, Hinweise, CSV-Import, Jahresbonus-Verteilung und Live-Mailversand; sichtbar fuer Owner/Admin auf mandant-einstellungen.php. Live getestet: Rate-Limit greift nach 10 Fehlversuchen, Audit-Log-Eintrag mit korrekten Metadaten und Nutzernamen ueber einen isolierten Test- Mandanten geprueft. Alle Regressionstests weiterhin gruen (26/26 Smoke, 104 Golden-Master-Assertions). Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
192 lines
6.4 KiB
PHP
192 lines
6.4 KiB
PHP
<?php
|
|
|
|
include "functions.php";
|
|
require_once __DIR__ . "/app/ledger.php";
|
|
require_once __DIR__ . "/app/saas-mail.php";
|
|
require_once __DIR__ . "/app/audit.php";
|
|
app_require_csrf();
|
|
include "header.php";
|
|
include "headerline.php";
|
|
include "nav.php";
|
|
|
|
?>
|
|
|
|
<!-- Banner -->
|
|
<section id="banner">
|
|
<div class="content">
|
|
|
|
<?php
|
|
|
|
$pdo = app_db_pdo();
|
|
$saasUser = saas_current_user($pdo);
|
|
$tenantId = 0;
|
|
$hasAccess = false;
|
|
|
|
if ($saasUser !== null && saas_user_has_role(['owner', 'admin', 'treasurer'], $saasUser)) {
|
|
$tenantId = (int)$saasUser['tenant_id'];
|
|
$hasAccess = true;
|
|
}
|
|
|
|
if (!$hasAccess && $saasUser === null && checkKaffeelisteAdmin($conn, $mailadress)) {
|
|
$tenant = ledger_fetch_default_tenant($pdo);
|
|
if ($tenant !== null) {
|
|
$tenantId = (int)$tenant['id'];
|
|
$hasAccess = true;
|
|
}
|
|
}
|
|
|
|
if (!$hasAccess) {
|
|
echo "<h2>Kein Zugriff</h2>";
|
|
include "footer.php";
|
|
exit;
|
|
}
|
|
|
|
$jahr = (int)date('Y');
|
|
$teilnehmer = ledger_fetch_participant_summaries($pdo, $tenantId, ['active_only' => true, 'year' => $jahr]);
|
|
$gesamtJahresstriche = array_sum(array_column($teilnehmer, 'year_marks'));
|
|
|
|
$fehler = null;
|
|
$ergebnisse = null;
|
|
$dryRun = true;
|
|
$gesamtbetragEingabe = '';
|
|
|
|
if ($_SERVER["REQUEST_METHOD"] === "POST") {
|
|
$dryRun = !empty($_POST['dry_run']);
|
|
$gesamtbetragEingabe = (string)($_POST['gesamtbetrag'] ?? '');
|
|
$gesamtbetrag = filter_var(str_replace(',', '.', $gesamtbetragEingabe), FILTER_VALIDATE_FLOAT);
|
|
$totalCents = $gesamtbetrag !== false ? (int)round($gesamtbetrag * 100) : 0;
|
|
|
|
if ($totalCents <= 0) {
|
|
$fehler = 'Bitte einen Gesamtbetrag größer 0 eingeben.';
|
|
} elseif ($gesamtJahresstriche <= 0) {
|
|
$fehler = "Für {$jahr} gibt es noch keine Striche, daher kann nichts verteilt werden.";
|
|
} else {
|
|
$ergebnisse = [];
|
|
$settings = saas_fetch_tenant_settings($pdo, $tenantId);
|
|
$dashboardUrl = saas_app_url('index.php');
|
|
$createdByUserId = $saasUser['user_id'] ?? null;
|
|
|
|
// Legacy-Verknuepfung je Teilnehmer vorab laden (gleiches Zweig-Muster
|
|
// wie bei Sammelerfassung und CSV-Import: Default-Mandant bucht
|
|
// zusaetzlich in kl_Einzahlungen, alle anderen Mandanten rein Ledger-nativ).
|
|
$legacyMap = [];
|
|
$stmt = $pdo->prepare('SELECT id, legacy_mitarbeiter_id FROM participants WHERE tenant_id = ?');
|
|
$stmt->execute([$tenantId]);
|
|
foreach ($stmt->fetchAll() as $row) {
|
|
$legacyMap[(int)$row['id']] = $row['legacy_mitarbeiter_id'] !== null ? (int)$row['legacy_mitarbeiter_id'] : null;
|
|
}
|
|
|
|
try {
|
|
if (!$dryRun) {
|
|
$pdo->beginTransaction();
|
|
}
|
|
$insertLegacy = $pdo->prepare('INSERT INTO kl_Einzahlungen (MitarbeiterID, Betrag, Datum) VALUES (?, ?, ?)');
|
|
$jetzt = date('Y-m-d H:i:s');
|
|
|
|
foreach ($teilnehmer as $person) {
|
|
$yearMarks = (int)$person['year_marks'];
|
|
if ($yearMarks <= 0) {
|
|
continue;
|
|
}
|
|
|
|
$anteil = $yearMarks / $gesamtJahresstriche;
|
|
$bonusCents = (int)round($totalCents * $anteil);
|
|
if ($bonusCents <= 0) {
|
|
continue;
|
|
}
|
|
|
|
$status = 'vorschau';
|
|
|
|
if (!$dryRun) {
|
|
$legacyMitarbeiterId = $legacyMap[$person['participant_id']] ?? null;
|
|
if ($legacyMitarbeiterId !== null) {
|
|
$insertLegacy->execute([$legacyMitarbeiterId, $bonusCents / 100, $jetzt]);
|
|
$legacyPaymentId = (int)$pdo->lastInsertId();
|
|
ledger_mirror_legacy_payment($pdo, $tenantId, $legacyPaymentId);
|
|
} else {
|
|
ledger_record_payment($pdo, $tenantId, $person['participant_id'], $bonusCents, 'year_end_bonus');
|
|
}
|
|
$status = 'gebucht';
|
|
|
|
$email = trim((string)($person['email'] ?? ''));
|
|
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
|
|
$subject = "Kaffeeliste - Dein Jahresbonus {$jahr}";
|
|
$body = saas_render_year_end_bonus_mail_body($person['display_name'], $yearMarks, $bonusCents, $dashboardUrl);
|
|
$sendResult = saas_send_mail($email, $subject, $body);
|
|
saas_log_outbound_email(
|
|
$pdo, $tenantId, $person['participant_id'], 'year_end_bonus', $subject,
|
|
$sendResult['ok'] ? 'sent' : 'failed', $sendResult['error'] ?? null, $createdByUserId
|
|
);
|
|
}
|
|
}
|
|
|
|
$ergebnisse[] = [
|
|
'name' => $person['display_name'],
|
|
'year_marks' => $yearMarks,
|
|
'anteil' => $anteil,
|
|
'bonus_cents' => $bonusCents,
|
|
'status' => $status,
|
|
];
|
|
}
|
|
|
|
if (!$dryRun) {
|
|
$pdo->commit();
|
|
app_audit_log($pdo, $tenantId, $createdByUserId, 'year_end_bonus.distributed', 'tenant', $tenantId, ['jahr' => $jahr, 'gesamtbetrag_cents' => $totalCents, 'empfaenger' => count($ergebnisse)]);
|
|
}
|
|
} catch (Throwable $e) {
|
|
if ($pdo->inTransaction()) {
|
|
$pdo->rollBack();
|
|
}
|
|
$fehler = 'Die Verteilung konnte nicht gespeichert werden.';
|
|
$ergebnisse = null;
|
|
}
|
|
}
|
|
}
|
|
|
|
?>
|
|
|
|
<h2>Jahresabschluss <?php echo (int)$jahr; ?></h2>
|
|
<p>Verteilt einen frei wählbaren Gesamtbetrag proportional zu den in diesem
|
|
Jahr gemachten Strichen als Guthaben an alle aktiven Mitglieder und
|
|
benachrichtigt sie per Mail. Im Dry-Run wird nur eine Vorschau berechnet,
|
|
es wird nichts gebucht und keine Mail verschickt.</p>
|
|
|
|
<p>Jahresstriche gesamt: <?php echo number_format($gesamtJahresstriche, 0, ',', '.'); ?></p>
|
|
|
|
<?php if ($fehler !== null): ?>
|
|
<div class="hint-box error"><p><?php echo saas_html($fehler); ?></p></div>
|
|
<?php endif; ?>
|
|
|
|
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
|
|
<?php echo app_csrf_field(); ?>
|
|
<label for="gesamtbetrag">Gesamtbetrag (€):</label>
|
|
<input type="text" name="gesamtbetrag" id="gesamtbetrag" value="<?php echo saas_html($gesamtbetragEingabe); ?>" required>
|
|
<br>
|
|
<div class="form-check">
|
|
<input class="form-check-input" type="checkbox" name="dry_run" id="dry_run" <?php echo $dryRun ? 'checked' : ''; ?>>
|
|
<label class="form-check-label" for="dry_run">Dry-Run (nur Vorschau, nichts buchen, keine Mails)</label>
|
|
</div>
|
|
<button type="submit">Berechnen</button>
|
|
</form>
|
|
|
|
<?php if ($ergebnisse !== null): ?>
|
|
<h3><?php echo $dryRun ? 'Vorschau' : 'Gebucht'; ?></h3>
|
|
<table>
|
|
<tr><th>Name</th><th>Jahresstriche</th><th>Anteil</th><th>Bonus</th><th>Status</th></tr>
|
|
<?php foreach ($ergebnisse as $eintrag): ?>
|
|
<tr>
|
|
<td><?php echo saas_html($eintrag['name']); ?></td>
|
|
<td><?php echo number_format($eintrag['year_marks'], 0, ',', '.'); ?></td>
|
|
<td><?php echo number_format($eintrag['anteil'] * 100, 1, ',', '.'); ?> %</td>
|
|
<td><?php echo saas_html(saas_format_money_cents($eintrag['bonus_cents'])); ?> €</td>
|
|
<td><?php echo $eintrag['status'] === 'gebucht' ? 'Gebucht' : 'Vorschau'; ?></td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
</table>
|
|
<?php endif; ?>
|
|
|
|
</div>
|
|
</section>
|
|
|
|
<?php include "footer.php"; ?>
|