feat(documents): Mietvertrag, Auftragsbestätigung und Rechnung zur Buchung
- Mietvertrag als PDF in drei Varianten (Mit Lieferung, Selbstabholer, Truhe), Seite 2+ mit der AGB aus app/agb.php – derselbe Text wie /agb. - Auftragsbestätigung als PDF, ebenfalls in drei Varianten. - PDF-Erzeuger ohne Fremdbibliothek (app/pdf.php, Helvetica + JPEG für die Unterschrift); Layout in app/documents.php. - Unterschrift auf dem Tablet (views/document_sign.php, Canvas -> JPEG) wird ins PDF gesetzt; Unterlagen lassen sich ansehen, drucken und per Mail an den Kunden senden (mail_send_attachment()). - Rechnung aus Lexware als PDF-Upload; Ablage unter storage/documents/<booking_id>/ (per .htaccess gesperrt). - Aufbewahrung: documents_cleanup() im Cron löscht einen Monat nach Mietende (app.document_retention_days), inklusive verwaister Dateien. - Migrationen per HTTP (/migrate?token=…, app/migrate.php), weil das Hosting kein SSH hat; deploy.sh ruft die Route nach dem Sync auf. - Schema: booking_documents (Migration 012, auch in schema.sql), Doku in README/ROADMAP/docs/todo_kundenemail_2026-11-30-unterlagen.md. Enthält außerdem die bislang nicht committeten Anpassungen in bootstrap.php und app.js/pages.php, die im selben Arbeitsstand lagen.
This commit is contained in:
parent
3e34a6c839
commit
2de8e95c15
24 changed files with 2412 additions and 49 deletions
194
app/pages.php
194
app/pages.php
|
|
@ -58,10 +58,14 @@ function page_handle(string $method, string $path): void
|
|||
page_invite($method, $m[1]);
|
||||
return;
|
||||
}
|
||||
if ($path === '/impressum' || $path === '/datenschutz') {
|
||||
if (in_array($path, ['/impressum', '/datenschutz', '/agb'], true)) {
|
||||
legal_page(substr($path, 1));
|
||||
return;
|
||||
}
|
||||
if ($path === '/migrate') {
|
||||
cron_migrate();
|
||||
return;
|
||||
}
|
||||
if ($path === '/anfrage') {
|
||||
page_inquiry($method);
|
||||
return;
|
||||
|
|
@ -139,6 +143,22 @@ function page_handle(string $method, string $path): void
|
|||
page_booking_move($user, (int) $m[1]);
|
||||
return;
|
||||
}
|
||||
if (preg_match('#^/bookings/(\d+)/documents$#', $path, $m) === 1) {
|
||||
page_booking_documents_create($user, (int) $m[1]);
|
||||
return;
|
||||
}
|
||||
if (preg_match('#^/bookings/(\d+)/documents/rechnung$#', $path, $m) === 1) {
|
||||
page_booking_document_invoice_upload($user, (int) $m[1]);
|
||||
return;
|
||||
}
|
||||
if (preg_match('#^/bookings/(\d+)/documents/(\d+)$#', $path, $m) === 1) {
|
||||
page_booking_document_file($user, (int) $m[1], (int) $m[2]);
|
||||
return;
|
||||
}
|
||||
if (preg_match('#^/bookings/(\d+)/documents/(\d+)/(sign|mail|delete)$#', $path, $m) === 1) {
|
||||
page_booking_document_action($user, $method, (int) $m[1], (int) $m[2], (string) $m[3]);
|
||||
return;
|
||||
}
|
||||
if ($path === '/api/availability') {
|
||||
api_availability();
|
||||
return;
|
||||
|
|
@ -210,10 +230,11 @@ function cron_reminders(): void
|
|||
$mail = reminder_send_due();
|
||||
echo 'Drink it cold – Erinnerungslauf ' . fmt_datetime(utc_now()) . "\n";
|
||||
echo sprintf(
|
||||
"Aufräumen: %d Login-Fehlversuche, %d Tokens, %d Einladungen, %d E-Mail-Protokolle, %d Push-Protokolle entfernt%s.\n",
|
||||
"Aufräumen: %d Login-Fehlversuche, %d Tokens, %d Einladungen, %d E-Mail-Protokolle, %d Push-Protokolle, %d Buchungsunterlagen entfernt%s.\n",
|
||||
$cleanup['login_attempts'], $cleanup['auth_tokens'], $cleanup['user_invites'],
|
||||
$cleanup['reminder_mail_log'],
|
||||
$cleanup['reminder_push_log'], $cleanup['log_rotated'] ? ', Fehlerprotokoll rotiert' : ''
|
||||
$cleanup['reminder_push_log'], $cleanup['booking_documents'],
|
||||
$cleanup['log_rotated'] ? ', Fehlerprotokoll rotiert' : ''
|
||||
);
|
||||
if (!push_is_configured()) {
|
||||
echo "Push: nicht konfiguriert (VAPID-Schlüssel fehlen).\n";
|
||||
|
|
@ -238,6 +259,35 @@ function cron_reminders(): void
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Schema-Migrationen per HTTP anstoßen (Token = app.cron_token).
|
||||
*
|
||||
* Auf dem Webhosting gibt es kein SSH; nach einem Sync ruft das Deployment
|
||||
* diese Route auf, damit neue Migrationen auf der Live-DB landen.
|
||||
*/
|
||||
function cron_migrate(): void
|
||||
{
|
||||
header('Content-Type: text/plain; charset=utf-8');
|
||||
header('Cache-Control: no-store');
|
||||
$expected = trim((string) config('app.cron_token', ''));
|
||||
$token = query_string('token');
|
||||
if ($expected === '' || $token === '' || !hash_equals($expected, $token)) {
|
||||
http_response_code(403);
|
||||
echo "Kein Zugriff.\n";
|
||||
return;
|
||||
}
|
||||
try {
|
||||
$result = migrations_run();
|
||||
echo $result['applied'] === []
|
||||
? "Keine ausstehenden Migrationen.\n"
|
||||
: 'Migrationen angewendet: ' . implode(', ', $result['applied']) . "\n";
|
||||
} catch (Throwable $error) {
|
||||
error_log('Migration: ' . $error->getMessage());
|
||||
http_response_code(500);
|
||||
echo 'Fehler: ' . $error->getMessage() . "\n";
|
||||
}
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------- Anmeldung */
|
||||
|
||||
function page_login(string $method): void
|
||||
|
|
@ -888,6 +938,13 @@ function booking_detail_context(array $booking, array $user): array
|
|||
'canEdit' => auth_can_edit_booking($user),
|
||||
'canCancel' => auth_can_cancel_booking($user),
|
||||
'canEditBilling' => $isAdmin,
|
||||
'documents' => documents_for_booking((int) $booking['id']),
|
||||
'documentVariants' => document_variants(),
|
||||
'documentVariantDefault' => document_variant_default($booking),
|
||||
'canManageDocuments' => auth_can_edit_booking($user),
|
||||
'canDeleteDocuments' => $isAdmin,
|
||||
'documentsMailEnabled' => mail_is_enabled(),
|
||||
'documentsRetentionDays' => max(0, (int) config('app.document_retention_days', 30)),
|
||||
];
|
||||
}
|
||||
|
||||
|
|
@ -992,6 +1049,137 @@ function page_booking_move(array $user, int $id): void
|
|||
json_response($result);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------- Buchungsunterlagen */
|
||||
|
||||
function booking_or_404(int $id): array
|
||||
{
|
||||
$booking = booking_find($id);
|
||||
if (!$booking) {
|
||||
abort_page(404, 'Buchung nicht gefunden.');
|
||||
}
|
||||
return $booking;
|
||||
}
|
||||
|
||||
/** Erzeugt Mietvertrag oder Auftragsbestätigung in der gewählten Variante. */
|
||||
function page_booking_documents_create(array $user, int $id): void
|
||||
{
|
||||
require_post();
|
||||
$booking = booking_or_404($id);
|
||||
if (!auth_can_edit_booking($user)) {
|
||||
abort_page(403, 'Dafür fehlt dir die Berechtigung.');
|
||||
}
|
||||
$kind = post_string('kind');
|
||||
$variant = post_string('variant');
|
||||
if (!in_array($kind, [DOCUMENT_KIND_CONTRACT, DOCUMENT_KIND_CONFIRMATION], true)) {
|
||||
flash('error', 'Unbekannte Unterlagen-Art.');
|
||||
redirect('/bookings/' . $id . '#unterlagen');
|
||||
}
|
||||
try {
|
||||
document_create($booking, $kind, $variant, (int) $user['id']);
|
||||
flash('success', document_kind_label($kind) . ' erstellt – zum Drucken öffnen oder direkt versenden.');
|
||||
} catch (Throwable $error) {
|
||||
flash('error', $error->getMessage());
|
||||
}
|
||||
redirect('/bookings/' . $id . '#unterlagen');
|
||||
}
|
||||
|
||||
/** Liefert die PDF-Datei einer Unterlage aus (nur angemeldet, nie öffentlich). */
|
||||
function page_booking_document_file(array $user, int $bookingId, int $docId): void
|
||||
{
|
||||
$doc = document_find($docId);
|
||||
if (!$doc || (int) $doc['booking_id'] !== $bookingId) {
|
||||
abort_page(404, 'Unterlage nicht gefunden.');
|
||||
}
|
||||
$path = document_path($doc);
|
||||
if (!is_file($path)) {
|
||||
abort_page(404, 'Die Datei ist nicht mehr vorhanden.');
|
||||
}
|
||||
$download = query_string('download') === '1';
|
||||
$name = (string) preg_replace('/[\r\n"]+/', '', (string) $doc['file_name']);
|
||||
header('Content-Type: ' . (string) $doc['mime_type']);
|
||||
header('Content-Length: ' . (string) filesize($path));
|
||||
header('Content-Disposition: ' . ($download ? 'attachment' : 'inline') . '; filename="' . $name . '"');
|
||||
header('Cache-Control: private, no-store');
|
||||
readfile($path);
|
||||
exit;
|
||||
}
|
||||
|
||||
/** Unterschrift auf dem Tablet, Mailversand und Löschen einer Unterlage. */
|
||||
function page_booking_document_action(array $user, string $method, int $bookingId, int $docId, string $action): void
|
||||
{
|
||||
$booking = booking_or_404($bookingId);
|
||||
$doc = document_find($docId);
|
||||
if (!$doc || (int) $doc['booking_id'] !== $bookingId) {
|
||||
abort_page(404, 'Unterlage nicht gefunden.');
|
||||
}
|
||||
|
||||
if ($action === 'sign') {
|
||||
if (!auth_can_edit_booking($user)) {
|
||||
abort_page(403, 'Dafür fehlt dir die Berechtigung.');
|
||||
}
|
||||
if ($method === 'GET') {
|
||||
render_standalone('document_sign', [
|
||||
'booking' => $booking,
|
||||
'doc' => $doc,
|
||||
'pageTitle' => 'Unterschrift Mietvertrag',
|
||||
], 'Unterschrift · ' . $booking['customer_name']);
|
||||
return;
|
||||
}
|
||||
require_post();
|
||||
try {
|
||||
document_save_signature($booking, $doc, post_string('signature'), (int) $user['id']);
|
||||
flash('success', 'Unterschrift gespeichert und in den Mietvertrag übernommen.');
|
||||
} catch (Throwable $error) {
|
||||
flash('error', $error->getMessage());
|
||||
}
|
||||
redirect('/bookings/' . $bookingId . '#unterlagen');
|
||||
}
|
||||
|
||||
require_post();
|
||||
if ($action === 'mail') {
|
||||
if (!auth_can_edit_booking($user)) {
|
||||
abort_page(403, 'Dafür fehlt dir die Berechtigung.');
|
||||
}
|
||||
try {
|
||||
$sent = document_mail_to_customer($booking, $doc);
|
||||
$sent
|
||||
? flash('success', document_kind_label((string) $doc['kind']) . ' an ' . $booking['email'] . ' gesendet.')
|
||||
: flash('error', 'Die E-Mail konnte nicht verschickt werden.');
|
||||
} catch (Throwable $error) {
|
||||
flash('error', $error->getMessage());
|
||||
}
|
||||
redirect('/bookings/' . $bookingId . '#unterlagen');
|
||||
}
|
||||
|
||||
if ($action === 'delete') {
|
||||
if (!auth_can_manage($user)) {
|
||||
abort_page(403, 'Unterlagen darf nur der Admin löschen.');
|
||||
}
|
||||
document_delete($docId);
|
||||
flash('success', 'Unterlage gelöscht.');
|
||||
redirect('/bookings/' . $bookingId . '#unterlagen');
|
||||
}
|
||||
|
||||
abort_page(404, 'Diese Aktion gibt es nicht.');
|
||||
}
|
||||
|
||||
/** Rechnung (PDF aus Lexware) zur Buchung hochladen. */
|
||||
function page_booking_document_invoice_upload(array $user, int $id): void
|
||||
{
|
||||
require_post();
|
||||
$booking = booking_or_404($id);
|
||||
if (!auth_can_edit_booking($user)) {
|
||||
abort_page(403, 'Dafür fehlt dir die Berechtigung.');
|
||||
}
|
||||
try {
|
||||
document_upload_invoice($booking, (array) ($_FILES['invoice'] ?? []), (int) $user['id']);
|
||||
flash('success', 'Rechnung abgelegt.');
|
||||
} catch (Throwable $error) {
|
||||
flash('error', $error->getMessage());
|
||||
}
|
||||
redirect('/bookings/' . $id . '#unterlagen');
|
||||
}
|
||||
|
||||
function api_availability(): void
|
||||
{
|
||||
auth_require_login();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue