Drink it cold: Terminplanung für Kühlanhänger
This commit is contained in:
commit
ec88c8ebac
55 changed files with 7091 additions and 0 deletions
122
app/mail.php
Normal file
122
app/mail.php
Normal file
|
|
@ -0,0 +1,122 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Optionaler täglicher E-Mail-Digest für fällige Erinnerungen.
|
||||
*
|
||||
* Die In-App-Erinnerungen sind immer vorhanden. Dieser Versand ist zusätzlich
|
||||
* und wird über tools/send_reminders.php (Cron) oder /cron/reminders?token=…
|
||||
* ausgelöst. Jede Erinnerung wird genau einmal verschickt; das Protokoll steht
|
||||
* in reminder_mail_log.
|
||||
*/
|
||||
|
||||
function reminder_mail_recipients(): array
|
||||
{
|
||||
$sql = "SELECT u.employee_id, u.email, u.first_name, u.last_name, e.name AS employee_name
|
||||
FROM users u JOIN employees e ON e.id = u.employee_id
|
||||
WHERE u.status = 'active' AND u.employee_id IS NOT NULL AND u.email IS NOT NULL AND u.email <> ''
|
||||
ORDER BY e.sort_order";
|
||||
$out = [];
|
||||
foreach (db()->query($sql)->fetchAll() as $row) {
|
||||
$out[(int) $row['employee_id']] = $row;
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Verschickt alle offenen, heute (oder in den letzten 7 Tagen) fälligen
|
||||
* Erinnerungen als einen Digest pro Mitarbeiter.
|
||||
*
|
||||
* @return array{enabled: bool, sent: int, skipped: int, errors: array, details: array}
|
||||
*/
|
||||
function reminder_send_due(int $lookbackDays = 7): array
|
||||
{
|
||||
$result = ['enabled' => (bool) config('app.mail_enabled', false), 'sent' => 0, 'skipped' => 0, 'errors' => [], 'details' => []];
|
||||
if (!$result['enabled']) {
|
||||
return $result;
|
||||
}
|
||||
|
||||
$today = today_iso();
|
||||
$from = add_days($today, -max(1, $lookbackDays));
|
||||
$recipients = reminder_mail_recipients();
|
||||
$rows = reminder_list($from, $today, 0, false);
|
||||
if (!$rows) {
|
||||
return $result;
|
||||
}
|
||||
|
||||
$byEmployee = [];
|
||||
foreach ($rows as $row) {
|
||||
$employeeId = (int) $row['booking']['employee_id'];
|
||||
if (!isset($recipients[$employeeId])) {
|
||||
$result['skipped']++;
|
||||
continue;
|
||||
}
|
||||
// Bereits verschickt?
|
||||
$stmt = db()->prepare('SELECT 1 FROM reminder_mail_log WHERE booking_id = ? AND kind = ? AND due_date = ?');
|
||||
$stmt->execute([(int) $row['booking']['id'], $row['kind'], $row['due_date']]);
|
||||
if ($stmt->fetchColumn()) {
|
||||
$result['skipped']++;
|
||||
continue;
|
||||
}
|
||||
$byEmployee[$employeeId][] = $row;
|
||||
}
|
||||
|
||||
$fromHeader = (string) config('app.mail_from', 'Drink it cold <noreply@localhost>');
|
||||
$appUrl = rtrim((string) config('app.public_url', ''), '/');
|
||||
|
||||
foreach ($byEmployee as $employeeId => $items) {
|
||||
$recipient = $recipients[$employeeId];
|
||||
$lines = [
|
||||
'Guten Tag ' . $recipient['employee_name'] . ',',
|
||||
'',
|
||||
'diese Erinnerungen stehen an:',
|
||||
'',
|
||||
];
|
||||
foreach ($items as $item) {
|
||||
$booking = $item['booking'];
|
||||
$lines[] = '• ' . fmt_date($item['due_date']) . ' – ' . $item['label'];
|
||||
$lines[] = ' ' . $item['message'];
|
||||
$lines[] = ' ' . $booking['customer_name'] . ', ' . $booking['trailer_label'] . ' (' . $booking['trailer_color_name'] . ')';
|
||||
$lines[] = ' Miete: ' . fmt_date($booking['start_date']) . ' bis ' . fmt_date($booking['end_date']);
|
||||
if ($appUrl !== '') {
|
||||
$lines[] = ' ' . $appUrl . '/bookings/' . (int) $booking['id'];
|
||||
}
|
||||
$lines[] = '';
|
||||
}
|
||||
$lines[] = 'Diese Nachricht wurde automatisch von der Terminplanung erzeugt.';
|
||||
$body = implode("\n", $lines);
|
||||
$subject = 'Drink it cold – ' . count($items) . ' fällige Erinnerung' . (count($items) === 1 ? '' : 'en');
|
||||
|
||||
$headers = [
|
||||
'From: ' . $fromHeader,
|
||||
'MIME-Version: 1.0',
|
||||
'Content-Type: text/plain; charset=UTF-8',
|
||||
'Content-Transfer-Encoding: 8bit',
|
||||
];
|
||||
$ok = @mail(
|
||||
(string) $recipient['email'],
|
||||
mb_encode_mimeheader($subject, 'UTF-8', 'B'),
|
||||
$body,
|
||||
implode("\r\n", $headers)
|
||||
);
|
||||
if (!$ok) {
|
||||
$result['errors'][] = 'Versand an ' . $recipient['email'] . ' fehlgeschlagen.';
|
||||
continue;
|
||||
}
|
||||
|
||||
$log = db()->prepare(
|
||||
'INSERT IGNORE INTO reminder_mail_log (booking_id, kind, due_date, employee_id, sent_to, sent_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?)'
|
||||
);
|
||||
foreach ($items as $item) {
|
||||
$log->execute([
|
||||
(int) $item['booking']['id'], $item['kind'], $item['due_date'],
|
||||
$employeeId, (string) $recipient['email'], utc_now(),
|
||||
]);
|
||||
$result['sent']++;
|
||||
}
|
||||
$result['details'][] = $recipient['email'] . ' (' . count($items) . ')';
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue