Fix: Beleg referenziert Rechnung via invoice_id, serveFile leitet auf Rechnungs-PDF weiter

This commit is contained in:
fds2610-bot 2026-05-05 15:29:50 +02:00
parent 994eb95f70
commit e62fbe9efe
4 changed files with 18 additions and 2 deletions

View file

@ -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;

View file

@ -230,6 +230,12 @@ class BelegController
$beleg = Beleg::find((int)$params['id']); $beleg = Beleg::find((int)$params['id']);
if (!$beleg) { http_response_code(404); return; } 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']; $absPath = dirname(__DIR__, 2) . '/public/' . $beleg['storage_path'];
if (!file_exists($absPath)) { http_response_code(404); return; } if (!file_exists($absPath)) { http_response_code(404); return; }

View file

@ -438,6 +438,7 @@ class InvoiceController
$belegNr = Beleg::generateBelegNr(); $belegNr = Beleg::generateBelegNr();
$belegId = Beleg::create([ $belegId = Beleg::create([
'invoice_id' => $id,
'beleg_nr' => $belegNr, 'beleg_nr' => $belegNr,
'filename' => $invoice['rechnungs_nr'] . '.pdf', 'filename' => $invoice['rechnungs_nr'] . '.pdf',
'storage_path' => '', 'storage_path' => '',

View file

@ -100,11 +100,14 @@ class Beleg
$fyId = $fy['id']; $fyId = $fy['id'];
} }
$invoiceId = isset($data['invoice_id']) && $data['invoice_id'] ? (int)$data['invoice_id'] : null;
Database::execute( 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) beleg_datum, rechnungs_nr, betrag_brutto, belegtyp, notes, fiscal_year_id)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)',
[ [
$invoiceId,
$data['beleg_nr'], $data['beleg_nr'],
$data['filename'], $data['filename'],
$data['storage_path'], $data['storage_path'],