From e62fbe9efee8372f4f68ad11bf396f1536ad12dd Mon Sep 17 00:00:00 2001 From: fds2610-bot Date: Tue, 5 May 2026 15:29:50 +0200 Subject: [PATCH] Fix: Beleg referenziert Rechnung via invoice_id, serveFile leitet auf Rechnungs-PDF weiter --- migrations/043_belege_invoice_id.sql | 6 ++++++ src/Controllers/BelegController.php | 6 ++++++ src/Controllers/InvoiceController.php | 1 + src/Models/Beleg.php | 7 +++++-- 4 files changed, 18 insertions(+), 2 deletions(-) create mode 100644 migrations/043_belege_invoice_id.sql diff --git a/migrations/043_belege_invoice_id.sql b/migrations/043_belege_invoice_id.sql new file mode 100644 index 0000000..5272a79 --- /dev/null +++ b/migrations/043_belege_invoice_id.sql @@ -0,0 +1,6 @@ +ALTER TABLE belege + ADD COLUMN invoice_id INT UNSIGNED NULL DEFAULT NULL + COMMENT 'FK zur zugehörigen Ausgangsrechnung (wenn kein physischer Beleg)' + AFTER id, + ADD CONSTRAINT fk_belege_invoice + FOREIGN KEY (invoice_id) REFERENCES invoices(id) ON DELETE SET NULL; diff --git a/src/Controllers/BelegController.php b/src/Controllers/BelegController.php index e34fb1d..471cbfb 100644 --- a/src/Controllers/BelegController.php +++ b/src/Controllers/BelegController.php @@ -230,6 +230,12 @@ class BelegController $beleg = Beleg::find((int)$params['id']); if (!$beleg) { http_response_code(404); return; } + // Beleg ohne physische Datei → PDF der verknüpften Rechnung ausliefern + if (empty($beleg['storage_path']) && !empty($beleg['invoice_id'])) { + header('Location: /invoices/' . (int)$beleg['invoice_id'] . '/pdf'); + exit; + } + $absPath = dirname(__DIR__, 2) . '/public/' . $beleg['storage_path']; if (!file_exists($absPath)) { http_response_code(404); return; } diff --git a/src/Controllers/InvoiceController.php b/src/Controllers/InvoiceController.php index 81045c2..c6ad0b2 100644 --- a/src/Controllers/InvoiceController.php +++ b/src/Controllers/InvoiceController.php @@ -438,6 +438,7 @@ class InvoiceController $belegNr = Beleg::generateBelegNr(); $belegId = Beleg::create([ + 'invoice_id' => $id, 'beleg_nr' => $belegNr, 'filename' => $invoice['rechnungs_nr'] . '.pdf', 'storage_path' => '', diff --git a/src/Models/Beleg.php b/src/Models/Beleg.php index 4004430..080e3d5 100644 --- a/src/Models/Beleg.php +++ b/src/Models/Beleg.php @@ -100,11 +100,14 @@ class Beleg $fyId = $fy['id']; } + $invoiceId = isset($data['invoice_id']) && $data['invoice_id'] ? (int)$data['invoice_id'] : null; + Database::execute( - 'INSERT INTO belege (beleg_nr, filename, storage_path, mime_type, file_hash, + 'INSERT INTO belege (invoice_id, beleg_nr, filename, storage_path, mime_type, file_hash, beleg_datum, rechnungs_nr, betrag_brutto, belegtyp, notes, fiscal_year_id) - VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', [ + $invoiceId, $data['beleg_nr'], $data['filename'], $data['storage_path'],