diff --git a/admin/impfworkflow.php b/admin/impfworkflow.php index 86f8342..6e77124 100644 --- a/admin/impfworkflow.php +++ b/admin/impfworkflow.php @@ -235,7 +235,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') { } else { $dosen = (int)$rule['dosen_pro_flasche']; - $stCount = $pdo->prepare("SELECT COUNT(*) + $stCount = $pdo->prepare("SELECT COUNT(DISTINCT userid) FROM warteliste WHERE checked = 1 AND (impfstoff = :iid OR impfstoff = 0) @@ -291,16 +291,27 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') { WHERE checked = 1 AND (impfstoff = :iid OR impfstoff = 0) AND (zeitraum_id = :zid OR zeitraum_id IS NULL) - ORDER BY date_created ASC, warteid ASC - LIMIT :lim"); + ORDER BY date_created ASC, warteid ASC"); $stW->bindValue(':iid', $impfstoffId, PDO::PARAM_INT); $stW->bindValue(':zid', $planId, PDO::PARAM_INT); - $stW->bindValue(':lim', $dosen, PDO::PARAM_INT); $stW->execute(); - $warteRows = $stW->fetchAll(PDO::FETCH_ASSOC); + $warteRowsRaw = $stW->fetchAll(PDO::FETCH_ASSOC); + $warteRows = []; + $seenUserIds = []; + foreach ($warteRowsRaw as $warteRow) { + $warteUserId = (int)$warteRow['userid']; + if (isset($seenUserIds[$warteUserId])) { + continue; + } + $seenUserIds[$warteUserId] = true; + $warteRows[] = $warteRow; + if (count($warteRows) >= $dosen) { + break; + } + } if (count($warteRows) < $dosen) { - throw new RuntimeException("Warteliste während Verarbeitung zu klein geworden."); + throw new RuntimeException("Warteliste waehrend Verarbeitung zu klein geworden."); } $stInsTermin = $pdo->prepare("INSERT INTO impftermin @@ -540,7 +551,7 @@ $eventOverview = []; try { $stRules = $pdo->prepare("SELECT r.impfstoff_id, r.dosen_pro_flasche, i.impfname, - COALESCE((SELECT COUNT(*) FROM warteliste w WHERE w.checked = 1 AND (w.impfstoff = r.impfstoff_id OR w.impfstoff = 0)),0) AS wartende + COALESCE((SELECT COUNT(DISTINCT w.userid) FROM warteliste w WHERE w.checked = 1 AND (w.impfstoff = r.impfstoff_id OR w.impfstoff = 0)),0) AS wartende FROM impfstoff_workflow r INNER JOIN impfstoff i ON i.impfid = r.impfstoff_id WHERE (i.aktiv = 1 OR i.aktivwarteliste = 1 OR i.aktivtermin = 1 OR i.aktivgrippe = 1) diff --git a/admin/impfworkflow_stammdaten.php b/admin/impfworkflow_stammdaten.php index d25a6ce..65eb402 100644 --- a/admin/impfworkflow_stammdaten.php +++ b/admin/impfworkflow_stammdaten.php @@ -411,7 +411,7 @@ $allImpforte = getImpforte($pdo, false); $zeitraeume = impfGetZeitraumRows($pdo, true); $stRules = $pdo->prepare("SELECT r.impfstoff_id, r.dosen_pro_flasche, i.impfname, - COALESCE((SELECT COUNT(*) FROM warteliste w WHERE w.checked = 1 AND (w.impfstoff = r.impfstoff_id OR w.impfstoff = 0)),0) AS wartende + COALESCE((SELECT COUNT(DISTINCT w.userid) FROM warteliste w WHERE w.checked = 1 AND (w.impfstoff = r.impfstoff_id OR w.impfstoff = 0)),0) AS wartende FROM impfstoff_workflow r INNER JOIN impfstoff i ON i.impfid = r.impfstoff_id WHERE (i.aktiv = 1 OR i.aktivwarteliste = 1 OR i.aktivtermin = 1 OR i.aktivgrippe = 1) diff --git a/inc/functions.impfen.inc.php b/inc/functions.impfen.inc.php index d11df9a..ceb70d8 100644 --- a/inc/functions.impfen.inc.php +++ b/inc/functions.impfen.inc.php @@ -358,3 +358,145 @@ if (!function_exists('impfGetZeitraeumeByImpfstoff')) { return $result; } } + +if (!function_exists('impfGetWartelistenFormOptions')) { + function impfGetWartelistenFormOptions(PDO $pdo): array + { + $impfstoffe = []; + $zeitfenster = []; + + $stImpfstoffe = $pdo->prepare("SELECT r.impfstoff_id, i.impfname + FROM impfstoff_workflow r + INNER JOIN impfstoff i ON i.impfid = r.impfstoff_id + WHERE (i.aktiv = 1 OR i.aktivwarteliste = 1 OR i.aktivtermin = 1 OR i.aktivgrippe = 1) + AND r.dosen_pro_flasche > 0 + ORDER BY i.impfname"); + $stImpfstoffe->execute(); + foreach ($stImpfstoffe->fetchAll(PDO::FETCH_ASSOC) as $row) { + $impfstoffe[(int)$row['impfstoff_id']] = (string)$row['impfname']; + } + + $zeitraeumeByImpfstoff = impfGetZeitraeumeByImpfstoff($pdo, true); + foreach ($zeitraeumeByImpfstoff as $impfstoffId => $zeitraeume) { + if (!isset($impfstoffe[$impfstoffId])) { + continue; + } + + foreach ($zeitraeume as $zeitraum) { + if (!isset($zeitfenster[$impfstoffId])) { + $zeitfenster[$impfstoffId] = []; + } + $zeitfenster[$impfstoffId][] = [ + 'id' => (int)$zeitraum['zeitraum_id'], + 'label' => (string)$zeitraum['label'], + ]; + } + } + + foreach (array_keys($impfstoffe) as $impfstoffId) { + if (empty($zeitfenster[$impfstoffId])) { + unset($impfstoffe[$impfstoffId]); + } + } + + return [ + 'impfstoffe' => $impfstoffe, + 'zeitfenster' => $zeitfenster, + ]; + } +} + +if (!function_exists('impfCreateWaitlistEntryForPerson')) { + function impfCreateWaitlistEntryForPerson( + PDO $pdo, + int $personId, + int $impfstoffId, + int $zeitraumId, + int $impfart, + ?string $letzteImpfung = null, + int $checked = 1 + ): array { + if ($personId <= 0) { + return [false, 'Keine gueltige Person uebergeben.', null]; + } + if ($impfstoffId <= 0) { + return [false, 'Bitte einen Impfstoff auswaehlen.', null]; + } + if ($zeitraumId <= 0) { + return [false, 'Bitte ein Zeitfenster auswaehlen.', null]; + } + if ($impfart < 1 || $impfart > 4) { + return [false, 'Bitte eine gueltige Impfungsart auswaehlen.', null]; + } + + $letzteImpfung = $letzteImpfung !== null ? trim($letzteImpfung) : null; + if ($impfart === 1) { + $letzteImpfung = null; + } elseif ($letzteImpfung === null || $letzteImpfung === '') { + return [false, 'Bitte das Datum der letzten Impfung angeben.', null]; + } elseif (!preg_match('/^\d{4}-\d{2}-\d{2}$/', $letzteImpfung)) { + return [false, 'Das Datum der letzten Impfung ist ungueltig.', null]; + } + + $stPerson = $pdo->prepare("SELECT person_id, patientenart, vorname, nachname + FROM persons + WHERE person_id = :pid + LIMIT 1"); + $stPerson->execute(['pid' => $personId]); + $person = $stPerson->fetch(PDO::FETCH_ASSOC); + if (!$person) { + return [false, 'Die Person wurde nicht gefunden.', null]; + } + + $zeitraum = impfLoadZeitraumById($pdo, $zeitraumId, true); + if (!$zeitraum) { + return [false, 'Das ausgewaehlte Zeitfenster ist nicht mehr verfuegbar.', null]; + } + if (!in_array($impfstoffId, $zeitraum['impfstoff_id_list'] ?? [], true)) { + return [false, 'Impfstoff und Zeitfenster passen nicht zusammen.', null]; + } + + $stDup = $pdo->prepare("SELECT warteid + FROM warteliste + WHERE userid = :uid + AND checked IN (0, 1) + AND impfstoff = :impfstoff + AND COALESCE(zeitraum_id, 0) = :zeitraum_id + AND impfart = :impfart + LIMIT 1"); + $stDup->execute([ + 'uid' => $personId, + 'impfstoff' => $impfstoffId, + 'zeitraum_id' => $zeitraumId, + 'impfart' => $impfart, + ]); + if ($stDup->fetchColumn()) { + return [false, 'Diese Anfrage ist fuer diese Person bereits auf der aktiven Warteliste vorhanden.', null]; + } + + $patientenart = ((int)($person['patientenart'] ?? 0) === 1) ? 1 : 0; + $hash = bin2hex(random_bytes(16)); + $checkedValue = ($checked === 0) ? 0 : 1; + + $stInsert = $pdo->prepare("INSERT INTO warteliste + (userid, checked, hash, impfenangebot, impfstoff, Patientenart, Impfaufklaerung, WeitereFragen, impfart, impfenmit, letzteimpfung, impfenzeitraum, zeitraum_id, date_created) + VALUES + (:userid, :checked, :hash, 1, :impfstoff, :patientenart, 0, 0, :impfart, '', :letzteimpfung, :impfenzeitraum, :zeitraum_id, NOW())"); + $stInsert->execute([ + 'userid' => $personId, + 'checked' => $checkedValue, + 'hash' => $hash, + 'impfstoff' => $impfstoffId, + 'patientenart' => $patientenart, + 'impfart' => $impfart, + 'letzteimpfung' => $letzteImpfung, + 'impfenzeitraum' => (string)$zeitraum['label'], + 'zeitraum_id' => $zeitraumId, + ]); + + $warteid = (int)$pdo->lastInsertId(); + $personName = trim((string)$person['vorname'] . ' ' . (string)$person['nachname']); + + return [true, 'Wartelistenplatz fuer ' . $personName . ' wurde gespeichert.', $warteid]; + } +} diff --git a/intern/impfwarteliste.php b/intern/impfwarteliste.php new file mode 100644 index 0000000..11516ee --- /dev/null +++ b/intern/impfwarteliste.php @@ -0,0 +1,283 @@ +"; +echo "
Hallo " . e((string)($user['vorname'] ?? '')) . ",
Hier koennen Sie sich direkt aus dem internen Bereich in die Impfwarteliste eintragen lassen. Ihre Stammdaten werden aus Ihrem Kundenkonto uebernommen.
"; + +echo "| Impfstoff | Zeitraum | Impfungsart | Status | Letzte Impfung |
|---|---|---|---|---|
| " . e((string)($activeWaitRow['impfname'] ?? 'Unbekannt')) . " | "; + echo "" . e((string)($activeWaitRow['impfenzeitraum'] ?? '')) . " | "; + echo "" . e((string)($impfartLabels[(int)$activeWaitRow['impfart']] ?? ('Status ' . (int)$activeWaitRow['impfart']))) . " | "; + echo "" . e($statusText) . " | "; + echo "" . e((string)($activeWaitRow['letzteimpfung'] ?? '')) . " | "; + echo "
Neue Anfragen können Sie über diesen Button einreichen:
Wenn Sie sich für eine Impfung vormerken lassen möchten, können Sie sich hier direkt in die Warteliste eintragen:
Hier können Sie Ihre Anfragen einsehen. Die Antwort erhalten Sie per E-Mail.