impfwarteliste angepasst

This commit is contained in:
2026-03-23 17:14:09 +01:00
parent 70a78c9586
commit 4b4c1f74df
10 changed files with 1128 additions and 177 deletions
+255 -44
View File
@@ -35,6 +35,41 @@ if (!function_exists('impfTableHasIndex')) {
}
}
if (!function_exists('impfEnsureTable')) {
function impfEnsureTable(PDO $pdo, string $table, string $createSql): void
{
if (impfTableExists($pdo, $table)) {
return;
}
$pdo->exec($createSql);
}
}
if (!function_exists('impfNormalizeZeitraumIds')) {
function impfNormalizeZeitraumIds($zeitraumIds): array
{
if ($zeitraumIds === null) {
return [];
}
if (!is_array($zeitraumIds)) {
$zeitraumIds = [$zeitraumIds];
}
$result = [];
foreach ($zeitraumIds as $zeitraumId) {
$zeitraumId = (int)$zeitraumId;
if ($zeitraumId <= 0 || isset($result[$zeitraumId])) {
continue;
}
$result[$zeitraumId] = $zeitraumId;
}
return array_values($result);
}
}
if (!function_exists('impfWeekdayName')) {
function impfWeekdayName(int $day): string
{
@@ -55,21 +90,21 @@ if (!function_exists('impfWeekdayName')) {
if (!function_exists('impfWorkflowEnsureTables')) {
function impfWorkflowEnsureTables(PDO $pdo): void
{
$pdo->exec("CREATE TABLE IF NOT EXISTS impf_workflow_meta (
impfEnsureTable($pdo, 'impf_workflow_meta', "CREATE TABLE impf_workflow_meta (
meta_key VARCHAR(100) NOT NULL,
meta_value VARCHAR(255) NOT NULL DEFAULT '',
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (meta_key)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3");
$pdo->exec("CREATE TABLE IF NOT EXISTS impfstoff_workflow (
impfEnsureTable($pdo, 'impfstoff_workflow', "CREATE TABLE impfstoff_workflow (
impfstoff_id INT NOT NULL,
dosen_pro_flasche INT NOT NULL,
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (impfstoff_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3");
$pdo->exec("CREATE TABLE IF NOT EXISTS impfstoff_wochenplan (
impfEnsureTable($pdo, 'impfstoff_wochenplan', "CREATE TABLE impfstoff_wochenplan (
plan_id INT NOT NULL AUTO_INCREMENT,
impfstoff_id INT NOT NULL,
wochentag TINYINT NOT NULL,
@@ -83,7 +118,7 @@ if (!function_exists('impfWorkflowEnsureTables')) {
INDEX idx_impfstoff_wochenplan_wochentag (wochentag)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3");
$pdo->exec("CREATE TABLE IF NOT EXISTS impf_zeitraum (
impfEnsureTable($pdo, 'impf_zeitraum', "CREATE TABLE impf_zeitraum (
zeitraum_id INT NOT NULL AUTO_INCREMENT,
bezeichnung VARCHAR(120) NOT NULL DEFAULT '',
wochentag TINYINT NOT NULL,
@@ -101,7 +136,7 @@ if (!function_exists('impfWorkflowEnsureTables')) {
$pdo->exec("ALTER TABLE impf_zeitraum ADD COLUMN bezeichnung VARCHAR(120) NOT NULL DEFAULT '' AFTER zeitraum_id");
}
$pdo->exec("CREATE TABLE IF NOT EXISTS impf_zeitraum_impfstoff (
impfEnsureTable($pdo, 'impf_zeitraum_impfstoff', "CREATE TABLE impf_zeitraum_impfstoff (
zeitraum_id INT NOT NULL,
impfstoff_id INT NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
@@ -109,6 +144,18 @@ if (!function_exists('impfWorkflowEnsureTables')) {
INDEX idx_impf_zeitraum_impfstoff_impfstoff (impfstoff_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3");
impfEnsureTable($pdo, 'warteliste_zeitraum', "CREATE TABLE warteliste_zeitraum (
warteid INT NOT NULL,
zeitraum_id INT NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (warteid, zeitraum_id),
INDEX idx_warteliste_zeitraum_zeitraum (zeitraum_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3");
if (impfTableExists($pdo, 'warteliste_zeitraum') && !impfTableHasIndex($pdo, 'warteliste_zeitraum', 'idx_warteliste_zeitraum_zeitraum')) {
$pdo->exec("ALTER TABLE warteliste_zeitraum ADD INDEX idx_warteliste_zeitraum_zeitraum (zeitraum_id)");
}
if (impfTableExists($pdo, 'warteliste') && !impfTableHasColumn($pdo, 'warteliste', 'zeitraum_id')) {
$pdo->exec("ALTER TABLE warteliste ADD COLUMN zeitraum_id INT NULL AFTER impfenzeitraum");
}
@@ -117,6 +164,7 @@ if (!function_exists('impfWorkflowEnsureTables')) {
}
impfWorkflowMigrateLegacyPlans($pdo);
impfWorkflowMigrateLegacyWartelisteZeitraeume($pdo);
}
}
@@ -227,6 +275,42 @@ if (!function_exists('impfWorkflowMigrateLegacyPlans')) {
}
}
if (!function_exists('impfWorkflowMigrateLegacyWartelisteZeitraeume')) {
function impfWorkflowMigrateLegacyWartelisteZeitraeume(PDO $pdo): void
{
if (!impfTableExists($pdo, 'warteliste') || !impfTableExists($pdo, 'warteliste_zeitraum')) {
return;
}
if (impfWorkflowGetMeta($pdo, 'legacy_warteliste_zeitraeume_migrated') === '1') {
return;
}
$manageTransaction = !$pdo->inTransaction();
if ($manageTransaction) {
$pdo->beginTransaction();
}
try {
$pdo->exec("INSERT IGNORE INTO warteliste_zeitraum (warteid, zeitraum_id)
SELECT warteid, zeitraum_id
FROM warteliste
WHERE zeitraum_id IS NOT NULL
AND zeitraum_id > 0");
impfWorkflowSetMeta($pdo, 'legacy_warteliste_zeitraeume_migrated', '1');
if ($manageTransaction) {
$pdo->commit();
}
} catch (Throwable $e) {
if ($manageTransaction && $pdo->inTransaction()) {
$pdo->rollBack();
}
throw $e;
}
}
}
if (!function_exists('impfCsvToIntList')) {
function impfCsvToIntList(?string $csv): array
{
@@ -243,7 +327,7 @@ if (!function_exists('impfCsvToIntList')) {
}
if (!function_exists('impfZeitraumLabel')) {
function impfZeitraumLabel(array $zeitraum): string
function impfZeitraumLabel(array $zeitraum, bool $includeName = true): string
{
$zeitText = impfWeekdayName((int)$zeitraum['wochentag']) . ' ' . substr((string)$zeitraum['start'], 0, 5) . '-' . substr((string)$zeitraum['ende'], 0, 5);
$ort = trim((string)($zeitraum['anzeigename'] ?? '') . ' - ' . (string)($zeitraum['adresse'] ?? ''));
@@ -253,7 +337,7 @@ if (!function_exists('impfZeitraumLabel')) {
}
$bezeichnung = trim((string)($zeitraum['bezeichnung'] ?? ''));
if ($bezeichnung !== '') {
if ($includeName && $bezeichnung !== '') {
return $bezeichnung . ': ' . $zeitText;
}
@@ -388,7 +472,7 @@ if (!function_exists('impfGetWartelistenFormOptions')) {
}
$zeitfenster[$impfstoffId][] = [
'id' => (int)$zeitraum['zeitraum_id'],
'label' => (string)$zeitraum['label'],
'label' => impfZeitraumLabel($zeitraum, false),
];
}
}
@@ -406,12 +490,112 @@ if (!function_exists('impfGetWartelistenFormOptions')) {
}
}
if (!function_exists('impfGetWartelistenZeitraeume')) {
function impfGetWartelistenZeitraeume(PDO $pdo, int $warteid, bool $onlyActive = false): array
{
if ($warteid <= 0 || !impfTableExists($pdo, 'warteliste_zeitraum')) {
return [];
}
$sql = "SELECT z.zeitraum_id, z.bezeichnung, z.wochentag, z.start, z.ende, z.impfortid, z.aktiv, z.created_at,
o.anzeigename, o.adresse
FROM warteliste_zeitraum wz
INNER JOIN impf_zeitraum z ON z.zeitraum_id = wz.zeitraum_id
LEFT JOIN impfort o ON o.ortid = z.impfortid
WHERE wz.warteid = :warteid";
if ($onlyActive) {
$sql .= " AND z.aktiv = 1";
}
$sql .= " ORDER BY z.wochentag, z.start, z.ende, z.bezeichnung, z.zeitraum_id";
$st = $pdo->prepare($sql);
$st->execute(['warteid' => $warteid]);
$rows = $st->fetchAll(PDO::FETCH_ASSOC);
foreach ($rows as &$row) {
$row['label'] = impfZeitraumLabel($row);
}
unset($row);
if (!empty($rows)) {
return $rows;
}
$stFallback = $pdo->prepare("SELECT w.zeitraum_id, z.bezeichnung, z.wochentag, z.start, z.ende, z.impfortid, z.aktiv, z.created_at,
o.anzeigename, o.adresse
FROM warteliste w
LEFT JOIN impf_zeitraum z ON z.zeitraum_id = w.zeitraum_id
LEFT JOIN impfort o ON o.ortid = z.impfortid
WHERE w.warteid = :warteid
AND w.zeitraum_id IS NOT NULL
LIMIT 1");
$stFallback->execute(['warteid' => $warteid]);
$row = $stFallback->fetch(PDO::FETCH_ASSOC);
if (!$row) {
return [];
}
$row['label'] = impfZeitraumLabel($row);
return [$row];
}
}
if (!function_exists('impfGetWartelistenZeitraeumeLabels')) {
function impfGetWartelistenZeitraeumeLabels(PDO $pdo, int $warteid, bool $onlyActive = false): array
{
$rows = impfGetWartelistenZeitraeume($pdo, $warteid, $onlyActive);
return array_values(array_map(static function (array $row): string {
return (string)($row['label'] ?? '');
}, $rows));
}
}
if (!function_exists('impfSetWartelistenZeitraeume')) {
function impfSetWartelistenZeitraeume(PDO $pdo, int $warteid, $zeitraumIds): void
{
$zeitraumIds = impfNormalizeZeitraumIds($zeitraumIds);
if ($warteid <= 0) {
throw new InvalidArgumentException('Unguelige Wartelisten-ID.');
}
$manageTransaction = !$pdo->inTransaction();
if ($manageTransaction) {
$pdo->beginTransaction();
}
try {
$stDelete = $pdo->prepare("DELETE FROM warteliste_zeitraum WHERE warteid = :warteid");
$stDelete->execute(['warteid' => $warteid]);
if (!empty($zeitraumIds)) {
$stInsert = $pdo->prepare("INSERT INTO warteliste_zeitraum (warteid, zeitraum_id)
VALUES (:warteid, :zeitraum_id)");
foreach ($zeitraumIds as $zeitraumId) {
$stInsert->execute([
'warteid' => $warteid,
'zeitraum_id' => $zeitraumId,
]);
}
}
if ($manageTransaction) {
$pdo->commit();
}
} catch (Throwable $e) {
if ($manageTransaction && $pdo->inTransaction()) {
$pdo->rollBack();
}
throw $e;
}
}
}
if (!function_exists('impfCreateWaitlistEntryForPerson')) {
function impfCreateWaitlistEntryForPerson(
PDO $pdo,
int $personId,
int $impfstoffId,
int $zeitraumId,
$zeitraumIds,
int $impfart,
?string $letzteImpfung = null,
int $checked = 1
@@ -422,13 +606,15 @@ if (!function_exists('impfCreateWaitlistEntryForPerson')) {
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];
}
$zeitraumIds = impfNormalizeZeitraumIds($zeitraumIds);
if (empty($zeitraumIds)) {
return [false, 'Bitte mindestens ein Zeitfenster auswaehlen.', null];
}
$letzteImpfung = $letzteImpfung !== null ? trim($letzteImpfung) : null;
if ($impfart === 1) {
$letzteImpfung = null;
@@ -448,26 +634,32 @@ if (!function_exists('impfCreateWaitlistEntryForPerson')) {
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];
$zeitraumRows = [];
$zeitraumLabels = [];
foreach ($zeitraumIds as $zeitraumId) {
$row = impfLoadZeitraumById($pdo, $zeitraumId, true);
if (!$row) {
return [false, 'Mindestens ein ausgewaehltes Zeitfenster ist nicht mehr verfuegbar.', null];
}
if (!in_array($impfstoffId, $row['impfstoff_id_list'] ?? [], true)) {
return [false, 'Impfstoff und Zeitfenster passen nicht zusammen.', null];
}
$zeitraumRows[$zeitraumId] = $row;
$zeitraumLabels[] = (string)$row['label'];
}
$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
$stDup = $pdo->prepare("SELECT w.warteid
FROM warteliste w
LEFT JOIN warteliste_zeitraum wz ON wz.warteid = w.warteid
WHERE w.userid = :uid
AND w.checked IN (0, 1)
AND w.impfstoff = :impfstoff
AND w.impfart = :impfart
GROUP BY w.warteid
LIMIT 1");
$stDup->execute([
'uid' => $personId,
'impfstoff' => $impfstoffId,
'zeitraum_id' => $zeitraumId,
'impfart' => $impfart,
]);
if ($stDup->fetchColumn()) {
@@ -477,26 +669,45 @@ if (!function_exists('impfCreateWaitlistEntryForPerson')) {
$patientenart = ((int)($person['patientenart'] ?? 0) === 1) ? 1 : 0;
$hash = bin2hex(random_bytes(16));
$checkedValue = ($checked === 0) ? 0 : 1;
$primaerZeitraumId = (int)$zeitraumIds[0];
$impfenzeitraum = implode(' | ', $zeitraumLabels);
$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,
]);
$manageTransaction = !$pdo->inTransaction();
if ($manageTransaction) {
$pdo->beginTransaction();
}
$warteid = (int)$pdo->lastInsertId();
$personName = trim((string)$person['vorname'] . ' ' . (string)$person['nachname']);
try {
$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' => $impfenzeitraum,
'zeitraum_id' => $primaerZeitraumId,
]);
return [true, 'Wartelistenplatz fuer ' . $personName . ' wurde gespeichert.', $warteid];
$warteid = (int)$pdo->lastInsertId();
impfSetWartelistenZeitraeume($pdo, $warteid, $zeitraumIds);
if ($manageTransaction) {
$pdo->commit();
}
$personName = trim((string)$person['vorname'] . ' ' . (string)$person['nachname']);
return [true, 'Wartelistenplatz fuer ' . $personName . ' wurde gespeichert.', $warteid];
} catch (Throwable $e) {
if ($manageTransaction && $pdo->inTransaction()) {
$pdo->rollBack();
}
throw $e;
}
}
}
+2
View File
@@ -1262,6 +1262,8 @@ function Userspeichern($vorname, $nachname, $geburtstag, $mail, $tele, $ort, $pl
UPDATE persons
SET vorname=:vorname,
nachname=:nachname,
geburtstag=:geburtstag,
email=:email,
tele=:tele,
ort=:ort,
plz=:plz,
+256
View File
@@ -0,0 +1,256 @@
<?php
if (!function_exists('impfWorkflowNotificationEnsureMetaTable')) {
function impfWorkflowNotificationEnsureMetaTable(PDO $pdo): void
{
$pdo->exec("CREATE TABLE IF NOT EXISTS impf_workflow_meta (
meta_key VARCHAR(100) NOT NULL,
meta_value VARCHAR(255) NOT NULL DEFAULT '',
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (meta_key)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4");
}
}
if (!function_exists('impfWorkflowNotificationGetMeta')) {
function impfWorkflowNotificationGetMeta(PDO $pdo, string $key): ?string
{
impfWorkflowNotificationEnsureMetaTable($pdo);
$st = $pdo->prepare("SELECT meta_value
FROM impf_workflow_meta
WHERE meta_key = :meta_key
LIMIT 1");
$st->execute(['meta_key' => $key]);
$value = $st->fetchColumn();
return ($value === false) ? null : (string)$value;
}
}
if (!function_exists('impfWorkflowNotificationSetMeta')) {
function impfWorkflowNotificationSetMeta(PDO $pdo, string $key, string $value): void
{
impfWorkflowNotificationEnsureMetaTable($pdo);
$st = $pdo->prepare("INSERT INTO impf_workflow_meta (meta_key, meta_value)
VALUES (:meta_key, :meta_value)
ON DUPLICATE KEY UPDATE meta_value = VALUES(meta_value)");
$st->execute([
'meta_key' => $key,
'meta_value' => $value,
]);
}
}
if (!function_exists('impfWorkflowNotificationGetEmail')) {
function impfWorkflowNotificationGetEmail(PDO $pdo): string
{
return trim((string)(impfWorkflowNotificationGetMeta($pdo, 'benachrichtigung_email') ?? ''));
}
}
if (!function_exists('impfWorkflowNotificationSetEmail')) {
function impfWorkflowNotificationSetEmail(PDO $pdo, string $email): void
{
$email = trim($email);
impfWorkflowNotificationSetMeta($pdo, 'benachrichtigung_email', $email);
}
}
if (!function_exists('impfWorkflowNotificationIsReady')) {
function impfWorkflowNotificationIsReady(PDO $pdo): bool
{
return impfWorkflowNotificationGetEmail($pdo) !== '';
}
}
if (!function_exists('impfWorkflowNotificationShouldTrigger')) {
function impfWorkflowNotificationShouldTrigger(int $wartende, int $dosen): bool
{
if ($wartende < 5) {
return false;
}
if ($dosen > 5) {
return $wartende >= $dosen;
}
return true;
}
}
if (!function_exists('impfWorkflowNotificationStateKey')) {
function impfWorkflowNotificationStateKey(int $impfstoffId, int $zeitraumId): string
{
return 'notification_sent_' . $impfstoffId . '_' . $zeitraumId;
}
}
if (!function_exists('impfWorkflowNotificationCountWaitersForPlan')) {
function impfWorkflowNotificationCountWaitersForPlan(PDO $pdo, int $impfstoffId, int $zeitraumId): int
{
if ($impfstoffId <= 0 || $zeitraumId <= 0) {
return 0;
}
$st = $pdo->prepare("SELECT COUNT(DISTINCT w.userid)
FROM warteliste w
WHERE w.checked = 1
AND (w.impfstoff = :iid OR w.impfstoff = 0)
AND (
EXISTS (
SELECT 1
FROM warteliste_zeitraum wz
WHERE wz.warteid = w.warteid
AND wz.zeitraum_id = :zid
)
OR (
NOT EXISTS (
SELECT 1
FROM warteliste_zeitraum wz_none
WHERE wz_none.warteid = w.warteid
)
AND (w.zeitraum_id = :zid OR w.zeitraum_id IS NULL)
)
)");
$st->execute([
'iid' => $impfstoffId,
'zid' => $zeitraumId,
]);
return (int)$st->fetchColumn();
}
}
if (!function_exists('impfWorkflowNotificationSendForPlan')) {
function impfWorkflowNotificationSendForPlan(
PDO $pdo,
int $impfstoffId,
string $impfstoffName,
int $zeitraumId,
string $zeitraumLabel,
int $wartende,
int $dosen
): array {
if ($impfstoffId <= 0 || $zeitraumId <= 0) {
return [];
}
$stateKey = impfWorkflowNotificationStateKey($impfstoffId, $zeitraumId);
$alreadySent = impfWorkflowNotificationGetMeta($pdo, $stateKey) === '1';
$shouldTrigger = impfWorkflowNotificationShouldTrigger($wartende, $dosen);
if (!$shouldTrigger) {
if ($alreadySent) {
impfWorkflowNotificationSetMeta($pdo, $stateKey, '0');
}
return [];
}
if ($alreadySent) {
return [];
}
$email = impfWorkflowNotificationGetEmail($pdo);
if ($email === '') {
return [];
}
$thresholdText = ($dosen > 5)
? 'Die Flasche hat mehr als 5 Dosen, daher wird erst bei einer vollen Flasche benachrichtigt.'
: 'Es sind mindestens 5 Interessenten fuer dieses Zeitfenster vorhanden.';
$subject = 'Impfworkflow: Warteliste ist bereit fuer ' . $impfstoffName;
$body = '<p>Fuer den Impfworkflow ist ein Zeitfenster benachrichtigungsreif.</p>'
. '<p><strong>Impfstoff:</strong> ' . htmlspecialchars($impfstoffName, ENT_QUOTES, 'UTF-8') . '<br>'
. '<strong>Zeitfenster:</strong> ' . htmlspecialchars($zeitraumLabel, ENT_QUOTES, 'UTF-8') . '<br>'
. '<strong>Interessenten:</strong> ' . $wartende . '<br>'
. '<strong>Dosen pro Flasche:</strong> ' . $dosen . '</p>'
. '<p>' . htmlspecialchars($thresholdText, ENT_QUOTES, 'UTF-8') . '</p>';
if (!SendMailMessage($pdo, $email, $subject, $body)) {
throw new RuntimeException('Benachrichtigungs-E-Mail konnte nicht versendet werden.');
}
impfWorkflowNotificationSetMeta($pdo, $stateKey, '1');
return [[
'impfstoff_id' => $impfstoffId,
'zeitraum_id' => $zeitraumId,
'email' => $email,
'impfstoff' => $impfstoffName,
'zeitraum' => $zeitraumLabel,
'wartende' => $wartende,
'dosen' => $dosen,
]];
}
}
if (!function_exists('impfWorkflowNotificationProcess')) {
function impfWorkflowNotificationProcess(PDO $pdo, int $impfstoffId = 0, array $zeitraumIds = []): array
{
if (!function_exists('impfGetZeitraeumeByImpfstoff') || !function_exists('impfLoadZeitraumById')) {
return [];
}
$zeitraumIds = array_values(array_unique(array_filter(array_map('intval', $zeitraumIds), static function (int $zeitraumId): bool {
return $zeitraumId > 0;
})));
$sql = "SELECT r.impfstoff_id, r.dosen_pro_flasche, 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)";
$params = [];
if ($impfstoffId > 0) {
$sql .= " AND r.impfstoff_id = :iid";
$params['iid'] = $impfstoffId;
}
$sql .= " ORDER BY i.impfname";
$stRules = $pdo->prepare($sql);
$stRules->execute($params);
$rules = $stRules->fetchAll(PDO::FETCH_ASSOC);
if (empty($rules)) {
return [];
}
$zeitraeumeByImpfstoff = impfGetZeitraeumeByImpfstoff($pdo, true);
$sent = [];
foreach ($rules as $rule) {
$currentImpfstoffId = (int)$rule['impfstoff_id'];
$dosen = (int)$rule['dosen_pro_flasche'];
if ($currentImpfstoffId <= 0 || $dosen <= 0 || empty($zeitraeumeByImpfstoff[$currentImpfstoffId])) {
continue;
}
foreach ($zeitraeumeByImpfstoff[$currentImpfstoffId] as $zeitraum) {
$currentZeitraumId = (int)($zeitraum['zeitraum_id'] ?? 0);
if ($currentZeitraumId <= 0) {
continue;
}
if (!empty($zeitraumIds) && !in_array($currentZeitraumId, $zeitraumIds, true)) {
continue;
}
$wartende = impfWorkflowNotificationCountWaitersForPlan($pdo, $currentImpfstoffId, $currentZeitraumId);
$sent = array_merge(
$sent,
impfWorkflowNotificationSendForPlan(
$pdo,
$currentImpfstoffId,
(string)$rule['impfname'],
$currentZeitraumId,
(string)($zeitraum['label'] ?? ''),
$wartende,
$dosen
)
);
}
}
return $sent;
}
}