Invoice document type
OpeningThe heading is a legal decision, not a label. A seller who is not VAT-registered gets 'Invoice' and no VAT wording, because calling it a VAT invoice is an offence in the UK. A registered seller gets 'VAT invoice', or 'Simplified VAT invoice' under the local ceiling where they allow it. A pro forma says it is not a VAT invoice, so nobody reclaims on it, and a self-billed invoice carries its jurisdiction's statement. The supply date prints only when it differs from the issue date, except in Germany, which always wants a Leistungsdatum. All of it is one deriver, not a dozen published arms.
npx dxcl add invoice-document-type invoice.kind Voorbeelddata
de JSON waarmee dit voorbeeld is gebouwd{
"invoice": {
"number": "SI-2026-11-004817/REV-2",
"issueDate": "2026-11-30",
"supplyDate": "2026-09-28",
"kind": "invoice",
"jurisdiction": "UK",
"grossTotal": 1284750,
"purchaseOrder": "PO-4500098817-FACILITIES-NORTH"
},
"seller": {
"vatRegistered": true,
"vatNumber": "GB 987 6543 21",
"simplifiedAllowed": true
}
} {
"invoice": {
"number": "SI-2026-11-004817/REV-2",
"issueDate": "2026-11-30",
"supplyDate": "2026-09-28",
"kind": "proforma",
"jurisdiction": "UK",
"grossTotal": 1284750,
"purchaseOrder": "PO-4500098817-FACILITIES-NORTH"
},
"seller": {
"vatRegistered": true,
"vatNumber": "GB 987 6543 21",
"simplifiedAllowed": true
}
} {
"invoice": {
"number": "SI-2026-11-004817/REV-2",
"issueDate": "2026-11-30",
"supplyDate": "2026-09-28",
"kind": "self-billed",
"jurisdiction": "UK",
"grossTotal": 1284750,
"purchaseOrder": "PO-4500098817-FACILITIES-NORTH"
},
"seller": {
"vatRegistered": true,
"vatNumber": "GB 987 6543 21",
"simplifiedAllowed": true
}
} Gerenderd voorbeeld
elke opstelling op zichzelf gerenderdarrangement="stacked" standaard arrangement="table" arrangement="split" arrangement="stacked" standaard arrangement="table" arrangement="split" arrangement="stacked" standaard arrangement="table" arrangement="split" Opgeloste nodes
wat een renderer krijgt — 1 nodesinvoice-type section invoice-type section invoice-type section De bestanden
wat installeren schrijft; het gekozen bestand is de componentnodes/invoice-document-type.node.tsx import { arrange, Section, type Section as SectionComponent, useDeriver, useState } from "docxcelerate/template";
import type { InvoiceDocumentTypeLines } from "../arrangements/invoice-document-type/lines.ts";
import { split } from "../arrangements/invoice-document-type/split.arrangement.tsx";
import { stacked } from "../arrangements/invoice-document-type/stacked.arrangement.tsx";
import { table } from "../arrangements/invoice-document-type/table.arrangement.tsx";
import invoiceDocumentType from "../derivers/invoice-document-type.deriver.ts";
/**
* The heading and opening lines of an invoice: what the document is allowed to
* call itself, and which dates it has to carry. Those are decisions about the
* law, not about layout.
*
* Not VAT-registered: a seller who is not registered gets "Invoice", with no
* VAT number and no VAT wording. Calling the document a VAT invoice is an
* offence in the UK. A missing registration flag is therefore read as "not
* registered": the wrong answer that way costs the customer a reclaim, while
* the other way round is unlawful.
*
* Registered: a registered seller gets "VAT invoice". It becomes "Simplified
* VAT invoice" only when the seller allows simplified invoices and the
* VAT-inclusive total is at or under the local ceiling.
*
* Pro forma: headed as one, and when VAT is in play it says "This is not a VAT
* invoice." That stops the customer reclaiming on it.
*
* Self-billed: headed as one, and carries the statement its jurisdiction
* requires.
*
* Date of supply: printed only when it falls on a different day from the issue
* date, which is the UK and EU rule. In Germany a line is always printed,
* because UStG section 14(4) no. 6 requires the Leistungsdatum even when it
* equals the invoice date.
*
* All of this is decided once, in one deriver, and the node prints what the
* deriver returns. Written as ordinary conditionals the cases come to about
* sixteen published arms, half the default `branchLimit` of 32 spent before
* the rest of the invoice, and dates from request data cannot be formatted at
* publish time without a deriver anyway.
*
* Arrangement: where the lines sit is a separate choice from what they say,
* made with the `arrangement` prop. This file decides every line; each
* arrangement, in `arrangements/invoice-document-type/`, only places them, so
* all of them print the same lines under the same conditions.
*
* Installed by `dxcl add invoice-document-type`, which also installs its deriver.
*/
/** What this component reads. Add these fields to your document data type. */
export interface InvoiceDocumentTypeData {
invoice: {
/**
* The invoice number, unique in the seller's sequence. Printed as given.
* Required: the deriver throws when it is missing or blank.
*/
number: string;
/**
* The date of issue, as an ISO 8601 date: `2026-11-30`. Anything after the
* tenth character (a time) is ignored. Required: the deriver throws when it
* is missing or does not start with YYYY-MM-DD.
*/
issueDate: string;
/**
* When the goods or services were supplied (the tax point), ISO 8601.
* Missing or empty means the same day as `issueDate`. Printed only when it
* is a different day, except in Germany, where a supply line is always
* printed. Ignored when `supplyPeriod` is given. Throws when present but
* not an ISO date.
*/
supplyDate?: string;
/**
* A supply that ran over a period (a subscription, a month of service),
* ISO 8601 at both ends. Printed whenever given, in place of `supplyDate`.
* Throws when either end is missing or not a date, or when `to` is before
* `from`.
*/
supplyPeriod?: { from: string; to: string };
/**
* What the document is. `proforma` is a request for payment, not an
* invoice. `self-billed` is issued by the customer on the supplier's
* behalf. Required: anything else throws, because a credit note headed
* "VAT invoice" is worse than no document.
*/
kind: "invoice" | "proforma" | "self-billed";
/**
* Whose rules apply. `US` has no VAT, so every VAT rule is skipped. `EU`
* means an EU member state with no rules of its own here: the Directive's
* defaults. Required: anything else throws.
*/
jurisdiction: "UK" | "EU" | "DE" | "FR" | "US";
/**
* The VAT-inclusive total, in the jurisdiction's currency (GBP for UK,
* EUR otherwise). Read only to decide whether a simplified invoice is
* allowed, and never printed. Missing or not a finite number means "not
* simplified", which is always lawful.
*/
grossTotal: number;
/** The customer's purchase order. Printed when given; omitted when missing or empty. */
purchaseOrder?: string;
};
/**
* The supplier: the party whose VAT this is. On a self-billed invoice that
* is not the party who sent the document, which is why this is `seller`
* and not the `sender` the letterhead reads.
*/
seller: {
/**
* Whether the supplier is registered for VAT. Missing means not
* registered: "Invoice", no VAT number, no VAT wording.
*/
vatRegistered: boolean;
/**
* The VAT number, printed as given (trimmed). Required when
* `vatRegistered` is true outside the US: the deriver throws rather than
* head a document "VAT invoice" without one. Ignored when not registered.
*/
vatNumber?: string;
/**
* Whether this seller issues simplified invoices at all. Missing means
* false: a full VAT invoice is always valid, a simplified one is a
* permission.
*/
simplifiedAllowed?: boolean;
};
}
/**
* Where the lines sit.
*
* - `stacked`: one line under another, each with its label. The default.
* - `table`: labels in a narrow column, values beside them, so the values
* line up down the page.
* - `split`: what identifies the invoice on the left (number, purchase order,
* VAT number), its dates ranged right, the way an invoice head often faces
* the letterhead.
*/
export type InvoiceDocumentTypeArrangement = "stacked" | "table" | "split";
/** Props for {@linkcode InvoiceDocumentType}. */
export interface InvoiceDocumentTypeProps {
/** Where the lines sit. Defaults to `"stacked"`. */
arrangement?: InvoiceDocumentTypeArrangement;
}
export const InvoiceDocumentType: SectionComponent<InvoiceDocumentTypeProps> = async ({ arrangement }) => {
const [invoice] = useState((data: InvoiceDocumentTypeData) => data.invoice);
const [seller] = useState((data: InvoiceDocumentTypeData) => data.seller);
const type = await useDeriver(invoiceDocumentType, [invoice, seller]);
const lines: InvoiceDocumentTypeLines = {
statement: type.statement,
number: invoice.number,
issueDate: type.issueDate,
supplyLine: type.supplyLine,
supplyLabel: type.supplyLabel,
supplyValue: type.supplyValue,
purchaseOrder: invoice.purchaseOrder,
vatNumber: type.vatNumber,
};
return (
<Section id="invoice-type" title={type.heading}>
{arrange(arrangement, {
stacked: () => stacked(lines),
table: () => table(lines),
split: () => split(lines),
})}
</Section>
);
}; derivers/invoice-document-type.deriver.ts import { deriver } from "docxcelerate/template";
/**
* Decides an invoice's heading, statement and dates. Installed by
* `dxcl add invoice-document-type`.
*
* It is self-contained on purpose:
* it travels to the engine as source text, so a module-level constant or helper
* would be a ReferenceError there. Thresholds are declared inside it.
*/
export default deriver({
name: "invoiceDocumentType",
run: (invoice: unknown, seller: unknown) => {
// deno-lint-ignore no-explicit-any
const inv = invoice as any;
// deno-lint-ignore no-explicit-any
const sel = seller as any;
if (typeof inv?.number !== "string" || inv.number.trim() === "") {
throw new Error("invoice.number is required");
}
if (typeof inv.issueDate !== "string" || !/^\d{4}-\d{2}-\d{2}/.test(inv.issueDate)) {
throw new Error("invoice.issueDate must be an ISO 8601 date");
}
const kinds = ["invoice", "proforma", "self-billed"];
if (!kinds.includes(inv.kind)) {
throw new Error("invoice.kind must be one of " + kinds.join(", "));
}
const jurisdictions = ["UK", "EU", "DE", "FR", "US"];
if (!jurisdictions.includes(inv.jurisdiction)) {
throw new Error("invoice.jurisdiction must be one of " + jurisdictions.join(", "));
}
const kind: string = inv.kind;
const jurisdiction: string = inv.jurisdiction;
const vatApplies = jurisdiction !== "US" && sel?.vatRegistered === true;
const vatNumber: string = typeof sel?.vatNumber === "string" ? sel.vatNumber.trim() : "";
if (vatApplies && vatNumber === "") {
throw new Error("seller.vatNumber is required when seller.vatRegistered is true");
}
// VAT-inclusive ceilings for a simplified invoice. These are the numbers an
// owner edits. UK: HMRC, 250 GBP. DE: UStDV section 33 Kleinbetragsrechnung,
// 250 EUR gross. EU and FR: Directive 2006/112/EC Art 220a (content in
// Art 226b), 100 EUR; France may differ, check it.
const limit = jurisdiction === "UK" || jurisdiction === "DE" ? 250 : 100;
const gross = inv.grossTotal;
// Rounded to 2 dp so the decision agrees with the printed total.
const simplified = kind === "invoice" && vatApplies && sel.simplifiedAllowed === true &&
typeof gross === "number" && Number.isFinite(gross) &&
Math.round(gross * 100) / 100 <= limit;
const heading = kind === "proforma"
? "Pro forma invoice"
: kind === "self-billed"
? "Self-billed invoice"
: !vatApplies
? "Invoice"
: simplified
? "Simplified VAT invoice"
: "VAT invoice";
let statement: string | null = null;
if (vatApplies && kind === "proforma") {
statement = "This is not a VAT invoice.";
} else if (vatApplies && kind === "self-billed") {
const statements: Record<string, string> = {
UK: "The VAT shown is your output tax due to HMRC.",
// UStG section 14(4) no. 10 requires the word "Gutschrift".
DE: "Gutschrift (self-billing).",
// CGI requires "Autofacturation".
FR: "Autofacturation (self-billing).",
// Directive Art 226(10a).
EU: "Self-billing.",
};
statement = statements[jurisdiction] ?? null;
}
const day = (value: unknown, field: string): string | null => {
if (value === undefined || value === null || value === "") return null;
if (typeof value !== "string" || !/^\d{4}-\d{2}-\d{2}/.test(value)) {
throw new Error(field + " must be an ISO 8601 date");
}
return value.slice(0, 10);
};
// UTC, so the date does not shift by a day on the engine.
const formatter = new Intl.DateTimeFormat(jurisdiction === "US" ? "en-US" : "en-GB", {
dateStyle: "long",
timeZone: "UTC",
});
const fmt = (d: string) => formatter.format(new Date(d + "T00:00:00Z"));
const issueDay = day(inv.issueDate, "invoice.issueDate") as string;
const de = jurisdiction === "DE";
// Label and value apart, for an arrangement that sets them in two columns;
// the line is the two joined, for one that runs them down the page.
let supplyLabel: string | null = null;
let supplyValue: string | null = null;
let supplyLine: string | null = null;
if (inv.supplyPeriod !== undefined && inv.supplyPeriod !== null) {
const from = day(inv.supplyPeriod.from, "invoice.supplyPeriod.from");
const to = day(inv.supplyPeriod.to, "invoice.supplyPeriod.to");
if (from === null) throw new Error("invoice.supplyPeriod.from is required");
if (to === null) throw new Error("invoice.supplyPeriod.to is required");
if (to < from) throw new Error("invoice.supplyPeriod.to is before invoice.supplyPeriod.from");
supplyLabel = de ? "Leistungszeitraum" : "Supply period";
supplyValue = fmt(from) + (de ? " bis " : " to ") + fmt(to);
} else {
const supplyDay = day(inv.supplyDate, "invoice.supplyDate");
if (supplyDay !== null && supplyDay !== issueDay) {
supplyLabel = de ? "Leistungsdatum" : "Date of supply";
supplyValue = fmt(supplyDay);
} else if (de) {
supplyLabel = "Leistungsdatum";
supplyValue = "entspricht Rechnungsdatum";
supplyLine = "Leistungsdatum entspricht Rechnungsdatum.";
}
}
if (supplyLine === null && supplyLabel !== null) {
supplyLine = supplyLabel + ": " + supplyValue;
}
return {
heading,
statement,
issueDate: fmt(issueDay),
supplyLine,
supplyLabel,
supplyValue,
vatNumber: vatApplies ? vatNumber : null,
};
},
}); arrangements/invoice-document-type/lines.ts /**
* What the invoice document type hands each of its arrangements: every line
* it may print, already decided. An arrangement places these and decides
* nothing, so all three print the same lines under the same conditions.
*
* A field that is `null` (or a missing purchase order) is a line this invoice
* does not carry. Test it in the JSX, `{lines.supplyLine && …}`, so the test
* is compiled into a condition the engine settles per document.
*
* Installed by `dxcl add invoice-document-type`, with the arrangements.
*/
export interface InvoiceDocumentTypeLines {
/** "This is not a VAT invoice.", a self-billing statement, or null. */
statement: string | null;
/** The invoice number, as given. */
number: string;
/** The date of issue, formatted for the jurisdiction. */
issueDate: string;
/** The supply date or period as one line, or null when none is printed. */
supplyLine: string | null;
/** The same, its label alone: "Date of supply", "Leistungszeitraum". */
supplyLabel: string | null;
/** The same, its value alone. */
supplyValue: string | null;
/** The customer's purchase order, when there is one. */
purchaseOrder?: string;
/** The seller's VAT number, or null when no VAT applies. */
vatNumber: string | null;
} arrangements/invoice-document-type/stacked.arrangement.tsx import { Paragraph, type Yield } from "docxcelerate/template";
import type { InvoiceDocumentTypeLines } from "./lines.ts";
/**
* One labelled line under another, down the text column. The default: it
* reads in any width, and nothing has to line up with anything.
*/
export const stacked = (lines: InvoiceDocumentTypeLines): Yield => (
<>
{lines.statement && <Paragraph id="invoice-type-statement">{lines.statement}</Paragraph>}
<Paragraph id="invoice-type-number">Invoice number: {lines.number}</Paragraph>
<Paragraph id="invoice-type-issue-date">Date of issue: {lines.issueDate}</Paragraph>
{lines.supplyLine && <Paragraph id="invoice-type-supply">{lines.supplyLine}</Paragraph>}
{lines.purchaseOrder && (
<Paragraph id="invoice-type-purchase-order">Purchase order: {lines.purchaseOrder}</Paragraph>
)}
{lines.vatNumber && <Paragraph id="invoice-type-vat-number">VAT number: {lines.vatNumber}</Paragraph>}
</>
); arrangements/invoice-document-type/table.arrangement.tsx import { Cell, Paragraph, Row, Table, type Yield } from "docxcelerate/template";
import type { InvoiceDocumentTypeLines } from "./lines.ts";
/**
* Labels in a narrow column with the values beside them, so the values line
* up down the page and a reader scanning for the number finds it in one place.
*/
export const table = (lines: InvoiceDocumentTypeLines): Yield => (
<>
{lines.statement && <Paragraph id="invoice-type-statement">{lines.statement}</Paragraph>}
{/* 45mm holds the longest label, "Leistungszeitraum", on one line. */}
<Table id="invoice-type-details" columns={[{ width: 45 }, { width: "auto" }]}>
<Row id="invoice-type-number">
<Cell>Invoice number</Cell>
<Cell>{lines.number}</Cell>
</Row>
<Row id="invoice-type-issue-date">
<Cell>Date of issue</Cell>
<Cell>{lines.issueDate}</Cell>
</Row>
{lines.supplyLabel && (
<Row id="invoice-type-supply">
<Cell>{lines.supplyLabel}</Cell>
<Cell>{lines.supplyValue}</Cell>
</Row>
)}
{lines.purchaseOrder && (
<Row id="invoice-type-purchase-order">
<Cell>Purchase order</Cell>
<Cell>{lines.purchaseOrder}</Cell>
</Row>
)}
{lines.vatNumber && (
<Row id="invoice-type-vat-number">
<Cell>VAT number</Cell>
<Cell>{lines.vatNumber}</Cell>
</Row>
)}
</Table>
</>
); arrangements/invoice-document-type/split.arrangement.tsx import { Cell, Paragraph, Row, Table, type Yield } from "docxcelerate/template";
import type { InvoiceDocumentTypeLines } from "./lines.ts";
/**
* What identifies the invoice on the left (number, purchase order, VAT
* number) and its dates ranged right, the way an invoice head often faces
* the letterhead.
*/
export const split = (lines: InvoiceDocumentTypeLines): Yield => (
<>
{lines.statement && <Paragraph id="invoice-type-statement">{lines.statement}</Paragraph>}
{/* The dates get what their longest line needs; the identifiers, which
run long (a PO with a cost centre on it), get the rest. */}
<Table id="invoice-type-details" columns={[{ width: "auto" }, { width: 70, align: "right" }]}>
<Row id="invoice-type-details-row">
<Cell>
<Paragraph id="invoice-type-number">Invoice number: {lines.number}</Paragraph>
{lines.purchaseOrder && (
<Paragraph id="invoice-type-purchase-order">Purchase order: {lines.purchaseOrder}</Paragraph>
)}
{lines.vatNumber && <Paragraph id="invoice-type-vat-number">VAT number: {lines.vatNumber}</Paragraph>}
</Cell>
<Cell>
<Paragraph id="invoice-type-issue-date">Date of issue: {lines.issueDate}</Paragraph>
{lines.supplyLine && <Paragraph id="invoice-type-supply">{lines.supplyLine}</Paragraph>}
</Cell>
</Row>
</Table>
</>
);