From cc8d7330f89ebdb9e59f71077428c8314cc915fc Mon Sep 17 00:00:00 2001 From: torsten Date: Mon, 30 Nov 2026 17:10:24 +0100 Subject: [PATCH] feat(admin): Anzahlungskonten in der Verwaltung pflegen (zwei IBANs) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Migration 016_deposit_accounts.sql: Tabelle deposit_accounts (label, holder, bank, iban, sort_order), gefüllt mit Postbank (DE12 …1261 30) und Sparkasse (DE83 3206 0362 3303 2140 10); bookings.deposit_account -> deposit_account_id mit Fremdschlüssel (ON DELETE SET NULL), Altbestand wird aufs erste Konto gemappt; schema.sql nachgezogen - Neue Seite Verwaltung → Konten (/admin/accounts): anlegen, ändern, löschen (Löschen nur ohne Buchungsbezug), Anzeige der Verwendung je Konto - Buchungsformular wählt aus dieser Liste (Anzeige mit IBAN-Ende), Validierung prüft IBAN-Format, Auftragsbestätigung druckt das gewählte Konto - deposit_accounts()/deposit_account() lesen aus der DB (Cache wird nach Änderungen verworfen); Konfigurations-Override entfällt - Anleitung (Abschnitt 3 und 15), README, config.example.php und ROADMAP (Feature 25) aktualisiert --- README.md | 11 +- ROADMAP.md | 4 + app/admin.php | 80 +++++++++++ app/config.example.php | 11 +- app/data.php | 16 ++- app/documents.php | 175 ++++++++++++++++++------ app/pages.php | 8 +- sql/migrations/016_deposit_accounts.sql | 42 ++++++ sql/schema.sql | 16 ++- views/_admin_nav.php | 1 + views/admin_accounts.php | 163 ++++++++++++++++++++++ views/booking_detail.php | 4 +- views/booking_form.php | 16 ++- views/layout.php | 1 + views/manual.php | 6 +- 15 files changed, 478 insertions(+), 76 deletions(-) create mode 100644 sql/migrations/016_deposit_accounts.sql create mode 100644 views/admin_accounts.php diff --git a/README.md b/README.md index 84955bd..9c15961 100644 --- a/README.md +++ b/README.md @@ -23,12 +23,13 @@ Die Anforderungen stammen aus `web/assets/icons/`. - **Anzahlung in der Auftragsbestätigung**: Im Buchungsformular wird neben dem Betrag festgelegt, ob eine - Anzahlung fällig ist (`bookings.deposit_required`) und auf welches Konto sie geht - (`bookings.deposit_account`); die Konten stehen in `deposit_accounts()` (`app/documents.php`, überschreibbar - über `app.deposit_accounts`). Die Auftragsbestätigung nennt Betrag, Frist + Anzahlung fällig ist (`bookings.deposit_required`) und auf welches Konto sie geht (`bookings.deposit_account_id`); + die Konten (Bezeichnung, Inhaber, Bank, IBAN, Sortierung) pflegt der Administrator unter + **Verwaltung → Konten** – Tabelle `deposit_accounts`, Migration `016_deposit_accounts.sql`, Konten, die in + Buchungen verwendet werden, sind gegen Löschen geschützt. Die Auftragsbestätigung nennt Betrag, Frist (`app.deposit_due_working_days`, Standard 7 Werktage Mo–Sa ohne NRW-Feiertage) mit Datum, Kontoinhaber/Bank/IBAN, - Verwendungszweck und den Hinweis, dass der Termin erst nach Zahlungseingang reserviert ist. Migration - `015_booking_deposit.sql`. + Verwendungszweck und den Hinweis, dass der Termin erst nach Zahlungseingang reserviert ist (Migrationen + `015_booking_deposit.sql`, `016_deposit_accounts.sql`). - **Sicherungen**: Unter **Verwaltung → Sicherungen** legt der Admin mit einem Klick ein Archiv aus Datenbank und allen abgelegten Dateien an, lädt es herunter, spielt einen früheren Stand wieder ein (mit automatischer diff --git a/ROADMAP.md b/ROADMAP.md index 7be0d10..be3e628 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -86,6 +86,10 @@ - **Umsetzung:** Migration `015_booking_deposit.sql` ergänzt `bookings.deposit_required` und `bookings.deposit_account` (bestehende Buchungen mit Betrag > 0 werden auf „JA“ gesetzt). Im Formular stehen „Anzahlung fällig: JA/NEIN“ und die Kontoauswahl neben dem Betrag (nur Verwaltung); Nicht-Admins können die Werte nicht ändern. `deposit_accounts()`/`deposit_due_date()` in `app/documents.php` liefern Konto und Frist (7 Werktage Mo–Sa, ohne NRW-Feiertage, per `app.deposit_due_working_days` einstellbar). Die Auftragsbestätigung druckt den Block „Anzahlung und verbindliche Reservierung“ mit Betrag, Datum, Kontoinhaber, Bank, IBAN, Verwendungszweck und dem Hinweis, dass der Termin erst nach Zahlungseingang reserviert ist; Beträge und Signatur rücken dafür kompakter zusammen und bleiben auf einer Seite. Buchungsdetail und Kopiertext zeigen Fälligkeit/Konto. - **Prüfung:** Buchung mit „JA“ und 50 € anlegen – Auftragsbestätigung nennt 50,00 €, Fristdatum in 7 Werktagen, Konto und Reservierungshinweis; „NEIN“ erzeugt keine Anzahlungszeile und keinen Absatz (Anzahlung = 0,00 €); „JA“ ohne Betrag meldet einen Fehler; Nicht-Admin sieht die Felder gesperrt; bestehende Buchung mit Anzahlung zeigt nach der Migration „JA“. +- [x] **Feature 25: Anzahlungskonten pflegbar (zwei IBANs)** + - **Umsetzung:** Migration `016_deposit_accounts.sql` legt die Tabelle `deposit_accounts` an (label, holder, bank, iban, sort_order) und füllt sie mit Postbank (`DE12 1001 0010 0464 1261 30`) und Sparkasse (`DE83 3206 0362 3303 2140 10`); `bookings.deposit_account` wird zu `bookings.deposit_account_id` mit Fremdschlüssel (ON DELETE SET NULL), Altbestand wird auf das erste Konto gemappt. Neue Admin-Seite **Verwaltung → Konten** (`/admin/accounts`) mit Anlegen, Ändern, Löschen (Löschen nur ohne Buchungsbezug) und Anzeige der Verwendung; Buchungsformular wählt aus dieser Liste, Auftragsbestätigung druckt das gewählte Konto mit IBAN. + - **Prüfung:** Migration auf einer Kopie der Live-DB: zwei Konten vorhanden, alle Buchungen mit „Anzahlung fällig“ zeigen Konto 1; Buchung mit Konto 2 erzeugen – Auftragsbestätigung nennt `DE83 3206 0362 3303 2140 10`; Konto anlegen/umbenennen/löschen funktioniert, Löschen eines verwendeten Kontos wird abgewiesen; ungültige IBAN wird gemeldet. + ## Rückmeldungen Michael (30.11.2026) - [x] **Feature 20: Logo und Namensumbruch im Mietvertrag** diff --git a/app/admin.php b/app/admin.php index d79a114..cda06ef 100644 --- a/app/admin.php +++ b/app/admin.php @@ -46,6 +46,10 @@ function admin_handle(string $path, array $user): void admin_mailings($user); return; } + if ($path === '/admin/accounts') { + admin_accounts(); + return; + } if ($path === '/admin/backups') { admin_backups($user); return; @@ -836,3 +840,79 @@ function admin_backup_download(string $name): void readfile($path); exit; } + +/* -------------------------------------------------- Anzahlungskonten */ + +function admin_accounts(): void +{ + $errors = []; + $createErrors = []; + $createValues = ['label' => '', 'holder' => '', 'bank' => '', 'iban' => '', 'sort_order' => '']; + $rowValues = []; + + if (is_post()) { + require_post(); + $action = post_string('action'); + try { + if ($action === 'create') { + $createValues = deposit_account_input($_POST); + $createErrors = deposit_account_validate($createValues); + if ($createErrors === []) { + $id = deposit_account_create($createValues); + flash('success', 'Konto „' . $createValues['label'] . '“ angelegt (ID ' . $id . ').'); + redirect('/admin/accounts'); + } + } elseif ($action === 'update') { + $id = (int) post_string('id'); + $account = deposit_account($id); + if ($account === null) { + throw new RuntimeException('Dieses Konto gibt es nicht (mehr).'); + } + $rowValues[$id] = deposit_account_input($_POST); + $rowErrors = deposit_account_validate($rowValues[$id]); + if ($rowErrors !== []) { + throw new InvalidArgumentException(implode(' ', $rowErrors)); + } + deposit_account_update($id, $rowValues[$id]); + flash('success', 'Konto „' . $rowValues[$id]['label'] . '“ gespeichert.'); + redirect('/admin/accounts'); + } elseif ($action === 'delete') { + $id = (int) post_string('id'); + $account = deposit_account($id); + if ($account === null) { + throw new RuntimeException('Dieses Konto gibt es nicht (mehr).'); + } + if (!deposit_account_delete($id)) { + $used = deposit_account_usage($id); + throw new RuntimeException('Das Konto wird in ' . $used . ' Buchung' . ($used === 1 ? '' : 'en') + . ' verwendet und kann deshalb nicht gelöscht werden. Bitte nur umbenennen oder die ' + . 'Buchungen auf ein anderes Konto umstellen.'); + } + flash('success', 'Konto „' . (string) $account['label'] . '“ gelöscht.'); + redirect('/admin/accounts'); + } + } catch (InvalidArgumentException $error) { + $decoded = json_decode($error->getMessage(), true); + $errors = is_array($decoded) ? $decoded : ['general' => $error->getMessage()]; + } catch (Throwable $error) { + error_log('Anzahlungskonten: ' . $error->getMessage()); + $errors['general'] = $error->getMessage(); + } + http_response_code(422); + } + + $accounts = deposit_accounts(); + $usage = []; + foreach ($accounts as $account) { + $usage[(int) $account['id']] = deposit_account_usage((int) $account['id']); + } + + render_page('admin_accounts', [ + 'accounts' => $accounts, + 'usage' => $usage, + 'errors' => $errors, + 'createErrors' => $createErrors, + 'createValues' => $createValues, + 'rowValues' => $rowValues, + ], 'Konten'); +} diff --git a/app/config.example.php b/app/config.example.php index 8023e3b..29f1a85 100644 --- a/app/config.example.php +++ b/app/config.example.php @@ -56,16 +56,9 @@ return [ // Maximale Größe einer hochgeladenen Sicherung (MB). 'backup_max_mb' => 200, // Anzahlung in der Auftragsbestätigung: Frist in Werktagen (Mo–Sa ohne - // gesetzliche Feiertage). Die Konten stehen als Standard in - // app/documents.php (deposit_accounts()) und lassen sich hier - // überschreiben: Schlüssel => ['label', 'holder', 'bank', 'iban']. + // gesetzliche Feiertage). Die Konten selbst pflegt der Administrator + // unter Verwaltung → Konten (Tabelle deposit_accounts). 'deposit_due_working_days' => 7, - // 'deposit_accounts' => [ - // 'postbank' => ['label' => 'Postbank – Michael Schmitz', 'holder' => 'Michael Schmitz', - // 'bank' => 'Postbank', 'iban' => 'DE…'], - // 'sparkasse' => ['label' => 'Sparkasse – Drink it cold', 'holder' => '…', - // 'bank' => '…', 'iban' => 'DE…'], - // ], ], // Optional: Angaben für Impressum und Datenschutz überschreiben. Die // Standardwerte stehen in app/legal.php. Hinweis: tools/deploy.sh erzeugt diff --git a/app/data.php b/app/data.php index 01d12a4..9ba5491 100644 --- a/app/data.php +++ b/app/data.php @@ -190,7 +190,7 @@ function booking_input(array $source): array 'end_time' => normalize_time($source['end_time'] ?? ''), 'deposit_cents' => $depositRequired ? parse_money((string) ($source['deposit'] ?? '0')) : 0, 'deposit_required' => $depositRequired, - 'deposit_account' => trim((string) ($source['deposit_account'] ?? '')), + 'deposit_account_id' => (int) ($source['deposit_account_id'] ?? 0), 'rent_cents' => $rent, 'delivery_cents' => $delivery, 'total_cents' => $rent + $delivery, @@ -219,7 +219,7 @@ function booking_to_input(array $booking): array 'end_time' => normalize_time($booking['end_time'] ?? ''), 'deposit_cents' => (int) $booking['deposit_cents'], 'deposit_required' => (int) ($booking['deposit_required'] ?? 0), - 'deposit_account' => (string) ($booking['deposit_account'] ?? ''), + 'deposit_account_id' => (int) ($booking['deposit_account_id'] ?? 0), 'rent_cents' => (int) ($booking['rent_cents'] ?? $booking['total_cents']), 'delivery_cents' => (int) ($booking['delivery_cents'] ?? 0), 'total_cents' => (int) $booking['total_cents'], @@ -326,7 +326,7 @@ function booking_validate(array $data, int $excludeId = 0, int $allowTrailerId = } elseif ((int) ($data['deposit_required'] ?? 0) === 1 && $data['deposit_cents'] <= 0) { $errors['deposit'] = 'Bitte den Betrag der Anzahlung angeben oder „Anzahlung: NEIN“ wählen.'; } - if ((int) ($data['deposit_required'] ?? 0) === 1 && !deposit_account((string) ($data['deposit_account'] ?? ''))) { + 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.'; } @@ -525,7 +525,7 @@ 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, + deposit_required, deposit_account_id, rent_cents, delivery_cents, total_cents, invoice, invoice_no, kind, customer_name, street, city, mobile, email, note, status, created_at, updated_at, created_by, updated_by) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, "active", ?, ?, ?, ?)' @@ -534,7 +534,8 @@ function booking_create(array $data, int $userId): int $data['trailer_id'], $data['employee_id'], $data['return_employee_id'] ?: null, $data['start_date'], $data['end_date'], null_if_empty($data['start_time']), null_if_empty($data['end_time']), - $data['deposit_cents'], (int) ($data['deposit_required'] ?? 0), (string) ($data['deposit_account'] ?? ''), + $data['deposit_cents'], (int) ($data['deposit_required'] ?? 0), + ((int) ($data['deposit_account_id'] ?? 0)) > 0 ? (int) $data['deposit_account_id'] : null, $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'], $data['mobile'], $data['email'], @@ -567,7 +568,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 = ?, + start_time = ?, end_time = ?, deposit_cents = ?, deposit_required = ?, deposit_account_id = ?, rent_cents = ?, delivery_cents = ?, total_cents = ?, invoice = ?, invoice_no = ?, kind = ?, customer_name = ?, street = ?, city = ?, mobile = ?, email = ?, note = ?, updated_at = ?, updated_by = ? WHERE id = ?' @@ -576,7 +577,8 @@ function booking_update(int $id, array $data, int $userId): void $data['trailer_id'], $data['employee_id'], $data['return_employee_id'] ?: null, $data['start_date'], $data['end_date'], null_if_empty($data['start_time']), null_if_empty($data['end_time']), - $data['deposit_cents'], (int) ($data['deposit_required'] ?? 0), (string) ($data['deposit_account'] ?? ''), + $data['deposit_cents'], (int) ($data['deposit_required'] ?? 0), + ((int) ($data['deposit_account_id'] ?? 0)) > 0 ? (int) $data['deposit_account_id'] : null, $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'], $data['mobile'], $data['email'], diff --git a/app/documents.php b/app/documents.php index 776aa26..1577ce9 100644 --- a/app/documents.php +++ b/app/documents.php @@ -419,56 +419,149 @@ function document_pdf_rows(PdfDoc $pdf, float $top, array $rows, float $valueX = } /** - * Konten, auf die eine Anzahlung überwiesen werden kann. + * Anzahlungskonten (Verwaltung → Konten), sortiert. * - * Standard steht in dieser Funktion, damit sie auch dann greift, wenn die - * app/config.local.php beim Deployment neu erzeugt wird (dort wird kein - * `app.deposit_accounts` geschrieben). Weitere Konten lassen sich hier - * ergänzen oder per `app.deposit_accounts` in der lokalen Konfiguration - * überschreiben (Schlüssel => label/holder/bank/iban). + * Der Stamm liegt in der Tabelle deposit_accounts und wird vom Administrator + * gepflegt; die Auftragsbestätigung druckt das in der Buchung gewählte Konto. * - * @return array + * @return list */ -function deposit_accounts(): array +function deposit_accounts(bool $refresh = false): array { - $defaults = [ - 'postbank' => [ - 'label' => 'Postbank – Michael Schmitz', - 'holder' => 'Michael Schmitz', - 'bank' => 'Postbank', - 'iban' => 'DE12 1001 0010 0464 1261 30', - ], - ]; - - $configured = config('app.deposit_accounts', null); - if (!is_array($configured) || $configured === []) { - return $defaults; + static $cache = null; + if ($refresh) { + $cache = null; } - - $accounts = []; - foreach ($configured as $key => $account) { - if (!is_array($account)) { - continue; - } - $accounts[(string) $key] = [ - 'label' => (string) ($account['label'] ?? $key), - 'holder' => (string) ($account['holder'] ?? ''), - 'bank' => (string) ($account['bank'] ?? ''), - 'iban' => (string) ($account['iban'] ?? ''), + if (is_array($cache)) { + return $cache; + } + $cache = []; + try { + $rows = db()->query('SELECT id, label, holder, bank, iban, sort_order FROM deposit_accounts ORDER BY sort_order, id') + ->fetchAll(); + } catch (Throwable) { + return $cache; // Tabelle fehlt noch (Migration ausstehend) + } + foreach ($rows as $row) { + $cache[] = [ + 'id' => (int) $row['id'], + 'label' => (string) $row['label'], + 'holder' => (string) $row['holder'], + 'bank' => (string) $row['bank'], + 'iban' => (string) $row['iban'], + 'sort_order' => (int) $row['sort_order'], ]; } - return $accounts !== [] ? $accounts : $defaults; + return $cache; } -/** Konto zur Kennung; ohne gültige Kennung das erste Konto (oder null). */ -function deposit_account(string $key): ?array +/** Einzelnes Konto; ohne gültige Kennung null. */ +function deposit_account(int $id): ?array { - $accounts = deposit_accounts(); - if ($key !== '' && isset($accounts[$key])) { - return $accounts[$key]; + foreach (deposit_accounts() as $account) { + if ((int) $account['id'] === $id) { + return $account; + } } - $first = reset($accounts); - return $first === false ? null : $first; + return null; +} + +/** Anzeigename inklusive IBAN-Ende, damit zwei Konten unterscheidbar sind. */ +function deposit_account_label(array $account): string +{ + $iban = preg_replace('/\s+/', '', (string) $account['iban']) ?? ''; + return (string) $account['label'] . ($iban !== '' ? ' (…' . substr($iban, -4) . ')' : ''); +} + +/** Eingabewerte eines Anzahlungskontos normalisieren. */ +function deposit_account_input(array $source): array +{ + return [ + 'label' => trim((string) ($source['label'] ?? '')), + 'holder' => trim((string) ($source['holder'] ?? '')), + 'bank' => trim((string) ($source['bank'] ?? '')), + 'iban' => mb_strtoupper(trim((string) ($source['iban'] ?? ''))), + 'sort_order' => (int) ($source['sort_order'] ?? 0), + ]; +} + +/** @return array */ +function deposit_account_validate(array $data): array +{ + $errors = []; + if ($data['label'] === '') { + $errors['label'] = 'Bitte eine Bezeichnung angeben (z. B. „Postbank – Michael Schmitz“).'; + } elseif (mb_strlen($data['label']) > 80) { + $errors['label'] = 'Die Bezeichnung ist zu lang (max. 80 Zeichen).'; + } + if ($data['holder'] === '') { + $errors['holder'] = 'Bitte den Kontoinhaber angeben.'; + } elseif (mb_strlen($data['holder']) > 120) { + $errors['holder'] = 'Der Kontoinhaber ist zu lang (max. 120 Zeichen).'; + } + if (mb_strlen($data['bank']) > 80) { + $errors['bank'] = 'Der Bankname ist zu lang (max. 80 Zeichen).'; + } + $iban = preg_replace('/\s+/', '', $data['iban']) ?? ''; + if ($iban === '') { + $errors['iban'] = 'Bitte die IBAN angeben.'; + } elseif (mb_strlen($iban) < 15 || mb_strlen($iban) > 34 || preg_match('/^[A-Z]{2}[0-9A-Z]+$/', $iban) !== 1) { + $errors['iban'] = 'Die IBAN sieht nicht gültig aus (z. B. DE12 1001 0010 0464 1261 30).'; + } + return $errors; +} + +function deposit_account_create(array $data): int +{ + $errors = deposit_account_validate($data); + if ($errors !== []) { + throw new InvalidArgumentException((string) json_encode($errors, JSON_UNESCAPED_UNICODE)); + } + $stmt = db()->prepare( + 'INSERT INTO deposit_accounts (label, holder, bank, iban, sort_order, created_at, updated_at) + VALUES (?, ?, ?, ?, ?, ?, ?)' + ); + $stmt->execute([ + $data['label'], $data['holder'], $data['bank'], $data['iban'], $data['sort_order'], + utc_now(), utc_now(), + ]); + $id = (int) db()->lastInsertId(); // vor dem Neuladen der Liste lesen + deposit_accounts(true); + return $id; +} + +function deposit_account_update(int $id, array $data): void +{ + $errors = deposit_account_validate($data); + if ($errors !== []) { + throw new InvalidArgumentException((string) json_encode($errors, JSON_UNESCAPED_UNICODE)); + } + db()->prepare( + 'UPDATE deposit_accounts SET label = ?, holder = ?, bank = ?, iban = ?, sort_order = ?, updated_at = ? + WHERE id = ?' + )->execute([ + $data['label'], $data['holder'], $data['bank'], $data['iban'], $data['sort_order'], utc_now(), $id, + ]); + deposit_accounts(true); +} + +/** Anzahl der Buchungen, die dieses Konto verwenden. */ +function deposit_account_usage(int $id): int +{ + $stmt = db()->prepare('SELECT COUNT(*) FROM bookings WHERE deposit_account_id = ?'); + $stmt->execute([$id]); + return (int) $stmt->fetchColumn(); +} + +/** Löscht ein Konto – nur solange keine Buchung darauf verweist. */ +function deposit_account_delete(int $id): bool +{ + if (deposit_account_usage($id) > 0) { + return false; + } + db()->prepare('DELETE FROM deposit_accounts WHERE id = ?')->execute([$id]); + deposit_accounts(true); + return true; } /** Frist für die Anzahlung in Werktagen (Mo–Sa, ohne gesetzliche Feiertage). */ @@ -750,12 +843,12 @@ function document_confirmation_pdf(array $booking, string $variant): string } // Anzahlung: vereinbart (Häkchen in der Buchung) und Betrag > 0. $depositRequired = (int) ($booking['deposit_required'] ?? 0) === 1; - $account = $depositRequired && $paid > 0 ? deposit_account((string) ($booking['deposit_account'] ?? '')) : null; + $account = $depositRequired && $paid > 0 ? deposit_account((int) ($booking['deposit_account_id'] ?? 0)) : null; $dueDate = $account !== null ? deposit_due_date() : null; $workingDays = deposit_due_working_days(); if ($paid > 0) { $priceRows[] = [ - $depositRequired ? 'Anzahlung (bis ' . fmt_date((string) $dueDate) . ')' : 'Anzahlung', + $account !== null ? 'Anzahlung (bis ' . fmt_date((string) $dueDate) . ')' : 'Anzahlung', '− ' . fmt_money($paid), ]; } diff --git a/app/pages.php b/app/pages.php index ef85394..eb87a10 100644 --- a/app/pages.php +++ b/app/pages.php @@ -823,7 +823,7 @@ function form_values_from_input(array $data): array 'end_time' => (string) ($data['end_time'] ?? ''), 'deposit' => number_format($data['deposit_cents'] / 100, 2, ',', ''), 'deposit_required' => (int) ($data['deposit_required'] ?? 0), - 'deposit_account' => (string) ($data['deposit_account'] ?? ''), + 'deposit_account_id' => (int) ($data['deposit_account_id'] ?? 0), 'rent' => number_format($data['rent_cents'] / 100, 2, ',', ''), 'delivery' => number_format($data['delivery_cents'] / 100, 2, ',', ''), 'invoice' => (int) $data['invoice'], @@ -877,7 +877,7 @@ function page_booking_new(array $user, string $method): void 'end_time' => '', 'deposit' => '', 'deposit_required' => 0, - 'deposit_account' => '', + 'deposit_account_id' => (int) (deposit_accounts()[0]['id'] ?? 0), 'rent' => '', 'delivery' => '', 'invoice' => 0, @@ -933,7 +933,7 @@ function page_booking_edit(array $user, int $id, string $method): void 'end_time' => (string) ($booking['end_time'] ?? ''), 'deposit' => number_format((int) $booking['deposit_cents'] / 100, 2, ',', ''), 'deposit_required' => (int) ($booking['deposit_required'] ?? 0), - 'deposit_account' => (string) ($booking['deposit_account'] ?? ''), + 'deposit_account_id' => (int) ($booking['deposit_account_id'] ?? 0) ?: (int) (deposit_accounts()[0]['id'] ?? 0), 'rent' => number_format((int) $booking['rent_cents'] / 100, 2, ',', ''), 'delivery' => number_format((int) $booking['delivery_cents'] / 100, 2, ',', ''), 'invoice' => (int) $booking['invoice'], @@ -959,7 +959,7 @@ function page_booking_edit(array $user, int $id, string $method): void if (!$canEditBilling) { $data['deposit_cents'] = (int) $booking['deposit_cents']; $data['deposit_required'] = (int) ($booking['deposit_required'] ?? 0); - $data['deposit_account'] = (string) ($booking['deposit_account'] ?? ''); + $data['deposit_account_id'] = (int) ($booking['deposit_account_id'] ?? 0); $data['rent_cents'] = (int) $booking['rent_cents']; $data['delivery_cents'] = (int) $booking['delivery_cents']; $data['total_cents'] = (int) $booking['total_cents']; diff --git a/sql/migrations/016_deposit_accounts.sql b/sql/migrations/016_deposit_accounts.sql new file mode 100644 index 0000000..4ffe518 --- /dev/null +++ b/sql/migrations/016_deposit_accounts.sql @@ -0,0 +1,42 @@ +-- Anzahlungskonten pflegbar machen (Verwaltung → Konten). +-- +-- Bisher stand das Konto als Schlüssel in bookings.deposit_account und der +-- Kontenstamm im Code. Jetzt gibt es eine Tabelle, die der Administrator +-- pflegt; Buchungen verweisen per Fremdschlüssel darauf. +CREATE TABLE IF NOT EXISTS deposit_accounts ( + id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, + label VARCHAR(80) NOT NULL, + holder VARCHAR(120) NOT NULL, + bank VARCHAR(80) NOT NULL DEFAULT '', + iban VARCHAR(34) NOT NULL, + sort_order INT NOT NULL DEFAULT 0, + created_at DATETIME NOT NULL, + updated_at DATETIME NOT NULL, + PRIMARY KEY (id), + KEY idx_deposit_accounts_sort (sort_order, id) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; + +INSERT INTO deposit_accounts (label, holder, bank, iban, sort_order, created_at, updated_at) +VALUES + ('Postbank – Michael Schmitz', 'Michael Schmitz', 'Postbank', 'DE12 1001 0010 0464 1261 30', 1, UTC_TIMESTAMP(), UTC_TIMESTAMP()), + ('Sparkasse – Michael Schmitz', 'Michael Schmitz', 'Sparkasse', 'DE83 3206 0362 3303 2140 10', 2, UTC_TIMESTAMP(), UTC_TIMESTAMP()); + +ALTER TABLE bookings ADD COLUMN deposit_account_id BIGINT UNSIGNED NULL AFTER deposit_account; + +UPDATE bookings SET deposit_account_id = (SELECT MIN(id) FROM deposit_accounts) + WHERE deposit_account = 'postbank'; + +UPDATE bookings SET deposit_account_id = (SELECT MAX(id) FROM deposit_accounts) + WHERE deposit_account <> '' AND deposit_account_id IS NULL; + +-- Buchungen mit „Anzahlung fällig“, aber ohne gespeichertes Konto (Altbestand +-- aus 015) bekommen das erste Konto, damit die Auftragsbestätigung weiterhin +-- Konto und Frist nennt. +UPDATE bookings SET deposit_account_id = (SELECT MIN(id) FROM deposit_accounts) + WHERE deposit_required = 1 AND deposit_account_id IS NULL; + +ALTER TABLE bookings DROP COLUMN deposit_account; + +ALTER TABLE bookings + ADD CONSTRAINT fk_bookings_deposit_account FOREIGN KEY (deposit_account_id) + REFERENCES deposit_accounts (id) ON DELETE SET NULL; diff --git a/sql/schema.sql b/sql/schema.sql index a62afcb..449473d 100644 --- a/sql/schema.sql +++ b/sql/schema.sql @@ -55,6 +55,19 @@ CREATE TABLE IF NOT EXISTS users ( CONSTRAINT fk_users_employee FOREIGN KEY (employee_id) REFERENCES employees (id) ON DELETE SET NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; +CREATE TABLE IF NOT EXISTS deposit_accounts ( + id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, + label VARCHAR(80) NOT NULL, + holder VARCHAR(120) NOT NULL, + bank VARCHAR(80) NOT NULL DEFAULT '', + iban VARCHAR(34) NOT NULL, + sort_order INT NOT NULL DEFAULT 0, + created_at DATETIME NOT NULL, + updated_at DATETIME NOT NULL, + PRIMARY KEY (id), + KEY idx_deposit_accounts_sort (sort_order, id) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; + CREATE TABLE IF NOT EXISTS bookings ( id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, trailer_id BIGINT UNSIGNED NOT NULL, @@ -67,7 +80,7 @@ CREATE TABLE IF NOT EXISTS bookings ( deposit_cents INT UNSIGNED NOT NULL DEFAULT 0, -- Anzahlung laut Auftragsbestätigung: vereinbart? auf welches Konto? deposit_required TINYINT(1) NOT NULL DEFAULT 0, - deposit_account VARCHAR(40) NOT NULL DEFAULT '', + deposit_account_id BIGINT UNSIGNED NULL DEFAULT NULL, rent_cents INT UNSIGNED NOT NULL DEFAULT 0, delivery_cents INT UNSIGNED NOT NULL DEFAULT 0, total_cents INT UNSIGNED NOT NULL DEFAULT 0, @@ -96,6 +109,7 @@ CREATE TABLE IF NOT EXISTS bookings ( CONSTRAINT fk_bookings_trailer FOREIGN KEY (trailer_id) REFERENCES trailers (id) ON DELETE RESTRICT, CONSTRAINT fk_bookings_employee FOREIGN KEY (employee_id) REFERENCES employees (id) ON DELETE RESTRICT, CONSTRAINT fk_bookings_return_employee FOREIGN KEY (return_employee_id) REFERENCES employees (id) ON DELETE SET NULL, + CONSTRAINT fk_bookings_deposit_account FOREIGN KEY (deposit_account_id) REFERENCES deposit_accounts (id) ON DELETE SET NULL, CONSTRAINT fk_bookings_created_by FOREIGN KEY (created_by) REFERENCES users (id) ON DELETE SET NULL, CONSTRAINT fk_bookings_updated_by FOREIGN KEY (updated_by) REFERENCES users (id) ON DELETE SET NULL, CONSTRAINT fk_bookings_cancelled_by FOREIGN KEY (cancelled_by) REFERENCES users (id) ON DELETE SET NULL diff --git a/views/_admin_nav.php b/views/_admin_nav.php index d4d115c..810560a 100644 --- a/views/_admin_nav.php +++ b/views/_admin_nav.php @@ -5,6 +5,7 @@ $items = [ '/admin/employees' => ['label' => 'Mitarbeiter', 'icon' => 'i-users'], '/admin/trailers' => ['label' => 'Anhänger', 'icon' => 'i-truck'], '/admin/mailings' => ['label' => 'Rundmail', 'icon' => 'i-mail'], + '/admin/accounts' => ['label' => 'Konten', 'icon' => 'i-bank'], '/admin/backups' => ['label' => 'Sicherungen', 'icon' => 'i-database'], ]; ?> diff --git a/views/admin_accounts.php b/views/admin_accounts.php new file mode 100644 index 0000000..a73b597 --- /dev/null +++ b/views/admin_accounts.php @@ -0,0 +1,163 @@ + $accounts */ +/** @var array $usage */ +/** @var array $errors */ +/** @var array $createErrors */ +/** @var array $createValues */ +/** @var array> $rowValues */ +$error = static fn (string $key): string => (string) ($errors[$key] ?? ''); +$createError = static fn (string $key): string => (string) ($createErrors[$key] ?? ''); +?> +
+
+

Verwaltung

+

Konten

+

Konten, auf die Kunden die Anzahlung überweisen. Im Buchungsformular + wird je Buchung eines davon ausgewählt; die Auftragsbestätigung druckt Kontoinhaber, Bank und IBAN. + Änderungen gelten für alle neu erzeugten Auftragsbestätigungen.

+
+
+ + + + + + + +
+
+
+

Hinterlegte Konten

+

Konto. Reihenfolge über „Sortierung“ + (kleinere Zahl zuerst).

+
+
+
> + + Konto anlegen + +
+
+ + + + + + + +
+ +
+
+
+
+
+
+ + +

Noch kein Konto hinterlegt – ohne Konto kann keine Anzahlung in der Auftragsbestätigung genannt werden.

+ +
+ + + + + + + + + + + + + + + $account['label'], + 'holder' => $account['holder'], + 'bank' => $account['bank'], + 'iban' => $account['iban'], + 'sort_order' => $account['sort_order'] ?? 0, + ]; + $used = (int) ($usage[$id] ?? 0); + ?> + + + + + + + +
BezeichnungKontoinhaberBankIBANSortierungVerwendet inAktionen
+
+ + + + + + + + +
+
–' : (int) $used . ' Buchung' . ($used === 1 ? '' : 'en') ?> +
+ +
+ + + + +
+
+
+
+

