feat(booking): Kaution je Buchung, drei Vertragsunterschriften, Mails und Aufbewahrung nach Kundenwunsch
Kundenwunsch vom 04.12.2026 (docs/auftraege): - Kaution je Buchung (bookings.deposit_amount_cents, Standard 100 €, 0 = keine) statt globaler Konfiguration; Voreinstellungen im Formular angepasst. - Auftragsbestätigung rechnet Kaution nicht mehr in Gesamt/Restbetrag ein, zeigt sie als "in bar"-Hinweis unter dem Zahlungseingang, Verwendungszweck "#Buchungsnummer Name", Hinweis auf Anruf 8–10 Tage vor dem Termin. - Mietvertrag mit drei Unterschriften (Migration 021, document_signatures): Mitarbeiter bestätigt Zahlungseingang, Kunde Übernahme, Kunde Kautionsrückgabe; Zusatzvereinbarungen werden im Vertrag abgedruckt. - Zahlungserinnerung ebenfalls ohne Kaution im Betrag. - Mail-Fuß mit Signaturblock und Antwort-Sperrhinweis, Betreffzeilen mit Buchungsnummer und Mietgegenstand (ohne Anhängernummer), Wochentag in Datumsangaben. - Aufbewahrung: ohne Rechnung 30 Tage, mit Rechnung Mietvertrag 8 Jahre; Jahresarchiv der Mietverträge (app/export.php, Verwaltung → Sicherungen). - Automatische Bewertungsmail eine Woche nach Rückgabe (Migration 022). - Mehrfachvermietung: "Weiterer Anhänger für diesen Kunden" (/bookings/new?from=ID). Migrationen 020–022 sind lokal gelaufen; PDF-Erzeugung, Unterschrifts-Lebenszyklus und Jahresexport getestet.
This commit is contained in:
parent
606237dbda
commit
00b7b7f0e9
33 changed files with 1355 additions and 218 deletions
|
|
@ -23,6 +23,90 @@ const DOCUMENT_KIND_CONFIRMATION = 'confirmation';
|
|||
const DOCUMENT_KIND_INVOICE = 'invoice';
|
||||
const DOCUMENT_KIND_REMINDER = 'reminder';
|
||||
|
||||
/**
|
||||
* Rollen der Unterschriften im Mietvertrag (Tabelle document_signatures).
|
||||
* handover = Mitarbeiter bei der Übergabe (Zahlung erhalten),
|
||||
* customer = Kunde bei der Übernahme, return = Kunde bei der Rückgabe
|
||||
* (Kaution zurückerhalten).
|
||||
*/
|
||||
const DOCUMENT_SIGN_HANDOVER = 'handover';
|
||||
const DOCUMENT_SIGN_CUSTOMER = 'customer';
|
||||
const DOCUMENT_SIGN_RETURN = 'return';
|
||||
|
||||
/** @return array<string, string> */
|
||||
function document_sign_roles(): array
|
||||
{
|
||||
return [
|
||||
DOCUMENT_SIGN_HANDOVER => 'Übergabe: Zahlung erhalten (Mitarbeiter)',
|
||||
DOCUMENT_SIGN_CUSTOMER => 'Übernahme Mietgegenstand (Kunde)',
|
||||
DOCUMENT_SIGN_RETURN => 'Rückgabe: Kaution zurück (Kunde)',
|
||||
];
|
||||
}
|
||||
|
||||
function document_sign_role_label(string $role): string
|
||||
{
|
||||
return document_sign_roles()[$role] ?? $role;
|
||||
}
|
||||
|
||||
/** Kurzform für Status-Pills: Übergabe, Übernahme, Rückgabe. */
|
||||
function document_sign_role_short(string $role): string
|
||||
{
|
||||
return match ($role) {
|
||||
DOCUMENT_SIGN_HANDOVER => 'Übergabe',
|
||||
DOCUMENT_SIGN_CUSTOMER => 'Übernahme',
|
||||
default => 'Rückgabe',
|
||||
};
|
||||
}
|
||||
|
||||
function document_sign_role_valid(string $role): bool
|
||||
{
|
||||
return array_key_exists($role, document_sign_roles());
|
||||
}
|
||||
|
||||
/** Ist die Tabelle für die Unterschriften-Rollen vorhanden (Migration 021)? */
|
||||
function document_signatures_schema_ready(): bool
|
||||
{
|
||||
static $ready = null;
|
||||
if ($ready !== null) {
|
||||
return $ready;
|
||||
}
|
||||
try {
|
||||
$stmt = db()->query("SHOW TABLES LIKE 'document_signatures'");
|
||||
$ready = $stmt !== false && $stmt->fetchColumn() !== false;
|
||||
} catch (Throwable) {
|
||||
$ready = false;
|
||||
}
|
||||
return $ready;
|
||||
}
|
||||
|
||||
/**
|
||||
* Unterschriften einer Unterlage, nach Rolle. Fällt auf die Kundensignatur in
|
||||
* booking_documents zurück, solange Migration 021 noch nicht gelaufen ist.
|
||||
*
|
||||
* @return array<string, array<string, mixed>>
|
||||
*/
|
||||
function document_signatures(array $doc): array
|
||||
{
|
||||
$docId = (int) $doc['id'];
|
||||
$result = [];
|
||||
if (document_signatures_schema_ready()) {
|
||||
$stmt = db()->prepare('SELECT * FROM document_signatures WHERE document_id = ?');
|
||||
$stmt->execute([$docId]);
|
||||
foreach ($stmt->fetchAll() as $row) {
|
||||
$result[(string) $row['role']] = $row;
|
||||
}
|
||||
}
|
||||
if (!isset($result[DOCUMENT_SIGN_CUSTOMER]) && trim((string) ($doc['signature_file'] ?? '')) !== '') {
|
||||
$result[DOCUMENT_SIGN_CUSTOMER] = [
|
||||
'role' => DOCUMENT_SIGN_CUSTOMER,
|
||||
'signature_file' => (string) $doc['signature_file'],
|
||||
'signed_at' => (string) ($doc['signed_at'] ?? ''),
|
||||
'signed_via' => (string) ($doc['signed_via'] ?? 'tablet'),
|
||||
];
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/** @return array<string, string> */
|
||||
function document_kinds(): array
|
||||
{
|
||||
|
|
@ -233,7 +317,7 @@ function document_create(array $booking, string $kind, string $variant, ?int $us
|
|||
$dir = document_booking_dir((int) $booking['id'], true);
|
||||
$token = bin2hex(random_bytes(8));
|
||||
$stored = $token . '.pdf';
|
||||
$bytes = document_render($booking, $kind, $variant, null, null);
|
||||
$bytes = document_render($booking, $kind, $variant);
|
||||
if (@file_put_contents($dir . '/' . $stored, $bytes) === false) {
|
||||
throw new RuntimeException('Unterlage konnte nicht gespeichert werden.');
|
||||
}
|
||||
|
|
@ -270,14 +354,7 @@ function document_regenerate(array $booking, array $doc): array
|
|||
if ($kind === DOCUMENT_KIND_REMINDER) {
|
||||
$variant = ''; // Erinnerungen haben keine Mietvertrags-Variante.
|
||||
}
|
||||
$bytes = document_render(
|
||||
$booking,
|
||||
$kind,
|
||||
$variant,
|
||||
document_signature_path($doc),
|
||||
$doc['signed_at'] !== null ? (string) $doc['signed_at'] : null,
|
||||
trim((string) ($doc['signed_via'] ?? '')) !== '' ? (string) $doc['signed_via'] : null
|
||||
);
|
||||
$bytes = document_render($booking, $kind, $variant, $doc);
|
||||
$path = document_path($doc);
|
||||
document_booking_dir((int) $doc['booking_id'], true);
|
||||
if (@file_put_contents($path, $bytes) === false) {
|
||||
|
|
@ -293,14 +370,16 @@ function document_render(
|
|||
array $booking,
|
||||
string $kind,
|
||||
string $variant,
|
||||
?string $signatureFile,
|
||||
?string $signedAt,
|
||||
?string $signedVia = null
|
||||
?array $doc = null
|
||||
): string {
|
||||
return match ($kind) {
|
||||
DOCUMENT_KIND_CONFIRMATION => document_confirmation_pdf($booking, $variant),
|
||||
DOCUMENT_KIND_REMINDER => document_reminder_pdf($booking),
|
||||
default => document_contract_pdf($booking, $variant, $signatureFile, $signedAt, $signedVia),
|
||||
default => document_contract_pdf(
|
||||
$booking,
|
||||
$variant,
|
||||
$doc !== null ? document_signatures($doc) : []
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
|
|
@ -742,54 +821,76 @@ function document_pdf_note(PdfDoc $pdf, float $top, string $title, string $text)
|
|||
* $signedVia: tablet (vor Ort übergeben), link (Unterschriftslink des Kunden)
|
||||
* oder null, wenn noch nicht unterschrieben wurde.
|
||||
*/
|
||||
function document_pdf_signatures(
|
||||
PdfDoc $pdf,
|
||||
float $top,
|
||||
array $booking,
|
||||
?string $signatureFile,
|
||||
?string $signedAt,
|
||||
?string $signedVia = null
|
||||
): void {
|
||||
/** Pfad einer Signaturdatei im Buchungsordner (null, wenn nicht vorhanden). */
|
||||
function document_signature_file_path(int $bookingId, string $file): ?string
|
||||
{
|
||||
$file = trim($file);
|
||||
if ($file === '') {
|
||||
return null;
|
||||
}
|
||||
$path = document_store_root() . '/' . $bookingId . '/' . $file;
|
||||
return is_file($path) ? $path : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Unterschriftenblock des Mietvertrags: drei Zeilen in der Reihenfolge des
|
||||
* Papiervertrags – Mitarbeiter (Zahlung erhalten), Mieter (Übernahme), Mieter
|
||||
* (Kaution bei Rückgabe zurückerhalten).
|
||||
*
|
||||
* @param array<string, array<string, mixed>> $signatures Rolle => Datensatz
|
||||
* @return float Unterkante des Blocks
|
||||
*/
|
||||
function document_pdf_signatures(PdfDoc $pdf, float $top, array $booking, array $signatures): float
|
||||
{
|
||||
$legal = legal_data();
|
||||
$owner = trim((string) ($legal['owner'] ?? '')) !== '' ? (string) $legal['owner'] : (string) $legal['business_name'];
|
||||
$lineLeft = 50.0;
|
||||
$lineRight = 310.0;
|
||||
$lineWidth = 235.0;
|
||||
$blocks = [
|
||||
DOCUMENT_SIGN_HANDOVER => [
|
||||
'Übergabe: Der Mitarbeiter bestätigt, dass die Zahlung eingegangen ist.',
|
||||
'Datum, Unterschrift Mitarbeiter – ' . $owner,
|
||||
],
|
||||
DOCUMENT_SIGN_CUSTOMER => [
|
||||
'Übernahme: Der Mieter bestätigt, den Mietgegenstand übernommen zu haben.',
|
||||
'Datum, Unterschrift Mieter',
|
||||
],
|
||||
DOCUMENT_SIGN_RETURN => [
|
||||
'Rückgabe: Der Mieter bestätigt, die Kaution zurückerhalten zu haben.',
|
||||
'Datum, Unterschrift Mieter',
|
||||
],
|
||||
];
|
||||
|
||||
if ($signatureFile !== null) {
|
||||
$pdf->image($signatureFile, $lineLeft, $top - 52, 150, 50);
|
||||
foreach ($blocks as $role => [$heading, $caption]) {
|
||||
$row = $signatures[$role] ?? null;
|
||||
$file = $row !== null
|
||||
? document_signature_file_path((int) $booking['id'], (string) ($row['signature_file'] ?? ''))
|
||||
: null;
|
||||
$pdf->text(50, $top, 8.5, $heading);
|
||||
if ($file !== null) {
|
||||
$pdf->image($file, 50, $top + 6, 140, 33);
|
||||
}
|
||||
$lineY = $top + 46;
|
||||
$pdf->line(50, $lineY, 545, $lineY, 0.7);
|
||||
$pdf->text(50, $lineY + 11, 7.5, $caption);
|
||||
if ($row !== null && trim((string) ($row['signed_at'] ?? '')) !== '') {
|
||||
$via = (string) ($row['signed_via'] ?? '') === 'link' ? 'per Unterschriftslink' : 'auf dem Tablet';
|
||||
$pdf->text(320, $lineY + 11, 7.5, 'Unterschrieben am ' . fmt_datetime((string) $row['signed_at']) . ' (' . $via . ')');
|
||||
}
|
||||
$top = $lineY + 26;
|
||||
}
|
||||
$pdf->text($lineRight, $top - 10, 10, $owner . ' · ' . (string) $legal['business_name']);
|
||||
|
||||
$pdf->line($lineLeft, $top, $lineLeft + $lineWidth, $top, 0.7);
|
||||
$pdf->line($lineRight, $top, $lineRight + $lineWidth, $top, 0.7);
|
||||
$pdf->text($lineLeft, $top + 13, 7.5, 'Datum, Unterschrift Mieter');
|
||||
$pdf->text($lineRight, $top + 13, 7.5, 'Datum, Unterschrift Vermieter');
|
||||
|
||||
if ($signedAt !== null && $signatureFile !== null) {
|
||||
$label = match ($signedVia) {
|
||||
'tablet' => 'vor Ort auf dem Tablet',
|
||||
'link' => 'per Unterschriftslink',
|
||||
default => 'digitale Signatur',
|
||||
};
|
||||
$pdf->text($lineLeft, $top - 6, 8.5, 'Unterschrieben am ' . fmt_datetime($signedAt) . ' (' . $label . ')');
|
||||
}
|
||||
$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: _____________________ €');
|
||||
return $top;
|
||||
}
|
||||
|
||||
/** Mietvertrag als PDF (Variante Lieferung, Selbstabholer oder Truhe). */
|
||||
function document_contract_pdf(
|
||||
array $booking,
|
||||
string $variant,
|
||||
?string $signatureFile,
|
||||
?string $signedAt,
|
||||
?string $signedVia = null
|
||||
array $signatures = []
|
||||
): string {
|
||||
$legal = legal_data();
|
||||
$owner = trim((string) ($legal['owner'] ?? '')) !== '' ? (string) $legal['owner'] : (string) $legal['business_name'];
|
||||
$texts = document_texts($variant);
|
||||
$deposit = max(0, (int) config('app.document_deposit_cents', 10000));
|
||||
$deposit = booking_deposit_amount($booking);
|
||||
$rent = (int) ($booking['rent_cents'] ?? $booking['total_cents']);
|
||||
$delivery = (int) ($booking['delivery_cents'] ?? 0);
|
||||
$paid = (int) ($booking['deposit_cents'] ?? 0);
|
||||
|
|
@ -837,7 +938,7 @@ function document_contract_pdf(
|
|||
$priceRows[] = ['Lieferung / Abholung', fmt_money($delivery)];
|
||||
}
|
||||
if ($deposit > 0) {
|
||||
$priceRows[] = ['Kaution', fmt_money($deposit) . ' (Rückgabe: volle Erstattung)'];
|
||||
$priceRows[] = ['Kaution', fmt_money($deposit) . ' (in bar bei Übergabe, Rückgabe: volle Erstattung)'];
|
||||
}
|
||||
if ($paid > 0) {
|
||||
$priceRows[] = ['Anzahlung', '− ' . fmt_money($paid)];
|
||||
|
|
@ -854,13 +955,34 @@ function document_contract_pdf(
|
|||
. 'Bei Rückgabe ist der Mietgegenstand gereinigt und vollständig zurückzugeben.';
|
||||
$y = $pdf->paragraph(50, $y + 4, $pdf->pageWidth() - 100, 9, $explain, false, 12.5);
|
||||
|
||||
$signatureTop = $y + 58;
|
||||
if ($signatureTop > 720) {
|
||||
// Zusatzvereinbarungen aus der Buchung gehören in den Vertrag (der Kunde
|
||||
// unterschreibt sie damit mit).
|
||||
$agreements = trim((string) ($booking['agreements'] ?? ''));
|
||||
if ($agreements !== '') {
|
||||
$y = document_pdf_heading($pdf, $y + 6, 'Zusatzvereinbarungen');
|
||||
foreach (preg_split('/\R/', $agreements) ?: [] as $line) {
|
||||
if (trim($line) === '') {
|
||||
continue;
|
||||
}
|
||||
foreach ($pdf->wrap(trim($line), 9, $pdf->pageWidth() - 100) as $wrappedLine) {
|
||||
if ($y > 700) {
|
||||
document_pdf_footer($pdf, 'Mietvertrag · Buchung #' . (int) $booking['id'] . ' · ' . (string) $booking['customer_name']);
|
||||
$pdf->newPage();
|
||||
$y = 90.0;
|
||||
}
|
||||
$pdf->text(50, $y, 9, $wrappedLine);
|
||||
$y += 12.5;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$signatureTop = $y + 24;
|
||||
if ($signatureTop + 220 > 762) {
|
||||
document_pdf_footer($pdf, 'Mietvertrag · Buchung #' . (int) $booking['id'] . ' · ' . (string) $booking['customer_name']);
|
||||
$pdf->newPage();
|
||||
$signatureTop = 150;
|
||||
$signatureTop = 90.0;
|
||||
}
|
||||
document_pdf_signatures($pdf, $signatureTop, $booking, $signatureFile, $signedAt, $signedVia);
|
||||
document_pdf_signatures($pdf, $signatureTop, $booking, $signatures);
|
||||
document_pdf_footer($pdf, 'Mietvertrag · Buchung #' . (int) $booking['id'] . ' · ' . (string) $booking['customer_name']);
|
||||
|
||||
// Seite 2: AGB (gleiche Fassung wie unter /agb).
|
||||
|
|
@ -907,12 +1029,15 @@ function document_confirmation_pdf(array $booking, string $variant): string
|
|||
{
|
||||
$legal = legal_data();
|
||||
$texts = document_texts($variant);
|
||||
$deposit = max(0, (int) config('app.document_deposit_cents', 10000));
|
||||
$deposit = booking_deposit_amount($booking);
|
||||
$rent = (int) ($booking['rent_cents'] ?? $booking['total_cents']);
|
||||
$delivery = (int) ($booking['delivery_cents'] ?? 0);
|
||||
$paid = (int) ($booking['deposit_cents'] ?? 0);
|
||||
$total = (int) $booking['total_cents'];
|
||||
$grand = $total + $deposit;
|
||||
// Die Kaution ist keine zahlbare Summe: sie wird bei der Übergabe in bar
|
||||
// hinterlegt und bei der Rückgabe vollständig erstattet. Deshalb steht in
|
||||
// der Auftragsbestätigung überall die Summe ohne Kaution.
|
||||
$grand = $total;
|
||||
|
||||
$pdf = new PdfDoc();
|
||||
document_pdf_title($pdf, 'Auftragsbestätigung', 'Buchung #' . (int) $booking['id'] . ' · ' . fmt_date(today_iso()));
|
||||
|
|
@ -922,8 +1047,8 @@ function document_confirmation_pdf(array $booking, string $variant): string
|
|||
$rows = [
|
||||
['Mietgegenstand', document_subject_plain($booking)],
|
||||
['Art', document_variant_label($variant)],
|
||||
['Mietzeitraum', 'von ' . fmt_date_with_time((string) $booking['start_date'], $booking['start_time'] ?? null)
|
||||
. ' bis ' . fmt_date_with_time((string) $booking['end_date'], $booking['end_time'] ?? null)],
|
||||
['Mietzeitraum', 'von ' . fmt_date_weekday_with_time((string) $booking['start_date'], $booking['start_time'] ?? null)
|
||||
. ' bis ' . fmt_date_weekday_with_time((string) $booking['end_date'], $booking['end_time'] ?? null)],
|
||||
['Übergabe durch', (string) $booking['employee_name']],
|
||||
['Rücknahme durch', booking_return_employee_name($booking)],
|
||||
];
|
||||
|
|
@ -937,9 +1062,6 @@ function document_confirmation_pdf(array $booking, string $variant): string
|
|||
if ($delivery > 0) {
|
||||
$priceRows[] = ['Lieferung / Abholung', fmt_money($delivery)];
|
||||
}
|
||||
if ($deposit > 0) {
|
||||
$priceRows[] = ['Kaution', fmt_money($deposit) . ' (Rückgabe: volle Erstattung)'];
|
||||
}
|
||||
// Anzahlung: vereinbart (Häkchen in der Buchung) und Betrag > 0.
|
||||
$depositRequired = (int) ($booking['deposit_required'] ?? 0) === 1;
|
||||
$transfer = booking_balance_transfer($booking);
|
||||
|
|
@ -957,9 +1079,14 @@ function document_confirmation_pdf(array $booking, string $variant): string
|
|||
$priceRows[] = ['Gesamtbetrag', fmt_money($grand), true];
|
||||
$priceRows[] = [$transfer ? 'Restbetrag per Überweisung' : 'Bei Übergabe in bar zu zahlen', fmt_money($open), true];
|
||||
if ($transfer) {
|
||||
$priceRows[] = ['Zahlungseingang bis', fmt_date($balanceDue) . ' (' . document_reminder_days()
|
||||
$priceRows[] = ['Zahlungseingang bis', fmt_date_weekday($balanceDue) . ' (' . document_reminder_days()
|
||||
. ' Tage vor Mietbeginn)'];
|
||||
}
|
||||
if ($deposit > 0) {
|
||||
// Steht bewusst unter dem Zahlungseingang: die Kaution ist keine
|
||||
// zahlbare Summe, sie wird bei der Übergabe in bar hinterlegt.
|
||||
$priceRows[] = ['Kaution (in bar bei Übergabe)', fmt_money($deposit) . ' · wird zurückgegeben'];
|
||||
}
|
||||
$y = document_pdf_rows($pdf, $y, $priceRows);
|
||||
|
||||
if ($account !== null) {
|
||||
|
|
@ -983,8 +1110,7 @@ function document_confirmation_pdf(array $booking, string $variant): string
|
|||
['Kontoinhaber', $account['holder']],
|
||||
['Bank', $account['bank']],
|
||||
['IBAN', $account['iban']],
|
||||
['Verwendungszweck', document_subject_plain($booking) . ' ' . fmt_date((string) $booking['start_date'])
|
||||
. ' · ' . (string) $booking['customer_name']],
|
||||
['Verwendungszweck', '#' . (int) $booking['id'] . ' ' . (string) $booking['customer_name']],
|
||||
], 168.0);
|
||||
if ($paid > 0) {
|
||||
$warning = 'Bitte beachten: Erst nach Eingang der Anzahlung auf dem genannten Konto ist der Termin '
|
||||
|
|
@ -995,25 +1121,26 @@ function document_confirmation_pdf(array $booking, string $variant): string
|
|||
}
|
||||
|
||||
// Zahlungshinweis und Schlusstext in einem Block – hält die Seite einseitig.
|
||||
$depositText = $deposit > 0
|
||||
? 'Die Kaution von ' . fmt_money($deposit) . ' ist bei der Übergabe in bar zu zahlen und wird bei '
|
||||
. 'ordnungsgemäßer Rückgabe vollständig erstattet. '
|
||||
: '';
|
||||
if ($transfer && $account !== null) {
|
||||
$payment = 'Der Restbetrag von ' . fmt_money($open) . ' ist bis spätestens ' . fmt_date($balanceDue)
|
||||
. ' auf das genannte Konto zu überweisen; ' . transfer_reminder_days() . ' Tage vor dem Termin erhalten '
|
||||
. 'Sie dazu noch eine Zahlungserinnerung. '
|
||||
. 'Die Kaution von ' . fmt_money($deposit) . ' wird bei ordnungsgemäßer Rückgabe vollständig erstattet. ';
|
||||
. 'Sie dazu noch eine Zahlungserinnerung. ' . $depositText;
|
||||
} elseif ($account !== null) {
|
||||
$payment = 'Der Restbetrag von ' . fmt_money($open) . ' ist bei der Übergabe in bar fällig. '
|
||||
. 'Die Kaution von ' . fmt_money($deposit) . ' wird bei ordnungsgemäßer Rückgabe vollständig erstattet. ';
|
||||
$payment = 'Der Restbetrag von ' . fmt_money($open) . ' ist bei der Übergabe in bar fällig. ' . $depositText;
|
||||
} else {
|
||||
$payment = $transfer
|
||||
? 'Der Restbetrag von ' . fmt_money($open) . ' ist bis spätestens ' . fmt_date($balanceDue)
|
||||
. ' zu überweisen; die Bankverbindung erhalten Sie mit der Zahlungserinnerung. '
|
||||
. 'Die Kaution von ' . fmt_money($deposit) . ' wird bei ordnungsgemäßer Rückgabe vollständig erstattet. '
|
||||
. ' zu überweisen; die Bankverbindung erhalten Sie mit der Zahlungserinnerung. ' . $depositText
|
||||
: 'Der Gesamtbetrag ist bei der Übergabe in bar zu zahlen; nach Absprache auch per Überweisung '
|
||||
. '(Geld bis spätestens Montag der Terminwoche). Die Kaution von ' . fmt_money($deposit)
|
||||
. ' wird bei ordnungsgemäßer Rückgabe vollständig erstattet. ';
|
||||
. '(Geld bis spätestens Montag der Terminwoche). ' . $depositText;
|
||||
}
|
||||
$closing = 'Vielen Dank für die Buchung. Diese Auftragsbestätigung bestätigt den vereinbarten Termin und '
|
||||
. 'die genannten Beträge. ' . $payment
|
||||
. 'Ein Mitarbeiter von uns meldet sich 8–10 Tage vor dem Termin, um die genaue Übergabezeit abzusprechen. '
|
||||
. 'Es gelten die Allgemeinen Geschäftsbedingungen des Vermieters. '
|
||||
. 'Bitte prüfen Sie die Angaben und melden Sie sich bei Abweichungen.';
|
||||
$y = $pdf->paragraph(50, $y + 8, $pdf->pageWidth() - 100, 9, $closing, false, 12.5);
|
||||
|
|
@ -1151,8 +1278,10 @@ function document_reminder_account(array $booking): array
|
|||
*/
|
||||
function document_reminder_amounts(array $booking): array
|
||||
{
|
||||
$deposit = max(0, (int) config('app.document_deposit_cents', 10000));
|
||||
$grand = (int) $booking['total_cents'] + $deposit;
|
||||
// Die Kaution wird bei der Übergabe in bar hinterlegt und ist damit keine
|
||||
// Forderung – sie steht nur als Hinweis im Dokument.
|
||||
$deposit = booking_deposit_amount($booking);
|
||||
$grand = (int) $booking['total_cents'];
|
||||
$paid = max(0, (int) ($booking['deposit_cents'] ?? 0));
|
||||
$hasDeposit = (int) ($booking['deposit_required'] ?? 0) === 1 && $paid > 0;
|
||||
$open = $hasDeposit ? max(0, $grand - $paid) : $grand;
|
||||
|
|
@ -1173,7 +1302,7 @@ function document_reminder_pdf(array $booking): string
|
|||
$due = document_reminder_due_date($booking);
|
||||
$days = document_reminder_days();
|
||||
$subject = document_subject_plain($booking);
|
||||
$term = fmt_date((string) $booking['start_date']) . ' bis ' . fmt_date((string) $booking['end_date']);
|
||||
$term = fmt_date_weekday((string) $booking['start_date']) . ' bis ' . fmt_date_weekday((string) $booking['end_date']);
|
||||
|
||||
$pdf = new PdfDoc();
|
||||
document_pdf_title($pdf, 'Zahlungserinnerung', 'Buchung #' . (int) $booking['id'] . ' · ' . fmt_date(today_iso()));
|
||||
|
|
@ -1203,15 +1332,15 @@ function document_reminder_pdf(array $booking): string
|
|||
['Kontoinhaber', (string) $account['holder']],
|
||||
['Bank', (string) $account['bank']],
|
||||
['IBAN', (string) $account['iban']],
|
||||
['Verwendungszweck', $subject . ' ' . fmt_date((string) $booking['start_date'])
|
||||
. ' · ' . (string) $booking['customer_name']],
|
||||
['Verwendungszweck', '#' . (int) $booking['id'] . ' ' . (string) $booking['customer_name']],
|
||||
], 168.0);
|
||||
|
||||
$closing = 'Bitte geben Sie bei der Überweisung den Verwendungszweck an, damit wir die Zahlung zuordnen können. '
|
||||
. 'Ist die Zahlung bereits veranlasst, betrachten Sie diese Erinnerung als gegenstandslos.';
|
||||
if ($amounts['deposit'] > 0) {
|
||||
$closing .= ' Die Kaution von ' . fmt_money($amounts['deposit']) . ' wird bei ordnungsgemäßer '
|
||||
. 'Rückgabe vollständig erstattet.';
|
||||
$closing .= ' Die Kaution von ' . fmt_money($amounts['deposit']) . ' ist bei der Übergabe in bar zu '
|
||||
. 'hinterlegen und wird bei ordnungsgemäßer Rückgabe vollständig erstattet (kein Teil des oben '
|
||||
. 'genannten Betrags).';
|
||||
}
|
||||
$closing .= ' Es gelten die Allgemeinen Geschäftsbedingungen des Vermieters.';
|
||||
$y = $pdf->paragraph(50, $y + 6, $pdf->pageWidth() - 100, 9, $closing, false, 12.5);
|
||||
|
|
@ -1235,11 +1364,15 @@ function document_save_signature(
|
|||
array $doc,
|
||||
string $dataUrl,
|
||||
?int $userId,
|
||||
string $via = 'tablet'
|
||||
string $via = 'tablet',
|
||||
string $role = DOCUMENT_SIGN_CUSTOMER
|
||||
): array {
|
||||
if ($doc['kind'] !== DOCUMENT_KIND_CONTRACT) {
|
||||
throw new RuntimeException('Nur der Mietvertrag wird unterschrieben.');
|
||||
}
|
||||
if (!document_sign_role_valid($role)) {
|
||||
$role = DOCUMENT_SIGN_CUSTOMER;
|
||||
}
|
||||
if (!preg_match('#^data:image/jpeg;base64,([A-Za-z0-9+/=\s]+)$#', trim($dataUrl), $m)) {
|
||||
throw new RuntimeException('Die Unterschrift konnte nicht gelesen werden.');
|
||||
}
|
||||
|
|
@ -1252,15 +1385,31 @@ function document_save_signature(
|
|||
}
|
||||
|
||||
$dir = document_booking_dir((int) $doc['booking_id'], true);
|
||||
$file = 'unterschrift-' . (int) $doc['id'] . '.jpg';
|
||||
// Die Kundenunterschrift behält den bisherigen Dateinamen (Kompatibilität),
|
||||
// die beiden anderen Rollen bekommen ein eigenes Suffix.
|
||||
$file = $role === DOCUMENT_SIGN_CUSTOMER
|
||||
? 'unterschrift-' . (int) $doc['id'] . '.jpg'
|
||||
: 'unterschrift-' . (int) $doc['id'] . '-' . $role . '.jpg';
|
||||
if (@file_put_contents($dir . '/' . $file, $binary) === false) {
|
||||
throw new RuntimeException('Die Unterschrift konnte nicht gespeichert werden.');
|
||||
}
|
||||
|
||||
$via = in_array($via, ['tablet', 'link'], true) ? $via : 'tablet';
|
||||
db()->prepare(
|
||||
'UPDATE booking_documents SET signature_file = ?, signed_at = ?, signed_via = ?, sign_token = NULL WHERE id = ?'
|
||||
)->execute([$file, utc_now(), $via, (int) $doc['id']]);
|
||||
if (document_signatures_schema_ready()) {
|
||||
db()->prepare(
|
||||
'INSERT INTO document_signatures (document_id, role, signature_file, signed_at, signed_via, signed_by)
|
||||
VALUES (?, ?, ?, ?, ?, ?)
|
||||
ON DUPLICATE KEY UPDATE signature_file = VALUES(signature_file), signed_at = VALUES(signed_at),
|
||||
signed_via = VALUES(signed_via), signed_by = VALUES(signed_by)'
|
||||
)->execute([(int) $doc['id'], $role, $file, utc_now(), $via, $userId]);
|
||||
}
|
||||
if ($role === DOCUMENT_SIGN_CUSTOMER) {
|
||||
// Kundenunterschrift zusätzlich in booking_documents (Anzeige, Mailversand,
|
||||
// Löschprotokoll); der Unterschriftslink ist damit verbraucht.
|
||||
db()->prepare(
|
||||
'UPDATE booking_documents SET signature_file = ?, signed_at = ?, signed_via = ?, sign_token = NULL WHERE id = ?'
|
||||
)->execute([$file, utc_now(), $via, (int) $doc['id']]);
|
||||
}
|
||||
$doc = document_find((int) $doc['id']) ?? $doc;
|
||||
if (!empty($userId)) {
|
||||
db()->prepare('UPDATE booking_documents SET created_by = COALESCE(created_by, ?) WHERE id = ?')
|
||||
|
|
@ -1337,21 +1486,19 @@ function document_mail_signature_request(array $booking, array $doc): bool
|
|||
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']);
|
||||
$subject = 'Mietvertrag zur Unterschrift bei Drink it cold #' . (int) $booking['id'] . ' '
|
||||
. booking_item_label($booking);
|
||||
$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"
|
||||
. 'Mietgegenstand: ' . booking_item_label($booking) . "\n"
|
||||
. 'Zeitraum: ' . fmt_date_weekday_with_time((string) $booking['start_date'], $booking['start_time'] ?? null)
|
||||
. ' bis ' . fmt_date_weekday_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'];
|
||||
. mail_signature_block() . "\n\n"
|
||||
. mail_auto_hint();
|
||||
|
||||
if (!mail_send($to, $subject, $body)) {
|
||||
return false;
|
||||
|
|
@ -1373,15 +1520,15 @@ function document_mail_signed_copy(array $booking, array $doc): bool
|
|||
}
|
||||
return mail_send_attachment(
|
||||
trim((string) $booking['email']),
|
||||
'Ihr unterschriebener Mietvertrag · ' . (string) $booking['trailer_label'] . ' · '
|
||||
. fmt_date_short((string) $booking['start_date']),
|
||||
'Ihr unterschriebener Mietvertrag zur Buchung bei Drink it cold #' . (int) $booking['id'] . ' '
|
||||
. booking_item_label($booking),
|
||||
'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'],
|
||||
. "Anhang.\n\nMietgegenstand: " . booking_item_label($booking) . "\n"
|
||||
. 'Zeitraum: ' . fmt_date_weekday_with_time((string) $booking['start_date'], $booking['start_time'] ?? null)
|
||||
. ' bis ' . fmt_date_weekday_with_time((string) $booking['end_date'], $booking['end_time'] ?? null) . "\n\n"
|
||||
. mail_signature_block() . "\n\n"
|
||||
. mail_auto_hint(),
|
||||
(string) $doc['file_name'],
|
||||
'application/pdf',
|
||||
(string) file_get_contents($path)
|
||||
|
|
@ -1491,11 +1638,9 @@ function document_mail_to_customer(array $booking, array $doc): bool
|
|||
if (!is_file($path)) {
|
||||
throw new RuntimeException('Die Datei fehlt.');
|
||||
}
|
||||
$legal = legal_data();
|
||||
$owner = trim((string) ($legal['owner'] ?? '')) !== '' ? (string) $legal['owner'] : (string) $legal['business_name'];
|
||||
$label = document_kind_label((string) $doc['kind']);
|
||||
$subject = $label . ' zur Buchung #' . (int) $booking['id'] . ' · '
|
||||
. (string) $booking['trailer_label'] . ' · ' . fmt_date_short((string) $booking['start_date']);
|
||||
$subject = $label . ' zur Buchung bei Drink it cold #' . (int) $booking['id'] . ' '
|
||||
. booking_item_label($booking);
|
||||
|
||||
$body = 'Guten Tag ' . (string) $booking['customer_name'] . ",\n\n"
|
||||
. 'anbei erhalten Sie ' . ($doc['kind'] === DOCUMENT_KIND_CONTRACT ? 'den' : 'die') . ' ' . $label
|
||||
|
|
@ -1504,16 +1649,19 @@ function document_mail_to_customer(array $booking, array $doc): bool
|
|||
$amounts = document_reminder_amounts($booking);
|
||||
$account = document_reminder_account($booking);
|
||||
$body .= $amounts['label'] . ': ' . fmt_money($amounts['open']) . "\n"
|
||||
. 'Zahlungseingang bis: ' . fmt_date(document_reminder_due_date($booking))
|
||||
. 'Zahlungseingang bis: ' . fmt_date_weekday(document_reminder_due_date($booking))
|
||||
. ' (' . document_reminder_days() . ' Tage vor Mietbeginn)' . "\n"
|
||||
. 'IBAN: ' . (string) $account['iban'] . ' (' . (string) $account['holder'] . ")\n\n";
|
||||
. 'IBAN: ' . (string) $account['iban'] . ' (' . (string) $account['holder'] . ")\n";
|
||||
if ($amounts['deposit'] > 0) {
|
||||
$body .= 'Kaution: ' . fmt_money($amounts['deposit']) . " (bitte bei der Übergabe in bar mitbringen)\n";
|
||||
}
|
||||
$body .= "\n";
|
||||
}
|
||||
$body .= '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"
|
||||
. 'Bei Fragen einfach auf diese E-Mail antworten.' . "\n\n"
|
||||
. 'Freundliche Grüße' . "\n" . $owner . "\n" . (string) $legal['business_name'] . "\n"
|
||||
. (string) $legal['phone'] . ' · ' . (string) $legal['email'];
|
||||
$body .= 'Mietgegenstand: ' . booking_item_label($booking) . "\n"
|
||||
. 'Zeitraum: ' . fmt_date_weekday_with_time((string) $booking['start_date'], $booking['start_time'] ?? null)
|
||||
. ' bis ' . fmt_date_weekday_with_time((string) $booking['end_date'], $booking['end_time'] ?? null) . "\n\n"
|
||||
. mail_signature_block() . "\n\n"
|
||||
. mail_auto_hint();
|
||||
|
||||
$ok = mail_send_attachment($to, $subject, $body, (string) $doc['file_name'], 'application/pdf', (string) file_get_contents($path));
|
||||
if ($ok) {
|
||||
|
|
@ -1530,9 +1678,19 @@ function document_delete(int $id): void
|
|||
if (!$doc) {
|
||||
return;
|
||||
}
|
||||
$signatureFiles = [];
|
||||
if (document_signatures_schema_ready()) {
|
||||
$stmt = db()->prepare('SELECT signature_file FROM document_signatures WHERE document_id = ?');
|
||||
$stmt->execute([$id]);
|
||||
$signatureFiles = array_map('strval', $stmt->fetchAll(PDO::FETCH_COLUMN));
|
||||
}
|
||||
db()->prepare('DELETE FROM booking_documents WHERE id = ?')->execute([$id]);
|
||||
foreach ([document_path($doc), document_signature_path($doc)] as $path) {
|
||||
if ($path !== null && is_file($path)) {
|
||||
$paths = [document_path($doc), document_signature_path($doc)];
|
||||
foreach ($signatureFiles as $signatureFile) {
|
||||
$paths[] = document_signature_file_path((int) $doc['booking_id'], $signatureFile);
|
||||
}
|
||||
foreach (array_unique(array_filter($paths)) as $path) {
|
||||
if (is_file($path)) {
|
||||
@unlink($path);
|
||||
}
|
||||
}
|
||||
|
|
@ -1541,38 +1699,39 @@ function document_delete(int $id): void
|
|||
/**
|
||||
* Aufbewahrungsfrist einer Unterlagen-Art in Tagen ab Mietende.
|
||||
*
|
||||
* Best effort, bis die Fristen rechtlich abgesichert sind:
|
||||
* - Mietvertrag: Verjährung von Ansprüchen (3 Jahre, beginnend am Jahresende)
|
||||
* plus Puffer, also rund 4 Jahre nach Mietende.
|
||||
* - Rechnung: 8 Jahre für Buchungsbelege; der GoBD-Schutz (Unveränderbarkeit,
|
||||
* Protokollierung) ist noch nicht umgesetzt, bitte steuerlich prüfen.
|
||||
* - Auftragsbestätigung/Zahlungserinnerung: nur für die Abwicklung nötig.
|
||||
* Regel des Kunden (04.12.2026):
|
||||
* - Rechnung NEIN → alle Unterlagen 30 Tage,
|
||||
* - Rechnung JA → alle Unterlagen 30 Tage, nur der Mietvertrag bleibt
|
||||
* 8 Jahre (2922 Tage) als Buchungsbeleg aufbewahrt.
|
||||
*
|
||||
* Die Fristen stehen in app.document_retention_days (with_invoice /
|
||||
* without_invoice); die alte Form mit einer Frist je Art wird weiter gelesen.
|
||||
* Rückgabe <= 0 bedeutet: nicht automatisch löschen.
|
||||
*/
|
||||
function document_retention_days(string $kind): int
|
||||
function document_retention_days(string $kind, ?array $booking = null): int
|
||||
{
|
||||
$invoice = $booking !== null && (int) ($booking['invoice'] ?? 0) === 1;
|
||||
$defaults = [
|
||||
DOCUMENT_KIND_CONTRACT => 1461,
|
||||
DOCUMENT_KIND_CONFIRMATION => 90,
|
||||
DOCUMENT_KIND_REMINDER => 365,
|
||||
DOCUMENT_KIND_INVOICE => 2922,
|
||||
'default' => 30,
|
||||
'with_invoice' => [DOCUMENT_KIND_CONTRACT => 2922, 'default' => 30],
|
||||
'without_invoice' => ['default' => 30],
|
||||
];
|
||||
$config = config('app.document_retention_days', $defaults);
|
||||
if (is_array($config)) {
|
||||
$days = $config[$kind] ?? $config['default'] ?? $defaults['default'];
|
||||
} else {
|
||||
if (!is_array($config)) {
|
||||
// Altform: eine Zahl gilt für alle Arten.
|
||||
$days = $config;
|
||||
return (int) $config;
|
||||
}
|
||||
return (int) $days;
|
||||
if (isset($config['with_invoice']) || isset($config['without_invoice'])) {
|
||||
$set = $invoice ? (array) ($config['with_invoice'] ?? []) : (array) ($config['without_invoice'] ?? []);
|
||||
return (int) ($set[$kind] ?? $set['default'] ?? 30);
|
||||
}
|
||||
// Altform: art-spezifische Tage.
|
||||
return (int) ($config[$kind] ?? $config['default'] ?? 30);
|
||||
}
|
||||
|
||||
/** Frist als kurzer Text für die Oberfläche, z. B. "4 Jahre" oder "90 Tage". */
|
||||
function document_retention_text(string $kind): string
|
||||
/** Frist als kurzer Text für die Oberfläche, z. B. "8 Jahre" oder "30 Tage". */
|
||||
function document_retention_text(string $kind, ?array $booking = null): string
|
||||
{
|
||||
$days = document_retention_days($kind);
|
||||
$days = document_retention_days($kind, $booking);
|
||||
if ($days <= 0) {
|
||||
return 'keine automatische Löschung';
|
||||
}
|
||||
|
|
@ -1601,10 +1760,10 @@ function documents_cleanup(): array
|
|||
}
|
||||
|
||||
$stmt = db()->query(
|
||||
'SELECT d.*, b.end_date FROM booking_documents d JOIN bookings b ON b.id = d.booking_id'
|
||||
'SELECT d.*, b.end_date, b.invoice FROM booking_documents d JOIN bookings b ON b.id = d.booking_id'
|
||||
);
|
||||
foreach ($stmt->fetchAll() as $doc) {
|
||||
$days = document_retention_days((string) $doc['kind']);
|
||||
$days = document_retention_days((string) $doc['kind'], $doc);
|
||||
if ($days <= 0) {
|
||||
continue; // dauerhaft aufbewahren
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue