feat(documents): Mietvertrag digital vom Kunden unterschreiben lassen
- „Unterschrift anfordern“ erzeugt einen persönlichen Link (/unterschrift/<64 Hex>) und mailt ihn an den Kunden; der Link bleibt gültig, bis unterschrieben oder ein neuer erzeugt wird. - Öffentliche Seite ohne Login: Buchungsdaten, Vertrags-PDF zur Ansicht (/unterschrift/<token>/pdf), Unterschriftfeld (Canvas) und Hinweis auf die AGB. Nach der Unterschrift wird der Token entwertet, die Signatur ins PDF gesetzt und der unterschriebene Vertrag automatisch an den Kunden gemailt. - Migration 013 (sign_token, sign_requested_at, Unique-Index), portabel und wiederholbar; schema.sql entsprechend ergänzt. - Booking-Karte zeigt „Unterschrift angefragt“ samt Link zum Kopieren/Öffnen; das Unterschreiben auf dem Tablet bleibt unverändert möglich.
This commit is contained in:
parent
7c0c2c3676
commit
ddc607bff3
12 changed files with 496 additions and 7 deletions
|
|
@ -439,7 +439,7 @@ function document_pdf_signatures(PdfDoc $pdf, float $top, array $booking, ?strin
|
|||
$pdf->text($lineRight, $top + 13, 7.5, 'Datum, Unterschrift Vermieter');
|
||||
|
||||
if ($signedAt !== null && $signatureFile !== null) {
|
||||
$pdf->text($lineLeft, $top - 6, 8.5, 'Unterschrieben am ' . fmt_datetime($signedAt) . ' (Tablet/eSignatur)');
|
||||
$pdf->text($lineLeft, $top - 6, 8.5, 'Unterschrieben am ' . fmt_datetime($signedAt) . ' (digitale Signatur)');
|
||||
}
|
||||
$pdf->text($lineLeft, $top + 30, 8.5, 'Gesamtbetrag bei Übergabe erhalten: ______________________ €');
|
||||
$pdf->text($lineLeft, $top + 46, 8.5, 'Kaution bei Rücknahme zurück erhalten: _____________________ €');
|
||||
|
|
@ -665,7 +665,7 @@ function document_save_signature(array $booking, array $doc, string $dataUrl, ?i
|
|||
throw new RuntimeException('Die Unterschrift konnte nicht gespeichert werden.');
|
||||
}
|
||||
|
||||
db()->prepare('UPDATE booking_documents SET signature_file = ?, signed_at = ? WHERE id = ?')
|
||||
db()->prepare('UPDATE booking_documents SET signature_file = ?, signed_at = ?, sign_token = NULL WHERE id = ?')
|
||||
->execute([$file, utc_now(), (int) $doc['id']]);
|
||||
$doc = document_find((int) $doc['id']) ?? $doc;
|
||||
if (!empty($userId)) {
|
||||
|
|
@ -677,6 +677,137 @@ function document_save_signature(array $booking, array $doc, string $dataUrl, ?i
|
|||
return document_regenerate($booking, $doc);
|
||||
}
|
||||
|
||||
/* ------------------------------------------- Digitale Unterschrift per Link */
|
||||
|
||||
/** Absolute URL für Links in E-Mails (config app.public_url, sonst Host). */
|
||||
function document_absolute_url(string $path): string
|
||||
{
|
||||
$base = rtrim((string) config('app.public_url', ''), '/');
|
||||
if ($base === '' && !empty($_SERVER['HTTP_HOST'])) {
|
||||
$base = (is_https() ? 'https://' : 'http://') . (string) $_SERVER['HTTP_HOST'];
|
||||
}
|
||||
return $base . url($path);
|
||||
}
|
||||
|
||||
/** Unterschriftslink des Kunden (leer, solange keiner angefordert wurde). */
|
||||
function document_sign_link(array $doc): string
|
||||
{
|
||||
$token = trim((string) ($doc['sign_token'] ?? ''));
|
||||
return $token === '' ? '' : document_absolute_url('/unterschrift/' . $token);
|
||||
}
|
||||
|
||||
function document_find_by_sign_token(string $token): ?array
|
||||
{
|
||||
$token = trim($token);
|
||||
if (!preg_match('/^[a-f0-9]{64}$/', $token) || !documents_schema_ready()) {
|
||||
return null;
|
||||
}
|
||||
$stmt = db()->prepare('SELECT * FROM booking_documents WHERE sign_token = ? LIMIT 1');
|
||||
$stmt->execute([$token]);
|
||||
$row = $stmt->fetch();
|
||||
return $row ?: null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Erzeugt (oder erneuert) den persönlichen Unterschriftslink des Kunden.
|
||||
* Der Link bleibt gültig, bis der Kunde unterschreibt oder ein neuer erzeugt wird.
|
||||
*/
|
||||
function document_request_signature(array $doc, bool $renew = false): array
|
||||
{
|
||||
if ($doc['kind'] !== DOCUMENT_KIND_CONTRACT) {
|
||||
throw new RuntimeException('Nur der Mietvertrag wird unterschrieben.');
|
||||
}
|
||||
if (!documents_schema_ready()) {
|
||||
throw new RuntimeException('Die Tabelle für Unterlagen fehlt noch. Bitte zuerst /migrate mit dem Cron-Token aufrufen.');
|
||||
}
|
||||
$token = trim((string) ($doc['sign_token'] ?? ''));
|
||||
if ($token === '' || $renew) {
|
||||
$token = bin2hex(random_bytes(32));
|
||||
db()->prepare('UPDATE booking_documents SET sign_token = ?, sign_requested_at = ? WHERE id = ?')
|
||||
->execute([$token, utc_now(), (int) $doc['id']]);
|
||||
}
|
||||
return document_find((int) $doc['id']) ?? $doc;
|
||||
}
|
||||
|
||||
/** Verschickt den Unterschriftslink an den Kunden. */
|
||||
function document_mail_signature_request(array $booking, array $doc): bool
|
||||
{
|
||||
if (!mail_is_enabled()) {
|
||||
throw new RuntimeException('Der E-Mail-Versand ist nicht aktiviert (app.mail_enabled).');
|
||||
}
|
||||
$to = trim((string) $booking['email']);
|
||||
if ($to === '' || !filter_var($to, FILTER_VALIDATE_EMAIL)) {
|
||||
throw new RuntimeException('Für diese Buchung ist keine gültige E-Mail-Adresse hinterlegt.');
|
||||
}
|
||||
$link = document_sign_link($doc);
|
||||
if ($link === '') {
|
||||
throw new RuntimeException('Es gibt noch keinen Unterschriftslink.');
|
||||
}
|
||||
$legal = legal_data();
|
||||
$owner = trim((string) ($legal['owner'] ?? '')) !== '' ? (string) $legal['owner'] : (string) $legal['business_name'];
|
||||
$subject = 'Mietvertrag zur Unterschrift · ' . (string) $booking['trailer_label'] . ' · '
|
||||
. fmt_date_short((string) $booking['start_date']);
|
||||
$body = 'Guten Tag ' . (string) $booking['customer_name'] . ",\n\n"
|
||||
. 'bitte unterschreiben Sie den Mietvertrag zu Ihrer Buchung digital. Der Link ist persönlich für Sie '
|
||||
. "bestimmt und bleibt gültig, bis Sie unterschrieben haben:\n\n"
|
||||
. $link . "\n\n"
|
||||
. 'Mietgegenstand: ' . (string) $booking['trailer_label'] . "\n"
|
||||
. 'Zeitraum: ' . fmt_date_short_with_time((string) $booking['start_date'], $booking['start_time'] ?? null)
|
||||
. ' bis ' . fmt_date_short_with_time((string) $booking['end_date'], $booking['end_time'] ?? null) . "\n\n"
|
||||
. 'Den Vertrag können Sie sich im Link vorab als PDF ansehen. Nach der Unterschrift erhalten Sie den '
|
||||
. "unterschriebenen Vertrag automatisch per E-Mail.\n\n"
|
||||
. 'Freundliche Grüße' . "\n" . $owner . "\n" . (string) $legal['business_name'] . "\n"
|
||||
. (string) $legal['phone'] . ' · ' . (string) $legal['email'];
|
||||
|
||||
if (!mail_send($to, $subject, $body)) {
|
||||
return false;
|
||||
}
|
||||
db()->prepare('UPDATE booking_documents SET sign_requested_at = COALESCE(sign_requested_at, ?) WHERE id = ?')
|
||||
->execute([utc_now(), (int) $doc['id']]);
|
||||
return true;
|
||||
}
|
||||
|
||||
/** Schickt dem Kunden den unterschriebenen Vertrag als PDF. */
|
||||
function document_mail_signed_copy(array $booking, array $doc): bool
|
||||
{
|
||||
if (!mail_is_enabled()) {
|
||||
return false;
|
||||
}
|
||||
$path = document_path($doc);
|
||||
if (!is_file($path)) {
|
||||
return false;
|
||||
}
|
||||
return mail_send_attachment(
|
||||
trim((string) $booking['email']),
|
||||
'Ihr unterschriebener Mietvertrag · ' . (string) $booking['trailer_label'] . ' · '
|
||||
. fmt_date_short((string) $booking['start_date']),
|
||||
'Guten Tag ' . (string) $booking['customer_name'] . ",\n\n"
|
||||
. "vielen Dank – Ihre Unterschrift ist eingegangen. Den unterschriebenen Mietvertrag finden Sie im "
|
||||
. "Anhang.\n\nMietgegenstand: " . (string) $booking['trailer_label'] . "\n"
|
||||
. 'Zeitraum: ' . fmt_date_short_with_time((string) $booking['start_date'], $booking['start_time'] ?? null)
|
||||
. ' bis ' . fmt_date_short_with_time((string) $booking['end_date'], $booking['end_time'] ?? null) . "\n\n"
|
||||
. "Bei Fragen einfach auf diese E-Mail antworten.\n\nFreundliche Grüße\n"
|
||||
. (string) legal_data()['business_name'],
|
||||
(string) $doc['file_name'],
|
||||
'application/pdf',
|
||||
(string) file_get_contents($path)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Unterschrift aus dem Kundenlink: speichert die Signatur, entwertet den Link
|
||||
* und schickt dem Kunden den unterschriebenen Vertrag.
|
||||
*/
|
||||
function document_sign_by_customer(array $booking, array $doc, string $dataUrl): array
|
||||
{
|
||||
$doc = document_save_signature($booking, $doc, $dataUrl, null);
|
||||
$mailed = false;
|
||||
if (mail_is_enabled() && filter_var((string) $booking['email'], FILTER_VALIDATE_EMAIL) !== false) {
|
||||
$mailed = document_mail_signed_copy($booking, $doc);
|
||||
}
|
||||
return ['doc' => $doc, 'mailed' => $mailed];
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ Rechnung */
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -75,6 +75,15 @@ function page_handle(string $method, string $path): void
|
|||
return;
|
||||
}
|
||||
|
||||
if (preg_match('#^/unterschrift/([a-f0-9]{64})/pdf$#', $path, $m) === 1) {
|
||||
page_signature_public_pdf($m[1]);
|
||||
return;
|
||||
}
|
||||
if (preg_match('#^/unterschrift/([a-f0-9]{64})$#', $path, $m) === 1) {
|
||||
page_signature_public($method, $m[1]);
|
||||
return;
|
||||
}
|
||||
|
||||
$user = auth_user();
|
||||
if ($user !== null && $user['must_change_password'] === 1
|
||||
&& !in_array($path, ['/account', '/logout', '/push/subscribe', '/push/unsubscribe', '/push/test'], true)
|
||||
|
|
@ -155,7 +164,7 @@ function page_handle(string $method, string $path): void
|
|||
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) {
|
||||
if (preg_match('#^/bookings/(\d+)/documents/(\d+)/(sign|request|mail|delete)$#', $path, $m) === 1) {
|
||||
page_booking_document_action($user, $method, (int) $m[1], (int) $m[2], (string) $m[3]);
|
||||
return;
|
||||
}
|
||||
|
|
@ -1136,6 +1145,26 @@ function page_booking_document_action(array $user, string $method, int $bookingI
|
|||
}
|
||||
|
||||
require_post();
|
||||
if ($action === 'request') {
|
||||
if (!auth_can_edit_booking($user)) {
|
||||
abort_page(403, 'Dafür fehlt dir die Berechtigung.');
|
||||
}
|
||||
try {
|
||||
$doc = document_request_signature($doc);
|
||||
$link = document_sign_link($doc);
|
||||
if (mail_is_enabled()) {
|
||||
document_mail_signature_request($booking, $doc)
|
||||
? flash('success', 'Unterschriftslink an ' . $booking['email'] . ' gesendet.')
|
||||
: flash('error', 'Die E-Mail konnte nicht verschickt werden – Link bitte kopieren.');
|
||||
} else {
|
||||
flash('info', 'E-Mail-Versand ist aus. Unterschriftslink zum Kopieren: ' . $link);
|
||||
}
|
||||
} catch (Throwable $error) {
|
||||
flash('error', $error->getMessage());
|
||||
}
|
||||
redirect('/bookings/' . $bookingId . '#unterlagen');
|
||||
}
|
||||
|
||||
if ($action === 'mail') {
|
||||
if (!auth_can_edit_booking($user)) {
|
||||
abort_page(403, 'Dafür fehlt dir die Berechtigung.');
|
||||
|
|
@ -1180,6 +1209,69 @@ function page_booking_document_invoice_upload(array $user, int $id): void
|
|||
redirect('/bookings/' . $id . '#unterlagen');
|
||||
}
|
||||
|
||||
/**
|
||||
* Öffentliche Unterschriftsseite des Kunden (/unterschrift/<token>).
|
||||
*
|
||||
* Kein Login: der Token im Link ist der Ausweis. Nach dem Unterschreiben wird
|
||||
* er entwertet und der unterschriebene Vertrag per E-Mail verschickt.
|
||||
*/
|
||||
function page_signature_public(string $method, string $token): void
|
||||
{
|
||||
$doc = document_find_by_sign_token($token);
|
||||
if (!$doc) {
|
||||
abort_page(404, 'Dieser Unterschriftslink ist nicht mehr gültig. Bitte den Betrieb um einen neuen Link bitten.');
|
||||
}
|
||||
$booking = booking_find((int) $doc['booking_id']);
|
||||
if (!$booking) {
|
||||
abort_page(404, 'Zu diesem Link gibt es keine Buchung mehr.');
|
||||
}
|
||||
|
||||
$error = '';
|
||||
if ($method === 'POST') {
|
||||
if (!csrf_valid()) {
|
||||
$error = 'Die Sitzung ist abgelaufen. Bitte die Seite neu laden und erneut unterschreiben.';
|
||||
} else {
|
||||
try {
|
||||
$result = document_sign_by_customer($booking, $doc, post_string('signature'));
|
||||
render_standalone('document_sign_done', [
|
||||
'booking' => $booking,
|
||||
'doc' => $result['doc'],
|
||||
'mailed' => $result['mailed'],
|
||||
], 'Unterschrift gespeichert');
|
||||
return;
|
||||
} catch (Throwable $e) {
|
||||
$error = $e->getMessage();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
render_standalone('document_sign_customer', [
|
||||
'booking' => $booking,
|
||||
'doc' => $doc,
|
||||
'token' => $token,
|
||||
'error' => $error,
|
||||
], 'Mietvertrag unterschreiben');
|
||||
}
|
||||
|
||||
/** Vertrag als PDF zur Ansicht (nur mit gültigem Unterschriftslink). */
|
||||
function page_signature_public_pdf(string $token): void
|
||||
{
|
||||
$doc = document_find_by_sign_token($token);
|
||||
if (!$doc) {
|
||||
abort_page(404, 'Dieser Unterschriftslink ist nicht mehr gültig.');
|
||||
}
|
||||
$path = document_path($doc);
|
||||
if (!is_file($path)) {
|
||||
abort_page(404, 'Die Datei ist nicht mehr vorhanden.');
|
||||
}
|
||||
header('Content-Type: ' . (string) $doc['mime_type']);
|
||||
header('Content-Length: ' . (string) filesize($path));
|
||||
header('Content-Disposition: inline; filename="' . (string) preg_replace('/[\r\n\"]+/', '', (string) $doc['file_name']) . '"');
|
||||
header('Cache-Control: private, no-store');
|
||||
readfile($path);
|
||||
exit;
|
||||
}
|
||||
|
||||
function api_availability(): void
|
||||
{
|
||||
auth_require_login();
|
||||
|
|
|
|||
|
|
@ -130,7 +130,7 @@ function url(string $path = '/'): string
|
|||
}
|
||||
|
||||
/** Cache-Buster: bei Änderungen an app.css/app.js erhöhen. */
|
||||
const DIC_ASSET_VERSION = '29';
|
||||
const DIC_ASSET_VERSION = '30';
|
||||
|
||||
function asset(string $path): string
|
||||
{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue