Preferential origin statement
LegalPrints the statement on origin each agreement prescribes, in its own words: the EU-UK TCA statement on origin, the EU GSP REX statement, the CETA origin declaration, the EU-Japan statement and the UK DCTS statement, with the exporter reference, the origin, the origin criteria that agreement uses and the multiple-shipment period. The EUR 6,000 threshold is tested against the originating lines only, converted at the rate you supply, never against the invoice total or a fixed sterling figure. Above it the exporter's REX or EORI number is required, and when it is missing a warning prints in place of the statement, because an importer who claims preference on an incomplete statement faces a duty demand later. Non-originating lines are listed by reference, so "except where otherwise clearly indicated" is true. A period longer than 12 months is refused the same way. Nothing prints when no line is originating. Worked out by one deriver.
npx dxcl add preferential-origin-statement Voorbeelddata
de JSON waarmee dit voorbeeld is gebouwd{
"origin": {
"agreement": "eu-uk-tca",
"originCountry": "UK",
"exporterReference": "GB123456789000",
"eurRate": 1.1712,
"period": {
"from": "2026-12-01",
"to": "2027-11-30"
},
"place": "Newcastle upon Tyne, Tyne and Wear",
"date": "2026-11-30",
"exporterName": "Northumbrian Precision Engineering and Fabrication Holdings Limited"
},
"consignment": {
"currency": "GBP",
"lines": [
{
"ref": "10",
"valueMinor": 128450000,
"originating": true,
"criterion": "A"
},
{
"ref": "20",
"valueMinor": 96320075,
"originating": true,
"criterion": "A"
},
{
"ref": "30",
"valueMinor": 18999900,
"originating": false
},
{
"ref": "40",
"valueMinor": 254000000,
"originating": true,
"criterion": "C, D"
},
{
"ref": "50",
"valueMinor": 7250050,
"originating": true,
"criterion": "E"
},
{
"ref": "60",
"valueMinor": 4410000,
"originating": false
}
]
}
} Gerenderd voorbeeld
de component op zichzelf gerenderdOpgeloste nodes
wat een renderer krijgt — 1 nodesorigin-statement section De bestanden
wat installeren schrijft; het gekozen bestand is de componentnodes/preferential-origin-statement.node.tsx import { type Nodes, Paragraph, Section, useDeriver, useState } from "docxcelerate/template";
import preferentialOriginStatement from "../derivers/preferential-origin-statement.deriver.ts";
/**
* The statement on origin a trade agreement prescribes, printed on an invoice
* so the importer can claim preferential duty, or a warning in its place when
* it cannot be made truthfully. Nothing at all when no line is originating, so
* it can sit on every export invoice.
*
* Decisions already made for you:
*
* - The statement is either true and complete, or it is not printed. A
* statement the importer relies on and that turns out to be incomplete costs
* them a post-clearance duty demand, which is worse than no statement.
* - The EUR 6,000 threshold is tested on the originating lines only, converted
* at the rate you supply. It is never tested against the invoice total, and
* never against a fixed sterling figure: the law is in euro.
* - Above the threshold the exporter's reference (REX, or EORI for a UK
* exporter) is mandatory. Without it the warning prints instead.
* - Every non-originating line is listed by its reference, because the
* statement says "except where otherwise clearly indicated". Without that
* list it would cover every line on the document.
* - A multiple-shipment period over 12 months is refused the same way.
* - Each agreement's wording is printed exactly, in the prescribed order: the
* period, the sentence, the exceptions, the criteria, then place and date
* over the exporter's name. There are no arrangements, because moving any of
* that would separate the exceptions from the sentence they qualify.
*
* All of it is decided in one deriver, because it sums, filters, joins and
* formats dates on request data, and publishing refuses all of those. The node
* computes nothing: it reads the deriver's result and branches on it, with
* every test written inline so a publish build keeps each arm.
*
* Not checked: whether the goods originate, which agreement applies, or that a
* REX or EORI number is well formed. Presence is the only test.
*
* Installed by `dxcl add preferential-origin-statement`.
*/
/** What this component reads. Add these fields to your document data type. */
export interface PreferentialOriginData {
origin: {
/**
* The agreement the goods are claimed under. It picks the wording, the
* reference label and the origin criterion codes. Required: anything else
* throws, because a statement in the wrong agreement's words is invalid
* for all of it.
*/
agreement: "eu-uk-tca" | "eu-gsp" | "ceta" | "eu-japan" | "uk-dcts";
/**
* Where the products originate, printed into "these products are of …
* preferential origin". `eu-uk-tca` takes "UK" or "EU". `ceta` and
* `eu-japan` take "EU" only. `eu-gsp` and `uk-dcts` take the beneficiary
* country as it should be printed ("Bangladesh"), never "EU" or "UK".
* Compared exactly after trimming. Missing, or not allowed for the
* agreement, throws.
*/
originCountry: "UK" | "EU" | string;
/**
* The exporter's reference, printed as given (trimmed). That is the EORI
* for a UK exporter under the TCA, and the REX number for an EU exporter
* or a GSP/DCTS exporter. Printed whenever given. Missing or blank is
* allowed while the originating products come to EUR 6,000 or less: the
* reference clause is then left out. Above that, missing means the
* statement is withheld and a warning prints. Its format is not checked.
*/
exporterReference?: string;
/**
* How many euros one unit of `consignment.currency` is worth (for GBP,
* about 1.17). Used only to test the EUR 6,000 threshold; never printed
* except inside the warning. Ignored when the currency is EUR. Otherwise
* required: missing, zero, negative or not finite throws. Which rate the
* agreement wants is the caller's decision.
*/
eurRate?: number;
/**
* For a statement covering multiple shipments of identical products:
* ISO 8601 dates at both ends, both inclusive. It prints as the "Period"
* line. Missing means a single shipment, and no period line prints.
* Either end missing or malformed throws, and so does `to` before
* `from`. A period longer than 12 months withholds the statement.
*/
period?: { from: string; to: string };
/** Where the statement is made, printed as given. Required; missing or blank throws. */
place: string;
/** When it is made, ISO 8601 (`2026-11-30`). Required; missing or not YYYY-MM-DD throws. */
date: string;
/** The exporter's name, printed as given. Required; missing or blank throws. */
exporterName: string;
};
consignment: {
/**
* ISO 4217 code of `valueMinor`, three upper-case letters. It sets the
* minor units. Required: missing or malformed throws.
*/
currency: string;
/**
* Every line of the document, in document order. Missing or empty prints
* nothing.
*/
lines: Array<{
/**
* The line's reference as the document shows it ("3", "SKU-1182"). It
* is how exceptions and criteria point at the line. Required and
* unique: blank or duplicated throws.
*/
ref: string;
/** The line's value in integer minor units. Missing, negative or not an integer throws. */
valueMinor: number;
/**
* Whether the line is claimed as originating. Anything but `true`
* (including missing) means not originating: a preference claim has
* to be explicit.
*/
originating?: boolean;
/**
* The origin criterion for an originating line. `eu-uk-tca` and
* `eu-japan` take one or more of A–E. `eu-gsp` and `uk-dcts` take P
* (wholly obtained) or W (sufficiently worked). `ceta` ignores it.
* Missing or invalid on an originating line, where the agreement
* uses criteria, withholds the statement. Ignored on lines that are
* not originating.
*/
criterion?: string;
/**
* The four-digit HS heading for a GSP/DCTS "W" line ("6109"). Required
* there: missing or not four digits withholds the statement. Ignored
* everywhere else.
*/
hsHeading?: string;
}>;
};
}
/**
* Prints the statement on origin the chosen agreement prescribes, or a warning
* saying why it cannot be made. Prints nothing when no line is originating.
*/
export const PreferentialOriginStatement: Nodes = async () => {
const [{ origin, consignment }] = useState((data: PreferentialOriginData) => ({
origin: data.origin,
consignment: data.consignment,
}));
const s = await useDeriver(preferentialOriginStatement, [origin, consignment]);
return (
<>
{s.outcome === "statement" && (
<Section id="origin-statement" title={s.title}>
{s.period && <Paragraph id="origin-statement-period">{s.period}</Paragraph>}
<Paragraph id="origin-statement-declaration">{s.declaration}</Paragraph>
{s.exceptions && <Paragraph id="origin-statement-exceptions">{s.exceptions}</Paragraph>}
{s.criteriaLine && <Paragraph id="origin-statement-criteria">{s.criteriaLine}</Paragraph>}
<Paragraph id="origin-statement-place-date">{s.placeAndDate}</Paragraph>
<Paragraph id="origin-statement-exporter">{s.exporterName}</Paragraph>
</Section>
)}
{s.outcome === "withheld" && (
<Section id="origin-statement-withheld" title={s.title}>
<Paragraph id="origin-statement-withheld-warning">{s.warning}</Paragraph>
<Paragraph id="origin-statement-withheld-place-date">{s.placeAndDate}</Paragraph>
<Paragraph id="origin-statement-withheld-exporter">{s.exporterName}</Paragraph>
</Section>
)}
</>
);
}; derivers/preferential-origin-statement.deriver.ts import { deriver } from "docxcelerate/template";
/**
* Decides whether a preferential origin statement can be made, and if so what
* it says. Installed by `dxcl add preferential-origin-statement`.
*
* 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. The
* wording, the threshold and every helper are declared inside `run`.
*
* Wording sources, to be checked against the text in force before relying on it:
* EU-UK TCA, Annex ORIG-4; EU-Japan EPA, Annex 3-D; CETA, Protocol on rules of
* origin, Annex 2 (the bracket label is the likeliest to differ); EU GSP,
* Commission Implementing Regulation (EU) 2015/2447, Annex 22-07; UK DCTS, the
* HMRC DCTS origin reference document.
*/
export default deriver({
name: "preferentialOriginStatement",
run: (origin: unknown, consignment: unknown) => {
// deno-lint-ignore no-explicit-any
const o = origin as any;
// deno-lint-ignore no-explicit-any
const c = consignment as any;
if (typeof o !== "object" || o === null) throw new Error("origin is required");
const agreements = ["eu-uk-tca", "eu-gsp", "ceta", "eu-japan", "uk-dcts"];
if (!agreements.includes(o.agreement)) {
throw new Error("origin.agreement must be one of " + agreements.join(", "));
}
const agreement: string = o.agreement;
const originCountry = typeof o.originCountry === "string" ? o.originCountry.trim() : "";
const countryOk = agreement === "eu-uk-tca"
? originCountry === "UK" || originCountry === "EU"
: agreement === "ceta" || agreement === "eu-japan"
? originCountry === "EU"
: originCountry !== "" && originCountry !== "EU" && originCountry !== "UK";
if (!countryOk) {
throw new Error("origin.originCountry is not allowed for " + agreement + ": " + JSON.stringify(o.originCountry));
}
const blank = (v: unknown) => typeof v !== "string" || v.trim() === "";
if (blank(o.place)) throw new Error("origin.place is required");
if (blank(o.exporterName)) throw new Error("origin.exporterName is required");
if (typeof o.date !== "string" || !/^\d{4}-\d{2}-\d{2}/.test(o.date)) {
throw new Error("origin.date must be an ISO 8601 date");
}
const date = o.date.slice(0, 10);
const currency = c?.currency;
if (typeof currency !== "string" || !/^[A-Z]{3}$/.test(currency)) {
throw new Error("consignment.currency must be three upper-case letters");
}
if (currency !== "EUR" && !(typeof o.eurRate === "number" && Number.isFinite(o.eurRate) && o.eurRate > 0)) {
throw new Error("origin.eurRate must be a number above zero when consignment.currency is not EUR");
}
const given: unknown = c.lines;
if (given !== undefined && given !== null && !Array.isArray(given)) {
throw new Error("consignment.lines must be an array");
}
// deno-lint-ignore no-explicit-any
const lines: any[] = (given as any[] | undefined | null) ?? [];
const seen = new Set<string>();
lines.forEach((l, i) => {
const at = "consignment.lines[" + i + "]";
if (blank(l?.ref)) throw new Error(at + ".ref is required");
const key = l.ref.trim();
if (seen.has(key)) throw new Error(at + ".ref " + JSON.stringify(key) + " is used by another line");
seen.add(key);
if (!Number.isInteger(l.valueMinor) || l.valueMinor < 0) {
throw new Error(at + ".valueMinor must be a non-negative integer");
}
});
const day = (value: unknown, field: string): string => {
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);
};
let from = "";
let to = "";
if (o.period !== undefined && o.period !== null) {
from = day(o.period.from, "origin.period.from");
to = day(o.period.to, "origin.period.to");
if (to < from) throw new Error("origin.period.to is before origin.period.from");
}
// UTC, so the date does not shift by a day on the engine.
const formatter = new Intl.DateTimeFormat("en-GB", { dateStyle: "long", timeZone: "UTC" });
const fmt = (d: string) => formatter.format(new Date(d + "T00:00:00Z"));
const refOf = (l: { ref: string }) => l.ref.trim();
const originating = lines.filter((l) => l.originating === true);
const others = lines.filter((l) => l.originating !== true);
const title = agreement === "ceta" ? "Origin declaration" : "Statement on origin";
const exporterName: string = o.exporterName.trim();
const placeAndDate = o.place.trim() + ", " + fmt(date);
const originatingMinor: number = originating.reduce((sum, l) => sum + l.valueMinor, 0);
const digits = new Intl.NumberFormat("en", { style: "currency", currency }).resolvedOptions()
.maximumFractionDigits as number;
const rate = currency === "EUR" ? 1 : (o.eurRate as number);
// Rounded once, here, and nowhere else.
const originatingEurCents = Math.round(originatingMinor * rate * 100 / 10 ** digits);
const result = {
outcome: "none" as "none" | "statement" | "withheld",
title: "",
period: null as string | null,
declaration: "",
criteriaLine: null as string | null,
exceptions: null as string | null,
placeAndDate: "",
exporterName: "",
warning: "",
originatingMinor,
originatingEurCents,
};
if (originating.length === 0) return result;
const withheld = (warning: string) => ({
...result,
outcome: "withheld" as const,
title: title + ": not made",
placeAndDate,
exporterName,
warning: "The " + title + " cannot be made: " + warning,
});
// 1. A period over 12 months. The last allowed day is one day short of a
// year on; Date.UTC rolls over month ends, so 2024-02-29 ends 2025-02-28.
if (from !== "") {
const last = Date.UTC(+from.slice(0, 4) + 1, +from.slice(5, 7) - 1, +from.slice(8, 10) - 1);
if (Date.UTC(+to.slice(0, 4), +to.slice(5, 7) - 1, +to.slice(8, 10)) > last) {
return withheld(
"the period from " + from + " to " + to + " is longer than the 12 months a statement for multiple " +
"shipments may cover. Shorten the period and reissue this document.",
);
}
}
// 2. Origin criteria, where the agreement uses them.
const agreementName: Record<string, string> = {
"eu-uk-tca": "the EU-UK Trade and Cooperation Agreement",
"eu-japan": "the EU-Japan Economic Partnership Agreement",
"eu-gsp": "the EU Generalised System of Preferences",
"uk-dcts": "the UK Developing Countries Trading Scheme",
};
const letters = agreement === "eu-uk-tca" || agreement === "eu-japan";
const codes = letters ? "A to E" : "P or W";
const groups = new Map<string, string[]>();
if (agreement !== "ceta") {
for (const l of originating) {
const raw: unknown = l.criterion;
const text = typeof raw === "string" ? raw : "";
let code = "";
if (letters) {
const stripped = text.toUpperCase().replace(/[\s,]/g, "");
if (/^[A-E]+$/.test(stripped)) code = [...new Set(stripped)].sort().join(", ");
} else {
const p = text.trim().toUpperCase();
if (p === "P") code = "P";
if (p === "W") {
const heading = typeof l.hsHeading === "string" ? l.hsHeading.trim() : "";
if (!/^\d{4}$/.test(heading)) {
return withheld(
"line " + refOf(l) + " has origin criterion W but no four-digit HS heading. " +
"Add the heading and reissue this document.",
);
}
code = "W " + heading;
}
}
if (code === "") {
return withheld(
text.trim() === ""
? "line " + refOf(l) + " is claimed as originating but has no origin criterion. Add one of " +
codes + " and reissue this document."
: "line " + refOf(l) + " has the origin criterion " + JSON.stringify(text) + ", which " +
agreementName[agreement] + " does not use (" + codes + "). Correct it and reissue this document.",
);
}
groups.set(code, [...(groups.get(code) ?? []), refOf(l)]);
}
}
// 3. The exporter reference. The EUR 6,000 threshold is tested on the
// originating lines only. It is the same under the EU-UK TCA, EU-Japan,
// CETA and EU GSP; UK DCTS is the one to check against the DCTS origin
// reference document.
const threshold = 600000;
const reference = typeof o.exporterReference === "string" ? o.exporterReference.trim() : "";
if (originatingEurCents > threshold && reference === "") {
const kind = agreement === "eu-uk-tca" && originCountry === "UK" ? "EORI number" : "REX number";
const eur = new Intl.NumberFormat("en-GB", { style: "currency", currency: "EUR" }).format(
originatingEurCents / 100,
);
return withheld(
"the originating products in this consignment come to " + eur + ", more than €6,000, so the " +
"statement must carry the exporter's " + kind + ". Add it and reissue this document.",
);
}
const plural = (refs: string[]) => (refs.length === 1 ? "line " : "lines ") + refs.join(", ");
const entries = [...groups.entries()];
const criteria = entries.length === 1
? entries[0][0]
: entries.map(([code, refs]) => code + " (" + plural(refs) + ")").join("; ");
const label = agreement === "ceta"
? "customs or competent governmental authorisation No"
: agreement === "eu-gsp" || agreement === "uk-dcts"
? "Number of Registered Exporter"
: "Exporter Reference No";
const refClause = reference ? " (" + label + " " + reference + ")" : "";
const claim = "declares that, except where otherwise clearly indicated, these products are of " +
originCountry + " preferential origin";
const declaration = agreement === "eu-gsp" || agreement === "uk-dcts"
? "The exporter " + exporterName + refClause + " of the products covered by this document " +
claim + " according to rules of origin of the " +
(agreement === "eu-gsp"
? "Generalised System of Preferences of the European Union"
: "Developing Countries Trading Scheme of the United Kingdom") +
" and that the origin criterion met is " + criteria + "."
: "The exporter of the products covered by this document" + refClause + " " + claim + ".";
return {
...result,
outcome: "statement" as const,
title,
period: from === "" ? null : "Period: from " + fmt(from) + " to " + fmt(to),
declaration,
criteriaLine: letters ? "Origin criteria: " + criteria : null,
exceptions: others.length === 0
? null
: "Not of " + originCountry + " preferential origin: " + plural(others.map(refOf)) + ".",
placeAndDate,
exporterName,
};
},
});