@formancy/report-runtime 0.2.0 → 0.3.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 +434 -15
- package/dist/index.d.mts +661 -66
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +2238 -644
- package/dist/index.mjs.map +1 -1
- package/package.json +5 -4
package/dist/index.mjs
CHANGED
|
@@ -1,7 +1,30 @@
|
|
|
1
|
-
import { ASSET_MEDIA_TYPES, EPC_CAPTION, EPC_CAPTION_MODULE, EPC_FRAME, EPC_MAX_BYTES, EPC_MAX_VERSION, EPC_TEXT_FIELDS, E_INVOICE_DATES, E_INVOICE_DECIMALS, E_INVOICE_LINE_CODES, E_INVOICE_LINE_COLUMNS, E_INVOICE_TEXTS, E_INVOICE_TEXT_TERMS, FACTUR_X, FACTUR_X as FACTUR_X$1, FACTUR_X_PROFILES, FACTUR_X_PROFILES as FACTUR_X_PROFILES$1, IMAGE_REFUSAL_CODES, MAX_ASSET_BYTES, MAX_ASSET_BYTES as MAX_ASSET_BYTES$1, MAX_ASSET_SIDE, MAX_ERRORS, MAX_KEY_LENGTH, MAX_QR_BILL_ADDITIONAL_INFORMATION, MAX_SCALE, MAX_SVG_BYTES, MAX_SVG_BYTES as MAX_SVG_BYTES$1, MAX_SVG_COORDINATE, MAX_SVG_DEPTH, MAX_SVG_GROUPS, MAX_SVG_SEGMENTS, QR_BILL_ADDRESS_RULES, QR_BILL_BILLING_RULE, QR_BILL_CURRENCIES, QR_BILL_MESSAGE_RULE, QR_BILL_SIZE, QR_BILL_TOP, SVG_ELEMENTS, SVG_REFUSAL_CODES, buyerIdentificationBreak, checkEInvoiceDate, checkEInvoiceDecimal, checkEInvoiceText, checkEpcAmount, checkEpcField, checkEpcPayload, checkInvoicingPeriod, checkLineCategory, checkQrBillAccount, checkQrBillAdditionalInformation, checkQrBillAmount, checkQrBillAmount as checkQrBillAmount$1, checkQrBillReference, checkQrBillText, checkSvg, checkVatRate, cleanSvg, eInvoiceBlank, en16931Rule, epcBlank, epcCaptionBreak, epcLeastVersion, epcModule, epcPairing, epcPayload, imageHeader, isIdentifier, qrBillPageBreak, qrBillPairing, qrSymbolModules, reportHash, sellerIdentificationBreaks, sniffImage, supplyBreak, validateReport, vatReasonBreak, walkComponents, xmlCharacterBreak } from "@formancy/report-spec";
|
|
1
|
+
import { ASSET_MEDIA_TYPES, BASELINE_SHIFTS, BIDI_CONTROLS, CHART_PALETTE, EPC_CAPTION, EPC_CAPTION_MODULE, EPC_FRAME, EPC_MAX_BYTES, EPC_MAX_BYTES as EPC_MAX_BYTES$1, EPC_MAX_VERSION, EPC_MAX_VERSION as EPC_MAX_VERSION$1, EPC_TEXT_FIELDS, E_INVOICE_DATES, E_INVOICE_DECIMALS, E_INVOICE_KIND_TEXTS, E_INVOICE_LINE_CODES, E_INVOICE_LINE_COLUMNS, E_INVOICE_TEXTS, E_INVOICE_TEXT_TERMS, FACTUR_X, FACTUR_X as FACTUR_X$1, FACTUR_X_PROFILES, FACTUR_X_PROFILES as FACTUR_X_PROFILES$1, FONT_EMBEDDING_REFUSALS, FONT_FACE_KEYS, FONT_FACE_KEYS as FONT_FACE_KEYS$1, FONT_FORMATS, FONT_MEDIA_TYPES, FONT_REFUSAL_CODES, IMAGE_REFUSAL_CODES, MAX_ASSET_BYTES, MAX_ASSET_BYTES as MAX_ASSET_BYTES$1, MAX_ASSET_SIDE, MAX_CHART_SERIES, MAX_ERRORS, MAX_FONT_BYTES, MAX_FONT_BYTES as MAX_FONT_BYTES$1, MAX_FONT_CHARACTERS, MAX_FONT_LINE, MAX_FONT_SIZE, MAX_KEEP_WITH_ROWS, MAX_KEY_LENGTH, MAX_QR_BILL_ADDITIONAL_INFORMATION, MAX_QR_BILL_ADDITIONAL_INFORMATION as MAX_QR_BILL_ADDITIONAL_INFORMATION$1, MAX_SCALE, MAX_SCALE as MAX_SCALE$1, MAX_SVG_BYTES, MAX_SVG_BYTES as MAX_SVG_BYTES$1, MAX_SVG_COORDINATE, MAX_SVG_DEPTH, MAX_SVG_GROUPS, MAX_SVG_SEGMENTS, MIN_FONT_LINE, MIN_FONT_SIZE, PRINTED_BLOCKS, QR_BILL_ADDRESS_RULES, QR_BILL_BILLING_RULE, QR_BILL_CURRENCIES, QR_BILL_CURRENCIES as QR_BILL_CURRENCIES$1, QR_BILL_MESSAGE_RULE, QR_BILL_SIZE, QR_BILL_TOP, SVG_ELEMENTS, SVG_REFUSAL_CODES, UNICODE_VERSION, XRECHNUNG, XRECHNUNG as XRECHNUNG$1, XRECHNUNG_VERSIONS, assetDigest, buyerIdentificationBreak, characterMap, checkEInvoiceDate, checkEInvoiceDecimal, checkEInvoiceText, checkElectronicAddress, checkEpcAmount, checkEpcField, checkEpcPayload, checkInvoicingPeriod, checkLineCategory, checkQrBillAccount, checkQrBillAdditionalInformation, checkQrBillAmount, checkQrBillAmount as checkQrBillAmount$1, checkQrBillReference, checkQrBillText, checkSvg, checkVatRate, cleanSvg, declaredLocales, eInvoiceBlank, en16931Rule, epcBlank, epcCaptionBreak, epcCaptionBreak as epcCaptionBreak$1, epcLeastVersion, epcLeastVersion as epcLeastVersion$1, epcModule, epcPairing, epcPayload, faceKey, imageHeader, isFontMediaType, isFontMediaType as isFontMediaType$1, isFormId, isIdentifier, isLocale, isSchemaHash, qrBillPageBreak, qrBillPageBreak as qrBillPageBreak$1, qrBillPairing, qrSymbolModules, readFont, readFont as readFont$1, refusedCharacter, renderLocale, reportHash, reportIn, sellerIdentificationBreaks, sniffImage, supplyBreak, utf8Length, validateReport, vatReasonBreak, walkComponents, xmlCharacterBreak } from "@formancy/report-spec";
|
|
2
|
+
import { encode } from "uqr";
|
|
2
3
|
import { DEFAULT_BUDGET, DEFAULT_VALUE_LIMITS, Decimal, addDecimal, compareDecimal, decimalFromString, decimalToString, evaluate, fixedCapabilities, multiplyDecimal, roundDecimal, subtractDecimal } from "@formancy/expressions";
|
|
3
|
-
import { compileReport, diagnose, expressionValues } from "@formancy/report-core";
|
|
4
|
-
|
|
4
|
+
import { compileReport, diagnose, expressionValues, seriesType } from "@formancy/report-core";
|
|
5
|
+
// src/document.ts
|
|
6
|
+
/**
|
|
7
|
+
* A run set below or above the line prints at this fraction of its
|
|
8
|
+
* paragraph's size, in the paragraph's face (0061). It is pdfmake 0.3.11's
|
|
9
|
+
* own figure (`TextInlines.js`), stated here as the document's, for any
|
|
10
|
+
* engine: the renderer counts such a run's line at it (`bound.ts`), the
|
|
11
|
+
* corpus reads a run at it as one of its line (`lines.ts`), and a PDF that
|
|
12
|
+
* draws another fails there.
|
|
13
|
+
*/
|
|
14
|
+
const SHIFTED_SIZE = .58;
|
|
15
|
+
/**
|
|
16
|
+
* How far such a run's baseline is moved from its line's, as a fraction of
|
|
17
|
+
* its paragraph's size, up positive: 0.75 of the reduced size up for a run
|
|
18
|
+
* set above the line, 0.35 of it down for one below, 0.435 and 0.203 of the
|
|
19
|
+
* paragraph's size. pdfmake 0.3.11's own figures (`Renderer.js`'s
|
|
20
|
+
* `offsetText`), as `SHIFTED_SIZE` is. The move itself takes no room in
|
|
21
|
+
* the line: what room a line makes for such a run, below or above it, is
|
|
22
|
+
* the engine's.
|
|
23
|
+
*/
|
|
24
|
+
const SHIFTED_RISE = {
|
|
25
|
+
superscript: .75 * SHIFTED_SIZE,
|
|
26
|
+
subscript: -.35 * SHIFTED_SIZE
|
|
27
|
+
};
|
|
5
28
|
const REFUSAL_CODES = [
|
|
6
29
|
"too-many-pages",
|
|
7
30
|
"content-too-large",
|
|
@@ -10,8 +33,9 @@ const REFUSAL_CODES = [
|
|
|
10
33
|
"unsupported"
|
|
11
34
|
];
|
|
12
35
|
/**
|
|
13
|
-
* Why an engine produced no PDF
|
|
14
|
-
*
|
|
36
|
+
* Why an engine produced no file: the PDF renderer no PDF, the Word writer
|
|
37
|
+
* no Word file (0068). An engine refuses rather than truncates: a report
|
|
38
|
+
* that cannot be printed, or written, whole is not, and the caller is told
|
|
15
39
|
* what stopped it. `subject` names the asset, table or block concerned, when
|
|
16
40
|
* the refusal is about one.
|
|
17
41
|
*/
|
|
@@ -25,8 +49,476 @@ var RenderRefusal = class extends Error {
|
|
|
25
49
|
this.subject = subject;
|
|
26
50
|
}
|
|
27
51
|
};
|
|
28
|
-
|
|
29
|
-
|
|
52
|
+
// src/qr-symbol.ts
|
|
53
|
+
/**
|
|
54
|
+
* A QR code's symbol (ISO/IEC 18004), and what refuses one: decided here
|
|
55
|
+
* once, below every engine, so that the PDF renderer and the Word writer
|
|
56
|
+
* draw one symbol of a code and refuse the same codes in the same words
|
|
57
|
+
* (0074). An engine draws the symbol it is handed, and decides nothing of
|
|
58
|
+
* it.
|
|
59
|
+
*
|
|
60
|
+
* uqr encodes the text as UTF-8 in byte mode -- or as digits or
|
|
61
|
+
* upper-case alphanumerics, when that is all it holds -- in the smallest
|
|
62
|
+
* version that holds it at the level asked, and never raises the level to
|
|
63
|
+
* fill a version. An engine encodes a code once, when it checks the
|
|
64
|
+
* document, and draws that symbol, so that what is refused and what is
|
|
65
|
+
* drawn are one decision (0020).
|
|
66
|
+
*
|
|
67
|
+
* The square a code stands in is its `size`, its quiet zone inside it:
|
|
68
|
+
* four modules on every side, white, the symbol between them, so a
|
|
69
|
+
* module is the size over the symbol's modules and eight.
|
|
70
|
+
*/
|
|
71
|
+
/** ISO/IEC 18004's quiet zone: four modules of white on every side of the symbol. */
|
|
72
|
+
const QR_QUIET_ZONE = 4;
|
|
73
|
+
/**
|
|
74
|
+
* The smallest module drawn: 0.4 mm, 1.13 pt. ISO/IEC 18004 leaves the
|
|
75
|
+
* module's size to the application; SIX's Swiss Implementation Guidelines
|
|
76
|
+
* for the QR-bill, version 2.3, section 6.3, require at least 0.4 mm when
|
|
77
|
+
* printing, and that is taken here as what a printer prints and a phone's
|
|
78
|
+
* camera reads reliably. A square too small for its text at that module is
|
|
79
|
+
* refused, naming the size it needs, never drawn finer. It is a borrowed
|
|
80
|
+
* minimum: the same guidelines draw their largest code finer (0020).
|
|
81
|
+
*/
|
|
82
|
+
const QR_MIN_MODULE = 28.8 / 25.4;
|
|
83
|
+
/**
|
|
84
|
+
* The most characters any QR code holds: 7089 digits, version 40 at level
|
|
85
|
+
* L (ISO/IEC 18004, table 7). A character is a code point, as a reader
|
|
86
|
+
* counts one: an emoji is one, though JavaScript holds it in two code
|
|
87
|
+
* units. Longer text fits no version in any mode, and is refused before it
|
|
88
|
+
* is encoded, which takes time and memory with the length of the text;
|
|
89
|
+
* within it, the encoder decides by the bytes the text takes.
|
|
90
|
+
*/
|
|
91
|
+
const QR_MAX_CHARACTERS = 7089;
|
|
92
|
+
const LEVELS = /* @__PURE__ */ new Set([
|
|
93
|
+
"L",
|
|
94
|
+
"M",
|
|
95
|
+
"Q",
|
|
96
|
+
"H"
|
|
97
|
+
]);
|
|
98
|
+
/** The version `symbol` is, from its modules a side (ISO/IEC 18004: 4 a version and 17). */
|
|
99
|
+
function qrSymbolVersion(symbol) {
|
|
100
|
+
return (symbol.modules.length - 17) / 4;
|
|
101
|
+
}
|
|
102
|
+
/** A code point as Unicode writes it: U+5C71. */
|
|
103
|
+
const codePoint = (point) => `U+${point.toString(16).toUpperCase().padStart(4, "0")}`;
|
|
104
|
+
/** The lone half of a surrogate pair `text` holds, as a refusal names it, or undefined: with the u flag, a pair is one character and only a lone half is a surrogate. */
|
|
105
|
+
function loneSurrogate(text) {
|
|
106
|
+
const lone = /\p{Cs}/u.exec(text)?.[0];
|
|
107
|
+
return lone === void 0 ? void 0 : codePoint(lone.charCodeAt(0));
|
|
108
|
+
}
|
|
109
|
+
/** How many code points `text` holds, counted without building them into an array. */
|
|
110
|
+
function codePoints(text) {
|
|
111
|
+
let count = 0;
|
|
112
|
+
for (const _ of text) count++;
|
|
113
|
+
return count;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Refuses a code's square that is no size, as `unsupported`, or wider
|
|
117
|
+
* than the `width` it stands at, as `content-too-large`: a code is a
|
|
118
|
+
* square that stands where an image may. How tall a page's body is, is an
|
|
119
|
+
* engine's to know, and to refuse a square by.
|
|
120
|
+
*/
|
|
121
|
+
function refuseSquare(what, id, size, width) {
|
|
122
|
+
if (!(size > 0 && Number.isFinite(size))) throw new RenderRefusal("unsupported", `${what} is drawn ${size} pt a side`, id);
|
|
123
|
+
if (size > width) throw new RenderRefusal("content-too-large", `${what} is ${size} pt wide; it stands ${width.toFixed(2)} pt wide`, id);
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* `code`'s symbol where it stands `width` points wide, or the refusal
|
|
127
|
+
* that says why it is not drawn: a square of no size, a level or text no
|
|
128
|
+
* encoder can draw, as `unsupported`; a square wider than where it
|
|
129
|
+
* stands, text no version holds, or a square too small for the version
|
|
130
|
+
* that holds it, as `content-too-large`. Each names the code by its id.
|
|
131
|
+
*/
|
|
132
|
+
function qrSymbol(code, width) {
|
|
133
|
+
const { id, text, size, errorCorrection } = code;
|
|
134
|
+
refuseSquare(`the QR code ${id}`, id, size, width);
|
|
135
|
+
if (!LEVELS.has(errorCorrection)) throw new RenderRefusal("unsupported", `the QR code ${id} asks for the error correction level '${String(errorCorrection)}'; the levels are L, M, Q and H`, id);
|
|
136
|
+
if (typeof text !== "string") throw new RenderRefusal("unsupported", `the QR code ${id} encodes something that is not text`, id);
|
|
137
|
+
const lone = loneSurrogate(text);
|
|
138
|
+
if (lone !== void 0) throw new RenderRefusal("unsupported", `the QR code ${id} holds ${lone}, half of a surrogate pair without the other, which has no UTF-8`, id);
|
|
139
|
+
const characters = text.length > 7089 ? codePoints(text) : text.length;
|
|
140
|
+
if (characters > 7089) throw new RenderRefusal("content-too-large", `the QR code ${id} holds ${characters} characters; no QR code holds more than ${QR_MAX_CHARACTERS}`, id);
|
|
141
|
+
const modules = encoded(text, errorCorrection, id);
|
|
142
|
+
refuseFineModules({ modules }, size, `the QR code ${id}`, "its text", id);
|
|
143
|
+
return { modules };
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Refuses `symbol` drawn in a square of `size` with its quiet zone when its
|
|
147
|
+
* modules would be finer than 0.4 mm, naming the square `holds` needs:
|
|
148
|
+
* the floor every code drawn with a quiet zone is held to (0020).
|
|
149
|
+
*/
|
|
150
|
+
function refuseFineModules(symbol, size, what, holds, id) {
|
|
151
|
+
const count = symbol.modules.length + 8;
|
|
152
|
+
const smallest = count * QR_MIN_MODULE;
|
|
153
|
+
if (size < smallest) throw new RenderRefusal("content-too-large", `${what} is ${size} pt a side, which makes the ${count} modules across its symbol and quiet zone ${(size / count).toFixed(2)} pt each; the smallest module is 0.4 mm (${QR_MIN_MODULE.toFixed(2)} pt), so ${holds} needs a square of at least ${(Math.ceil(smallest * 100) / 100).toFixed(2)} pt`, id);
|
|
154
|
+
}
|
|
155
|
+
/** Whether `error` is what uqr 0.1.3 says of text no version holds, and says of nothing else. */
|
|
156
|
+
function tooLong(error) {
|
|
157
|
+
return error instanceof RangeError && error.message === "Data too long";
|
|
158
|
+
}
|
|
159
|
+
/** The symbol uqr encodes, without its quiet zone; refused when no version holds the text. */
|
|
160
|
+
function encoded(text, level, id) {
|
|
161
|
+
try {
|
|
162
|
+
return encode(text, {
|
|
163
|
+
ecc: level,
|
|
164
|
+
border: 0,
|
|
165
|
+
boostEcc: false
|
|
166
|
+
}).data;
|
|
167
|
+
} catch (error) {
|
|
168
|
+
if (!tooLong(error)) throw error;
|
|
169
|
+
throw new RenderRefusal("content-too-large", `the QR code ${id} holds ${utf8Length(text)} bytes of text, more than a QR code at level ${level} holds`, id);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* The most characters a Swiss QR Code holds, its line feeds included: 997
|
|
174
|
+
* (IG 6.2), which is what version 25 holds at level M in byte mode. A
|
|
175
|
+
* payload of more is refused before it is encoded.
|
|
176
|
+
*/
|
|
177
|
+
const SWISS_QR_MAX_CHARACTERS = 997;
|
|
178
|
+
/** The largest version SIX allows (IG 6.2): 117 modules a side. */
|
|
179
|
+
const SWISS_MAX_VERSION = 25;
|
|
180
|
+
/**
|
|
181
|
+
* A QR-bill's Swiss QR Code (IG 6), or the refusal that says why it is not
|
|
182
|
+
* drawn: its payload as UTF-8 at level M (6.1), in the smallest version that
|
|
183
|
+
* holds it (6.4), and never past version 25 (6.2). A payload of more than
|
|
184
|
+
* 997 characters is refused unencoded; one whose UTF-8 is more than version
|
|
185
|
+
* 25 holds, once the encoder finds none does. Every refusal names the
|
|
186
|
+
* payment part, and none quotes the payload: it holds names and accounts.
|
|
187
|
+
*
|
|
188
|
+
* SIX prints the code at 46 mm whatever its version, so a module is 46 mm
|
|
189
|
+
* over the symbol's modules, 0.393 mm at version 25: finer than the 0.4 mm
|
|
190
|
+
* a `qrCode` is held to, which is SIX's own figure for print (6.3). SIX's
|
|
191
|
+
* size is drawn, not the floor (0020's generic minimum does not apply).
|
|
192
|
+
*/
|
|
193
|
+
function swissSymbol(payload) {
|
|
194
|
+
const refuse = (code, what) => new RenderRefusal(code, `the payment part's Swiss QR Code ${what}`, "paymentPart");
|
|
195
|
+
if (typeof payload !== "string") throw refuse("unsupported", "holds something that is not text");
|
|
196
|
+
const lone = loneSurrogate(payload);
|
|
197
|
+
if (lone !== void 0) throw refuse("unsupported", `holds ${lone}, half of a surrogate pair without the other, which has no UTF-8`);
|
|
198
|
+
const characters = payload.length > 997 ? codePoints(payload) : payload.length;
|
|
199
|
+
if (characters > 997) throw refuse("content-too-large", `holds ${characters} characters; SIX allows 997`);
|
|
200
|
+
try {
|
|
201
|
+
return { modules: encode(payload, {
|
|
202
|
+
ecc: "M",
|
|
203
|
+
border: 0,
|
|
204
|
+
boostEcc: false,
|
|
205
|
+
maxVersion: SWISS_MAX_VERSION
|
|
206
|
+
}).data };
|
|
207
|
+
} catch (error) {
|
|
208
|
+
if (!tooLong(error)) throw error;
|
|
209
|
+
throw refuse("content-too-large", `holds ${utf8Length(payload)} bytes, more than version ${SWISS_MAX_VERSION} holds at level M, the largest code SIX allows`);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
// src/bill-check.ts
|
|
213
|
+
/** The longest value a slip prints: a line of the additional information, which the message and the billing information share (IG Table 8). An address line is shorter. */
|
|
214
|
+
const MAX_VALUE = MAX_QR_BILL_ADDITIONAL_INFORMATION$1;
|
|
215
|
+
/** An amount as the slip prints it: a space between thousands, a point and two decimals (IG 3.5.3). Which amounts a bill pays is report-spec's `checkQrBillAmount`. */
|
|
216
|
+
const AMOUNT = /^\d{1,3}(?: \d{3})*\.\d{2}$/;
|
|
217
|
+
/** The lines an address prints: its name, its street and building number, its postal code and town (IG 3.5.4). */
|
|
218
|
+
const ADDRESS_LINES = 3;
|
|
219
|
+
/** The message and the billing information, each on its line. */
|
|
220
|
+
const ADDITIONAL_LINES = 2;
|
|
221
|
+
function refuse$5(what) {
|
|
222
|
+
return new RenderRefusal("unsupported", `the payment part's ${what}`, "paymentPart");
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* Refuses `page` for a slip by report-spec's rule, the one the editor
|
|
226
|
+
* refuses it by (`qrBillPageBreak`): a page that is not A4 in portrait,
|
|
227
|
+
* which has no foot the slip spans, and one whose top margin, with `above`
|
|
228
|
+
* printed under it, reaches into the slip, which no layout can undo.
|
|
229
|
+
*/
|
|
230
|
+
function refuseBillPage(page, above = 0) {
|
|
231
|
+
const broken = qrBillPageBreak$1(page, above);
|
|
232
|
+
if (broken !== void 0) throw new RenderRefusal(broken.field === "page" ? "unsupported" : "content-too-large", broken.rule, "paymentPart");
|
|
233
|
+
}
|
|
234
|
+
/** Refuses what no engine draws of `bill` on `page`, the top margin alone before anything above the slip is measured, and encodes its code. */
|
|
235
|
+
function checkQrBill(bill, page, undrawn) {
|
|
236
|
+
const { kind } = bill;
|
|
237
|
+
if (kind !== "swissQrBill") throw new RenderRefusal("unsupported", `the payment part is a '${String(kind)}', which a report does not know`, "paymentPart");
|
|
238
|
+
refuseBillPage(page);
|
|
239
|
+
const text = (value, what, face) => {
|
|
240
|
+
if (typeof value !== "string") throw refuse$5(`${what} holds something that is not text`);
|
|
241
|
+
if (value.length > MAX_VALUE) throw refuse$5(`${what} holds a value longer than any SIX allows, ${MAX_VALUE} characters`);
|
|
242
|
+
const missing = undrawn(value, face);
|
|
243
|
+
if (missing !== void 0) throw refuse$5(`${what} ${missing}`);
|
|
244
|
+
};
|
|
245
|
+
const lines = (value, what, most, least) => {
|
|
246
|
+
if (!Array.isArray(value)) throw refuse$5(`${what} is not lines of text`);
|
|
247
|
+
if (value.length > most || value.length < least) throw refuse$5(`${what} has the wrong number of lines: a slip prints ${least} to ${most}`);
|
|
248
|
+
for (const line of value) text(line, what, "normal");
|
|
249
|
+
};
|
|
250
|
+
const headings = bill.headings ?? {};
|
|
251
|
+
for (const [key, face] of Object.entries(BILL_HEADING_FACES)) text(headings[key], `heading '${key}'`, face);
|
|
252
|
+
text(bill.account, "account", "normal");
|
|
253
|
+
lines(bill.creditor, "creditor's address", ADDRESS_LINES, 2);
|
|
254
|
+
if (bill.reference !== null) text(bill.reference, "reference", "normal");
|
|
255
|
+
lines(bill.additionalInformation, "additional information", ADDITIONAL_LINES, 0);
|
|
256
|
+
if (bill.debtor !== null) lines(bill.debtor, "debtor's address", ADDRESS_LINES, 2);
|
|
257
|
+
if (!QR_BILL_CURRENCIES$1.includes(bill.currency)) throw refuse$5(`currency is not one SIX allows: ${QR_BILL_CURRENCIES$1.join(" or ")}`);
|
|
258
|
+
text(bill.currency, "currency", "normal");
|
|
259
|
+
if (bill.amount !== null) {
|
|
260
|
+
if (typeof bill.amount !== "string" || !AMOUNT.test(bill.amount)) throw refuse$5("amount is not written as the slip prints one: its thousands apart and two decimals");
|
|
261
|
+
const paid = checkQrBillAmount$1(bill.amount.replaceAll(" ", ""));
|
|
262
|
+
if (!paid.ok) throw refuse$5(`amount ${paid.rule}`);
|
|
263
|
+
text(bill.amount, "amount", "normal");
|
|
264
|
+
}
|
|
265
|
+
return {
|
|
266
|
+
bill,
|
|
267
|
+
symbol: swissSymbol(bill.payload)
|
|
268
|
+
};
|
|
269
|
+
}
|
|
270
|
+
/**
|
|
271
|
+
* The face each heading of a slip prints in, which an engine's glyphs
|
|
272
|
+
* and widths are held to: `separate` regular, the rest bold. A table
|
|
273
|
+
* over every heading, so that one added to the document's
|
|
274
|
+
* `QrBillHeadings` without its face does not compile, rather than going
|
|
275
|
+
* unchecked.
|
|
276
|
+
*/
|
|
277
|
+
const BILL_HEADING_FACES = {
|
|
278
|
+
paymentPart: "bold",
|
|
279
|
+
receipt: "bold",
|
|
280
|
+
account: "bold",
|
|
281
|
+
reference: "bold",
|
|
282
|
+
additionalInformation: "bold",
|
|
283
|
+
payableBy: "bold",
|
|
284
|
+
currency: "bold",
|
|
285
|
+
amount: "bold",
|
|
286
|
+
acceptancePoint: "bold",
|
|
287
|
+
separate: "normal"
|
|
288
|
+
};
|
|
289
|
+
// src/slip-print.ts
|
|
290
|
+
/**
|
|
291
|
+
* Where SIX puts each part of a QR-bill's slip, the receipt and the
|
|
292
|
+
* payment part, and at what size: stated here once, below every engine,
|
|
293
|
+
* so that the PDF renderer and the Word writer lay one slip out (0075).
|
|
294
|
+
* An engine draws by these, in its own way, and decides none of them.
|
|
295
|
+
* Nothing here formats a value: the runtime hands every text as it
|
|
296
|
+
* prints (`QrBill`).
|
|
297
|
+
*
|
|
298
|
+
* The numbers are SIX's: "IG" is the Swiss Implementation Guidelines for
|
|
299
|
+
* the QR-bill, version 2.4, and "SG" its Style Guide, version 1.1, whose
|
|
300
|
+
* page 6 gives every section's place and size in millimetres, page 7 the
|
|
301
|
+
* blank fields', and page 15 the sizes and line spacing. What neither gives
|
|
302
|
+
* is taken from manuelbl's SwissQRBill generator, which matches SG 7 drawn
|
|
303
|
+
* to scale: where the blank amount fields stand, and 2 pt between a
|
|
304
|
+
* debtor's heading and its field.
|
|
305
|
+
*
|
|
306
|
+
* Positions are in points from the slip's top left. The slip is
|
|
307
|
+
* report-spec's `QR_BILL_SIZE`, 210 by 105 mm.
|
|
308
|
+
*/
|
|
309
|
+
/** Millimetres in points. */
|
|
310
|
+
const mm = (value) => value * 72 / 25.4;
|
|
311
|
+
/** The receipt is 62 mm wide, and the payment part starts there (IG 3.3). */
|
|
312
|
+
const SLIP_RECEIPT_WIDTH = mm(62);
|
|
313
|
+
/**
|
|
314
|
+
* "Separate before paying in", above the line along the slip's top, outside
|
|
315
|
+
* the payment part, which a slip may print instead of a scissors symbol
|
|
316
|
+
* (IG 3.7, SG 4): 8 pt, the payment part's heading size, centred over the
|
|
317
|
+
* slip, its baseline 3 pt above the line. SIX gives neither.
|
|
318
|
+
*/
|
|
319
|
+
const SLIP_INSTRUCTION = {
|
|
320
|
+
size: 8,
|
|
321
|
+
gap: 3
|
|
322
|
+
};
|
|
323
|
+
/** The lines the slip is cut along (IG 3.7): solid, half a point. SIX gives no weight. */
|
|
324
|
+
const SLIP_SEPARATION = .5;
|
|
325
|
+
/** A blank field's corner marks: 3 mm arms, 0.75 pt (IG 3.5.3, SG 7). */
|
|
326
|
+
const SLIP_CORNER = {
|
|
327
|
+
arm: mm(3),
|
|
328
|
+
weight: .75
|
|
329
|
+
};
|
|
330
|
+
/** Between a debtor's heading's line and its blank field. */
|
|
331
|
+
const SLIP_FIELD_GAP = 2;
|
|
332
|
+
/** The titles, "Receipt" and "Payment part": 11 pt bold (IG 3.5.1, 3.6.1). */
|
|
333
|
+
const SLIP_TITLE = 11;
|
|
334
|
+
/** The receipt, 62 by 105 mm (SG 6): 5 mm margins, its title, 56 mm of information, 14 of amount, 18 of acceptance point. */
|
|
335
|
+
const SLIP_RECEIPT = {
|
|
336
|
+
name: "the receipt",
|
|
337
|
+
lettering: {
|
|
338
|
+
heading: 6,
|
|
339
|
+
value: 8,
|
|
340
|
+
pitch: 9
|
|
341
|
+
},
|
|
342
|
+
title: {
|
|
343
|
+
x: mm(5),
|
|
344
|
+
top: mm(5),
|
|
345
|
+
heading: "receipt"
|
|
346
|
+
},
|
|
347
|
+
additionalInformation: false,
|
|
348
|
+
information: {
|
|
349
|
+
x: mm(5),
|
|
350
|
+
top: mm(12),
|
|
351
|
+
width: mm(52),
|
|
352
|
+
height: mm(56),
|
|
353
|
+
millimetres: 56
|
|
354
|
+
},
|
|
355
|
+
amount: {
|
|
356
|
+
x: mm(5),
|
|
357
|
+
top: mm(68),
|
|
358
|
+
indent: mm(12),
|
|
359
|
+
pitch: 11
|
|
360
|
+
},
|
|
361
|
+
amountField: {
|
|
362
|
+
x: mm(27),
|
|
363
|
+
width: mm(30),
|
|
364
|
+
height: mm(10),
|
|
365
|
+
onAmountLine: false
|
|
366
|
+
},
|
|
367
|
+
debtorField: {
|
|
368
|
+
width: mm(52),
|
|
369
|
+
height: mm(20)
|
|
370
|
+
}
|
|
371
|
+
};
|
|
372
|
+
/** The payment part, 148 by 105 mm (SG 6): the code's 46 mm and the amount's 22 mm beside 87 by 85 mm of information. */
|
|
373
|
+
const SLIP_PAYMENT_PART = {
|
|
374
|
+
name: "the payment part",
|
|
375
|
+
lettering: {
|
|
376
|
+
heading: 8,
|
|
377
|
+
value: 10,
|
|
378
|
+
pitch: 11
|
|
379
|
+
},
|
|
380
|
+
title: {
|
|
381
|
+
x: SLIP_RECEIPT_WIDTH + mm(5),
|
|
382
|
+
top: mm(5),
|
|
383
|
+
heading: "paymentPart"
|
|
384
|
+
},
|
|
385
|
+
additionalInformation: true,
|
|
386
|
+
information: {
|
|
387
|
+
x: SLIP_RECEIPT_WIDTH + mm(56),
|
|
388
|
+
top: mm(5),
|
|
389
|
+
width: mm(87),
|
|
390
|
+
height: mm(85),
|
|
391
|
+
millimetres: 85
|
|
392
|
+
},
|
|
393
|
+
amount: {
|
|
394
|
+
x: SLIP_RECEIPT_WIDTH + mm(5),
|
|
395
|
+
top: mm(68),
|
|
396
|
+
indent: mm(15),
|
|
397
|
+
pitch: 13
|
|
398
|
+
},
|
|
399
|
+
amountField: {
|
|
400
|
+
x: SLIP_RECEIPT_WIDTH + mm(16),
|
|
401
|
+
width: mm(40),
|
|
402
|
+
height: mm(15),
|
|
403
|
+
onAmountLine: true
|
|
404
|
+
},
|
|
405
|
+
debtorField: {
|
|
406
|
+
width: mm(65),
|
|
407
|
+
height: mm(25)
|
|
408
|
+
}
|
|
409
|
+
};
|
|
410
|
+
/** The Swiss QR Code's place: 5 mm in, below the title section and 5 mm of quiet zone (IG 3.5.2, SG 6). */
|
|
411
|
+
const SLIP_CODE = {
|
|
412
|
+
x: SLIP_RECEIPT_WIDTH + mm(5),
|
|
413
|
+
top: mm(17)
|
|
414
|
+
};
|
|
415
|
+
/** The acceptance point, right-aligned to the receipt's text at the top of its section (IG 3.6.4, SG 6). */
|
|
416
|
+
const SLIP_ACCEPTANCE = {
|
|
417
|
+
right: mm(57),
|
|
418
|
+
top: mm(82)
|
|
419
|
+
};
|
|
420
|
+
/** The blocks a part's information holds, in SIX's order (IG 3.5.4, 3.6.2): the additional information in the payment part alone. */
|
|
421
|
+
function slipBlocks(bill, part) {
|
|
422
|
+
const { headings } = bill;
|
|
423
|
+
return [
|
|
424
|
+
{
|
|
425
|
+
heading: headings.account,
|
|
426
|
+
values: [bill.account, ...bill.creditor]
|
|
427
|
+
},
|
|
428
|
+
...bill.reference === null ? [] : [{
|
|
429
|
+
heading: headings.reference,
|
|
430
|
+
values: [bill.reference]
|
|
431
|
+
}],
|
|
432
|
+
...part.additionalInformation && bill.additionalInformation.length > 0 ? [{
|
|
433
|
+
heading: headings.additionalInformation,
|
|
434
|
+
values: bill.additionalInformation
|
|
435
|
+
}] : [],
|
|
436
|
+
bill.debtor === null ? {
|
|
437
|
+
heading: headings.payableBy,
|
|
438
|
+
field: part.debtorField
|
|
439
|
+
} : {
|
|
440
|
+
heading: headings.payableBy,
|
|
441
|
+
values: bill.debtor
|
|
442
|
+
}
|
|
443
|
+
];
|
|
444
|
+
}
|
|
445
|
+
/** A Swiss QR Code's side, without a quiet zone, whatever its version (IG 6.4): its modules are this over theirs. */
|
|
446
|
+
const SWISS_CODE_SIZE = mm(46);
|
|
447
|
+
/**
|
|
448
|
+
* The Swiss cross over the code's centre (IG 6.4.2), in millimetres: SIX's
|
|
449
|
+
* logo, as schoero's generator draws it, is a white square of 7 mm, a black
|
|
450
|
+
* square of 6 mm on it, and a white cross on that, its arms 7/6 mm wide and
|
|
451
|
+
* 35/9 mm from end to end. manuelbl's draws the same black square and cross
|
|
452
|
+
* on modules it clears, rather than on a white square.
|
|
453
|
+
*/
|
|
454
|
+
const SWISS_CROSS = {
|
|
455
|
+
white: 7,
|
|
456
|
+
black: 6,
|
|
457
|
+
arm: 7 / 6,
|
|
458
|
+
span: 35 / 9
|
|
459
|
+
};
|
|
460
|
+
// src/epc-symbol.ts
|
|
461
|
+
/**
|
|
462
|
+
* An EPC QR code's symbol (EPC069-12 v3.1), and what refuses one: decided
|
|
463
|
+
* here once, below every engine, as a QR code's is (`qr-symbol.ts`, 0074).
|
|
464
|
+
* Its payload as UTF-8 at level M, in a symbol of version 13 at most,
|
|
465
|
+
* which an engine encodes once, when it checks the document.
|
|
466
|
+
*
|
|
467
|
+
* Without a caption it is a QR code's symbol, in a square with a quiet
|
|
468
|
+
* zone of four modules inside it, held to the 0.4 mm floor. With
|
|
469
|
+
* Austria's caption it is the PSA's (PSA v3.0 5), by report-spec's
|
|
470
|
+
* `epc-print.ts`, the one place those rules are decided: modules of 15 to
|
|
471
|
+
* 20 mil, and a symbol as tall as the caption beside it. In a square the
|
|
472
|
+
* author sized, the version that just holds a payload may have modules
|
|
473
|
+
* over 20 mil: the symbol is the least version that prints there
|
|
474
|
+
* (`epcLeastVersion`), a larger symbol of the same payload at the same
|
|
475
|
+
* level.
|
|
476
|
+
*
|
|
477
|
+
* Every refusal names the code by its id, and none quotes the payload: it
|
|
478
|
+
* holds a name and an account.
|
|
479
|
+
*/
|
|
480
|
+
/**
|
|
481
|
+
* `code`'s symbol where it stands `width` points wide, or the refusal
|
|
482
|
+
* that says why it is not drawn: a square of no size, a caption no engine
|
|
483
|
+
* knows, or a payload that is not text, as `unsupported`; a square wider
|
|
484
|
+
* than where it stands, a payload past 331 bytes, refused unencoded when
|
|
485
|
+
* it is longer than that in characters, or modules the square cannot
|
|
486
|
+
* print, as `content-too-large`.
|
|
487
|
+
*/
|
|
488
|
+
function epcSymbol(code, width) {
|
|
489
|
+
const { id, payload, size, caption } = code;
|
|
490
|
+
const what = `the EPC QR code ${id}`;
|
|
491
|
+
refuseSquare(what, id, size, width);
|
|
492
|
+
if (caption !== void 0 && caption !== "zahlenMitCode") throw new RenderRefusal("unsupported", `${what} asks for the caption '${String(caption)}', which a report does not know`, id);
|
|
493
|
+
if (typeof payload !== "string") throw new RenderRefusal("unsupported", `${what} holds something that is not text`, id);
|
|
494
|
+
const lone = loneSurrogate(payload);
|
|
495
|
+
if (lone !== void 0) throw new RenderRefusal("unsupported", `${what} holds ${lone}, half of a surrogate pair without the other, which has no UTF-8`, id);
|
|
496
|
+
if (payload.length > EPC_MAX_BYTES$1) throw new RenderRefusal("content-too-large", `${what} holds more than the ${EPC_MAX_BYTES$1} bytes the EPC allows`, id);
|
|
497
|
+
const square = caption === void 0 ? void 0 : epcCaptionBreak$1(size);
|
|
498
|
+
if (square !== void 0) throw new RenderRefusal("content-too-large", `${what} cannot be printed as the PSA prints it: ${square}`, id);
|
|
499
|
+
const least = epcLeastVersion$1(size, caption);
|
|
500
|
+
let symbol;
|
|
501
|
+
try {
|
|
502
|
+
symbol = { modules: encode(payload, {
|
|
503
|
+
ecc: "M",
|
|
504
|
+
border: 0,
|
|
505
|
+
boostEcc: false,
|
|
506
|
+
minVersion: least,
|
|
507
|
+
maxVersion: EPC_MAX_VERSION$1
|
|
508
|
+
}).data };
|
|
509
|
+
} catch (error) {
|
|
510
|
+
if (!tooLong(error)) throw error;
|
|
511
|
+
throw new RenderRefusal("content-too-large", `${what} holds ${utf8Length(payload)} bytes, more than version ${EPC_MAX_VERSION$1} holds at level M, the largest code the EPC allows`, id);
|
|
512
|
+
}
|
|
513
|
+
if (caption === void 0) {
|
|
514
|
+
refuseFineModules(symbol, size, what, "its payload", id);
|
|
515
|
+
return symbol;
|
|
516
|
+
}
|
|
517
|
+
const version = epcCaptionBreak$1(size, qrSymbolVersion(symbol));
|
|
518
|
+
if (version !== void 0) throw new RenderRefusal("content-too-large", `${what} cannot be printed as the PSA prints it: ${version}`, id);
|
|
519
|
+
return symbol;
|
|
520
|
+
}
|
|
521
|
+
// src/input.ts
|
|
30
522
|
/** Stands in a read list where an item could not be read; its problem says why. */
|
|
31
523
|
const UNREADABLE = Symbol("unreadable");
|
|
32
524
|
const ACCESSOR = "a getter or setter is not data, and is not run";
|
|
@@ -133,8 +625,7 @@ function readList(value, max) {
|
|
|
133
625
|
};
|
|
134
626
|
}
|
|
135
627
|
}
|
|
136
|
-
|
|
137
|
-
//#region src/values.ts
|
|
628
|
+
// src/values.ts
|
|
138
629
|
/**
|
|
139
630
|
* One value read as the type a dataset field or a parameter declares, and
|
|
140
631
|
* held as the runtime computes with it.
|
|
@@ -276,7 +767,7 @@ function readDecimal(raw, scale) {
|
|
|
276
767
|
if (typeof raw !== "string") return wrongType("a decimal, written as a string of its exact digits", raw);
|
|
277
768
|
if (raw.length > MAX_STRING_LENGTH || !DECIMAL.test(raw)) return refused$1("invalid-decimal", "a decimal is written as its digits, with \".\" before any places, \"-\" for a negative one, no leading zero and no \"-0\"");
|
|
278
769
|
const places = raw.length - raw.indexOf(".") - 1;
|
|
279
|
-
const allowed = scale ?? MAX_SCALE;
|
|
770
|
+
const allowed = scale ?? MAX_SCALE$1;
|
|
280
771
|
if (raw.includes(".") && places > allowed) return refused$1("decimal-scale", `a decimal with ${places} places, more than the ${allowed} ${scale === void 0 ? "a decimal holds" : "the field declares"}; rounding it here would print another number than the source holds`);
|
|
281
772
|
return {
|
|
282
773
|
ok: true,
|
|
@@ -380,8 +871,7 @@ const READERS = {
|
|
|
380
871
|
instant: readInstant,
|
|
381
872
|
image: readImage
|
|
382
873
|
};
|
|
383
|
-
|
|
384
|
-
//#region src/data-errors.ts
|
|
874
|
+
// src/data-errors.ts
|
|
385
875
|
/**
|
|
386
876
|
* What the runtime can say is wrong with the data and parameters it is
|
|
387
877
|
* given, as stable codes, and the bounded list one check collects.
|
|
@@ -405,6 +895,8 @@ const DATA_ERROR_CODES = [
|
|
|
405
895
|
"completeness-mismatch",
|
|
406
896
|
"invalid-completeness",
|
|
407
897
|
"invalid-consistency",
|
|
898
|
+
"invalid-source",
|
|
899
|
+
"labels-locale-differs",
|
|
408
900
|
"missing-parameter",
|
|
409
901
|
"undeclared-parameter",
|
|
410
902
|
"too-many-errors"
|
|
@@ -458,8 +950,245 @@ function isLongKey(key) {
|
|
|
458
950
|
function keyAt(path, key) {
|
|
459
951
|
return isLongKey(key) ? path : at(path, key);
|
|
460
952
|
}
|
|
461
|
-
|
|
462
|
-
|
|
953
|
+
// src/languages.ts
|
|
954
|
+
/**
|
|
955
|
+
* The locale a render of `report` prints in -- `requested`, or the
|
|
956
|
+
* report's own where nothing is requested -- or the error that refuses
|
|
957
|
+
* it: `undeclared-locale`, at the options, naming the locales the report
|
|
958
|
+
* is written in. `requested` is the host's, whatever its type says.
|
|
959
|
+
*/
|
|
960
|
+
function printLocale(report, requested) {
|
|
961
|
+
const choice = requested === void 0 || typeof requested === "string" ? renderLocale(report, requested) : {
|
|
962
|
+
ok: false,
|
|
963
|
+
message: `a render names its locale as a tag; this report is written in ${declaredLocales(report).join(", ")}`
|
|
964
|
+
};
|
|
965
|
+
return choice.ok ? choice : {
|
|
966
|
+
ok: false,
|
|
967
|
+
error: {
|
|
968
|
+
stage: "resolve",
|
|
969
|
+
code: "undeclared-locale",
|
|
970
|
+
path: "",
|
|
971
|
+
message: choice.message
|
|
972
|
+
}
|
|
973
|
+
};
|
|
974
|
+
}
|
|
975
|
+
/** Whether `diagnostic` refuses a render in `locale`: an error that names no language refuses every render, one that names a language a render in it alone. */
|
|
976
|
+
const refuses = (diagnostic, locale) => diagnostic.severity === "error" && (diagnostic.language === void 0 || diagnostic.language === locale);
|
|
977
|
+
/** Where `locale`, one `report` is written in, stands in it: the report's own at `/locale`, a translation's at the translation that declares it. */
|
|
978
|
+
function localePath(report, locale) {
|
|
979
|
+
const index = (report.translations ?? []).findIndex((translation) => translation.locale === locale);
|
|
980
|
+
return index === -1 ? "/locale" : `/translations/${index}/locale`;
|
|
981
|
+
}
|
|
982
|
+
/**
|
|
983
|
+
* Reports form data whose labels were read in another locale than
|
|
984
|
+
* `printed`, the one the render prints in: a source that names none was
|
|
985
|
+
* read in `own`, the report's own. Data of no form, or of Formancy Data,
|
|
986
|
+
* holds no label of a form's.
|
|
987
|
+
*/
|
|
988
|
+
function checkLabels(errors, source, own, printed) {
|
|
989
|
+
if (source?.provider !== "formancy-form" || (source.locale ?? own) === printed) return;
|
|
990
|
+
const read = source.locale === void 0 ? "the report's own locale" : "another locale";
|
|
991
|
+
errors.report(source.locale === void 0 ? "/source" : "/source/locale", "labels-locale-differs", `the form's labels were read in ${read}, and this render prints in ${printed}: read them again in ${printed}, or render in the locale they were read in, since labels are never printed in a language the render does not name`);
|
|
992
|
+
}
|
|
993
|
+
// src/source.ts
|
|
994
|
+
/**
|
|
995
|
+
* Where a report's data came from, as its provider names it, and the check
|
|
996
|
+
* that admits it (0041). The render's manifest carries it, so that a PDF
|
|
997
|
+
* kept for years can be traced to what collected its data: plan 13.2's
|
|
998
|
+
* identity and provenance, as far as identifiers say them.
|
|
999
|
+
*
|
|
1000
|
+
* - **A formancy form**: each submission the data was read from, with the
|
|
1001
|
+
* form's id and the `schemaHash` of the version that collected it -- one
|
|
1002
|
+
* for a report of one submission, one a row for a report of many,
|
|
1003
|
+
* whatever versions they answered. The form's id is a submission's too,
|
|
1004
|
+
* since formancy lets a new version change it. And the locale a
|
|
1005
|
+
* choice's labels were read in, where it is not the report's own (0063).
|
|
1006
|
+
* - **Formancy Data**: the published form each dataset was read from. Never
|
|
1007
|
+
* a record's token, which spells the record's key (formancy.ai-data
|
|
1008
|
+
* 0012), and a key can be the customer's own data.
|
|
1009
|
+
*
|
|
1010
|
+
* A source names identifiers and nothing else. It is held to their syntax,
|
|
1011
|
+
* bounded in count and length, and refused rather than cut short; a key
|
|
1012
|
+
* its shape does not name is refused by its container's path, since the
|
|
1013
|
+
* key may be the answer, and a provider the runtime does not know is
|
|
1014
|
+
* refused, never read as one it does. The syntax keeps out a space, an
|
|
1015
|
+
* `@`, a `/`, a `:`, a letter beyond ASCII and anything past 128
|
|
1016
|
+
* characters; it cannot tell a single word, a personal number or a digest
|
|
1017
|
+
* of an answer from an identifier, which 0041 says is its cost. Whether a
|
|
1018
|
+
* source is true -- that the host read what it names -- is the provider's
|
|
1019
|
+
* statement, as its completeness is; the runtime holds its form.
|
|
1020
|
+
*
|
|
1021
|
+
* As `data.ts` does, the check reports every problem and returns what it
|
|
1022
|
+
* read; `checkData` refuses the data when any was reported, so what a
|
|
1023
|
+
* render keeps is a source read whole.
|
|
1024
|
+
*/
|
|
1025
|
+
/** Who handed the data over: a formancy form's submissions, or Formancy Data's forms. */
|
|
1026
|
+
const SOURCE_PROVIDERS = ["formancy-form", "formancy-data"];
|
|
1027
|
+
const ROLES = ["header", "detail"];
|
|
1028
|
+
/** The characters a form's id and a submission's may hold, as a refusal says them. */
|
|
1029
|
+
const ID_SYNTAX = "letters, digits, '.', '_' and '-', starting with a letter or a digit, at most 128 characters";
|
|
1030
|
+
/**
|
|
1031
|
+
* A submission's id. The same characters as a form's id, by choice and not
|
|
1032
|
+
* by rule: report-spec's `FORM_ID` is formancy's syntax for a form, which
|
|
1033
|
+
* formancy may change, and a submission's id is the host's, which formancy's
|
|
1034
|
+
* server makes as a UUID. A UUID, a ULID or a number fits, and so does a
|
|
1035
|
+
* single word or a personal number, which a syntax cannot tell from an id.
|
|
1036
|
+
*/
|
|
1037
|
+
const SUBMISSION_ID = /^[A-Za-z0-9][A-Za-z0-9._-]{0,127}$/;
|
|
1038
|
+
/**
|
|
1039
|
+
* Whether `value` is a submission's id as a source names one: what the form
|
|
1040
|
+
* bridge holds an id to before it reads a submission, so that the two
|
|
1041
|
+
* cannot read an id differently (0042).
|
|
1042
|
+
*/
|
|
1043
|
+
function isSubmissionId(value) {
|
|
1044
|
+
return typeof value === "string" && SUBMISSION_ID.test(value);
|
|
1045
|
+
}
|
|
1046
|
+
/** What `isSubmissionId` accepts, in the words a refusal says it: the form bridge says them when it refuses an id, so the two cannot describe one rule two ways. */
|
|
1047
|
+
const SUBMISSION_ID_SYNTAX = ID_SYNTAX;
|
|
1048
|
+
const FORM_ID = `a form's id as formancy writes one: ${ID_SYNTAX}`;
|
|
1049
|
+
const NOTHING_ELSE = "and nothing else: a source names identifiers, and no answer rides into the manifest a PDF is kept with";
|
|
1050
|
+
const SUBMISSION = "a submission is { \"id\": its id, \"form\": the form's id, \"formHash\": the schemaHash of the version that collected it }";
|
|
1051
|
+
/** What each provider's source names besides its provider. */
|
|
1052
|
+
const NAMES = {
|
|
1053
|
+
"formancy-form": ["submissions", "locale"],
|
|
1054
|
+
"formancy-data": ["forms"]
|
|
1055
|
+
};
|
|
1056
|
+
/** What each provider's source names, as a refusal says it. */
|
|
1057
|
+
const SHAPES = {
|
|
1058
|
+
"formancy-form": "a formancy-form source names submissions, and the locale its labels were read in where that is not the report's own",
|
|
1059
|
+
"formancy-data": "a formancy-data source names forms"
|
|
1060
|
+
};
|
|
1061
|
+
/** A container that could not be read, as a source's error: what is not data stays `not-data`. */
|
|
1062
|
+
const container = (code) => code === "not-data" ? "not-data" : "invalid-source";
|
|
1063
|
+
/**
|
|
1064
|
+
* Reports a record's properties that are not data: a key of `known` where
|
|
1065
|
+
* it stands, any other by the record's own path, since a key the shape
|
|
1066
|
+
* does not name may be the answer. Returns the known keys reported.
|
|
1067
|
+
*/
|
|
1068
|
+
function notData(errors, path, problems, known) {
|
|
1069
|
+
const keys = /* @__PURE__ */ new Set();
|
|
1070
|
+
for (const { key, message } of problems) {
|
|
1071
|
+
const named = key !== void 0 && known.includes(key);
|
|
1072
|
+
if (named) keys.add(key);
|
|
1073
|
+
errors.report(named ? at(path, key) : path, "not-data", message);
|
|
1074
|
+
}
|
|
1075
|
+
return keys;
|
|
1076
|
+
}
|
|
1077
|
+
/** Refuses, once and by the record's own path, the keys of `entries` that `known` does not hold. */
|
|
1078
|
+
function onlyKnown(errors, path, entries, known, shape) {
|
|
1079
|
+
if ([...entries.keys()].some((key) => !known.includes(key))) errors.report(path, "invalid-source", `${shape}, ${NOTHING_ELSE}`);
|
|
1080
|
+
}
|
|
1081
|
+
/** One submission, or undefined when it cannot be read at all; `seen` holds the ids read before it. */
|
|
1082
|
+
function readSubmission(errors, raw, path, seen) {
|
|
1083
|
+
const read = readRecord(raw);
|
|
1084
|
+
if (!read.ok) {
|
|
1085
|
+
errors.report(path, container(read.code), SUBMISSION);
|
|
1086
|
+
return;
|
|
1087
|
+
}
|
|
1088
|
+
const known = [
|
|
1089
|
+
"id",
|
|
1090
|
+
"form",
|
|
1091
|
+
"formHash"
|
|
1092
|
+
];
|
|
1093
|
+
const unreadable = notData(errors, path, read.problems, known);
|
|
1094
|
+
onlyKnown(errors, path, read.entries, known, SUBMISSION);
|
|
1095
|
+
const [id, form, formHash] = known.map((key) => read.entries.get(key));
|
|
1096
|
+
if (!unreadable.has("id")) {
|
|
1097
|
+
if (!isSubmissionId(id)) errors.report(at(path, "id"), "invalid-source", `a submission's id: ${ID_SYNTAX}; never a name, an address or an answer`);
|
|
1098
|
+
else if (seen.has(id)) errors.report(at(path, "id"), "invalid-source", "a source names each submission once, and an earlier entry names this one");
|
|
1099
|
+
else seen.add(id);
|
|
1100
|
+
}
|
|
1101
|
+
if (!unreadable.has("form") && !isFormId(form)) errors.report(at(path, "form"), "invalid-source", `the form the submission answered: ${FORM_ID}`);
|
|
1102
|
+
if (!unreadable.has("formHash") && !isSchemaHash(formHash)) errors.report(at(path, "formHash"), "invalid-source", "the schemaHash of the form version that collected the submission: 64 lower-case hex digits");
|
|
1103
|
+
return Object.freeze({
|
|
1104
|
+
id,
|
|
1105
|
+
form,
|
|
1106
|
+
formHash
|
|
1107
|
+
});
|
|
1108
|
+
}
|
|
1109
|
+
/** A formancy form's source: each submission, read after the bound is checked. */
|
|
1110
|
+
function readFormSource(errors, entries, max) {
|
|
1111
|
+
const list = readList(entries.get("submissions"), max);
|
|
1112
|
+
if (!list.ok) {
|
|
1113
|
+
const message = list.code === "too-many" ? `${list.message}: a source names no more submissions than the data gives detail rows, or one where it gives none, since a report of one submission reads its repeater's rows and a report of many one a row` : "submissions is a list of { \"id\", \"form\", \"formHash\" }, one for each submission the data was read from";
|
|
1114
|
+
errors.report("/source/submissions", container(list.code), message);
|
|
1115
|
+
return;
|
|
1116
|
+
}
|
|
1117
|
+
if (list.items.length === 0) errors.report("/source/submissions", "invalid-source", "a form source names at least one submission the data was read from");
|
|
1118
|
+
const unreadable = new Map(list.problems.map(({ index, message }) => [index, message]));
|
|
1119
|
+
const seen = /* @__PURE__ */ new Set();
|
|
1120
|
+
const submissions = [];
|
|
1121
|
+
for (const [index, item] of list.items.entries()) {
|
|
1122
|
+
if (errors.full) break;
|
|
1123
|
+
const path = at("/source/submissions", index);
|
|
1124
|
+
if (item === UNREADABLE) {
|
|
1125
|
+
errors.report(path, "not-data", unreadable.get(index));
|
|
1126
|
+
continue;
|
|
1127
|
+
}
|
|
1128
|
+
const submission = readSubmission(errors, item, path, seen);
|
|
1129
|
+
if (submission !== void 0) submissions.push(submission);
|
|
1130
|
+
}
|
|
1131
|
+
const locale = entries.get("locale");
|
|
1132
|
+
if (entries.has("locale") && !isLocale(locale)) errors.report("/source/locale", "invalid-source", "the locale the form's labels were read in, as a report writes its locale: a BCP 47 tag such as fr-CH");
|
|
1133
|
+
return Object.freeze({
|
|
1134
|
+
provider: "formancy-form",
|
|
1135
|
+
submissions: Object.freeze(submissions),
|
|
1136
|
+
...isLocale(locale) ? { locale } : {}
|
|
1137
|
+
});
|
|
1138
|
+
}
|
|
1139
|
+
/** Formancy Data's source: a form for each dataset the report declares, for no other, and at least one. */
|
|
1140
|
+
function readDataSource(errors, entries, datasets) {
|
|
1141
|
+
const read = readRecord(entries.get("forms"));
|
|
1142
|
+
if (!read.ok) {
|
|
1143
|
+
errors.report("/source/forms", container(read.code), "forms is { \"header\", \"detail\" }: the published form each dataset the report declares was read from");
|
|
1144
|
+
return;
|
|
1145
|
+
}
|
|
1146
|
+
const unreadable = notData(errors, "/source/forms", read.problems, ROLES);
|
|
1147
|
+
if (read.entries.size === 0 && read.problems.length === 0) {
|
|
1148
|
+
errors.report("/source/forms", "invalid-source", "a Formancy Data source names the form each dataset was read from, and data read from no form names no source");
|
|
1149
|
+
return;
|
|
1150
|
+
}
|
|
1151
|
+
onlyKnown(errors, "/source/forms", read.entries, ROLES, "forms is keyed by the datasets a report declares, header and detail");
|
|
1152
|
+
for (const role of ROLES) {
|
|
1153
|
+
if (unreadable.has(role)) continue;
|
|
1154
|
+
const declared = datasets[role] !== void 0;
|
|
1155
|
+
if (!read.entries.has(role)) {
|
|
1156
|
+
if (declared) errors.report("/source/forms", "invalid-source", `the report declares a ${role} dataset, and the source names no form it was read from`);
|
|
1157
|
+
} else if (!declared) errors.report(at("/source/forms", role), "invalid-source", `the report declares no ${role} dataset, so none was read from a form`);
|
|
1158
|
+
else if (!isFormId(read.entries.get(role))) errors.report(at("/source/forms", role), "invalid-source", `the published form the ${role} dataset was read from: ${FORM_ID}`);
|
|
1159
|
+
}
|
|
1160
|
+
const forms = Object.fromEntries(ROLES.filter((role) => read.entries.has(role)).map((role) => [role, read.entries.get(role)]));
|
|
1161
|
+
return Object.freeze({
|
|
1162
|
+
provider: "formancy-data",
|
|
1163
|
+
forms: Object.freeze(forms)
|
|
1164
|
+
});
|
|
1165
|
+
}
|
|
1166
|
+
/**
|
|
1167
|
+
* The source `raw` names, held to its provider's shape and to `datasets`,
|
|
1168
|
+
* as a frozen copy, with every problem reported to `errors`; undefined when
|
|
1169
|
+
* it cannot be read at all. No more than `maxSubmissions` submissions are
|
|
1170
|
+
* read, and a list past it is refused before any of its items is.
|
|
1171
|
+
*/
|
|
1172
|
+
function checkSource(errors, raw, datasets, maxSubmissions) {
|
|
1173
|
+
const shape = `a source is { "provider": one of ${SOURCE_PROVIDERS.map((provider) => `"${provider}"`).join(", ")}, and what that provider names }`;
|
|
1174
|
+
const read = readRecord(raw);
|
|
1175
|
+
if (!read.ok) {
|
|
1176
|
+
errors.report("/source", container(read.code), shape);
|
|
1177
|
+
return;
|
|
1178
|
+
}
|
|
1179
|
+
const unreadable = notData(errors, "/source", read.problems, ["provider", ...new Set(Object.values(NAMES).flat())]);
|
|
1180
|
+
if (unreadable.has("provider")) return void 0;
|
|
1181
|
+
const provider = SOURCE_PROVIDERS.find((each) => each === read.entries.get("provider"));
|
|
1182
|
+
if (provider === void 0) {
|
|
1183
|
+
errors.report("/source/provider", "invalid-source", shape);
|
|
1184
|
+
return;
|
|
1185
|
+
}
|
|
1186
|
+
const names = NAMES[provider];
|
|
1187
|
+
onlyKnown(errors, "/source", read.entries, ["provider", ...names], SHAPES[provider]);
|
|
1188
|
+
if (names.some((name) => unreadable.has(name))) return void 0;
|
|
1189
|
+
return provider === "formancy-form" ? readFormSource(errors, read.entries, maxSubmissions) : readDataSource(errors, read.entries, datasets);
|
|
1190
|
+
}
|
|
1191
|
+
// src/data.ts
|
|
463
1192
|
/**
|
|
464
1193
|
* The data a report is rendered from, and the check that admits it.
|
|
465
1194
|
*
|
|
@@ -485,6 +1214,12 @@ function keyAt(path, key) {
|
|
|
485
1214
|
* that disagrees with the rows given.
|
|
486
1215
|
* - **Bounded.** The rows and every string are bounded before they are
|
|
487
1216
|
* read, and so is the error list; once it is full, nothing more is read.
|
|
1217
|
+
* - **Its source, by the syntax of identifiers.** Where the provider names
|
|
1218
|
+
* where the data came from -- a formancy form's submissions and their
|
|
1219
|
+
* versions, or Formancy Data's forms -- it is held to `source.ts`'s
|
|
1220
|
+
* rules, no more submissions than the rows given, and the render's
|
|
1221
|
+
* manifest carries it (0041). A formancy form's labels were read in the
|
|
1222
|
+
* locale the report prints in, or are refused (0063).
|
|
488
1223
|
*
|
|
489
1224
|
* What is held afterwards is a frozen copy, read once from the host's
|
|
490
1225
|
* objects, which no later change to them reaches.
|
|
@@ -518,7 +1253,8 @@ const ENVELOPE = [
|
|
|
518
1253
|
"header",
|
|
519
1254
|
"detail",
|
|
520
1255
|
"completeness",
|
|
521
|
-
"consistency"
|
|
1256
|
+
"consistency",
|
|
1257
|
+
"source"
|
|
522
1258
|
];
|
|
523
1259
|
const COMPLETENESS = [
|
|
524
1260
|
"complete",
|
|
@@ -723,6 +1459,10 @@ function checkData(report, data, options) {
|
|
|
723
1459
|
const detail = checkDetail(c, report.datasets.detail, top.entries);
|
|
724
1460
|
const completeness = checkCompleteness(c, top.entries.get("completeness"), detail.given);
|
|
725
1461
|
const consistency = checkConsistency(c, top.entries.get("consistency"));
|
|
1462
|
+
const rows = top.entries.has("detail") ? detail.given : 0;
|
|
1463
|
+
const submissions = rows === void 0 ? MAX_DETAIL_ROWS : Math.max(1, rows);
|
|
1464
|
+
const source = top.entries.has("source") ? checkSource(c.errors, top.entries.get("source"), report.datasets, submissions) : void 0;
|
|
1465
|
+
checkLabels(c.errors, source, options.reportLocale ?? report.locale, report.locale);
|
|
726
1466
|
if (!c.errors.empty || completeness === void 0 || consistency === void 0) return {
|
|
727
1467
|
ok: false,
|
|
728
1468
|
errors: c.errors.errors()
|
|
@@ -732,7 +1472,8 @@ function checkData(report, data, options) {
|
|
|
732
1472
|
...header === void 0 ? {} : { header },
|
|
733
1473
|
detail: Object.freeze(detail.rows),
|
|
734
1474
|
completeness,
|
|
735
|
-
consistency
|
|
1475
|
+
consistency,
|
|
1476
|
+
...source === void 0 ? {} : { source }
|
|
736
1477
|
};
|
|
737
1478
|
return {
|
|
738
1479
|
ok: true,
|
|
@@ -740,8 +1481,7 @@ function checkData(report, data, options) {
|
|
|
740
1481
|
warnings: c.warnings
|
|
741
1482
|
};
|
|
742
1483
|
}
|
|
743
|
-
|
|
744
|
-
//#region src/parameters.ts
|
|
1484
|
+
// src/parameters.ts
|
|
745
1485
|
/**
|
|
746
1486
|
* The values a person supplies before rendering, held to the report's
|
|
747
1487
|
* parameters.
|
|
@@ -835,8 +1575,7 @@ function checkParameters(report, values) {
|
|
|
835
1575
|
values: Object.freeze(checked)
|
|
836
1576
|
};
|
|
837
1577
|
}
|
|
838
|
-
|
|
839
|
-
//#region src/decimal.ts
|
|
1578
|
+
// src/decimal.ts
|
|
840
1579
|
/**
|
|
841
1580
|
* Exact totals and declared rounding, over `@formancy/expressions`'
|
|
842
1581
|
* `Decimal`: a value as an integer of units at a scale, so that nothing here
|
|
@@ -881,6 +1620,11 @@ function roundRatio(numerator, denominator, rounding) {
|
|
|
881
1620
|
const steps = roundQuotient(numerator * 10n ** BigInt(step.scale), denominator * step.units, rounding.mode);
|
|
882
1621
|
return new Decimal(steps * step.units * 10n ** BigInt(rounding.scale - step.scale), rounding.scale);
|
|
883
1622
|
}
|
|
1623
|
+
/** The exact ratio of two decimals rounded once as `rounding` declares: a share, never computed through a double. Throws a RangeError for a denominator of 0 or less, which `roundQuotient` does not divide by. */
|
|
1624
|
+
function ratioRounded(numerator, denominator, rounding) {
|
|
1625
|
+
if (denominator.units <= 0n) throw new RangeError("a ratio is of a denominator more than 0");
|
|
1626
|
+
return roundRatio(numerator.units * 10n ** BigInt(denominator.scale), denominator.units * 10n ** BigInt(numerator.scale), rounding);
|
|
1627
|
+
}
|
|
884
1628
|
/**
|
|
885
1629
|
* `value` rounded as `rounding` declares, at exactly its scale. A negative
|
|
886
1630
|
* value that rounds to nothing is zero, never minus zero. Throws a
|
|
@@ -924,8 +1668,7 @@ function averageDecimals(values, rounding) {
|
|
|
924
1668
|
}
|
|
925
1669
|
return count === 0n ? null : roundRatio(total.units, count * 10n ** BigInt(total.scale), rounding);
|
|
926
1670
|
}
|
|
927
|
-
|
|
928
|
-
//#region src/format.ts
|
|
1671
|
+
// src/format.ts
|
|
929
1672
|
/**
|
|
930
1673
|
* A value as the text a report prints, for the report's locale: report-spec's
|
|
931
1674
|
* number, currency, date and text formats, through Intl.
|
|
@@ -937,7 +1680,9 @@ function averageDecimals(values, rounding) {
|
|
|
937
1680
|
* handed a double and never rounds. Up to a bound: ECMA-402 reads a
|
|
938
1681
|
* string whose double would be infinite as infinity, and prints '∞'. A
|
|
939
1682
|
* number with more than `MAX_FORMATTED_DIGITS` integer digits is refused
|
|
940
|
-
* instead; printed without a format, it prints whole.
|
|
1683
|
+
* instead; printed without a format, it prints whole. `percent` is held
|
|
1684
|
+
* to both: a share at more places than it keeps is refused, never rounded
|
|
1685
|
+
* by Intl.
|
|
941
1686
|
* - **The locale's conventions.** Grouping, separators, currency symbols,
|
|
942
1687
|
* month names and case are the locale's, as the engine's ICU has them.
|
|
943
1688
|
* ICU versions can differ -- which mark Swiss German groups digits with is
|
|
@@ -960,7 +1705,8 @@ const FORMAT_ERROR_CODES = [
|
|
|
960
1705
|
"format-type-mismatch",
|
|
961
1706
|
"no-currency",
|
|
962
1707
|
"invalid-currency",
|
|
963
|
-
"number-too-large"
|
|
1708
|
+
"number-too-large",
|
|
1709
|
+
"percent-rounds-again"
|
|
964
1710
|
];
|
|
965
1711
|
/**
|
|
966
1712
|
* The most integer digits a number or currency format prints: 308. A double
|
|
@@ -969,6 +1715,10 @@ const FORMAT_ERROR_CODES = [
|
|
|
969
1715
|
* number of 308 digits or fewer prints exactly and one of 309 may not.
|
|
970
1716
|
*/
|
|
971
1717
|
const MAX_FORMATTED_DIGITS = 308;
|
|
1718
|
+
/** Whether `digits`, a number as `decimalToString` writes it, has more integer digits than Intl prints: it would print them as infinity. */
|
|
1719
|
+
const tooManyDigits = (digits) => digits.replace(/^-/, "").split(".")[0].length > 308;
|
|
1720
|
+
/** A count of places as a message says it: "1 place", "3 places". */
|
|
1721
|
+
const placesOf = (count) => `${count} ${count === 1 ? "place" : "places"}`;
|
|
972
1722
|
function refused(code, message) {
|
|
973
1723
|
return {
|
|
974
1724
|
ok: false,
|
|
@@ -1107,6 +1857,20 @@ var LocaleFormatter = class {
|
|
|
1107
1857
|
default: return this.#number(value, format, scope);
|
|
1108
1858
|
}
|
|
1109
1859
|
}
|
|
1860
|
+
percent(fraction, places) {
|
|
1861
|
+
if (fraction.scale > places + 2) return refused("percent-rounds-again", `a share of ${placesOf(fraction.scale)}, printed as a percentage at ${placesOf(places)}, would be rounded again by Intl; round it to ${placesOf(places + 2)} first`);
|
|
1862
|
+
const digits = decimalToString(fraction);
|
|
1863
|
+
if (tooManyDigits(digits)) return refused("number-too-large", `a share of more than 308 integer digits, which Intl would print as infinity`);
|
|
1864
|
+
const options = {
|
|
1865
|
+
style: "percent",
|
|
1866
|
+
minimumFractionDigits: places,
|
|
1867
|
+
maximumFractionDigits: places
|
|
1868
|
+
};
|
|
1869
|
+
return {
|
|
1870
|
+
ok: true,
|
|
1871
|
+
text: kept$1(this.#numbers, options, () => new Intl.NumberFormat(this.locale, options)).format(digits)
|
|
1872
|
+
};
|
|
1873
|
+
}
|
|
1110
1874
|
#transformed(text, transform) {
|
|
1111
1875
|
if (transform === "upper") return text.toLocaleUpperCase(this.locale);
|
|
1112
1876
|
return transform === "lower" ? text.toLocaleLowerCase(this.locale) : text;
|
|
@@ -1120,7 +1884,7 @@ var LocaleFormatter = class {
|
|
|
1120
1884
|
scale: format.scale,
|
|
1121
1885
|
mode: "half-away-from-zero"
|
|
1122
1886
|
}));
|
|
1123
|
-
if (rounded
|
|
1887
|
+
if (tooManyDigits(rounded)) return refused("number-too-large", `a number of more than 308 digits, which Intl would print as infinity; printed without a format, it prints whole`);
|
|
1124
1888
|
const options = {
|
|
1125
1889
|
minimumFractionDigits: format.scale,
|
|
1126
1890
|
maximumFractionDigits: format.scale,
|
|
@@ -1165,8 +1929,7 @@ function createFormatter(context) {
|
|
|
1165
1929
|
formatter: new LocaleFormatter(context)
|
|
1166
1930
|
};
|
|
1167
1931
|
}
|
|
1168
|
-
|
|
1169
|
-
//#region src/sort-group.ts
|
|
1932
|
+
// src/sort-group.ts
|
|
1170
1933
|
/**
|
|
1171
1934
|
* The order of a table's rows and its groups, decided once, here (0007).
|
|
1172
1935
|
*
|
|
@@ -1251,6 +2014,23 @@ function sameKey(type, a, b) {
|
|
|
1251
2014
|
return type === "decimal" ? compareDecimal(a, b) === 0 : a === b;
|
|
1252
2015
|
}
|
|
1253
2016
|
/**
|
|
2017
|
+
* One value a Map can find a group key by: equal for two keys of a type
|
|
2018
|
+
* exactly when `sameKey` holds them one (a test holds the two to each other).
|
|
2019
|
+
* A decimal is normalised -- trailing zeros stripped while it has a scale (zero ends at 0),
|
|
2020
|
+
* so 1.0 and 1.00 meet; text, integers and booleans are
|
|
2021
|
+
* their own keys, which a Map compares by value; null is null.
|
|
2022
|
+
*/
|
|
2023
|
+
function canonicalKey(type, value) {
|
|
2024
|
+
if (value === null) return null;
|
|
2025
|
+
if (type !== "decimal") return value;
|
|
2026
|
+
let { units, scale } = value;
|
|
2027
|
+
while (scale > 0 && units % 10n === 0n) {
|
|
2028
|
+
units /= 10n;
|
|
2029
|
+
scale -= 1;
|
|
2030
|
+
}
|
|
2031
|
+
return `${units}/${scale}`;
|
|
2032
|
+
}
|
|
2033
|
+
/**
|
|
1254
2034
|
* Rows already in order, as consecutive groups of equal keys: the groups in
|
|
1255
2035
|
* the order of their rows, each keyed by its first row's value. A missing
|
|
1256
2036
|
* value groups with null.
|
|
@@ -1272,6 +2052,37 @@ function groupRows(rows, by) {
|
|
|
1272
2052
|
}));
|
|
1273
2053
|
}
|
|
1274
2054
|
/**
|
|
2055
|
+
* A chart's categories, grouped as a table's rows are: sorted by the
|
|
2056
|
+
* category's key, nulls last, then grouped; or, in the data's order, each
|
|
2057
|
+
* distinct key once at its first row's place, holding every row with that
|
|
2058
|
+
* key in the provider's order. Rows without a category are one group.
|
|
2059
|
+
*/
|
|
2060
|
+
function categoryGroups(rows, by, order, locale) {
|
|
2061
|
+
if (order !== "data") return groupRows(orderRows(rows, [{
|
|
2062
|
+
...by,
|
|
2063
|
+
direction: order
|
|
2064
|
+
}], locale), by);
|
|
2065
|
+
const groups = /* @__PURE__ */ new Map();
|
|
2066
|
+
for (const [index, record] of rows.entries()) {
|
|
2067
|
+
const value = record[by.field] ?? null;
|
|
2068
|
+
const row = Object.freeze({
|
|
2069
|
+
index,
|
|
2070
|
+
record
|
|
2071
|
+
});
|
|
2072
|
+
const id = canonicalKey(by.type, value);
|
|
2073
|
+
const found = groups.get(id);
|
|
2074
|
+
if (found !== void 0) found.rows.push(row);
|
|
2075
|
+
else groups.set(id, {
|
|
2076
|
+
key: value,
|
|
2077
|
+
rows: [row]
|
|
2078
|
+
});
|
|
2079
|
+
}
|
|
2080
|
+
return [...groups.values()].map((group) => Object.freeze({
|
|
2081
|
+
key: group.key,
|
|
2082
|
+
rows: Object.freeze(group.rows)
|
|
2083
|
+
}));
|
|
2084
|
+
}
|
|
2085
|
+
/**
|
|
1275
2086
|
* The keys `table` orders its rows by: its group's key first, then its sort
|
|
1276
2087
|
* keys (0007), each typed by the report's detail contract. Throws a
|
|
1277
2088
|
* RangeError for a key on a field that has no order -- an image, or one the
|
|
@@ -1290,91 +2101,872 @@ function orderKeys(report, table) {
|
|
|
1290
2101
|
};
|
|
1291
2102
|
return [...table.group === void 0 ? [] : [typed(table.group.by.field, table.group.direction)], ...(table.sort ?? []).map(({ field, direction }) => typed(field.field, direction))];
|
|
1292
2103
|
}
|
|
1293
|
-
/** `
|
|
1294
|
-
function
|
|
2104
|
+
/** `selected`, in the data's order, as `orderRows` orders them: each the very row selected, at its index in the data, not in the selection. */
|
|
2105
|
+
function selectedInOrder(selected, keys, locale) {
|
|
2106
|
+
return orderRows(selected.map(({ record }) => record), keys, locale).map(({ index }) => selected[index]);
|
|
2107
|
+
}
|
|
2108
|
+
/**
|
|
2109
|
+
* `detail` -- the report's checked detail rows -- in `table`'s order, and in
|
|
2110
|
+
* its groups when it has one. Given `selected`, the rows a table's
|
|
2111
|
+
* condition selected, in the data's order (0062), those alone, each still
|
|
2112
|
+
* at its index in `detail`: a total over a group reads a row's value by
|
|
2113
|
+
* that index, and an error names the row by it. Ties keep the data's order
|
|
2114
|
+
* among them.
|
|
2115
|
+
*/
|
|
2116
|
+
function orderTable(report, table, detail, selected) {
|
|
1295
2117
|
const keys = orderKeys(report, table);
|
|
1296
|
-
const rows = orderRows(detail, keys, report.locale);
|
|
2118
|
+
const rows = selected === void 0 ? orderRows(detail, keys, report.locale) : selectedInOrder(selected, keys, report.locale);
|
|
1297
2119
|
if (table.group === void 0) return { rows };
|
|
1298
2120
|
return {
|
|
1299
2121
|
rows,
|
|
1300
2122
|
groups: groupRows(rows, keys[0])
|
|
1301
2123
|
};
|
|
1302
2124
|
}
|
|
1303
|
-
|
|
1304
|
-
//#region src/evaluate.ts
|
|
2125
|
+
// src/chart-ticks.ts
|
|
1305
2126
|
/**
|
|
1306
|
-
* A
|
|
1307
|
-
* within a budget, each failure an error that says where.
|
|
1308
|
-
*
|
|
1309
|
-
* - **One compilation.** `@formancy/report-core`'s `compileReport` compiles
|
|
1310
|
-
* every expression where it stands, and the editor's diagnostics come
|
|
1311
|
-
* from the same function (0009). A report its diagnostics give an error
|
|
1312
|
-
* for is refused before anything is evaluated, so the runtime never runs
|
|
1313
|
-
* what the editor refuses. Each expression is compiled once per render,
|
|
1314
|
-
* never once per row -- twice, in fact, since `diagnose` does not hand
|
|
1315
|
-
* back the compilation it made.
|
|
1316
|
-
* - **One binding.** Each program's values are made by report-core's
|
|
1317
|
-
* `expressionValues`: an integer as a BigInt, a decimal as a `Decimal`,
|
|
1318
|
-
* an instant as a moment, a date as its ISO string. A value missing, or of
|
|
1319
|
-
* another type, fails the evaluation, naming the member and never quoting
|
|
1320
|
-
* the value.
|
|
1321
|
-
* - **Nothing differs between renders.** The diagnostics refuse `now()`,
|
|
1322
|
-
* `today()` and `random()`; the capabilities an evaluation is handed are
|
|
1323
|
-
* fixed besides, so that one reached anyway would answer the same in every
|
|
1324
|
-
* render.
|
|
1325
|
-
* - **Bounded.** Each evaluation has a budget of steps, and a render has one
|
|
1326
|
-
* for all its evaluations together. Past either, the evaluation is refused.
|
|
1327
|
-
* - **A failure is an error, never a blank.** It names the component, the
|
|
1328
|
-
* table column or the aggregate, and the detail row, and gives upstream's
|
|
1329
|
-
* code for what went wrong, never upstream's message, which can quote the
|
|
1330
|
-
* values it was computing with.
|
|
2127
|
+
* A chart's value axis: ticks a reader takes in at a glance, found exactly.
|
|
1331
2128
|
*
|
|
1332
|
-
*
|
|
1333
|
-
*
|
|
1334
|
-
*
|
|
1335
|
-
*
|
|
1336
|
-
* evaluated (report-core's `exactness.ts`), since upstream hands an integer
|
|
1337
|
-
* and a double back alike, as a JavaScript number, and `int()` of a double
|
|
1338
|
-
* would otherwise print as an exact integer. A number that is not a safe
|
|
1339
|
-
* integer is still refused here, rather than printed.
|
|
1340
|
-
*/
|
|
1341
|
-
/**
|
|
1342
|
-
* Steps one evaluation may take: upstream's default for a formancy form's
|
|
1343
|
-
* expression (`DEFAULT_BUDGET`). A report expression reads single values, so
|
|
1344
|
-
* the only way to spend many steps is a comprehension over a list literal;
|
|
1345
|
-
* the deepest the structural limits allow, 64 by 64, measured 8,385 steps,
|
|
1346
|
-
* and `evaluate.test.ts` holds that it fits while one reading three values
|
|
1347
|
-
* in each turn (28,865 steps) does not.
|
|
2129
|
+
* The step is 1, 2 or 5 times a power of ten, the finest that covers every
|
|
2130
|
+
* value in at most `MAX_TICK_INTERVALS` intervals and no finer than one unit
|
|
2131
|
+
* of the value format's last place. Everything is BigInt division on a
|
|
2132
|
+
* decimal's units, never a double, so a 40-digit total ticks exactly (0010).
|
|
1348
2133
|
*/
|
|
1349
|
-
|
|
2134
|
+
/** Intervals an axis may hold: six labels down it, chosen as what a reader takes in at a glance. */
|
|
2135
|
+
const MAX_TICK_INTERVALS = 5;
|
|
2136
|
+
/** `numerator / denominator` rounded down, denominator more than 0. */
|
|
2137
|
+
function floorDiv(numerator, denominator) {
|
|
2138
|
+
const quotient = numerator / denominator;
|
|
2139
|
+
return numerator % denominator < 0n ? quotient - 1n : quotient;
|
|
2140
|
+
}
|
|
1350
2141
|
/**
|
|
1351
|
-
*
|
|
1352
|
-
*
|
|
1353
|
-
*
|
|
1354
|
-
* 2026, on an Intel i9-10900K under Node 24.21 with `@formancy/expressions`
|
|
1355
|
-
* 0.5.0: ten million steps of `row.urgent` took 10.0 seconds (1.00
|
|
1356
|
-
* microseconds a step) and of `row.amount * dec("1.081") > dec("10")` 13.0
|
|
1357
|
-
* (1.30). An earlier measurement, of upstream's `evaluate` alone, had put
|
|
1358
|
-
* that budget at 8.5 seconds and missed the binding. So the budget is five
|
|
1359
|
-
* million: about 5 to 6.5 seconds of evaluation on that machine, against
|
|
1360
|
-
* the plan's investigation target of ten seconds in all for a hundred-page
|
|
1361
|
-
* report. A report of `MAX_DETAIL_ROWS` rows evaluating ten one-read
|
|
1362
|
-
* expressions per row spends 2,000,000.
|
|
2142
|
+
* The decimal of `units` at `scale`, without trailing zeros: a tick is
|
|
2143
|
+
* written as its value needs, and printed at the value format's scale, to
|
|
2144
|
+
* which the formatter pads it back -- 11 prints as 11.00 at two places.
|
|
1363
2145
|
*/
|
|
1364
|
-
|
|
2146
|
+
function tick(units, scale) {
|
|
2147
|
+
let kept = units;
|
|
2148
|
+
let places = scale;
|
|
2149
|
+
while (places > 0 && kept % 10n === 0n) {
|
|
2150
|
+
kept /= 10n;
|
|
2151
|
+
places--;
|
|
2152
|
+
}
|
|
2153
|
+
return new Decimal(kept, places);
|
|
2154
|
+
}
|
|
1365
2155
|
/**
|
|
1366
|
-
*
|
|
1367
|
-
*
|
|
1368
|
-
*
|
|
2156
|
+
* The ticks of an axis over `values` (non-empty, not null) at the value
|
|
2157
|
+
* format's `scale`, ascending, evenly spaced and holding every value; a bar
|
|
2158
|
+
* chart's axis, and a flat line's, hold zero as well. Zero is a tick whenever
|
|
2159
|
+
* the axis holds it.
|
|
1369
2160
|
*/
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
2161
|
+
function chartTicks(values, chart, scale) {
|
|
2162
|
+
const common = values.reduce((most, value) => Math.max(most, value.scale), scale);
|
|
2163
|
+
const units = values.map((value) => value.units * 10n ** BigInt(common - value.scale));
|
|
2164
|
+
let lo = units.reduce((least, value) => value < least ? value : least);
|
|
2165
|
+
let hi = units.reduce((most, value) => value > most ? value : most);
|
|
2166
|
+
if (chart === "bar" || lo === hi) {
|
|
2167
|
+
if (lo > 0n) lo = 0n;
|
|
2168
|
+
if (hi < 0n) hi = 0n;
|
|
2169
|
+
}
|
|
2170
|
+
if (lo === hi) hi = 10n ** BigInt(common);
|
|
2171
|
+
const e = (hi - lo).toString().length - 1 - common;
|
|
2172
|
+
for (let k = Math.max(-scale, e - 1);; k++) for (const m of [
|
|
2173
|
+
1n,
|
|
2174
|
+
2n,
|
|
2175
|
+
5n
|
|
2176
|
+
]) {
|
|
2177
|
+
const step = m * 10n ** BigInt(k + common);
|
|
2178
|
+
const first = floorDiv(lo, step);
|
|
2179
|
+
const intervals = -floorDiv(-hi, step) - first;
|
|
2180
|
+
if (intervals > BigInt(5)) continue;
|
|
2181
|
+
return Array.from({ length: Number(intervals) + 1 }, (_, i) => tick((first + BigInt(i)) * step, common));
|
|
2182
|
+
}
|
|
2183
|
+
}
|
|
2184
|
+
// src/resolve-errors.ts
|
|
2185
|
+
/** What lowering a report can find wrong besides what each earlier stage finds. */
|
|
2186
|
+
const RESOLVE_ERROR_CODES = [
|
|
2187
|
+
"invalid-creation-date",
|
|
2188
|
+
"undeclared-locale",
|
|
2189
|
+
"missing-asset",
|
|
2190
|
+
"invalid-asset",
|
|
2191
|
+
"asset-lookup-failed",
|
|
2192
|
+
"rounding-not-decimal",
|
|
2193
|
+
"aggregate-value-type",
|
|
2194
|
+
"no-qr-value",
|
|
2195
|
+
"empty-qr-code",
|
|
2196
|
+
"invalid-qr-bill",
|
|
2197
|
+
"invalid-epc-qr-code",
|
|
2198
|
+
"invalid-e-invoice",
|
|
2199
|
+
"e-invoice-preview",
|
|
2200
|
+
"no-e-invoice-copy",
|
|
2201
|
+
"chart-too-many-categories",
|
|
2202
|
+
"chart-negative-share",
|
|
2203
|
+
"too-many-errors"
|
|
2204
|
+
];
|
|
2205
|
+
/** The optional parts of a place, written only when present, as exactOptionalPropertyTypes asks. */
|
|
2206
|
+
function placed(place) {
|
|
2207
|
+
return {
|
|
2208
|
+
...place.componentId === void 0 ? {} : { componentId: place.componentId },
|
|
2209
|
+
...place.columnId === void 0 ? {} : { columnId: place.columnId },
|
|
2210
|
+
...place.row === void 0 ? {} : { row: place.row }
|
|
2211
|
+
};
|
|
2212
|
+
}
|
|
2213
|
+
/** A type as a message says what a value is: "a decimal", "an integer". */
|
|
2214
|
+
function aType(type) {
|
|
2215
|
+
return `${/^[aeiou]/.test(type) ? "an" : "a"} ${type}`;
|
|
2216
|
+
}
|
|
2217
|
+
function fromReport(error) {
|
|
2218
|
+
return {
|
|
2219
|
+
stage: "report",
|
|
2220
|
+
code: error.code,
|
|
2221
|
+
path: error.path,
|
|
2222
|
+
message: error.message,
|
|
2223
|
+
...placed(error)
|
|
2224
|
+
};
|
|
2225
|
+
}
|
|
2226
|
+
function fromDiagnostic(diagnostic) {
|
|
2227
|
+
return {
|
|
2228
|
+
stage: "expressions",
|
|
2229
|
+
code: diagnostic.code,
|
|
2230
|
+
path: diagnostic.path,
|
|
2231
|
+
message: diagnostic.message,
|
|
2232
|
+
...placed(diagnostic)
|
|
2233
|
+
};
|
|
2234
|
+
}
|
|
2235
|
+
function fromData(stage, error) {
|
|
2236
|
+
return {
|
|
2237
|
+
stage,
|
|
2238
|
+
code: error.code,
|
|
2239
|
+
path: error.path,
|
|
2240
|
+
message: error.message
|
|
2241
|
+
};
|
|
2242
|
+
}
|
|
2243
|
+
function fromEvaluation(error) {
|
|
2244
|
+
return {
|
|
2245
|
+
stage: "evaluation",
|
|
2246
|
+
code: error.code,
|
|
2247
|
+
path: error.path,
|
|
2248
|
+
message: error.message,
|
|
2249
|
+
...placed(error),
|
|
2250
|
+
...error.aggregateId === void 0 ? {} : { aggregateId: error.aggregateId }
|
|
2251
|
+
};
|
|
2252
|
+
}
|
|
2253
|
+
/**
|
|
2254
|
+
* The errors and warnings one resolution collects. Errors are bounded as the
|
|
2255
|
+
* data check bounds its own: past `MAX_DATA_ERRORS`, one `too-many-errors`
|
|
2256
|
+
* says the list was cut short, and `full` tells a long loop to stop. A
|
|
2257
|
+
* warning is said once per path: a preview that lacks a field a thousand
|
|
2258
|
+
* rows read warns once for it.
|
|
2259
|
+
*/
|
|
2260
|
+
var Problems = class {
|
|
2261
|
+
#errors = [];
|
|
2262
|
+
#warnings = /* @__PURE__ */ new Map();
|
|
2263
|
+
#overflowed = false;
|
|
2264
|
+
error(error) {
|
|
2265
|
+
if (this.#errors.length < MAX_DATA_ERRORS) this.#errors.push(error);
|
|
2266
|
+
else this.#overflowed = true;
|
|
2267
|
+
}
|
|
2268
|
+
warn(warning) {
|
|
2269
|
+
const key = `${warning.stage} ${warning.code} ${warning.path}`;
|
|
2270
|
+
if (!this.#warnings.has(key)) this.#warnings.set(key, warning);
|
|
2271
|
+
}
|
|
2272
|
+
/** Whether the error list is cut short: nothing more is worth computing. */
|
|
2273
|
+
get full() {
|
|
2274
|
+
return this.#overflowed;
|
|
2275
|
+
}
|
|
2276
|
+
get failed() {
|
|
2277
|
+
return this.#errors.length > 0;
|
|
2278
|
+
}
|
|
2279
|
+
errors() {
|
|
2280
|
+
const errors = [...this.#errors];
|
|
2281
|
+
if (this.#overflowed) errors.push({
|
|
2282
|
+
stage: "resolve",
|
|
2283
|
+
code: "too-many-errors",
|
|
2284
|
+
path: "",
|
|
2285
|
+
message: `more than ${MAX_DATA_ERRORS} errors were found; the first ${MAX_DATA_ERRORS} are listed`
|
|
2286
|
+
});
|
|
2287
|
+
return errors;
|
|
2288
|
+
}
|
|
2289
|
+
warnings() {
|
|
2290
|
+
return [...this.#warnings.values()];
|
|
2291
|
+
}
|
|
2292
|
+
};
|
|
2293
|
+
// src/render-context.ts
|
|
2294
|
+
/** Each aggregate's value as an expression is handed it: the typed value's own, or null. */
|
|
2295
|
+
function raw(values) {
|
|
2296
|
+
return Object.fromEntries([...values].map(([id, value]) => [id, value === null ? null : value.value]));
|
|
2297
|
+
}
|
|
2298
|
+
/** The header record, or none when the report declares no header dataset. */
|
|
2299
|
+
function headerOf$1(render) {
|
|
2300
|
+
return render.data.header ?? {};
|
|
2301
|
+
}
|
|
2302
|
+
/** The e-invoice's names, read wherever `totals` is, once they are computed; none in a report that is no e-invoice. */
|
|
2303
|
+
function invoiceNames$1(render) {
|
|
2304
|
+
return render.invoice?.names ?? {};
|
|
2305
|
+
}
|
|
2306
|
+
function reportScope(render, totals) {
|
|
2307
|
+
const header = headerOf$1(render);
|
|
2308
|
+
return {
|
|
2309
|
+
values: {
|
|
2310
|
+
params: render.params,
|
|
2311
|
+
header,
|
|
2312
|
+
totals: raw(totals),
|
|
2313
|
+
...invoiceNames$1(render)
|
|
2314
|
+
},
|
|
2315
|
+
currency: {
|
|
2316
|
+
params: render.params,
|
|
2317
|
+
header
|
|
2318
|
+
}
|
|
2319
|
+
};
|
|
2320
|
+
}
|
|
2321
|
+
/** A table's total rows: the report's values, and the table's aggregates, over the rows it prints, as `table`. */
|
|
2322
|
+
function tableScope(render, table) {
|
|
2323
|
+
const scope = reportScope(render, render.totals.report);
|
|
2324
|
+
return {
|
|
2325
|
+
...scope,
|
|
2326
|
+
values: {
|
|
2327
|
+
...scope.values,
|
|
2328
|
+
table: raw(table)
|
|
2329
|
+
},
|
|
2330
|
+
table
|
|
2331
|
+
};
|
|
2332
|
+
}
|
|
2333
|
+
/**
|
|
2334
|
+
* A group's header and footer: the table's scope, and the group's
|
|
2335
|
+
* aggregates and key as `group`. `by` is the field grouped by; where a
|
|
2336
|
+
* preview's rows lack it the key is missing, as the field is, not null.
|
|
2337
|
+
*/
|
|
2338
|
+
function groupScope(render, group, aggregates, table, by) {
|
|
2339
|
+
const record = group.rows[0].record;
|
|
2340
|
+
const header = headerOf$1(render);
|
|
2341
|
+
const key = Object.hasOwn(record, by) ? { key: group.key } : {};
|
|
2342
|
+
return {
|
|
2343
|
+
values: {
|
|
2344
|
+
params: render.params,
|
|
2345
|
+
header,
|
|
2346
|
+
totals: raw(render.totals.report),
|
|
2347
|
+
table: raw(table),
|
|
2348
|
+
group: {
|
|
2349
|
+
...raw(aggregates),
|
|
2350
|
+
...key
|
|
2351
|
+
},
|
|
2352
|
+
...invoiceNames$1(render)
|
|
2353
|
+
},
|
|
2354
|
+
currency: {
|
|
2355
|
+
params: render.params,
|
|
2356
|
+
header,
|
|
2357
|
+
row: record
|
|
2358
|
+
},
|
|
2359
|
+
table,
|
|
2360
|
+
group: aggregates,
|
|
2361
|
+
record
|
|
2362
|
+
};
|
|
2363
|
+
}
|
|
2364
|
+
/** One detail row: the report's values, and the row's as `row`. */
|
|
2365
|
+
function rowScope(render, totals, row) {
|
|
2366
|
+
const header = headerOf$1(render);
|
|
2367
|
+
return {
|
|
2368
|
+
values: {
|
|
2369
|
+
params: render.params,
|
|
2370
|
+
header,
|
|
2371
|
+
totals: raw(totals),
|
|
2372
|
+
row: row.record,
|
|
2373
|
+
...invoiceNames$1(render)
|
|
2374
|
+
},
|
|
2375
|
+
currency: {
|
|
2376
|
+
params: render.params,
|
|
2377
|
+
header,
|
|
2378
|
+
row: row.record
|
|
2379
|
+
},
|
|
2380
|
+
record: row.record,
|
|
2381
|
+
row: row.index
|
|
2382
|
+
};
|
|
2383
|
+
}
|
|
2384
|
+
/** A failed evaluation: an error, or in a preview a value the data lacks, which is a warning. Returns whether it was only a warning. */
|
|
2385
|
+
function failed(render, outcome) {
|
|
2386
|
+
if (render.preview && outcome.error.code === "missing-value") {
|
|
2387
|
+
const error = fromEvaluation(outcome.error);
|
|
2388
|
+
render.problems.warn({
|
|
2389
|
+
...error,
|
|
2390
|
+
message: `${error.message}; this preview prints nothing for it`
|
|
2391
|
+
});
|
|
2392
|
+
return true;
|
|
2393
|
+
}
|
|
2394
|
+
render.problems.error(fromEvaluation(outcome.error));
|
|
2395
|
+
return false;
|
|
2396
|
+
}
|
|
2397
|
+
/** The value of the expression whose `cel` is at `path`, read in `scope`; null for none, undefined when it failed and the failure is recorded. */
|
|
2398
|
+
function valueOf(render, scope, path) {
|
|
2399
|
+
const outcome = render.expressions.evaluate(path, scope.values, scope.row);
|
|
2400
|
+
if (outcome.ok) return outcome.value;
|
|
2401
|
+
return failed(render, outcome) ? null : void 0;
|
|
2402
|
+
}
|
|
2403
|
+
/** Whether the condition whose `cel` is at `path` holds in `scope`; undefined when it failed and the failure is recorded as an error. */
|
|
2404
|
+
function holds(render, scope, path) {
|
|
2405
|
+
const outcome = render.expressions.condition(path, scope.values, scope.row);
|
|
2406
|
+
if (outcome.ok) return outcome.value;
|
|
2407
|
+
return failed(render, outcome) ? false : void 0;
|
|
2408
|
+
}
|
|
2409
|
+
/** The type the report's contract gives `ref`'s field; the validator has checked that it declares one. */
|
|
2410
|
+
function fieldType(report, ref) {
|
|
2411
|
+
return report.datasets[ref.dataset]?.fields.find(({ id }) => id === ref.field)?.type;
|
|
2412
|
+
}
|
|
2413
|
+
function parameterType(report, parameter) {
|
|
2414
|
+
return report.parameters.find(({ id }) => id === parameter)?.type;
|
|
2415
|
+
}
|
|
2416
|
+
/**
|
|
2417
|
+
* The value of `ref`, typed by its contract: a header field's from the
|
|
2418
|
+
* header, a detail field's from `record`. Null where it is null, and where
|
|
2419
|
+
* a preview's data lacks it, which the data check has already warned of. An
|
|
2420
|
+
* image field is drawn, never printed (0007), and reads as null here.
|
|
2421
|
+
*/
|
|
2422
|
+
function fieldValue(render, record, ref) {
|
|
2423
|
+
const value = (ref.dataset === "header" ? render.data.header : record)?.[ref.field] ?? null;
|
|
2424
|
+
const type = fieldType(render.report, ref);
|
|
2425
|
+
if (value === null || type === "image") return null;
|
|
2426
|
+
return {
|
|
2427
|
+
type,
|
|
2428
|
+
value
|
|
2429
|
+
};
|
|
2430
|
+
}
|
|
2431
|
+
/** An image field's value in the header: an approved asset, or null when there is none to draw. */
|
|
2432
|
+
function imageValue(render, ref) {
|
|
2433
|
+
return render.data.header?.[ref.field] ?? null;
|
|
2434
|
+
}
|
|
2435
|
+
// src/totals.ts
|
|
2436
|
+
/**
|
|
2437
|
+
* The report's aggregates, computed exactly, once, here (0007): over every
|
|
2438
|
+
* detail row for `totals`, over a group's rows for `group`, and over the
|
|
2439
|
+
* rows a table's condition selects for `table` (0062).
|
|
2440
|
+
*
|
|
2441
|
+
* - **In report-core's order.** An aggregate's expression may read another
|
|
2442
|
+
* aggregate's total, so each is computed after every one it reads, in the
|
|
2443
|
+
* order `compileReport` gives (0009); an aggregate that could never be
|
|
2444
|
+
* computed has already refused the report.
|
|
2445
|
+
* - **Each row read once.** What an aggregate reads from a row is read once
|
|
2446
|
+
* per render, with the report's totals, and kept by the row's index; a
|
|
2447
|
+
* group's aggregate, and a table's, is the same function over its rows'
|
|
2448
|
+
* values. So a group's subtotal and the report's total are totals of the
|
|
2449
|
+
* same values, and the subtotals of a sum that declares no rounding add
|
|
2450
|
+
* up to the total. A declared rounding is applied to each group's exact
|
|
2451
|
+
* sum and to the report's, once each, so rounded subtotals need not add
|
|
2452
|
+
* up to the rounded total: two groups of 0.02, rounded to 0.05, print
|
|
2453
|
+
* 0.00 and 0.00 over 0.05.
|
|
2454
|
+
* - **Exact, and of one kind.** Integers as BigInt, decimals as `Decimal`,
|
|
2455
|
+
* through `decimal.ts`; nothing passes through a JavaScript number. A
|
|
2456
|
+
* total report-core compiled as a decimal is a decimal, its 0 too, and so
|
|
2457
|
+
* is one whose kind the compiler could not tell (`dyn`) once it declares a
|
|
2458
|
+
* rounding or meets a decimal; a group's total, and a table's, is of the
|
|
2459
|
+
* report total's kind, so an expression reading `group.<id>` or
|
|
2460
|
+
* `table.<id>` meets one type.
|
|
2461
|
+
* - **Nulls are skipped**, and so is a value a preview's data lacks. Over no
|
|
2462
|
+
* values a count and a sum are 0 and the rest null (0007).
|
|
2463
|
+
* - **Declared rounding is applied once**, to the exact result: an average
|
|
2464
|
+
* always declares one, and a sum, a minimum or a maximum of decimals may.
|
|
2465
|
+
* A rounding on a total that is an integer or a moment is refused, never
|
|
2466
|
+
* ignored.
|
|
2467
|
+
*/
|
|
2468
|
+
const isRefusal = (value) => value !== null && "code" in value;
|
|
2469
|
+
const integer = (value) => ({
|
|
2470
|
+
type: "integer",
|
|
2471
|
+
value
|
|
2472
|
+
});
|
|
2473
|
+
const decimal = (value) => ({
|
|
2474
|
+
type: "decimal",
|
|
2475
|
+
value
|
|
2476
|
+
});
|
|
2477
|
+
/** A number as a decimal: an integer at no places. */
|
|
2478
|
+
const asDecimal = (value) => value.type === "integer" ? new Decimal(value.value, 0) : value.value;
|
|
2479
|
+
/** Values as numbers of one kind: integers while every one is and decimals are not asked for, else decimals; undefined when one is not a number. */
|
|
2480
|
+
function numbers(values, decimals) {
|
|
2481
|
+
if (!decimals && values.every((value) => value.type === "integer")) return { integers: values.map((value) => value.value) };
|
|
2482
|
+
if (!values.every((value) => value.type === "integer" || value.type === "decimal")) return void 0;
|
|
2483
|
+
return { decimals: values.map(asDecimal) };
|
|
2484
|
+
}
|
|
2485
|
+
const NOT_NUMBERS = {
|
|
2486
|
+
code: "aggregate-value-type",
|
|
2487
|
+
message: "totals integers and decimals, and a value it read is neither"
|
|
2488
|
+
};
|
|
2489
|
+
function sum$1(values, decimals) {
|
|
2490
|
+
const read = numbers(values, decimals);
|
|
2491
|
+
if (read === void 0) return NOT_NUMBERS;
|
|
2492
|
+
return "integers" in read ? integer(read.integers.reduce((total, value) => total + value, 0n)) : decimal(sumDecimals(read.decimals));
|
|
2493
|
+
}
|
|
2494
|
+
function average(values, rounding) {
|
|
2495
|
+
const read = numbers(values, true);
|
|
2496
|
+
if (read === void 0) return NOT_NUMBERS;
|
|
2497
|
+
const result = averageDecimals(read.decimals, rounding);
|
|
2498
|
+
return result === null ? null : decimal(result);
|
|
2499
|
+
}
|
|
2500
|
+
const ordered = (a, b) => a < b ? -1 : a > b ? 1 : 0;
|
|
2501
|
+
/**
|
|
2502
|
+
* -1, 0 or 1 for two values of one kind: numbers by value, text as the
|
|
2503
|
+
* report's language sorts it, through `collator`, dates as days, moments in
|
|
2504
|
+
* time order. Text met without a collator is a defect: a total handed none
|
|
2505
|
+
* -- a chart's series, which totals numbers alone -- would otherwise order
|
|
2506
|
+
* it by code unit, which no reader takes for their language's order.
|
|
2507
|
+
*/
|
|
2508
|
+
function comparator(values, collator) {
|
|
2509
|
+
const types = new Set(values.map(({ type }) => type));
|
|
2510
|
+
if ([...types].every((type) => type === "integer" || type === "decimal")) return (a, b) => compareDecimal(asDecimal(a), asDecimal(b));
|
|
2511
|
+
const only = types.size === 1 ? [...types][0] : void 0;
|
|
2512
|
+
if (only === "string") {
|
|
2513
|
+
if (collator === void 0) throw new RangeError("a least or greatest of text is ordered by the report's collator, and none was handed: a defect");
|
|
2514
|
+
return (a, b) => collator.compare(a.value, b.value);
|
|
2515
|
+
}
|
|
2516
|
+
if (only === "date") return (a, b) => ordered(a.value, b.value);
|
|
2517
|
+
if (only === "datetime" || only === "instant") return (a, b) => ordered(fixedWidth(a.value), fixedWidth(b.value));
|
|
2518
|
+
}
|
|
2519
|
+
/** The least (`sign` -1) or greatest (1) value, the first of equals as written; null over none. */
|
|
2520
|
+
function extreme(values, sign, decimals, collator) {
|
|
2521
|
+
const compare = comparator(values, collator);
|
|
2522
|
+
if (compare === void 0) return {
|
|
2523
|
+
code: "aggregate-value-type",
|
|
2524
|
+
message: "takes the least or greatest of values of one kind, and the values it read are of several"
|
|
2525
|
+
};
|
|
2526
|
+
const found = values.reduce((kept, value) => kept === null || Math.sign(compare(value, kept)) === sign ? value : kept, null);
|
|
2527
|
+
return found?.type === "integer" && decimals ? decimal(asDecimal(found)) : found;
|
|
2528
|
+
}
|
|
2529
|
+
/** `total` rounded as `rounding` declares: a decimal at the rounding's scale. */
|
|
2530
|
+
function rounded(total, rounding) {
|
|
2531
|
+
if (rounding === void 0 || total === null || isRefusal(total)) return total;
|
|
2532
|
+
if (total.type === "decimal") return decimal(roundDecimalAs(total.value, rounding));
|
|
2533
|
+
return {
|
|
2534
|
+
code: "rounding-not-decimal",
|
|
2535
|
+
message: `declares a rounding, and its total is ${total.type === "integer" ? "an integer" : `a ${total.type}`}, which has no places to round`
|
|
2536
|
+
};
|
|
2537
|
+
}
|
|
2538
|
+
/** Whether a total the compiler typed as `type` is totalled in decimals: one it typed a decimal, or could not tell and declares a rounding. */
|
|
2539
|
+
function totalsInDecimals(type, rounding) {
|
|
2540
|
+
return type === "decimal" || type === "dyn" && rounding !== void 0;
|
|
2541
|
+
}
|
|
2542
|
+
/**
|
|
2543
|
+
* The aggregate over the values read from some rows -- each null or typed --
|
|
2544
|
+
* or over `rows` rows when it reads nothing: the one rule every total is
|
|
2545
|
+
* computed by, the report's, a group's and a chart's series'. `collator`
|
|
2546
|
+
* orders text, for a least or a greatest of it; a total of numbers needs none.
|
|
2547
|
+
*/
|
|
2548
|
+
function totalOf(aggregate, decimals, read, rows, collator) {
|
|
2549
|
+
const values = (read ?? []).filter((value) => value !== null);
|
|
2550
|
+
let total;
|
|
2551
|
+
switch (aggregate.fn) {
|
|
2552
|
+
case "count":
|
|
2553
|
+
total = rounded(integer(BigInt(read === void 0 ? rows : values.length)), aggregate.rounding);
|
|
2554
|
+
break;
|
|
2555
|
+
case "avg":
|
|
2556
|
+
total = values.length === 0 ? null : average(values, aggregate.rounding);
|
|
2557
|
+
break;
|
|
2558
|
+
case "sum":
|
|
2559
|
+
total = rounded(sum$1(values, decimals), aggregate.rounding);
|
|
2560
|
+
break;
|
|
2561
|
+
case "min":
|
|
2562
|
+
total = rounded(extreme(values, -1, decimals, collator), aggregate.rounding);
|
|
2563
|
+
break;
|
|
2564
|
+
case "max": total = rounded(extreme(values, 1, decimals, collator), aggregate.rounding);
|
|
2565
|
+
}
|
|
2566
|
+
return isRefusal(total) ? {
|
|
2567
|
+
ok: false,
|
|
2568
|
+
...total
|
|
2569
|
+
} : {
|
|
2570
|
+
ok: true,
|
|
2571
|
+
value: total
|
|
2572
|
+
};
|
|
2573
|
+
}
|
|
2574
|
+
/**
|
|
2575
|
+
* Every aggregate of `render`'s report, over its detail rows and, through
|
|
2576
|
+
* `over`, over any of them; or undefined when one could not be computed,
|
|
2577
|
+
* each reason recorded. `render` has every input but the totals.
|
|
2578
|
+
*/
|
|
2579
|
+
function computeTotals(render) {
|
|
2580
|
+
const { report, expressions, data, problems } = render;
|
|
2581
|
+
const collator = new Intl.Collator(report.locale, { usage: "sort" });
|
|
2582
|
+
const rows = data.detail.map((record, index) => ({
|
|
2583
|
+
index,
|
|
2584
|
+
record
|
|
2585
|
+
}));
|
|
2586
|
+
const read = /* @__PURE__ */ new Map();
|
|
2587
|
+
const totals = /* @__PURE__ */ new Map();
|
|
2588
|
+
const inDecimals = /* @__PURE__ */ new Map();
|
|
2589
|
+
const refuse = (index, refusal) => {
|
|
2590
|
+
problems.error({
|
|
2591
|
+
stage: "resolve",
|
|
2592
|
+
code: refusal.code,
|
|
2593
|
+
path: `/aggregates/${index}`,
|
|
2594
|
+
message: `aggregate ${report.aggregates[index]?.id} ${refusal.message}`
|
|
2595
|
+
});
|
|
2596
|
+
};
|
|
2597
|
+
let computed = true;
|
|
2598
|
+
for (const id of expressions.compilation.aggregateOrder) {
|
|
2599
|
+
const index = report.aggregates.findIndex((aggregate) => aggregate.id === id);
|
|
2600
|
+
const aggregate = report.aggregates[index];
|
|
2601
|
+
const compiled = expressions.compilation.members.totals.get(id)?.type;
|
|
2602
|
+
const decimals = totalsInDecimals(compiled, aggregate.rounding);
|
|
2603
|
+
const of = aggregate.of;
|
|
2604
|
+
if (of !== void 0) {
|
|
2605
|
+
const path = `/aggregates/${index}/of/expression/cel`;
|
|
2606
|
+
const values = rows.map((row) => of.kind === "field" ? fieldValue(render, row.record, of.field) : valueOf(render, rowScope(render, totals, row), path));
|
|
2607
|
+
if (values.some((value) => value === void 0)) {
|
|
2608
|
+
computed = false;
|
|
2609
|
+
continue;
|
|
2610
|
+
}
|
|
2611
|
+
read.set(id, values);
|
|
2612
|
+
}
|
|
2613
|
+
const outcome = totalOf(aggregate, decimals, read.get(id), rows.length, collator);
|
|
2614
|
+
if (!outcome.ok) {
|
|
2615
|
+
computed = false;
|
|
2616
|
+
refuse(index, outcome);
|
|
2617
|
+
continue;
|
|
2618
|
+
}
|
|
2619
|
+
const total = outcome.value;
|
|
2620
|
+
totals.set(id, total);
|
|
2621
|
+
inDecimals.set(id, decimals || total?.type === "decimal");
|
|
2622
|
+
}
|
|
2623
|
+
if (!computed) return void 0;
|
|
2624
|
+
return {
|
|
2625
|
+
report: totals,
|
|
2626
|
+
over(group) {
|
|
2627
|
+
const values = /* @__PURE__ */ new Map();
|
|
2628
|
+
for (const aggregate of report.aggregates) {
|
|
2629
|
+
const each = read.get(aggregate.id);
|
|
2630
|
+
const grouped = totalOf(aggregate, inDecimals.get(aggregate.id), each && group.map((row) => each[row.index]), group.length, collator);
|
|
2631
|
+
if (!grouped.ok) throw new RangeError(`a total of some rows of aggregate ${aggregate.id} was refused as ${grouped.code} after the report's was not: a defect, refused rather than printed as nothing`);
|
|
2632
|
+
values.set(aggregate.id, grouped.value);
|
|
2633
|
+
}
|
|
2634
|
+
return values;
|
|
2635
|
+
}
|
|
2636
|
+
};
|
|
2637
|
+
}
|
|
2638
|
+
// src/lower-chart.ts
|
|
2639
|
+
/**
|
|
2640
|
+
* The most categories a chart groups its rows into: 200, chosen as a bound
|
|
2641
|
+
* on one chart's work, totalling every series at every category. How many
|
|
2642
|
+
* a box draws legibly is the renderer's to say, and so is how many charts
|
|
2643
|
+
* a render draws together: the renderer's `MAX_CHART_DRAWING` (0058) weighs
|
|
2644
|
+
* a render's charts and refuses them past it.
|
|
2645
|
+
*
|
|
2646
|
+
* What the costliest of eight chart forms tried costs a render, at one
|
|
2647
|
+
* packing, measured on 2026-11-15 on a Mac (Apple M2) under Node 22.23.4,
|
|
2648
|
+
* ECharts 6.1.0, with the machine at a load average of about 5 from other
|
|
2649
|
+
* work: three charts to an A4 landscape page, zero margins, each as wide
|
|
2650
|
+
* as the page and a third of its height, eight series where it has them,
|
|
2651
|
+
* value labels off, and `renderPdf` run seven times to each page after a
|
|
2652
|
+
* warm-up. Each form is the largest `drawChart` does not refuse: a line
|
|
2653
|
+
* chart of 8 series and the full 200 categories, whose empty labels are no
|
|
2654
|
+
* word, took a median of 586 ms (549 to 849) against 4.6 ms for the same
|
|
2655
|
+
* page without charts; the same chart with one-letter labels, refused past
|
|
2656
|
+
* 123 categories, 430 ms; the most categories a bar chart draws, 186 of
|
|
2657
|
+
* one series, 77 ms. The bound stays, for one chart's work; how many
|
|
2658
|
+
* charts a render draws, and so what a page costs, is the renderer's
|
|
2659
|
+
* `MAX_CHART_DRAWING`, which refuses that page of three 8 × 200 lines at
|
|
2660
|
+
* its third chart, at 5,016 units, and admits two. The renderer's word
|
|
2661
|
+
* checks took under 1 ms of any page.
|
|
2662
|
+
*/
|
|
2663
|
+
const MAX_CHART_CATEGORIES = 200;
|
|
2664
|
+
/** The places of a percentage a pie's share prints at: 12.5 %. */
|
|
2665
|
+
const SHARE_PLACES = 1;
|
|
2666
|
+
/** Records a refusal of the chart at `at`; undefined, for its caller to return. */
|
|
2667
|
+
function refuse$4(c, code, at, message) {
|
|
2668
|
+
c.render.problems.error({
|
|
2669
|
+
stage: "resolve",
|
|
2670
|
+
code,
|
|
2671
|
+
path: at,
|
|
2672
|
+
message,
|
|
2673
|
+
componentId: c.chart.id
|
|
2674
|
+
});
|
|
2675
|
+
}
|
|
2676
|
+
/** `formatted`'s text, or undefined when the formatter refused it, which is recorded at `at`. */
|
|
2677
|
+
function said(c, formatted, at) {
|
|
2678
|
+
if (formatted.ok) return formatted.text;
|
|
2679
|
+
c.render.problems.error({
|
|
2680
|
+
stage: "format",
|
|
2681
|
+
code: formatted.code,
|
|
2682
|
+
path: at,
|
|
2683
|
+
message: `chart ${c.chart.id}: ${formatted.message}`,
|
|
2684
|
+
componentId: c.chart.id
|
|
2685
|
+
});
|
|
2686
|
+
}
|
|
2687
|
+
/** A value as the block holds it: its exact digits, and its text in the chart's value format. */
|
|
2688
|
+
function charted(c, value) {
|
|
2689
|
+
const text = said(c, c.render.formatter.format(value, c.chart.valueFormat, c.currency), `${c.path}/valueFormat`);
|
|
2690
|
+
return text === void 0 ? void 0 : {
|
|
2691
|
+
exact: plainText(value),
|
|
2692
|
+
text
|
|
2693
|
+
};
|
|
2694
|
+
}
|
|
2695
|
+
/** Each of `values` as the block holds it, null as null; undefined once one could not be printed, which is recorded, and nothing after it is tried. */
|
|
2696
|
+
function chartedAll(c, values) {
|
|
2697
|
+
const found = [];
|
|
2698
|
+
for (const value of values) {
|
|
2699
|
+
const each = value === null ? null : charted(c, value);
|
|
2700
|
+
if (each === void 0) return void 0;
|
|
2701
|
+
found.push(each);
|
|
2702
|
+
}
|
|
2703
|
+
return found;
|
|
2704
|
+
}
|
|
2705
|
+
/**
|
|
2706
|
+
* Each category's label, in order: its key, of the category field's `type`,
|
|
2707
|
+
* as `categoryGroups` read it once, in the category's format; undefined
|
|
2708
|
+
* when one could not be printed, which is recorded.
|
|
2709
|
+
*/
|
|
2710
|
+
function labels(c, groups, type) {
|
|
2711
|
+
const { format, nullLabel } = c.chart.category;
|
|
2712
|
+
const found = [];
|
|
2713
|
+
for (const { key } of groups) {
|
|
2714
|
+
const typed = {
|
|
2715
|
+
type,
|
|
2716
|
+
value: key
|
|
2717
|
+
};
|
|
2718
|
+
const label = key === null ? nullLabel ?? "" : said(c, c.render.formatter.format(typed, format, c.currency), `${c.path}/category`);
|
|
2719
|
+
if (label === void 0) return void 0;
|
|
2720
|
+
found.push(label);
|
|
2721
|
+
}
|
|
2722
|
+
return found;
|
|
2723
|
+
}
|
|
2724
|
+
/** `series` as the aggregate of its definition: the same function, over the same field, rounded as it declares. */
|
|
2725
|
+
function asAggregate(series) {
|
|
2726
|
+
return {
|
|
2727
|
+
id: series.id,
|
|
2728
|
+
fn: series.fn,
|
|
2729
|
+
...series.of === void 0 ? {} : { of: {
|
|
2730
|
+
kind: "field",
|
|
2731
|
+
field: series.of
|
|
2732
|
+
} },
|
|
2733
|
+
...series.rounding === void 0 ? {} : { rounding: series.rounding }
|
|
2734
|
+
};
|
|
2735
|
+
}
|
|
2736
|
+
/**
|
|
2737
|
+
* Each series' total at each category, null where it has none; undefined
|
|
2738
|
+
* when a series could not be totalled, which is recorded once for it. The
|
|
2739
|
+
* validator types every field a series reads, and report-core refuses a
|
|
2740
|
+
* rounding with no places to round before anything renders; one refused
|
|
2741
|
+
* here all the same is refused as an aggregate's is, never drawn.
|
|
2742
|
+
*/
|
|
2743
|
+
function totals(c, groups) {
|
|
2744
|
+
const { render, chart } = c;
|
|
2745
|
+
let refused = false;
|
|
2746
|
+
const found = chart.series.map((series, index) => {
|
|
2747
|
+
const aggregate = asAggregate(series);
|
|
2748
|
+
const decimals = totalsInDecimals(seriesType(render.report, series).type, series.rounding);
|
|
2749
|
+
const of = series.of;
|
|
2750
|
+
const values = [];
|
|
2751
|
+
for (const group of groups) {
|
|
2752
|
+
const outcome = totalOf(aggregate, decimals, of === void 0 ? void 0 : group.rows.map((row) => fieldValue(render, row.record, of)), group.rows.length);
|
|
2753
|
+
if (!outcome.ok) {
|
|
2754
|
+
refused = true;
|
|
2755
|
+
refuse$4(c, outcome.code, `${c.path}/series/${index}`, `series ${series.id} of chart ${chart.id} ${outcome.message}`);
|
|
2756
|
+
break;
|
|
2757
|
+
}
|
|
2758
|
+
values.push(outcome.value);
|
|
2759
|
+
}
|
|
2760
|
+
return values;
|
|
2761
|
+
});
|
|
2762
|
+
return refused ? void 0 : found;
|
|
2763
|
+
}
|
|
2764
|
+
/** A bar or a line chart over `values`, one list for each series; with no value at all, its empty text. */
|
|
2765
|
+
function axisChart(c, frame, categories, values) {
|
|
2766
|
+
const { chart } = c;
|
|
2767
|
+
const kind = chart.chart;
|
|
2768
|
+
const valueLabels = chart.valueLabels === true;
|
|
2769
|
+
const exact = values.flat().flatMap((total) => total === null ? [] : [asDecimal(total)]);
|
|
2770
|
+
if (exact.length === 0) return {
|
|
2771
|
+
...frame,
|
|
2772
|
+
chart: kind,
|
|
2773
|
+
legend: [],
|
|
2774
|
+
empty: chart.emptyText,
|
|
2775
|
+
categories: [],
|
|
2776
|
+
series: [],
|
|
2777
|
+
ticks: [],
|
|
2778
|
+
valueLabels
|
|
2779
|
+
};
|
|
2780
|
+
const series = [];
|
|
2781
|
+
for (const [index, each] of values.entries()) {
|
|
2782
|
+
const printed = chartedAll(c, each);
|
|
2783
|
+
if (printed === void 0) return void 0;
|
|
2784
|
+
series.push({
|
|
2785
|
+
color: c.palette[index],
|
|
2786
|
+
values: printed
|
|
2787
|
+
});
|
|
2788
|
+
}
|
|
2789
|
+
const ticks = chartedAll(c, chartTicks(exact, kind, chart.valueFormat.scale).map((tick) => ({
|
|
2790
|
+
type: "decimal",
|
|
2791
|
+
value: tick
|
|
2792
|
+
})));
|
|
2793
|
+
if (ticks === void 0) return void 0;
|
|
2794
|
+
const legend = chart.series.length > 1 ? chart.series.map(({ label }, index) => ({
|
|
2795
|
+
text: label,
|
|
2796
|
+
color: c.palette[index]
|
|
2797
|
+
})) : [];
|
|
2798
|
+
return {
|
|
2799
|
+
...frame,
|
|
2800
|
+
chart: kind,
|
|
2801
|
+
...chart.categoryTitle === void 0 ? {} : { categoryTitle: chart.categoryTitle },
|
|
2802
|
+
...chart.valueTitle === void 0 ? {} : { valueTitle: chart.valueTitle },
|
|
2803
|
+
legend,
|
|
2804
|
+
categories,
|
|
2805
|
+
series,
|
|
2806
|
+
ticks,
|
|
2807
|
+
valueLabels
|
|
2808
|
+
};
|
|
2809
|
+
}
|
|
2810
|
+
/**
|
|
2811
|
+
* A pie over `values`, a sum or a count at each category, which is never
|
|
2812
|
+
* null: a negative one refused; all zero, its empty text; otherwise a
|
|
2813
|
+
* slice and a legend entry for each, its share in the entry where the
|
|
2814
|
+
* chart labels its values.
|
|
2815
|
+
*/
|
|
2816
|
+
function pieChart(c, frame, categories, values) {
|
|
2817
|
+
const { chart, path } = c;
|
|
2818
|
+
const exact = values.map(asDecimal);
|
|
2819
|
+
if (exact.some((value) => value.units < 0n)) return refuse$4(c, "chart-negative-share", `${path}/series/0`, `chart ${chart.id} is a pie, and a category of it totals less than nothing, which is no share of a whole`);
|
|
2820
|
+
const total = sumDecimals(exact);
|
|
2821
|
+
if (total.units === 0n) return {
|
|
2822
|
+
...frame,
|
|
2823
|
+
chart: "pie",
|
|
2824
|
+
legend: [],
|
|
2825
|
+
empty: chart.emptyText,
|
|
2826
|
+
slices: []
|
|
2827
|
+
};
|
|
2828
|
+
const printed = chartedAll(c, values);
|
|
2829
|
+
if (printed === void 0) return void 0;
|
|
2830
|
+
const legend = [];
|
|
2831
|
+
for (const [index, value] of printed.entries()) {
|
|
2832
|
+
const label = categories[index];
|
|
2833
|
+
let text = label;
|
|
2834
|
+
if (chart.valueLabels === true) {
|
|
2835
|
+
const share = ratioRounded(exact[index], total, {
|
|
2836
|
+
scale: 3,
|
|
2837
|
+
mode: "half-away-from-zero"
|
|
2838
|
+
});
|
|
2839
|
+
const percent = said(c, c.render.formatter.percent(share, 1), `${path}/series/0`);
|
|
2840
|
+
if (percent === void 0) return void 0;
|
|
2841
|
+
text = `${label}: ${value.text} (${percent})`;
|
|
2842
|
+
}
|
|
2843
|
+
legend.push({
|
|
2844
|
+
text,
|
|
2845
|
+
color: c.palette[index]
|
|
2846
|
+
});
|
|
2847
|
+
}
|
|
2848
|
+
return {
|
|
2849
|
+
...frame,
|
|
2850
|
+
chart: "pie",
|
|
2851
|
+
legend,
|
|
2852
|
+
slices: printed.map((value, index) => ({
|
|
2853
|
+
color: c.palette[index],
|
|
2854
|
+
value
|
|
2855
|
+
}))
|
|
2856
|
+
};
|
|
2857
|
+
}
|
|
2858
|
+
/**
|
|
2859
|
+
* `chart`, at `path`, as the block a renderer draws, `style` its words'
|
|
2860
|
+
* resolved style; undefined when it is refused, the reason recorded.
|
|
2861
|
+
*/
|
|
2862
|
+
function chartBlock(render, chart, path, style) {
|
|
2863
|
+
const c = {
|
|
2864
|
+
render,
|
|
2865
|
+
chart,
|
|
2866
|
+
path,
|
|
2867
|
+
currency: reportScope(render, render.totals.report).currency,
|
|
2868
|
+
palette: chart.palette ?? CHART_PALETTE
|
|
2869
|
+
};
|
|
2870
|
+
const by = {
|
|
2871
|
+
field: chart.category.field.field,
|
|
2872
|
+
type: fieldType(render.report, chart.category.field)
|
|
2873
|
+
};
|
|
2874
|
+
const groups = categoryGroups(render.data.detail, by, chart.category.order, render.report.locale);
|
|
2875
|
+
if (groups.length > 200) {
|
|
2876
|
+
const message = `chart ${chart.id} groups its rows into ${groups.length} categories, and a chart groups at most 200`;
|
|
2877
|
+
return refuse$4(c, "chart-too-many-categories", `${path}/category`, message);
|
|
2878
|
+
}
|
|
2879
|
+
if (chart.chart === "pie" && groups.length > c.palette.length) {
|
|
2880
|
+
const message = `chart ${chart.id} is a pie of ${groups.length} categories; a pie colours each slice apart, and its palette has ${c.palette.length} colours`;
|
|
2881
|
+
return refuse$4(c, "chart-too-many-categories", `${path}/category`, message);
|
|
2882
|
+
}
|
|
2883
|
+
const categories = labels(c, groups, by.type);
|
|
2884
|
+
const values = categories && totals(c, groups);
|
|
2885
|
+
if (categories === void 0 || values === void 0) return void 0;
|
|
2886
|
+
const frame = {
|
|
2887
|
+
kind: "chart",
|
|
2888
|
+
id: chart.id,
|
|
2889
|
+
width: chart.width,
|
|
2890
|
+
height: chart.height,
|
|
2891
|
+
...style === void 0 ? {} : { style },
|
|
2892
|
+
...chart.title === void 0 ? {} : { title: chart.title }
|
|
2893
|
+
};
|
|
2894
|
+
return chart.chart === "pie" ? pieChart(c, frame, categories, values[0] ?? []) : axisChart(c, frame, categories, values);
|
|
2895
|
+
}
|
|
2896
|
+
// src/evaluate.ts
|
|
2897
|
+
/**
|
|
2898
|
+
* A report's expressions, compiled once and evaluated against checked data,
|
|
2899
|
+
* within a budget, each failure an error that says where.
|
|
2900
|
+
*
|
|
2901
|
+
* - **One compilation.** `@formancy/report-core`'s `compileReport` compiles
|
|
2902
|
+
* every expression where it stands, and the editor's diagnostics come
|
|
2903
|
+
* from the same function (0009). A report its diagnostics give an error
|
|
2904
|
+
* for is refused before anything is evaluated, so the runtime never runs
|
|
2905
|
+
* what the editor refuses. Each expression is compiled once per render,
|
|
2906
|
+
* never once per row -- twice, in fact, since `diagnose` does not hand
|
|
2907
|
+
* back the compilation it made.
|
|
2908
|
+
* - **One binding.** Each program's values are made by report-core's
|
|
2909
|
+
* `expressionValues`: an integer as a BigInt, a decimal as a `Decimal`,
|
|
2910
|
+
* an instant as a moment, a date as its ISO string. A value missing, or of
|
|
2911
|
+
* another type, fails the evaluation, naming the member and never quoting
|
|
2912
|
+
* the value.
|
|
2913
|
+
* - **Nothing differs between renders.** The diagnostics refuse `now()`,
|
|
2914
|
+
* `today()` and `random()`; the capabilities an evaluation is handed are
|
|
2915
|
+
* fixed besides, so that one reached anyway would answer the same in every
|
|
2916
|
+
* render.
|
|
2917
|
+
* - **Bounded.** Each evaluation has a budget of steps, and a render has one
|
|
2918
|
+
* for all its evaluations together. Past either, the evaluation is refused.
|
|
2919
|
+
* - **A failure is an error, never a blank.** It names the component, the
|
|
2920
|
+
* table column or the aggregate, and the detail row, and gives upstream's
|
|
2921
|
+
* code for what went wrong, never upstream's message, which can quote the
|
|
2922
|
+
* values it was computing with.
|
|
2923
|
+
*
|
|
2924
|
+
* What an expression gives is handed back typed as the runtime holds values:
|
|
2925
|
+
* an integer as a BigInt, an instant in UTC. A list or a map, which has no
|
|
2926
|
+
* text, is refused. A double never arrives: the diagnostics refuse every
|
|
2927
|
+
* double literal, every `double()` and every `/` before anything is
|
|
2928
|
+
* evaluated (report-core's `exactness.ts`), since upstream hands an integer
|
|
2929
|
+
* and a double back alike, as a JavaScript number, and `int()` of a double
|
|
2930
|
+
* would otherwise print as an exact integer. A number that is not a safe
|
|
2931
|
+
* integer is still refused here, rather than printed.
|
|
2932
|
+
*/
|
|
2933
|
+
/**
|
|
2934
|
+
* Steps one evaluation may take: upstream's default for a formancy form's
|
|
2935
|
+
* expression (`DEFAULT_BUDGET`). A report expression reads single values, so
|
|
2936
|
+
* the only way to spend many steps is a comprehension over a list literal;
|
|
2937
|
+
* the deepest the structural limits allow, 64 by 64, measured 8,385 steps,
|
|
2938
|
+
* and `evaluate.test.ts` holds that it fits while one reading three values
|
|
2939
|
+
* in each turn (28,865 steps) does not.
|
|
2940
|
+
*/
|
|
2941
|
+
const EXPRESSION_STEP_BUDGET = DEFAULT_BUDGET.maxSteps;
|
|
2942
|
+
/**
|
|
2943
|
+
* Steps every evaluation of one render may take together, each charged its
|
|
2944
|
+
* steps and one more for itself. Measured as a render spends them, through
|
|
2945
|
+
* `condition()` with each evaluation's binding included, on 15 October
|
|
2946
|
+
* 2026, on an Intel i9-10900K under Node 24.21 with `@formancy/expressions`
|
|
2947
|
+
* 0.5.0: ten million steps of `row.urgent` took 10.0 seconds (1.00
|
|
2948
|
+
* microseconds a step) and of `row.amount * dec("1.081") > dec("10")` 13.0
|
|
2949
|
+
* (1.30). An earlier measurement, of upstream's `evaluate` alone, had put
|
|
2950
|
+
* that budget at 8.5 seconds and missed the binding. So the budget is five
|
|
2951
|
+
* million: about 5 to 6.5 seconds of evaluation on that machine, against
|
|
2952
|
+
* the plan's investigation target of ten seconds in all for a hundred-page
|
|
2953
|
+
* report. A report of `MAX_DETAIL_ROWS` rows evaluating ten one-read
|
|
2954
|
+
* expressions per row spends 2,000,000.
|
|
2955
|
+
*/
|
|
2956
|
+
const RENDER_STEP_BUDGET = 5e6;
|
|
2957
|
+
/**
|
|
2958
|
+
* What `now()`, `today()` and `random()` would give: nothing a report may
|
|
2959
|
+
* read (0009), fixed so that one reached anyway gives the same answer in
|
|
2960
|
+
* every render.
|
|
2961
|
+
*/
|
|
2962
|
+
const CAPABILITIES = fixedCapabilities({
|
|
2963
|
+
nowMs: 0,
|
|
2964
|
+
today: "1970-01-01",
|
|
2965
|
+
random: 0
|
|
2966
|
+
});
|
|
2967
|
+
const EVALUATION_ERROR_CODES = [
|
|
2968
|
+
"unknown-expression",
|
|
2969
|
+
"missing-value",
|
|
1378
2970
|
"unbound-value",
|
|
1379
2971
|
"expression-failed",
|
|
1380
2972
|
"expression-budget",
|
|
@@ -1511,24 +3103,143 @@ var Evaluator = class {
|
|
|
1511
3103
|
}
|
|
1512
3104
|
};
|
|
1513
3105
|
/**
|
|
1514
|
-
* `report`'s expressions compiled for one render
|
|
1515
|
-
* refuse it: every error `diagnose` gives, which the
|
|
1516
|
-
*
|
|
1517
|
-
*
|
|
3106
|
+
* `report`'s expressions compiled for one render in `locale`, or the
|
|
3107
|
+
* diagnostics that refuse it: every error `diagnose` gives, which the
|
|
3108
|
+
* editor shows too, that names no language or names `locale` (0063). What
|
|
3109
|
+
* is compiled is `report` written in `locale`, which the render prints.
|
|
3110
|
+
* `report` is one the validator accepts, and `locale` the report's own or
|
|
3111
|
+
* one it declares, as `printLocale` holds it. The evaluations of one
|
|
3112
|
+
* render share one budget, so a render prepares its own.
|
|
1518
3113
|
*/
|
|
1519
|
-
function prepareExpressions(report) {
|
|
1520
|
-
const errors = diagnose(report).filter((
|
|
3114
|
+
function prepareExpressions(report, locale = report.locale) {
|
|
3115
|
+
const errors = diagnose(report).filter((diagnostic) => refuses(diagnostic, locale));
|
|
1521
3116
|
if (errors.length > 0) return {
|
|
1522
3117
|
ok: false,
|
|
1523
3118
|
diagnostics: errors
|
|
1524
3119
|
};
|
|
3120
|
+
const printed = reportIn(report, locale);
|
|
1525
3121
|
return {
|
|
1526
3122
|
ok: true,
|
|
1527
|
-
|
|
3123
|
+
report: printed,
|
|
3124
|
+
expressions: new Evaluator(printed, compileReport(printed))
|
|
3125
|
+
};
|
|
3126
|
+
}
|
|
3127
|
+
// src/page-label.ts
|
|
3128
|
+
/**
|
|
3129
|
+
* A label, as the runtime heads every page with one -- a preview's, an
|
|
3130
|
+
* XRechnung's copy's (0038), a copy of an e-invoice's (0077): the text
|
|
3131
|
+
* alone, small, bold and red, in a paragraph of its own.
|
|
3132
|
+
*/
|
|
3133
|
+
const label = (text) => ({
|
|
3134
|
+
kind: "paragraph",
|
|
3135
|
+
runs: [{ text }],
|
|
3136
|
+
style: {
|
|
3137
|
+
size: 8,
|
|
3138
|
+
bold: true,
|
|
3139
|
+
color: "#b00020"
|
|
3140
|
+
}
|
|
3141
|
+
});
|
|
3142
|
+
/** `paragraph` frozen through: its runs, each run, and its style. */
|
|
3143
|
+
function frozen(paragraph) {
|
|
3144
|
+
for (const run of paragraph.runs) Object.freeze(run);
|
|
3145
|
+
Object.freeze(paragraph.runs);
|
|
3146
|
+
Object.freeze(paragraph.style);
|
|
3147
|
+
return Object.freeze(paragraph);
|
|
3148
|
+
}
|
|
3149
|
+
// src/e-invoice-copy.ts
|
|
3150
|
+
/**
|
|
3151
|
+
* The label of a copy of an e-invoice (0077): what every page of a final
|
|
3152
|
+
* render says where a host asked for the copy and not the invoice
|
|
3153
|
+
* (`ResolveOptions.eInvoice`). A copy is what a Word file is written of:
|
|
3154
|
+
* a file a person may change is no invoice, so its every page says that
|
|
3155
|
+
* it is a copy, that it is not the invoice, and what the invoice is --
|
|
3156
|
+
* the PDF that embeds its XML, of a Factur-X, or its XML, of an
|
|
3157
|
+
* XRechnung. It carries no XML itself.
|
|
3158
|
+
*
|
|
3159
|
+
* The words are the runtime's, in the language the render prints in
|
|
3160
|
+
* (0063): the label is the one thing that tells a reader what the page
|
|
3161
|
+
* is, and in a language the page does not speak it tells them nothing.
|
|
3162
|
+
* So a copy in a language no label is written in is refused, never
|
|
3163
|
+
* labelled in another. It says "this document", not "this Word file":
|
|
3164
|
+
* the copy is a resolved document, and whatever is made of it is no
|
|
3165
|
+
* invoice. A preview's label and an XRechnung's PDF's stay English, as
|
|
3166
|
+
* they were (0038).
|
|
3167
|
+
*
|
|
3168
|
+
* One decision, here: the runtime writes the label, and an engine reads
|
|
3169
|
+
* it with `readCopyLabel` and decides no word of it.
|
|
3170
|
+
*/
|
|
3171
|
+
/** The languages a copy is labelled in: those the starters print in. A locale's language is its first subtag. */
|
|
3172
|
+
const E_INVOICE_COPY_LANGUAGES = [
|
|
3173
|
+
"de",
|
|
3174
|
+
"en",
|
|
3175
|
+
"es",
|
|
3176
|
+
"fr",
|
|
3177
|
+
"it",
|
|
3178
|
+
"nl"
|
|
3179
|
+
];
|
|
3180
|
+
/** What every page of a copy says, by the e-invoice's kind and the render's language. */
|
|
3181
|
+
const E_INVOICE_COPY_LABELS = Object.freeze({
|
|
3182
|
+
facturX: Object.freeze({
|
|
3183
|
+
de: "Kopie einer E-Rechnung: Die Rechnung ist das PDF, das ihr XML einbettet, und dieses Dokument ist nicht die Rechnung",
|
|
3184
|
+
en: "Copy of an e-invoice: the invoice is the PDF that embeds its XML, and this document is not the invoice",
|
|
3185
|
+
es: "Copia de una factura electrónica: la factura es el PDF que incorpora su XML, y este documento no es la factura",
|
|
3186
|
+
fr: "Copie d'une facture électronique\xA0: la facture est le PDF qui intègre son XML, et ce document n'est pas la facture",
|
|
3187
|
+
it: "Copia di una fattura elettronica: la fattura è il PDF che incorpora il suo XML, e questo documento non è la fattura",
|
|
3188
|
+
nl: "Kopie van een e-factuur: de factuur is de pdf waarin haar XML is ingebed, en dit document is niet de factuur"
|
|
3189
|
+
}),
|
|
3190
|
+
xrechnung: Object.freeze({
|
|
3191
|
+
de: "Kopie einer XRechnung: Die Rechnung ist ihr XML, und dieses Dokument ist nicht die Rechnung",
|
|
3192
|
+
en: "Copy of an XRechnung: the invoice is its XML, and this document is not the invoice",
|
|
3193
|
+
es: "Copia de una XRechnung: la factura es su XML, y este documento no es la factura",
|
|
3194
|
+
fr: "Copie d'une XRechnung\xA0: la facture est son XML, et ce document n'est pas la facture",
|
|
3195
|
+
it: "Copia di una XRechnung: la fattura è il suo XML, e questo documento non è la fattura",
|
|
3196
|
+
nl: "Kopie van een XRechnung: de factuur is haar XML, en dit document is niet de factuur"
|
|
3197
|
+
})
|
|
3198
|
+
});
|
|
3199
|
+
/** Every label's paragraph, made once and frozen: what a document's own paragraph is held to. */
|
|
3200
|
+
const PARAGRAPHS = ["facturX", "xrechnung"].flatMap((kind) => E_INVOICE_COPY_LANGUAGES.map((language) => ({
|
|
3201
|
+
kind,
|
|
3202
|
+
language,
|
|
3203
|
+
paragraph: frozen(label(E_INVOICE_COPY_LABELS[kind][language]))
|
|
3204
|
+
})));
|
|
3205
|
+
/**
|
|
3206
|
+
* The paragraph a copy of an e-invoice of `kind` is labelled with in a
|
|
3207
|
+
* render that prints in `locale`, as the runtime writes it; undefined
|
|
3208
|
+
* where no label is written in that locale's language. Frozen, and the
|
|
3209
|
+
* same object each time: a document carries a paragraph of its own.
|
|
3210
|
+
*/
|
|
3211
|
+
function eInvoiceCopyParagraph(kind, locale) {
|
|
3212
|
+
const language = locale.split("-")[0].toLowerCase();
|
|
3213
|
+
return PARAGRAPHS.find((each) => each.kind === kind && each.language === language)?.paragraph;
|
|
3214
|
+
}
|
|
3215
|
+
/**
|
|
3216
|
+
* The copy's label `header` opens with, or undefined where it opens with
|
|
3217
|
+
* none: a paragraph of one run whose text is a label's, whatever its
|
|
3218
|
+
* style, as a preview's label is read (0057). `asWritten` says whether
|
|
3219
|
+
* the paragraph is the runtime's whole -- its size, bold and its colour,
|
|
3220
|
+
* and nothing else -- which an engine that writes a copy holds it to:
|
|
3221
|
+
* the words in white say nothing to a reader. A label among other runs,
|
|
3222
|
+
* or below another block, heads nothing and is not found.
|
|
3223
|
+
*/
|
|
3224
|
+
function readCopyLabel(header) {
|
|
3225
|
+
const first = header[0];
|
|
3226
|
+
if (first?.kind !== "paragraph" || first.runs.length !== 1) return void 0;
|
|
3227
|
+
const run = first.runs[0];
|
|
3228
|
+
if (!("text" in run)) return void 0;
|
|
3229
|
+
const known = PARAGRAPHS.find(({ paragraph }) => paragraph.runs[0].text === run.text);
|
|
3230
|
+
return known === void 0 ? void 0 : {
|
|
3231
|
+
...known,
|
|
3232
|
+
asWritten: samePlain(first, known.paragraph)
|
|
1528
3233
|
};
|
|
1529
3234
|
}
|
|
1530
|
-
|
|
1531
|
-
|
|
3235
|
+
/** Whether two values of a resolved document are the same data: equal primitives, or two arrays, or two objects that are not, of the same keys holding the same values. */
|
|
3236
|
+
function samePlain(a, b) {
|
|
3237
|
+
if (typeof a !== "object" || typeof b !== "object" || a === null || b === null) return a === b;
|
|
3238
|
+
if (Array.isArray(a) !== Array.isArray(b)) return false;
|
|
3239
|
+
const [left, right] = [Object.keys(a), Object.keys(b)];
|
|
3240
|
+
return left.length === right.length && left.every((key) => right.includes(key) && samePlain(a[key], b[key]));
|
|
3241
|
+
}
|
|
3242
|
+
// src/xml.ts
|
|
1532
3243
|
/**
|
|
1533
3244
|
* XML, written one way: a tree of elements serialised with the same bytes
|
|
1534
3245
|
* for the same tree, every text escaped so that a parser gives back exactly
|
|
@@ -1614,8 +3325,7 @@ function xmlDocument(root) {
|
|
|
1614
3325
|
write(root, 0, lines);
|
|
1615
3326
|
return `${lines.join("\n")}\n`;
|
|
1616
3327
|
}
|
|
1617
|
-
|
|
1618
|
-
//#region src/cii-parties.ts
|
|
3328
|
+
// src/cii-parties.ts
|
|
1619
3329
|
/** `make` of `value` where there is one; nothing where the term is left out. */
|
|
1620
3330
|
function optional$1(value, make) {
|
|
1621
3331
|
return value === void 0 ? void 0 : make(value);
|
|
@@ -1630,17 +3340,30 @@ function postalAddress(address) {
|
|
|
1630
3340
|
leaf("ram:CountryID", address.country)
|
|
1631
3341
|
]);
|
|
1632
3342
|
}
|
|
3343
|
+
/** BG-6: the contact point (BT-41), the telephone number (BT-42) and the email address (BT-43), all three, as a contact gives them; no element where the seller names none. */
|
|
3344
|
+
function tradeContact(contact) {
|
|
3345
|
+
if (contact === void 0) return void 0;
|
|
3346
|
+
return element("ram:DefinedTradeContact", [
|
|
3347
|
+
leaf("ram:PersonName", contact.name),
|
|
3348
|
+
element("ram:TelephoneUniversalCommunication", [leaf("ram:CompleteNumber", contact.telephone)]),
|
|
3349
|
+
element("ram:EmailURIUniversalCommunication", [leaf("ram:URIID", contact.email)])
|
|
3350
|
+
]);
|
|
3351
|
+
}
|
|
1633
3352
|
/**
|
|
1634
3353
|
* The seller or the buyer: its name (BT-27, BT-44), its legal registration
|
|
1635
|
-
* and its scheme (BT-30, BT-47),
|
|
1636
|
-
*
|
|
3354
|
+
* and its scheme (BT-30, BT-47), the seller's contact (BG-6), its address,
|
|
3355
|
+
* its electronic address and its scheme (BT-34, BT-49), its VAT
|
|
3356
|
+
* identifier (BT-31, BT-48) and, the seller's, its tax number (BT-32).
|
|
1637
3357
|
*/
|
|
1638
3358
|
function tradeParty(name, party) {
|
|
1639
3359
|
const registration = party.legalId;
|
|
3360
|
+
const reached = party.electronicAddress;
|
|
1640
3361
|
return element(name, [
|
|
1641
3362
|
leaf("ram:Name", party.name),
|
|
1642
3363
|
registration === void 0 ? void 0 : element("ram:SpecifiedLegalOrganization", [leaf("ram:ID", registration.id, registration.scheme === void 0 ? [] : [["schemeID", registration.scheme]])]),
|
|
3364
|
+
tradeContact(party.contact),
|
|
1643
3365
|
postalAddress(party.address),
|
|
3366
|
+
reached === void 0 ? void 0 : element("ram:URIUniversalCommunication", [leaf("ram:URIID", reached.id, [["schemeID", reached.scheme]])]),
|
|
1644
3367
|
optional$1(party.vatId, (id) => element("ram:SpecifiedTaxRegistration", [leaf("ram:ID", id, [["schemeID", "VA"]])])),
|
|
1645
3368
|
optional$1(party.taxNumber, (id) => element("ram:SpecifiedTaxRegistration", [leaf("ram:ID", id, [["schemeID", "FC"]])]))
|
|
1646
3369
|
]);
|
|
@@ -1657,16 +3380,18 @@ function paymentMeans(payment) {
|
|
|
1657
3380
|
optional$1(payment.bic, (bic) => element("ram:PayeeSpecifiedCreditorFinancialInstitution", [leaf("ram:BICID", bic)]))
|
|
1658
3381
|
]);
|
|
1659
3382
|
}
|
|
1660
|
-
|
|
1661
|
-
//#region src/cii.ts
|
|
3383
|
+
// src/cii.ts
|
|
1662
3384
|
/**
|
|
1663
3385
|
* An e-invoice written as UN/CEFACT Cross Industry Invoice XML in the
|
|
1664
|
-
* Factur-X 1.09.2 / ZUGFeRD 2.5.2 EN 16931 profile (0027)
|
|
1665
|
-
* as the QR-bill's payload is, since
|
|
1666
|
-
* the renderer decides none of it.
|
|
3386
|
+
* Factur-X 1.09.2 / ZUGFeRD 2.5.2 EN 16931 profile (0027), or as an
|
|
3387
|
+
* XRechnung 3.0 (0038): the runtime's, as the QR-bill's payload is, since
|
|
3388
|
+
* the XML is the invoice's payload and the renderer decides none of it.
|
|
3389
|
+
* One writer for both kinds: the XML of an XRechnung is the XML of a
|
|
3390
|
+
* Factur-X invoice of the same terms, but for the specification
|
|
3391
|
+
* identifier (BT-24) it opens with, which says whose rules it follows.
|
|
1667
3392
|
*
|
|
1668
3393
|
* - **Its shape.** The root `rsm:CrossIndustryInvoice` with the four
|
|
1669
|
-
* namespaces of CEN/TC 434's binding; its context (BT-24), its document
|
|
3394
|
+
* namespaces of CEN/TC 434's binding; its context (BT-23, BT-24), its document
|
|
1670
3395
|
* (BT-1, BT-3, BT-2) and its transaction: the lines, then the
|
|
1671
3396
|
* agreement, the delivery and the settlement, each element in the order
|
|
1672
3397
|
* the profile's XSD `xs:sequence` gives it, at the path CEN's binding
|
|
@@ -1753,7 +3478,7 @@ function ciiXml(invoice) {
|
|
|
1753
3478
|
const { terms, totals } = invoice;
|
|
1754
3479
|
const breakdowns = new Map(totals.breakdown.map((breakdown) => [breakdown.category.id, breakdown]));
|
|
1755
3480
|
return xmlDocument(element("rsm:CrossIndustryInvoice", [
|
|
1756
|
-
element("rsm:ExchangedDocumentContext", [element("ram:GuidelineSpecifiedDocumentContextParameter", [leaf("ram:ID",
|
|
3481
|
+
element("rsm:ExchangedDocumentContext", [optional$1(terms.businessProcess, (process) => element("ram:BusinessProcessSpecifiedDocumentContextParameter", [leaf("ram:ID", process)])), element("ram:GuidelineSpecifiedDocumentContextParameter", [leaf("ram:ID", invoice.guideline)])]),
|
|
1757
3482
|
element("rsm:ExchangedDocument", [
|
|
1758
3483
|
leaf("ram:ID", terms.number),
|
|
1759
3484
|
leaf("ram:TypeCode", invoice.type),
|
|
@@ -1771,8 +3496,7 @@ function ciiXml(invoice) {
|
|
|
1771
3496
|
])
|
|
1772
3497
|
], NAMESPACES));
|
|
1773
3498
|
}
|
|
1774
|
-
|
|
1775
|
-
//#region src/e-invoice-totals.ts
|
|
3499
|
+
// src/e-invoice-totals.ts
|
|
1776
3500
|
/**
|
|
1777
3501
|
* An e-invoice's totals and VAT breakdown, computed once, here, from its
|
|
1778
3502
|
* lines (0027): the page prints them through `invoice`, `vatBasis` and
|
|
@@ -1813,7 +3537,7 @@ function cents(amount, what) {
|
|
|
1813
3537
|
if (amount.scale !== 2) throw new RangeError(`${what} is at ${amount.scale} places, and the e-invoice's lines are read at two`);
|
|
1814
3538
|
return amount;
|
|
1815
3539
|
}
|
|
1816
|
-
const sum
|
|
3540
|
+
const sum = (amounts) => {
|
|
1817
3541
|
let total = ZERO;
|
|
1818
3542
|
for (const amount of amounts) total = addDecimal(total, amount);
|
|
1819
3543
|
return total;
|
|
@@ -1844,7 +3568,7 @@ function invoiceTotals(categories, lines, paid) {
|
|
|
1844
3568
|
const breakdown = categories.flatMap((category) => {
|
|
1845
3569
|
const own = lines.flatMap((line, index) => line.category === category ? [amounts[index]] : []);
|
|
1846
3570
|
if (own.length === 0) return [];
|
|
1847
|
-
const basis = sum
|
|
3571
|
+
const basis = sum(own);
|
|
1848
3572
|
return [{
|
|
1849
3573
|
category,
|
|
1850
3574
|
rate: checkVatRate(category.code, category.rate).value,
|
|
@@ -1852,8 +3576,8 @@ function invoiceTotals(categories, lines, paid) {
|
|
|
1852
3576
|
amount: tax(basis, category.rate)
|
|
1853
3577
|
}];
|
|
1854
3578
|
});
|
|
1855
|
-
const lineTotal = sum
|
|
1856
|
-
const taxTotal = sum
|
|
3579
|
+
const lineTotal = sum(amounts);
|
|
3580
|
+
const taxTotal = sum(breakdown.map(({ amount }) => amount));
|
|
1857
3581
|
const grandTotal = addDecimal(lineTotal, taxTotal);
|
|
1858
3582
|
const prepaid = paid === null ? null : cents(paid, "the paid amount");
|
|
1859
3583
|
return {
|
|
@@ -1863,269 +3587,44 @@ function invoiceTotals(categories, lines, paid) {
|
|
|
1863
3587
|
grandTotal,
|
|
1864
3588
|
paid: prepaid,
|
|
1865
3589
|
duePayable: prepaid === null ? grandTotal : subtractDecimal(grandTotal, prepaid),
|
|
1866
|
-
breakdown
|
|
1867
|
-
};
|
|
1868
|
-
}
|
|
1869
|
-
/**
|
|
1870
|
-
* What the e-invoice's names hold, as an expression is handed them (0027,
|
|
1871
|
-
* beside 0009): `invoice` each document total, `paid` only where the
|
|
1872
|
-
* invoice declares a paid amount, as report-core binds it; `vatBasis` and
|
|
1873
|
-
* `vatAmount` each declared category's basis and tax by its id, null where
|
|
1874
|
-
* no line is in it, which prints as nothing (0007). The very values the
|
|
1875
|
-
* XML is written from.
|
|
1876
|
-
*/
|
|
1877
|
-
function invoiceNames$1(invoice, totals) {
|
|
1878
|
-
const of = (category) => totals.breakdown.find((each) => each.category.id === category.id);
|
|
1879
|
-
return {
|
|
1880
|
-
invoice: {
|
|
1881
|
-
lineTotal: totals.lineTotal,
|
|
1882
|
-
taxBasisTotal: totals.taxBasisTotal,
|
|
1883
|
-
taxTotal: totals.taxTotal,
|
|
1884
|
-
grandTotal: totals.grandTotal,
|
|
1885
|
-
...invoice.paid === void 0 ? {} : { paid: totals.paid },
|
|
1886
|
-
duePayable: totals.duePayable
|
|
1887
|
-
},
|
|
1888
|
-
vatBasis: Object.fromEntries(invoice.vat.map((category) => [category.id, of(category)?.basis ?? null])),
|
|
1889
|
-
vatAmount: Object.fromEntries(invoice.vat.map((category) => [category.id, of(category)?.amount ?? null]))
|
|
1890
|
-
};
|
|
1891
|
-
}
|
|
1892
|
-
//#endregion
|
|
1893
|
-
//#region src/resolve-errors.ts
|
|
1894
|
-
/** What lowering a report can find wrong besides what each earlier stage finds. */
|
|
1895
|
-
const RESOLVE_ERROR_CODES = [
|
|
1896
|
-
"invalid-creation-date",
|
|
1897
|
-
"missing-asset",
|
|
1898
|
-
"invalid-asset",
|
|
1899
|
-
"asset-lookup-failed",
|
|
1900
|
-
"rounding-not-decimal",
|
|
1901
|
-
"aggregate-value-type",
|
|
1902
|
-
"no-qr-value",
|
|
1903
|
-
"empty-qr-code",
|
|
1904
|
-
"invalid-qr-bill",
|
|
1905
|
-
"invalid-epc-qr-code",
|
|
1906
|
-
"invalid-e-invoice",
|
|
1907
|
-
"e-invoice-preview",
|
|
1908
|
-
"too-many-errors"
|
|
1909
|
-
];
|
|
1910
|
-
/** The optional parts of a place, written only when present, as exactOptionalPropertyTypes asks. */
|
|
1911
|
-
function placed(place) {
|
|
1912
|
-
return {
|
|
1913
|
-
...place.componentId === void 0 ? {} : { componentId: place.componentId },
|
|
1914
|
-
...place.columnId === void 0 ? {} : { columnId: place.columnId },
|
|
1915
|
-
...place.row === void 0 ? {} : { row: place.row }
|
|
1916
|
-
};
|
|
1917
|
-
}
|
|
1918
|
-
/** A type as a message says what a value is: "a decimal", "an integer". */
|
|
1919
|
-
function aType(type) {
|
|
1920
|
-
return `${/^[aeiou]/.test(type) ? "an" : "a"} ${type}`;
|
|
1921
|
-
}
|
|
1922
|
-
function fromReport(error) {
|
|
1923
|
-
return {
|
|
1924
|
-
stage: "report",
|
|
1925
|
-
code: error.code,
|
|
1926
|
-
path: error.path,
|
|
1927
|
-
message: error.message,
|
|
1928
|
-
...placed(error)
|
|
1929
|
-
};
|
|
1930
|
-
}
|
|
1931
|
-
function fromDiagnostic(diagnostic) {
|
|
1932
|
-
return {
|
|
1933
|
-
stage: "expressions",
|
|
1934
|
-
code: diagnostic.code,
|
|
1935
|
-
path: diagnostic.path,
|
|
1936
|
-
message: diagnostic.message,
|
|
1937
|
-
...placed(diagnostic)
|
|
1938
|
-
};
|
|
1939
|
-
}
|
|
1940
|
-
function fromData(stage, error) {
|
|
1941
|
-
return {
|
|
1942
|
-
stage,
|
|
1943
|
-
code: error.code,
|
|
1944
|
-
path: error.path,
|
|
1945
|
-
message: error.message
|
|
1946
|
-
};
|
|
1947
|
-
}
|
|
1948
|
-
function fromEvaluation(error) {
|
|
1949
|
-
return {
|
|
1950
|
-
stage: "evaluation",
|
|
1951
|
-
code: error.code,
|
|
1952
|
-
path: error.path,
|
|
1953
|
-
message: error.message,
|
|
1954
|
-
...placed(error),
|
|
1955
|
-
...error.aggregateId === void 0 ? {} : { aggregateId: error.aggregateId }
|
|
1956
|
-
};
|
|
1957
|
-
}
|
|
1958
|
-
/**
|
|
1959
|
-
* The errors and warnings one resolution collects. Errors are bounded as the
|
|
1960
|
-
* data check bounds its own: past `MAX_DATA_ERRORS`, one `too-many-errors`
|
|
1961
|
-
* says the list was cut short, and `full` tells a long loop to stop. A
|
|
1962
|
-
* warning is said once per path: a preview that lacks a field a thousand
|
|
1963
|
-
* rows read warns once for it.
|
|
1964
|
-
*/
|
|
1965
|
-
var Problems = class {
|
|
1966
|
-
#errors = [];
|
|
1967
|
-
#warnings = /* @__PURE__ */ new Map();
|
|
1968
|
-
#overflowed = false;
|
|
1969
|
-
error(error) {
|
|
1970
|
-
if (this.#errors.length < MAX_DATA_ERRORS) this.#errors.push(error);
|
|
1971
|
-
else this.#overflowed = true;
|
|
1972
|
-
}
|
|
1973
|
-
warn(warning) {
|
|
1974
|
-
const key = `${warning.stage} ${warning.code} ${warning.path}`;
|
|
1975
|
-
if (!this.#warnings.has(key)) this.#warnings.set(key, warning);
|
|
1976
|
-
}
|
|
1977
|
-
/** Whether the error list is cut short: nothing more is worth computing. */
|
|
1978
|
-
get full() {
|
|
1979
|
-
return this.#overflowed;
|
|
1980
|
-
}
|
|
1981
|
-
get failed() {
|
|
1982
|
-
return this.#errors.length > 0;
|
|
1983
|
-
}
|
|
1984
|
-
errors() {
|
|
1985
|
-
const errors = [...this.#errors];
|
|
1986
|
-
if (this.#overflowed) errors.push({
|
|
1987
|
-
stage: "resolve",
|
|
1988
|
-
code: "too-many-errors",
|
|
1989
|
-
path: "",
|
|
1990
|
-
message: `more than ${MAX_DATA_ERRORS} errors were found; the first ${MAX_DATA_ERRORS} are listed`
|
|
1991
|
-
});
|
|
1992
|
-
return errors;
|
|
1993
|
-
}
|
|
1994
|
-
warnings() {
|
|
1995
|
-
return [...this.#warnings.values()];
|
|
1996
|
-
}
|
|
1997
|
-
};
|
|
1998
|
-
//#endregion
|
|
1999
|
-
//#region src/render-context.ts
|
|
2000
|
-
/** Each aggregate's value as an expression is handed it: the typed value's own, or null. */
|
|
2001
|
-
function raw(values) {
|
|
2002
|
-
return Object.fromEntries([...values].map(([id, value]) => [id, value === null ? null : value.value]));
|
|
2003
|
-
}
|
|
2004
|
-
/** The header record, or none when the report declares no header dataset. */
|
|
2005
|
-
function headerOf(render) {
|
|
2006
|
-
return render.data.header ?? {};
|
|
2007
|
-
}
|
|
2008
|
-
/** The e-invoice's names, read wherever `totals` is, once they are computed; none in a report that is no e-invoice. */
|
|
2009
|
-
function invoiceNames(render) {
|
|
2010
|
-
return render.invoice?.names ?? {};
|
|
2011
|
-
}
|
|
2012
|
-
function reportScope(render, totals) {
|
|
2013
|
-
const header = headerOf(render);
|
|
2014
|
-
return {
|
|
2015
|
-
values: {
|
|
2016
|
-
params: render.params,
|
|
2017
|
-
header,
|
|
2018
|
-
totals: raw(totals),
|
|
2019
|
-
...invoiceNames(render)
|
|
2020
|
-
},
|
|
2021
|
-
currency: {
|
|
2022
|
-
params: render.params,
|
|
2023
|
-
header
|
|
2024
|
-
}
|
|
2025
|
-
};
|
|
2026
|
-
}
|
|
2027
|
-
/**
|
|
2028
|
-
* A group's header and footer: the report's values, and the group's
|
|
2029
|
-
* aggregates and key as `group`. `by` is the field grouped by; where a
|
|
2030
|
-
* preview's rows lack it the key is missing, as the field is, not null.
|
|
2031
|
-
*/
|
|
2032
|
-
function groupScope(render, group, aggregates, by) {
|
|
2033
|
-
const record = group.rows[0].record;
|
|
2034
|
-
const header = headerOf(render);
|
|
2035
|
-
const key = Object.hasOwn(record, by) ? { key: group.key } : {};
|
|
2036
|
-
return {
|
|
2037
|
-
values: {
|
|
2038
|
-
params: render.params,
|
|
2039
|
-
header,
|
|
2040
|
-
totals: raw(render.totals.report),
|
|
2041
|
-
group: {
|
|
2042
|
-
...raw(aggregates),
|
|
2043
|
-
...key
|
|
2044
|
-
},
|
|
2045
|
-
...invoiceNames(render)
|
|
2046
|
-
},
|
|
2047
|
-
currency: {
|
|
2048
|
-
params: render.params,
|
|
2049
|
-
header,
|
|
2050
|
-
row: record
|
|
2051
|
-
},
|
|
2052
|
-
group: aggregates,
|
|
2053
|
-
record
|
|
2054
|
-
};
|
|
2055
|
-
}
|
|
2056
|
-
/** One detail row: the report's values, and the row's as `row`. */
|
|
2057
|
-
function rowScope(render, totals, row) {
|
|
2058
|
-
const header = headerOf(render);
|
|
2059
|
-
return {
|
|
2060
|
-
values: {
|
|
2061
|
-
params: render.params,
|
|
2062
|
-
header,
|
|
2063
|
-
totals: raw(totals),
|
|
2064
|
-
row: row.record,
|
|
2065
|
-
...invoiceNames(render)
|
|
2066
|
-
},
|
|
2067
|
-
currency: {
|
|
2068
|
-
params: render.params,
|
|
2069
|
-
header,
|
|
2070
|
-
row: row.record
|
|
2071
|
-
},
|
|
2072
|
-
record: row.record,
|
|
2073
|
-
row: row.index
|
|
2074
|
-
};
|
|
2075
|
-
}
|
|
2076
|
-
/** A failed evaluation: an error, or in a preview a value the data lacks, which is a warning. Returns whether it was only a warning. */
|
|
2077
|
-
function failed(render, outcome) {
|
|
2078
|
-
if (render.preview && outcome.error.code === "missing-value") {
|
|
2079
|
-
const error = fromEvaluation(outcome.error);
|
|
2080
|
-
render.problems.warn({
|
|
2081
|
-
...error,
|
|
2082
|
-
message: `${error.message}; this preview prints nothing for it`
|
|
2083
|
-
});
|
|
2084
|
-
return true;
|
|
2085
|
-
}
|
|
2086
|
-
render.problems.error(fromEvaluation(outcome.error));
|
|
2087
|
-
return false;
|
|
2088
|
-
}
|
|
2089
|
-
/** The value of the expression whose `cel` is at `path`, read in `scope`; null for none, undefined when it failed and the failure is recorded. */
|
|
2090
|
-
function valueOf(render, scope, path) {
|
|
2091
|
-
const outcome = render.expressions.evaluate(path, scope.values, scope.row);
|
|
2092
|
-
if (outcome.ok) return outcome.value;
|
|
2093
|
-
return failed(render, outcome) ? null : void 0;
|
|
2094
|
-
}
|
|
2095
|
-
/** Whether the condition whose `cel` is at `path` holds in `scope`; undefined when it failed and the failure is recorded as an error. */
|
|
2096
|
-
function holds(render, scope, path) {
|
|
2097
|
-
const outcome = render.expressions.condition(path, scope.values, scope.row);
|
|
2098
|
-
if (outcome.ok) return outcome.value;
|
|
2099
|
-
return failed(render, outcome) ? false : void 0;
|
|
2100
|
-
}
|
|
2101
|
-
/** The type the report's contract gives `ref`'s field; the validator has checked that it declares one. */
|
|
2102
|
-
function fieldType(report, ref) {
|
|
2103
|
-
return report.datasets[ref.dataset]?.fields.find(({ id }) => id === ref.field)?.type;
|
|
2104
|
-
}
|
|
2105
|
-
function parameterType(report, parameter) {
|
|
2106
|
-
return report.parameters.find(({ id }) => id === parameter)?.type;
|
|
3590
|
+
breakdown
|
|
3591
|
+
};
|
|
2107
3592
|
}
|
|
2108
3593
|
/**
|
|
2109
|
-
*
|
|
2110
|
-
*
|
|
2111
|
-
*
|
|
2112
|
-
*
|
|
3594
|
+
* What the e-invoice's names hold, as an expression is handed them (0027,
|
|
3595
|
+
* beside 0009): `invoice` each document total, `paid` only where the
|
|
3596
|
+
* invoice declares a paid amount, as report-core binds it; `vatBasis` and
|
|
3597
|
+
* `vatAmount` each declared category's basis and tax by its id, null where
|
|
3598
|
+
* no line is in it, which prints as nothing (0007). The very values the
|
|
3599
|
+
* XML is written from.
|
|
2113
3600
|
*/
|
|
2114
|
-
function
|
|
2115
|
-
const
|
|
2116
|
-
const type = fieldType(render.report, ref);
|
|
2117
|
-
if (value === null || type === "image") return null;
|
|
3601
|
+
function invoiceNames(invoice, totals) {
|
|
3602
|
+
const of = (category) => totals.breakdown.find((each) => each.category.id === category.id);
|
|
2118
3603
|
return {
|
|
2119
|
-
|
|
2120
|
-
|
|
3604
|
+
invoice: {
|
|
3605
|
+
lineTotal: totals.lineTotal,
|
|
3606
|
+
taxBasisTotal: totals.taxBasisTotal,
|
|
3607
|
+
taxTotal: totals.taxTotal,
|
|
3608
|
+
grandTotal: totals.grandTotal,
|
|
3609
|
+
...invoice.paid === void 0 ? {} : { paid: totals.paid },
|
|
3610
|
+
duePayable: totals.duePayable
|
|
3611
|
+
},
|
|
3612
|
+
vatBasis: Object.fromEntries(invoice.vat.map((category) => [category.id, of(category)?.basis ?? null])),
|
|
3613
|
+
vatAmount: Object.fromEntries(invoice.vat.map((category) => [category.id, of(category)?.amount ?? null]))
|
|
2121
3614
|
};
|
|
2122
3615
|
}
|
|
2123
|
-
|
|
2124
|
-
|
|
2125
|
-
|
|
2126
|
-
|
|
2127
|
-
|
|
2128
|
-
|
|
3616
|
+
// src/inlines.ts
|
|
3617
|
+
/**
|
|
3618
|
+
* The totals an aggregate run reads, by what it is over: every detail
|
|
3619
|
+
* row's wherever it stands, or the current table's or group's. report-spec
|
|
3620
|
+
* allows a table's only in its total rows and its group's rows, and a
|
|
3621
|
+
* group's only in a group's (0062), where the scope holds them.
|
|
3622
|
+
*/
|
|
3623
|
+
const OVER = {
|
|
3624
|
+
report: (render) => render.totals.report,
|
|
3625
|
+
table: (_, scope) => scope.table,
|
|
3626
|
+
group: (_, scope) => scope.group
|
|
3627
|
+
};
|
|
2129
3628
|
/** The value `inline`, at `path`, reads in `scope`, unformatted; undefined when its evaluation failed, which is recorded. */
|
|
2130
3629
|
function inlineValue(render, scope, inline, path) {
|
|
2131
3630
|
switch (inline.kind) {
|
|
@@ -2138,7 +3637,7 @@ function inlineValue(render, scope, inline, path) {
|
|
|
2138
3637
|
};
|
|
2139
3638
|
}
|
|
2140
3639
|
case "expression": return valueOf(render, scope, `${path}/expression/cel`);
|
|
2141
|
-
case "aggregate": return
|
|
3640
|
+
case "aggregate": return OVER[inline.over](render, scope)?.get(inline.aggregate) ?? null;
|
|
2142
3641
|
}
|
|
2143
3642
|
}
|
|
2144
3643
|
/** What the e-invoice kept of the detail row `row` for the column value at `path`: the value, null among them; or undefined where it kept nothing. */
|
|
@@ -2173,12 +3672,26 @@ function printed(render, scope, inline, path, owner) {
|
|
|
2173
3672
|
})
|
|
2174
3673
|
});
|
|
2175
3674
|
}
|
|
2176
|
-
/**
|
|
3675
|
+
/**
|
|
3676
|
+
* `inline`, at `path` in the report, as the run it prints in `scope`;
|
|
3677
|
+
* undefined when it could not be, which is recorded. A run carries the
|
|
3678
|
+
* baseline its source sets (0061), and a run that sets none carries no key,
|
|
3679
|
+
* so that the document of a report that sets none is the one it was. A
|
|
3680
|
+
* table's cell joins its runs' text and drops it, where report-spec refuses
|
|
3681
|
+
* one.
|
|
3682
|
+
*/
|
|
2177
3683
|
function runOf(render, scope, inline, path, owner) {
|
|
2178
|
-
if (inline.kind === "text") return { text: inline.text };
|
|
2179
3684
|
if (inline.kind === "pageNumber" || inline.kind === "pageCount") return { field: inline.kind };
|
|
3685
|
+
const shift = inline.baseline === void 0 ? {} : { baseline: inline.baseline };
|
|
3686
|
+
if (inline.kind === "text") return {
|
|
3687
|
+
text: inline.text,
|
|
3688
|
+
...shift
|
|
3689
|
+
};
|
|
2180
3690
|
const run = printed(render, scope, inline, path, owner);
|
|
2181
|
-
return run && {
|
|
3691
|
+
return run && {
|
|
3692
|
+
text: run.text,
|
|
3693
|
+
...shift
|
|
3694
|
+
};
|
|
2182
3695
|
}
|
|
2183
3696
|
/** What a code of less than its runs would scan as, by what they are encoded in. */
|
|
2184
3697
|
const SCANNED_AS = {
|
|
@@ -2276,8 +3789,7 @@ function encodedText(render, scope, content, path, id) {
|
|
|
2276
3789
|
function textOfRuns(runs) {
|
|
2277
3790
|
return runs.map((run) => "text" in run ? run.text : "").join("");
|
|
2278
3791
|
}
|
|
2279
|
-
|
|
2280
|
-
//#region src/e-invoice-lines.ts
|
|
3792
|
+
// src/e-invoice-lines.ts
|
|
2281
3793
|
/**
|
|
2282
3794
|
* An e-invoice's lines (BG-25): one for each row of the table that prints
|
|
2283
3795
|
* them, read once, before the totals, for the page and the XML alike
|
|
@@ -2493,8 +4005,7 @@ function readLines(render, invoice) {
|
|
|
2493
4005
|
function writtenLines(lines) {
|
|
2494
4006
|
return lines.every((line) => Object.values(line).every((term) => term !== null)) ? lines : void 0;
|
|
2495
4007
|
}
|
|
2496
|
-
|
|
2497
|
-
//#region src/e-invoice-terms.ts
|
|
4008
|
+
// src/e-invoice-terms.ts
|
|
2498
4009
|
/**
|
|
2499
4010
|
* An e-invoice's terms but its lines -- its number, its dates, its seller
|
|
2500
4011
|
* and its buyer, its categories' reasons, its payment instructions, its
|
|
@@ -2512,7 +4023,13 @@ function writtenLines(lines) {
|
|
|
2512
4023
|
* optional texts are: a nullable field's null is how data says "none".
|
|
2513
4024
|
* A needed one that prints nothing breaks its rule (BR-02, BR-06 ...);
|
|
2514
4025
|
* one whose runs print something around a null is refused as a text cut
|
|
2515
|
-
* short.
|
|
4026
|
+
* short. The seller's contact is left out whole or written whole: one
|
|
4027
|
+
* the data gives in part is refused at each term it lacks (0037).
|
|
4028
|
+
* - **Each kind's rules.** An XRechnung's texts are held to its kind's
|
|
4029
|
+
* (`E_INVOICE_KIND_TEXTS`, 0038): what XRechnung needs is needed, a
|
|
4030
|
+
* contact's three terms among them, and its payment terms hold no line
|
|
4031
|
+
* led by #. An electronic address under 0204 is a Leitweg-ID in either
|
|
4032
|
+
* kind.
|
|
2516
4033
|
* - **What only the whole says** -- the identifiers the lines' categories
|
|
2517
4034
|
* need of the seller and the buyer (BR-CO-26, BR-S-02, BR-E-02,
|
|
2518
4035
|
* BR-AE-02), the time of supply (§ 14 UStG), a category's reason (BR-E-10,
|
|
@@ -2537,17 +4054,17 @@ function refuse$2({ render }, path, message) {
|
|
|
2537
4054
|
message: `the e-invoice: ${message}`
|
|
2538
4055
|
});
|
|
2539
4056
|
}
|
|
2540
|
-
/** The text of `term`, its runs at `path`, held to its rule. */
|
|
2541
|
-
function text(reader, term, runs, path) {
|
|
4057
|
+
/** The text of `term`, its runs at `path`, held to its rule of the reader's kind, or to `check` where it is given. */
|
|
4058
|
+
function text(reader, term, runs, path, check = (written) => checkEInvoiceText(term, written, reader.kind)) {
|
|
2542
4059
|
if (runs === void 0) return null;
|
|
2543
4060
|
const read = readRuns(reader.render, reader.scope, runs, path, OWNER);
|
|
2544
4061
|
if (read.refused) return void 0;
|
|
2545
|
-
const rule =
|
|
4062
|
+
const rule = E_INVOICE_KIND_TEXTS[reader.kind][term];
|
|
2546
4063
|
const blank = eInvoiceBlank(read.text);
|
|
2547
4064
|
if (reader.render.preview && (blank || read.nulls.length > 0)) return null;
|
|
2548
4065
|
if (blank && rule.required === void 0) return null;
|
|
2549
4066
|
if (!blank && read.nulls.length > 0) return refuse$2(reader, path, `${rule.what} has a run with no value, and written without it the e-invoice would hold less than its runs print`);
|
|
2550
|
-
const checked =
|
|
4067
|
+
const checked = check(read.text);
|
|
2551
4068
|
return checked.ok ? checked.value : refuse$2(reader, path, `${rule.what} ${checked.rule}`);
|
|
2552
4069
|
}
|
|
2553
4070
|
/** The date of `term`, its expression at `path`: a day on the calendar, YYYY-MM-DD. */
|
|
@@ -2581,19 +4098,59 @@ function address$1(reader, held, path, which) {
|
|
|
2581
4098
|
country
|
|
2582
4099
|
};
|
|
2583
4100
|
}
|
|
4101
|
+
/** An electronic address: its identifier read, held to its scheme where that is 0204, with the scheme the template names; none where the data gives the identifier as nothing. */
|
|
4102
|
+
function electronicAddress(reader, held, path, term) {
|
|
4103
|
+
if (held === void 0) return null;
|
|
4104
|
+
const id = text(reader, term, held.id, `${path}/id`, (written) => checkElectronicAddress(term, held.scheme, written, reader.kind));
|
|
4105
|
+
return id === null || id === void 0 ? id : {
|
|
4106
|
+
id,
|
|
4107
|
+
scheme: held.scheme
|
|
4108
|
+
};
|
|
4109
|
+
}
|
|
4110
|
+
/**
|
|
4111
|
+
* The seller's contact: all three of its terms, or none where the data
|
|
4112
|
+
* gives none of them. One it gives in part is refused at each term it
|
|
4113
|
+
* lacks, as the validator refuses a literal one: a contact gives all three
|
|
4114
|
+
* or none (0037). A preview lacks what it has no value for, and says
|
|
4115
|
+
* nothing of it.
|
|
4116
|
+
*/
|
|
4117
|
+
function contact(reader, held, path) {
|
|
4118
|
+
if (held === void 0) return null;
|
|
4119
|
+
const terms = E_INVOICE_TEXT_TERMS.seller.contact;
|
|
4120
|
+
const read = {
|
|
4121
|
+
name: text(reader, terms.name, held.name, `${path}/name`),
|
|
4122
|
+
telephone: text(reader, terms.telephone, held.telephone, `${path}/telephone`),
|
|
4123
|
+
email: text(reader, terms.email, held.email, `${path}/email`)
|
|
4124
|
+
};
|
|
4125
|
+
const { name, telephone, email } = read;
|
|
4126
|
+
if (name === void 0 || telephone === void 0 || email === void 0) return void 0;
|
|
4127
|
+
if (name !== null && telephone !== null && email !== null) return {
|
|
4128
|
+
name,
|
|
4129
|
+
telephone,
|
|
4130
|
+
email
|
|
4131
|
+
};
|
|
4132
|
+
if (name === null && telephone === null && email === null || reader.render.preview) return null;
|
|
4133
|
+
for (const key of [
|
|
4134
|
+
"name",
|
|
4135
|
+
"telephone",
|
|
4136
|
+
"email"
|
|
4137
|
+
]) if (read[key] === null) refuse$2(reader, `${path}/${key}`, `${E_INVOICE_KIND_TEXTS[reader.kind][terms[key]].what} is empty, and a contact gives all three of its terms or none`);
|
|
4138
|
+
}
|
|
2584
4139
|
function party$1(reader, held, path, which) {
|
|
2585
4140
|
const terms = E_INVOICE_TEXT_TERMS[which];
|
|
2586
4141
|
const name = text(reader, terms.name, held.name, `${path}/name`);
|
|
2587
4142
|
const vatId = text(reader, terms.vatId, held.vatId, `${path}/vatId`);
|
|
2588
4143
|
const taxNumber = which === "seller" ? text(reader, E_INVOICE_TEXT_TERMS.seller.taxNumber, held.taxNumber, `${path}/taxNumber`) : null;
|
|
2589
4144
|
const legalId = text(reader, terms.legalId, held.legalId?.id, `${path}/legalId/id`);
|
|
4145
|
+
const reached = electronicAddress(reader, held.electronicAddress, `${path}/electronicAddress`, terms.electronicAddress);
|
|
4146
|
+
const contacted = which === "seller" ? contact(reader, held.contact, `${path}/contact`) : null;
|
|
2590
4147
|
const where = address$1(reader, held.address, `${path}/address`, which);
|
|
2591
4148
|
const has = {
|
|
2592
4149
|
vatId: vatId !== null,
|
|
2593
4150
|
taxNumber: taxNumber !== null,
|
|
2594
4151
|
legalId: legalId !== null
|
|
2595
4152
|
};
|
|
2596
|
-
if (vatId === void 0 || taxNumber === void 0 || legalId === void 0 || !all(name, where)) return {
|
|
4153
|
+
if (vatId === void 0 || taxNumber === void 0 || legalId === void 0 || reached === void 0 || contacted === void 0 || !all(name, where)) return {
|
|
2597
4154
|
party: void 0,
|
|
2598
4155
|
has
|
|
2599
4156
|
};
|
|
@@ -2608,6 +4165,8 @@ function party$1(reader, held, path, which) {
|
|
|
2608
4165
|
...term("vatId", vatId),
|
|
2609
4166
|
...term("taxNumber", taxNumber),
|
|
2610
4167
|
...registration,
|
|
4168
|
+
...term("electronicAddress", reached),
|
|
4169
|
+
...term("contact", contacted),
|
|
2611
4170
|
address: where
|
|
2612
4171
|
},
|
|
2613
4172
|
has
|
|
@@ -2660,10 +4219,12 @@ function paidAmount(render, invoice) {
|
|
|
2660
4219
|
function readTerms(render, invoice, used) {
|
|
2661
4220
|
const reader = {
|
|
2662
4221
|
render,
|
|
2663
|
-
scope: reportScope(render, render.totals.report)
|
|
4222
|
+
scope: reportScope(render, render.totals.report),
|
|
4223
|
+
kind: invoice.kind
|
|
2664
4224
|
};
|
|
2665
4225
|
const number = text(reader, E_INVOICE_TEXT_TERMS.number, invoice.number, "/eInvoice/number");
|
|
2666
4226
|
const issued = date(reader, "BT-2", invoice.issued, "/eInvoice/issued");
|
|
4227
|
+
const businessProcess = text(reader, E_INVOICE_TEXT_TERMS.businessProcess, invoice.businessProcess, "/eInvoice/businessProcess");
|
|
2667
4228
|
const seller = party$1(reader, invoice.seller, "/eInvoice/seller", "seller");
|
|
2668
4229
|
const buyer = party$1(reader, invoice.buyer, "/eInvoice/buyer", "buyer");
|
|
2669
4230
|
const buyerReference = text(reader, E_INVOICE_TEXT_TERMS.buyerReference, invoice.buyerReference, "/eInvoice/buyerReference");
|
|
@@ -2691,6 +4252,7 @@ function readTerms(render, invoice, used) {
|
|
|
2691
4252
|
const read = [
|
|
2692
4253
|
number,
|
|
2693
4254
|
issued,
|
|
4255
|
+
businessProcess,
|
|
2694
4256
|
seller.party,
|
|
2695
4257
|
buyer.party,
|
|
2696
4258
|
buyerReference,
|
|
@@ -2710,6 +4272,7 @@ function readTerms(render, invoice, used) {
|
|
|
2710
4272
|
terms: {
|
|
2711
4273
|
number,
|
|
2712
4274
|
issued,
|
|
4275
|
+
...term("businessProcess", businessProcess),
|
|
2713
4276
|
seller: seller.party,
|
|
2714
4277
|
buyer: buyer.party,
|
|
2715
4278
|
...term("buyerReference", buyerReference),
|
|
@@ -2726,8 +4289,7 @@ function readTerms(render, invoice, used) {
|
|
|
2726
4289
|
account: paying.account
|
|
2727
4290
|
};
|
|
2728
4291
|
}
|
|
2729
|
-
|
|
2730
|
-
//#region src/lower-e-invoice.ts
|
|
4292
|
+
// src/lower-e-invoice.ts
|
|
2731
4293
|
/**
|
|
2732
4294
|
* A report's e-invoice resolved (0027): its lines and its totals before
|
|
2733
4295
|
* anything is lowered, since the page prints them; and after the page is
|
|
@@ -2750,8 +4312,29 @@ function readTerms(render, invoice, used) {
|
|
|
2750
4312
|
* `e-invoice-preview` whatever data it has, so that every page carries a
|
|
2751
4313
|
* preview label, complete data or not. A final render writes the XML or
|
|
2752
4314
|
* refuses.
|
|
4315
|
+
*
|
|
4316
|
+
* **Both kinds are resolved alike** (0038): an XRechnung's terms are EN
|
|
4317
|
+
* 16931's, read by the same readers under its kind's rules, and written by
|
|
4318
|
+
* the same writer, its specification identifier apart. What differs is
|
|
4319
|
+
* the file the document carries: a Factur-X file a PDF/A-3b embeds, or an
|
|
4320
|
+
* XRechnung's XML a host takes and sends.
|
|
4321
|
+
*/
|
|
4322
|
+
/** BT-24 of each kind: a Factur-X profile's identifier, or an XRechnung version's. A table over every kind, so a kind added without one does not compile. */
|
|
4323
|
+
const GUIDELINES = {
|
|
4324
|
+
facturX: (invoice) => FACTUR_X_PROFILES$1[invoice.profile].guideline,
|
|
4325
|
+
xrechnung: (invoice) => XRECHNUNG$1[invoice.version].guideline
|
|
4326
|
+
};
|
|
4327
|
+
/** The guideline of `invoice`, by its kind. */
|
|
4328
|
+
const guidelineOf = (invoice) => GUIDELINES[invoice.kind](invoice);
|
|
4329
|
+
/**
|
|
4330
|
+
* `invoice`, `render`'s e-invoice, resolved before anything is lowered,
|
|
4331
|
+
* whatever its kind: its lines, its totals and its terms, each held to its
|
|
4332
|
+
* kind's rules, and in a final render whose every term resolved what its
|
|
4333
|
+
* XML is written from. Undefined when its lines or its totals could not
|
|
4334
|
+
* be, which is recorded: nothing that prints them can be lowered. A term
|
|
4335
|
+
* refused is recorded, and leaves nothing to write.
|
|
2753
4336
|
*/
|
|
2754
|
-
function
|
|
4337
|
+
function resolveEInvoice(render, invoice) {
|
|
2755
4338
|
const read = readLines(render, invoice);
|
|
2756
4339
|
const paid = paidAmount(render, invoice);
|
|
2757
4340
|
if (read === void 0 || paid === void 0) return void 0;
|
|
@@ -2763,7 +4346,7 @@ function facturX(render, invoice) {
|
|
|
2763
4346
|
const { terms, account } = readTerms(render, invoice, totals.breakdown.map(({ category }) => category));
|
|
2764
4347
|
const lines = writtenLines(read.lines);
|
|
2765
4348
|
const written = render.preview || terms === void 0 || lines === void 0 ? void 0 : {
|
|
2766
|
-
|
|
4349
|
+
guideline: guidelineOf(invoice),
|
|
2767
4350
|
type: invoice.type,
|
|
2768
4351
|
currency: invoice.currency,
|
|
2769
4352
|
terms,
|
|
@@ -2772,7 +4355,7 @@ function facturX(render, invoice) {
|
|
|
2772
4355
|
};
|
|
2773
4356
|
return {
|
|
2774
4357
|
reads: {
|
|
2775
|
-
names: invoiceNames
|
|
4358
|
+
names: invoiceNames(invoice, totals),
|
|
2776
4359
|
columns: read.columns,
|
|
2777
4360
|
payable: {
|
|
2778
4361
|
amount: totals.duePayable,
|
|
@@ -2782,17 +4365,33 @@ function facturX(render, invoice) {
|
|
|
2782
4365
|
written
|
|
2783
4366
|
};
|
|
2784
4367
|
}
|
|
2785
|
-
/** How each kind of e-invoice is resolved. A table over every kind, so a kind added to report-spec without a way down does not compile. */
|
|
2786
|
-
const RESOLVERS = { facturX };
|
|
2787
4368
|
/**
|
|
2788
|
-
*
|
|
2789
|
-
*
|
|
2790
|
-
*
|
|
2791
|
-
* lowered. A term refused is recorded, and leaves nothing to write.
|
|
4369
|
+
* The file of each kind, around its XML: Factur-X's, named as report-spec
|
|
4370
|
+
* names the file a PDF/A-3b embeds [S]; an XRechnung's, standalone, named
|
|
4371
|
+
* by its syntax, guideline and version. A table over every kind.
|
|
2792
4372
|
*/
|
|
2793
|
-
|
|
2794
|
-
|
|
2795
|
-
|
|
4373
|
+
const FILES = {
|
|
4374
|
+
facturX: (invoice, xml) => {
|
|
4375
|
+
const profile = FACTUR_X_PROFILES$1[invoice.profile];
|
|
4376
|
+
return {
|
|
4377
|
+
kind: "facturX",
|
|
4378
|
+
xml,
|
|
4379
|
+
fileName: FACTUR_X$1.fileName,
|
|
4380
|
+
relationship: profile.relationship,
|
|
4381
|
+
conformanceLevel: profile.conformanceLevel
|
|
4382
|
+
};
|
|
4383
|
+
},
|
|
4384
|
+
xrechnung: (invoice, xml) => {
|
|
4385
|
+
const version = XRECHNUNG$1[invoice.version];
|
|
4386
|
+
return {
|
|
4387
|
+
kind: "xrechnung",
|
|
4388
|
+
xml,
|
|
4389
|
+
syntax: version.syntax,
|
|
4390
|
+
guideline: version.guideline,
|
|
4391
|
+
version: invoice.version
|
|
4392
|
+
};
|
|
4393
|
+
}
|
|
4394
|
+
};
|
|
2796
4395
|
/** What a preview of an e-invoice warns, whatever data it has. */
|
|
2797
4396
|
const PREVIEWED = "this is a preview, which writes no e-invoice: a final render alone writes its XML, and this PDF is not the invoice";
|
|
2798
4397
|
/**
|
|
@@ -2818,16 +4417,96 @@ function eInvoiceFile(render, resolved, invoice) {
|
|
|
2818
4417
|
}
|
|
2819
4418
|
if (render.problems.failed) return void 0;
|
|
2820
4419
|
if (resolved.written === void 0) throw new RangeError("a final render of an e-invoice wrote no XML and recorded no reason: a defect, refused rather than drawn as a plain PDF");
|
|
2821
|
-
|
|
4420
|
+
return FILES[invoice.kind](invoice, ciiXml(resolved.written));
|
|
4421
|
+
}
|
|
4422
|
+
// src/lower-fonts.ts
|
|
4423
|
+
/**
|
|
4424
|
+
* A report's font map resolved to the bytes the renderer embeds (0043).
|
|
4425
|
+
*
|
|
4426
|
+
* The definition maps each font token to approved font assets, face by face
|
|
4427
|
+
* ([report-spec's `font-map.ts`](../../report-spec/src/font-map.ts)); here
|
|
4428
|
+
* the host's lookup hands over each face's bytes, as it does an image's
|
|
4429
|
+
* (0033), and the runtime reads them once with report-spec's `readFont`
|
|
4430
|
+
* before it hands them on, so that a font the renderer could not embed is
|
|
4431
|
+
* refused where the data is resolved, not deep inside the engine. The
|
|
4432
|
+
* renderer reads them again, since it is a package boundary that takes
|
|
4433
|
+
* resolved input and approved bytes and refuses what it cannot draw (0011);
|
|
4434
|
+
* what is read here is read there too, by the one reader.
|
|
4435
|
+
*
|
|
4436
|
+
* A font is structural, not a value: a token a report maps prints all its
|
|
4437
|
+
* text in that face, so a face whose bytes cannot be had, or that `readFont`
|
|
4438
|
+
* refuses, refuses the render -- in a preview too, since a preview that
|
|
4439
|
+
* dropped a mapped face would print its text in another font nobody chose
|
|
4440
|
+
* (fail closed). Each face is fetched once, however many tokens or weights
|
|
4441
|
+
* share one asset.
|
|
4442
|
+
*
|
|
4443
|
+
* Nothing here is shared with the host: the bytes are copied, as an image's
|
|
4444
|
+
* are, so that what the host changes afterwards never reaches the document.
|
|
4445
|
+
*/
|
|
4446
|
+
/**
|
|
4447
|
+
* `fonts` resolved to the bytes each face prints in, or undefined when the
|
|
4448
|
+
* report maps nothing. A face that cannot be had or that `readFont` refuses
|
|
4449
|
+
* records an error and resolves nothing for that face; the caller refuses
|
|
4450
|
+
* the render when any error was recorded.
|
|
4451
|
+
*/
|
|
4452
|
+
function resolveFonts(render, fonts) {
|
|
4453
|
+
if (fonts === void 0) return void 0;
|
|
4454
|
+
const byId = /* @__PURE__ */ new Map();
|
|
4455
|
+
const result = {};
|
|
4456
|
+
for (const [family, faces] of Object.entries(fonts)) {
|
|
4457
|
+
if (faces === void 0) continue;
|
|
4458
|
+
const resolved = {};
|
|
4459
|
+
for (const key of FONT_FACE_KEYS$1) {
|
|
4460
|
+
const id = faces[key];
|
|
4461
|
+
if (id === void 0) continue;
|
|
4462
|
+
const asset = faceAsset(render, byId, id, `/fonts/${family}/${key}`);
|
|
4463
|
+
if (asset !== void 0) resolved[key] = asset;
|
|
4464
|
+
}
|
|
4465
|
+
if (resolved.normal !== void 0) result[family] = resolved;
|
|
4466
|
+
}
|
|
4467
|
+
return result;
|
|
4468
|
+
}
|
|
4469
|
+
/** The bytes of the face asset `id`, fetched once and kept, checked with `readFont`; undefined when it could not be had, which is recorded at `path`. */
|
|
4470
|
+
function faceAsset(render, byId, id, path) {
|
|
4471
|
+
if (byId.has(id)) return byId.get(id);
|
|
4472
|
+
const asset = fetchFace(render, id, path);
|
|
4473
|
+
byId.set(id, asset);
|
|
4474
|
+
return asset;
|
|
4475
|
+
}
|
|
4476
|
+
/** What the host holds for the font asset `id`, copied and read, or undefined with an error recorded at `path`. */
|
|
4477
|
+
function fetchFace(render, id, path) {
|
|
4478
|
+
const error = (code, message) => {
|
|
4479
|
+
render.problems.error({
|
|
4480
|
+
stage: "resolve",
|
|
4481
|
+
code,
|
|
4482
|
+
path,
|
|
4483
|
+
message
|
|
4484
|
+
});
|
|
4485
|
+
};
|
|
4486
|
+
if (render.images === void 0) return error("missing-asset", `the report maps a font, and the host gave no way to fetch approved assets`);
|
|
4487
|
+
const declared = render.report.assets.find((ref) => ref.id === id);
|
|
4488
|
+
let found;
|
|
4489
|
+
try {
|
|
4490
|
+
found = render.images({
|
|
4491
|
+
asset: declared.id,
|
|
4492
|
+
digest: declared.digest
|
|
4493
|
+
});
|
|
4494
|
+
} catch {
|
|
4495
|
+
return error("asset-lookup-failed", `fetching the approved font ${id} failed`);
|
|
4496
|
+
}
|
|
4497
|
+
if (found === void 0) return error("missing-asset", `the host has no bytes for the approved font ${id} at its digest`);
|
|
4498
|
+
if (typeof found !== "object" || !(found.bytes instanceof Uint8Array) || !isFontMediaType$1(found.mediaType)) return error("invalid-asset", `the host's answer for the approved font ${id} is not bytes of ${declared.mediaType}`);
|
|
4499
|
+
if (found.bytes.length > MAX_FONT_BYTES$1) return error("invalid-asset", `the approved font ${id} is ${found.bytes.length} bytes; a font may have ${MAX_FONT_BYTES$1}`);
|
|
4500
|
+
const bytes = new Uint8Array(found.bytes);
|
|
4501
|
+
const read = readFont$1(bytes);
|
|
4502
|
+
if (!read.ok) return error("invalid-asset", `the approved font ${id}: the font ${read.message}`);
|
|
2822
4503
|
return {
|
|
2823
|
-
|
|
2824
|
-
|
|
2825
|
-
|
|
2826
|
-
conformanceLevel: profile.conformanceLevel
|
|
4504
|
+
id,
|
|
4505
|
+
digest: declared.digest,
|
|
4506
|
+
bytes
|
|
2827
4507
|
};
|
|
2828
4508
|
}
|
|
2829
|
-
|
|
2830
|
-
//#region src/e-invoice-payable.ts
|
|
4509
|
+
// src/e-invoice-payable.ts
|
|
2831
4510
|
/**
|
|
2832
4511
|
* A payment code printed with an e-invoice -- the QR-bill, an EPC QR code
|
|
2833
4512
|
* -- held to it once both have resolved: it asks for the amount due for
|
|
@@ -2863,8 +4542,7 @@ function checkPayable(render, code, amount, account) {
|
|
|
2863
4542
|
if (amount !== null && compareDecimal(amount, payable.amount) !== 0) refuse(code.amountPath, `${code.code} asks for another amount than the e-invoice's amount due for payment (BT-115): a page and its XML ask for one amount`);
|
|
2864
4543
|
if (payable.account !== void 0 && account !== payable.account) refuse(code.accountPath, `${code.code} asks to be paid to another account than the e-invoice's payment account (BT-84): a page and its XML name one account`);
|
|
2865
4544
|
}
|
|
2866
|
-
|
|
2867
|
-
//#region src/qr-bill.ts
|
|
4545
|
+
// src/qr-bill.ts
|
|
2868
4546
|
/**
|
|
2869
4547
|
* A Swiss QR-bill made from values SIX's rules have accepted: the Swiss QR
|
|
2870
4548
|
* Code's payload, and every text its slip prints.
|
|
@@ -3042,8 +4720,7 @@ function qrBillPart(bill) {
|
|
|
3042
4720
|
amount: bill.amount === null ? null : printedAmount(bill.amount)
|
|
3043
4721
|
};
|
|
3044
4722
|
}
|
|
3045
|
-
|
|
3046
|
-
//#region src/lower-payment-part.ts
|
|
4723
|
+
// src/lower-payment-part.ts
|
|
3047
4724
|
/**
|
|
3048
4725
|
* A report's payment part lowered to the document's: each text read in the
|
|
3049
4726
|
* report's scope, held to SIX's rules, and made into the bill
|
|
@@ -3166,8 +4843,7 @@ function lowerPaymentPart(render) {
|
|
|
3166
4843
|
scope: reportScope(render, render.totals.report)
|
|
3167
4844
|
}, part);
|
|
3168
4845
|
}
|
|
3169
|
-
|
|
3170
|
-
//#region src/images.ts
|
|
4846
|
+
// src/images.ts
|
|
3171
4847
|
/**
|
|
3172
4848
|
* An image component lowered to the bytes it draws.
|
|
3173
4849
|
*
|
|
@@ -3301,8 +4977,7 @@ function imageBlock(render, component, path) {
|
|
|
3301
4977
|
fit: component.fit
|
|
3302
4978
|
};
|
|
3303
4979
|
}
|
|
3304
|
-
|
|
3305
|
-
//#region src/lower-epc-qr-code.ts
|
|
4980
|
+
// src/lower-epc-qr-code.ts
|
|
3306
4981
|
/**
|
|
3307
4982
|
* An EPC QR code lowered to the document's block: each text read in the
|
|
3308
4983
|
* report's scope, held to the EPC's rules, and written into the payload the
|
|
@@ -3447,8 +5122,7 @@ function epcQrCodeBlock(render, scope, code, path) {
|
|
|
3447
5122
|
...code.caption === void 0 ? {} : { caption: code.caption }
|
|
3448
5123
|
};
|
|
3449
5124
|
}
|
|
3450
|
-
|
|
3451
|
-
//#region src/styles.ts
|
|
5125
|
+
// src/styles.ts
|
|
3452
5126
|
/** Each print style as the document's: the same tokens without the id. */
|
|
3453
5127
|
function documentStyles(styles) {
|
|
3454
5128
|
return new Map(styles.map(({ id, ...style }) => [id, style]));
|
|
@@ -3468,8 +5142,7 @@ function styled(render, scope, base, style, rules, path) {
|
|
|
3468
5142
|
for (const [index, rule] of (rules ?? []).entries()) if (holds(render, scope, `${path}/${index}/when/cel`) === true) held.push(render.styles.get(rule.style));
|
|
3469
5143
|
return layered(base, style === void 0 ? void 0 : render.styles.get(style), ...held);
|
|
3470
5144
|
}
|
|
3471
|
-
|
|
3472
|
-
//#region src/lower-table.ts
|
|
5145
|
+
// src/lower-table.ts
|
|
3473
5146
|
/** `style` as a row's: the table's under the named print style; absent when neither sets anything. */
|
|
3474
5147
|
function rowStyle(lowering, style) {
|
|
3475
5148
|
const resolved = layered(lowering.base, style === void 0 ? void 0 : lowering.render.styles.get(style));
|
|
@@ -3522,13 +5195,15 @@ function groupedRows(lowering, groups) {
|
|
|
3522
5195
|
const rows = [];
|
|
3523
5196
|
for (const each of groups) {
|
|
3524
5197
|
if (render.problems.full) break;
|
|
3525
|
-
const scope = groupScope(render, each, render.totals.over(each.rows), group.by.field);
|
|
5198
|
+
const scope = groupScope(render, each, render.totals.over(each.rows), lowering.totals, group.by.field);
|
|
3526
5199
|
const runs = group.header.map((inline, index) => runOf(render, scope, inline, `${path}/group/header/${index}`, { componentId: table.id }));
|
|
5200
|
+
const keep = Math.min(group.keepWithRows ?? 1, each.rows.length);
|
|
3527
5201
|
rows.push({
|
|
3528
5202
|
kind: "groupHeader",
|
|
3529
5203
|
cells: [textOfRuns(runs.filter((run) => run !== void 0))],
|
|
3530
5204
|
across: true,
|
|
3531
|
-
...rowStyle(lowering, group.headerStyle)
|
|
5205
|
+
...rowStyle(lowering, group.headerStyle),
|
|
5206
|
+
...keep > 1 ? { keepWithRows: keep } : {}
|
|
3532
5207
|
});
|
|
3533
5208
|
for (const row of each.rows) rows.push(detailRow(lowering, row));
|
|
3534
5209
|
rows.push(...footerRows(lowering, group.footer, `${path}/group/footer`, "groupFooter", scope));
|
|
@@ -3536,6 +5211,25 @@ function groupedRows(lowering, groups) {
|
|
|
3536
5211
|
return rows;
|
|
3537
5212
|
}
|
|
3538
5213
|
/**
|
|
5214
|
+
* The detail rows `table`'s condition, at `path`, selects, in the data's
|
|
5215
|
+
* order; undefined for a table without one, which has every row. A row
|
|
5216
|
+
* whose condition could not be read is left out and recorded as an error,
|
|
5217
|
+
* so the report is refused.
|
|
5218
|
+
*/
|
|
5219
|
+
function selectedRows(render, table, path) {
|
|
5220
|
+
if (table.rowsWhen === void 0) return void 0;
|
|
5221
|
+
const selected = [];
|
|
5222
|
+
for (const [index, record] of render.data.detail.entries()) {
|
|
5223
|
+
if (render.problems.full) break;
|
|
5224
|
+
const row = {
|
|
5225
|
+
index,
|
|
5226
|
+
record
|
|
5227
|
+
};
|
|
5228
|
+
if (holds(render, rowScope(render, render.totals.report, row), `${path}/rowsWhen/cel`) === true) selected.push(row);
|
|
5229
|
+
}
|
|
5230
|
+
return selected;
|
|
5231
|
+
}
|
|
5232
|
+
/**
|
|
3539
5233
|
* `table`, at `path`, as the document's table, `base` its own resolved
|
|
3540
5234
|
* style; undefined when every column is hidden. A value that could not be
|
|
3541
5235
|
* read prints nothing here, and is recorded as an error, so the report is
|
|
@@ -3548,22 +5242,24 @@ function tableBlock(render, table, path, base) {
|
|
|
3548
5242
|
index
|
|
3549
5243
|
})).filter(({ column, index }) => column.visibleWhen === void 0 || holds(render, scope, `${path}/columns/${index}/visibleWhen/cel`) === true);
|
|
3550
5244
|
if (shown.length === 0) return void 0;
|
|
5245
|
+
const selected = selectedRows(render, table, path);
|
|
5246
|
+
const totals = selected === void 0 ? render.totals.report : render.totals.over(selected);
|
|
3551
5247
|
const lowering = {
|
|
3552
5248
|
render,
|
|
3553
5249
|
table,
|
|
3554
5250
|
path,
|
|
3555
5251
|
shown,
|
|
3556
|
-
base
|
|
5252
|
+
base,
|
|
5253
|
+
totals
|
|
3557
5254
|
};
|
|
3558
|
-
const ordered = orderTable(render.report, table, render.data.detail);
|
|
5255
|
+
const ordered = orderTable(render.report, table, render.data.detail, selected);
|
|
3559
5256
|
const rows = [];
|
|
3560
5257
|
if (ordered.groups !== void 0) rows.push(...groupedRows(lowering, ordered.groups));
|
|
3561
5258
|
else for (const row of ordered.rows) {
|
|
3562
5259
|
if (render.problems.full) break;
|
|
3563
5260
|
rows.push(detailRow(lowering, row));
|
|
3564
5261
|
}
|
|
3565
|
-
rows.push(...footerRows(lowering, table.totals, `${path}/totals`, "total",
|
|
3566
|
-
const headerStyle = layered(base, table.headerStyle === void 0 ? void 0 : render.styles.get(table.headerStyle));
|
|
5262
|
+
rows.push(...footerRows(lowering, table.totals, `${path}/totals`, "total", tableScope(render, totals)));
|
|
3567
5263
|
return {
|
|
3568
5264
|
kind: "table",
|
|
3569
5265
|
id: table.id,
|
|
@@ -3571,14 +5267,29 @@ function tableBlock(render, table, path, base) {
|
|
|
3571
5267
|
width: column.width,
|
|
3572
5268
|
align: column.align
|
|
3573
5269
|
})),
|
|
3574
|
-
|
|
3575
|
-
...headerStyle === void 0 ? {} : { headerStyle },
|
|
5270
|
+
...headerOf(render, table, shown, base),
|
|
3576
5271
|
rows,
|
|
3577
5272
|
emptyText: table.emptyText
|
|
3578
5273
|
};
|
|
3579
5274
|
}
|
|
3580
|
-
|
|
3581
|
-
|
|
5275
|
+
/**
|
|
5276
|
+
* The header row of the `shown` columns and its style, or neither where no
|
|
5277
|
+
* title prints (0078): white space prints nothing, read as every engine
|
|
5278
|
+
* reads a cell, by `trim`. A title is a literal, never a value, and a word
|
|
5279
|
+
* of no letter is never translated (report-spec's `not-a-word`) nor one
|
|
5280
|
+
* translated to white space (`blank-translation`), so a table has a header
|
|
5281
|
+
* in every language it is written in, or in none.
|
|
5282
|
+
*/
|
|
5283
|
+
function headerOf(render, table, shown, base) {
|
|
5284
|
+
const header = shown.map(({ column }) => column.header);
|
|
5285
|
+
if (header.every((title) => title.trim() === "")) return {};
|
|
5286
|
+
const headerStyle = layered(base, table.headerStyle === void 0 ? void 0 : render.styles.get(table.headerStyle));
|
|
5287
|
+
return {
|
|
5288
|
+
header,
|
|
5289
|
+
...headerStyle === void 0 ? {} : { headerStyle }
|
|
5290
|
+
};
|
|
5291
|
+
}
|
|
5292
|
+
// src/lower.ts
|
|
3582
5293
|
/** `value` under `key` when it is defined: the document writes only what is set. */
|
|
3583
5294
|
const optional = (key, value) => value === void 0 ? {} : { [key]: value };
|
|
3584
5295
|
const LOWER = {
|
|
@@ -3633,6 +5344,7 @@ const LOWER = {
|
|
|
3633
5344
|
...optional("padding", section.padding)
|
|
3634
5345
|
}),
|
|
3635
5346
|
table: (table, { render, path, style }) => tableBlock(render, table, path, style),
|
|
5347
|
+
chart: (chart, { render, path, style }) => chartBlock(render, chart, path, style),
|
|
3636
5348
|
pageBreak: () => ({ kind: "pageBreak" }),
|
|
3637
5349
|
keepTogether: (kept, { render, path, style }) => ({
|
|
3638
5350
|
kind: "keepTogether",
|
|
@@ -3655,12 +5367,15 @@ function blockOf(render, component, path, inherited) {
|
|
|
3655
5367
|
function blocksOf(render, components, path, inherited) {
|
|
3656
5368
|
return components.flatMap((component, index) => blockOf(render, component, `${path}/${index}`, inherited) ?? []);
|
|
3657
5369
|
}
|
|
5370
|
+
/** A region every report holds, lowered. */
|
|
3658
5371
|
function region(render, name) {
|
|
3659
5372
|
return blocksOf(render, render.report.regions[name], `/regions/${name}`, void 0);
|
|
3660
5373
|
}
|
|
3661
5374
|
/** Every region of `render`'s report, lowered: the report header, body and summary as the one body. */
|
|
3662
5375
|
function lowerRegions(render) {
|
|
5376
|
+
const own = render.report.regions.firstPageHeader;
|
|
3663
5377
|
return {
|
|
5378
|
+
...own === void 0 ? {} : { firstPageHeader: blocksOf(render, own, "/regions/firstPageHeader", void 0) },
|
|
3664
5379
|
pageHeader: region(render, "pageHeader"),
|
|
3665
5380
|
pageFooter: region(render, "pageFooter"),
|
|
3666
5381
|
body: [
|
|
@@ -3670,20 +5385,27 @@ function lowerRegions(render) {
|
|
|
3670
5385
|
]
|
|
3671
5386
|
};
|
|
3672
5387
|
}
|
|
3673
|
-
|
|
3674
|
-
//#region src/report-check.ts
|
|
5388
|
+
// src/report-check.ts
|
|
3675
5389
|
/**
|
|
3676
|
-
* `report` validated and its expressions compiled for one render
|
|
3677
|
-
*
|
|
3678
|
-
*
|
|
5390
|
+
* `report` validated and its expressions compiled for one render in
|
|
5391
|
+
* `locale`, the report's own where absent, or every reason it is refused:
|
|
5392
|
+
* each structural error; a locale the report is not written in
|
|
5393
|
+
* (`undeclared-locale`); or, for a report that is structurally valid,
|
|
5394
|
+
* each diagnostic of severity error that names no language or that
|
|
5395
|
+
* locale (0063).
|
|
3679
5396
|
*/
|
|
3680
|
-
function checkReport(report) {
|
|
5397
|
+
function checkReport(report, locale) {
|
|
3681
5398
|
const validation = validateReport(report);
|
|
3682
5399
|
if (!validation.valid) return {
|
|
3683
5400
|
ok: false,
|
|
3684
5401
|
errors: validation.errors.map(fromReport)
|
|
3685
5402
|
};
|
|
3686
|
-
const
|
|
5403
|
+
const chosen = printLocale(validation.report, locale);
|
|
5404
|
+
if (!chosen.ok) return {
|
|
5405
|
+
ok: false,
|
|
5406
|
+
errors: [chosen.error]
|
|
5407
|
+
};
|
|
5408
|
+
const prepared = prepareExpressions(validation.report, chosen.locale);
|
|
3687
5409
|
if (!prepared.ok) return {
|
|
3688
5410
|
ok: false,
|
|
3689
5411
|
errors: prepared.diagnostics.map(fromDiagnostic)
|
|
@@ -3691,184 +5413,11 @@ function checkReport(report) {
|
|
|
3691
5413
|
return {
|
|
3692
5414
|
ok: true,
|
|
3693
5415
|
report: validation.report,
|
|
5416
|
+
printed: prepared.report,
|
|
3694
5417
|
expressions: prepared.expressions
|
|
3695
5418
|
};
|
|
3696
5419
|
}
|
|
3697
|
-
|
|
3698
|
-
//#region src/totals.ts
|
|
3699
|
-
/**
|
|
3700
|
-
* The report's aggregates, computed exactly, once, here (0007): over every
|
|
3701
|
-
* detail row for `totals`, and over a group's rows for `group`.
|
|
3702
|
-
*
|
|
3703
|
-
* - **In report-core's order.** An aggregate's expression may read another
|
|
3704
|
-
* aggregate's total, so each is computed after every one it reads, in the
|
|
3705
|
-
* order `compileReport` gives (0009); an aggregate that could never be
|
|
3706
|
-
* computed has already refused the report.
|
|
3707
|
-
* - **Each row read once.** What an aggregate reads from a row is read once
|
|
3708
|
-
* per render, with the report's totals, and kept by the row's index; a
|
|
3709
|
-
* group's aggregate is the same function over its rows' values. So a
|
|
3710
|
-
* group's subtotal and the report's total are totals of the same values,
|
|
3711
|
-
* and the subtotals of a sum that declares no rounding add up to the
|
|
3712
|
-
* total. A declared rounding is applied to each group's exact sum and to
|
|
3713
|
-
* the report's, once each, so rounded subtotals need not add up to the
|
|
3714
|
-
* rounded total: two groups of 0.02, rounded to 0.05, print 0.00 and 0.00
|
|
3715
|
-
* over 0.05.
|
|
3716
|
-
* - **Exact, and of one kind.** Integers as BigInt, decimals as `Decimal`,
|
|
3717
|
-
* through `decimal.ts`; nothing passes through a JavaScript number. A
|
|
3718
|
-
* total report-core compiled as a decimal is a decimal, its 0 too, and so
|
|
3719
|
-
* is one whose kind the compiler could not tell (`dyn`) once it declares a
|
|
3720
|
-
* rounding or meets a decimal; a group's total is of the report total's
|
|
3721
|
-
* kind, so an expression reading `group.<id>` meets one type.
|
|
3722
|
-
* - **Nulls are skipped**, and so is a value a preview's data lacks. Over no
|
|
3723
|
-
* values a count and a sum are 0 and the rest null (0007).
|
|
3724
|
-
* - **Declared rounding is applied once**, to the exact result: an average
|
|
3725
|
-
* always declares one, and a sum, a minimum or a maximum of decimals may.
|
|
3726
|
-
* A rounding on a total that is an integer or a moment is refused, never
|
|
3727
|
-
* ignored.
|
|
3728
|
-
*/
|
|
3729
|
-
const isRefusal = (value) => value !== null && "code" in value;
|
|
3730
|
-
const integer = (value) => ({
|
|
3731
|
-
type: "integer",
|
|
3732
|
-
value
|
|
3733
|
-
});
|
|
3734
|
-
const decimal = (value) => ({
|
|
3735
|
-
type: "decimal",
|
|
3736
|
-
value
|
|
3737
|
-
});
|
|
3738
|
-
const asDecimal = (value) => value.type === "integer" ? new Decimal(value.value, 0) : value.value;
|
|
3739
|
-
/** Values as numbers of one kind: integers while every one is and decimals are not asked for, else decimals; undefined when one is not a number. */
|
|
3740
|
-
function numbers(values, decimals) {
|
|
3741
|
-
if (!decimals && values.every((value) => value.type === "integer")) return { integers: values.map((value) => value.value) };
|
|
3742
|
-
if (!values.every((value) => value.type === "integer" || value.type === "decimal")) return void 0;
|
|
3743
|
-
return { decimals: values.map(asDecimal) };
|
|
3744
|
-
}
|
|
3745
|
-
const NOT_NUMBERS = {
|
|
3746
|
-
code: "aggregate-value-type",
|
|
3747
|
-
message: "totals integers and decimals, and a value it read is neither"
|
|
3748
|
-
};
|
|
3749
|
-
function sum(values, decimals) {
|
|
3750
|
-
const read = numbers(values, decimals);
|
|
3751
|
-
if (read === void 0) return NOT_NUMBERS;
|
|
3752
|
-
return "integers" in read ? integer(read.integers.reduce((total, value) => total + value, 0n)) : decimal(sumDecimals(read.decimals));
|
|
3753
|
-
}
|
|
3754
|
-
function average(values, rounding) {
|
|
3755
|
-
const read = numbers(values, true);
|
|
3756
|
-
if (read === void 0) return NOT_NUMBERS;
|
|
3757
|
-
const result = averageDecimals(read.decimals, rounding);
|
|
3758
|
-
return result === null ? null : decimal(result);
|
|
3759
|
-
}
|
|
3760
|
-
const ordered = (a, b) => a < b ? -1 : a > b ? 1 : 0;
|
|
3761
|
-
/** -1, 0 or 1 for two values of one kind: numbers by value, text as the report's language sorts it, dates as days, moments in time order. */
|
|
3762
|
-
function comparator(values, collator) {
|
|
3763
|
-
const types = new Set(values.map(({ type }) => type));
|
|
3764
|
-
if ([...types].every((type) => type === "integer" || type === "decimal")) return (a, b) => compareDecimal(asDecimal(a), asDecimal(b));
|
|
3765
|
-
const only = types.size === 1 ? [...types][0] : void 0;
|
|
3766
|
-
if (only === "string") return (a, b) => collator.compare(a.value, b.value);
|
|
3767
|
-
if (only === "date") return (a, b) => ordered(a.value, b.value);
|
|
3768
|
-
if (only === "datetime" || only === "instant") return (a, b) => ordered(fixedWidth(a.value), fixedWidth(b.value));
|
|
3769
|
-
}
|
|
3770
|
-
/** The least (`sign` -1) or greatest (1) value, the first of equals as written; null over none. */
|
|
3771
|
-
function extreme(values, sign, totalling) {
|
|
3772
|
-
const compare = comparator(values, totalling.collator);
|
|
3773
|
-
if (compare === void 0) return {
|
|
3774
|
-
code: "aggregate-value-type",
|
|
3775
|
-
message: "takes the least or greatest of values of one kind, and the values it read are of several"
|
|
3776
|
-
};
|
|
3777
|
-
const found = values.reduce((kept, value) => kept === null || Math.sign(compare(value, kept)) === sign ? value : kept, null);
|
|
3778
|
-
return found?.type === "integer" && totalling.decimals ? decimal(asDecimal(found)) : found;
|
|
3779
|
-
}
|
|
3780
|
-
/** `total` rounded as `rounding` declares: a decimal at the rounding's scale. */
|
|
3781
|
-
function rounded(total, rounding) {
|
|
3782
|
-
if (rounding === void 0 || total === null || isRefusal(total)) return total;
|
|
3783
|
-
if (total.type === "decimal") return decimal(roundDecimalAs(total.value, rounding));
|
|
3784
|
-
return {
|
|
3785
|
-
code: "rounding-not-decimal",
|
|
3786
|
-
message: `declares a rounding, and its total is ${total.type === "integer" ? "an integer" : `a ${total.type}`}, which has no places to round`
|
|
3787
|
-
};
|
|
3788
|
-
}
|
|
3789
|
-
/** The aggregate over the values read from some rows -- each null or typed -- or over `rows` rows when it reads nothing. */
|
|
3790
|
-
function reduce(totalling, read, rows) {
|
|
3791
|
-
const { aggregate, decimals } = totalling;
|
|
3792
|
-
const values = (read ?? []).filter((value) => value !== null);
|
|
3793
|
-
switch (aggregate.fn) {
|
|
3794
|
-
case "count": return integer(BigInt(read === void 0 ? rows : values.length));
|
|
3795
|
-
case "avg": return values.length === 0 ? null : average(values, aggregate.rounding);
|
|
3796
|
-
case "sum": return rounded(sum(values, decimals), aggregate.rounding);
|
|
3797
|
-
case "min": return rounded(extreme(values, -1, totalling), aggregate.rounding);
|
|
3798
|
-
case "max": return rounded(extreme(values, 1, totalling), aggregate.rounding);
|
|
3799
|
-
}
|
|
3800
|
-
}
|
|
3801
|
-
/**
|
|
3802
|
-
* Every aggregate of `render`'s report, over its detail rows and, through
|
|
3803
|
-
* `over`, over any of them; or undefined when one could not be computed,
|
|
3804
|
-
* each reason recorded. `render` has every input but the totals.
|
|
3805
|
-
*/
|
|
3806
|
-
function computeTotals(render) {
|
|
3807
|
-
const { report, expressions, data, problems } = render;
|
|
3808
|
-
const collator = new Intl.Collator(report.locale, { usage: "sort" });
|
|
3809
|
-
const rows = data.detail.map((record, index) => ({
|
|
3810
|
-
index,
|
|
3811
|
-
record
|
|
3812
|
-
}));
|
|
3813
|
-
const read = /* @__PURE__ */ new Map();
|
|
3814
|
-
const totals = /* @__PURE__ */ new Map();
|
|
3815
|
-
const totallings = /* @__PURE__ */ new Map();
|
|
3816
|
-
const refuse = (index, refusal) => {
|
|
3817
|
-
problems.error({
|
|
3818
|
-
stage: "resolve",
|
|
3819
|
-
code: refusal.code,
|
|
3820
|
-
path: `/aggregates/${index}`,
|
|
3821
|
-
message: `aggregate ${report.aggregates[index]?.id} ${refusal.message}`
|
|
3822
|
-
});
|
|
3823
|
-
};
|
|
3824
|
-
let computed = true;
|
|
3825
|
-
for (const id of expressions.compilation.aggregateOrder) {
|
|
3826
|
-
const index = report.aggregates.findIndex((aggregate) => aggregate.id === id);
|
|
3827
|
-
const aggregate = report.aggregates[index];
|
|
3828
|
-
const compiled = expressions.compilation.members.totals.get(id)?.type;
|
|
3829
|
-
const decimals = compiled === "decimal" || compiled === "dyn" && aggregate.rounding !== void 0;
|
|
3830
|
-
const of = aggregate.of;
|
|
3831
|
-
if (of !== void 0) {
|
|
3832
|
-
const path = `/aggregates/${index}/of/expression/cel`;
|
|
3833
|
-
const values = rows.map((row) => of.kind === "field" ? fieldValue(render, row.record, of.field) : valueOf(render, rowScope(render, totals, row), path));
|
|
3834
|
-
if (values.some((value) => value === void 0)) {
|
|
3835
|
-
computed = false;
|
|
3836
|
-
continue;
|
|
3837
|
-
}
|
|
3838
|
-
read.set(id, values);
|
|
3839
|
-
}
|
|
3840
|
-
const totalling = {
|
|
3841
|
-
aggregate,
|
|
3842
|
-
decimals,
|
|
3843
|
-
collator
|
|
3844
|
-
};
|
|
3845
|
-
const total = reduce(totalling, read.get(id), rows.length);
|
|
3846
|
-
if (isRefusal(total)) {
|
|
3847
|
-
computed = false;
|
|
3848
|
-
refuse(index, total);
|
|
3849
|
-
continue;
|
|
3850
|
-
}
|
|
3851
|
-
totals.set(id, total);
|
|
3852
|
-
totallings.set(id, total?.type === "decimal" ? {
|
|
3853
|
-
...totalling,
|
|
3854
|
-
decimals: true
|
|
3855
|
-
} : totalling);
|
|
3856
|
-
}
|
|
3857
|
-
if (!computed) return void 0;
|
|
3858
|
-
return {
|
|
3859
|
-
report: totals,
|
|
3860
|
-
over(group) {
|
|
3861
|
-
const values = /* @__PURE__ */ new Map();
|
|
3862
|
-
for (const aggregate of report.aggregates) {
|
|
3863
|
-
const each = read.get(aggregate.id);
|
|
3864
|
-
values.set(aggregate.id, reduce(totallings.get(aggregate.id), each && group.map((row) => each[row.index]), group.length));
|
|
3865
|
-
}
|
|
3866
|
-
return values;
|
|
3867
|
-
}
|
|
3868
|
-
};
|
|
3869
|
-
}
|
|
3870
|
-
//#endregion
|
|
3871
|
-
//#region src/resolve.ts
|
|
5420
|
+
// src/resolve.ts
|
|
3872
5421
|
/**
|
|
3873
5422
|
* A report resolved against its data: the renderer-neutral document it
|
|
3874
5423
|
* prints, and the manifest that says what it was printed from (plan 14).
|
|
@@ -3877,6 +5426,9 @@ function computeTotals(render) {
|
|
|
3877
5426
|
*
|
|
3878
5427
|
* 1. the report, validated again and its expressions compiled; one the
|
|
3879
5428
|
* editor's diagnostics give an error for is refused (`report-check.ts`);
|
|
5429
|
+
* then written in the locale the render names, its own or one its
|
|
5430
|
+
* translations declare, which every later stage reads (`languages.ts`,
|
|
5431
|
+
* 0063);
|
|
3880
5432
|
* 2. the options, the parameters and the data, each checked, and every
|
|
3881
5433
|
* error of the three listed together (`parameters.ts`, `data.ts`);
|
|
3882
5434
|
* 3. the aggregates, exactly, over every row (`totals.ts`);
|
|
@@ -3897,21 +5449,53 @@ function computeTotals(render) {
|
|
|
3897
5449
|
* `PREVIEW_LABEL`, so that a preview printed or passed on is never taken for
|
|
3898
5450
|
* the report. A final render refuses such data. A preview of an e-invoice
|
|
3899
5451
|
* writes no XML, and is labelled whatever its data:
|
|
3900
|
-
* `E_INVOICE_PREVIEW_LABEL` where nothing else is lacking.
|
|
5452
|
+
* `E_INVOICE_PREVIEW_LABEL` where nothing else is lacking. A final render
|
|
5453
|
+
* of an XRechnung is labelled too, `XRECHNUNG_COPY_LABEL`: its XML is the
|
|
5454
|
+
* invoice, and its PDF a copy (0038). And a final render of an e-invoice
|
|
5455
|
+
* of either kind that a host asks for as the copy (`eInvoice: 'copy'`,
|
|
5456
|
+
* 0077) is labelled as one in the render's language and carries no XML
|
|
5457
|
+
* (`e-invoice-copy.ts`): what a Word file is written of.
|
|
3901
5458
|
*/
|
|
3902
|
-
/** What every page of a preview that lacks data says, at the top of its page header. */
|
|
5459
|
+
/** What every page of a preview that lacks data says, at the top of its page header, and of page 1's own where it has one. */
|
|
3903
5460
|
const PREVIEW_LABEL = "Preview of incomplete data: not a final report";
|
|
3904
5461
|
/** What every page of a preview of an e-invoice says when it lacks nothing else: it writes no XML, and is not the invoice. */
|
|
3905
5462
|
const E_INVOICE_PREVIEW_LABEL = "Preview: not an e-invoice, and not a final report";
|
|
3906
|
-
|
|
3907
|
-
|
|
3908
|
-
|
|
3909
|
-
|
|
3910
|
-
|
|
3911
|
-
|
|
3912
|
-
|
|
3913
|
-
|
|
3914
|
-
|
|
5463
|
+
/** What every page of a final render of an XRechnung says: the invoice is the XML the document carries, which a host sends, and the PDF drawn of the page is a copy (0038). */
|
|
5464
|
+
const XRECHNUNG_COPY_LABEL = "Copy of an XRechnung: the invoice is its XML, and this PDF is not the invoice";
|
|
5465
|
+
/**
|
|
5466
|
+
* The paragraph that opens every page's header of a final render of an
|
|
5467
|
+
* XRechnung, as the runtime writes it: `XRECHNUNG_COPY_LABEL` in the
|
|
5468
|
+
* labels' style. The renderer refuses an XRechnung's document whose page
|
|
5469
|
+
* header, or page 1's own where it has one (0060), does not open with it,
|
|
5470
|
+
* style and all, since the text in white or at one point would say nothing
|
|
5471
|
+
* to a reader (0038). Frozen, so that what
|
|
5472
|
+
* the renderer holds a copy to is not a host's to change; each document
|
|
5473
|
+
* carries a paragraph of its own.
|
|
5474
|
+
*/
|
|
5475
|
+
const XRECHNUNG_COPY_PARAGRAPH = frozen(label(XRECHNUNG_COPY_LABEL));
|
|
5476
|
+
/**
|
|
5477
|
+
* The label a copy of `report`'s e-invoice is headed with where `options`
|
|
5478
|
+
* ask for one (0077), `locale` the one the render prints in: its text,
|
|
5479
|
+
* undefined where no copy is asked for or the report carries no
|
|
5480
|
+
* e-invoice, or the error that refuses the copy. The option is the
|
|
5481
|
+
* host's, whatever its type says.
|
|
5482
|
+
*/
|
|
5483
|
+
function copyLabel(report, own, locale, options) {
|
|
5484
|
+
const asked = options.eInvoice;
|
|
5485
|
+
if (asked === void 0) return void 0;
|
|
5486
|
+
const no = (path, message) => ({
|
|
5487
|
+
stage: "resolve",
|
|
5488
|
+
code: "no-e-invoice-copy",
|
|
5489
|
+
path,
|
|
5490
|
+
message
|
|
5491
|
+
});
|
|
5492
|
+
if (asked !== "copy") return no("", "a render's eInvoice option is 'copy' or absent");
|
|
5493
|
+
if (report.eInvoice === void 0) return void 0;
|
|
5494
|
+
if (options.mode === "preview") return no("", "a copy is of a final render of an e-invoice; a preview writes no XML, and there is no invoice it is a copy of");
|
|
5495
|
+
const [run] = eInvoiceCopyParagraph(report.eInvoice.kind, locale)?.runs ?? [];
|
|
5496
|
+
if (run === void 0 || !("text" in run)) return no(localePath(own, locale), `a copy of an e-invoice is labelled in the language it prints in, and the runtime has no label in the language of ${locale}: it has one in ${E_INVOICE_COPY_LANGUAGES.join(", ")}`);
|
|
5497
|
+
return run.text;
|
|
5498
|
+
}
|
|
3915
5499
|
/** Each parameter's value as the format writes one. */
|
|
3916
5500
|
function writtenParameters(values) {
|
|
3917
5501
|
return Object.fromEntries(Object.entries(values).map(([id, value]) => {
|
|
@@ -3927,12 +5511,12 @@ function writtenParameters(values) {
|
|
|
3927
5511
|
* nothing in the result is shared with them.
|
|
3928
5512
|
*/
|
|
3929
5513
|
function resolveReport(report, data, options) {
|
|
3930
|
-
const checked = checkReport(report);
|
|
5514
|
+
const checked = checkReport(report, options.locale);
|
|
3931
5515
|
if (!checked.ok) return {
|
|
3932
5516
|
ok: false,
|
|
3933
5517
|
errors: checked.errors
|
|
3934
5518
|
};
|
|
3935
|
-
const valid = checked.
|
|
5519
|
+
const valid = checked.printed;
|
|
3936
5520
|
const timeZone = options.timeZone ?? "UTC";
|
|
3937
5521
|
const errors = [];
|
|
3938
5522
|
const creationDate = canonicalInstant(String(options.creationDate));
|
|
@@ -3949,13 +5533,18 @@ function resolveReport(report, data, options) {
|
|
|
3949
5533
|
if (!formatter.ok) errors.push({
|
|
3950
5534
|
stage: "format",
|
|
3951
5535
|
code: formatter.code,
|
|
3952
|
-
path: formatter.code === "unsupported-locale" ?
|
|
5536
|
+
path: formatter.code === "unsupported-locale" ? localePath(checked.report, valid.locale) : "",
|
|
3953
5537
|
message: formatter.message
|
|
3954
5538
|
});
|
|
3955
5539
|
const parameters = checkParameters(valid, options.parameters ?? {});
|
|
3956
5540
|
if (!parameters.ok) errors.push(...parameters.errors.map((error) => fromData("parameters", error)));
|
|
3957
|
-
const checkedData = checkData(valid, data, {
|
|
5541
|
+
const checkedData = checkData(valid, data, {
|
|
5542
|
+
mode: options.mode,
|
|
5543
|
+
reportLocale: checked.report.locale
|
|
5544
|
+
});
|
|
3958
5545
|
if (!checkedData.ok) errors.push(...checkedData.errors.map((error) => fromData("data", error)));
|
|
5546
|
+
const copy = copyLabel(valid, checked.report, valid.locale, options);
|
|
5547
|
+
if (typeof copy === "object") errors.push(copy);
|
|
3959
5548
|
if (errors.length > 0 || creationDate === void 0 || !formatter.ok || !parameters.ok || !checkedData.ok) return {
|
|
3960
5549
|
ok: false,
|
|
3961
5550
|
errors
|
|
@@ -3994,15 +5583,17 @@ function resolveReport(report, data, options) {
|
|
|
3994
5583
|
};
|
|
3995
5584
|
const regions = lowerRegions(render);
|
|
3996
5585
|
const paymentPart = lowerPaymentPart(render);
|
|
5586
|
+
const fonts = resolveFonts(render, valid.fonts);
|
|
3997
5587
|
const eInvoice = invoice === void 0 || valid.eInvoice === void 0 ? void 0 : eInvoiceFile(render, invoice, valid.eInvoice);
|
|
3998
5588
|
if (problems.failed) return {
|
|
3999
5589
|
ok: false,
|
|
4000
5590
|
errors: problems.errors()
|
|
4001
5591
|
};
|
|
4002
|
-
const { completeness, consistency } = checkedData.data;
|
|
5592
|
+
const { completeness, consistency, source } = checkedData.data;
|
|
4003
5593
|
const warnings = problems.warnings();
|
|
4004
5594
|
const lacking = !completeness.complete || warnings.some(({ code }) => code !== "e-invoice-preview");
|
|
4005
|
-
const labelled = !preview ? void 0 : lacking ? PREVIEW_LABEL : invoice === void 0 ? void 0 : E_INVOICE_PREVIEW_LABEL;
|
|
5595
|
+
const labelled = !preview ? typeof copy === "string" ? copy : eInvoice?.kind === "xrechnung" ? XRECHNUNG_COPY_LABEL : void 0 : lacking ? PREVIEW_LABEL : invoice === void 0 ? void 0 : E_INVOICE_PREVIEW_LABEL;
|
|
5596
|
+
const headed = (blocks) => labelled === void 0 ? blocks : [label(labelled), ...blocks];
|
|
4006
5597
|
return {
|
|
4007
5598
|
ok: true,
|
|
4008
5599
|
document: {
|
|
@@ -4011,26 +5602,29 @@ function resolveReport(report, data, options) {
|
|
|
4011
5602
|
creationDate
|
|
4012
5603
|
},
|
|
4013
5604
|
page: valid.page,
|
|
4014
|
-
|
|
5605
|
+
...regions.firstPageHeader === void 0 ? {} : { firstPageHeader: headed(regions.firstPageHeader) },
|
|
5606
|
+
pageHeader: headed(regions.pageHeader),
|
|
4015
5607
|
pageFooter: regions.pageFooter,
|
|
4016
5608
|
body: regions.body,
|
|
4017
5609
|
...paymentPart === void 0 ? {} : { paymentPart },
|
|
4018
|
-
...eInvoice === void 0 ? {} : { eInvoice }
|
|
5610
|
+
...eInvoice === void 0 || typeof copy === "string" ? {} : { eInvoice },
|
|
5611
|
+
...fonts === void 0 ? {} : { fonts }
|
|
4019
5612
|
},
|
|
4020
5613
|
manifest: {
|
|
4021
|
-
reportHash: reportHash(
|
|
5614
|
+
reportHash: reportHash(checked.report),
|
|
4022
5615
|
reportSpecVersion: valid.reportSpecVersion,
|
|
4023
5616
|
locale: valid.locale,
|
|
4024
5617
|
timeZone,
|
|
4025
5618
|
mode: checkedData.data.mode,
|
|
4026
5619
|
consistency,
|
|
4027
5620
|
completeness,
|
|
4028
|
-
parameters: writtenParameters(parameters.values)
|
|
5621
|
+
parameters: writtenParameters(parameters.values),
|
|
5622
|
+
...source === void 0 ? {} : { source },
|
|
5623
|
+
...typeof copy === "string" ? { eInvoice: "copy" } : {}
|
|
4029
5624
|
},
|
|
4030
5625
|
warnings
|
|
4031
5626
|
};
|
|
4032
5627
|
}
|
|
4033
|
-
|
|
4034
|
-
export { CAPABILITIES, CONSISTENCY_MODES, DATA_ERROR_CODES, DATA_MODES, EPC_CAPTION, EPC_CAPTION_MODULE, EPC_FRAME, EPC_MAX_BYTES, EPC_MAX_VERSION, EVALUATION_ERROR_CODES, EXPRESSION_STEP_BUDGET, E_INVOICE_PREVIEW_LABEL, FACTUR_X, FACTUR_X_PROFILES, FORMAT_ERROR_CODES, IMAGE_REFUSAL_CODES, MAX_ASSET_BYTES, MAX_ASSET_SIDE, MAX_DATA_ERRORS, MAX_DETAIL_ROWS, MAX_FORMATTED_DIGITS, MAX_QR_BILL_ADDITIONAL_INFORMATION, MAX_STRING_LENGTH, MAX_SVG_BYTES, MAX_SVG_COORDINATE, MAX_SVG_DEPTH, MAX_SVG_GROUPS, MAX_SVG_SEGMENTS, PREVIEW_LABEL, QR_BILL_CURRENCIES, QR_BILL_SIZE, QR_BILL_TOP, REFUSAL_CODES, RENDER_STEP_BUDGET, RESOLVE_ERROR_CODES, RenderRefusal, SVG_ELEMENTS, SVG_REFUSAL_CODES, VALUE_ERROR_CODES, averageDecimals, checkData, checkParameters, checkQrBillAmount, checkReport, checkSvg, cleanSvg, createFormatter, epcCaptionBreak, epcLeastVersion, epcModule, groupRows, imageHeader, maxDecimal, minDecimal, orderKeys, orderRows, orderTable, plainText, prepareExpressions, qrBillPageBreak, qrSymbolModules, readValue, resolveReport, roundDecimalAs, sniffImage, sumDecimals, supportedLocale };
|
|
5628
|
+
export { BASELINE_SHIFTS, BIDI_CONTROLS, BILL_HEADING_FACES, CAPABILITIES, CONSISTENCY_MODES, DATA_ERROR_CODES, DATA_MODES, EPC_CAPTION, EPC_CAPTION_MODULE, EPC_FRAME, EPC_MAX_BYTES, EPC_MAX_VERSION, EVALUATION_ERROR_CODES, EXPRESSION_STEP_BUDGET, E_INVOICE_COPY_LABELS, E_INVOICE_COPY_LANGUAGES, E_INVOICE_PREVIEW_LABEL, FACTUR_X, FACTUR_X_PROFILES, FONT_EMBEDDING_REFUSALS, FONT_FACE_KEYS, FONT_FORMATS, FONT_MEDIA_TYPES, FONT_REFUSAL_CODES, FORMAT_ERROR_CODES, IMAGE_REFUSAL_CODES, MAX_ASSET_BYTES, MAX_ASSET_SIDE, MAX_CHART_CATEGORIES, MAX_CHART_SERIES, MAX_DATA_ERRORS, MAX_DETAIL_ROWS, MAX_FONT_BYTES, MAX_FONT_CHARACTERS, MAX_FONT_LINE, MAX_FONT_SIZE, MAX_FORMATTED_DIGITS, MAX_KEEP_WITH_ROWS, MAX_QR_BILL_ADDITIONAL_INFORMATION, MAX_SCALE, MAX_STRING_LENGTH, MAX_SVG_BYTES, MAX_SVG_COORDINATE, MAX_SVG_DEPTH, MAX_SVG_GROUPS, MAX_SVG_SEGMENTS, MAX_TICK_INTERVALS, MIN_FONT_LINE, MIN_FONT_SIZE, PREVIEW_LABEL, PRINTED_BLOCKS, QR_BILL_CURRENCIES, QR_BILL_SIZE, QR_BILL_TOP, QR_MAX_CHARACTERS, QR_MIN_MODULE, QR_QUIET_ZONE, REFUSAL_CODES, RENDER_STEP_BUDGET, RESOLVE_ERROR_CODES, RenderRefusal, SHARE_PLACES, SHIFTED_RISE, SHIFTED_SIZE, SLIP_ACCEPTANCE, SLIP_CODE, SLIP_CORNER, SLIP_FIELD_GAP, SLIP_INSTRUCTION, SLIP_PAYMENT_PART, SLIP_RECEIPT, SLIP_RECEIPT_WIDTH, SLIP_SEPARATION, SLIP_TITLE, SOURCE_PROVIDERS, SUBMISSION_ID_SYNTAX, SVG_ELEMENTS, SVG_REFUSAL_CODES, SWISS_CODE_SIZE, SWISS_CROSS, SWISS_QR_MAX_CHARACTERS, UNICODE_VERSION, VALUE_ERROR_CODES, XRECHNUNG, XRECHNUNG_COPY_LABEL, XRECHNUNG_COPY_PARAGRAPH, XRECHNUNG_VERSIONS, assetDigest, averageDecimals, characterMap, checkData, checkParameters, checkQrBill, checkQrBillAmount, checkReport, checkSvg, cleanSvg, createFormatter, eInvoiceCopyParagraph, epcCaptionBreak, epcLeastVersion, epcModule, epcSymbol, faceKey, groupRows, imageHeader, isFontMediaType, isSubmissionId, maxDecimal, minDecimal, orderKeys, orderRows, orderTable, plainText, prepareExpressions, qrBillPageBreak, qrSymbol, qrSymbolModules, qrSymbolVersion, readCopyLabel, readFont, readValue, refuseBillPage, refusedCharacter, resolveReport, roundDecimalAs, slipBlocks, sniffImage, sumDecimals, supportedLocale, swissSymbol };
|
|
4035
5629
|
|
|
4036
5630
|
//# sourceMappingURL=index.mjs.map
|