impfwarteliste angepasst
This commit is contained in:
+255
-44
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user