Teilnehmerauswertung auf Ledger-Lesemodell umstellen

This commit is contained in:
2026-07-14 22:21:28 +02:00
parent 539117b409
commit 8688cc78db
8 changed files with 284 additions and 217 deletions
+177 -192
View File
@@ -1,213 +1,198 @@
<?php
include "functions.php";
require_once __DIR__ . "/app/ledger.php";
$legacyUserId = filter_input(INPUT_GET, 'user_id', FILTER_VALIDATE_INT);
if ($legacyUserId === null) {
http_response_code(400);
exit('Fehlender Parameter');
}
if ($legacyUserId === false || $legacyUserId <= 0) {
http_response_code(400);
exit('Ungültige Benutzer-ID');
}
$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;
}
}
$participant = $hasAccess
? ledger_fetch_participant_summary_by_legacy_id($pdo, $tenantId, $legacyUserId)
: null;
$settings = $hasAccess ? saas_fetch_tenant_settings($pdo, $tenantId) : null;
$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,
])
: [];
function teilnehmerauswertung_money(int $cents): string
{
return saas_format_money_cents($cents) . ' €';
}
function teilnehmerauswertung_date(string $value): string
{
try {
return (new DateTimeImmutable($value))->format('d.m.Y');
} catch (Throwable $e) {
return $value;
}
}
function teilnehmerauswertung_current_name(?array $saasUser, mixed $conn, string $mailadress): string
{
$name = trim((string)($saasUser['display_name'] ?? ''));
if ($name !== '') {
return $name;
}
return trim((string)getUserName($conn, $mailadress));
}
function teilnehmerauswertung_paypal_action(string $template, string $amount): string
{
return trim($template) . $amount;
}
function teilnehmerauswertung_render_payments(array $entries): void
{
echo "<h4>Letzte Einzahlungen</h4>";
echo "<table><tr><th style='width:120px'>Datum</th><th>Einzahlung</th></tr>";
if ($entries === []) {
echo "<tr><td colspan='2'>Keine Einzahlungen vorhanden.</td></tr>";
}
foreach ($entries as $entry) {
echo "<tr>";
echo "<td>" . saas_html(teilnehmerauswertung_date((string)$entry['booked_at'])) . "</td>";
echo "<td>" . saas_html(teilnehmerauswertung_money((int)$entry['amount_cents'])) . "</td>";
echo "</tr>";
}
echo "</table>";
}
function teilnehmerauswertung_render_consumption(array $entries): void
{
echo "<h4>Letzte Striche</h4>";
echo "<table><tr><th style='width:120px'>Datum</th><th>Striche</th><th>Kosten</th></tr>";
if ($entries === []) {
echo "<tr><td colspan='3'>Keine Striche vorhanden.</td></tr>";
}
foreach ($entries as $entry) {
echo "<tr>";
echo "<td>" . saas_html(teilnehmerauswertung_date((string)$entry['booked_at'])) . "</td>";
echo "<td>" . number_format((int)($entry['marks_count'] ?? 0), 0, ',', '.') . "</td>";
echo "<td>" . saas_html(teilnehmerauswertung_money(abs((int)$entry['amount_cents']))) . "</td>";
echo "</tr>";
}
echo "</table>";
}
include "header.php";
include "headerline.php";
include "nav.php";
?>
<!-- Banner -->
<section id="banner">
<div class="content">
<section id="banner">
<div class="content">
<?php
if ($hasAccess && $participant !== null) {
$balanceCents = (int)$participant['balance_cents'];
$balance = teilnehmerauswertung_money($balanceCents);
$currentName = teilnehmerauswertung_current_name($saasUser, $conn, $mailadress);
$paypalEnabled = $settings !== null
&& (int)$settings['paypal_enabled'] === 1
&& trim((string)$settings['paypal_url_template']) !== '';
?>
<h2>Auswertung</h2>
<?php if ($currentName !== ''): ?>
Hallo <?php echo saas_html($currentName); ?>!<br><br>
<?php endif; ?>
if(checkKaffeelisteAdmin($conn, $mailadress)){
// Sichere Entgegennahme
$userId = filter_input(INPUT_GET, 'user_id', FILTER_VALIDATE_INT);
if ($userId === null) {
http_response_code(400);
exit('Fehlender Parameter');
<h2>Gesamtausgabe und Gesamtstriche</h2>
<p>Name: <?php echo saas_html($participant['display_name']); ?></p>
<p>E-Mail: <?php echo saas_html((string)($participant['email'] ?? '')); ?></p>
Gesamtausgabe: <?php echo saas_html(teilnehmerauswertung_money((int)$participant['total_costs_cents'])); ?><br>
Gesamtstriche: <?php echo number_format((int)$participant['total_marks'], 0, ',', '.'); ?><br>
<p>Gesamteinzahlung: <?php echo saas_html(teilnehmerauswertung_money((int)$participant['total_payments_cents'])); ?></p>
<?php
if ($balanceCents > 0) {
echo "<p><b>Aktueller Stand: " . saas_html($balance) . " (Guthaben)</b></p>";
} elseif ($balanceCents < 0) {
echo "<p><b>Aktueller Stand: " . saas_html($balance) . " (Schulden)</b></p>";
} else {
echo "<p><b>Aktueller Stand: " . saas_html($balance) . "</b></p>";
}
if ($userId === false) {
http_response_code(400);
exit('Ungültige Benutzer-ID');
?>
<h2>Jahresübersicht</h2>
Ausgabe im aktuellen Jahr: <?php echo saas_html(teilnehmerauswertung_money((int)$participant['year_costs_cents'])); ?><br>
Gesamtstriche im aktuellen Jahr: <?php echo number_format((int)$participant['year_marks'], 0, ',', '.'); ?><br>
<p>Gesamteinzahlung im aktuellen Jahr: <?php echo saas_html(teilnehmerauswertung_money((int)$participant['year_payments_cents'])); ?></p>
<?php if ($paypalEnabled): ?>
<h2>PayPal-Einzahlungen</h2>
<b>Bezahle immer über die Freunde-Funktion von PayPal. Ansonsten stellen wir 20% des Betrags als Gebühr in Rechnung.</b><br>
<br>
<?php
$paypalTemplate = trim((string)$settings['paypal_url_template']);
if ($balanceCents < 0) {
$debtCents = abs($balanceCents);
$debtAmount = teilnehmerauswertung_money($debtCents);
echo '<form action="' . saas_html(teilnehmerauswertung_paypal_action($paypalTemplate, saas_format_money_cents($debtCents))) . '" target="_blank"><button type="submit">' . saas_html($debtAmount) . ' bezahlen</button></form>';
}
echo "<h2>Auswertung</h2>";
echo "Hallo " . getUserName($conn,$mailadress) . "!<br><br>";
// Funktion zum Berechnen der Gesamtausgabe und Gesamtstriche pro Mitarbeiter
function berechneGesamtausgabe($email, $conn) {
// MitarbeiterID anhand der E-Mail-Adresse abrufen
$sqlMitarbeiterID = "SELECT MitarbeiterID FROM kl_Mitarbeiter WHERE Email = ?";
$stmtMitarbeiterID = sqlsrv_query($conn, $sqlMitarbeiterID, array($email));
$rowMitarbeiterID = sqlsrv_fetch_array($stmtMitarbeiterID, SQLSRV_FETCH_ASSOC);
?>
<ul class="actions">
<li><form action="<?php echo saas_html(teilnehmerauswertung_paypal_action($paypalTemplate, '5')); ?>" target="_blank"><button type="submit">5,00 € einzahlen</button></form></li>
<li><form action="<?php echo saas_html(teilnehmerauswertung_paypal_action($paypalTemplate, '10')); ?>" target="_blank"><button type="submit">10,00 € einzahlen</button></form></li>
<li><form action="<?php echo saas_html(teilnehmerauswertung_paypal_action($paypalTemplate, '15')); ?>" target="_blank"><button type="submit">15,00 € einzahlen</button></form></li>
</ul>
<?php endif; ?>
if (!$rowMitarbeiterID) {
return null; // Mitarbeiter nicht gefunden
}
$mitarbeiterID = $rowMitarbeiterID["MitarbeiterID"];
// Gesamteinzahlung pro Mitarbeiter
$sqleinzahlung = "SELECT SUM(Betrag) AS Gesamteinzahlung FROM kl_Einzahlungen WHERE MitarbeiterID = ?";
$stmteinzahlung = sqlsrv_query($conn, $sqleinzahlung, array($mitarbeiterID));
$roweinzahlung = sqlsrv_fetch_array($stmteinzahlung, SQLSRV_FETCH_ASSOC);
$gesamteinzahlung = $roweinzahlung['Gesamteinzahlung'];
// Gesamtausgabe für Kaffeeverbrauch pro Mitarbeiter
$sqlAusgabe = "SELECT SUM(AnzahlStriche) AS Gesamtstriche, SUM(Kosten) AS Gesamtausgabe FROM kl_Kaffeeverbrauch WHERE MitarbeiterID = ?";
$stmtAusgabe = sqlsrv_query($conn, $sqlAusgabe, array($mitarbeiterID));
$rowAusgabe = sqlsrv_fetch_array($stmtAusgabe, SQLSRV_FETCH_ASSOC);
$gesamtausgabe = $rowAusgabe['Gesamtausgabe'];
$gesamtstriche = $rowAusgabe['Gesamtstriche'];
$aktuellerStand = $gesamteinzahlung - $gesamtausgabe;
// Gesamteinzahlung pro Mitarbeiter und Aktuellem Jahr
$sqleinzahlung = "SELECT SUM(Betrag) AS Gesamteinzahlung FROM kl_Einzahlungen WHERE MitarbeiterID = ? AND FORMAT(Datum, 'yyyy') = FORMAT(GETDATE(), 'yyyy') ";
$stmteinzahlung = sqlsrv_query($conn, $sqleinzahlung, array($mitarbeiterID));
$roweinzahlung = sqlsrv_fetch_array($stmteinzahlung, SQLSRV_FETCH_ASSOC);
$yeareinzahlung = $roweinzahlung['Gesamteinzahlung'];
// Gesamtausgabe für Kaffeeverbrauch pro Mitarbeiter und Aktuellem Jahr
$sqlAusgabe = "SELECT SUM(AnzahlStriche) AS Gesamtstriche, SUM(Kosten) AS Gesamtausgabe FROM kl_Kaffeeverbrauch WHERE MitarbeiterID = ? AND FORMAT(Datum, 'yyyy') = FORMAT(GETDATE(), 'yyyy')";
$stmtAusgabe = sqlsrv_query($conn, $sqlAusgabe, array($mitarbeiterID));
$rowAusgabe = sqlsrv_fetch_array($stmtAusgabe, SQLSRV_FETCH_ASSOC);
$yearausgabe = $rowAusgabe['Gesamtausgabe'];
$yearstriche = $rowAusgabe['Gesamtstriche'];
return array('Jahresausgabe' => $yearausgabe, 'Jahresstriche' => $yearstriche, 'Jahreseinzahlung' => $yeareinzahlung, 'Gesamtausgabe' => $gesamtausgabe, 'Gesamtstriche' => $gesamtstriche, 'Gesamteinzahlung' => $gesamteinzahlung, 'aktuellerStand' => $aktuellerStand);
#return array('Gesamtausgabe' => $gesamtausgabe, 'Gesamtstriche' => $gesamtstriche, 'Gesamteinzahlung' => $gesamteinzahlung, 'aktuellerStand' => $aktuellerStand);
}
function AusgabeletztenEinzahlungen($email, $conn) {
// MitarbeiterID anhand der E-Mail-Adresse abrufen
$sqlMitarbeiterID = "SELECT TOP 20 MitarbeiterID FROM kl_Mitarbeiter WHERE Email = ?";
$stmtMitarbeiterID = sqlsrv_query($conn, $sqlMitarbeiterID, array($email));
$rowMitarbeiterID = sqlsrv_fetch_array($stmtMitarbeiterID, SQLSRV_FETCH_ASSOC);
if (!$rowMitarbeiterID) {
return null; // Mitarbeiter nicht gefunden
}
$mitarbeiterID = $rowMitarbeiterID["MitarbeiterID"];
// Gesamteinzahlung pro Mitarbeiter
$sqleinzahlung = "SELECT Betrag,Datum FROM kl_Einzahlungen WHERE MitarbeiterID = ? ORDER BY Datum DESC ";
$stmteinzahlung = sqlsrv_query($conn, $sqleinzahlung, array($mitarbeiterID));
$ausgabe = "<h4>Letzte Einzahlungen</h4><table><tr><th style='width:120'>Datum</th><th>Einzahlung</th></tr>";
while ($row = sqlsrv_fetch_array($stmteinzahlung, SQLSRV_FETCH_ASSOC)) {
$ausgabe .= "<tr><td>" . date_format($row["Datum"],"d.m.Y") . "</td><td>" . number_format($row["Betrag"], 2, ',', '') . "€</td></tr>";
}
$ausgabe .= "</table>";
return $ausgabe;
}
function AusgabeletztenStriche($email, $conn) {
// MitarbeiterID anhand der E-Mail-Adresse abrufen
$sqlMitarbeiterID = "SELECT TOP 20 MitarbeiterID FROM kl_Mitarbeiter WHERE Email = ?";
$stmtMitarbeiterID = sqlsrv_query($conn, $sqlMitarbeiterID, array($email));
$rowMitarbeiterID = sqlsrv_fetch_array($stmtMitarbeiterID, SQLSRV_FETCH_ASSOC);
if (!$rowMitarbeiterID) {
return null; // Mitarbeiter nicht gefunden
}
$mitarbeiterID = $rowMitarbeiterID["MitarbeiterID"];
// Gesamteinzahlung pro Mitarbeiter
$sqleinzahlung = "SELECT AnzahlStriche,Kosten,Datum FROM kl_Kaffeeverbrauch WHERE MitarbeiterID = ? ORDER BY Datum DESC ";
$stmteinzahlung = sqlsrv_query($conn, $sqleinzahlung, array($mitarbeiterID));
$ausgabe = "<h4>Letzte Striche</h4><table ><tr><th style='width:120'>Datum</th><th>Striche</th><th>Kosten</th></tr>";
while ($row = sqlsrv_fetch_array($stmteinzahlung, SQLSRV_FETCH_ASSOC)) {
$ausgabe .= "<tr><td>" . date_format($row["Datum"],"d.m.Y") . "</td><td>" . $row["AnzahlStriche"] . "</td><td>" . number_format($row["Kosten"], 2, ',', '') . "€</td></tr>";
}
$ausgabe .= "</table>";
return $ausgabe;
}
$sqlMitglieder = "SELECT MitarbeiterID, Name, Email FROM kl_Mitarbeiter WHERE MitarbeiterID = $userId";
$stmtMitglieder = sqlsrv_query($conn, $sqlMitglieder);
while ($row = sqlsrv_fetch_array($stmtMitglieder, SQLSRV_FETCH_ASSOC)) {
$mitarbeiterID = $row['MitarbeiterID'];
$name = $row['Name'];
$email = $row['Email'];
}
// Berechne Gesamtausgabe und Gesamtstriche für den Mitarbeiter
$result = berechneGesamtausgabe($email, $conn);
if ($result !== null) {
echo "<h2>Gesamtausgabe und Gesamtstriche</h2>";
echo "<p>Name: $name</p>";
echo "<p>E-Mail: $email</p>";
echo "Gesamtausgabe: " . number_format($result['Gesamtausgabe'], 2, ',', '') . " €<br>";
echo "Gesamtstriche: {$result['Gesamtstriche']}<br>";
echo "<p>Gesamteinzahlung: " . number_format($result['Gesamteinzahlung'], 2, ',', '') . " €<br>";
$aktuellerstand = number_format($result['aktuellerStand'], 2, ',', '.');
if($result['aktuellerStand'] > 0){
echo "<p><b>Aktueller Stand: {$aktuellerstand} € (Guthaben)</p>";
}elseif($result['aktuellerStand'] < 0){
echo "<p><b>Aktueller Stand: {$aktuellerstand} € (Schulden)</p>";
}else{
echo "<p><b>Aktueller Stand: {$aktuellerstand} €</p>";
}
echo "</b>";
echo "<h2>Jahresübersicht</h2>";
echo "Ausgabe im aktuellem Jahr: " . number_format($result['Jahresausgabe'], 2, ',', '') . " €<br>";
echo "Gesamtstriche im aktuellem Jahr: {$result['Jahresstriche']}<br>";
echo "<p>Gesamteinzahlung im aktuellem Jahr: " . number_format($result['Jahreseinzahlung'], 2, ',', '') . " €<br>";
$sqlconfig = "SELECT paypaluse,paypallink FROM kl_config";
$stmtconfig = sqlsrv_query($conn, $sqlconfig, array($email));
$rowconfig = sqlsrv_fetch_array($stmtconfig, SQLSRV_FETCH_ASSOC);
if($rowconfig["paypaluse"] == 1){
echo "<h2>Paypal-Einzahlungen</h2>";
echo '<b>Bezahle immer über die Freunde-Funktion von Paypal. Ansonsten stellen wir 20% des Betrags als Gebühr in Rechnung.</b><br>';
$paypallink = trim($rowconfig["paypallink"]);
echo "<br>";
if($result['aktuellerStand'] < 0){
echo '<form action="' . $paypallink .'' . $aktuellerstand . '" target="_blank" ><button type="submit">'. $aktuellerstand . ' € bezahlen</button></form>';
}
echo'<ul class="actions">
<li>';
echo '<form action="' . $paypallink .'5" target="_blank" ><button type="submit">5,00 € einzahlen</button></form>';
echo '</li><li>';
echo '<form action="' . $paypallink .'10" target="_blank" ><button type="submit">10,00 € einzahlen</button></form>';
echo '</li><li>';
echo '<form action="' . $paypallink .'15" target="_blank" ><button type="submit">15,00 € einzahlen</button></form>';
echo '</li></ul>';
}
echo "<br>";
echo "<br>";
echo AusgabeletztenEinzahlungen($email, $conn);
echo "<br>";
echo AusgabeletztenStriche($email, $conn);
?>
<br><br>
<!-- Formular mit Button zum Anpassen des Namens -->
<?php
teilnehmerauswertung_render_payments($paymentEntries);
echo "<br>";
teilnehmerauswertung_render_consumption($consumptionEntries);
?>
<br><br>
<form action="namenanpassen.php" method="get">
<button type="submit">Namensanpassung</button>
</form>
<?php
} else {
echo "<p>Mitarbeiter mit der E-Mail-Adresse $email wurde nicht gefunden.</p>";
}
}else{
echo "<h2>Sie haben keine Zugang zu dieser Webseite</h2>";
<?php
} elseif ($hasAccess) {
echo "<h2>Auswertung</h2>";
echo "<p>Mitarbeiter wurde im aktuellen Mandanten nicht gefunden.</p>";
} else {
echo "<h2>Kein Zugriff</h2>";
}
?>
</div>
</div>
</section>
<?php include "footer.php";
?>
<?php include "footer.php"; ?>