Haerte Verwaltungsflows und Zahlungsabgleich
This commit is contained in:
+107
-31
@@ -1,5 +1,7 @@
|
||||
<?php
|
||||
|
||||
ob_start();
|
||||
|
||||
include "functions.php";
|
||||
require_once __DIR__ . "/app/ledger.php";
|
||||
app_require_csrf();
|
||||
@@ -57,64 +59,137 @@ function stricheintragen_fetch_participants(PDO $pdo, int $tenantId, string $act
|
||||
$eingetragen = 0;
|
||||
$fehlgeschlagen = false;
|
||||
$hatGespeichert = false;
|
||||
$erfolgsmeldung = null;
|
||||
$validierungsFehler = [];
|
||||
$eingaben = [];
|
||||
$kostenproStrichEingabe = null;
|
||||
$action = (string)($_GET['action'] ?? 'alle');
|
||||
if (!in_array($action, ['vorderseite', 'rueckseite', 'alle'], true)) {
|
||||
$action = 'alle';
|
||||
}
|
||||
|
||||
if (isset($_SESSION['flash_stricheintragen']) && is_array($_SESSION['flash_stricheintragen'])) {
|
||||
$flash = $_SESSION['flash_stricheintragen'];
|
||||
unset($_SESSION['flash_stricheintragen']);
|
||||
if (($flash['type'] ?? '') === 'success') {
|
||||
$erfolgsmeldung = sprintf('%d Einträge erfolgreich hinzugefügt.', (int)($flash['count'] ?? 0));
|
||||
}
|
||||
}
|
||||
|
||||
const STRICHE_MAX_PRO_PERSON = 50;
|
||||
const STRICHE_MAX_PREIS_CENTS = 10000;
|
||||
|
||||
// Verarbeitung des Formulars, wenn es gesendet wurde
|
||||
if ($_SERVER["REQUEST_METHOD"] == "POST") {
|
||||
$hatGespeichert = true;
|
||||
$kostenproStrich = floatval($_POST["kostenproStrich"] ?? 0);
|
||||
$unitPriceCents = (int)round($kostenproStrich * 100);
|
||||
$action = (string)($_POST['liste_action'] ?? 'alle');
|
||||
if (!in_array($action, ['vorderseite', 'rueckseite', 'alle'], true)) {
|
||||
$action = 'alle';
|
||||
}
|
||||
$kostenproStrichRaw = trim((string)($_POST["kostenproStrich"] ?? ''));
|
||||
$kostenproStrichEingabe = $kostenproStrichRaw;
|
||||
$unitPriceCents = saas_parse_money_cents($kostenproStrichRaw);
|
||||
|
||||
if ($unitPriceCents === null || $unitPriceCents <= 0) {
|
||||
$validierungsFehler[] = 'Bitte gültige Kosten pro Strich angeben.';
|
||||
} elseif ($unitPriceCents > STRICHE_MAX_PREIS_CENTS) {
|
||||
$validierungsFehler[] = 'Die Kosten pro Strich überschreiten die Plausibilitätsgrenze von 100,00 €. Bitte prüfen.';
|
||||
}
|
||||
|
||||
// Teilnehmer des Mandanten - dient der Zugehoerigkeitspruefung, damit ueber
|
||||
// das Formular keine fremden IDs bebucht werden koennen.
|
||||
$eigeneTeilnehmer = [];
|
||||
$stmt = $pdo->prepare('SELECT id FROM participants WHERE tenant_id = ?');
|
||||
$stmt = $pdo->prepare('SELECT id, display_name FROM participants WHERE tenant_id = ?');
|
||||
$stmt->execute([$tenantId]);
|
||||
foreach ($stmt->fetchAll(PDO::FETCH_COLUMN) as $id) {
|
||||
$eigeneTeilnehmer[(int)$id] = true;
|
||||
foreach ($stmt->fetchAll() as $row) {
|
||||
$eigeneTeilnehmer[(int)$row['id']] = (string)$row['display_name'];
|
||||
}
|
||||
|
||||
try {
|
||||
$pdo->beginTransaction();
|
||||
$zuBuchen = [];
|
||||
foreach ($_POST["anzahlStriche"] ?? [] as $participantId => $anzahlStriche) {
|
||||
$participantId = (int)$participantId;
|
||||
if ($participantId <= 0 || !array_key_exists($participantId, $eigeneTeilnehmer)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach ($_POST["anzahlStriche"] ?? [] as $participantId => $anzahlStriche) {
|
||||
$participantId = (int)$participantId;
|
||||
$anzahlStriche = (int)$anzahlStriche;
|
||||
if ($participantId <= 0 || $anzahlStriche === 0 || !isset($eigeneTeilnehmer[$participantId])) {
|
||||
continue;
|
||||
$rohStriche = trim((string)$anzahlStriche);
|
||||
$eingaben[$participantId] = $rohStriche;
|
||||
if ($rohStriche === '' || $rohStriche === '0') {
|
||||
continue;
|
||||
}
|
||||
|
||||
$name = $eigeneTeilnehmer[$participantId];
|
||||
if (!preg_match('/^[0-9]+$/', $rohStriche)) {
|
||||
$validierungsFehler[] = sprintf('%s: Bitte eine ganze positive Strich-Anzahl eintragen.', $name);
|
||||
continue;
|
||||
}
|
||||
|
||||
$anzahlStriche = (int)$rohStriche;
|
||||
if ($anzahlStriche > STRICHE_MAX_PRO_PERSON) {
|
||||
$validierungsFehler[] = sprintf(
|
||||
'%s: %d Striche überschreiten die Plausibilitätsgrenze von %d pro Buchung.',
|
||||
$name,
|
||||
$anzahlStriche,
|
||||
STRICHE_MAX_PRO_PERSON
|
||||
);
|
||||
continue;
|
||||
}
|
||||
|
||||
$zuBuchen[$participantId] = $anzahlStriche;
|
||||
}
|
||||
|
||||
if ($validierungsFehler === [] && $zuBuchen === []) {
|
||||
$validierungsFehler[] = 'Bitte mindestens eine Strich-Anzahl eintragen.';
|
||||
}
|
||||
|
||||
if ($validierungsFehler === []) {
|
||||
try {
|
||||
$pdo->beginTransaction();
|
||||
|
||||
foreach ($zuBuchen as $participantId => $anzahlStriche) {
|
||||
ledger_record_consumption($pdo, $tenantId, $participantId, $anzahlStriche, (int)$unitPriceCents, 'manual_bulk');
|
||||
$eingetragen++;
|
||||
}
|
||||
|
||||
ledger_record_consumption($pdo, $tenantId, $participantId, $anzahlStriche, $unitPriceCents, 'manual_bulk');
|
||||
$eingetragen++;
|
||||
$pdo->commit();
|
||||
$_SESSION['flash_stricheintragen'] = ['type' => 'success', 'count' => $eingetragen];
|
||||
header('Location: stricheintragen.php?action=' . urlencode($action));
|
||||
exit;
|
||||
} catch (Throwable $e) {
|
||||
if ($pdo->inTransaction()) {
|
||||
$pdo->rollBack();
|
||||
}
|
||||
$fehlgeschlagen = true;
|
||||
}
|
||||
|
||||
$pdo->commit();
|
||||
} catch (Throwable $e) {
|
||||
if ($pdo->inTransaction()) {
|
||||
$pdo->rollBack();
|
||||
}
|
||||
$fehlgeschlagen = true;
|
||||
}
|
||||
|
||||
$action = 'alle';
|
||||
} else {
|
||||
$action = (string)($_GET['action'] ?? 'alle');
|
||||
}
|
||||
|
||||
$mitarbeiter = stricheintragen_fetch_participants($pdo, $tenantId, $action);
|
||||
$settings = saas_fetch_tenant_settings($pdo, $tenantId);
|
||||
$kostenproStrichAnzeige = number_format(($settings['mark_price_cents'] ?? 20) / 100, 2, '.', '');
|
||||
$kostenproStrichAnzeige = $kostenproStrichEingabe !== null
|
||||
? $kostenproStrichEingabe
|
||||
: number_format(($settings['mark_price_cents'] ?? 20) / 100, 2, '.', '');
|
||||
|
||||
?>
|
||||
|
||||
<h2>Anzahl der Striche für alle Mitarbeiter</h2>
|
||||
|
||||
<?php if ($hatGespeichert): ?>
|
||||
<?php if ($fehlgeschlagen): ?>
|
||||
<?php if ($validierungsFehler !== []): ?>
|
||||
<div class="hint-box error">
|
||||
<ul>
|
||||
<?php foreach ($validierungsFehler as $fehler): ?>
|
||||
<li><?php echo saas_html($fehler); ?></li>
|
||||
<?php endforeach; ?>
|
||||
</ul>
|
||||
</div>
|
||||
<?php elseif ($fehlgeschlagen): ?>
|
||||
<div class="hint-box error"><p>Die Einträge konnten nicht gespeichert werden.</p></div>
|
||||
<?php else: ?>
|
||||
<div class="hint-box success"><p><?php echo (int)$eingetragen; ?> Einträge erfolgreich hinzugefügt.</p></div>
|
||||
<?php endif; ?>
|
||||
<?php endif; ?>
|
||||
<?php if ($erfolgsmeldung !== null): ?>
|
||||
<div class="hint-box success"><p><?php echo saas_html($erfolgsmeldung); ?></p></div>
|
||||
<?php endif; ?>
|
||||
|
||||
<ul class="actions">
|
||||
<li>
|
||||
@@ -139,11 +214,12 @@ $kostenproStrichAnzeige = number_format(($settings['mark_price_cents'] ?? 20) /
|
||||
|
||||
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
|
||||
<?php echo app_csrf_field(); ?>
|
||||
<input type="hidden" name="liste_action" value="<?php echo saas_html($action); ?>">
|
||||
<?php
|
||||
|
||||
// Kein <br> noetig - das Feld ist ein Blockelement mit eigenem Abstand.
|
||||
echo "<label for='kostenproStrich'>Kosten pro Strich:</label>
|
||||
<input type='number' name='kostenproStrich' step='0.01' value='" . saas_html($kostenproStrichAnzeige) . "'>";
|
||||
<input type='number' name='kostenproStrich' step='0.01' min='0.01' value='" . saas_html($kostenproStrichAnzeige) . "'>";
|
||||
|
||||
echo "<table>";
|
||||
echo " <tr>
|
||||
@@ -156,7 +232,7 @@ $kostenproStrichAnzeige = number_format(($settings['mark_price_cents'] ?? 20) /
|
||||
$name = saas_html($teilnehmer['display_name']);
|
||||
echo "<tr>";
|
||||
echo "<td><label for='anzahlStriche[$participantId]'>$name:</label></td>";
|
||||
echo "<td><input type='number' name='anzahlStriche[$participantId]' ></td>";
|
||||
echo "<td><input type='number' name='anzahlStriche[$participantId]' min='0' max='" . STRICHE_MAX_PRO_PERSON . "' step='1' value='" . saas_html($eingaben[$participantId] ?? '') . "'></td>";
|
||||
echo "</tr>";
|
||||
}
|
||||
echo "</table>";
|
||||
|
||||
Reference in New Issue
Block a user