@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/src/statement-pdf.js
CHANGED
|
@@ -9,27 +9,27 @@
|
|
|
9
9
|
// bookings a small diagram (#889, statement-chart.js): the balance over the
|
|
10
10
|
// period, or what was consumed per day.
|
|
11
11
|
|
|
12
|
-
import { rgb } from 'pdf-lib';
|
|
13
12
|
import { asset, createPdfA, LOGOS, savePdfA, wrapText } from '@le-space/pdf';
|
|
14
13
|
import { round } from '@le-space/ledger';
|
|
15
14
|
|
|
16
|
-
import { DATA_SOURCES, deDate,
|
|
15
|
+
import { DATA_SOURCES, conversionText, deDate, deFixed, deMoney, deNumber } from './document.js';
|
|
17
16
|
import { CREATOR, PRODUCER } from './pdf.js';
|
|
18
17
|
import { isDust, reconciled } from './statement.js';
|
|
19
18
|
import { drawScene, sceneMeasure } from './chart-pdf.js';
|
|
20
19
|
import { statementChart } from './statement-chart.js';
|
|
21
|
-
import {
|
|
20
|
+
import { BLACK, GREY, INK, MARK_SIZE, TYPE, WARN, pageLayout } from './layout.js';
|
|
22
21
|
|
|
23
|
-
|
|
24
|
-
const
|
|
22
|
+
// The same frame as every other document of the package (layout.js).
|
|
23
|
+
const M = 48;
|
|
25
24
|
|
|
26
|
-
|
|
25
|
+
/** The table of bookings; its last column names the statement's currency. @param {string} currency */
|
|
26
|
+
const columns = (currency) => [
|
|
27
27
|
{ title: 'Buchungstag', width: 52 },
|
|
28
|
-
{ title: 'Vorgang', width:
|
|
28
|
+
{ title: 'Vorgang', width: 167 },
|
|
29
29
|
{ title: 'Soll', width: 72, right: true },
|
|
30
30
|
{ title: 'Haben', width: 72, right: true },
|
|
31
31
|
{ title: 'Saldo', width: 80, right: true },
|
|
32
|
-
{ title:
|
|
32
|
+
{ title: `Wert ${currency}`, width: 56, right: true }
|
|
33
33
|
];
|
|
34
34
|
|
|
35
35
|
/** Quantities in the table: at most 12 decimals, the CSV keeps them all. @param {string} q */
|
|
@@ -38,8 +38,9 @@ const qty = (q) => (q ? deNumber(round(q, 12)) : '');
|
|
|
38
38
|
/**
|
|
39
39
|
* @param {import('./statement.js').Statement} st
|
|
40
40
|
* @param {{ creator?: string, producer?: string, logo?: boolean, chart?: boolean }} [o] logo: the chain's
|
|
41
|
-
* mark,
|
|
42
|
-
* diagram above each list of bookings (on unless switched off)
|
|
41
|
+
* mark, larger than on an Eigenbeleg, beside the title (off unless asked for; @le-space/pdf's LOGOS).
|
|
42
|
+
* chart: the diagram above each list of bookings (on unless switched off). A statement carries no
|
|
43
|
+
* issuer's logo: it shows what the account did, it is not the issuer's own receipt.
|
|
43
44
|
* @returns {Promise<Uint8Array>}
|
|
44
45
|
*/
|
|
45
46
|
export async function statementPdf(
|
|
@@ -51,6 +52,14 @@ export async function statementPdf(
|
|
|
51
52
|
const aleph = st.chain === 'aleph';
|
|
52
53
|
// Filecoin Pay: the same, in USDFC and by rail.
|
|
53
54
|
const pay = st.chain === 'filecoin-pay';
|
|
55
|
+
// The statement's currency (EUR unless another): its code in the column heads and sums.
|
|
56
|
+
const cur = st.currency ?? 'EUR';
|
|
57
|
+
const COLS = columns(cur);
|
|
58
|
+
/** @param {string} amount */
|
|
59
|
+
const money = (amount) => deMoney(amount, cur);
|
|
60
|
+
/** What the values are: in euro as it always said, or how they became another currency. @param {string} eur @param {string} rate */
|
|
61
|
+
const valued = (eur, rate) =>
|
|
62
|
+
cur === 'EUR' ? `Euro-Werte ${eur}` : `Werte in ${cur}: ${rate}, ${conversionText(cur)}`;
|
|
54
63
|
const title = `${st.title} ${st.number}`;
|
|
55
64
|
const subject = `${st.title} ${st.chainName} ${st.account}, ${deDate(st.period.from)} – ${deDate(st.period.to)}`;
|
|
56
65
|
const { pdf, fonts } = await createPdfA();
|
|
@@ -63,32 +72,40 @@ export async function statementPdf(
|
|
|
63
72
|
margin: M,
|
|
64
73
|
bottom: 16,
|
|
65
74
|
enterPage: (p) => {
|
|
66
|
-
p.at(`${st.title} ${st.number} · ${st.account}`, M,
|
|
75
|
+
p.at(`${st.title} ${st.number} · ${st.account}`, M, TYPE.label, regular, GREY);
|
|
67
76
|
p.y -= 18;
|
|
68
77
|
}
|
|
69
78
|
});
|
|
70
|
-
const { text } = l;
|
|
79
|
+
const { text, label } = l;
|
|
71
80
|
const width = l.width;
|
|
72
|
-
/**
|
|
73
|
-
const
|
|
74
|
-
|
|
81
|
+
/** Where each column ends, from the left margin: sums and balances stand under their columns. */
|
|
82
|
+
const ends = COLS.reduce(
|
|
83
|
+
(a, c) => [...a, (a[a.length - 1] ?? 0) + c.width],
|
|
84
|
+
/** @type {number[]} */ ([])
|
|
85
|
+
);
|
|
86
|
+
/** The right edge of the figures in column `i`. @param {number} i */
|
|
87
|
+
const edge = (i) => M + ends[i] - 4;
|
|
88
|
+
const [DEBIT, CREDIT, BALANCE, VALUE] = [2, 3, 4, 5];
|
|
75
89
|
|
|
76
|
-
// ---- head:
|
|
90
|
+
// ---- head: the title first, as on every document; below the rule the holder and the facts
|
|
77
91
|
if (st.testnet) {
|
|
78
92
|
text('TESTNET – keine echten Werte. Kurse des Mainnet-Assets nur zur Veranschaulichung.', {
|
|
79
93
|
face: bold,
|
|
80
|
-
size:
|
|
94
|
+
size: TYPE.table,
|
|
81
95
|
color: WARN
|
|
82
96
|
});
|
|
83
|
-
l.y -=
|
|
97
|
+
l.y -= 6;
|
|
84
98
|
}
|
|
99
|
+
// The chain's mark, larger: a statement shows what the network's account did. The issuer's logo is
|
|
100
|
+
// not on it: a statement is not the issuer's own receipt.
|
|
101
|
+
l.head(st.title, { image: mark, mark: MARK_SIZE.large });
|
|
85
102
|
const top = l.y;
|
|
86
|
-
|
|
87
|
-
text(st.holder.name, { face: bold, size:
|
|
88
|
-
text(st.holder.address, {
|
|
103
|
+
label('Kontoinhaber', { max: 250 });
|
|
104
|
+
text(st.holder.name, { face: bold, size: TYPE.lead, max: 250 });
|
|
105
|
+
text(st.holder.address, { max: 250 });
|
|
89
106
|
const leftEnd = l.y;
|
|
90
107
|
l.y = top;
|
|
91
|
-
const rx = M +
|
|
108
|
+
const rx = M + width / 2 + 10;
|
|
92
109
|
const facts = [
|
|
93
110
|
// The label column is 72 pt wide: »Verbrauchsauszug« does not fit.
|
|
94
111
|
[st.title === 'Kontoauszug' ? 'Kontoauszug' : 'Auszug-Nr.', st.number],
|
|
@@ -97,24 +114,25 @@ export async function statementPdf(
|
|
|
97
114
|
['Erstellt am', `${deDate(st.issuedAt)}, ${st.issuedAt.slice(11, 16)} UTC`]
|
|
98
115
|
];
|
|
99
116
|
for (const [k, v] of facts) {
|
|
100
|
-
|
|
101
|
-
regular.draw(l.page, v, { x: rx + 72, y: l.y -
|
|
102
|
-
l.y -=
|
|
117
|
+
regular.draw(l.page, k, { x: rx, y: l.y - TYPE.body, size: TYPE.small, color: GREY });
|
|
118
|
+
regular.draw(l.page, v, { x: rx + 72, y: l.y - TYPE.body, size: TYPE.body, color: INK });
|
|
119
|
+
l.y -= 13;
|
|
103
120
|
}
|
|
104
|
-
l.y = Math.min(l.y, leftEnd) -
|
|
105
|
-
|
|
106
|
-
text(
|
|
107
|
-
text(st.account, { face: mono, size: 9 });
|
|
108
|
-
l.y -= 6;
|
|
109
|
-
text(st.note, { size: 8, color: GREY });
|
|
121
|
+
l.y = Math.min(l.y, leftEnd) - 8;
|
|
122
|
+
label('Konto');
|
|
123
|
+
text(st.account, { face: mono, size: TYPE.body });
|
|
110
124
|
l.y -= 10;
|
|
125
|
+
l.note(st.note, TYPE.table);
|
|
126
|
+
l.y -= 18;
|
|
111
127
|
|
|
112
128
|
// ---- one section per asset
|
|
113
129
|
for (const s of st.sections) {
|
|
114
130
|
l.ensure(90);
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
l.y -=
|
|
131
|
+
// A section: its head on a hairline. No band, no fill.
|
|
132
|
+
text(`${s.asset} · ${st.account}`, { face: bold, size: TYPE.lead });
|
|
133
|
+
l.y -= 3;
|
|
134
|
+
l.rule();
|
|
135
|
+
l.y -= 10;
|
|
118
136
|
// The month in sums, first (#24): what the books take as one booking each; the rows below are the proof.
|
|
119
137
|
if (aleph || pay) {
|
|
120
138
|
const m = s.monthly;
|
|
@@ -123,25 +141,29 @@ export async function statementPdf(
|
|
|
123
141
|
.filter((x) => x !== null);
|
|
124
142
|
if (lines.length) {
|
|
125
143
|
l.ensure(14 + lines.length * 11 + 8);
|
|
126
|
-
l.at('Monatsübersicht', M,
|
|
127
|
-
l.right(pay ? s.asset : 'Credits',
|
|
128
|
-
l.right(
|
|
129
|
-
l.y -=
|
|
144
|
+
l.at('Monatsübersicht', M, TYPE.label, regular, GREY);
|
|
145
|
+
l.right(pay ? s.asset : 'Credits', edge(CREDIT), TYPE.label, regular, GREY);
|
|
146
|
+
l.right(`Wert ${cur}`, edge(VALUE), TYPE.label, regular, GREY);
|
|
147
|
+
l.y -= 11;
|
|
148
|
+
l.strong();
|
|
149
|
+
l.y -= 4;
|
|
130
150
|
for (const ml of lines) {
|
|
131
151
|
const total = ml === m.usageTotal;
|
|
132
152
|
const f = total ? bold : regular;
|
|
133
|
-
l.at(ml.label, M
|
|
134
|
-
l.right(qty(ml.amount),
|
|
153
|
+
l.at(ml.label, M, TYPE.table, f);
|
|
154
|
+
l.right(qty(ml.amount), edge(CREDIT), TYPE.table, f);
|
|
135
155
|
l.right(
|
|
136
|
-
ml.eur ?
|
|
137
|
-
|
|
138
|
-
|
|
156
|
+
ml.eur ? deFixed(ml.eur, 4) : '—',
|
|
157
|
+
edge(VALUE),
|
|
158
|
+
TYPE.table,
|
|
139
159
|
f,
|
|
140
160
|
ml.eur ? BLACK : GREY
|
|
141
161
|
);
|
|
142
|
-
l.y -=
|
|
162
|
+
l.y -= 12;
|
|
143
163
|
}
|
|
144
|
-
l.y -=
|
|
164
|
+
l.y -= 1;
|
|
165
|
+
l.rule();
|
|
166
|
+
l.y -= 14;
|
|
145
167
|
}
|
|
146
168
|
}
|
|
147
169
|
// The diagram, above the list of bookings (#889); every figure in it is in the tables.
|
|
@@ -149,24 +171,26 @@ export async function statementPdf(
|
|
|
149
171
|
if (drawn) {
|
|
150
172
|
l.ensure(drawn.scene.height + 40);
|
|
151
173
|
drawScene(l.page, drawn.scene, { x: M, top: l.y, fonts });
|
|
152
|
-
l.y -= drawn.scene.height +
|
|
174
|
+
l.y -= drawn.scene.height + 14;
|
|
153
175
|
}
|
|
154
176
|
const head = () => {
|
|
155
177
|
let x = M;
|
|
156
178
|
for (const c of COLS) {
|
|
157
|
-
if (c.right) l.right(c.title, x + c.width - 4,
|
|
158
|
-
else l.at(c.title, x,
|
|
179
|
+
if (c.right) l.right(c.title, x + c.width - 4, TYPE.label, regular, GREY);
|
|
180
|
+
else l.at(c.title, x, TYPE.label, regular, GREY);
|
|
159
181
|
x += c.width;
|
|
160
182
|
}
|
|
161
183
|
l.y -= 11;
|
|
162
|
-
|
|
163
|
-
l.y -=
|
|
184
|
+
l.strong();
|
|
185
|
+
l.y -= 4;
|
|
164
186
|
};
|
|
165
187
|
head();
|
|
166
188
|
// opening balance
|
|
167
189
|
l.at(`Kontostand am ${deDate(st.period.from)} (Beginn)`, M + COLS[0].width, 8, bold);
|
|
168
|
-
l.right(qty(s.opening),
|
|
190
|
+
l.right(qty(s.opening), edge(BALANCE), 8, bold);
|
|
169
191
|
l.y -= 13;
|
|
192
|
+
l.rule();
|
|
193
|
+
l.y -= 2;
|
|
170
194
|
for (const r of s.rows) {
|
|
171
195
|
const detail = [
|
|
172
196
|
r.counterparty && `${r.debit ? 'an' : 'von'} ${r.counterparty}`,
|
|
@@ -177,13 +201,13 @@ export async function statementPdf(
|
|
|
177
201
|
.join(' · ');
|
|
178
202
|
const lines = wrapText(r.text, regular, 8, COLS[1].width - 6);
|
|
179
203
|
const sub = wrapText(detail, regular, 6.5, COLS[1].width - 6);
|
|
180
|
-
const h = lines.length * 10 + sub.length * 8.5 +
|
|
204
|
+
const h = lines.length * 10 + sub.length * 8.5 + 6;
|
|
181
205
|
if (l.ensure(h)) head();
|
|
182
206
|
const y = l.y;
|
|
183
|
-
regular.draw(l.page, deDate(r.date), { x: M, y: y - 8, size: 8 });
|
|
207
|
+
regular.draw(l.page, deDate(r.date), { x: M, y: y - 8, size: 8, color: INK });
|
|
184
208
|
regular.draw(l.page, r.time, { x: M, y: y - 16.5, size: 6.5, color: GREY });
|
|
185
209
|
lines.forEach((t, i) =>
|
|
186
|
-
regular.draw(l.page, t, { x: M + COLS[0].width, y: y - 8 - i * 10, size: 8 })
|
|
210
|
+
regular.draw(l.page, t, { x: M + COLS[0].width, y: y - 8 - i * 10, size: 8, color: INK })
|
|
187
211
|
);
|
|
188
212
|
sub.forEach((t, i) =>
|
|
189
213
|
regular.draw(l.page, t, {
|
|
@@ -201,30 +225,36 @@ export async function statementPdf(
|
|
|
201
225
|
l.right(qty(r.balance), x + COLS[4].width - 4, 8, regular);
|
|
202
226
|
x += COLS[4].width;
|
|
203
227
|
l.right(
|
|
204
|
-
r.eur ?
|
|
228
|
+
r.eur ? deFixed(r.eur, 4) : '—',
|
|
205
229
|
x + COLS[5].width - 4,
|
|
206
230
|
8,
|
|
207
231
|
regular,
|
|
208
232
|
r.eur ? BLACK : GREY
|
|
209
233
|
);
|
|
210
234
|
l.y -= h;
|
|
211
|
-
|
|
235
|
+
l.y += 1;
|
|
236
|
+
l.rule();
|
|
237
|
+
l.y -= 1;
|
|
212
238
|
}
|
|
213
239
|
if (s.rows.length === 0) {
|
|
214
240
|
l.at('Keine Buchungen in diesem Zeitraum.', M + COLS[0].width, 8, regular, GREY);
|
|
215
241
|
l.y -= 13;
|
|
216
242
|
}
|
|
217
243
|
// sums and closing balance
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
l.y
|
|
221
|
-
l.
|
|
222
|
-
l.
|
|
223
|
-
l.
|
|
224
|
-
l.
|
|
225
|
-
l.
|
|
226
|
-
l.
|
|
227
|
-
l.
|
|
244
|
+
// Between two strong rules, like the amount of an Eigenbeleg: the sums, then the balance to carry on.
|
|
245
|
+
l.ensure(70);
|
|
246
|
+
l.y += 1;
|
|
247
|
+
l.strong();
|
|
248
|
+
l.y -= 5;
|
|
249
|
+
l.at('Summe', M + COLS[0].width, 8, regular);
|
|
250
|
+
l.right(qty(s.debit), edge(DEBIT), 8, regular);
|
|
251
|
+
l.right(qty(s.credit), edge(CREDIT), 8, regular);
|
|
252
|
+
l.y -= 14;
|
|
253
|
+
l.at(`Kontostand am ${deDate(st.period.to)} (Ende)`, M + COLS[0].width, TYPE.lead, bold);
|
|
254
|
+
l.right(`${qty(s.closing)} ${s.asset}`, edge(BALANCE), TYPE.lead, bold);
|
|
255
|
+
l.y -= 17;
|
|
256
|
+
l.strong();
|
|
257
|
+
l.y -= 8;
|
|
228
258
|
// the check
|
|
229
259
|
if (st.method === 'archive' && s.reported !== null) {
|
|
230
260
|
const ok = reconciled(s.difference) === true;
|
|
@@ -238,47 +268,39 @@ export async function statementPdf(
|
|
|
238
268
|
: ok
|
|
239
269
|
? `Abgestimmt: Der Kontostand laut Chain nach dem Zeitraum${when} ist ${qty(s.reported)} ${s.asset} und stimmt mit Anfangsstand plus Buchungen überein.`
|
|
240
270
|
: `Nicht abgestimmt: Der Kontostand laut Chain${when} ist ${qty(s.reported)} ${s.asset}; Differenz ${deNumber(difference)} ${s.asset} (Bewegungen, die der Leser nicht sieht, etwa Auszahlungen eines Staking-Pools).`,
|
|
241
|
-
|
|
271
|
+
// Reconciled is the normal case and reads as text; only a difference is a warning.
|
|
272
|
+
{ size: TYPE.small, color: ok ? INK : WARN }
|
|
242
273
|
);
|
|
243
274
|
} else {
|
|
244
275
|
text(
|
|
245
276
|
`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' : pay ? 'der Zahlungsvertrag nennt nur den heutigen Stand' : 'die Chain hält keine alten Kontostände vor'}).`,
|
|
246
|
-
{ size:
|
|
277
|
+
{ size: TYPE.small, color: GREY }
|
|
247
278
|
);
|
|
248
279
|
}
|
|
249
280
|
l.y -= 4;
|
|
250
281
|
text(
|
|
251
282
|
pay
|
|
252
|
-
? `Ausgänge ${
|
|
283
|
+
? `Ausgänge ${money(s.eurOut)} · Eingänge ${money(s.eurIn)} · davon Verbrauch (Aufwand) ${money(s.eurUsage)}${s.unvalued ? ` · ${s.unvalued} Buchung(en) ohne Kurs` : ''} – ${valued(`zum Tageskurs von USDC, an den US-Dollar gebunden wie ${s.asset} (Kurs 00:00 UTC: CoinGecko, sonst Kraken)`, `Tageskurs von USDC, an den US-Dollar gebunden wie ${s.asset} (Kurs 00:00 UTC: CoinGecko, sonst Kraken)`)}.`
|
|
253
284
|
: aleph
|
|
254
|
-
? `Ausgänge ${
|
|
255
|
-
|
|
256
|
-
|
|
285
|
+
? `Ausgänge ${money(s.eurOut)} · Eingänge ${money(s.eurIn)} · davon Verbrauch (Aufwand) ${money(s.eurUsage)}${s.unvalued ? ` · ${s.unvalued} Buchung(en) ohne Kurs` : ''} – ${
|
|
286
|
+
cur === 'EUR'
|
|
287
|
+
? 'Euro-Werte: USD-Preis je Credit (letzter Kauf am oder vor dem Tag, sonst Listenpreis) × EZB-Referenzkurs USD/EUR des Tages'
|
|
288
|
+
: `Werte in ${cur}: USD-Preis je Credit (letzter Kauf am oder vor dem Tag, sonst Listenpreis)${cur === 'USD' ? '' : `, ${conversionText(cur)}`}`
|
|
289
|
+
}.`
|
|
290
|
+
: `Ausgänge ${money(s.eurOut)} · Eingänge ${money(s.eurIn)} · davon Netzwerkgebühren (Aufwand) ${money(s.eurFees)}${s.unvalued ? ` · ${s.unvalued} Buchung(en) ohne Kurs` : ''} – ${valued('zum Tageskurs (Kurs 00:00 UTC: CoinGecko, sonst Kraken)', 'Tageskurs (Kurs 00:00 UTC: CoinGecko, sonst Kraken)')}.`,
|
|
291
|
+
{ size: TYPE.small, color: GREY }
|
|
257
292
|
);
|
|
258
|
-
l.y -=
|
|
293
|
+
l.y -= 20;
|
|
259
294
|
}
|
|
260
295
|
if (st.unlisted)
|
|
261
296
|
text(
|
|
262
297
|
`${st.unlisted} Bewegung(en) in Token ohne verlässlichen Kurs (oft Spam) sind nur in der CSV-Datei aufgeführt.`,
|
|
263
|
-
{ size:
|
|
298
|
+
{ size: TYPE.small, color: GREY }
|
|
264
299
|
);
|
|
265
300
|
|
|
266
301
|
// page numbers and footer on every page
|
|
267
302
|
const data = `Daten: ${DATA_SOURCES[st.chain] ?? st.chainName} · kein Dokument eines Kreditinstituts`;
|
|
268
|
-
l.pages.forEach((p, i) => {
|
|
269
|
-
regular.draw(p, `${st.number} · Seite ${i + 1} von ${l.pages.length}`, {
|
|
270
|
-
x: M,
|
|
271
|
-
y: M / 2,
|
|
272
|
-
size: 7,
|
|
273
|
-
color: GREY
|
|
274
|
-
});
|
|
275
|
-
regular.draw(p, data, {
|
|
276
|
-
x: A4[0] - M - regular.width(data, 7),
|
|
277
|
-
y: M / 2,
|
|
278
|
-
size: 7,
|
|
279
|
-
color: GREY
|
|
280
|
-
});
|
|
281
|
-
});
|
|
303
|
+
l.pages.forEach((p, i) => l.foot(p, `${st.number} · Seite ${i + 1} von ${l.pages.length}`, data));
|
|
282
304
|
|
|
283
305
|
return savePdfA(pdf, {
|
|
284
306
|
title,
|
package/src/statement.js
CHANGED
|
@@ -20,7 +20,13 @@
|
|
|
20
20
|
// 10⁻²⁴ NEAR), so an account that moved tokens is off by a few yocto.
|
|
21
21
|
|
|
22
22
|
import { normalize, sum } from '@le-space/ledger';
|
|
23
|
-
import {
|
|
23
|
+
import {
|
|
24
|
+
CHAIN_NAMES,
|
|
25
|
+
checkValued,
|
|
26
|
+
conversionText,
|
|
27
|
+
documentCurrency,
|
|
28
|
+
kindText
|
|
29
|
+
} from './document.js';
|
|
24
30
|
|
|
25
31
|
/** Smaller than the twelve decimals the documents print (and not zero). @param {string} difference */
|
|
26
32
|
export const isDust = (difference) => /^-?0\.0{12}\d*$/.test(difference);
|
|
@@ -54,6 +60,25 @@ export const USAGE_NOTES = /** @type {Record<string, string>} */ ({
|
|
|
54
60
|
'filecoin-pay': FILECOIN_PAY_USAGE_NOTE
|
|
55
61
|
});
|
|
56
62
|
|
|
63
|
+
/**
|
|
64
|
+
* The note of a Verbrauchsauszug in the document's currency: what its values
|
|
65
|
+
* are, said for that currency. EUR: the notes above, as they were.
|
|
66
|
+
*
|
|
67
|
+
* @param {string} chain from USAGE_NOTES
|
|
68
|
+
* @param {string} currency
|
|
69
|
+
*/
|
|
70
|
+
export function usageNote(chain, currency) {
|
|
71
|
+
const note = USAGE_NOTES[chain];
|
|
72
|
+
if (currency === 'EUR') return note;
|
|
73
|
+
const head = note.slice(0, note.indexOf('Euro-Wert:'));
|
|
74
|
+
if (chain === 'aleph') {
|
|
75
|
+
const price =
|
|
76
|
+
'USD-Preis je Credit (letzter Kauf am oder vor dem Tag, sonst Alephs Listenpreis von 1 USD je 1.000.000 Credits)';
|
|
77
|
+
return `${head}Wert in ${currency}: ${price}${currency === 'USD' ? '' : `, ${conversionText(currency)}`}.`;
|
|
78
|
+
}
|
|
79
|
+
return `${head}Wert in ${currency}: USDFC ist an den US-Dollar gebunden und wird zum Tageskurs von USDC bewertet, ${conversionText(currency)}.`;
|
|
80
|
+
}
|
|
81
|
+
|
|
57
82
|
export const STATEMENT_NOTE =
|
|
58
83
|
'Kontoauszug eines Blockchain-Kontos, erstellt vom Kontoinhaber aus öffentlichen Chain-Daten. ' +
|
|
59
84
|
'Kein Dokument eines Kreditinstituts. Jede Buchung ist über ihren Transaktions-Hash in einem öffentlichen Block-Explorer nachprüfbar.';
|
|
@@ -69,7 +94,7 @@ export const STATEMENT_NOTE =
|
|
|
69
94
|
* @property {string} debit amount out, positive, or ''
|
|
70
95
|
* @property {string} credit amount in, positive, or ''
|
|
71
96
|
* @property {string} balance running balance after it
|
|
72
|
-
* @property {string} eur signed
|
|
97
|
+
* @property {string} eur signed value at the day's rate, in the statement's currency (EUR unless another), or ''
|
|
73
98
|
* @property {string} beleg the Eigenbeleg's number, when the booking has one
|
|
74
99
|
* @property {boolean} fee
|
|
75
100
|
* @property {boolean} usage consumption billed from the balance (Aleph credits, a Filecoin Pay settlement)
|
|
@@ -144,6 +169,8 @@ export function monthly(rows) {
|
|
|
144
169
|
* @param {Map<string, string>} [p.belege] hash → Eigenbeleg number
|
|
145
170
|
* @param {string} [p.number]
|
|
146
171
|
* @param {string} [p.issuedAt]
|
|
172
|
+
* @param {string} [p.currency] the statement's currency (DOCUMENT_CURRENCIES), EUR when not
|
|
173
|
+
* given; the entries are valued in it, and the fields named `eur…` hold values in it
|
|
147
174
|
*/
|
|
148
175
|
export function buildStatement({
|
|
149
176
|
payer,
|
|
@@ -158,8 +185,11 @@ export function buildStatement({
|
|
|
158
185
|
at = null,
|
|
159
186
|
belege = new Map(),
|
|
160
187
|
number,
|
|
161
|
-
issuedAt
|
|
188
|
+
issuedAt,
|
|
189
|
+
currency: asked
|
|
162
190
|
}) {
|
|
191
|
+
const currency = documentCurrency(asked);
|
|
192
|
+
checkValued(entries, currency);
|
|
163
193
|
// An Aleph credit purchase (kind `swap`) is the payment on another chain; the
|
|
164
194
|
// credits it bought are the incoming row. Only that row belongs on the statement.
|
|
165
195
|
const mine = entries.filter(
|
|
@@ -236,11 +266,12 @@ export function buildStatement({
|
|
|
236
266
|
network,
|
|
237
267
|
testnet: network !== 'mainnet',
|
|
238
268
|
period,
|
|
269
|
+
currency,
|
|
239
270
|
method,
|
|
240
271
|
at,
|
|
241
272
|
sections,
|
|
242
273
|
unlisted: mine.filter((e) => !e.priceAs && !assets.includes(e.asset)).length,
|
|
243
|
-
note: usage ?
|
|
274
|
+
note: usage ? usageNote(chain, currency) : STATEMENT_NOTE
|
|
244
275
|
};
|
|
245
276
|
}
|
|
246
277
|
|
package/src/value.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
2
|
-
// Every entry with its
|
|
2
|
+
// Every entry with its value on the day it was made: in euro, or in the
|
|
3
|
+
// document's currency when another is asked for (`currency`; the rates handed
|
|
4
|
+
// in are then in that currency, and say so). Where the rate
|
|
3
5
|
// comes from is the caller's (the pipeline asks @le-space/rates); this file
|
|
4
6
|
// only applies it, and names a fixed rate for tests and offline demos.
|
|
5
7
|
//
|
|
@@ -10,17 +12,22 @@
|
|
|
10
12
|
|
|
11
13
|
import { multiply } from '@le-space/ledger';
|
|
12
14
|
|
|
15
|
+
import { documentCurrency } from './document.js';
|
|
16
|
+
|
|
13
17
|
/**
|
|
14
18
|
* @typedef {object} Rate
|
|
15
|
-
* @property {string} rate EUR per unit, decimal string
|
|
19
|
+
* @property {string} rate EUR per unit (or the document currency's), decimal string
|
|
16
20
|
* @property {string} source human-readable: where and which moment, printed on the document
|
|
17
21
|
* @property {string} date YYYY-MM-DD the rate is for
|
|
22
|
+
* @property {string} [currency] what the rate is in; EUR when not given
|
|
18
23
|
*/
|
|
19
24
|
|
|
20
25
|
/** @typedef {{ rateFor: (asset: string, date: string) => Promise<Rate | null> }} Rates */
|
|
21
26
|
|
|
22
27
|
/**
|
|
23
|
-
* @typedef {import('@le-space/ledger').LedgerEntry & { eur: { value: string, rate: string, source: string } | null }} ValuedEntry
|
|
28
|
+
* @typedef {import('@le-space/ledger').LedgerEntry & { eur: { value: string, rate: string, source: string, currency?: string } | null }} ValuedEntry
|
|
29
|
+
* `eur`: the value, named so since every document was in euro; in another
|
|
30
|
+
* currency it carries that `currency`
|
|
24
31
|
*/
|
|
25
32
|
|
|
26
33
|
/**
|
|
@@ -29,10 +36,21 @@ import { multiply } from '@le-space/ledger';
|
|
|
29
36
|
* `unitPrice` in USD (Aleph credits) is valued at that price and the day's rate for USD.
|
|
30
37
|
*
|
|
31
38
|
* @param {import('@le-space/ledger').LedgerEntry[]} entries
|
|
32
|
-
* @param {Rates} rates
|
|
39
|
+
* @param {Rates} rates in the document's currency
|
|
40
|
+
* @param {{ currency?: string }} [o] the document's currency (DOCUMENT_CURRENCIES), EUR when not given
|
|
33
41
|
* @returns {Promise<ValuedEntry[]>}
|
|
34
42
|
*/
|
|
35
|
-
export async function valueEntries(entries, rates) {
|
|
43
|
+
export async function valueEntries(entries, rates, { currency: asked } = {}) {
|
|
44
|
+
const currency = documentCurrency(asked);
|
|
45
|
+
// An EUR value stays as it always was; another currency is named on it.
|
|
46
|
+
const mark = currency === 'EUR' ? {} : { currency };
|
|
47
|
+
/** A rate in another currency than the document's is the caller's mistake, not a value. @param {string} asset @param {string} date */
|
|
48
|
+
const rateFor = async (asset, date) => {
|
|
49
|
+
const r = await rates.rateFor(asset, date);
|
|
50
|
+
if (r && (r.currency ?? 'EUR') !== currency)
|
|
51
|
+
throw new Error(`a rate in ${r.currency ?? 'EUR'} for a document in ${currency}`);
|
|
52
|
+
return r;
|
|
53
|
+
};
|
|
36
54
|
const out = [];
|
|
37
55
|
for (const e of entries) {
|
|
38
56
|
const unit =
|
|
@@ -40,35 +58,58 @@ export async function valueEntries(entries, rates) {
|
|
|
40
58
|
/** @type {any} */ (e).unitPrice
|
|
41
59
|
);
|
|
42
60
|
if (!e.priceAs && e.success && unit?.currency === 'USD' && unit.amount) {
|
|
43
|
-
const usd = await rates.rateFor('USD', e.date);
|
|
44
61
|
const how = unit.source === 'purchase' ? 'Kaufpreis' : 'Listenpreis';
|
|
62
|
+
// A document in USD: Aleph's own price, no rate at all.
|
|
63
|
+
if (currency === 'USD') {
|
|
64
|
+
out.push({
|
|
65
|
+
...e,
|
|
66
|
+
eur: {
|
|
67
|
+
value: multiply(e.amount, unit.amount, 8),
|
|
68
|
+
rate: unit.amount,
|
|
69
|
+
source: `Aleph ${how} ${unit.amount} USD je Credit`,
|
|
70
|
+
...mark
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
const usd = await rateFor('USD', e.date);
|
|
45
76
|
out.push({
|
|
46
77
|
...e,
|
|
47
78
|
eur: usd
|
|
48
79
|
? {
|
|
49
80
|
value: multiply(multiply(e.amount, unit.amount, 12), usd.rate, 8),
|
|
50
81
|
rate: multiply(unit.amount, usd.rate, 14),
|
|
51
|
-
source: `Aleph ${how} ${unit.amount} USD je Credit × ${usd.source}
|
|
82
|
+
source: `Aleph ${how} ${unit.amount} USD je Credit × ${usd.source}`,
|
|
83
|
+
...mark
|
|
52
84
|
}
|
|
53
85
|
: null
|
|
54
86
|
});
|
|
55
87
|
continue;
|
|
56
88
|
}
|
|
57
|
-
const r = e.priceAs && e.success ? await
|
|
89
|
+
const r = e.priceAs && e.success ? await rateFor(e.priceAs, e.date) : null;
|
|
58
90
|
out.push({
|
|
59
91
|
...e,
|
|
60
|
-
eur: r
|
|
92
|
+
eur: r
|
|
93
|
+
? { value: multiply(e.amount, r.rate, 8), rate: r.rate, source: r.source, ...mark }
|
|
94
|
+
: null
|
|
61
95
|
});
|
|
62
96
|
}
|
|
63
97
|
return out;
|
|
64
98
|
}
|
|
65
99
|
|
|
66
|
-
/**
|
|
67
|
-
|
|
100
|
+
/**
|
|
101
|
+
* Fixed rates, for tests and offline demos.
|
|
102
|
+
*
|
|
103
|
+
* @param {Record<string, string>} table `NEAR:2026-10-05` → `3.8796`
|
|
104
|
+
* @param {string} [currency] what the table's rates are in; EUR when not given
|
|
105
|
+
* @returns {Rates}
|
|
106
|
+
*/
|
|
107
|
+
export function fixedRates(table, currency) {
|
|
108
|
+
const mark = currency && currency !== 'EUR' ? { currency } : {};
|
|
68
109
|
return {
|
|
69
110
|
async rateFor(asset, date) {
|
|
70
111
|
const rate = table[`${asset}:${date}`] ?? table[asset];
|
|
71
|
-
return rate ? { rate, source: `fester Testkurs (${asset})`, date } : null;
|
|
112
|
+
return rate ? { rate, source: `fester Testkurs (${asset})`, date, ...mark } : null;
|
|
72
113
|
}
|
|
73
114
|
};
|
|
74
115
|
}
|