feat(booking): Restzahlung bar oder per Überweisung mit Zahlungserinnerung und Verfügbarkeitsampel

- Buchung: neues Feld balance_method (cash|transfer) inkl. Migration 019,
  Formularfeld, Validierung (Konto Pflicht bei Überweisung), Anzeige in
  Buchungsdetail und kopiertem Kundentext.
- Auftragsbestätigung: bei Überweisung Bankverbindung der Buchung, Restbetrag
  mit Zahlungsziel (5 Tage vor Mietbeginn) und Hinweis auf die Erinnerung;
  bei Barzahlung unverändert "bei der Übergabe in bar fällig".
- Zahlungserinnerung als eigene Unterlage (Migration 018): offener Betrag
  (Restbetrag bei Anzahlung, sonst Gesamtbetrag), Konto der Buchung, Frist,
  Erzeugen/Mailen über die Buchungsseite, Handbuch und README.
- Automatischer Versand: payment_reminder_send_due() im Cron-Lauf, 10 Tage vor
  Mietbeginn (app.transfer_reminder_days), genau ein Versand pro Termin über
  payment_reminder_log, kein Doppelversand nach manuellem Mailen.
- Startseite: öffentliche Verfügbarkeitsampel für Kühlanhänger ohne Truhen
  (GET /verfuegbarkeit.json, availability_public_days()), Monatskalender mit
  Grün (>2 frei), Gelb (1-2) und Rot (ausgebucht); neuer Abschnitt
  #verfuegbarkeit inkl. Navigation, Footer und noscript-Hinweis.
- Aufräumen: payment_reminder_log nach einem Jahr, Cron-Report erweitert.
This commit is contained in:
torsten 2026-11-30 21:55:51 +01:00
parent 506d802b33
commit 7445740b90
21 changed files with 911 additions and 46 deletions

View file

