@le-space/eigenbeleg 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/pdf.js ADDED
@@ -0,0 +1,277 @@
1
+ // SPDX-License-Identifier: AGPL-3.0-or-later
2
+ // The Eigenbeleg of a period as an A4 PDF/A-2b (@le-space/pdf): fonts
3
+ // embedded as subsets, text in any script, an sRGB output intent, XMP
4
+ // metadata that matches the document info, and a document id. PDF/A is what
5
+ // an archive for bookkeeping records expects.
6
+ //
7
+ // The text is German: the document is for German bookkeeping.
8
+
9
+ import { createPdfA, savePdfA } from '@le-space/pdf';
10
+ import { round } from '@le-space/ledger';
11
+
12
+ import { DATA_SOURCES, deDate, deEuro, deNumber } from './document.js';
13
+ import { A4, BLACK, GREY, WARN, pageLayout } from './layout.js';
14
+
15
+ /** What made the file, unless the caller says otherwise. */
16
+ export const CREATOR = 'eigenbeleg';
17
+ export const PRODUCER = 'eigenbeleg (Le Space UG), pdf-lib';
18
+
19
+ /**
20
+ * @param {import('./document.js').EigenbelegDocument} doc
21
+ * @param {{ creator?: string, producer?: string }} [o]
22
+ * @returns {Promise<Uint8Array>}
23
+ */
24
+ export async function eigenbelegPdf(doc, { creator = CREATOR, producer = PRODUCER } = {}) {
25
+ const created = new Date(doc.issuedAt);
26
+ const title = `Eigenbeleg ${doc.number}`;
27
+ const subject = `${doc.layout === 'single' ? 'Eigenbelege' : 'Sammel-Eigenbeleg'} ${doc.chainName}, ${deDate(doc.period.from)} – ${deDate(doc.period.to)}`;
28
+ const { pdf, fonts } = await createPdfA();
29
+ const { regular, bold, mono } = fonts;
30
+ const M = 48;
31
+ let pageNo = 1;
32
+ let current = doc.number;
33
+ const footerRight = `Daten: ${DATA_SOURCES[doc.chain] ?? doc.chainName} · Kurse: siehe Kursquelle`;
34
+
35
+ const footer = (/** @type {import('./layout.js').Layout} */ l) => {
36
+ const s = 7;
37
+ regular.draw(l.page, `${current} · Seite ${pageNo}`, { x: M, y: M / 2, size: s, color: GREY });
38
+ regular.draw(l.page, footerRight, {
39
+ x: A4[0] - M - regular.width(footerRight, s),
40
+ y: M / 2,
41
+ size: s,
42
+ color: GREY
43
+ });
44
+ };
45
+ const l = pageLayout(pdf, fonts, {
46
+ margin: M,
47
+ leavePage: footer,
48
+ enterPage: () => {
49
+ pageNo++;
50
+ }
51
+ });
52
+ const { text, gap, field } = l;
53
+ const width = l.width;
54
+
55
+ /** @param {string} number @param {string} subtitle */
56
+ function head(number, subtitle) {
57
+ current = number;
58
+ if (doc.testnet) {
59
+ text(
60
+ 'TESTNET – keine echte Zahlung. Kurse des Mainnet-Assets nur zur Veranschaulichung; dieser Beleg ist kein Buchungsbeleg.',
61
+ { face: bold, size: 9, color: WARN }
62
+ );
63
+ gap(4);
64
+ }
65
+ text('Eigenbeleg', { face: bold, size: 20 });
66
+ text(`Nr. ${number}`, { face: bold, size: 12 });
67
+ gap(2);
68
+ text(subtitle, { size: 10 });
69
+ gap(10);
70
+ // payer | issue data, two columns
71
+ const top = l.y;
72
+ text('Zahlender und Aussteller', { face: bold, size: 8.5, color: GREY, max: width / 2 - 10 });
73
+ text(doc.payer.name, { face: bold, size: 10, max: width / 2 - 10 });
74
+ text(doc.payer.address, { size: 9.5, max: width / 2 - 10 });
75
+ const leftEnd = l.y;
76
+ l.y = top;
77
+ const rx = M + width / 2 + 10;
78
+ text('Ausgestellt am', { x: rx, face: bold, size: 8.5, color: GREY });
79
+ text(`${deDate(doc.issuedAt)}, ${doc.issuedAt.slice(11, 16)} UTC`, { x: rx });
80
+ gap(3);
81
+ text('Empfänger', { x: rx, face: bold, size: 8.5, color: GREY });
82
+ text(`${doc.chainName}: Peer-to-Peer-Netzwerk ohne rechnungsstellende Organisation`, { x: rx });
83
+ l.y = Math.min(l.y, leftEnd);
84
+ gap(8);
85
+ text(doc.payer.addresses.length > 1 ? 'Eigene Zahlungsadressen' : 'Eigene Zahlungsadresse', {
86
+ face: bold,
87
+ size: 8.5,
88
+ color: GREY
89
+ });
90
+ for (const a of doc.payer.addresses) text(a, { face: mono, size: 8 });
91
+ gap(10);
92
+ l.box(doc.note);
93
+ gap(12);
94
+ }
95
+
96
+ const columns = [
97
+ { title: 'Bewegung', width: 150 },
98
+ { title: 'Menge', width: 118, right: true },
99
+ { title: 'Kurs EUR', width: 52, right: true },
100
+ { title: 'Wert EUR', width: 66, right: true },
101
+ { title: 'Auf dem Beleg', width: width - 386 }
102
+ ];
103
+
104
+ /**
105
+ * One transaction: hash, who paid whom with full addresses, its QR code,
106
+ * and every movement it had, the ones not on the Eigenbeleg with the reason.
107
+ *
108
+ * @param {import('./document.js').Transaction} t @param {{ big?: boolean }} [o]
109
+ */
110
+ function transaction(t, { big = false } = {}) {
111
+ const side = big ? 96 : 58;
112
+ const textMax = width - side - 12;
113
+ l.ensure(big ? 150 : 120);
114
+ const top = l.y;
115
+ const title = `${big ? 'Zahlung' : `Position ${t.pos}`} · ${deDate(t.time)} ${t.time.slice(11, 19)} UTC`;
116
+ text(title, { face: bold, size: 9.5, max: textMax });
117
+ gap(2);
118
+ field('Transaktion', t.hash, { face: mono, size: 7.5, max: textMax });
119
+ field('Von', `${t.from.address} (${t.from.label})`, { face: mono, size: 7.5, max: textMax });
120
+ field('An', t.to.label ? `${t.to.address} (${t.to.label})` : t.to.address, {
121
+ face: mono,
122
+ size: 7.5,
123
+ max: textMax
124
+ });
125
+ field('Block-Explorer', t.explorerUrl, { face: mono, size: 6.5, max: textMax });
126
+ const qrTop = top - 2;
127
+ l.qr(t.explorerUrl, A4[0] - M - side, qrTop, side);
128
+ l.y = Math.min(l.y, qrTop - side - 4);
129
+ gap(3);
130
+ l.row(
131
+ columns,
132
+ columns.map((c) => c.title),
133
+ { face: bold, color: GREY }
134
+ );
135
+ for (const m of t.movements) {
136
+ const e = m.entry;
137
+ l.row(
138
+ columns,
139
+ [
140
+ `${m.what}${e.type === 'received' ? ' (Eingang)' : ''}`,
141
+ `${deNumber(e.amount)} ${e.asset}`,
142
+ e.eur ? deNumber(e.eur.rate) : '—',
143
+ e.eur ? deEuro(e.eur.value, 4) : '—',
144
+ m.onIt ? 'ja' : `nein – ${m.why}`
145
+ ],
146
+ { color: m.onIt ? BLACK : GREY }
147
+ );
148
+ }
149
+ l.ensure(12);
150
+ l.right(`Auf dem Beleg: ${deEuro(t.eurExact, 4)}`, A4[0] - M, 8.5, bold);
151
+ l.y -= 14;
152
+ }
153
+
154
+ /** @param {{ why: string }[]} excluded @param {string[]} sources */
155
+ function sourcesAndSignature(excluded, sources) {
156
+ gap(6);
157
+ text('Kursquelle', { face: bold, size: 8.5, color: GREY });
158
+ text(sources.length ? `${sources.join('; ')} – jeweils für den Tag der Zahlung (UTC).` : '—', {
159
+ size: 8.5
160
+ });
161
+ gap(6);
162
+ if (excluded.length) {
163
+ text('Nicht auf diesem Eigenbeleg', { face: bold, size: 8.5, color: GREY });
164
+ /** @type {Record<string, number>} */
165
+ const why = {};
166
+ for (const x of excluded) why[x.why] = (why[x.why] ?? 0) + 1;
167
+ for (const [w, n] of Object.entries(why)) text(`${n} × ${w}`, { size: 8.5 });
168
+ text('Alle Bewegungen des Zeitraums stehen in der CSV-Datei zu diesem Eigenbeleg.', {
169
+ size: 8.5,
170
+ color: GREY
171
+ });
172
+ gap(6);
173
+ }
174
+ text(doc.notInvoice, { size: 8.5, color: GREY });
175
+ gap(24);
176
+ l.ensure(40);
177
+ l.page.drawLine({
178
+ start: { x: M, y: l.y },
179
+ end: { x: M + 230, y: l.y },
180
+ thickness: 0.6,
181
+ color: BLACK
182
+ });
183
+ l.y -= 4;
184
+ text('Ort, Datum, Unterschrift des Zahlenden', { size: 8, color: GREY });
185
+ }
186
+
187
+ /** @param {import('./document.js').Transaction[]} ts */
188
+ const sourcesOf = (ts) => [
189
+ ...new Set(
190
+ ts.flatMap((t) =>
191
+ t.movements
192
+ .filter((m) => m.onIt)
193
+ .map((m) => /** @type {any} */ (m.entry.eur).source.replace(/ \d{4}-\d{2}-\d{2}.*$/, ''))
194
+ )
195
+ )
196
+ ];
197
+ /** The amount line: a label on the left, the euro amount on the right. @param {string} label @param {string} total */
198
+ const amountLine = (label, total) => {
199
+ l.ensure(40);
200
+ l.at(label, M, 10, bold);
201
+ l.right(total, A4[0] - M, 11, bold);
202
+ l.y -= 16;
203
+ };
204
+ const period = `${deDate(doc.period.from)} – ${deDate(doc.period.to)}`;
205
+
206
+ if (doc.layout === 'single' && doc.transactions.length > 0) {
207
+ doc.transactions.forEach((t, i) => {
208
+ if (i > 0) {
209
+ // Each Eigenbeleg starts on a page of its own, counted from 1.
210
+ footer(l);
211
+ l.page = pdf.addPage(A4);
212
+ l.pages.push(l.page);
213
+ pageNo = 1;
214
+ l.y = A4[1] - M;
215
+ }
216
+ head(
217
+ t.number,
218
+ `Eigenbeleg für eine Zahlung auf ${doc.chainName} (${doc.network}) am ${deDate(t.time)}`
219
+ );
220
+ transaction(t, { big: true });
221
+ const what = [...new Set(t.movements.filter((m) => m.onIt).map((m) => m.what))].join(', ');
222
+ amountLine('Betrag', deEuro(t.eurExact, 2));
223
+ if (round(t.eurExact, 2) === '0' && t.eurExact !== '0')
224
+ text(`Ungerundet ${deEuro(t.eurExact, 8)} (unter einem Cent).`, { size: 8, color: GREY });
225
+ gap(4);
226
+ field('Was wurde bezahlt', `${what} an ${t.to.label || t.to.address}`);
227
+ field(
228
+ 'Warum kein Fremdbeleg',
229
+ 'Der Empfänger ist ein Peer-to-Peer-Netzwerk ohne rechnungsstellende Organisation; es stellt keine Rechnung aus.'
230
+ );
231
+ sourcesAndSignature([], sourcesOf([t]));
232
+ });
233
+ } else {
234
+ head(
235
+ doc.number,
236
+ `Sammel-Eigenbeleg für Zahlungen auf ${doc.chainName} (${doc.network}), Zeitraum ${period}`
237
+ );
238
+ if (doc.transactions.length === 0) {
239
+ text('Keine Zahlungen an das Netzwerk in diesem Zeitraum.', { size: 10 });
240
+ } else {
241
+ for (const t of doc.transactions) {
242
+ transaction(t);
243
+ l.rule();
244
+ gap(8);
245
+ }
246
+ const qty = Object.entries(doc.totals.quantities)
247
+ .map(([a, q]) => `${deNumber(q)} ${a}`)
248
+ .join(', ');
249
+ const n = doc.transactions.length;
250
+ amountLine(
251
+ `Summe (${n} ${n === 1 ? 'Position' : 'Positionen'}): ${qty}`,
252
+ deEuro(doc.totals.eur, 2)
253
+ );
254
+ const exact = /^0(\.0*)?$/.test(round(doc.totals.eurExact, 4))
255
+ ? deEuro(doc.totals.eurExact, 8)
256
+ : deEuro(doc.totals.eurExact, 4);
257
+ const subCent = doc.totals.eur === '0' && doc.totals.eurExact !== '0';
258
+ text(
259
+ `Ungerundet ${exact}${subCent ? ' (unter einem Cent)' : ''}; jede Position zum Tageskurs ihres Zahlungstages.`,
260
+ { size: 8, color: GREY }
261
+ );
262
+ }
263
+ gap(4);
264
+ sourcesAndSignature(doc.excluded, doc.sources);
265
+ }
266
+ footer(l);
267
+
268
+ return savePdfA(pdf, {
269
+ title,
270
+ author: doc.payer.name,
271
+ subject,
272
+ creator,
273
+ producer,
274
+ created,
275
+ seed: `${doc.number}|${doc.issuedAt}|${doc.payer.name}`
276
+ });
277
+ }
@@ -0,0 +1,164 @@
1
+ // SPDX-License-Identifier: AGPL-3.0-or-later
2
+ // The layout of a statement Eigenbeleg (Belege's for Aleph and Akash, its
3
+ // travel statement): an A4 PDF/A-2b in Eigenbeleg's look (layout.js) – a head
4
+ // with the title, number, issuer and date, labelled fields, rules, headings,
5
+ // tables whose header repeats on a new page, and a line to sign; a running
6
+ // head on every further page and the page numbers at the foot. Pages as long
7
+ // as the content needs; text in any script (@le-space/pdf).
8
+ //
9
+ // Moved from Belege's app/src/lib/pdf/statement-layout.js; the same calls,
10
+ // now in Eigenbeleg's look and PDF/A.
11
+
12
+ import { createPdfA, savePdfA } from '@le-space/pdf';
13
+
14
+ import { deDate } from './document.js';
15
+ import { A4, BLACK, GREY, pageLayout } from './layout.js';
16
+
17
+ /** The label column of a field: wide enough for "Übernachtungspauschalen". */
18
+ const LABEL = 120;
19
+
20
+ /**
21
+ * A new document and the means to fill it.
22
+ *
23
+ * @param {object} p
24
+ * @param {string} p.title the PDF's title
25
+ * @param {Date} p.created
26
+ * @param {string} p.continuation printed on top of every further page
27
+ * @param {string} [p.subject]
28
+ * @param {string} [p.creator] what made it: `Belege` when not given
29
+ */
30
+ export async function statementLayout({
31
+ title,
32
+ created,
33
+ continuation,
34
+ subject = '',
35
+ creator = 'Belege'
36
+ }) {
37
+ const { pdf, fonts } = await createPdfA();
38
+ const { regular, bold, mono } = fonts;
39
+ const M = 48;
40
+ let number = '';
41
+ let issuer = '';
42
+ const l = pageLayout(pdf, fonts, {
43
+ margin: M,
44
+ enterPage: (p) => {
45
+ p.at(continuation, M, 8, regular, GREY);
46
+ p.y -= 20;
47
+ }
48
+ });
49
+ const width = l.width;
50
+
51
+ /**
52
+ * The title ("Eigenbeleg" unless another), its number, the issuer and the
53
+ * day it was made, a line under them.
54
+ *
55
+ * @param {string} no
56
+ * @param {string} by the issuer, '' when none is set
57
+ * @param {string} subtitle
58
+ * @param {string} [heading]
59
+ */
60
+ function head(no, by, subtitle, heading = 'Eigenbeleg') {
61
+ number = no;
62
+ issuer = by;
63
+ l.text(heading, { face: bold, size: 20 });
64
+ l.text(`Nr. ${no}`, { face: bold, size: 12 });
65
+ l.gap(2);
66
+ l.text(subtitle, { size: 10 });
67
+ l.gap(10);
68
+ const top = l.y;
69
+ const half = width / 2 - 10;
70
+ l.text('Aussteller', { face: bold, size: 8.5, color: GREY, max: half });
71
+ l.text(by || '—', { face: bold, size: 10, max: half });
72
+ const leftEnd = l.y;
73
+ l.y = top;
74
+ const rx = M + width / 2 + 10;
75
+ l.text('Erstellt am', { x: rx, face: bold, size: 8.5, color: GREY });
76
+ l.text(`${deDate(created.toISOString())}, ${created.toISOString().slice(11, 16)} UTC`, {
77
+ x: rx
78
+ });
79
+ l.y = Math.min(l.y, leftEnd);
80
+ l.gap(8);
81
+ rule();
82
+ }
83
+
84
+ /** @param {string} label @param {string} value @param {boolean | 'mono'} [style] */
85
+ function field(label, value, style = false) {
86
+ l.field(label, value, {
87
+ face: style === 'mono' ? mono : style ? bold : regular,
88
+ size: style === 'mono' ? 7.5 : 8.5,
89
+ labelWidth: LABEL
90
+ });
91
+ l.gap(3);
92
+ }
93
+
94
+ function rule() {
95
+ l.ensure(20);
96
+ l.gap(2);
97
+ l.rule();
98
+ l.gap(10);
99
+ }
100
+
101
+ /** @param {string} t */
102
+ function heading(t) {
103
+ l.ensure(40);
104
+ l.text(t, { face: bold, size: 10 });
105
+ l.gap(3);
106
+ }
107
+
108
+ /**
109
+ * A table: columns by width and alignment; the header repeats on a new page.
110
+ *
111
+ * @param {import('./layout.js').Column[]} columns
112
+ * @param {string[][]} rows
113
+ */
114
+ function table(columns, rows) {
115
+ const header = () =>
116
+ l.row(
117
+ columns.map((c) => ({ ...c, mono: false })),
118
+ columns.map((c) => c.title),
119
+ { face: bold, color: GREY }
120
+ );
121
+ l.ensure(30);
122
+ header();
123
+ for (const row of rows) l.row(columns, row, { header });
124
+ l.gap(8);
125
+ }
126
+
127
+ function signature() {
128
+ l.gap(24);
129
+ l.ensure(40);
130
+ l.page.drawLine({
131
+ start: { x: M, y: l.y },
132
+ end: { x: M + 230, y: l.y },
133
+ thickness: 0.6,
134
+ color: BLACK
135
+ });
136
+ l.y -= 4;
137
+ l.text('Ort, Datum, Unterschrift', { size: 8, color: GREY });
138
+ }
139
+
140
+ async function save() {
141
+ const n = l.pages.length;
142
+ l.pages.forEach((p, i) => {
143
+ const foot = `${number ? `${number} · ` : ''}Seite ${i + 1} von ${n}`;
144
+ regular.draw(p, foot, { x: M, y: M / 2, size: 7, color: GREY });
145
+ const made = `Erstellt mit ${creator}`;
146
+ regular.draw(p, made, {
147
+ x: A4[0] - M - regular.width(made, 7),
148
+ y: M / 2,
149
+ size: 7,
150
+ color: GREY
151
+ });
152
+ });
153
+ return savePdfA(pdf, {
154
+ title,
155
+ author: issuer,
156
+ subject,
157
+ creator,
158
+ created,
159
+ seed: `${number}|${created.toISOString()}|${title}`
160
+ });
161
+ }
162
+
163
+ return { width, head, field, rule, heading, table, signature, save };
164
+ }
@@ -0,0 +1,235 @@
1
+ // SPDX-License-Identifier: AGPL-3.0-or-later
2
+ //
3
+ // The Kontoauszug as an A4 PDF/A-2b, laid out like a bank statement:
4
+ // holder and account in the head, per asset the opening balance, a table of
5
+ // bookings (date, text, debit, credit, running balance, EUR value), the
6
+ // closing balance with its check against the chain, and the sums a
7
+ // bookkeeper needs. German, for German bookkeeping. Aleph Cloud credits get
8
+ // the same layout titled »Verbrauchsauszug« (`st.title`).
9
+
10
+ import { rgb } from 'pdf-lib';
11
+ import { createPdfA, savePdfA, wrapText } from '@le-space/pdf';
12
+ import { round } from '@le-space/ledger';
13
+
14
+ import { DATA_SOURCES, deDate, deEuro, deNumber } from './document.js';
15
+ import { CREATOR, PRODUCER } from './pdf.js';
16
+ import { A4, BAND, BLACK, GREY, OK, RULE, WARN, pageLayout } from './layout.js';
17
+
18
+ const M = 44;
19
+ const DARK = { thickness: 0.8, color: rgb(0.2, 0.2, 0.2) };
20
+
21
+ const COLS = [
22
+ { title: 'Buchungstag', width: 52 },
23
+ { title: 'Vorgang', width: 175 },
24
+ { title: 'Soll', width: 72, right: true },
25
+ { title: 'Haben', width: 72, right: true },
26
+ { title: 'Saldo', width: 80, right: true },
27
+ { title: 'Wert EUR', width: 56, right: true }
28
+ ];
29
+
30
+ /** Quantities in the table: at most 12 decimals, the CSV keeps them all. @param {string} q */
31
+ const qty = (q) => (q ? deNumber(round(q, 12)) : '');
32
+
33
+ /**
34
+ * @param {import('./statement.js').Statement} st
35
+ * @param {{ creator?: string, producer?: string }} [o]
36
+ * @returns {Promise<Uint8Array>}
37
+ */
38
+ export async function statementPdf(st, { creator = CREATOR, producer = PRODUCER } = {}) {
39
+ const created = new Date(st.issuedAt);
40
+ // Aleph credits: a Verbrauchsauszug in the same layout, valued by its own rule.
41
+ const aleph = st.chain === 'aleph';
42
+ const title = `${st.title} ${st.number}`;
43
+ const subject = `${st.title} ${st.chainName} ${st.account}, ${deDate(st.period.from)} – ${deDate(st.period.to)}`;
44
+ const { pdf, fonts } = await createPdfA();
45
+ const { regular, bold, mono } = fonts;
46
+ const l = pageLayout(pdf, fonts, {
47
+ margin: M,
48
+ bottom: 16,
49
+ enterPage: (p) => {
50
+ p.at(`${st.title} ${st.number} · ${st.account}`, M, 8, regular, GREY);
51
+ p.y -= 18;
52
+ }
53
+ });
54
+ const { text } = l;
55
+ const width = l.width;
56
+ /** @param {number} y @param {{ thickness: number, color: import('pdf-lib').Color }} [o] */
57
+ const line = (y, o = DARK) =>
58
+ l.page.drawLine({ start: { x: M, y }, end: { x: A4[0] - M, y }, ...o });
59
+
60
+ // ---- head: like a bank's, with the holder's address window on the left
61
+ if (st.testnet) {
62
+ text('TESTNET – keine echten Werte. Kurse des Mainnet-Assets nur zur Veranschaulichung.', {
63
+ face: bold,
64
+ size: 8.5,
65
+ color: WARN
66
+ });
67
+ l.y -= 4;
68
+ }
69
+ const top = l.y;
70
+ text('Kontoinhaber', { face: bold, size: 7.5, color: GREY, max: 250 });
71
+ text(st.holder.name, { face: bold, size: 10, max: 250 });
72
+ text(st.holder.address, { size: 9.5, max: 250 });
73
+ const leftEnd = l.y;
74
+ l.y = top;
75
+ const rx = M + 300;
76
+ const facts = [
77
+ // The label column is 72 pt wide: »Verbrauchsauszug« does not fit.
78
+ [st.title === 'Kontoauszug' ? 'Kontoauszug' : 'Auszug-Nr.', st.number],
79
+ ['Zeitraum', `${deDate(st.period.from)} – ${deDate(st.period.to)}`],
80
+ ['Chain', `${st.chainName} (${st.network})`],
81
+ ['Erstellt am', `${deDate(st.issuedAt)}, ${st.issuedAt.slice(11, 16)} UTC`]
82
+ ];
83
+ for (const [k, v] of facts) {
84
+ bold.draw(l.page, k, { x: rx, y: l.y - 8.5, size: 8, color: GREY });
85
+ regular.draw(l.page, v, { x: rx + 72, y: l.y - 8.5, size: 8.5 });
86
+ l.y -= 12;
87
+ }
88
+ l.y = Math.min(l.y, leftEnd) - 14;
89
+ text(st.title, { face: bold, size: 18 });
90
+ text('Konto', { face: bold, size: 7.5, color: GREY });
91
+ text(st.account, { face: mono, size: 9 });
92
+ l.y -= 6;
93
+ text(st.note, { size: 8, color: GREY });
94
+ l.y -= 10;
95
+
96
+ // ---- one section per asset
97
+ for (const s of st.sections) {
98
+ l.ensure(90);
99
+ l.page.drawRectangle({ x: M, y: l.y - 18, width, height: 18, color: BAND });
100
+ bold.draw(l.page, `${s.asset} · ${st.account}`, { x: M + 6, y: l.y - 12.5, size: 9.5 });
101
+ l.y -= 24;
102
+ const head = () => {
103
+ let x = M;
104
+ for (const c of COLS) {
105
+ if (c.right) l.right(c.title, x + c.width - 4, 7.5, bold, GREY);
106
+ else l.at(c.title, x, 7.5, bold, GREY);
107
+ x += c.width;
108
+ }
109
+ l.y -= 11;
110
+ line(l.y);
111
+ l.y -= 3;
112
+ };
113
+ head();
114
+ // opening balance
115
+ l.at(`Kontostand am ${deDate(st.period.from)} (Beginn)`, M + COLS[0].width, 8, bold);
116
+ l.right(qty(s.opening), M + 451 - 4, 8, bold);
117
+ l.y -= 13;
118
+ for (const r of s.rows) {
119
+ const detail = [
120
+ r.counterparty && `${r.debit ? 'an' : 'von'} ${r.counterparty}`,
121
+ r.hash && `${r.hashLabel} ${r.hash.slice(0, 10)}…${r.hash.slice(-6)}`,
122
+ r.beleg && `Eigenbeleg ${r.beleg}`
123
+ ]
124
+ .filter(Boolean)
125
+ .join(' · ');
126
+ const lines = wrapText(r.text, regular, 8, COLS[1].width - 6);
127
+ const sub = wrapText(detail, regular, 6.5, COLS[1].width - 6);
128
+ const h = lines.length * 10 + sub.length * 8.5 + 4;
129
+ if (l.ensure(h)) head();
130
+ const y = l.y;
131
+ regular.draw(l.page, deDate(r.date), { x: M, y: y - 8, size: 8 });
132
+ regular.draw(l.page, r.time, { x: M, y: y - 16.5, size: 6.5, color: GREY });
133
+ lines.forEach((t, i) =>
134
+ regular.draw(l.page, t, { x: M + COLS[0].width, y: y - 8 - i * 10, size: 8 })
135
+ );
136
+ sub.forEach((t, i) =>
137
+ regular.draw(l.page, t, {
138
+ x: M + COLS[0].width,
139
+ y: y - 8 - lines.length * 10 - i * 8.5 + 1.5,
140
+ size: 6.5,
141
+ color: GREY
142
+ })
143
+ );
144
+ let x = M + COLS[0].width + COLS[1].width;
145
+ l.right(qty(r.debit), x + COLS[2].width - 4, 8, regular);
146
+ x += COLS[2].width;
147
+ l.right(qty(r.credit), x + COLS[3].width - 4, 8, regular);
148
+ x += COLS[3].width;
149
+ l.right(qty(r.balance), x + COLS[4].width - 4, 8, regular);
150
+ x += COLS[4].width;
151
+ l.right(
152
+ r.eur ? deEuro(r.eur, 4).replace(' €', '') : '—',
153
+ x + COLS[5].width - 4,
154
+ 8,
155
+ regular,
156
+ r.eur ? BLACK : GREY
157
+ );
158
+ l.y -= h;
159
+ line(l.y + 1, { thickness: 0.25, color: RULE });
160
+ }
161
+ if (s.rows.length === 0) {
162
+ l.at('Keine Buchungen in diesem Zeitraum.', M + COLS[0].width, 8, regular, GREY);
163
+ l.y -= 13;
164
+ }
165
+ // sums and closing balance
166
+ l.ensure(60);
167
+ line(l.y);
168
+ l.y -= 3;
169
+ l.at('Summe', M + COLS[0].width, 8, bold);
170
+ l.right(qty(s.debit), M + 299 - 4, 8, bold);
171
+ l.right(qty(s.credit), M + 371 - 4, 8, bold);
172
+ l.y -= 13;
173
+ l.at(`Kontostand am ${deDate(st.period.to)} (Ende)`, M + COLS[0].width, 9, bold);
174
+ l.right(`${qty(s.closing)} ${s.asset}`, M + 451 - 4, 9, bold);
175
+ l.y -= 15;
176
+ // the check
177
+ if (st.method === 'archive' && s.reported !== null) {
178
+ const ok = s.difference !== null && /^-?0(\.0+)?$/.test(s.difference);
179
+ const when = st.at
180
+ ? ` (Block ${st.at.closing.height}, ${st.at.closing.time.slice(0, 19).replace('T', ' ')} UTC)`
181
+ : '';
182
+ text(
183
+ ok
184
+ ? `Abgestimmt: Der Kontostand laut Chain nach dem Zeitraum${when} ist ${qty(s.reported)} ${s.asset} und stimmt mit Anfangsstand plus Buchungen überein.`
185
+ : `Nicht abgestimmt: Der Kontostand laut Chain${when} ist ${qty(s.reported)} ${s.asset}; Differenz ${deNumber(/** @type {string} */ (s.difference))} ${s.asset} (Bewegungen, die der Leser nicht sieht, etwa Auszahlungen eines Staking-Pools).`,
186
+ { size: 7.5, color: ok ? OK : WARN }
187
+ );
188
+ } else {
189
+ text(
190
+ `Anfangs- und Endstand abgeleitet aus dem heutigen Kontostand abzüglich aller Bewegungen seither (${aleph ? 'die Aleph-API nennt den heutigen Stand und die Summen seither' : 'die Chain hält keine alten Kontostände vor'}).`,
191
+ { size: 7.5, color: GREY }
192
+ );
193
+ }
194
+ l.y -= 4;
195
+ text(
196
+ aleph
197
+ ? `Ausgänge ${deEuro(s.eurOut)} · Eingänge ${deEuro(s.eurIn)} · davon Verbrauch (Aufwand) ${deEuro(s.eurUsage)}${s.unvalued ? ` · ${s.unvalued} Buchung(en) ohne Kurs` : ''} – Euro-Werte: USD-Preis je Credit (letzter Kauf am oder vor dem Tag, sonst Listenpreis) × EZB-Referenzkurs USD/EUR des Tages.`
198
+ : `Ausgänge ${deEuro(s.eurOut)} · Eingänge ${deEuro(s.eurIn)} · davon Netzwerkgebühren (Aufwand) ${deEuro(s.eurFees)}${s.unvalued ? ` · ${s.unvalued} Buchung(en) ohne Kurs` : ''} – Euro-Werte zum Tageskurs (Kurs 00:00 UTC: CoinGecko, sonst Kraken).`,
199
+ { size: 7.5, color: GREY }
200
+ );
201
+ l.y -= 12;
202
+ }
203
+ if (st.unlisted)
204
+ text(
205
+ `${st.unlisted} Bewegung(en) in Token ohne verlässlichen Kurs (oft Spam) sind nur in der CSV-Datei aufgeführt.`,
206
+ { size: 7.5, color: GREY }
207
+ );
208
+
209
+ // page numbers and footer on every page
210
+ const data = `Daten: ${DATA_SOURCES[st.chain] ?? st.chainName} · kein Dokument eines Kreditinstituts`;
211
+ l.pages.forEach((p, i) => {
212
+ regular.draw(p, `${st.number} · Seite ${i + 1} von ${l.pages.length}`, {
213
+ x: M,
214
+ y: M / 2,
215
+ size: 7,
216
+ color: GREY
217
+ });
218
+ regular.draw(p, data, {
219
+ x: A4[0] - M - regular.width(data, 7),
220
+ y: M / 2,
221
+ size: 7,
222
+ color: GREY
223
+ });
224
+ });
225
+
226
+ return savePdfA(pdf, {
227
+ title,
228
+ author: st.holder.name,
229
+ subject,
230
+ creator,
231
+ producer,
232
+ created,
233
+ seed: `${st.number}|${st.issuedAt}|${st.account}`
234
+ });
235
+ }