diff --git a/src/Controllers/InvoiceController.php b/src/Controllers/InvoiceController.php index 37dcd64..a58b0b9 100644 --- a/src/Controllers/InvoiceController.php +++ b/src/Controllers/InvoiceController.php @@ -271,10 +271,25 @@ class InvoiceController public function pdf(array $params): void { - $invoice = Invoice::find((int)$params['id']); + $invoice = Invoice::find((int)$params['id']); if (!$invoice) { http_response_code(404); return; } - // Bei Stornorechnung Originalrechnung laden für Referenzzeile + // Gespeicherte PDF aus dem Beleg ausliefern (GoBD: unveränderlicher Stand) + if (!empty($invoice['beleg_id'])) { + $beleg = Beleg::find((int)$invoice['beleg_id']); + $absPath = $beleg + ? dirname(__DIR__, 2) . '/public/' . $beleg['storage_path'] + : ''; + if ($absPath && file_exists($absPath)) { + header('Content-Type: application/pdf'); + header('Content-Disposition: attachment; filename="' . $beleg['filename'] . '"'); + header('Content-Length: ' . filesize($absPath)); + readfile($absPath); + return; + } + } + + // Fallback: PDF on-the-fly generieren (Entwurf oder Beleg noch ohne Datei) if (!empty($invoice['storno_of_id'])) { $original = Invoice::find((int)$invoice['storno_of_id']); if ($original) {