@ -191,6 +191,7 @@ function booking_input(array $source): array
'deposit_cents' => $depositRequired ? parse_money((string) ($source['deposit'] ?? '0')) : 0,
'deposit_required' => $depositRequired,
'deposit_account_id' => (int) ($source['deposit_account_id'] ?? 0),
'balance_method' => (string) ($source['balance_method'] ?? 'cash') === 'transfer' ? 'transfer' : 'cash',
'rent_cents' => $rent,
'delivery_cents' => $delivery,
'total_cents' => $rent + $delivery,
@ -223,6 +224,7 @@ function booking_to_input(array $booking): array
'deposit_cents' => (int) $booking['deposit_cents'],
'deposit_required' => (int) ($booking['deposit_required'] ?? 0),
'deposit_account_id' => (int) ($booking['deposit_account_id'] ?? 0),
'balance_method' => (string) ($booking['balance_method'] ?? 'cash') === 'transfer' ? 'transfer' : 'cash',
'rent_cents' => (int) ($booking['rent_cents'] ?? $booking['total_cents']),
'delivery_cents' => (int) ($booking['delivery_cents'] ?? 0),
'total_cents' => (int) $booking['total_cents'],
@ -343,6 +345,10 @@ function booking_validate(array $data, int $excludeId = 0, int $allowTrailerId =
if ((int) ($data['deposit_required'] ?? 0) === 1 && !deposit_account((int) ($data['deposit_account_id'] ?? 0))) {
$errors['deposit_account'] = 'Bitte das Konto für die Anzahlung auswählen.';
}
if ((string) ($data['balance_method'] ?? 'cash') === 'transfer'
&& !deposit_account((int) ($data['deposit_account_id'] ?? 0))) {
$errors['balance_method'] = 'Für die Restzahlung per Überweisung bitte ein Konto auswählen.';
}
if (!$errors && $trailer) {
$conflicts = booking_conflicts((int) $trailer['id'], $data['start_date'], $data['end_date'], $excludeId);
@ -539,11 +545,11 @@ function booking_create(array $data, int $userId): int
$stmt = $pdo->prepare(
'INSERT INTO bookings
(trailer_id, employee_id, return_employee_id, start_date, end_date, start_time, end_time, deposit_cents,
deposit_required, deposit_account_id,
deposit_required, deposit_account_id, balance_method,
rent_cents, delivery_cents, total_cents, invoice, invoice_no, kind,
customer_name, street, city, delivery_street, delivery_city, mobile, email, note, agreements,
status, created_at, updated_at, created_by, updated_by)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, "active", ?, ?, ?, ?)'
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, "active", ?, ?, ?, ?)'
);
$stmt->execute([
$data['trailer_id'], $data['employee_id'], $data['return_employee_id'] ?: null,
@ -551,6 +557,7 @@ function booking_create(array $data, int $userId): int
null_if_empty($data['start_time']), null_if_empty($data['end_time']),
$data['deposit_cents'], (int) ($data['deposit_required'] ?? 0),
((int) ($data['deposit_account_id'] ?? 0)) > 0 ? (int) $data['deposit_account_id'] : null,
(string) ($data['balance_method'] ?? 'cash'),
$data['rent_cents'], $data['delivery_cents'], $data['total_cents'],
$data['invoice'], null_if_empty($data['invoice_no']), $data['kind'],
$data['customer_name'], $data['street'], $data['city'],
@ -586,6 +593,7 @@ function booking_update(int $id, array $data, int $userId): void
$stmt = $pdo->prepare(
'UPDATE bookings SET trailer_id = ?, employee_id = ?, return_employee_id = ?, start_date = ?, end_date = ?,
start_time = ?, end_time = ?, deposit_cents = ?, deposit_required = ?, deposit_account_id = ?,
balance_method = ?,
rent_cents = ?, delivery_cents = ?, total_cents = ?, invoice = ?, invoice_no = ?, kind = ?,
customer_name = ?, street = ?, city = ?, delivery_street = ?, delivery_city = ?,
mobile = ?, email = ?, note = ?, agreements = ?, updated_at = ?,
@ -597,6 +605,7 @@ function booking_update(int $id, array $data, int $userId): void
null_if_empty($data['start_time']), null_if_empty($data['end_time']),
$data['deposit_cents'], (int) ($data['deposit_required'] ?? 0),
((int) ($data['deposit_account_id'] ?? 0)) > 0 ? (int) $data['deposit_account_id'] : null,
(string) ($data['balance_method'] ?? 'cash'),
$data['rent_cents'], $data['delivery_cents'], $data['total_cents'],
$data['invoice'], null_if_empty($data['invoice_no']), $data['kind'],
$data['customer_name'], $data['street'], $data['city'],
@ -794,6 +803,88 @@ function booking_invoice_display(array $booking): string
}
/** Exakt der Text aus der Anforderung, Zeile für Zeile. */
/* ------------------------------------------------- Öffentliche Verfügbarkeit */
/**
* Tagesübersicht für den öffentlichen Kalender auf der Startseite: wie viele
* Kühlanhänger (ohne Truhen) pro Tag noch frei sind.
*
* Bewusst nur Zähler – keine Namen, keine Buchungsdetails. Ein Tag gilt als
* belegt, wenn eine aktive Buchung ihn einschließt (Beginn bis Ende).
*
* @return array{total: int, days: array<string, array{date: string, booked: int, free: int, level: string}>}
*/
function availability_public_days(string $fromIso, string $toIso): array
{
if (!valid_date($fromIso) || !valid_date($toIso) || $toIso < $fromIso) {
throw new InvalidArgumentException('Ungültiger Zeitraum.');
}
$trailers = array_values(array_filter(trailers_all(), static fn (array $t): bool => !trailer_is_chest($t)));
$total = count($trailers);
$booked = [];
if ($total > 0) {
$ids = array_map(static fn (array $t): int => (int) $t['id'], $trailers);
$placeholders = implode(',', array_fill(0, count($ids), '?'));
$stmt = db()->prepare(
"SELECT start_date, end_date FROM bookings
WHERE status = 'active' AND trailer_id IN ($placeholders)
AND start_date <= ? AND end_date >= ?"
);
$stmt->execute([...$ids, $toIso, $fromIso]);
foreach ($stmt->fetchAll() as $row) {
// Nur der überlappende Teil der Buchung, damit lange Mietverhältnisse
// die Tagesschleife nicht auffressen.
$cursor = date_obj(max((string) $row['start_date'], $fromIso));
$end = date_obj(min((string) $row['end_date'], $toIso));
$guard = 0;
while ($cursor <= $end && $guard < 800) {
$iso = $cursor->format('Y-m-d');
if ($iso >= $fromIso && $iso <= $toIso) {
$booked[$iso] = ($booked[$iso] ?? 0) + 1;
}
$cursor = $cursor->modify('+1 day');
$guard++;
}
}
}
$days = [];
$cursor = date_obj($fromIso);
$end = date_obj($toIso);
$guard = 0;
while ($cursor <= $end && $guard < 800) {
$iso = $cursor->format('Y-m-d');
$used = $booked[$iso] ?? 0;
$free = max(0, $total - $used);
$days[$iso] = [
'date' => $iso,
'booked' => min($used, $total),
'free' => $free,
'level' => availability_level($free, $total),
];
$cursor = $cursor->modify('+1 day');
$guard++;
}
return ['total' => $total, 'days' => $days];
}
/**
* Ampelfarbe eines Tages: 0 frei = rot, 1–2 frei = gelb, mehr als 2 = grün.
* Ohne einsatzbereite Anhänger gibt es keine Aussage (grau).
*/
function availability_level(int $free, int $total): string
{
if ($total <= 0) {
return 'unknown';
}
if ($free <= 0) {
return 'full';
}
return $free <= 2 ? 'few' : 'free';
}
function booking_copy_text(array $booking): string
{
return implode("\n", [
@ -803,6 +894,9 @@ function booking_copy_text(array $booking): string
: ''),
'Gesamtbetrag: ' . fmt_money((int) $booking['total_cents']),
'Noch zu zahlen: ' . fmt_money(due_cents((int) $booking['total_cents'], (int) $booking['deposit_cents'])),
'Restzahlung: ' . (booking_balance_transfer($booking)
? 'Überweisung (bis ' . fmt_date(booking_balance_due_date($booking)) . ')'
: 'bar bei der Übergabe'),
'Rechnung: ' . booking_invoice_display($booking),
'Von: ' . 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),