- Neue Buchung direkt aus der Buchungsansicht (links neben Stornieren) - optionaler, abweichender Mitarbeiter für Abholung/Rücknahme (bookings.return_employee_id, Migration 006); Rücknahme-Erinnerungen inkl. Mail/Push gehen an diese Person - Begrüßung auf der Übersicht mit Vor- und Nachnamen - Übergaben-Kennzahl zählt Übergaben und Rücknahmen je zuständigem Mitarbeiter, offene Erinnerungen bleiben mitarbeitergefiltert - Verfügbarkeit weist Wechseltage (Randtage) mit Buchungsdetails aus - "Bis" wird nach Auswahl von "Von" auf den Folgetag gesetzt und der Datepicker öffnet im passenden Monat - Monat/Jahr im Kalenderkopf kontraststärker - ROADMAP.md abgehakt, README und Architektur ergänzt
296 lines
9.7 KiB
PHP
296 lines
9.7 KiB
PHP
<?php
|
||
declare(strict_types=1);
|
||
|
||
/** Web-Push-Abos, Versand und Admin-Auswertung (VAPID). */
|
||
|
||
function push_is_available(): bool
|
||
{
|
||
static $available = null;
|
||
if ($available !== null) {
|
||
return $available;
|
||
}
|
||
try {
|
||
db()->query('SELECT 1 FROM push_subscriptions LIMIT 1');
|
||
$available = true;
|
||
} catch (Throwable) {
|
||
$available = false;
|
||
}
|
||
return $available;
|
||
}
|
||
|
||
function push_is_configured(): bool
|
||
{
|
||
return push_is_available() && WebPush::isConfigured();
|
||
}
|
||
|
||
function push_subscription_save(int $userId, string $endpoint, string $p256dh, string $auth, string $userAgent = ''): bool
|
||
{
|
||
if (!push_is_available() || !WebPush::isValidEndpoint($endpoint) || !WebPush::isValidKeyMaterial($p256dh, $auth)) {
|
||
return false;
|
||
}
|
||
$stmt = db()->prepare(
|
||
'INSERT INTO push_subscriptions
|
||
(user_id, endpoint, endpoint_hash, p256dh, auth, user_agent, created_at, updated_at)
|
||
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
||
ON DUPLICATE KEY UPDATE
|
||
user_id = VALUES(user_id), endpoint = VALUES(endpoint), p256dh = VALUES(p256dh),
|
||
auth = VALUES(auth), user_agent = VALUES(user_agent), updated_at = VALUES(updated_at),
|
||
last_error = NULL'
|
||
);
|
||
return $stmt->execute([
|
||
$userId, $endpoint, hash('sha256', $endpoint), $p256dh, $auth,
|
||
mb_substr($userAgent, 0, 255), utc_now(), utc_now(),
|
||
]);
|
||
}
|
||
|
||
function push_subscription_delete(int $userId, string $endpoint): int
|
||
{
|
||
if (!push_is_available() || $endpoint === '') {
|
||
return 0;
|
||
}
|
||
$stmt = db()->prepare('DELETE FROM push_subscriptions WHERE user_id = ? AND endpoint_hash = ?');
|
||
$stmt->execute([$userId, hash('sha256', $endpoint)]);
|
||
return $stmt->rowCount();
|
||
}
|
||
|
||
function push_subscription_delete_all(int $userId): int
|
||
{
|
||
if (!push_is_available()) {
|
||
return 0;
|
||
}
|
||
$stmt = db()->prepare('DELETE FROM push_subscriptions WHERE user_id = ?');
|
||
$stmt->execute([$userId]);
|
||
return $stmt->rowCount();
|
||
}
|
||
|
||
function push_subscription_count(int $userId): int
|
||
{
|
||
if (!push_is_available()) {
|
||
return 0;
|
||
}
|
||
$stmt = db()->prepare('SELECT COUNT(*) FROM push_subscriptions WHERE user_id = ?');
|
||
$stmt->execute([$userId]);
|
||
return (int) $stmt->fetchColumn();
|
||
}
|
||
|
||
/** @return array<int,int> user_id => Anzahl Geräte */
|
||
function push_counts_by_user(): array
|
||
{
|
||
if (!push_is_available()) {
|
||
return [];
|
||
}
|
||
$rows = db()->query('SELECT user_id, COUNT(*) AS devices FROM push_subscriptions GROUP BY user_id')->fetchAll();
|
||
$out = [];
|
||
foreach ($rows as $row) {
|
||
$out[(int) $row['user_id']] = (int) $row['devices'];
|
||
}
|
||
return $out;
|
||
}
|
||
|
||
function push_last_update_by_user(): array
|
||
{
|
||
if (!push_is_available()) {
|
||
return [];
|
||
}
|
||
$rows = db()->query('SELECT user_id, MAX(updated_at) AS last_update FROM push_subscriptions GROUP BY user_id')->fetchAll();
|
||
$out = [];
|
||
foreach ($rows as $row) {
|
||
$out[(int) $row['user_id']] = (string) $row['last_update'];
|
||
}
|
||
return $out;
|
||
}
|
||
|
||
function push_subscriptions_for_user(int $userId): array
|
||
{
|
||
if (!push_is_available()) {
|
||
return [];
|
||
}
|
||
$stmt = db()->prepare('SELECT * FROM push_subscriptions WHERE user_id = ? ORDER BY updated_at DESC');
|
||
$stmt->execute([$userId]);
|
||
return $stmt->fetchAll();
|
||
}
|
||
|
||
function push_subscription_for_user_endpoint(int $userId, string $endpoint): ?array
|
||
{
|
||
if (!push_is_available() || $endpoint === '') {
|
||
return null;
|
||
}
|
||
$stmt = db()->prepare('SELECT * FROM push_subscriptions WHERE user_id = ? AND endpoint_hash = ? LIMIT 1');
|
||
$stmt->execute([$userId, hash('sha256', $endpoint)]);
|
||
$row = $stmt->fetch();
|
||
return $row ?: null;
|
||
}
|
||
|
||
function push_delete_subscription_row(int $id): void
|
||
{
|
||
if (!push_is_available()) {
|
||
return;
|
||
}
|
||
db()->prepare('DELETE FROM push_subscriptions WHERE id = ?')->execute([$id]);
|
||
}
|
||
|
||
function push_mark_result(int $id, bool $ok, string $error = ''): void
|
||
{
|
||
if (!push_is_available()) {
|
||
return;
|
||
}
|
||
if ($ok) {
|
||
db()->prepare('UPDATE push_subscriptions SET last_success_at = ?, last_error = NULL WHERE id = ?')
|
||
->execute([utc_now(), $id]);
|
||
} else {
|
||
db()->prepare('UPDATE push_subscriptions SET last_error = ? WHERE id = ?')
|
||
->execute([mb_substr($error, 0, 255), $id]);
|
||
}
|
||
}
|
||
|
||
/** @return array{sent:int,failed:int,removed:int} */
|
||
function push_send_to_user(int $userId, array $payload): array
|
||
{
|
||
$result = ['sent' => 0, 'failed' => 0, 'removed' => 0];
|
||
if (!push_is_configured()) {
|
||
return $result;
|
||
}
|
||
foreach (push_subscriptions_for_user($userId) as $subscription) {
|
||
$send = WebPush::send($subscription, $payload);
|
||
if ($send['ok']) {
|
||
$result['sent']++;
|
||
push_mark_result((int) $subscription['id'], true);
|
||
} elseif ($send['gone']) {
|
||
$result['removed']++;
|
||
push_delete_subscription_row((int) $subscription['id']);
|
||
} else {
|
||
$result['failed']++;
|
||
push_mark_result((int) $subscription['id'], false, (string) $send['error']);
|
||
}
|
||
}
|
||
return $result;
|
||
}
|
||
|
||
/** @return int[] Benutzer-IDs, die dem Mitarbeiter zugeordnet sind */
|
||
function push_users_for_employee(int $employeeId): array
|
||
{
|
||
$stmt = db()->prepare("SELECT id FROM users WHERE employee_id = ? AND status = 'active'");
|
||
$stmt->execute([$employeeId]);
|
||
return array_map('intval', $stmt->fetchAll(PDO::FETCH_COLUMN));
|
||
}
|
||
|
||
function push_payload_for_reminder(array $booking, array $reminder): array
|
||
{
|
||
$body = $reminder['message'] . ' '
|
||
. $booking['customer_name'] . ' · ' . $booking['trailer_label'] . ' · '
|
||
. fmt_date_range($booking['start_date'], $booking['end_date']);
|
||
return [
|
||
'title' => 'Drink it cold – ' . $reminder['label'],
|
||
'body' => $body,
|
||
'url' => '/bookings/' . (int) $booking['id'],
|
||
'tag' => 'dic-reminder-' . (int) $booking['id'] . '-' . $reminder['kind'],
|
||
'icon' => '/assets/icons/icon-192.png',
|
||
'badge' => '/assets/icons/icon-192.png',
|
||
];
|
||
}
|
||
|
||
/**
|
||
* Verschickt alle fälligen, noch nicht erledigten Erinnerungen per Push.
|
||
* Dedupliziert über reminder_push_log (Buchung + Art + Fälligkeitsdatum).
|
||
*
|
||
* @return array{sent:int,failed:int,removed:int,skipped_done:int,skipped_user:int,skipped_no_subscription:int,logged:int}
|
||
*/
|
||
function push_dispatch_due(int $lookbackDays = 7): array
|
||
{
|
||
$result = [
|
||
'sent' => 0, 'failed' => 0, 'removed' => 0,
|
||
'skipped_done' => 0, 'skipped_user' => 0, 'skipped_no_subscription' => 0, 'logged' => 0,
|
||
];
|
||
if (!push_is_configured()) {
|
||
return $result;
|
||
}
|
||
|
||
$today = today_iso();
|
||
$from = add_days($today, -max(1, $lookbackDays));
|
||
$rows = reminder_list($from, $today, 0, false);
|
||
if (!$rows) {
|
||
return $result;
|
||
}
|
||
|
||
// Alte Dedupe-Einträge gelegentlich aufräumen.
|
||
db()->prepare('DELETE FROM reminder_push_log WHERE due_date < ?')->execute([add_days($today, -365)]);
|
||
|
||
foreach ($rows as $row) {
|
||
$booking = $row['booking'];
|
||
// Rücknahme-Erinnerungen gehen ggf. an den abweichend zugewiesenen Mitarbeiter.
|
||
$employeeId = booking_employee_for_kind($booking, (string) $row['kind']);
|
||
$userIds = push_users_for_employee($employeeId);
|
||
if (!$userIds) {
|
||
$result['skipped_user']++;
|
||
continue;
|
||
}
|
||
|
||
$stmt = db()->prepare('SELECT 1 FROM reminder_push_log WHERE booking_id = ? AND kind = ? AND due_date = ?');
|
||
$stmt->execute([(int) $booking['id'], $row['kind'], $row['due_date']]);
|
||
if ($stmt->fetchColumn()) {
|
||
$result['skipped_done']++;
|
||
continue;
|
||
}
|
||
|
||
$payload = push_payload_for_reminder($booking, $row);
|
||
$delivered = 0;
|
||
$removed = 0;
|
||
$failed = 0;
|
||
foreach ($userIds as $userId) {
|
||
$send = push_send_to_user($userId, $payload);
|
||
$delivered += $send['sent'];
|
||
$removed += $send['removed'];
|
||
$failed += $send['failed'];
|
||
}
|
||
$result['removed'] += $removed;
|
||
$result['failed'] += $failed;
|
||
|
||
if ($delivered === 0) {
|
||
if ($removed === 0 && $failed === 0) {
|
||
$result['skipped_no_subscription']++;
|
||
}
|
||
continue;
|
||
}
|
||
|
||
$result['sent'] += $delivered;
|
||
db()->prepare(
|
||
'INSERT IGNORE INTO reminder_push_log (booking_id, kind, due_date, employee_id, user_id, sent_at)
|
||
VALUES (?, ?, ?, ?, ?, ?)'
|
||
)->execute([
|
||
(int) $booking['id'], $row['kind'], $row['due_date'],
|
||
$employeeId, $userIds[0], utc_now(),
|
||
]);
|
||
$result['logged']++;
|
||
}
|
||
|
||
return $result;
|
||
}
|
||
|
||
/** @return array{sent:int,failed:int,removed:int} */
|
||
function push_send_test(int $userId, string $endpoint): array
|
||
{
|
||
$result = ['sent' => 0, 'failed' => 0, 'removed' => 0];
|
||
$subscription = push_subscription_for_user_endpoint($userId, $endpoint);
|
||
if (!$subscription || !push_is_configured()) {
|
||
return $result;
|
||
}
|
||
$send = WebPush::send($subscription, [
|
||
'title' => 'Drink it cold – Test-Erinnerung',
|
||
'body' => 'Push-Benachrichtigungen sind auf diesem Gerät aktiv. Erinnerungen erscheinen künftig hier.',
|
||
'url' => '/reminders',
|
||
'tag' => 'dic-push-test',
|
||
'icon' => '/assets/icons/icon-192.png',
|
||
'badge' => '/assets/icons/icon-192.png',
|
||
]);
|
||
if ($send['ok']) {
|
||
$result['sent'] = 1;
|
||
push_mark_result((int) $subscription['id'], true);
|
||
} elseif ($send['gone']) {
|
||
$result['removed'] = 1;
|
||
push_delete_subscription_row((int) $subscription['id']);
|
||
} else {
|
||
$result['failed'] = 1;
|
||
push_mark_result((int) $subscription['id'], false, (string) $send['error']);
|
||
}
|
||
return $result;
|
||
}
|