Incoterm charges and customs value
BodyStates the Incoterms 2020 rule with its named place and itemises every cost leg, from export packing to import VAT, by name and amount. Each leg is charged, included in the price or for the buyer's account, as the rule decides, and a buyer's leg never enters the total. From the same split it gives the customs value on the destination's basis: CIF to the place of entry for the EU and UK, with air freight apportioned by the Annex 23-01 percentage, or FOB at the place of export for the US, Canada and Australia, with freight and insurance itemised as 19 CFR 141.86(a)(8) requires. An EXW price gains the buyer's costs to the border, a CIF price loses its freight, and a DDP price gives back its duty. Figures are integer minor units, rounded once. It warns when a sea-only rule is used for air, container or land freight, when the named place is missing, when DDP has the seller paying import VAT, and when a CFR or CPT invoice shows insurance. All of it is one deriver.
npx dxcl add incoterm-charges-and-customs-value shipment.incoterm Данные предпросмотра
JSON, на котором построен этот предпросмотр{
"shipment": {
"incoterm": "EXW",
"namedPlace": "Unit 14, Maasvlakte Distribution Park, Europaweg 875, 3199 LD Rotterdam, Netherlands",
"mode": "sea-container",
"currency": "EUR",
"goodsMinor": 128475000,
"legs": [
{
"kind": "export-packing",
"amountMinor": 184500,
"inPrice": true
},
{
"kind": "origin-inland",
"amountMinor": 386000
},
{
"kind": "export-clearance",
"amountMinor": 42500
},
{
"kind": "origin-terminal",
"amountMinor": 61800
},
{
"kind": "main-carriage",
"amountMinor": 1874000
},
{
"kind": "insurance",
"amountMinor": 154200
},
{
"kind": "destination-terminal",
"amountMinor": 58700
},
{
"kind": "onward-delivery",
"amountMinor": 212500
},
{
"kind": "import-duty",
"amountMinor": 5396000
},
{
"kind": "import-tax",
"amountMinor": 26791400
}
],
"destination": {
"valuationBasis": "cif-border"
}
}
} {
"shipment": {
"incoterm": "FCA",
"namedPlace": "Unit 14, Maasvlakte Distribution Park, Europaweg 875, 3199 LD Rotterdam, Netherlands",
"mode": "sea-container",
"currency": "EUR",
"goodsMinor": 128475000,
"legs": [
{
"kind": "export-packing",
"amountMinor": 184500,
"inPrice": true
},
{
"kind": "origin-inland",
"amountMinor": 386000
},
{
"kind": "export-clearance",
"amountMinor": 42500
},
{
"kind": "origin-terminal",
"amountMinor": 61800
},
{
"kind": "main-carriage",
"amountMinor": 1874000
},
{
"kind": "insurance",
"amountMinor": 154200
},
{
"kind": "destination-terminal",
"amountMinor": 58700
},
{
"kind": "onward-delivery",
"amountMinor": 212500
},
{
"kind": "import-duty",
"amountMinor": 5396000
},
{
"kind": "import-tax",
"amountMinor": 26791400
}
],
"destination": {
"valuationBasis": "cif-border"
}
}
} {
"shipment": {
"incoterm": "FAS",
"namedPlace": "Unit 14, Maasvlakte Distribution Park, Europaweg 875, 3199 LD Rotterdam, Netherlands",
"mode": "sea-container",
"currency": "EUR",
"goodsMinor": 128475000,
"legs": [
{
"kind": "export-packing",
"amountMinor": 184500,
"inPrice": true
},
{
"kind": "origin-inland",
"amountMinor": 386000
},
{
"kind": "export-clearance",
"amountMinor": 42500
},
{
"kind": "origin-terminal",
"amountMinor": 61800
},
{
"kind": "main-carriage",
"amountMinor": 1874000
},
{
"kind": "insurance",
"amountMinor": 154200
},
{
"kind": "destination-terminal",
"amountMinor": 58700
},
{
"kind": "onward-delivery",
"amountMinor": 212500
},
{
"kind": "import-duty",
"amountMinor": 5396000
},
{
"kind": "import-tax",
"amountMinor": 26791400
}
],
"destination": {
"valuationBasis": "cif-border"
}
}
} {
"shipment": {
"incoterm": "FOB",
"namedPlace": "Unit 14, Maasvlakte Distribution Park, Europaweg 875, 3199 LD Rotterdam, Netherlands",
"mode": "sea-container",
"currency": "EUR",
"goodsMinor": 128475000,
"legs": [
{
"kind": "export-packing",
"amountMinor": 184500,
"inPrice": true
},
{
"kind": "origin-inland",
"amountMinor": 386000
},
{
"kind": "export-clearance",
"amountMinor": 42500
},
{
"kind": "origin-terminal",
"amountMinor": 61800
},
{
"kind": "main-carriage",
"amountMinor": 1874000
},
{
"kind": "insurance",
"amountMinor": 154200
},
{
"kind": "destination-terminal",
"amountMinor": 58700
},
{
"kind": "onward-delivery",
"amountMinor": 212500
},
{
"kind": "import-duty",
"amountMinor": 5396000
},
{
"kind": "import-tax",
"amountMinor": 26791400
}
],
"destination": {
"valuationBasis": "cif-border"
}
}
} {
"shipment": {
"incoterm": "CFR",
"namedPlace": "Unit 14, Maasvlakte Distribution Park, Europaweg 875, 3199 LD Rotterdam, Netherlands",
"mode": "sea-container",
"currency": "EUR",
"goodsMinor": 128475000,
"legs": [
{
"kind": "export-packing",
"amountMinor": 184500,
"inPrice": true
},
{
"kind": "origin-inland",
"amountMinor": 386000
},
{
"kind": "export-clearance",
"amountMinor": 42500
},
{
"kind": "origin-terminal",
"amountMinor": 61800
},
{
"kind": "main-carriage",
"amountMinor": 1874000
},
{
"kind": "insurance",
"amountMinor": 154200
},
{
"kind": "destination-terminal",
"amountMinor": 58700
},
{
"kind": "onward-delivery",
"amountMinor": 212500
},
{
"kind": "import-duty",
"amountMinor": 5396000
},
{
"kind": "import-tax",
"amountMinor": 26791400
}
],
"destination": {
"valuationBasis": "cif-border"
}
}
} {
"shipment": {
"incoterm": "CIF",
"namedPlace": "Unit 14, Maasvlakte Distribution Park, Europaweg 875, 3199 LD Rotterdam, Netherlands",
"mode": "sea-container",
"currency": "EUR",
"goodsMinor": 128475000,
"legs": [
{
"kind": "export-packing",
"amountMinor": 184500,
"inPrice": true
},
{
"kind": "origin-inland",
"amountMinor": 386000
},
{
"kind": "export-clearance",
"amountMinor": 42500
},
{
"kind": "origin-terminal",
"amountMinor": 61800
},
{
"kind": "main-carriage",
"amountMinor": 1874000
},
{
"kind": "insurance",
"amountMinor": 154200
},
{
"kind": "destination-terminal",
"amountMinor": 58700
},
{
"kind": "onward-delivery",
"amountMinor": 212500
},
{
"kind": "import-duty",
"amountMinor": 5396000
},
{
"kind": "import-tax",
"amountMinor": 26791400
}
],
"destination": {
"valuationBasis": "cif-border"
}
}
} {
"shipment": {
"incoterm": "CPT",
"namedPlace": "Unit 14, Maasvlakte Distribution Park, Europaweg 875, 3199 LD Rotterdam, Netherlands",
"mode": "sea-container",
"currency": "EUR",
"goodsMinor": 128475000,
"legs": [
{
"kind": "export-packing",
"amountMinor": 184500,
"inPrice": true
},
{
"kind": "origin-inland",
"amountMinor": 386000
},
{
"kind": "export-clearance",
"amountMinor": 42500
},
{
"kind": "origin-terminal",
"amountMinor": 61800
},
{
"kind": "main-carriage",
"amountMinor": 1874000
},
{
"kind": "insurance",
"amountMinor": 154200
},
{
"kind": "destination-terminal",
"amountMinor": 58700
},
{
"kind": "onward-delivery",
"amountMinor": 212500
},
{
"kind": "import-duty",
"amountMinor": 5396000
},
{
"kind": "import-tax",
"amountMinor": 26791400
}
],
"destination": {
"valuationBasis": "cif-border"
}
}
} {
"shipment": {
"incoterm": "CIP",
"namedPlace": "Unit 14, Maasvlakte Distribution Park, Europaweg 875, 3199 LD Rotterdam, Netherlands",
"mode": "sea-container",
"currency": "EUR",
"goodsMinor": 128475000,
"legs": [
{
"kind": "export-packing",
"amountMinor": 184500,
"inPrice": true
},
{
"kind": "origin-inland",
"amountMinor": 386000
},
{
"kind": "export-clearance",
"amountMinor": 42500
},
{
"kind": "origin-terminal",
"amountMinor": 61800
},
{
"kind": "main-carriage",
"amountMinor": 1874000
},
{
"kind": "insurance",
"amountMinor": 154200
},
{
"kind": "destination-terminal",
"amountMinor": 58700
},
{
"kind": "onward-delivery",
"amountMinor": 212500
},
{
"kind": "import-duty",
"amountMinor": 5396000
},
{
"kind": "import-tax",
"amountMinor": 26791400
}
],
"destination": {
"valuationBasis": "cif-border"
}
}
} {
"shipment": {
"incoterm": "DAP",
"namedPlace": "Unit 14, Maasvlakte Distribution Park, Europaweg 875, 3199 LD Rotterdam, Netherlands",
"mode": "sea-container",
"currency": "EUR",
"goodsMinor": 128475000,
"legs": [
{
"kind": "export-packing",
"amountMinor": 184500,
"inPrice": true
},
{
"kind": "origin-inland",
"amountMinor": 386000
},
{
"kind": "export-clearance",
"amountMinor": 42500
},
{
"kind": "origin-terminal",
"amountMinor": 61800
},
{
"kind": "main-carriage",
"amountMinor": 1874000
},
{
"kind": "insurance",
"amountMinor": 154200
},
{
"kind": "destination-terminal",
"amountMinor": 58700
},
{
"kind": "onward-delivery",
"amountMinor": 212500
},
{
"kind": "import-duty",
"amountMinor": 5396000
},
{
"kind": "import-tax",
"amountMinor": 26791400
}
],
"destination": {
"valuationBasis": "cif-border"
}
}
} {
"shipment": {
"incoterm": "DPU",
"namedPlace": "Unit 14, Maasvlakte Distribution Park, Europaweg 875, 3199 LD Rotterdam, Netherlands",
"mode": "sea-container",
"currency": "EUR",
"goodsMinor": 128475000,
"legs": [
{
"kind": "export-packing",
"amountMinor": 184500,
"inPrice": true
},
{
"kind": "origin-inland",
"amountMinor": 386000
},
{
"kind": "export-clearance",
"amountMinor": 42500
},
{
"kind": "origin-terminal",
"amountMinor": 61800
},
{
"kind": "main-carriage",
"amountMinor": 1874000
},
{
"kind": "insurance",
"amountMinor": 154200
},
{
"kind": "destination-terminal",
"amountMinor": 58700
},
{
"kind": "onward-delivery",
"amountMinor": 212500
},
{
"kind": "import-duty",
"amountMinor": 5396000
},
{
"kind": "import-tax",
"amountMinor": 26791400
}
],
"destination": {
"valuationBasis": "cif-border"
}
}
} {
"shipment": {
"incoterm": "DDP",
"namedPlace": "Unit 14, Maasvlakte Distribution Park, Europaweg 875, 3199 LD Rotterdam, Netherlands",
"mode": "sea-container",
"currency": "EUR",
"goodsMinor": 128475000,
"legs": [
{
"kind": "export-packing",
"amountMinor": 184500,
"inPrice": true
},
{
"kind": "origin-inland",
"amountMinor": 386000
},
{
"kind": "export-clearance",
"amountMinor": 42500
},
{
"kind": "origin-terminal",
"amountMinor": 61800
},
{
"kind": "main-carriage",
"amountMinor": 1874000
},
{
"kind": "insurance",
"amountMinor": 154200
},
{
"kind": "destination-terminal",
"amountMinor": 58700
},
{
"kind": "onward-delivery",
"amountMinor": 212500
},
{
"kind": "import-duty",
"amountMinor": 5396000
},
{
"kind": "import-tax",
"amountMinor": 26791400
}
],
"destination": {
"valuationBasis": "cif-border"
}
}
} Отрисованный предпросмотр
компонент, отрисованный сам по себеРазрешённые узлы
что получает рендерер — узлов: 1incoterm-charges section incoterm-charges section incoterm-charges section incoterm-charges section incoterm-charges section incoterm-charges section incoterm-charges section incoterm-charges section incoterm-charges section incoterm-charges section incoterm-charges section Файлы
что записывает установка; выбранный файл и есть компонентnodes/incoterm-charges-and-customs-value.node.tsx import { Cell, Paragraph, Row, Section, type Section as SectionComponent, Table, useDeriver, useState } from "docxcelerate/template";
import incotermChargesAndCustomsValue from "../derivers/incoterm-charges-and-customs-value.deriver.ts";
/**
* A commercial-invoice section that states the Incoterms 2020 rule and lists
* every cost leg of the shipment, from export packing to import VAT, then gives
* the customs value on the basis the destination uses.
*
* Decisions already made for you:
*
* - No flat "shipping" line. Every leg is a named row with its amount, so
* customs can separate the goods price from the cost of delivering them. The
* US requires it by law (19 CFR 141.86(a)(8)), and the EU and UK need it
* before post-entry transport and import charges can come out of the value.
* - Who pays each leg is fixed by the rule, not chosen by the author. A buyer's
* leg is printed and used for the customs value, but never enters the total.
* - The customs value is not the invoice total. An EXW invoice is below the
* EU/UK CIF value, because the buyer's origin costs, freight and insurance to
* the border are added. A CIF invoice is above the US FOB value, because
* international freight and insurance come off. A DDP price includes duty,
* which comes back out. EU/UK air freight counts only at the Annex 23-01
* percentage.
* - Four common mistakes are flagged in print: a sea-only rule used for air,
* container or land freight; a missing named place; DDP with the seller
* paying import VAT; and a CFR or CPT invoice that shows insurance.
*
* All of it is decided in one deriver, because every step does arithmetic on
* request data, and publishing refuses that. 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.
*
* The legs are ten fixed slots rather than a `.map()`. The resolver reads a
* published loop's collection from request data and ignores the derived scope,
* so a `.map()` over the deriver's legs would publish and then resolve to no
* rows. A three-column table is the only placement that keeps the total
* checkable against the amounts above it, so there is no `arrangement` prop.
*
* The node publishes 15 branches against the default `branchLimit` of 32: four
* warnings, ten legs and the customs note.
*
* Installed by `dxcl add incoterm-charges-and-customs-value`.
*/
/** The ten cost legs of a shipment, in the order the goods meet them. */
export type IncotermLegKind =
| "export-packing"
| "origin-inland"
| "export-clearance"
| "origin-terminal"
| "main-carriage"
| "insurance"
| "destination-terminal"
| "onward-delivery"
| "import-duty"
| "import-tax";
/** What this component reads. Add these fields to your document data type. */
export interface IncotermChargesAndCustomsValueData {
shipment: {
/**
* The Incoterms 2020 rule the goods are sold under. Required: the deriver
* throws on anything else, including a 2010-only rule such as DAT, because
* the cost split is the 2020 one.
*/
incoterm: "EXW" | "FCA" | "FAS" | "FOB" | "CFR" | "CIF" | "CPT" | "CIP" | "DAP" | "DPU" | "DDP";
/**
* The named place or port the rule applies at, printed as given (trimmed),
* such as "Port of Felixstowe". Missing or blank prints the term without a
* place and prints the named-place warning: a rule with no place does not
* say where costs pass.
*/
namedPlace?: string;
/**
* How the main carriage travels. `"sea"` means bulk or break-bulk on a
* vessel; `"sea-container"` means containerised ocean freight. Used for the
* sea-only warning and for EU/UK air-freight apportionment. Missing means
* unknown: no mode warning, and air freight is counted in full. Throws on
* any other string.
*/
mode?: "sea" | "sea-container" | "air" | "road" | "rail" | "multimodal";
/**
* ISO 4217 code every amount is in. It also sets the minor-unit exponent
* (two for EUR, none for JPY). Required: throws when missing or not a code
* `Intl` knows.
*/
currency: string;
/**
* The goods price in integer minor units. Legs marked `inPrice` are inside
* this figure, not added to it. Required: throws unless it is a
* non-negative integer. Zero is accepted and prints as zero.
*/
goodsMinor: number;
/**
* Every cost leg there is a figure for, in any order. They print in the
* journey order of `IncotermLegKind`. At most one leg per kind: a duplicate
* throws, so add the amounts together first.
*
* A leg left out is treated as zero and prints no row, with one exception:
* the customs value cannot be stated when a figure it needs is missing. For
* a `cif-border` destination, supply the buyer's legs up to the border too,
* or the value understates. Missing or empty means no legs.
*/
legs?: Array<{
/** Which leg. Throws on anything else. */
kind: IncotermLegKind;
/**
* What it costs, in integer minor units. Required on every leg: throws
* unless it is a non-negative integer. A buyer's leg's amount is printed
* and used for the customs value, never added to the total.
*/
amountMinor: number;
/**
* True when a leg the seller pays is already inside `goodsMinor`. It
* prints "Included in price" with its amount and is not added again.
* Missing means false: the leg is charged on top. Throws when true on a
* leg the rule puts on the buyer, or when the included legs add up to
* more than `goodsMinor`.
*/
inPrice?: boolean;
}>;
/** How the importing country values goods for duty. Required: throws when missing. */
destination: {
/**
* `"cif-border"`: the EU and UK, which value goods at the place of entry,
* with freight and insurance to the border added. `"fob-export"`: the US,
* Canada and Australia, which value goods at the place of export, with
* international freight and insurance taken off. Required: throws on
* anything else.
*/
valuationBasis: "cif-border" | "fob-export";
/**
* The share of air freight that counts towards customs value, as a
* whole-number percentage 0 to 100, from Annex 23-01 of Implementing
* Regulation (EU) 2015/2447 or the UK's equivalent. Read only when
* `valuationBasis` is `"cif-border"` and `mode` is `"air"`. Missing there
* means 100%, and the customs note says so: over-declaring is the lawful
* direction to be wrong in. Throws when present but not an integer from
* 0 to 100.
*/
airFreightDutiablePercent?: number;
};
};
}
/** Prints the delivery terms, every cost leg, the invoice total and the customs value. */
export const IncotermChargesAndCustomsValue: SectionComponent = async () => {
const [shipment] = useState((d: IncotermChargesAndCustomsValueData) => d.shipment);
const c = await useDeriver(incotermChargesAndCustomsValue, [shipment]);
return (
<Section id="incoterm-charges" title="Delivery terms and charges">
<Paragraph id="incoterm-charges-term">Delivery terms: {c.term}</Paragraph>
{c.warnSeaOnly && <Paragraph id="incoterm-charges-warning-sea-only">{c.warnSeaOnly}</Paragraph>}
{c.warnNamedPlace && <Paragraph id="incoterm-charges-warning-named-place">{c.warnNamedPlace}</Paragraph>}
{c.warnDdpVat && <Paragraph id="incoterm-charges-warning-ddp-vat">{c.warnDdpVat}</Paragraph>}
{c.warnImpliedInsurance && (
<Paragraph id="incoterm-charges-warning-implied-insurance">{c.warnImpliedInsurance}</Paragraph>
)}
<Table id="incoterm-charges-lines" columns={[{ width: "auto" }, { width: 45 }, { width: 32, align: "right" }]}>
<Row id="incoterm-charges-header" header>
<Cell>Charge</Cell>
<Cell>Treatment</Cell>
<Cell>Amount</Cell>
</Row>
<Row id="incoterm-charges-goods">
<Cell>Goods</Cell>
<Cell></Cell>
<Cell>{c.goods}</Cell>
</Row>
{c.legs["export-packing"].label && (
<Row id="incoterm-charges-export-packing">
<Cell>{c.legs["export-packing"].label}</Cell>
<Cell>{c.legs["export-packing"].treatment}</Cell>
<Cell>{c.legs["export-packing"].amount}</Cell>
</Row>
)}
{c.legs["origin-inland"].label && (
<Row id="incoterm-charges-origin-inland">
<Cell>{c.legs["origin-inland"].label}</Cell>
<Cell>{c.legs["origin-inland"].treatment}</Cell>
<Cell>{c.legs["origin-inland"].amount}</Cell>
</Row>
)}
{c.legs["export-clearance"].label && (
<Row id="incoterm-charges-export-clearance">
<Cell>{c.legs["export-clearance"].label}</Cell>
<Cell>{c.legs["export-clearance"].treatment}</Cell>
<Cell>{c.legs["export-clearance"].amount}</Cell>
</Row>
)}
{c.legs["origin-terminal"].label && (
<Row id="incoterm-charges-origin-terminal">
<Cell>{c.legs["origin-terminal"].label}</Cell>
<Cell>{c.legs["origin-terminal"].treatment}</Cell>
<Cell>{c.legs["origin-terminal"].amount}</Cell>
</Row>
)}
{c.legs["main-carriage"].label && (
<Row id="incoterm-charges-main-carriage">
<Cell>{c.legs["main-carriage"].label}</Cell>
<Cell>{c.legs["main-carriage"].treatment}</Cell>
<Cell>{c.legs["main-carriage"].amount}</Cell>
</Row>
)}
{c.legs["insurance"].label && (
<Row id="incoterm-charges-insurance">
<Cell>{c.legs["insurance"].label}</Cell>
<Cell>{c.legs["insurance"].treatment}</Cell>
<Cell>{c.legs["insurance"].amount}</Cell>
</Row>
)}
{c.legs["destination-terminal"].label && (
<Row id="incoterm-charges-destination-terminal">
<Cell>{c.legs["destination-terminal"].label}</Cell>
<Cell>{c.legs["destination-terminal"].treatment}</Cell>
<Cell>{c.legs["destination-terminal"].amount}</Cell>
</Row>
)}
{c.legs["onward-delivery"].label && (
<Row id="incoterm-charges-onward-delivery">
<Cell>{c.legs["onward-delivery"].label}</Cell>
<Cell>{c.legs["onward-delivery"].treatment}</Cell>
<Cell>{c.legs["onward-delivery"].amount}</Cell>
</Row>
)}
{c.legs["import-duty"].label && (
<Row id="incoterm-charges-import-duty">
<Cell>{c.legs["import-duty"].label}</Cell>
<Cell>{c.legs["import-duty"].treatment}</Cell>
<Cell>{c.legs["import-duty"].amount}</Cell>
</Row>
)}
{c.legs["import-tax"].label && (
<Row id="incoterm-charges-import-tax">
<Cell>{c.legs["import-tax"].label}</Cell>
<Cell>{c.legs["import-tax"].treatment}</Cell>
<Cell>{c.legs["import-tax"].amount}</Cell>
</Row>
)}
<Row id="incoterm-charges-total">
<Cell>Invoice total</Cell>
<Cell></Cell>
<Cell>{c.total}</Cell>
</Row>
</Table>
<Paragraph id="incoterm-charges-customs-value">{c.customsLine}</Paragraph>
{c.customsNote && <Paragraph id="incoterm-charges-customs-note">{c.customsNote}</Paragraph>}
</Section>
);
}; derivers/incoterm-charges-and-customs-value.deriver.ts // deno-lint-ignore-file no-explicit-any
import { deriver } from "docxcelerate/template";
/**
* Splits a shipment's cost legs by who pays them under the Incoterms 2020 rule,
* totals the invoice, and states the customs value on the destination's basis.
* Installed by `dxcl add incoterm-charges-and-customs-value`.
*
* 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
* matrix, labels, formatters and rounding all live inside `run`.
*
* Reading the matrix:
*
* - Packing is the seller's under every rule (A8).
* - FCA at the seller's own premises has no origin-inland leg: the author
* leaves it out.
* - Under CPT and CIP to an inland place, carriage to that place is all
* main-carriage.
* - Insurance under the D-rules is the seller's because the seller carries the
* risk. It is not obliged, which is why a missing leg there means
* "uninsured", not "incomplete".
* - Destination handling that the seller's freight contract already includes
* belongs in main-carriage.
*/
export default deriver({
name: "incotermChargesAndCustomsValue",
run: (shipment: unknown) => {
const s = shipment as any;
const rules = ["EXW", "FCA", "FAS", "FOB", "CFR", "CIF", "CPT", "CIP", "DAP", "DPU", "DDP"];
// One column per rule, in the order of `rules`: S seller, B buyer.
const matrix: Record<string, string> = {
"export-packing": "SSSSSSSSSSS",
"origin-inland": "BSSSSSSSSSS",
"export-clearance": "BSSSSSSSSSS",
"origin-terminal": "BBBSSSSSSSS",
"main-carriage": "BBBBSSSSSSS",
"insurance": "BBBBBSBSSSS",
"destination-terminal": "BBBBBBBBSSS",
"onward-delivery": "BBBBBBBBSSS",
"import-duty": "BBBBBBBBBBS",
"import-tax": "BBBBBBBBBBS",
};
const labels: Record<string, string> = {
"export-packing": "Export packing",
"origin-inland": "Inland transport to the place of export",
"export-clearance": "Export clearance",
"origin-terminal": "Terminal handling at origin",
"main-carriage": "Main carriage (international freight)",
"insurance": "Cargo insurance",
"destination-terminal": "Terminal handling at destination",
"onward-delivery": "Delivery from the place of arrival",
"import-duty": "Import duty",
"import-tax": "Import VAT and taxes",
};
const kinds = Object.keys(matrix);
const modes = ["sea", "sea-container", "air", "road", "rail", "multimodal"];
const modeWords: Record<string, string> = {
"sea-container": "containerised",
air: "air",
road: "road",
rail: "rail",
multimodal: "multimodal",
};
const rule: string = s?.incoterm;
const ruleIndex = rules.indexOf(rule);
if (ruleIndex < 0) throw new Error("shipment.incoterm must be one of the eleven Incoterms 2020 rules");
const currency = s.currency;
let money: Intl.NumberFormat;
try {
// Intl accepts any three letters, so a code it does not list is refused by hand.
if (typeof currency !== "string" || !(Intl as any).supportedValuesOf("currency").includes(currency)) {
throw new Error("unknown");
}
money = new Intl.NumberFormat("en-GB", { style: "currency", currency });
} catch {
throw new Error("shipment.currency must be an ISO 4217 code");
}
const exponent = money.resolvedOptions().maximumFractionDigits as number;
const fmt = (minor: number) => money.format(minor / 10 ** exponent);
const minorOf = (value: unknown, field: string): number => {
if (typeof value !== "number" || !Number.isInteger(value) || value < 0) {
throw new Error(field + " must be a non-negative integer of minor units");
}
return value;
};
const goodsMinor = minorOf(s.goodsMinor, "shipment.goodsMinor");
if (s.legs !== undefined && !Array.isArray(s.legs)) throw new Error("shipment.legs must be an array");
const mode: string | undefined = s.mode;
if (mode !== undefined && !modes.includes(mode)) {
throw new Error("shipment.mode must be one of " + modes.join(", "));
}
const dest = s.destination;
const basis = dest?.valuationBasis;
if (basis !== "cif-border" && basis !== "fob-export") {
throw new Error('shipment.destination.valuationBasis must be "cif-border" or "fob-export"');
}
const pctGiven = dest.airFreightDutiablePercent;
if (pctGiven !== undefined && (typeof pctGiven !== "number" || !Number.isInteger(pctGiven) || pctGiven < 0 || pctGiven > 100)) {
throw new Error("shipment.destination.airFreightDutiablePercent must be an integer from 0 to 100");
}
// Validate every leg, then index by kind.
const byKind: Record<string, { amount: number; inPrice: boolean; seller: boolean }> = {};
((s.legs ?? []) as any[]).forEach((leg, i) => {
const f = "shipment.legs[" + i + "]";
if (!kinds.includes(leg?.kind)) throw new Error(f + ".kind is not a known leg");
if (byKind[leg.kind]) throw new Error(f + ".kind " + leg.kind + " appears twice; add the amounts together");
const amount = minorOf(leg.amountMinor, f + ".amountMinor");
const seller = matrix[leg.kind][ruleIndex] === "S";
const inPrice = leg.inPrice === true;
if (inPrice && !seller) throw new Error(f + ".inPrice is true, but " + rule + " puts " + leg.kind + " on the buyer");
byKind[leg.kind] = { amount, inPrice, seller };
});
let included = 0;
let charged = 0;
for (const k of kinds) {
const l = byKind[k];
if (!l || !l.seller) continue;
if (l.inPrice) included += l.amount;
else charged += l.amount;
}
if (included > goodsMinor) {
throw new Error("the legs included in the price come to more than shipment.goodsMinor");
}
const totalMinor = goodsMinor + charged;
// FOB: take the seller's post-export legs out, put the buyer's pre-export legs in.
const afterExport = ["main-carriage", "insurance", "destination-terminal", "onward-delivery", "import-duty", "import-tax"];
const beforeExport = ["export-packing", "origin-inland", "export-clearance", "origin-terminal"];
let fobMinor = totalMinor;
for (const k of kinds) {
const l = byKind[k];
if (!l) continue;
if (l.seller && afterExport.includes(k)) fobMinor -= l.amount;
if (!l.seller && beforeExport.includes(k)) fobMinor += l.amount;
}
const mainAmount = byKind["main-carriage"]?.amount ?? 0;
const insuranceAmount = byKind["insurance"]?.amount ?? 0;
// Round half up on exact integers, once, on the apportioned freight.
const pct = pctGiven ?? 100;
const dutiableFreight = basis === "cif-border" && mode === "air"
? Math.floor((2 * mainAmount * pct + 100) / 200)
: mainAmount;
const cifComputable = !(basis === "cif-border" && !byKind["main-carriage"]);
const cifMinor = cifComputable ? fobMinor + dutiableFreight + insuranceAmount : null;
let reason = "";
if (basis === "cif-border") {
if (!byKind["main-carriage"]) reason = "the cost of main carriage to the place of entry was not given";
} else if (matrix["main-carriage"][ruleIndex] === "S" && !byKind["main-carriage"]) {
reason = "the price includes main carriage, and it has to be itemised to be taken out";
} else if ((rule === "CIF" || rule === "CIP") && !byKind["insurance"]) {
reason = "the price includes insurance, and it has to be itemised to be taken out";
}
const customsMinor = reason ? null : basis === "cif-border" ? cifMinor : fobMinor;
const customsLine = reason
? "Customs value: not stated, because " + reason + "."
: basis === "cif-border"
? "Customs value, CIF to the place of entry: " + fmt(customsMinor as number) +
". Transport and handling after entry, and import duty and taxes, are shown separately above and are not part of it."
: "Customs value, FOB at the place of export: " + fmt(customsMinor as number) + ".";
let customsNote = "";
if (basis === "fob-export") {
customsNote =
"International freight and insurance are itemised above by name and amount, and are not part of this value (19 CFR 141.86(a)(8)).";
} else if (mode === "air") {
customsNote = pctGiven === undefined
? "Air freight counted in full. Supply the Annex 23-01 percentage for the country of departure to apportion it."
: "Air freight counted at " + pctGiven + "% under Annex 23-01 of Implementing Regulation (EU) 2015/2447.";
}
const place = typeof s.namedPlace === "string" ? s.namedPlace.trim() : "";
const term = rule + (place ? " " + place : "") + " Incoterms® 2020";
const seaOnly = ["FAS", "FOB", "CFR", "CIF"].includes(rule);
const replacement: Record<string, string> = { FAS: "FCA", FOB: "FCA", CFR: "CPT", CIF: "CIP" };
const warnSeaOnly = seaOnly && mode !== undefined && mode !== "sea"
? rule + " is a rule for sea and inland waterway transport only. For " + modeWords[mode] +
" freight the rule meant is " + replacement[rule] + ": " + rule +
" leaves the goods at the seller's risk until they are on board, long after the carrier has taken them."
: "";
const warnNamedPlace = place
? ""
: "No named place is given for " + rule + ", so the invoice does not say where costs and risk pass to the buyer.";
const warnDdpVat = rule === "DDP" && byKind["import-tax"]
? "Under DDP the seller pays the import VAT, which a seller not registered in the destination usually cannot recover. Consider DAP, or agree that import VAT is for the buyer's account."
: "";
const warnImpliedInsurance = (rule === "CFR" || rule === "CPT") && byKind["insurance"]
? rule + " does not oblige the seller to insure the goods. This invoice shows insurance, which implies they are insured. If the seller insured them, the rule is " +
(rule === "CFR" ? "CIF" : "CIP") + "."
: "";
const legs: Record<string, { label: string; treatment: string; amount: string }> = {};
for (const k of kinds) {
const l = byKind[k];
legs[k] = l
? {
label: labels[k],
treatment: !l.seller ? "For buyer's account" : l.inPrice ? "Included in price" : "Charged",
amount: fmt(l.amount),
}
: { label: "", treatment: "", amount: "" };
}
return {
term,
goods: fmt(goodsMinor),
legs,
total: fmt(totalMinor),
customsLine,
customsNote,
warnSeaOnly,
warnNamedPlace,
warnDdpVat,
warnImpliedInsurance,
totalMinor,
fobMinor,
cifMinor,
customsMinor,
};
},
});