feat(admin): Anzahlungskonten in der Verwaltung pflegen (zwei IBANs)
- 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
This commit is contained in:
parent
bf85a64a79
commit
cc8d7330f8
15 changed files with 478 additions and 76 deletions
|
|
@ -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<string, array{label: string, holder: string, bank: string, iban: string}>
|
||||
* @return list<array{id: int, label: string, holder: string, bank: string, iban: string, sort_order: int}>
|
||||
*/
|
||||
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<string, string> */
|
||||
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),
|
||||
];
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue