@le-space/eigenbeleg 0.3.0 → 0.6.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/README.md +8 -1
- package/package.json +4 -4
- package/src/booking-pdf.js +44 -62
- package/src/chart-pdf.js +21 -2
- package/src/csv.js +10 -7
- package/src/document.js +129 -14
- package/src/index.js +13 -2
- package/src/layout.js +230 -43
- package/src/pdf.js +100 -107
- package/src/statement-layout.js +25 -42
- package/src/statement-pdf.js +110 -88
- package/src/statement.js +35 -4
- package/src/value.js +53 -12
package/README.md
CHANGED
|
@@ -6,10 +6,17 @@ An Eigenbeleg is a receipt the payer writes for a payment that has no receipt fr
|
|
|
6
6
|
|
|
7
7
|
- **A period's Eigenbeleg** (Eigenbeleg's): `valueEntries(entries, rates)` values [`@le-space/ledger`](../ledger) entries at the day's rate (`rates.rateFor(asset, day)`, e.g. from [`@le-space/rates`](../rates); `fixedRates` for tests; Aleph credits at their USD price per credit). `buildDocument({ payer, chain, network, period, entries, number, includeKinds, layout })` puts the payments to the network on it (fees, storage deposits; transfers only when included) and says of every other movement why not; `layout: 'collective'` (one Sammel-Eigenbeleg) or `'single'` (one per transaction, numbered up from the first, `nthNumber`). `eigenbelegPdf(doc, { logo })`, `eigenbelegCsv(doc)` (semicolons, decimal commas, BOM; formulas defused).
|
|
8
8
|
- **The Kontoauszug** (Eigenbeleg's): `buildStatement({ payer, account, chain, period, entries, opening, closing, method, belege })` – opening balance, each booking with debit, credit and running balance, closing balance checked against the chain (`reconciled(difference)`: exactly, or but for dust below the twelve decimals the documents print, `isDust`, such as NEAR's 1 yocto deposit of a token transfer); Aleph credits and a payer's balance in Filecoin Pay (chain `filecoin-pay`, the entries of `@le-space/chains/filecoin-pay`'s `filecoinPayEntries`) as a monthly **Verbrauchsauszug**, each with its own note (`USAGE_NOTES`). `statementPdf(st, { logo, chart })`. Above each list of bookings stands a small diagram ([#889](https://github.com/Le-Space/belege/issues/889)), drawn as vectors from [`@le-space/charts`](../charts): the balance over the period on a Kontoauszug, the consumption per resource on a Verbrauchsauszug, per day (per month where the period is longer than a quarter), in the asset's own unit; `chart: false` leaves it out. `statementChart(st, section)` gives its series and its scene, for an API's answer or `svg(scene)`; `drawScene(page, scene, { x, top, fonts })` draws any scene on a PDF page. `logo: true` draws the chain's mark, small, beside the title (NEAR, Akash, Filecoin, Aleph; from `@le-space/pdf`'s `LOGOS`, sources and terms in its `assets/README.md`); off unless asked for.
|
|
9
|
+
- **Another currency than the euro** ([eigenbeleg-agent#72](https://github.com/Le-Space/eigenbeleg-agent/issues/72)): `valueEntries(entries, rates, { currency })`, `buildDocument({ …, currency })` and `buildStatement({ …, currency })` take the document's currency, EUR when none is given – `DOCUMENT_CURRENCIES`: EUR, USD, CHF, GBP, SEK, NOK, DKK, PLN, CZK, HUF, RON, ISK, TRY (through the ECB's reference rate) and RUB (the Bank of Russia's official rate). The rates handed in are in that currency and say so (`{ rate, source, date, currency }`; `@le-space/rates` `rate(asset, date, { currency })`), `source` naming both legs; a value in one currency under a head in another is refused. The documents then say _Kurs CHF_, _Wert CHF_, amounts as `1.234,56 CHF`, sums rounded to the currency's own decimals (ISK: none), and how the value came about (`conversionText`, `usageNote`); Aleph credits in USD are Aleph's own USD price, without a rate. The fields named `eur…` (`entry.eur`, `totals.eur`, `eurOut` …) keep their names and hold the value in the document's currency, with `currency` beside them. An EUR document is the same file as before. `bookingPdf` is Belege's own booking and stays in euro.
|
|
9
10
|
- **The Eigenbeleg of one booking** (Belege's, `@le-space/eigenbeleg/booking`, `/booking-pdf`): `bookingPdf(doc)` for a `BookingEigenbeleg` – a bank booking or a crypto payment, with an own wallet's transaction in full (whole hash, both addresses, every movement with the gas, the explorer link as a QR code). The booking's context is the caller's to gather. `nextSelfNumber(receipts, year)` – the range EB-YYYY-NNN.
|
|
10
11
|
- **A statement Eigenbeleg** (Belege's Aleph, Akash and travel statements, `@le-space/eigenbeleg/statement-layout`): `statementLayout({ title, created, continuation })` → `head`, `field`, `rule`, `heading`, `table` (the header repeats on a new page), `signature`, `save`.
|
|
11
12
|
|
|
12
|
-
Every PDF has Eigenbeleg's look (`layout.js
|
|
13
|
+
Every PDF has Eigenbeleg's look (`layout.js`, [#951](https://github.com/Le-Space/belege/issues/951)): A4 with 48 pt margins, the title first and a strong rule under the head, small grey labels, rules in two weights and no fills or boxes, a total between two rules, colour for a warning only, page numbers at the foot, a line to sign. The sizes, colours and rule weights are tokens (`TYPE`, `INK`, `GREY`, `RULE`, `WARN`, `STROKE`); nothing is set outside them. `creator` names the program in the metadata (`eigenbeleg` for the period's documents, `Belege` for a booking's and a statement's unless given).
|
|
14
|
+
|
|
15
|
+
## The issuer's own logo
|
|
16
|
+
|
|
17
|
+
An Eigenbeleg is issued by the payer, so the logo that belongs on it is the payer's ([#906](https://github.com/Le-Space/belege/issues/906)). `eigenbelegPdf(doc, { issuerLogo })`, `bookingPdf(doc, { issuerLogo })` and `statementLayout({ …, issuerLogo })` take the bytes of a PNG or JPEG file and draw it large, as a letterhead: top right, fitted into about 70 × 25 mm (`ISSUER_LOGO_BOX`, 200 × 70 pt) with its proportions kept, the text below it and not above 45 mm from the top edge (`LETTERHEAD`, the head zone of DIN 5008 form B). Without a logo the page is as before. The chain's mark (`logo: true`) is the network's, not the issuer's: on an Eigenbeleg it stays very small beside the title (`MARK_SIZE.small`); on a Kontoauszug or Verbrauchsauszug, which shows what the account did and is not the issuer's own receipt, it is larger (`MARK_SIZE.large`) and the statement carries no issuer's logo. The package ships no such picture and fetches none: the caller hands it in.
|
|
18
|
+
|
|
19
|
+
The file goes through [`@le-space/pdf`](../pdf)'s `pdfImage` first: at most 512 kB and 2048 pixels a side, no wider than six times its height and no taller than twice its width (`IMAGE_LIMITS`); a PNG is laid on white and written again without transparency, text chunks or a colour profile, a JPEG loses EXIF, comments and its other application segments. SVG is refused, as is a JPEG in CMYK. A file that does not pass is an `ImageError` with a sentence for the person, never a document without its logo.
|
|
13
20
|
|
|
14
21
|
```bash
|
|
15
22
|
pnpm --filter @le-space/eigenbeleg test # Node, and every document byte for byte in Chromium
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@le-space/eigenbeleg",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"description": "The Eigenbeleg and the Kontoauszug of Le Space's tools: a payer's own receipt for payments without one from the other side (a period's network fees, one booking in any script), a blockchain account's statement (Aleph credits as a Verbrauchsauszug), as PDF/A-2b and CSV. Browser and Node; the rates handed in.",
|
|
5
5
|
"license": "AGPL-3.0-or-later",
|
|
6
6
|
"type": "module",
|
|
@@ -36,16 +36,16 @@
|
|
|
36
36
|
"dependencies": {
|
|
37
37
|
"pdf-lib": "1.17.1",
|
|
38
38
|
"uqr": "0.1.3",
|
|
39
|
-
"@le-space/charts": "^0.
|
|
39
|
+
"@le-space/charts": "^0.2.0",
|
|
40
40
|
"@le-space/ledger": "^0.3.0",
|
|
41
|
-
"@le-space/pdf": "^0.
|
|
41
|
+
"@le-space/pdf": "^0.2.0"
|
|
42
42
|
},
|
|
43
43
|
"devDependencies": {
|
|
44
44
|
"@noble/hashes": "2.4.0",
|
|
45
45
|
"prettier": "3.6.2",
|
|
46
46
|
"unpdf": "1.8.1",
|
|
47
47
|
"@le-space/browser-test": "0.1.0",
|
|
48
|
-
"@le-space/chains": "^0.
|
|
48
|
+
"@le-space/chains": "^0.6.1"
|
|
49
49
|
},
|
|
50
50
|
"scripts": {
|
|
51
51
|
"test": "node --test --test-reporter=spec \"test/*.test.js\"",
|
package/src/booking-pdf.js
CHANGED
|
@@ -13,26 +13,24 @@ import { fromUnits } from '@le-space/ledger';
|
|
|
13
13
|
|
|
14
14
|
import { bookingNote, bookingNotInvoice } from './booking.js';
|
|
15
15
|
import { deDate, deEuro, deNumber } from './document.js';
|
|
16
|
-
import { A4, BLACK, GREY, pageLayout } from './layout.js';
|
|
16
|
+
import { A4, BLACK, GREY, TYPE, embedIssuerLogo, pageLayout } from './layout.js';
|
|
17
17
|
|
|
18
18
|
/**
|
|
19
19
|
* @param {import('./booking.js').BookingEigenbeleg} doc
|
|
20
|
-
* @param {{ creator?: string, producer?: string }} [o] what made it: `Belege` when not given
|
|
20
|
+
* @param {{ creator?: string, producer?: string, issuerLogo?: Uint8Array | null }} [o] what made it: `Belege` when not given. issuerLogo: the issuer's own
|
|
21
|
+
* logo, a PNG or JPEG file's bytes, large as a letterhead: top right, the text below it (none unless given; an
|
|
22
|
+
* ImageError of @le-space/pdf when the file is no small PNG or JPEG)
|
|
21
23
|
* @returns {Promise<Uint8Array>}
|
|
22
24
|
*/
|
|
23
|
-
export async function bookingPdf(doc, { creator = 'Belege', producer } = {}) {
|
|
25
|
+
export async function bookingPdf(doc, { creator = 'Belege', producer, issuerLogo = null } = {}) {
|
|
24
26
|
const created = new Date(doc.createdAt);
|
|
25
27
|
const { pdf, fonts } = await createPdfA();
|
|
26
|
-
const {
|
|
28
|
+
const { bold, mono } = fonts;
|
|
29
|
+
const own = await embedIssuerLogo(pdf, issuerLogo);
|
|
27
30
|
const M = 48;
|
|
28
31
|
let pageNo = 1;
|
|
29
32
|
const footer = (/** @type {import('./layout.js').Layout} */ l) =>
|
|
30
|
-
|
|
31
|
-
x: M,
|
|
32
|
-
y: M / 2,
|
|
33
|
-
size: 7,
|
|
34
|
-
color: GREY
|
|
35
|
-
});
|
|
33
|
+
l.foot(l.page, `${doc.number} · Seite ${pageNo}`);
|
|
36
34
|
const l = pageLayout(pdf, fonts, {
|
|
37
35
|
margin: M,
|
|
38
36
|
leavePage: footer,
|
|
@@ -40,36 +38,36 @@ export async function bookingPdf(doc, { creator = 'Belege', producer } = {}) {
|
|
|
40
38
|
pageNo++;
|
|
41
39
|
}
|
|
42
40
|
});
|
|
43
|
-
const { text, gap, field } = l;
|
|
41
|
+
const { text, gap, field, label } = l;
|
|
44
42
|
const width = l.width;
|
|
45
43
|
const out = doc.amountCents < 0;
|
|
46
44
|
|
|
47
45
|
// ---- head
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
46
|
+
l.letterhead(own);
|
|
47
|
+
l.head('Eigenbeleg', {
|
|
48
|
+
number: doc.number,
|
|
49
|
+
subtitle: 'Beleg für eine Zahlung, zu der es keinen Beleg der Gegenseite gibt.'
|
|
50
|
+
});
|
|
53
51
|
const top = l.y;
|
|
54
52
|
const half = width / 2 - 10;
|
|
55
|
-
|
|
56
|
-
text(doc.issuer || '—', { face: bold, size:
|
|
57
|
-
if (doc.issuerAddress) text(doc.issuerAddress, {
|
|
53
|
+
label('Aussteller', { max: half });
|
|
54
|
+
text(doc.issuer || '—', { face: bold, size: TYPE.lead, max: half });
|
|
55
|
+
if (doc.issuerAddress) text(doc.issuerAddress, { max: half });
|
|
58
56
|
const leftEnd = l.y;
|
|
59
57
|
l.y = top;
|
|
60
58
|
const rx = M + width / 2 + 10;
|
|
61
|
-
|
|
59
|
+
label('Ausgestellt am', { x: rx });
|
|
62
60
|
text(`${deDate(doc.createdAt)}, ${doc.createdAt.slice(11, 16)} UTC`, { x: rx });
|
|
63
|
-
gap(
|
|
64
|
-
|
|
61
|
+
gap(5);
|
|
62
|
+
label(out ? 'Empfänger' : 'Zahlende Seite', { x: rx });
|
|
65
63
|
text(doc.counterparty || '—', { x: rx });
|
|
66
64
|
l.y = Math.min(l.y, leftEnd);
|
|
67
65
|
gap(8);
|
|
68
|
-
|
|
69
|
-
text(doc.account || '—'
|
|
70
|
-
gap(10);
|
|
71
|
-
l.box(bookingNote(Boolean(doc.chain)));
|
|
66
|
+
label('Konto');
|
|
67
|
+
text(doc.account || '—');
|
|
72
68
|
gap(12);
|
|
69
|
+
l.note(bookingNote(Boolean(doc.chain)));
|
|
70
|
+
gap(16);
|
|
73
71
|
|
|
74
72
|
// ---- the payment
|
|
75
73
|
field('Datum der Zahlung', deDate(doc.date));
|
|
@@ -86,78 +84,62 @@ export async function bookingPdf(doc, { creator = 'Belege', producer } = {}) {
|
|
|
86
84
|
|
|
87
85
|
if (doc.chain) {
|
|
88
86
|
const c = doc.chain;
|
|
89
|
-
const side =
|
|
90
|
-
const textMax = width - side -
|
|
87
|
+
const side = 72;
|
|
88
|
+
const textMax = width - side - 16;
|
|
91
89
|
l.ensure(150);
|
|
92
90
|
l.rule();
|
|
93
|
-
gap(
|
|
91
|
+
gap(10);
|
|
94
92
|
const qrTop = l.y;
|
|
95
93
|
text(`Zahlung auf ${c.chain}${c.at ? ` · ${deDate(c.at)} ${c.at.slice(11, 19)} UTC` : ''}`, {
|
|
96
94
|
face: bold,
|
|
97
|
-
size:
|
|
95
|
+
size: TYPE.lead,
|
|
98
96
|
max: textMax
|
|
99
97
|
});
|
|
100
|
-
gap(
|
|
98
|
+
gap(3);
|
|
101
99
|
/** @param {import('./booking.js').Party} p */
|
|
102
100
|
const party = (p) => {
|
|
103
101
|
const note = [p.label, p.own ? 'eigene Wallet' : ''].filter(Boolean).join(', ');
|
|
104
102
|
if (!p.address) return note || '—';
|
|
105
103
|
return note && p.address !== p.label ? `${p.address} (${note})` : p.address;
|
|
106
104
|
};
|
|
107
|
-
|
|
108
|
-
field('
|
|
109
|
-
field('
|
|
105
|
+
const hash = { face: mono, size: TYPE.small, max: textMax, labelWidth: 76 };
|
|
106
|
+
field('Transaktion', c.hash, hash);
|
|
107
|
+
field('Von', party(c.from), hash);
|
|
108
|
+
field('An', party(c.to), hash);
|
|
110
109
|
if (c.explorerUrl) {
|
|
111
|
-
field('Block-Explorer', c.explorerUrl, {
|
|
110
|
+
field('Block-Explorer', c.explorerUrl, { ...hash, size: TYPE.label });
|
|
112
111
|
l.qr(c.explorerUrl, A4[0] - M - side, qrTop, side);
|
|
113
|
-
l.y = Math.min(l.y, qrTop - side
|
|
112
|
+
l.y = Math.min(l.y, qrTop - side);
|
|
114
113
|
}
|
|
115
|
-
gap(
|
|
114
|
+
gap(10);
|
|
116
115
|
const columns = [
|
|
117
116
|
{ title: 'Bewegung', width: 170 },
|
|
118
117
|
{ title: 'Menge', width: 150, right: true },
|
|
119
118
|
{ title: 'Wert EUR', width: 90, right: true },
|
|
120
119
|
{ title: 'Gebucht', width: width - 410 }
|
|
121
120
|
];
|
|
122
|
-
const header = () =>
|
|
123
|
-
l.row(
|
|
124
|
-
columns,
|
|
125
|
-
columns.map((x) => x.title),
|
|
126
|
-
{ face: bold, color: GREY }
|
|
127
|
-
);
|
|
121
|
+
const header = () => l.tableHead(columns);
|
|
128
122
|
header();
|
|
129
123
|
for (const m of c.movements)
|
|
130
124
|
l.row(columns, [m.what, m.quantity || '—', m.euro || '—', m.booked ? 'ja' : 'nein'], {
|
|
131
125
|
color: m.booked ? BLACK : GREY,
|
|
132
126
|
header
|
|
133
127
|
});
|
|
134
|
-
gap(
|
|
135
|
-
} else if (doc.txRef) field('Referenz', doc.txRef, { face: mono, size:
|
|
128
|
+
gap(12);
|
|
129
|
+
} else if (doc.txRef) field('Referenz', doc.txRef, { face: mono, size: TYPE.small });
|
|
136
130
|
|
|
137
131
|
// ---- the amount, what and why
|
|
138
|
-
l.
|
|
139
|
-
l.rule();
|
|
140
|
-
gap(8);
|
|
141
|
-
l.at('Betrag', M, 10, bold);
|
|
142
|
-
l.right(deEuro(fromUnits(String(doc.amountCents), 2), 2), A4[0] - M, 11, bold);
|
|
143
|
-
l.y -= 18;
|
|
132
|
+
l.total('Betrag', deEuro(fromUnits(String(doc.amountCents), 2), 2));
|
|
144
133
|
field('Was wurde bezahlt', doc.description);
|
|
134
|
+
gap(2);
|
|
145
135
|
field('Warum kein Fremdbeleg', doc.reason);
|
|
136
|
+
gap(2);
|
|
146
137
|
field('Erstellt', `${deDate(doc.createdAt)}${doc.createdBy ? ` von ${doc.createdBy}` : ''}`);
|
|
147
138
|
|
|
148
139
|
// ---- what it is not, and the line to sign
|
|
149
140
|
gap(8);
|
|
150
|
-
text(bookingNotInvoice(creator), { size:
|
|
151
|
-
|
|
152
|
-
l.ensure(40);
|
|
153
|
-
l.page.drawLine({
|
|
154
|
-
start: { x: M, y: l.y },
|
|
155
|
-
end: { x: M + 230, y: l.y },
|
|
156
|
-
thickness: 0.6,
|
|
157
|
-
color: BLACK
|
|
158
|
-
});
|
|
159
|
-
l.y -= 4;
|
|
160
|
-
text('Ort, Datum, Unterschrift', { size: 8, color: GREY });
|
|
141
|
+
text(bookingNotInvoice(creator), { size: TYPE.table, color: GREY });
|
|
142
|
+
l.signature('Ort, Datum, Unterschrift');
|
|
161
143
|
footer(l);
|
|
162
144
|
|
|
163
145
|
return savePdfA(pdf, {
|
package/src/chart-pdf.js
CHANGED
|
@@ -18,9 +18,28 @@ const color = (hex) =>
|
|
|
18
18
|
parseInt(hex.slice(5, 7), 16) / 255
|
|
19
19
|
);
|
|
20
20
|
|
|
21
|
-
/**
|
|
21
|
+
/**
|
|
22
|
+
* A document's own tones (#951): the diagram is drawn in the ink of the page,
|
|
23
|
+
* its series a ramp from ink to light that is told apart by lightness alone,
|
|
24
|
+
* so it reads on a black-and-white print and with any colour vision. The
|
|
25
|
+
* roles are the chart's; a role this map does not name keeps the chart's
|
|
26
|
+
* light colour.
|
|
27
|
+
*/
|
|
28
|
+
const DOCUMENT = /** @type {Partial<Record<Role, string>>} */ ({
|
|
29
|
+
text: '#17191f',
|
|
30
|
+
muted: '#666b75',
|
|
31
|
+
grid: '#e3e5e9',
|
|
32
|
+
axis: '#9a9fa8',
|
|
33
|
+
s1: '#1f3350',
|
|
34
|
+
s2: '#6d87a8',
|
|
35
|
+
s3: '#b3c1d3',
|
|
36
|
+
s4: '#3f5f8a',
|
|
37
|
+
other: '#9a9fa8'
|
|
38
|
+
});
|
|
22
39
|
const COLORS = /** @type {Record<Role, import('pdf-lib').Color>} */ (
|
|
23
|
-
Object.fromEntries(
|
|
40
|
+
Object.fromEntries(
|
|
41
|
+
Object.entries({ ...PALETTE.light, ...DOCUMENT }).map(([role, hex]) => [role, color(hex)])
|
|
42
|
+
)
|
|
24
43
|
);
|
|
25
44
|
|
|
26
45
|
/**
|
package/src/csv.js
CHANGED
|
@@ -8,9 +8,10 @@
|
|
|
8
8
|
// where anyone can write anything.
|
|
9
9
|
|
|
10
10
|
import { round } from '@le-space/ledger';
|
|
11
|
-
import { kindText } from './document.js';
|
|
11
|
+
import { currencyPlaces, kindText } from './document.js';
|
|
12
12
|
|
|
13
|
-
|
|
13
|
+
/** The head; rate and value name the document's currency. @param {string} currency */
|
|
14
|
+
const header = (currency) => [
|
|
14
15
|
'Belegnummer',
|
|
15
16
|
'Position',
|
|
16
17
|
'Datum (UTC)',
|
|
@@ -24,8 +25,8 @@ const HEADER = [
|
|
|
24
25
|
'Asset',
|
|
25
26
|
'Token-Contract',
|
|
26
27
|
'Menge',
|
|
27
|
-
|
|
28
|
-
|
|
28
|
+
`Kurs ${currency}`,
|
|
29
|
+
`Wert ${currency}`,
|
|
29
30
|
'Kursquelle',
|
|
30
31
|
'Transaktions-Hash',
|
|
31
32
|
'Explorer',
|
|
@@ -50,8 +51,10 @@ function cell(v) {
|
|
|
50
51
|
* @returns {string}
|
|
51
52
|
*/
|
|
52
53
|
export function eigenbelegCsv(doc) {
|
|
54
|
+
const currency = doc.currency ?? 'EUR';
|
|
53
55
|
/** @type {unknown[][]} */
|
|
54
|
-
const rows = [
|
|
56
|
+
const rows = [header(currency)];
|
|
57
|
+
const places = currencyPlaces(currency);
|
|
55
58
|
const numberOf = (/** @type {number | string} */ pos) =>
|
|
56
59
|
doc.layout === 'single' ? (doc.transactions[Number(pos) - 1]?.number ?? '') : doc.number;
|
|
57
60
|
/** @param {import('./value.js').ValuedEntry} e @param {string | number} pos @param {boolean} on @param {string} why */
|
|
@@ -95,8 +98,8 @@ export function eigenbelegCsv(doc) {
|
|
|
95
98
|
'',
|
|
96
99
|
'',
|
|
97
100
|
'',
|
|
98
|
-
num(round(`-${doc.totals.eurExact}`,
|
|
99
|
-
`gerundet auf
|
|
101
|
+
num(round(`-${doc.totals.eurExact}`, places)),
|
|
102
|
+
`${currency === 'EUR' ? 'gerundet auf Cent' : places ? `gerundet auf ${places} Nachkommastellen` : 'gerundet auf ganze Beträge'}, ungerundet ${doc.totals.eurExact === '0' ? '0' : '-' + num(doc.totals.eurExact)}`,
|
|
100
103
|
'',
|
|
101
104
|
'',
|
|
102
105
|
'ja',
|
package/src/document.js
CHANGED
|
@@ -60,6 +60,86 @@ export function noInvoice(chain, chainName) {
|
|
|
60
60
|
export const NOT_AN_INVOICE =
|
|
61
61
|
'Dies ist keine Rechnung und kein Beleg eines Dritten. Le Space stellt keine Rechnungen für andere aus.';
|
|
62
62
|
|
|
63
|
+
/**
|
|
64
|
+
* The currencies a document can be kept in (Le-Space/eigenbeleg-agent#72),
|
|
65
|
+
* each with the decimals its totals are rounded to (ISO 4217) and where its
|
|
66
|
+
* rate against the euro comes from: the ECB's reference rate, or the Bank of
|
|
67
|
+
* Russia's official rate. EUR is the default, and what every document was in
|
|
68
|
+
* before. The caller's rates must be in the document's currency
|
|
69
|
+
* (`valueEntries(entries, rates, { currency })`; @le-space/rates
|
|
70
|
+
* `rate(asset, date, { currency })`).
|
|
71
|
+
*/
|
|
72
|
+
export const DOCUMENT_CURRENCIES = Object.freeze(
|
|
73
|
+
/** @type {Record<string, { places: number, via: 'ecb' | 'cbr' | null }>} */ ({
|
|
74
|
+
EUR: { places: 2, via: null },
|
|
75
|
+
USD: { places: 2, via: 'ecb' },
|
|
76
|
+
CHF: { places: 2, via: 'ecb' },
|
|
77
|
+
GBP: { places: 2, via: 'ecb' },
|
|
78
|
+
SEK: { places: 2, via: 'ecb' },
|
|
79
|
+
NOK: { places: 2, via: 'ecb' },
|
|
80
|
+
DKK: { places: 2, via: 'ecb' },
|
|
81
|
+
PLN: { places: 2, via: 'ecb' },
|
|
82
|
+
CZK: { places: 2, via: 'ecb' },
|
|
83
|
+
HUF: { places: 2, via: 'ecb' },
|
|
84
|
+
RON: { places: 2, via: 'ecb' },
|
|
85
|
+
ISK: { places: 0, via: 'ecb' },
|
|
86
|
+
TRY: { places: 2, via: 'ecb' },
|
|
87
|
+
RUB: { places: 2, via: 'cbr' }
|
|
88
|
+
})
|
|
89
|
+
);
|
|
90
|
+
|
|
91
|
+
/** The currency of a document, checked: EUR when none is given. @param {unknown} [currency] */
|
|
92
|
+
export function documentCurrency(currency = 'EUR') {
|
|
93
|
+
if (typeof currency !== 'string' || !Object.hasOwn(DOCUMENT_CURRENCIES, currency))
|
|
94
|
+
throw new Error(`currency: one of ${Object.keys(DOCUMENT_CURRENCIES).join(', ')}`);
|
|
95
|
+
return currency;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** The decimals a currency's totals are rounded to. @param {string} currency */
|
|
99
|
+
export const currencyPlaces = (currency) => DOCUMENT_CURRENCIES[documentCurrency(currency)].places;
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* How a value in euro became one in the document's currency, as the documents
|
|
103
|
+
* say it after naming the euro rate; '' for EUR. The rate is named for what it
|
|
104
|
+
* is: the ECB's reference rate, or the Bank of Russia's official rate.
|
|
105
|
+
*
|
|
106
|
+
* @param {string} currency
|
|
107
|
+
*/
|
|
108
|
+
export function conversionText(currency) {
|
|
109
|
+
const ecb = 'zum Euro-Referenzkurs der EZB des Tages oder des letzten davor';
|
|
110
|
+
if (documentCurrency(currency) === 'EUR') return '';
|
|
111
|
+
if (DOCUMENT_CURRENCIES[currency].via === 'cbr')
|
|
112
|
+
return `über den Euro in ${currency} umgerechnet zum amtlichen Kurs der Bank von Russland, gültig am Tag (bis 01.03.2022 ${ecb})`;
|
|
113
|
+
return `über den Euro in ${currency} umgerechnet ${ecb}`;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* The currency an entry was valued in, and that it is the document's: a value
|
|
118
|
+
* in francs under a head that says EUR would be a wrong booking.
|
|
119
|
+
*
|
|
120
|
+
* @param {import('./value.js').ValuedEntry[]} entries
|
|
121
|
+
* @param {string} currency
|
|
122
|
+
*/
|
|
123
|
+
export function checkValued(entries, currency) {
|
|
124
|
+
for (const e of entries) {
|
|
125
|
+
const valued = e.eur ? (e.eur.currency ?? 'EUR') : currency;
|
|
126
|
+
if (valued !== currency)
|
|
127
|
+
throw new Error(`an entry is valued in ${valued}, the document is in ${currency}`);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* A rate's name without its day (`… vom 2026-08-10`), each leg of it: the day
|
|
133
|
+
* is each position's.
|
|
134
|
+
*
|
|
135
|
+
* @param {string} source
|
|
136
|
+
*/
|
|
137
|
+
export const sourceName = (source) =>
|
|
138
|
+
source
|
|
139
|
+
.split(' × ')
|
|
140
|
+
.map((leg) => leg.replace(/ \d{4}-\d{2}-\d{2}.*$/, '').replace(/ vom$/, ''))
|
|
141
|
+
.join(' × ');
|
|
142
|
+
|
|
63
143
|
/** Kinds that go on the Eigenbeleg unless the payer chooses otherwise. */
|
|
64
144
|
export const DEFAULT_KINDS = ['fee', 'storage'];
|
|
65
145
|
|
|
@@ -146,7 +226,7 @@ export const DATA_SOURCES = /** @type {Record<string, string>} */ ({
|
|
|
146
226
|
* @property {{ address: string, label: string }} from the own address that paid
|
|
147
227
|
* @property {{ address: string, label: string }} to who received the payment
|
|
148
228
|
* @property {Movement[]} movements every movement of the transaction the period has
|
|
149
|
-
* @property {string} eurExact what is on the Eigenbeleg, in
|
|
229
|
+
* @property {string} eurExact what is on the Eigenbeleg, in the document's currency, positive
|
|
150
230
|
*/
|
|
151
231
|
|
|
152
232
|
/**
|
|
@@ -160,6 +240,9 @@ export const DATA_SOURCES = /** @type {Record<string, string>} */ ({
|
|
|
160
240
|
* @param {string[]} [p.includeKinds]
|
|
161
241
|
* @param {string} [p.issuedAt] ISO; now when not given
|
|
162
242
|
* @param {'collective' | 'single'} [p.layout]
|
|
243
|
+
* @param {string} [p.currency] the document's currency (DOCUMENT_CURRENCIES), EUR when not
|
|
244
|
+
* given; the entries are valued in it. The fields named `eur…` hold the values in this
|
|
245
|
+
* currency: they keep the name they had when every document was in euro.
|
|
163
246
|
*/
|
|
164
247
|
export function buildDocument({
|
|
165
248
|
payer,
|
|
@@ -170,10 +253,13 @@ export function buildDocument({
|
|
|
170
253
|
number,
|
|
171
254
|
includeKinds = DEFAULT_KINDS,
|
|
172
255
|
issuedAt,
|
|
173
|
-
layout = 'collective'
|
|
256
|
+
layout = 'collective',
|
|
257
|
+
currency: asked
|
|
174
258
|
}) {
|
|
175
259
|
if (layout !== 'collective' && layout !== 'single')
|
|
176
260
|
throw new Error('layout: collective or single');
|
|
261
|
+
const currency = documentCurrency(asked);
|
|
262
|
+
checkValued(entries, currency);
|
|
177
263
|
checkPayer(payer);
|
|
178
264
|
const kinds = new Set(includeKinds);
|
|
179
265
|
const own = new Set(payer.addresses);
|
|
@@ -276,23 +362,18 @@ export function buildDocument({
|
|
|
276
362
|
network,
|
|
277
363
|
testnet: network !== 'mainnet',
|
|
278
364
|
period,
|
|
365
|
+
currency,
|
|
279
366
|
positions,
|
|
280
367
|
excluded,
|
|
281
368
|
totals: {
|
|
282
369
|
quantities,
|
|
283
|
-
// What was paid, as a positive amount, rounded to cents.
|
|
284
|
-
eur: round(eurExact.replace(/^-/, ''),
|
|
370
|
+
// What was paid, as a positive amount, rounded to cents (the currency's smallest unit).
|
|
371
|
+
eur: round(eurExact.replace(/^-/, ''), currencyPlaces(currency)),
|
|
285
372
|
eurExact: eurExact.replace(/^-/, '')
|
|
286
373
|
},
|
|
287
374
|
// The rate's name without its day (`… vom 2026-08-10`): the day is each position's.
|
|
288
375
|
sources: [
|
|
289
|
-
...new Set(
|
|
290
|
-
positions.map((p) =>
|
|
291
|
-
/** @type {any} */ (p.entry.eur).source
|
|
292
|
-
.replace(/ \d{4}-\d{2}-\d{2}.*$/, '')
|
|
293
|
-
.replace(/ vom$/, '')
|
|
294
|
-
)
|
|
295
|
-
)
|
|
376
|
+
...new Set(positions.map((p) => sourceName(/** @type {any} */ (p.entry.eur).source)))
|
|
296
377
|
],
|
|
297
378
|
note: noInvoice(chain, chainName).note,
|
|
298
379
|
notInvoice: NOT_AN_INVOICE
|
|
@@ -344,12 +425,46 @@ export function deNumber(amount) {
|
|
|
344
425
|
return `${neg ? '−' : ''}${grouped}${frac ? ',' + frac : ''}`;
|
|
345
426
|
}
|
|
346
427
|
|
|
347
|
-
/**
|
|
348
|
-
|
|
428
|
+
/**
|
|
429
|
+
* A rate for the table, rounded to `digits` significant digits: `0.012345678901234567` → `0,0123457`,
|
|
430
|
+
* `0.00000072500003` → `0,000000725`. Read, not recomputed: the values stay at the full rate,
|
|
431
|
+
* which the CSV keeps; a rate with every decimal does not fit its column.
|
|
432
|
+
*
|
|
433
|
+
* @param {string} rate @param {number} [digits]
|
|
434
|
+
*/
|
|
435
|
+
export function deRate(rate, digits = 6) {
|
|
436
|
+
const s = normalize(String(rate)).replace(/^-/, '');
|
|
437
|
+
const [int, frac = ''] = s.split('.');
|
|
438
|
+
const places =
|
|
439
|
+
int !== '0'
|
|
440
|
+
? Math.max(0, digits - int.length)
|
|
441
|
+
: frac.length - frac.replace(/^0+/, '').length + digits;
|
|
442
|
+
return deNumber(normalize(round(String(rate), places)));
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
/** An amount with fixed decimals and no unit: `0.0017` → `0,0017`. @param {string} amount @param {number} places */
|
|
446
|
+
export function deFixed(amount, places = 2) {
|
|
349
447
|
const r = round(String(amount), places);
|
|
350
448
|
const [int, frac = ''] = r.replace(/^-/, '').split('.');
|
|
351
449
|
const grouped = int.replace(/\B(?=(\d{3})+(?!\d))/g, '.');
|
|
352
|
-
return `${r.startsWith('-') ? '−' : ''}${grouped}
|
|
450
|
+
return `${r.startsWith('-') ? '−' : ''}${grouped}${places ? `,${frac.padEnd(places, '0')}` : ''}`;
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
/** A euro amount with fixed decimals: `0.0017` → `0,0017 €`. @param {string} amount @param {number} places */
|
|
454
|
+
export function deEuro(amount, places = 2) {
|
|
455
|
+
return `${deFixed(amount, places)} €`;
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
/**
|
|
459
|
+
* An amount in a document's currency: `12,34 €`, and for every other currency
|
|
460
|
+
* its code (`12,34 CHF`) – kr and $ are several currencies' signs.
|
|
461
|
+
*
|
|
462
|
+
* @param {string} amount
|
|
463
|
+
* @param {string} currency
|
|
464
|
+
* @param {number} [places] the currency's own when not given
|
|
465
|
+
*/
|
|
466
|
+
export function deMoney(amount, currency, places = currencyPlaces(currency)) {
|
|
467
|
+
return `${deFixed(amount, places)} ${currency === 'EUR' ? '€' : currency}`;
|
|
353
468
|
}
|
|
354
469
|
|
|
355
470
|
/** `2026-10-05` → `05.10.2026`. @param {string} date */
|
package/src/index.js
CHANGED
|
@@ -15,8 +15,15 @@ export {
|
|
|
15
15
|
CHAIN_NAMES,
|
|
16
16
|
DATA_SOURCES,
|
|
17
17
|
deNumber,
|
|
18
|
+
deRate,
|
|
18
19
|
deEuro,
|
|
19
|
-
|
|
20
|
+
deFixed,
|
|
21
|
+
deMoney,
|
|
22
|
+
deDate,
|
|
23
|
+
DOCUMENT_CURRENCIES,
|
|
24
|
+
documentCurrency,
|
|
25
|
+
currencyPlaces,
|
|
26
|
+
conversionText
|
|
20
27
|
} from './document.js';
|
|
21
28
|
export { eigenbelegCsv } from './csv.js';
|
|
22
29
|
export { eigenbelegPdf, CREATOR, PRODUCER } from './pdf.js';
|
|
@@ -28,7 +35,8 @@ export {
|
|
|
28
35
|
STATEMENT_NOTE,
|
|
29
36
|
USAGE_NOTE,
|
|
30
37
|
USAGE_NOTES,
|
|
31
|
-
FILECOIN_PAY_USAGE_NOTE
|
|
38
|
+
FILECOIN_PAY_USAGE_NOTE,
|
|
39
|
+
usageNote
|
|
32
40
|
} from './statement.js';
|
|
33
41
|
/** @typedef {import('./statement.js').MonthlyLine} MonthlyLine */
|
|
34
42
|
export { statementPdf } from './statement-pdf.js';
|
|
@@ -38,6 +46,9 @@ export { valueEntries, fixedRates } from './value.js';
|
|
|
38
46
|
export { nextSelfNumber, bookingNote, bookingNotInvoice } from './booking.js';
|
|
39
47
|
export { bookingPdf } from './booking-pdf.js';
|
|
40
48
|
export { statementLayout } from './statement-layout.js';
|
|
49
|
+
export { ISSUER_LOGO_BOX, LETTERHEAD, MARK_SIZE } from './layout.js';
|
|
50
|
+
// What an issuer's logo may be, and the error a file that is not gets (issuerLogo of the PDFs).
|
|
51
|
+
export { ImageError, IMAGE_LIMITS } from '@le-space/pdf';
|
|
41
52
|
/** @typedef {import('./value.js').Rate} Rate */
|
|
42
53
|
/** @typedef {import('./value.js').Rates} Rates */
|
|
43
54
|
/** @typedef {import('./value.js').ValuedEntry} ValuedEntry */
|