@le-space/eigenbeleg 0.0.0-stage → 0.1.1
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/LICENSE +662 -0
- package/README.md +18 -2
- package/package.json +49 -3
- package/src/booking-pdf.js +172 -0
- package/src/booking.js +88 -0
- package/src/csv.js +106 -0
- package/src/document.js +357 -0
- package/src/index.js +43 -0
- package/src/layout.js +249 -0
- package/src/pdf.js +286 -0
- package/src/statement-layout.js +164 -0
- package/src/statement-pdf.js +276 -0
- package/src/statement.js +232 -0
- package/src/value.js +74 -0
package/src/document.js
ADDED
|
@@ -0,0 +1,357 @@
|
|
|
1
|
+
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
2
|
+
// What an Eigenbeleg says, before it is drawn (pdf.js) or written (csv.js).
|
|
3
|
+
//
|
|
4
|
+
// An Eigenbeleg is a receipt the payer writes for a payment that has no
|
|
5
|
+
// receipt from the other side. It is never an invoice, and never one written
|
|
6
|
+
// for somebody else: the payer issues it, for their own payments.
|
|
7
|
+
//
|
|
8
|
+
// For a blockchain, the clean case is the network fee: it goes to a
|
|
9
|
+
// peer-to-peer network that has no organisation to issue an invoice. A
|
|
10
|
+
// storage deposit to a protocol contract is the same. A transfer to another
|
|
11
|
+
// account may well be to a company that does invoice; it goes on the
|
|
12
|
+
// Eigenbeleg only when the payer says so (`includeKinds`), and is always in
|
|
13
|
+
// the CSV. Buying Aleph Cloud credits with a token is a payment of its own
|
|
14
|
+
// (kind `swap` on chain `aleph`, issue #23): its Eigenbeleg says *Kauf von
|
|
15
|
+
// Guthaben*; the credits it brought appear on it as the incoming movement.
|
|
16
|
+
//
|
|
17
|
+
// Two layouts, the same content:
|
|
18
|
+
// collective one Eigenbeleg for the period (Sammel-Eigenbeleg), one
|
|
19
|
+
// position per transaction;
|
|
20
|
+
// single one Eigenbeleg per transaction (as Belege writes them), each
|
|
21
|
+
// with its own number, counted up from the first.
|
|
22
|
+
// Every transaction shows its full hash, who paid whom with full addresses,
|
|
23
|
+
// every movement it had (the gas too, and what is not on the Eigenbeleg,
|
|
24
|
+
// with the reason) and its explorer link as a QR code.
|
|
25
|
+
// Numbers come from the payer's own range, since this service stores nothing.
|
|
26
|
+
|
|
27
|
+
import { normalize, round, sum } from '@le-space/ledger';
|
|
28
|
+
|
|
29
|
+
/* The document is German: it is for German bookkeeping. */
|
|
30
|
+
|
|
31
|
+
export const NOTE =
|
|
32
|
+
'Eigenbeleg. Für diese Zahlung gibt es keinen Fremdbeleg: Empfänger ist ein Peer-to-Peer-Netzwerk ohne rechnungsstellende Organisation. ' +
|
|
33
|
+
'Ausgestellt vom Zahlenden auf Grundlage der öffentlichen Transaktion; jede Position ist über ihren Transaktions-Hash in einem öffentlichen Block-Explorer nachprüfbar.';
|
|
34
|
+
|
|
35
|
+
/** Aleph Cloud is an organisation, but the purchase of credits is a token payment on chain that gets no invoice (#23). */
|
|
36
|
+
export const ALEPH_NOTE =
|
|
37
|
+
'Eigenbeleg. Für diese Zahlung gibt es keinen Fremdbeleg: Aleph-Cloud-Guthaben (Credits) wird per Token-Zahlung an das Aleph-Netzwerk gekauft; Aleph stellt dafür keine Rechnung aus. ' +
|
|
38
|
+
'Ausgestellt vom Zahlenden auf Grundlage der öffentlichen Transaktion; jede Position ist über ihren Transaktions-Hash in einem öffentlichen Block-Explorer nachprüfbar.';
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Why there is no invoice for a payment on this chain: the recipient line and the reason, as the Eigenbeleg prints them.
|
|
42
|
+
*
|
|
43
|
+
* @param {string} chain
|
|
44
|
+
* @param {string} chainName
|
|
45
|
+
*/
|
|
46
|
+
export function noInvoice(chain, chainName) {
|
|
47
|
+
if (chain === 'aleph')
|
|
48
|
+
return {
|
|
49
|
+
recipient: `${chainName}: Kauf von Guthaben per Token-Zahlung an das Netzwerk, ohne Rechnung`,
|
|
50
|
+
why: 'Aleph-Cloud-Guthaben (Credits) wird per Token-Zahlung an das Aleph-Netzwerk gekauft; Aleph stellt dafür keine Rechnung aus.',
|
|
51
|
+
note: ALEPH_NOTE
|
|
52
|
+
};
|
|
53
|
+
return {
|
|
54
|
+
recipient: `${chainName}: Peer-to-Peer-Netzwerk ohne rechnungsstellende Organisation`,
|
|
55
|
+
why: 'Der Empfänger ist ein Peer-to-Peer-Netzwerk ohne rechnungsstellende Organisation; es stellt keine Rechnung aus.',
|
|
56
|
+
note: NOTE
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export const NOT_AN_INVOICE =
|
|
61
|
+
'Dies ist keine Rechnung und kein Beleg eines Dritten. Le Space stellt keine Rechnungen für andere aus.';
|
|
62
|
+
|
|
63
|
+
/** Kinds that go on the Eigenbeleg unless the payer chooses otherwise. */
|
|
64
|
+
export const DEFAULT_KINDS = ['fee', 'storage'];
|
|
65
|
+
|
|
66
|
+
export const KIND_TEXT = {
|
|
67
|
+
fee: 'Netzwerkgebühr (Gas)',
|
|
68
|
+
storage: 'Speicherhinterlegung (Storage Deposit)',
|
|
69
|
+
transfer: 'Überweisung',
|
|
70
|
+
call: 'Zahlung an Contract',
|
|
71
|
+
stake: 'Staking (keine Zahlung)',
|
|
72
|
+
unstake: 'Unstaking',
|
|
73
|
+
reward: 'Staking-Ertrag',
|
|
74
|
+
swap: 'Tausch',
|
|
75
|
+
escrow: 'Hinterlegung für ein Deployment (Escrow)',
|
|
76
|
+
usage: 'Verbrauch (aus Guthaben abgerechnet)'
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* What an entry's kind is called on the documents. An Aleph credit purchase is a swap
|
|
81
|
+
* of a token for credits in the ledger; for the books it is the purchase of a balance.
|
|
82
|
+
*
|
|
83
|
+
* @param {{ chain: string, kind: string }} e
|
|
84
|
+
*/
|
|
85
|
+
export const kindText = (e) =>
|
|
86
|
+
e.chain === 'aleph' && e.kind === 'swap'
|
|
87
|
+
? 'Kauf von Guthaben (Credits)'
|
|
88
|
+
: (KIND_TEXT[/** @type {keyof typeof KIND_TEXT} */ (e.kind)] ?? e.kind);
|
|
89
|
+
|
|
90
|
+
/** The chains Eigenbeleg makes documents for, by id (as @le-space/chains names them). */
|
|
91
|
+
export const CHAIN_NAMES = {
|
|
92
|
+
near: 'NEAR Protocol',
|
|
93
|
+
akash: 'Akash',
|
|
94
|
+
filecoin: 'Filecoin',
|
|
95
|
+
aleph: 'Aleph Cloud (Credits)',
|
|
96
|
+
ethereum: 'Ethereum',
|
|
97
|
+
base: 'Base'
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Where a chain's data comes from, named in the footer of its documents.
|
|
102
|
+
* Ethereum and Base: Eigenbeleg reads them through Alchemy (@le-space/chains'
|
|
103
|
+
* EVM reader, with the server's key).
|
|
104
|
+
*/
|
|
105
|
+
export const DATA_SOURCES = /** @type {Record<string, string>} */ ({
|
|
106
|
+
near: 'NearBlocks.io APIs, NEAR-Archiv-RPC',
|
|
107
|
+
filecoin: 'Filfox',
|
|
108
|
+
akash: 'Akash-RPC, Akash Console',
|
|
109
|
+
aleph: 'Aleph-API (api2.aleph.im)',
|
|
110
|
+
ethereum: 'Alchemy APIs (Ethereum Mainnet)',
|
|
111
|
+
base: 'Alchemy APIs (Base Mainnet)'
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* @typedef {object} Payer who pays and issues the Eigenbeleg
|
|
116
|
+
* @property {string} name
|
|
117
|
+
* @property {string} address postal address, lines separated by \n
|
|
118
|
+
* @property {string[]} addresses own payment addresses on the chain
|
|
119
|
+
*/
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* @typedef {object} Position
|
|
123
|
+
* @property {number} pos
|
|
124
|
+
* @property {import('./value.js').ValuedEntry} entry
|
|
125
|
+
* @property {string} what
|
|
126
|
+
* @property {string} recipient
|
|
127
|
+
*/
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* @typedef {object} Movement one movement of a transaction
|
|
131
|
+
* @property {import('./value.js').ValuedEntry} entry
|
|
132
|
+
* @property {string} what
|
|
133
|
+
* @property {boolean} onIt on the Eigenbeleg
|
|
134
|
+
* @property {string} why why not, when not
|
|
135
|
+
*/
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* @typedef {object} Transaction one position: a transaction with a payment on the Eigenbeleg
|
|
139
|
+
* @property {number} pos
|
|
140
|
+
* @property {string} number its own number in the single layout ('' in the collective one)
|
|
141
|
+
* @property {string} hash
|
|
142
|
+
* @property {string} time
|
|
143
|
+
* @property {string} explorerUrl
|
|
144
|
+
* @property {{ address: string, label: string }} from the own address that paid
|
|
145
|
+
* @property {{ address: string, label: string }} to who received the payment
|
|
146
|
+
* @property {Movement[]} movements every movement of the transaction the period has
|
|
147
|
+
* @property {string} eurExact what is on the Eigenbeleg, in EUR, positive
|
|
148
|
+
*/
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* @param {object} p
|
|
152
|
+
* @param {Payer} p.payer
|
|
153
|
+
* @param {string} p.chain
|
|
154
|
+
* @param {string} p.network
|
|
155
|
+
* @param {{ from: string, to: string }} p.period
|
|
156
|
+
* @param {import('./value.js').ValuedEntry[]} p.entries every entry of the period, all own addresses
|
|
157
|
+
* @param {string} [p.number] the Eigenbeleg's number in the payer's range, e.g. EB-2026-007
|
|
158
|
+
* @param {string[]} [p.includeKinds]
|
|
159
|
+
* @param {string} [p.issuedAt] ISO; now when not given
|
|
160
|
+
* @param {'collective' | 'single'} [p.layout]
|
|
161
|
+
*/
|
|
162
|
+
export function buildDocument({
|
|
163
|
+
payer,
|
|
164
|
+
chain,
|
|
165
|
+
network,
|
|
166
|
+
period,
|
|
167
|
+
entries,
|
|
168
|
+
number,
|
|
169
|
+
includeKinds = DEFAULT_KINDS,
|
|
170
|
+
issuedAt,
|
|
171
|
+
layout = 'collective'
|
|
172
|
+
}) {
|
|
173
|
+
if (layout !== 'collective' && layout !== 'single')
|
|
174
|
+
throw new Error('layout: collective or single');
|
|
175
|
+
checkPayer(payer);
|
|
176
|
+
const kinds = new Set(includeKinds);
|
|
177
|
+
const own = new Set(payer.addresses);
|
|
178
|
+
/** @type {Position[]} */
|
|
179
|
+
const positions = [];
|
|
180
|
+
/** @type {{ entry: import('./value.js').ValuedEntry, why: string }[]} */
|
|
181
|
+
const excluded = [];
|
|
182
|
+
for (const e of entries) {
|
|
183
|
+
const why =
|
|
184
|
+
e.type === 'received'
|
|
185
|
+
? 'Eingang, keine Zahlung'
|
|
186
|
+
: !e.success
|
|
187
|
+
? 'fehlgeschlagen, nichts gezahlt'
|
|
188
|
+
: own.has(e.counterparty)
|
|
189
|
+
? 'Umbuchung zwischen eigenen Adressen'
|
|
190
|
+
: !kinds.has(e.kind)
|
|
191
|
+
? e.kind === 'transfer' || e.kind === 'call'
|
|
192
|
+
? 'Zahlung an ein Konto: Fremdbeleg anfordern oder bewusst einbeziehen'
|
|
193
|
+
: e.kind === 'escrow'
|
|
194
|
+
? 'Hinterlegung (Escrow), noch kein Endbetrag'
|
|
195
|
+
: e.kind === 'usage'
|
|
196
|
+
? 'Verbrauch aus Guthaben: steht im Verbrauchsauszug'
|
|
197
|
+
: 'keine Zahlung an das Netzwerk'
|
|
198
|
+
: !e.eur
|
|
199
|
+
? 'kein verlässlicher Kurs (unbekannter Token)'
|
|
200
|
+
: '';
|
|
201
|
+
if (why) excluded.push({ entry: e, why });
|
|
202
|
+
else
|
|
203
|
+
positions.push({
|
|
204
|
+
pos: 0,
|
|
205
|
+
entry: e,
|
|
206
|
+
what: kindText(e),
|
|
207
|
+
recipient: e.counterpartyLabel || e.counterparty
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
const first = number || `EB-${period.to.slice(0, 4)}-001`;
|
|
211
|
+
const chainName = CHAIN_NAMES[/** @type {keyof typeof CHAIN_NAMES} */ (chain)] ?? chain;
|
|
212
|
+
/** @type {Transaction[]} */
|
|
213
|
+
const transactions = [];
|
|
214
|
+
for (const p of positions) {
|
|
215
|
+
let t = transactions.find((x) => x.hash === p.entry.hash);
|
|
216
|
+
if (!t) {
|
|
217
|
+
const all = entries.filter((e) => e.hash === p.entry.hash);
|
|
218
|
+
// The payment, not the gas, says to whom; a transaction with only gas paid the network.
|
|
219
|
+
const main = all.find((e) => e.type === 'sent') ?? p.entry;
|
|
220
|
+
t = {
|
|
221
|
+
pos: transactions.length + 1,
|
|
222
|
+
number: layout === 'single' ? nthNumber(first, transactions.length) : '',
|
|
223
|
+
hash: p.entry.hash,
|
|
224
|
+
time: p.entry.time,
|
|
225
|
+
explorerUrl: p.entry.explorerUrl,
|
|
226
|
+
from: { address: p.entry.account, label: 'eigene Adresse' },
|
|
227
|
+
to: { address: main.counterparty, label: main.counterpartyLabel },
|
|
228
|
+
movements: all.map((e) => {
|
|
229
|
+
const x = excluded.find((y) => y.entry === e);
|
|
230
|
+
return {
|
|
231
|
+
entry: e,
|
|
232
|
+
what: kindText(e),
|
|
233
|
+
onIt: !x,
|
|
234
|
+
why: x?.why ?? ''
|
|
235
|
+
};
|
|
236
|
+
}),
|
|
237
|
+
eurExact: '0'
|
|
238
|
+
};
|
|
239
|
+
transactions.push(t);
|
|
240
|
+
}
|
|
241
|
+
p.pos = t.pos;
|
|
242
|
+
}
|
|
243
|
+
for (const t of transactions)
|
|
244
|
+
t.eurExact = sum(
|
|
245
|
+
t.movements.filter((m) => m.onIt).map((m) => /** @type {any} */ (m.entry.eur).value)
|
|
246
|
+
).replace(/^-/, '');
|
|
247
|
+
/** @type {Record<string, string>} */
|
|
248
|
+
const quantities = {};
|
|
249
|
+
for (const asset of new Set(positions.map((p) => p.entry.asset)))
|
|
250
|
+
quantities[asset] = normalize(
|
|
251
|
+
sum(positions.filter((p) => p.entry.asset === asset).map((p) => p.entry.amount)).replace(
|
|
252
|
+
/^-/,
|
|
253
|
+
''
|
|
254
|
+
)
|
|
255
|
+
);
|
|
256
|
+
const eurExact = positions.length
|
|
257
|
+
? sum(positions.map((p) => /** @type {any} */ (p.entry.eur).value))
|
|
258
|
+
: '0';
|
|
259
|
+
return {
|
|
260
|
+
number:
|
|
261
|
+
layout === 'single' && transactions.length > 1
|
|
262
|
+
? `${first} – ${transactions.at(-1)?.number}`
|
|
263
|
+
: first,
|
|
264
|
+
layout,
|
|
265
|
+
transactions,
|
|
266
|
+
issuedAt: issuedAt ?? new Date().toISOString(),
|
|
267
|
+
payer: {
|
|
268
|
+
name: payer.name.trim(),
|
|
269
|
+
address: payer.address.trim(),
|
|
270
|
+
addresses: [...payer.addresses]
|
|
271
|
+
},
|
|
272
|
+
chain,
|
|
273
|
+
chainName,
|
|
274
|
+
network,
|
|
275
|
+
testnet: network !== 'mainnet',
|
|
276
|
+
period,
|
|
277
|
+
positions,
|
|
278
|
+
excluded,
|
|
279
|
+
totals: {
|
|
280
|
+
quantities,
|
|
281
|
+
// What was paid, as a positive amount, rounded to cents.
|
|
282
|
+
eur: round(eurExact.replace(/^-/, ''), 2),
|
|
283
|
+
eurExact: eurExact.replace(/^-/, '')
|
|
284
|
+
},
|
|
285
|
+
// The rate's name without its day (`… vom 2026-08-10`): the day is each position's.
|
|
286
|
+
sources: [
|
|
287
|
+
...new Set(
|
|
288
|
+
positions.map((p) =>
|
|
289
|
+
/** @type {any} */ (p.entry.eur).source
|
|
290
|
+
.replace(/ \d{4}-\d{2}-\d{2}.*$/, '')
|
|
291
|
+
.replace(/ vom$/, '')
|
|
292
|
+
)
|
|
293
|
+
)
|
|
294
|
+
],
|
|
295
|
+
note: noInvoice(chain, chainName).note,
|
|
296
|
+
notInvoice: NOT_AN_INVOICE
|
|
297
|
+
};
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
/** @typedef {ReturnType<typeof buildDocument>} EigenbelegDocument */
|
|
301
|
+
|
|
302
|
+
/**
|
|
303
|
+
* The n-th number counted up from the first: EB-2026-007 → EB-2026-008 …
|
|
304
|
+
* A number without digits at its end gets -1, -2 …
|
|
305
|
+
*
|
|
306
|
+
* @param {string} first @param {number} n
|
|
307
|
+
*/
|
|
308
|
+
export function nthNumber(first, n) {
|
|
309
|
+
if (n === 0) return first;
|
|
310
|
+
const m = /^(.*?)(\d+)$/.exec(first);
|
|
311
|
+
if (!m) return `${first}-${n + 1}`;
|
|
312
|
+
return `${m[1]}${String(Number(m[2]) + n).padStart(m[2].length, '0')}`;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/** @param {Payer} payer */
|
|
316
|
+
function checkPayer(payer) {
|
|
317
|
+
if (!payer || typeof payer !== 'object') throw new Error('payer is required');
|
|
318
|
+
if (typeof payer.name !== 'string' || payer.name.trim().length < 2 || payer.name.length > 200)
|
|
319
|
+
throw new Error('payer.name is required');
|
|
320
|
+
if (
|
|
321
|
+
typeof payer.address !== 'string' ||
|
|
322
|
+
payer.address.trim().length < 5 ||
|
|
323
|
+
payer.address.length > 500
|
|
324
|
+
)
|
|
325
|
+
throw new Error('payer.address (postal) is required');
|
|
326
|
+
if (
|
|
327
|
+
!Array.isArray(payer.addresses) ||
|
|
328
|
+
payer.addresses.length === 0 ||
|
|
329
|
+
payer.addresses.length > 20
|
|
330
|
+
)
|
|
331
|
+
throw new Error('payer.addresses: one to twenty own addresses');
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
// ---- German formats ---------------------------------------------------------
|
|
335
|
+
|
|
336
|
+
/** `-1234.5` → `-1.234,5`; keeps every decimal. @param {string} amount */
|
|
337
|
+
export function deNumber(amount) {
|
|
338
|
+
const s = normalize(String(amount));
|
|
339
|
+
const neg = s.startsWith('-');
|
|
340
|
+
const [int, frac] = s.replace(/^-/, '').split('.');
|
|
341
|
+
const grouped = int.replace(/\B(?=(\d{3})+(?!\d))/g, '.');
|
|
342
|
+
return `${neg ? '−' : ''}${grouped}${frac ? ',' + frac : ''}`;
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
/** A euro amount with fixed decimals: `0.0017` → `0,0017 €`. @param {string} amount @param {number} places */
|
|
346
|
+
export function deEuro(amount, places = 2) {
|
|
347
|
+
const r = round(String(amount), places);
|
|
348
|
+
const [int, frac = ''] = r.replace(/^-/, '').split('.');
|
|
349
|
+
const grouped = int.replace(/\B(?=(\d{3})+(?!\d))/g, '.');
|
|
350
|
+
return `${r.startsWith('-') ? '−' : ''}${grouped},${frac.padEnd(places, '0')} €`;
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
/** `2026-10-05` → `05.10.2026`. @param {string} date */
|
|
354
|
+
export function deDate(date) {
|
|
355
|
+
const [y, m, d] = date.slice(0, 10).split('-');
|
|
356
|
+
return `${d}.${m}.${y}`;
|
|
357
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
2
|
+
// @le-space/eigenbeleg: the Eigenbeleg and the Kontoauszug of Le Space's
|
|
3
|
+
// tools (Belege, the Eigenbeleg API and web UI) – what they say, as PDF/A-2b
|
|
4
|
+
// and CSV. Browser and Node.
|
|
5
|
+
export {
|
|
6
|
+
buildDocument,
|
|
7
|
+
nthNumber,
|
|
8
|
+
NOTE,
|
|
9
|
+
NOT_AN_INVOICE,
|
|
10
|
+
DEFAULT_KINDS,
|
|
11
|
+
KIND_TEXT,
|
|
12
|
+
kindText,
|
|
13
|
+
ALEPH_NOTE,
|
|
14
|
+
noInvoice,
|
|
15
|
+
CHAIN_NAMES,
|
|
16
|
+
DATA_SOURCES,
|
|
17
|
+
deNumber,
|
|
18
|
+
deEuro,
|
|
19
|
+
deDate
|
|
20
|
+
} from './document.js';
|
|
21
|
+
export { eigenbelegCsv } from './csv.js';
|
|
22
|
+
export { eigenbelegPdf, CREATOR, PRODUCER } from './pdf.js';
|
|
23
|
+
export {
|
|
24
|
+
buildStatement,
|
|
25
|
+
isDust,
|
|
26
|
+
monthly,
|
|
27
|
+
reconciled,
|
|
28
|
+
STATEMENT_NOTE,
|
|
29
|
+
USAGE_NOTE
|
|
30
|
+
} from './statement.js';
|
|
31
|
+
/** @typedef {import('./statement.js').MonthlyLine} MonthlyLine */
|
|
32
|
+
export { statementPdf } from './statement-pdf.js';
|
|
33
|
+
export { valueEntries, fixedRates } from './value.js';
|
|
34
|
+
export { nextSelfNumber, bookingNote, bookingNotInvoice } from './booking.js';
|
|
35
|
+
export { bookingPdf } from './booking-pdf.js';
|
|
36
|
+
export { statementLayout } from './statement-layout.js';
|
|
37
|
+
/** @typedef {import('./value.js').Rate} Rate */
|
|
38
|
+
/** @typedef {import('./value.js').Rates} Rates */
|
|
39
|
+
/** @typedef {import('./value.js').ValuedEntry} ValuedEntry */
|
|
40
|
+
/** @typedef {import('./document.js').EigenbelegDocument} EigenbelegDocument */
|
|
41
|
+
/** @typedef {import('./statement.js').Statement} Statement */
|
|
42
|
+
/** @typedef {import('./booking.js').BookingEigenbeleg} BookingEigenbeleg */
|
|
43
|
+
/** @typedef {import('./booking.js').ChainDetails} ChainDetails */
|
package/src/layout.js
ADDED
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
2
|
+
// Eigenbeleg's look, shared by every document this package draws (the
|
|
3
|
+
// Eigenbeleg of a period, the Kontoauszug, the Eigenbeleg of one booking, a
|
|
4
|
+
// statement Eigenbeleg): A4, a 48 pt margin, grey labels beside their values,
|
|
5
|
+
// thin rules, a tinted box for the note, tables whose header repeats on a
|
|
6
|
+
// new page, a QR code of the explorer link. Text in any script the fonts
|
|
7
|
+
// have (@le-space/pdf); pages as many as the content needs.
|
|
8
|
+
//
|
|
9
|
+
// The drawing helpers of Eigenbeleg's pdf.js and statement-pdf.js (from
|
|
10
|
+
// Le-Space/eigenbeleg-agent at e6cde90), on faces instead of pdf-lib fonts.
|
|
11
|
+
|
|
12
|
+
import { rgb } from 'pdf-lib';
|
|
13
|
+
import { encode } from 'uqr';
|
|
14
|
+
import { pdfText, wrapText } from '@le-space/pdf';
|
|
15
|
+
|
|
16
|
+
/** @typedef {import('@le-space/pdf').Face} Face */
|
|
17
|
+
/** @typedef {{ regular: Face, bold: Face, mono: Face }} Fonts */
|
|
18
|
+
/** @typedef {import('pdf-lib').Color} Color */
|
|
19
|
+
|
|
20
|
+
export const A4 = /** @type {[number, number]} */ ([595.28, 841.89]);
|
|
21
|
+
export const BLACK = rgb(0, 0, 0);
|
|
22
|
+
export const GREY = rgb(0.38, 0.38, 0.38);
|
|
23
|
+
export const RULE = rgb(0.78, 0.78, 0.78);
|
|
24
|
+
export const TINT = rgb(0.95, 0.95, 0.93);
|
|
25
|
+
export const BAND = rgb(0.93, 0.94, 0.95);
|
|
26
|
+
export const WARN = rgb(0.6, 0.12, 0.08);
|
|
27
|
+
export const OK = rgb(0.1, 0.42, 0.2);
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* @typedef {object} Column
|
|
31
|
+
* @property {string} title
|
|
32
|
+
* @property {number} width
|
|
33
|
+
* @property {boolean} [right] aligned right, as numbers are
|
|
34
|
+
* @property {boolean} [mono]
|
|
35
|
+
*/
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* A layout on a new A4 page of `pdf`.
|
|
39
|
+
*
|
|
40
|
+
* @param {import('pdf-lib').PDFDocument} pdf
|
|
41
|
+
* @param {Fonts} fonts
|
|
42
|
+
* @param {object} [o]
|
|
43
|
+
* @param {number} [o.margin]
|
|
44
|
+
* @param {number} [o.bottom] room kept free above the bottom margin, for the footer
|
|
45
|
+
* @param {(l: Layout) => void} [o.leavePage] before a new page: the old one's footer
|
|
46
|
+
* @param {(l: Layout) => void} [o.enterPage] on a new page: its running head
|
|
47
|
+
*/
|
|
48
|
+
export function pageLayout(pdf, fonts, { margin = 48, bottom = 14, leavePage, enterPage } = {}) {
|
|
49
|
+
const { regular } = fonts;
|
|
50
|
+
const l = {
|
|
51
|
+
pdf,
|
|
52
|
+
fonts,
|
|
53
|
+
margin,
|
|
54
|
+
width: A4[0] - 2 * margin,
|
|
55
|
+
page: pdf.addPage(A4),
|
|
56
|
+
y: A4[1] - margin,
|
|
57
|
+
/** @type {import('pdf-lib').PDFPage[]} */
|
|
58
|
+
pages: /** @type {import('pdf-lib').PDFPage[]} */ ([]),
|
|
59
|
+
|
|
60
|
+
newPage() {
|
|
61
|
+
leavePage?.(l);
|
|
62
|
+
l.page = pdf.addPage(A4);
|
|
63
|
+
l.pages.push(l.page);
|
|
64
|
+
l.y = A4[1] - margin;
|
|
65
|
+
enterPage?.(l);
|
|
66
|
+
},
|
|
67
|
+
|
|
68
|
+
/** A new page when fewer than `h` points are left. @param {number} h */
|
|
69
|
+
ensure(h) {
|
|
70
|
+
if (l.y - h >= margin + bottom) return false;
|
|
71
|
+
l.newPage();
|
|
72
|
+
return true;
|
|
73
|
+
},
|
|
74
|
+
|
|
75
|
+
/** @param {number} h */
|
|
76
|
+
gap(h) {
|
|
77
|
+
l.y -= h;
|
|
78
|
+
},
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Wrapped text, a new page where it runs out.
|
|
82
|
+
*
|
|
83
|
+
* @param {string} t
|
|
84
|
+
* @param {{ x?: number, size?: number, face?: Face, color?: Color, max?: number, lead?: number }} [o]
|
|
85
|
+
*/
|
|
86
|
+
text(t, o = {}) {
|
|
87
|
+
const {
|
|
88
|
+
x = margin,
|
|
89
|
+
size = 9.5,
|
|
90
|
+
face = regular,
|
|
91
|
+
color = BLACK,
|
|
92
|
+
max = l.width - (x - margin),
|
|
93
|
+
lead = 1.35
|
|
94
|
+
} = o;
|
|
95
|
+
for (const line of wrapText(t, face, size, max)) {
|
|
96
|
+
l.ensure(size * lead);
|
|
97
|
+
face.draw(l.page, line, { x, y: l.y - size, size, color });
|
|
98
|
+
l.y -= size * lead;
|
|
99
|
+
}
|
|
100
|
+
},
|
|
101
|
+
|
|
102
|
+
/** One line, right-aligned at `xRight`. @param {string} t @param {number} xRight @param {number} size @param {Face} face @param {Color} [color] */
|
|
103
|
+
right(t, xRight, size, face, color = BLACK) {
|
|
104
|
+
const c = pdfText(t);
|
|
105
|
+
face.draw(l.page, c, { x: xRight - face.width(c, size), y: l.y - size, size, color });
|
|
106
|
+
},
|
|
107
|
+
|
|
108
|
+
/** One line at `x`, on the current line. @param {string} t @param {number} x @param {number} size @param {Face} face @param {Color} [color] */
|
|
109
|
+
at(t, x, size, face, color = BLACK) {
|
|
110
|
+
face.draw(l.page, pdfText(t), { x, y: l.y - size, size, color });
|
|
111
|
+
},
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* A one-line title and, when given, an image beside it, a little higher
|
|
115
|
+
* than the letters: the chain's mark (`logo`).
|
|
116
|
+
*
|
|
117
|
+
* @param {string} t
|
|
118
|
+
* @param {{ face: Face, size: number }} o
|
|
119
|
+
* @param {import('pdf-lib').PDFImage | null} [image]
|
|
120
|
+
*/
|
|
121
|
+
title(t, { face, size }, image = null) {
|
|
122
|
+
l.text(t, { face, size });
|
|
123
|
+
if (!image) return;
|
|
124
|
+
const top = l.y + size * 1.35;
|
|
125
|
+
l.page.drawImage(image, {
|
|
126
|
+
x: margin + face.width(t, size) + 10,
|
|
127
|
+
y: top - size - 2,
|
|
128
|
+
width: size + 2,
|
|
129
|
+
height: size + 2
|
|
130
|
+
});
|
|
131
|
+
},
|
|
132
|
+
|
|
133
|
+
/** @param {number} [thickness] @param {Color} [color] */
|
|
134
|
+
rule(thickness = 0.6, color = RULE) {
|
|
135
|
+
l.page.drawLine({
|
|
136
|
+
start: { x: margin, y: l.y },
|
|
137
|
+
end: { x: A4[0] - margin, y: l.y },
|
|
138
|
+
thickness,
|
|
139
|
+
color
|
|
140
|
+
});
|
|
141
|
+
},
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* A label and its value side by side.
|
|
145
|
+
*
|
|
146
|
+
* @param {string} label
|
|
147
|
+
* @param {string} value
|
|
148
|
+
* @param {{ face?: Face, size?: number, max?: number, labelWidth?: number }} [o]
|
|
149
|
+
*/
|
|
150
|
+
field(label, value, o = {}) {
|
|
151
|
+
const lw = o.labelWidth ?? 92;
|
|
152
|
+
const size = o.size ?? 8.5;
|
|
153
|
+
const lines = wrapText(value || '—', o.face ?? regular, size, (o.max ?? l.width) - lw);
|
|
154
|
+
l.ensure(Math.min(lines.length, 3) * size * 1.35);
|
|
155
|
+
const top = l.y;
|
|
156
|
+
l.text(label, { face: fonts.bold, size: 8, color: GREY, max: lw - 6 });
|
|
157
|
+
const after = l.y;
|
|
158
|
+
const page = l.page;
|
|
159
|
+
l.y = top;
|
|
160
|
+
l.text(value || '—', {
|
|
161
|
+
x: margin + lw,
|
|
162
|
+
face: o.face ?? regular,
|
|
163
|
+
size,
|
|
164
|
+
max: (o.max ?? l.width) - lw
|
|
165
|
+
});
|
|
166
|
+
// Where the value went on to a new page, the label's end does not count.
|
|
167
|
+
l.y = l.page === page ? Math.min(l.y, after) : l.y;
|
|
168
|
+
},
|
|
169
|
+
|
|
170
|
+
/** A note in a tinted box. @param {string} t @param {number} [size] */
|
|
171
|
+
box(t, size = 9) {
|
|
172
|
+
const lines = wrapText(t, regular, size, l.width - 16);
|
|
173
|
+
const h = lines.length * size * 1.35 + 14;
|
|
174
|
+
l.ensure(h);
|
|
175
|
+
l.page.drawRectangle({
|
|
176
|
+
x: margin,
|
|
177
|
+
y: l.y - h,
|
|
178
|
+
width: l.width,
|
|
179
|
+
height: h,
|
|
180
|
+
color: TINT,
|
|
181
|
+
borderColor: RULE,
|
|
182
|
+
borderWidth: 0.6
|
|
183
|
+
});
|
|
184
|
+
l.y -= 7;
|
|
185
|
+
for (const line of lines) {
|
|
186
|
+
regular.draw(l.page, line, { x: margin + 8, y: l.y - size, size });
|
|
187
|
+
l.y -= size * 1.35;
|
|
188
|
+
}
|
|
189
|
+
l.y -= 7;
|
|
190
|
+
},
|
|
191
|
+
|
|
192
|
+
/** A QR code of `value`, `side` points wide, its top left at (x, top). @param {string} value @param {number} x @param {number} top @param {number} side */
|
|
193
|
+
qr(value, x, top, side) {
|
|
194
|
+
const code = encode(value, { border: 0 });
|
|
195
|
+
const cell = side / code.size;
|
|
196
|
+
code.data.forEach((row, r) => {
|
|
197
|
+
// Runs of dark cells as one rectangle each.
|
|
198
|
+
for (let c = 0; c < row.length; c++) {
|
|
199
|
+
if (!row[c]) continue;
|
|
200
|
+
let n = 1;
|
|
201
|
+
while (row[c + n]) n++;
|
|
202
|
+
l.page.drawRectangle({
|
|
203
|
+
x: x + c * cell,
|
|
204
|
+
y: top - (r + 1) * cell,
|
|
205
|
+
width: n * cell + 0.05,
|
|
206
|
+
height: cell + 0.05,
|
|
207
|
+
color: BLACK
|
|
208
|
+
});
|
|
209
|
+
c += n - 1;
|
|
210
|
+
}
|
|
211
|
+
});
|
|
212
|
+
},
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* One row of a table, its cells wrapped; a new page (and `header` again) where it does not fit.
|
|
216
|
+
*
|
|
217
|
+
* @param {Column[]} columns
|
|
218
|
+
* @param {string[]} cells
|
|
219
|
+
* @param {{ face?: Face, color?: Color, size?: number, lead?: number, header?: () => void }} [o]
|
|
220
|
+
*/
|
|
221
|
+
row(columns, cells, o = {}) {
|
|
222
|
+
const { face = regular, color = BLACK, size = 7.5, lead = 9.5, header } = o;
|
|
223
|
+
const wrapped = columns.map((c, i) =>
|
|
224
|
+
wrapText(cells[i] ?? '', c.mono ? fonts.mono : face, size, c.width - 6)
|
|
225
|
+
);
|
|
226
|
+
const h = Math.max(...wrapped.map((w) => w.length)) * lead + 2;
|
|
227
|
+
if (l.ensure(h)) header?.();
|
|
228
|
+
let x = margin;
|
|
229
|
+
columns.forEach((c, i) => {
|
|
230
|
+
const f = c.mono ? fonts.mono : face;
|
|
231
|
+
wrapped[i].forEach((line, j) => {
|
|
232
|
+
const w = f.width(line, size);
|
|
233
|
+
f.draw(l.page, line, {
|
|
234
|
+
x: c.right ? x + c.width - 6 - w : x,
|
|
235
|
+
y: l.y - size - j * lead,
|
|
236
|
+
size,
|
|
237
|
+
color
|
|
238
|
+
});
|
|
239
|
+
});
|
|
240
|
+
x += c.width;
|
|
241
|
+
});
|
|
242
|
+
l.y -= h;
|
|
243
|
+
}
|
|
244
|
+
};
|
|
245
|
+
l.pages.push(l.page);
|
|
246
|
+
return l;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/** @typedef {ReturnType<typeof pageLayout>} Layout */
|