// Druck-/PDF-Helfer: erzeugt eine eigenständige HTML-Seite mit der Rechnung/Gutschrift
// und öffnet sie als neues Fenster zum Drucken oder als PDF speichern.
function buildDocumentHTML({ titel, rechnungsNr, datum, student, posten, total, typ }) {
const isCredit = typ === 'gutschrift';
const farbe = isCredit ? '#047857' : '#2563eb';
const titleLabel = isCredit ? 'GUTSCHRIFT' : 'RECHNUNG';
const totalSign = isCredit ? '−' : '';
const fusszeile = isCredit
? 'Der Gutschriftbetrag wird auf das hinterlegte Konto überwiesen oder mit künftigen Rechnungen verrechnet.'
: 'Zahlbar innerhalb von 14 Tagen ohne Abzug. Bitte geben Sie bei Überweisung die Rechnungsnummer an.';
const rows = posten.map((p, i) => `
| ${String(i+1).padStart(2,'0')} |
${escapeHtml(p.titel)}
${escapeHtml(p.verlag || '')}
|
${escapeHtml((p.isbn || '').slice(-13))} |
${(p.betrag).toFixed(2).replace('.',',')} € |
`).join('');
return `
Städtisches Gymnasium
Schulstraße 12 · 52538 Gangelt · Tel. 02454 / 12345
${titleLabel}
Nr. ${escapeHtml(rechnungsNr)}
| Pos |
Bezeichnung |
ISBN |
${isCredit ? 'Gutschrift' : 'Preis'} |
${rows}
Zwischensumme
${totalSign}${total.toFixed(2).replace('.',',')} €
USt. (befreit § 4 Nr. 21 UStG)—
${isCredit ? 'Gutschriftbetrag' : 'Gesamt'}
${totalSign}${total.toFixed(2).replace('.',',')} €
`;
}
function escapeHtml(str) {
return String(str ?? '').replace(/[&<>"']/g, c => ({'&':'&','<':'<','>':'>','"':'"',"'":'''}[c]));
}
function addProtectedPreviewChrome(w) {
const d = w.document;
if (!d || !d.body || !d.head) return;
const style = d.createElement('style');
style.textContent = `
.codex-print-toolbar {
position: fixed;
top: 12px;
right: 12px;
z-index: 9999;
display: flex;
gap: 8px;
}
.codex-print-toolbar button {
font: inherit;
font-size: 12px;
padding: 8px 14px;
border-radius: 7px;
border: 1px solid #d1d5db;
background: #fff;
color: #0f172a;
cursor: pointer;
box-shadow: 0 2px 6px rgba(15, 23, 42, 0.15);
}
.codex-print-toolbar button.primary {
background: #1d4ed8;
border-color: #1d4ed8;
color: #fff;
}
@media screen {
html, body {
margin: 0 !important;
padding: 0 !important;
background: #e5e7eb !important;
}
body {
padding: 16px !important;
}
.codex-print-page {
max-width: 210mm;
min-height: 297mm;
margin: 44px auto 20px;
background: #fff;
box-shadow: 0 10px 28px rgba(15, 23, 42, 0.18);
padding: 18mm;
overflow: hidden;
}
.footer {
position: static !important;
left: auto !important;
right: auto !important;
bottom: auto !important;
margin-top: 16mm !important;
}
}
@media print {
.codex-print-toolbar { display: none !important; }
html, body {
background: #fff !important;
margin: 0 !important;
padding: 0 !important;
}
.codex-print-page {
max-width: none !important;
min-height: auto !important;
margin: 0 !important;
padding: 0 !important;
box-shadow: none !important;
overflow: visible !important;
}
}
`;
d.head.appendChild(style);
const toolbar = d.createElement('div');
toolbar.className = 'codex-print-toolbar';
toolbar.innerHTML = `