@qr-bill/invoice 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +9 -0
- package/README.md +86 -2
- package/dist/index.d.ts +577 -0
- package/dist/index.js +2593 -0
- package/dist/index.js.map +1 -0
- package/package.json +50 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Olivier Azevedo
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
|
+
|
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,87 @@
|
|
|
1
|
-
|
|
1
|
+
<h1 align="center">
|
|
2
|
+
<picture>
|
|
3
|
+
<source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/qr-bill/qr-bill/main/docs/assets/logo-white.svg" />
|
|
4
|
+
<source media="(prefers-color-scheme: light)" srcset="https://raw.githubusercontent.com/qr-bill/qr-bill/main/docs/assets/logo-black.svg" />
|
|
5
|
+
<img src="https://raw.githubusercontent.com/qr-bill/qr-bill/main/docs/assets/logo-auto.svg" alt="qr-bill" height="72" />
|
|
6
|
+
</picture>
|
|
7
|
+
</h1>
|
|
2
8
|
|
|
3
|
-
|
|
9
|
+
<p align="center">
|
|
10
|
+
<a href="https://github.com/qr-bill/qr-bill/actions/workflows/ci.yml"><img src="https://github.com/qr-bill/qr-bill/actions/workflows/ci.yml/badge.svg" alt="CI" /></a>
|
|
11
|
+
<a href="https://www.npmjs.com/package/@qr-bill/invoice"><img src="https://img.shields.io/npm/v/@qr-bill/invoice" alt="npm version of @qr-bill/invoice" /></a>
|
|
12
|
+
<a href="https://github.com/qr-bill/qr-bill/blob/main/vitest.config.ts"><img src="https://img.shields.io/badge/coverage-100%25-brightgreen" alt="Coverage: 100 %" /></a>
|
|
13
|
+
<a href="https://github.com/qr-bill/qr-bill/blob/main/LICENSE"><img src="https://img.shields.io/github/license/qr-bill/qr-bill" alt="License: MIT" /></a>
|
|
14
|
+
</p>
|
|
15
|
+
|
|
16
|
+
A Swiss invoice on A4 with its QR-bill, from your data: your logo and details, the recipient in the window of the envelope, the lines, the VAT and the totals, a few ready-made mentions, and the payment part at the bottom of the last page. In German, French, Italian, Romansh and English. Built on [`@qr-bill/core`](https://github.com/qr-bill/qr-bill/tree/main/packages/core).
|
|
17
|
+
|
|
18
|
+
<p align="center">
|
|
19
|
+
<a href="https://github.com/qr-bill/qr-bill/blob/main/docs/assets/invoice-business.pdf"><img src="https://raw.githubusercontent.com/qr-bill/qr-bill/main/docs/assets/invoice-business.png" alt="An invoice in English in the business theme, with its payment slip at the bottom of the page" width="600" /></a>
|
|
20
|
+
</p>
|
|
21
|
+
|
|
22
|
+
- **One call.** `createInvoicePDF(data)` returns the PDF: letterhead with your logo, the recipient in the window of the envelope, the details, the lines over as many pages as needed, the VAT and the totals, ready-made mentions, and the payment slip at the bottom of the last page.
|
|
23
|
+
- **Always produced, never overflowing.** Only the form of the data is checked; every field has a documented maximum length, and every text stays in its zone. The QR-bill of the payment part is checked by `@qr-bill/core`, since a wrong one would make the payment go astray.
|
|
24
|
+
- **Exact amounts.** Computed in integers from decimal strings: VAT per rate, prices with or without VAT, rounding to 5 centimes; or totals computed elsewhere, printed as given.
|
|
25
|
+
- **Five languages, three themes.** German, French, Italian, Romansh and English; `standard`, `business` and `modern`, with your accent colour, every text kept readable.
|
|
26
|
+
- **Light.** A page with its payment slip weighs about 42 KB: the fonts are embedded with only the characters used.
|
|
27
|
+
|
|
28
|
+
## Install
|
|
29
|
+
|
|
30
|
+
```sh
|
|
31
|
+
npm install @qr-bill/invoice pdfkit
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
Node 22.12 or later, PDFKit 0.17 or later.
|
|
35
|
+
|
|
36
|
+
## Example
|
|
37
|
+
|
|
38
|
+
```ts
|
|
39
|
+
import { writeFile } from 'node:fs/promises';
|
|
40
|
+
import { createInvoicePDF } from '@qr-bill/invoice';
|
|
41
|
+
|
|
42
|
+
const pdf = await createInvoicePDF({
|
|
43
|
+
language: 'en',
|
|
44
|
+
theme: 'business',
|
|
45
|
+
number: '2026-042',
|
|
46
|
+
date: '2026-11-20',
|
|
47
|
+
dueDate: '2026-12-20',
|
|
48
|
+
issuer: {
|
|
49
|
+
name: 'Max Muster & Söhne',
|
|
50
|
+
street: 'Musterstrasse',
|
|
51
|
+
buildingNumber: '123',
|
|
52
|
+
postalCode: '8000',
|
|
53
|
+
town: 'Seldwyla',
|
|
54
|
+
country: 'CH',
|
|
55
|
+
uid: 'CHE-123.456.788',
|
|
56
|
+
vatRegistered: true,
|
|
57
|
+
},
|
|
58
|
+
recipient: {
|
|
59
|
+
name: 'Pia-Maria Rutschmann-Schnyder',
|
|
60
|
+
street: 'Grosse Marktgasse',
|
|
61
|
+
buildingNumber: '28',
|
|
62
|
+
postalCode: '9400',
|
|
63
|
+
town: 'Rorschach',
|
|
64
|
+
country: 'CH',
|
|
65
|
+
},
|
|
66
|
+
items: [{ description: 'Development of the new website', quantity: 24, unit: 'h', unitPrice: '150', vatRate: '8.1' }],
|
|
67
|
+
mentions: { paymentDays: 30, thanks: true },
|
|
68
|
+
payment: { account: 'CH44 3199 9123 0008 8901 2', reference: '210000000003139471430009017' },
|
|
69
|
+
});
|
|
70
|
+
await writeFile('invoice.pdf', pdf);
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
Invalid data throws an `InvoiceValidationError` listing every problem; `validateInvoice` returns them without throwing, for a form.
|
|
74
|
+
|
|
75
|
+
## Documentation
|
|
76
|
+
|
|
77
|
+
The complete documentation is in [docs/invoice.md](https://github.com/qr-bill/qr-bill/blob/main/docs/invoice.md):
|
|
78
|
+
|
|
79
|
+
- [the page](https://github.com/qr-bill/qr-bill/blob/main/docs/invoice.md#the-page) and [the logo](https://github.com/qr-bill/qr-bill/blob/main/docs/invoice.md#the-logo);
|
|
80
|
+
- [the schema](https://github.com/qr-bill/qr-bill/blob/main/docs/invoice.md#schema), with the limit of every field;
|
|
81
|
+
- [the themes](https://github.com/qr-bill/qr-bill/blob/main/docs/invoice.md#themes), [the amounts](https://github.com/qr-bill/qr-bill/blob/main/docs/invoice.md#amounts) and [the payment part](https://github.com/qr-bill/qr-bill/blob/main/docs/invoice.md#the-payment-part);
|
|
82
|
+
- [what Swiss law asks of an invoice](https://github.com/qr-bill/qr-bill/blob/main/docs/invoice.md#what-swiss-law-asks-of-an-invoice);
|
|
83
|
+
- [the problems](https://github.com/qr-bill/qr-bill/blob/main/docs/invoice.md#problems) and their codes.
|
|
84
|
+
|
|
85
|
+
## License
|
|
86
|
+
|
|
87
|
+
MIT © Olivier Azevedo — maintained by [Claine](https://claine.ai).
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,577 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Public API of `@qr-bill/invoice`: a Swiss invoice with its QR-bill.
|
|
3
|
+
*
|
|
4
|
+
* - {@link createInvoicePDF}: the PDF of an invoice, with its payment slip.
|
|
5
|
+
* - {@link validateInvoice}: checks and normalizes the data of an invoice, computes its totals and its QR-bill.
|
|
6
|
+
* - {@link computeInvoiceTotals}: the amounts alone, for a form.
|
|
7
|
+
* - {@link validateUID}: the Swiss business identification number.
|
|
8
|
+
* - {@link INVOICE_LIMITS}, {@link INVOICE_THEMES}: the limits of each field and the themes, for a form.
|
|
9
|
+
*
|
|
10
|
+
* @module
|
|
11
|
+
*/
|
|
12
|
+
import { Address, Currency, IssueCode, IssuePath, Language, QRBill } from "@qr-bill/core";
|
|
13
|
+
/**
|
|
14
|
+
* Types of the amounts of an invoice: the totals given by the caller, the totals printed, and the integers they are
|
|
15
|
+
* computed with.
|
|
16
|
+
*
|
|
17
|
+
* @module
|
|
18
|
+
*/
|
|
19
|
+
/** VAT of one rate, as given in {@link InvoiceProvidedTotals}. */
|
|
20
|
+
interface InvoiceProvidedVAT {
|
|
21
|
+
/** The rate in percent, e.g. `"8.1"`. */
|
|
22
|
+
readonly rate: string | number;
|
|
23
|
+
/** The amount the rate applies to; optional. */
|
|
24
|
+
readonly base?: string | number | undefined;
|
|
25
|
+
/** The VAT. */
|
|
26
|
+
readonly amount: string | number;
|
|
27
|
+
}
|
|
28
|
+
/** Totals computed elsewhere, printed as they are: nothing is computed. */
|
|
29
|
+
interface InvoiceProvidedTotals {
|
|
30
|
+
/** Sum of the lines; optional. */
|
|
31
|
+
readonly subtotal?: string | number | undefined;
|
|
32
|
+
/** VAT per rate; optional. */
|
|
33
|
+
readonly vat?: readonly InvoiceProvidedVAT[] | undefined;
|
|
34
|
+
/** The total to pay. */
|
|
35
|
+
readonly total: string | number;
|
|
36
|
+
}
|
|
37
|
+
/** VAT of one rate. */
|
|
38
|
+
interface InvoiceVAT {
|
|
39
|
+
/** The rate in percent, without trailing zeros, e.g. `"8.1"`. */
|
|
40
|
+
readonly rate: string;
|
|
41
|
+
/**
|
|
42
|
+
* The amount the rate applies to: the sum of the lines at this rate, before VAT when the prices exclude it,
|
|
43
|
+
* VAT included when they include it. Absent when given totals have none.
|
|
44
|
+
*/
|
|
45
|
+
readonly base?: string;
|
|
46
|
+
/** The VAT. */
|
|
47
|
+
readonly amount: string;
|
|
48
|
+
}
|
|
49
|
+
/** The amounts of an invoice, as decimal strings with two decimals (`"-90.00"`). */
|
|
50
|
+
interface InvoiceTotals {
|
|
51
|
+
/** The amount of each line, in the order of the lines; absent for a line of text. */
|
|
52
|
+
readonly lines: readonly (string | undefined)[];
|
|
53
|
+
/** The sum of the lines. */
|
|
54
|
+
readonly subtotal?: string;
|
|
55
|
+
/** VAT per rate, highest rate first; empty when no line has a rate. */
|
|
56
|
+
readonly vat: readonly InvoiceVAT[];
|
|
57
|
+
/** What the rounding to 5 centimes added to the total; absent when there is no rounding. */
|
|
58
|
+
readonly rounding?: string;
|
|
59
|
+
/** The total to pay. */
|
|
60
|
+
readonly total: string;
|
|
61
|
+
}
|
|
62
|
+
/** How the totals are computed. */
|
|
63
|
+
interface TotalsOptions {
|
|
64
|
+
/** Whether the prices include VAT; they do not by default. */
|
|
65
|
+
readonly pricesIncludeVAT?: boolean | undefined;
|
|
66
|
+
/** Whether the total is rounded to 5 centimes. */
|
|
67
|
+
readonly roundTo5Centimes?: boolean | undefined;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* A line of the invoice. Its amount is either given (`amount`) or computed (`quantity` × `unitPrice`, the
|
|
71
|
+
* quantity being 1 when absent). A line with a description only is a line of text. Discounts, deposits and fees
|
|
72
|
+
* are lines too, with negative amounts. Numbers may be strings (`"12.50"`, the safest) or numbers.
|
|
73
|
+
*/
|
|
74
|
+
interface InvoiceItem {
|
|
75
|
+
/** What is invoiced, max. 300 characters and 6 lines. */
|
|
76
|
+
readonly description: string;
|
|
77
|
+
/** Quantity, with at most 3 decimals; may be negative. */
|
|
78
|
+
readonly quantity?: string | number | undefined;
|
|
79
|
+
/** Unit, e.g. "h" or "pce"; max. 10 characters. */
|
|
80
|
+
readonly unit?: string | undefined;
|
|
81
|
+
/** Price of one unit, with at most 4 decimals; may be negative. */
|
|
82
|
+
readonly unitPrice?: string | number | undefined;
|
|
83
|
+
/** VAT rate in percent, e.g. `"8.1"`, with at most 2 decimals, from 0 to 100. */
|
|
84
|
+
readonly vatRate?: string | number | undefined;
|
|
85
|
+
/** Amount of the line, with at most 2 decimals; takes precedence over quantity × unit price. */
|
|
86
|
+
readonly amount?: string | number | undefined;
|
|
87
|
+
}
|
|
88
|
+
/** A line of a valid invoice, normalized; numbers are decimal strings. */
|
|
89
|
+
interface InvoiceLine {
|
|
90
|
+
/** What is invoiced. */
|
|
91
|
+
readonly description: string;
|
|
92
|
+
/** Quantity, without trailing zeros after the decimal point. */
|
|
93
|
+
readonly quantity?: string;
|
|
94
|
+
/** Unit. */
|
|
95
|
+
readonly unit?: string;
|
|
96
|
+
/** Price of one unit, with at least two decimals. */
|
|
97
|
+
readonly unitPrice?: string;
|
|
98
|
+
/** VAT rate in percent, without trailing zeros. */
|
|
99
|
+
readonly vatRate?: string;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* The issuer of the invoice, who receives the payment. Its name and address are also the creditor of the payment
|
|
103
|
+
* part: they follow the rules of the QR-bill (structured address, character set).
|
|
104
|
+
*/
|
|
105
|
+
interface InvoiceIssuer extends Address {
|
|
106
|
+
/**
|
|
107
|
+
* The logo, as the bytes of a PNG or JPEG file, max. 2 MB. It is fitted in a box of 50 × 18 mm and embedded as it
|
|
108
|
+
* is: about 591 × 213 pixels (300 dpi) is the best size, a larger image only makes the PDF heavier.
|
|
109
|
+
*/
|
|
110
|
+
readonly logo?: Uint8Array | undefined;
|
|
111
|
+
/** Up to 2 lines printed under the name on the invoice only, e.g. a department; max. 70 characters each. */
|
|
112
|
+
readonly extraLines?: readonly string[] | undefined;
|
|
113
|
+
/** Up to 2 phone numbers, as they should be printed; max. 30 characters each. */
|
|
114
|
+
readonly phones?: readonly string[] | undefined;
|
|
115
|
+
/** E-mail address, max. 70 characters. */
|
|
116
|
+
readonly email?: string | undefined;
|
|
117
|
+
/** A free line for another contact, e.g. a website; max. 70 characters. */
|
|
118
|
+
readonly contact?: string | undefined;
|
|
119
|
+
/** The Swiss business identification number (UID), e.g. `CHE-123.456.788`; its check digit is verified. */
|
|
120
|
+
readonly uid?: string | undefined;
|
|
121
|
+
/** Whether the issuer is registered for VAT: the UID is then printed as its VAT number (`CHE-123.456.788 TVA`). */
|
|
122
|
+
readonly vatRegistered?: boolean | undefined;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* The recipient of the invoice, who pays. Its name and address are also the debtor of the payment part: they follow
|
|
126
|
+
* the rules of the QR-bill (structured address, character set).
|
|
127
|
+
*/
|
|
128
|
+
interface InvoiceRecipient extends Address {
|
|
129
|
+
/** A line above the name, e.g. "Madame"; max. 35 characters. */
|
|
130
|
+
readonly salutation?: string | undefined;
|
|
131
|
+
/** Up to 2 lines under the name on the invoice only, e.g. a department or "c/o"; max. 70 characters each. */
|
|
132
|
+
readonly extraLines?: readonly string[] | undefined;
|
|
133
|
+
}
|
|
134
|
+
/** A logo, with its format recognized from its first bytes. */
|
|
135
|
+
interface InvoiceLogo {
|
|
136
|
+
/** The format of the file. */
|
|
137
|
+
readonly format: "png" | "jpeg";
|
|
138
|
+
/** The bytes of the file. */
|
|
139
|
+
readonly bytes: Uint8Array;
|
|
140
|
+
}
|
|
141
|
+
/** The issuer of a valid invoice, normalized. */
|
|
142
|
+
interface InvoiceIssuerData extends Address {
|
|
143
|
+
/** The logo. */
|
|
144
|
+
readonly logo?: InvoiceLogo;
|
|
145
|
+
/** Lines under the name. */
|
|
146
|
+
readonly extraLines: readonly string[];
|
|
147
|
+
/** Phone numbers. */
|
|
148
|
+
readonly phones: readonly string[];
|
|
149
|
+
/** E-mail address. */
|
|
150
|
+
readonly email?: string;
|
|
151
|
+
/** Free contact line. */
|
|
152
|
+
readonly contact?: string;
|
|
153
|
+
/** UID, formatted as `CHE-123.456.788`. */
|
|
154
|
+
readonly uid?: string;
|
|
155
|
+
/** Whether the issuer is registered for VAT. */
|
|
156
|
+
readonly vatRegistered: boolean;
|
|
157
|
+
}
|
|
158
|
+
/** The recipient of a valid invoice, normalized. */
|
|
159
|
+
interface InvoiceRecipientData extends Address {
|
|
160
|
+
/** Line above the name. */
|
|
161
|
+
readonly salutation?: string;
|
|
162
|
+
/** Lines under the name. */
|
|
163
|
+
readonly extraLines: readonly string[];
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Types of the themes: what the caller chooses, how a theme is defined, and the colours computed from it.
|
|
167
|
+
*
|
|
168
|
+
* @module
|
|
169
|
+
*/
|
|
170
|
+
/** The names of the themes: `standard` (black and grey), `business` (a filled band), `modern` (light tints). */
|
|
171
|
+
type ThemeName = "standard" | "business" | "modern";
|
|
172
|
+
/** The look of the invoice, as given: a theme, and optionally its colour. */
|
|
173
|
+
interface InvoiceTheme {
|
|
174
|
+
/** The theme; `standard` by default. */
|
|
175
|
+
readonly name?: ThemeName | undefined;
|
|
176
|
+
/** The accent colour, as `#RRGGBB`; each theme has its own by default. */
|
|
177
|
+
readonly accent?: string | undefined;
|
|
178
|
+
}
|
|
179
|
+
/** The theme of a valid invoice. */
|
|
180
|
+
interface InvoiceThemeData {
|
|
181
|
+
/** The theme. */
|
|
182
|
+
readonly name: ThemeName;
|
|
183
|
+
/** The accent colour, as `#RRGGBB` in upper case, if one was given. */
|
|
184
|
+
readonly accent?: string;
|
|
185
|
+
}
|
|
186
|
+
/** How amounts are written on the invoice: `1 234.50` (as on the payment part) or `1'234.50`. */
|
|
187
|
+
type AmountFormat = "space" | "apostrophe";
|
|
188
|
+
/** Where the address of the recipient goes: in the window of the envelope on the left or on the right. */
|
|
189
|
+
type AddressWindow = "left" | "right";
|
|
190
|
+
/** A label and a value printed with the details of the invoice, e.g. "Customer no." and "4711". */
|
|
191
|
+
interface InvoiceField {
|
|
192
|
+
/** The label, max. 25 characters. */
|
|
193
|
+
readonly label: string;
|
|
194
|
+
/** The value, max. 50 characters. */
|
|
195
|
+
readonly value: string;
|
|
196
|
+
}
|
|
197
|
+
/** Ready-made mentions, printed in the language of the invoice. */
|
|
198
|
+
interface InvoiceMentions {
|
|
199
|
+
/** "Not registered for VAT", for an issuer below the VAT threshold. */
|
|
200
|
+
readonly notVatRegistered?: boolean | undefined;
|
|
201
|
+
/** "Supply excluded from VAT", e.g. for care, education or membership fees. */
|
|
202
|
+
readonly vatExcluded?: boolean | undefined;
|
|
203
|
+
/** "Payable within N days", from 1 to 365. */
|
|
204
|
+
readonly paymentDays?: number | undefined;
|
|
205
|
+
/** "Thank you for your trust". */
|
|
206
|
+
readonly thanks?: boolean | undefined;
|
|
207
|
+
}
|
|
208
|
+
/** The payment part. Its amount is always the total of the invoice. */
|
|
209
|
+
interface InvoicePayment {
|
|
210
|
+
/** IBAN or QR-IBAN of the issuer, from Switzerland or Liechtenstein; spaces and lower case are accepted. */
|
|
211
|
+
readonly account: string;
|
|
212
|
+
/** QR reference (with a QR-IBAN) or Creditor Reference (`RF…`); optional. */
|
|
213
|
+
readonly reference?: string | undefined;
|
|
214
|
+
/** Message of the payment part; by default the title and the number of the invoice. Max. 140 characters. */
|
|
215
|
+
readonly message?: string | undefined;
|
|
216
|
+
/** Coded billing information starting with `//`, e.g. Swico; optional. */
|
|
217
|
+
readonly billingInformation?: string | undefined;
|
|
218
|
+
}
|
|
219
|
+
/** The data of an invoice, as given to {@link validateInvoice}. */
|
|
220
|
+
interface InvoiceInput {
|
|
221
|
+
/** Language of the invoice and of its payment part. */
|
|
222
|
+
readonly language: Language;
|
|
223
|
+
/** `CHF` (default) or `EUR`. */
|
|
224
|
+
readonly currency?: Currency | undefined;
|
|
225
|
+
/** Title of the document; "Invoice" in the language of the invoice by default. Max. 35 characters. */
|
|
226
|
+
readonly title?: string | undefined;
|
|
227
|
+
/** Number of the invoice, max. 35 characters. */
|
|
228
|
+
readonly number?: string | undefined;
|
|
229
|
+
/** Date of the invoice, as `YYYY-MM-DD`. */
|
|
230
|
+
readonly date: string;
|
|
231
|
+
/** Due date, as `YYYY-MM-DD`. */
|
|
232
|
+
readonly dueDate?: string | undefined;
|
|
233
|
+
/** Period of the supply, as it should be printed, e.g. "November 2026"; max. 50 characters. */
|
|
234
|
+
readonly servicePeriod?: string | undefined;
|
|
235
|
+
/** The reference of the recipient, e.g. an order number; max. 35 characters. */
|
|
236
|
+
readonly customerReference?: string | undefined;
|
|
237
|
+
/** Up to 3 more details, e.g. a customer number. */
|
|
238
|
+
readonly extraFields?: readonly InvoiceField[] | undefined;
|
|
239
|
+
/** Subject, max. 140 characters. */
|
|
240
|
+
readonly subject?: string | undefined;
|
|
241
|
+
/** Text before the lines, max. 1000 characters and 10 lines. */
|
|
242
|
+
readonly introduction?: string | undefined;
|
|
243
|
+
/** Text after the totals, max. 1000 characters and 10 lines. */
|
|
244
|
+
readonly closing?: string | undefined;
|
|
245
|
+
/** Who issues the invoice. */
|
|
246
|
+
readonly issuer: InvoiceIssuer;
|
|
247
|
+
/** Who receives it. */
|
|
248
|
+
readonly recipient: InvoiceRecipient;
|
|
249
|
+
/** The lines, from 1 to 1000. */
|
|
250
|
+
readonly items: readonly InvoiceItem[];
|
|
251
|
+
/** Whether the prices include VAT; by default they do not, and the VAT is added. */
|
|
252
|
+
readonly pricesIncludeVAT?: boolean | undefined;
|
|
253
|
+
/** Whether the total is rounded to 5 centimes, with a rounding line. */
|
|
254
|
+
readonly roundTo5Centimes?: boolean | undefined;
|
|
255
|
+
/** Totals computed elsewhere, printed as they are instead of being computed. */
|
|
256
|
+
readonly totals?: InvoiceProvidedTotals | undefined;
|
|
257
|
+
/** Ready-made mentions. */
|
|
258
|
+
readonly mentions?: InvoiceMentions | undefined;
|
|
259
|
+
/** Up to 3 free remarks, max. 300 characters and 4 lines each. */
|
|
260
|
+
readonly notes?: readonly string[] | undefined;
|
|
261
|
+
/** The payment part; without it, the invoice has none. */
|
|
262
|
+
readonly payment?: InvoicePayment | undefined;
|
|
263
|
+
/** How amounts are written; `space` by default. */
|
|
264
|
+
readonly amountFormat?: AmountFormat | undefined;
|
|
265
|
+
/** Where the address of the recipient goes; `left` by default. */
|
|
266
|
+
readonly addressWindow?: AddressWindow | undefined;
|
|
267
|
+
/** The look of the invoice, or the name of a theme. */
|
|
268
|
+
readonly theme?: InvoiceTheme | ThemeName | undefined;
|
|
269
|
+
}
|
|
270
|
+
/** A valid invoice, normalized and frozen, with its totals and its QR-bill: what is printed. */
|
|
271
|
+
interface Invoice {
|
|
272
|
+
/** Language. */
|
|
273
|
+
readonly language: Language;
|
|
274
|
+
/** Currency. */
|
|
275
|
+
readonly currency: Currency;
|
|
276
|
+
/** Title, the default one if none was given. */
|
|
277
|
+
readonly title: string;
|
|
278
|
+
/** Number. */
|
|
279
|
+
readonly number?: string;
|
|
280
|
+
/** Date, `YYYY-MM-DD`. */
|
|
281
|
+
readonly date: string;
|
|
282
|
+
/** Due date, `YYYY-MM-DD`. */
|
|
283
|
+
readonly dueDate?: string;
|
|
284
|
+
/** Period of the supply. */
|
|
285
|
+
readonly servicePeriod?: string;
|
|
286
|
+
/** Reference of the recipient. */
|
|
287
|
+
readonly customerReference?: string;
|
|
288
|
+
/** More details. */
|
|
289
|
+
readonly extraFields: readonly InvoiceField[];
|
|
290
|
+
/** Subject. */
|
|
291
|
+
readonly subject?: string;
|
|
292
|
+
/** Text before the lines. */
|
|
293
|
+
readonly introduction?: string;
|
|
294
|
+
/** Text after the totals. */
|
|
295
|
+
readonly closing?: string;
|
|
296
|
+
/** Issuer. */
|
|
297
|
+
readonly issuer: InvoiceIssuerData;
|
|
298
|
+
/** Recipient. */
|
|
299
|
+
readonly recipient: InvoiceRecipientData;
|
|
300
|
+
/** Lines. */
|
|
301
|
+
readonly lines: readonly InvoiceLine[];
|
|
302
|
+
/** Whether the prices include VAT. */
|
|
303
|
+
readonly pricesIncludeVAT: boolean;
|
|
304
|
+
/** Amounts, computed or given. */
|
|
305
|
+
readonly totals: InvoiceTotals;
|
|
306
|
+
/** Whether the totals were given rather than computed. */
|
|
307
|
+
readonly totalsProvided: boolean;
|
|
308
|
+
/** Ready-made mentions. */
|
|
309
|
+
readonly mentions: InvoiceMentions;
|
|
310
|
+
/** Free remarks. */
|
|
311
|
+
readonly notes: readonly string[];
|
|
312
|
+
/** How amounts are written. */
|
|
313
|
+
readonly amountFormat: AmountFormat;
|
|
314
|
+
/** Where the address of the recipient goes. */
|
|
315
|
+
readonly addressWindow: AddressWindow;
|
|
316
|
+
/** The theme, and its accent colour if one was given. */
|
|
317
|
+
readonly theme: InvoiceThemeData;
|
|
318
|
+
/** The QR-bill of the payment part: absent without payment, or when the total is negative (a credit note). */
|
|
319
|
+
readonly bill?: QRBill;
|
|
320
|
+
}
|
|
321
|
+
/**
|
|
322
|
+
* What a problem is. The codes of the QR-bill ({@link IssueCode}) are reused for the fields of the payment part
|
|
323
|
+
* and of the addresses; the others are specific to the invoice. Stable: clients may translate them.
|
|
324
|
+
*/
|
|
325
|
+
type InvoiceIssueCode = IssueCode | "too_many" | "too_many_lines" | "invalid_language" | "invalid_date" | "invalid_number" | "out_of_range" | "invalid_uid" | "invalid_email" | "invalid_option" | "invalid_color" | "unsupported_logo" | "logo_too_large";
|
|
326
|
+
/** A problem found in an invoice. */
|
|
327
|
+
interface InvoiceIssue {
|
|
328
|
+
/** What the problem is; stable (see {@link InvoiceIssueCode}). */
|
|
329
|
+
readonly code: InvoiceIssueCode;
|
|
330
|
+
/** Where the problem is, e.g. `["items", 2, "unitPrice"]`; empty for the invoice as a whole. */
|
|
331
|
+
readonly path: IssuePath;
|
|
332
|
+
/** Explanation in English, for logs and developers. */
|
|
333
|
+
readonly message: string;
|
|
334
|
+
}
|
|
335
|
+
/** Result of {@link validateInvoice}: the invoice, or every problem found. */
|
|
336
|
+
type InvoiceResult = {
|
|
337
|
+
/** The invoice is valid. */
|
|
338
|
+
readonly success: true;
|
|
339
|
+
/** The normalized, frozen invoice. */
|
|
340
|
+
readonly data: Invoice;
|
|
341
|
+
} | {
|
|
342
|
+
/** The invoice is not valid. */
|
|
343
|
+
readonly success: false;
|
|
344
|
+
/** Every problem found. */
|
|
345
|
+
readonly issues: readonly InvoiceIssue[];
|
|
346
|
+
};
|
|
347
|
+
/**
|
|
348
|
+
* Creates the PDF of an invoice: an A4 document with the letterhead of the issuer, the recipient in the window of
|
|
349
|
+
* the envelope, the details, the lines over as many pages as needed, the totals, the mentions, and the payment slip
|
|
350
|
+
* at the bottom of the last page, or on a page of its own when there is no room left. Without `payment`, or with a
|
|
351
|
+
* negative total (a credit note), the invoice has no payment slip.
|
|
352
|
+
*
|
|
353
|
+
* Nothing overflows: every text is wrapped or shortened within its zone, whatever its length within
|
|
354
|
+
* {@link INVOICE_LIMITS}. The fonts are Liberation Sans, embedded with only the characters used.
|
|
355
|
+
*
|
|
356
|
+
* @param input - The data of the invoice, checked by {@link validateInvoice}.
|
|
357
|
+
* @returns The bytes of the PDF file.
|
|
358
|
+
* @throws {InvoiceValidationError} When the data is not valid, with every problem found.
|
|
359
|
+
* @example
|
|
360
|
+
* ```ts
|
|
361
|
+
* const pdf = await createInvoicePDF({
|
|
362
|
+
* language: 'fr',
|
|
363
|
+
* date: '2026-11-20',
|
|
364
|
+
* issuer: { name: 'Max Muster & Söhne', street: 'Musterstrasse', buildingNumber: '123', postalCode: '8000', town: 'Seldwyla', country: 'CH' },
|
|
365
|
+
* recipient: { name: 'Simon Muster', postalCode: '8000', town: 'Seldwyla', country: 'CH' },
|
|
366
|
+
* items: [{ description: 'Conseil', quantity: 6, unit: 'h', unitPrice: '150', vatRate: '8.1' }],
|
|
367
|
+
* payment: { account: 'CH44 3199 9123 0008 8901 2', reference: '210000000003139471430009017' },
|
|
368
|
+
* });
|
|
369
|
+
* await writeFile('facture.pdf', pdf);
|
|
370
|
+
* ```
|
|
371
|
+
*/
|
|
372
|
+
export declare function createInvoicePDF(input: InvoiceInput): Promise<Uint8Array>;
|
|
373
|
+
/**
|
|
374
|
+
* Thrown by {@link createInvoicePDF}. Lists every problem found, not just the first.
|
|
375
|
+
*
|
|
376
|
+
* @example
|
|
377
|
+
* ```ts
|
|
378
|
+
* try {
|
|
379
|
+
* await createInvoicePDF(input);
|
|
380
|
+
* } catch (error) {
|
|
381
|
+
* if (error instanceof InvoiceValidationError) {
|
|
382
|
+
* error.issues; // [{ code: 'invalid_uid', path: ['issuer', 'uid'], message: '…' }, …]
|
|
383
|
+
* }
|
|
384
|
+
* }
|
|
385
|
+
* ```
|
|
386
|
+
*/
|
|
387
|
+
export declare class InvoiceValidationError extends Error {
|
|
388
|
+
/** Always "InvoiceValidationError". */
|
|
389
|
+
override readonly name = "InvoiceValidationError";
|
|
390
|
+
/** Every problem found. */
|
|
391
|
+
readonly issues: readonly InvoiceIssue[];
|
|
392
|
+
/**
|
|
393
|
+
* @param issues - The problems found; the message of the error summarizes them.
|
|
394
|
+
*/
|
|
395
|
+
constructor(issues: readonly InvoiceIssue[]);
|
|
396
|
+
}
|
|
397
|
+
/**
|
|
398
|
+
* Validates and normalizes an invoice without throwing.
|
|
399
|
+
*
|
|
400
|
+
* Accepts `unknown`, so that a request body can be passed as is. The issuer and the recipient are checked with the
|
|
401
|
+
* validators of the QR-bill, since they are also the creditor and the debtor of the payment part; the payment part
|
|
402
|
+
* is checked by `validateQRBill` of `@qr-bill/core`, with the total of the invoice as its amount. Its codes are
|
|
403
|
+
* reused for these fields.
|
|
404
|
+
*
|
|
405
|
+
* @param input - The invoice to check, normally an {@link InvoiceInput}.
|
|
406
|
+
* @returns Either `{ success: true, data }`, where `data` is the normalized, frozen {@link Invoice} with its totals
|
|
407
|
+
* and its QR-bill, or `{ success: false, issues }` listing every problem found.
|
|
408
|
+
* @example
|
|
409
|
+
* ```ts
|
|
410
|
+
* const result = validateInvoice(requestBody);
|
|
411
|
+
* if (!result.success) {
|
|
412
|
+
* result.issues; // [{ code: 'invalid_uid', path: ['issuer', 'uid'], message: '…' }, …]
|
|
413
|
+
* }
|
|
414
|
+
* ```
|
|
415
|
+
*/
|
|
416
|
+
export declare function validateInvoice(input: unknown): InvoiceResult;
|
|
417
|
+
/**
|
|
418
|
+
* Types of the limits of the fields of an invoice.
|
|
419
|
+
*
|
|
420
|
+
* @module
|
|
421
|
+
*/
|
|
422
|
+
/** Limits of the issuer's fields. */
|
|
423
|
+
interface IssuerLimits {
|
|
424
|
+
/** Name, as on the payment part. */
|
|
425
|
+
readonly name: number;
|
|
426
|
+
/** Street, as on the payment part. */
|
|
427
|
+
readonly street: number;
|
|
428
|
+
/** Building number, as on the payment part. */
|
|
429
|
+
readonly buildingNumber: number;
|
|
430
|
+
/** Postal code, as on the payment part. */
|
|
431
|
+
readonly postalCode: number;
|
|
432
|
+
/** Town, as on the payment part. */
|
|
433
|
+
readonly town: number;
|
|
434
|
+
/** Number of lines under the name. */
|
|
435
|
+
readonly extraLines: number;
|
|
436
|
+
/** One line under the name. */
|
|
437
|
+
readonly extraLine: number;
|
|
438
|
+
/** Number of phone numbers. */
|
|
439
|
+
readonly phones: number;
|
|
440
|
+
/** One phone number. */
|
|
441
|
+
readonly phone: number;
|
|
442
|
+
/** E-mail address. */
|
|
443
|
+
readonly email: number;
|
|
444
|
+
/** Free contact line. */
|
|
445
|
+
readonly contact: number;
|
|
446
|
+
}
|
|
447
|
+
/** Limits of the recipient's fields. */
|
|
448
|
+
interface RecipientLimits {
|
|
449
|
+
/** Line above the name. */
|
|
450
|
+
readonly salutation: number;
|
|
451
|
+
/** Name, as on the payment part. */
|
|
452
|
+
readonly name: number;
|
|
453
|
+
/** Street, as on the payment part. */
|
|
454
|
+
readonly street: number;
|
|
455
|
+
/** Building number, as on the payment part. */
|
|
456
|
+
readonly buildingNumber: number;
|
|
457
|
+
/** Postal code, as on the payment part. */
|
|
458
|
+
readonly postalCode: number;
|
|
459
|
+
/** Town, as on the payment part. */
|
|
460
|
+
readonly town: number;
|
|
461
|
+
/** Number of lines under the name. */
|
|
462
|
+
readonly extraLines: number;
|
|
463
|
+
/** One line under the name. */
|
|
464
|
+
readonly extraLine: number;
|
|
465
|
+
}
|
|
466
|
+
/** Maximum lengths, in characters (Unicode code points), and maximum numbers of lines or entries. */
|
|
467
|
+
interface InvoiceLimits {
|
|
468
|
+
/** Title of the document. */
|
|
469
|
+
readonly title: number;
|
|
470
|
+
/** Number of the invoice. */
|
|
471
|
+
readonly number: number;
|
|
472
|
+
/** Period of the supply. */
|
|
473
|
+
readonly servicePeriod: number;
|
|
474
|
+
/** Reference of the recipient. */
|
|
475
|
+
readonly customerReference: number;
|
|
476
|
+
/** Number of extra details. */
|
|
477
|
+
readonly extraFields: number;
|
|
478
|
+
/** Label of an extra detail. */
|
|
479
|
+
readonly extraFieldLabel: number;
|
|
480
|
+
/** Value of an extra detail. */
|
|
481
|
+
readonly extraFieldValue: number;
|
|
482
|
+
/** Subject. */
|
|
483
|
+
readonly subject: number;
|
|
484
|
+
/** Text before the lines. */
|
|
485
|
+
readonly introduction: number;
|
|
486
|
+
/** Lines of the text before the lines. */
|
|
487
|
+
readonly introductionLines: number;
|
|
488
|
+
/** Text after the totals. */
|
|
489
|
+
readonly closing: number;
|
|
490
|
+
/** Lines of the text after the totals. */
|
|
491
|
+
readonly closingLines: number;
|
|
492
|
+
/** The issuer's fields. */
|
|
493
|
+
readonly issuer: IssuerLimits;
|
|
494
|
+
/** The recipient's fields. */
|
|
495
|
+
readonly recipient: RecipientLimits;
|
|
496
|
+
/** Number of lines of the invoice. */
|
|
497
|
+
readonly items: number;
|
|
498
|
+
/** Description of a line. */
|
|
499
|
+
readonly description: number;
|
|
500
|
+
/** Lines of the description of a line. */
|
|
501
|
+
readonly descriptionLines: number;
|
|
502
|
+
/** Unit of a line. */
|
|
503
|
+
readonly unit: number;
|
|
504
|
+
/** Number of free remarks. */
|
|
505
|
+
readonly notes: number;
|
|
506
|
+
/** A free remark. */
|
|
507
|
+
readonly note: number;
|
|
508
|
+
/** Lines of a free remark. */
|
|
509
|
+
readonly noteLines: number;
|
|
510
|
+
/** Size of the logo file, in bytes. */
|
|
511
|
+
readonly logoBytes: number;
|
|
512
|
+
}
|
|
513
|
+
/**
|
|
514
|
+
* The limits of each field, for the `maxlength` of a form. A longer value is reported by {@link validateInvoice}
|
|
515
|
+
* as `too_long`, `too_many` or `too_many_lines`.
|
|
516
|
+
*
|
|
517
|
+
* @example
|
|
518
|
+
* ```ts
|
|
519
|
+
* INVOICE_LIMITS.issuer.phones; // 2
|
|
520
|
+
* ```
|
|
521
|
+
*/
|
|
522
|
+
export declare const INVOICE_LIMITS: InvoiceLimits;
|
|
523
|
+
/**
|
|
524
|
+
* Computes the amounts of an invoice from its lines, without the rest of the invoice: useful to show the totals
|
|
525
|
+
* while a form is being filled.
|
|
526
|
+
*
|
|
527
|
+
* - The amount of a line is its `amount`, or its `quantity` (1 when absent) × `unitPrice`, rounded to the centime;
|
|
528
|
+
* a line with neither is a line of text.
|
|
529
|
+
* - The VAT is computed per rate on the sum of the lines at that rate. Prices exclude VAT by default: it is added to
|
|
530
|
+
* the total. With `pricesIncludeVAT`, the VAT is the part of the gross amount that is VAT.
|
|
531
|
+
* - Roundings are commercial (half away from zero), to the centime; the total is rounded to 5 centimes with
|
|
532
|
+
* `roundTo5Centimes`.
|
|
533
|
+
*
|
|
534
|
+
* @param items - The lines of the invoice.
|
|
535
|
+
* @param options - Prices with or without VAT, rounding to 5 centimes.
|
|
536
|
+
* @returns The amount of each line, the subtotal, the VAT per rate (highest rate first), the rounding and the
|
|
537
|
+
* total, as decimal strings.
|
|
538
|
+
* @throws {RangeError} When a quantity, price, rate or amount is not a number with the decimals allowed.
|
|
539
|
+
* @example
|
|
540
|
+
* ```ts
|
|
541
|
+
* computeInvoiceTotals([{ description: 'Advice', quantity: 6, unitPrice: '150', vatRate: '8.1' }]).total; // '972.90'
|
|
542
|
+
* ```
|
|
543
|
+
*/
|
|
544
|
+
export declare function computeInvoiceTotals(items: readonly InvoiceItem[], options?: TotalsOptions): InvoiceTotals;
|
|
545
|
+
/**
|
|
546
|
+
* Types of the Swiss business identification number (UID).
|
|
547
|
+
*
|
|
548
|
+
* @module
|
|
549
|
+
*/
|
|
550
|
+
/** Result of {@link validateUID}. */
|
|
551
|
+
type UIDStatus = "valid" | "required" | "invalid_uid";
|
|
552
|
+
/**
|
|
553
|
+
* Validates a Swiss business identification number (UID): its shape and its check digit. Separators, spaces, lower
|
|
554
|
+
* case and a VAT suffix (`MWST`, `TVA`, `IVA`) are accepted. Blank counts as missing.
|
|
555
|
+
*
|
|
556
|
+
* @param value - The UID as typed.
|
|
557
|
+
* @returns `'valid'`, `'required'` when blank, or `'invalid_uid'`.
|
|
558
|
+
* @example
|
|
559
|
+
* ```ts
|
|
560
|
+
* validateUID('CHE-123.456.788'); // 'valid'
|
|
561
|
+
* validateUID('che 123 456 788 tva'); // 'valid'
|
|
562
|
+
* validateUID('CHE-123.456.789'); // 'invalid_uid': wrong check digit
|
|
563
|
+
* validateUID(' '); // 'required'
|
|
564
|
+
* ```
|
|
565
|
+
*/
|
|
566
|
+
export declare function validateUID(value: string | null | undefined): UIDStatus;
|
|
567
|
+
/**
|
|
568
|
+
* The names of the themes, for a selector: `standard` (black and grey, the default), `business` (a filled band of
|
|
569
|
+
* colour), `modern` (light tints).
|
|
570
|
+
*
|
|
571
|
+
* @example
|
|
572
|
+
* ```ts
|
|
573
|
+
* INVOICE_THEMES.length; // 3
|
|
574
|
+
* ```
|
|
575
|
+
*/
|
|
576
|
+
export declare const INVOICE_THEMES: readonly ThemeName[];
|
|
577
|
+
export type { AddressWindow, AmountFormat, Invoice, InvoiceField, InvoiceInput, InvoiceIssue, InvoiceIssueCode, InvoiceIssuer, InvoiceIssuerData, InvoiceItem, InvoiceLimits, InvoiceLine, InvoiceLogo, InvoiceMentions, InvoicePayment, InvoiceProvidedTotals, InvoiceProvidedVAT, InvoiceRecipient, InvoiceRecipientData, InvoiceResult, InvoiceTheme, InvoiceThemeData, InvoiceTotals, InvoiceVAT, IssuerLimits, RecipientLimits, ThemeName, TotalsOptions, UIDStatus };
|