Konten, die bereits in Buchungen verwendet werden, lassen sich nur ändern, nicht löschen – + sonst ginge die Zuordnung in alten Auftragsbestätigungen verloren.

+ +
diff --git a/views/booking_detail.php b/views/booking_detail.php index 88f8342..f82a721 100644 --- a/views/booking_detail.php +++ b/views/booking_detail.php @@ -203,10 +203,10 @@ $returnTo = '/bookings/' . (int) $booking['id'];
Gesamtbetrag
Anzahlung
0) { - $depositTarget = deposit_account((string) ($booking['deposit_account'] ?? '')); + $depositTarget = deposit_account((int) ($booking['deposit_account_id'] ?? 0)); echo ' · fällig bis ' . e(fmt_date(deposit_due_date())); if ($depositTarget !== null) { - echo ' auf ' . e((string) $depositTarget['label']); + echo ' auf ' . e(deposit_account_label($depositTarget)); } } ?>
diff --git a/views/booking_form.php b/views/booking_form.php index 55e0d48..09c3841 100644 --- a/views/booking_form.php +++ b/views/booking_form.php @@ -7,7 +7,7 @@ /** @var array $user */ /** @var bool $isEdit */ /** @var bool $canEditBilling */ -/** @var array $depositAccounts */ +/** @var list $depositAccounts */ /** @var int $depositDueDays */ $fieldError = static fn (string $key): string => (string) ($errors[$key] ?? ''); $cancelled = $isEdit && ($booking['status'] ?? '') === 'cancelled'; @@ -232,9 +232,9 @@ $backUrl = $isEdit ? '/bookings/' . (int) $booking['id'] : '/calendar';
Anzahlung fällig · Konto
- > + + @@ -244,7 +244,8 @@ $backUrl = $isEdit ? '/bookings/' . (int) $booking['id'] : '/calendar';
Bei „JA“ fordert die Auftragsbestätigung die Anzahlung mit - Frist ( Werktage) und nennt das Konto. + Frist ( Werktage) und nennt das Konto. Konten pflegt die + Verwaltung unter „Konten“.
@@ -295,7 +296,10 @@ $backUrl = $isEdit ? '/bookings/' . (int) $booking['id'] : '/calendar';
Gesamtbetrag
Anzahlung
-
Konto
+
Konto
Noch zu zahlen
diff --git a/views/layout.php b/views/layout.php index 7eab645..3234466 100644 --- a/views/layout.php +++ b/views/layout.php @@ -74,6 +74,7 @@ $navActive = static function (string $area) use ($path): string { + diff --git a/views/manual.php b/views/manual.php index 33ecd44..be89d0d 100644 --- a/views/manual.php +++ b/views/manual.php @@ -104,7 +104,8 @@ $admin = !empty($isAdmin); Unterschrift entfernt; danach lässt sich die passende Variante neu erzeugen. Ein bereits an den Kunden gemailtes PDF bleibt natürlich in dessen Postfach.

Anzahlung: Im Buchungsformular lässt sich bei den Beträgen einstellen, ob eine Anzahlung - fällig ist („Anzahlung fällig: JA/NEIN“) und auf welches Konto sie überwiesen werden soll. + fällig ist („Anzahlung fällig: JA/NEIN“) und auf welches Konto sie überwiesen werden soll + (die Konten pflegt der Administrator unter Verwaltung → Konten). Bei „JA“ nennt die Auftragsbestätigung den Betrag, die Frist von 7 Werktagen mit konkretem Datum, das Konto mit IBAN und den Verwendungszweck – verbunden mit dem Hinweis, dass der Termin erst nach Zahlungseingang verbindlich reserviert ist. Ohne Anzahlung erscheint der Absatz nicht. @@ -303,6 +304,9 @@ $admin = !empty($isAdmin); erscheinen nicht mehr in neuen Buchungen, bleiben historisch erhalten.

  • Benutzer: Konten anlegen und einladen, Mitarbeiterkonto zuordnen (steuert Erinnerungen und Push).
  • Rundmail: Empfänger aus Buchungen, Testmail, Versand in Häppchen, Protokoll je Empfänger.
  • +
  • Konten: Konten für die Anzahlung (Bezeichnung, Inhaber, Bank, IBAN, Sortierung) anlegen + und ändern. Sie erscheinen im Buchungsformular und in der Auftragsbestätigung; Konten, die bereits in + Buchungen verwendet werden, lassen sich nicht löschen.
  • Konfiguration (app/config.local.php): Mailversand, Kaution, Aufbewahrung (app.document_retention_days), Vertragstexte je Variante (documents.delivery|pickup|chest).