GST-inclusive statement (AU/NZ)
BodyPrints the items and totals of an AU or NZ invoice and decides how GST appears. "Total price includes GST." replaces the GST figure only when every item is standard-rated, prices include GST and the GST is exactly 1/11 (AU) or 3/23 (NZ) of the total. Otherwise the figure is shown, and on a mixed invoice each taxable item is marked and a legend says so. GST is worked in cents under the ATO total-invoice rule, or the taxable-sale rule when chosen. Thresholds use the GST-inclusive total: the supplier's ABN always and the buyer's name or ABN from A$1,000 in Australia; in New Zealand, the GST number above NZ$200 and the buyer's name plus one identifier above NZ$1,000. ABNs, IRD numbers and NZBNs are checksummed, and a failure or a missing identity fails the build. An unregistered seller gets no GST wording. All of it is one deriver.
npx dxcl add gst-inclusive-statement Voorbeelddata
de JSON waarmee dit voorbeeld is gebouwd{
"gst": {
"country": "AU",
"sellerRegistered": true,
"sellerTaxId": "51 824 753 556",
"pricesIncludeGst": true,
"rounding": "total-invoice",
"lines": [
{
"description": "Commercial kitchen fit-out, Level 3, 1 Macquarie Place, Sydney: stage 2 of 3, labour and materials",
"amountMinor": 12950000,
"treatment": "taxable"
},
{
"description": "Fresh produce supplied to the staff canteen under the September 2026 catering agreement",
"amountMinor": 880000,
"treatment": "gst-free"
},
{
"description": "Residential lease, Unit 14/220 Sussex Street, Sydney: October 2026 rent",
"amountMinor": 429000,
"treatment": "input-taxed"
},
{
"description": "Early-payment rebate agreed 14 August 2026 on the fit-out contract",
"amountMinor": -259000,
"treatment": "taxable"
}
],
"buyer": {
"name": "Harbourside Hospitality Group Holdings Pty Ltd as trustee for the Harbourside Unit Trust",
"abn": "98 765 432 122"
}
}
} Gerenderd voorbeeld
elke opstelling op zichzelf gerenderdOpgeloste nodes
wat een renderer krijgt — 1 nodesgst-statement section De bestanden
wat installeren schrijft; het gekozen bestand is de componentnodes/gst-inclusive-statement.node.tsx import { arrange, Paragraph, Section, type Section as SectionComponent, useDeriver, useState } from "docxcelerate/template";
import type { GstInclusiveStatementLines } from "../arrangements/gst-inclusive-statement/lines.ts";
import { stacked } from "../arrangements/gst-inclusive-statement/stacked.arrangement.tsx";
import { table } from "../arrangements/gst-inclusive-statement/table.arrangement.tsx";
import gstInclusiveStatement from "../derivers/gst-inclusive-statement.deriver.ts";
/**
* The items, the GST and the total of an Australian or New Zealand invoice:
* how the GST is shown, and which identities the invoice has to carry. It
* prints the item list itself because, on a mixed invoice, the taxable mark
* has to sit on the item.
*
* Decisions already made for you:
*
* - "Total price includes GST." takes the place of a GST figure only when it
* is exactly true: every item taxable at the standard rate, prices GST
* inclusive, and the GST the rounding rule produced equal to 1/11 (AU) or
* 3/23 (NZ) of the total. A GST-free, input-taxed, margin-scheme, zero-rated
* or exempt item, GST-exclusive prices, or a per-line rounding that drifts a
* cent all show the figure instead.
* - A mixed invoice marks each taxable item with " *" and prints a legend, so
* the reader can see which items are taxable.
* - GST is 1/11 or 3/23 of an inclusive price, never 10% or 15% of the gross.
* - GST is worked in integer cents: one rounding on the taxable total (the
* ATO total-invoice rule, the default) or each taxable line rounded and then
* added (the taxable-sale rule, the issuer's choice).
* - Thresholds use the GST-inclusive total, which is what the law uses.
* - The AU buyer rule and the NZ buyer rule are different rules: AU asks for
* the buyer's name or ABN from A$1,000; NZ asks for the name and one
* identifier above NZ$1,000. The seller's ABN is always required in AU, the
* GST number above NZ$200 in NZ.
* - ABNs, IRD/GST numbers (with the secondary weights) and NZBNs are
* checksummed. A bad checksum or a missing required identity throws: an
* invoice that cannot support a credit claim is worse than a failed build.
* - A seller who is not registered gets "Invoice" and no GST wording at all.
* Claiming GST while unregistered is the unlawful direction, so a missing
* flag means not registered.
* - The A$82.50 threshold changes nothing printed. A registered buyer may ask
* for a tax invoice above it, but issuing one below it is always lawful and
* whether the buyer asked is not in the data. Do not add it.
* - All of it is one deriver, because totals, rounding, thresholds and
* checksums all compute on request data.
*
* Arrangement: where the items and totals sit is a separate choice from what
* they say, made with the `arrangement` prop. The identity lines, statement,
* legend and margin note are prose and stay here.
*
* Not checked: whether the buyer is registered, whether an ABN or GST number
* is live (an ABR or IRD lookup), or whether a treatment is right for the goods.
*
* Installed by `dxcl add gst-inclusive-statement`, which also installs its deriver.
*/
/** What this component reads. Add these fields to your document data type. */
export interface GstInclusiveStatementData {
gst: {
/**
* Whose GST rules apply. `AU`: 10%, A New Tax System (GST) Act 1999, amounts in AUD.
* `NZ`: 15%, Goods and Services Tax Act 1985, amounts in NZD. Required: anything
* else throws.
*/
country: "AU" | "NZ";
/**
* Whether the seller is registered for GST. Missing, or anything but `true`, means
* not registered: heading "Invoice", no GST rows, no marks, no legend, no statement,
* and no threshold checks. Claiming GST while unregistered is the unlawful
* direction, so that is not the default.
*/
sellerRegistered: boolean;
/**
* AU: the seller's ABN (11 digits; spaces allowed). NZ: the seller's GST number,
* which is its IRD number (8 or 9 digits; spaces and hyphens allowed).
* AU: required when registered and any line is taxable. Printed as
* "ABN 51 824 753 556" whenever given, even when not registered, because quoting an
* ABN is not a GST claim and avoids no-ABN withholding.
* NZ: required when registered, a line is taxable and the inclusive total is above
* NZ$200. Printed as "GST number 49-091-850" when given and registered, and ignored
* when not registered.
* Throws when given but failing its checksum, or when required and missing or blank.
*/
sellerTaxId?: string;
/**
* Whether line amounts include GST. Required when registered and any line is
* taxable: missing throws, because guessing moves the GST by a tenth. Ignored
* otherwise.
*/
pricesIncludeGst?: boolean;
/**
* How GST is rounded. `total-invoice` (the default when missing): one rounding, on
* the total of the taxable lines. `taxable-sale`: round each taxable line's GST,
* then add. Any other value throws.
*/
rounding?: "total-invoice" | "taxable-sale";
/** The items, in printing order. Missing or empty throws. */
lines: Array<{
/** What was sold, printed as given (trimmed). Missing or blank throws. */
description: string;
/**
* The line amount in cents, on the basis `pricesIncludeGst` names. Must be an
* integer (`Number.isInteger`), or it throws. Negative is allowed for a discount
* or rebate line. A negative total, or a negative total of the taxable lines,
* throws: adjustment notes are out of scope.
*/
amountMinor: number;
/**
* How GST treats the line. AU accepts `taxable | gst-free | input-taxed |
* margin-scheme`. NZ accepts `taxable | zero-rated | exempt`. Anything else,
* including the other country's term, throws, naming the allowed list. Only
* `taxable` is standard-rated. Every other treatment makes the invoice mixed and
* adds no GST.
*/
treatment: "taxable" | "gst-free" | "input-taxed" | "margin-scheme" | "zero-rated" | "exempt";
}>;
/**
* Who is buying. Missing is fine unless a threshold needs it. Every field
* given is printed, whatever the threshold.
*/
buyer?: {
/** The buyer's legal or trading name, printed as "Buyer: …". Blank counts as missing. */
name?: string;
/**
* AU only (ignored for NZ): the buyer's ABN, printed as "Buyer ABN 98 765 432 122".
* Throws when given and failing the checksum.
*/
abn?: string;
/**
* NZ only (ignored for AU): one identifier besides the name, printed as
* "Buyer <label>: <value>". A kind outside the union throws. A blank value counts
* as missing. An `nzbn` must be 13 digits passing the GS1 check digit, or it
* throws.
*/
identifier?: {
kind: "address" | "phone" | "email" | "nzbn" | "url" | "trading-name";
value: string;
};
};
};
}
/** Where the items and totals sit. `table` (the default) or `stacked`. */
export type GstInclusiveStatementArrangement = "table" | "stacked";
/** Props for {@linkcode GstInclusiveStatement}. */
export interface GstInclusiveStatementProps {
/** Where the items and totals sit. Defaults to `"table"`. */
arrangement?: GstInclusiveStatementArrangement;
}
export const GstInclusiveStatement: SectionComponent<GstInclusiveStatementProps> = async ({ arrangement }) => {
const [gst] = useState((d: GstInclusiveStatementData) => d.gst);
const s = await useDeriver(gstInclusiveStatement, [gst]);
const lines: GstInclusiveStatementLines = {
items: s.items,
subtotal: s.subtotal,
gst: s.gst,
gstLabel: s.gstLabel,
total: s.total,
totalLabel: s.totalLabel,
};
return (
<Section id="gst-statement" title={s.heading}>
{s.sellerTaxId && <Paragraph id="gst-seller-tax-id">{s.sellerTaxId}</Paragraph>}
{s.buyerName && <Paragraph id="gst-buyer-name">{s.buyerName}</Paragraph>}
{s.buyerTaxId && <Paragraph id="gst-buyer-tax-id">{s.buyerTaxId}</Paragraph>}
{s.buyerIdentifier && <Paragraph id="gst-buyer-identifier">{s.buyerIdentifier}</Paragraph>}
{arrange(arrangement, {
table: () => table(lines),
stacked: () => stacked(lines),
})}
{s.statement && <Paragraph id="gst-statement-text">{s.statement}</Paragraph>}
{s.legend && <Paragraph id="gst-legend">{s.legend}</Paragraph>}
{s.marginNote && <Paragraph id="gst-margin-note">{s.marginNote}</Paragraph>}
</Section>
);
}; derivers/gst-inclusive-statement.deriver.ts import { deriver } from "docxcelerate/template";
/**
* Works out how GST shows on an AU or NZ invoice, and checks who it names.
* Installed by `dxcl add gst-inclusive-statement`.
*
* It is self-contained on purpose: it travels to the engine as source text, so
* a module-level helper or constant would be a ReferenceError there. Every
* helper, checksum and label is declared inside `run`.
*/
export default deriver({
name: "gstInclusiveStatement",
run: (input: unknown) => {
// deno-lint-ignore no-explicit-any
const gst = input as any;
const blank = (v: unknown) => typeof v !== "string" || v.trim() === "";
if (typeof gst !== "object" || gst === null) throw new Error("gst is required");
if (gst.country !== "AU" && gst.country !== "NZ") throw new Error('gst.country must be "AU" or "NZ"');
const au = gst.country === "AU";
if (gst.rounding !== undefined && gst.rounding !== "total-invoice" && gst.rounding !== "taxable-sale") {
throw new Error("gst.rounding must be total-invoice or taxable-sale");
}
if (!Array.isArray(gst.lines) || gst.lines.length === 0) throw new Error("gst.lines must be a non-empty array");
const treatments = au
? ["taxable", "gst-free", "input-taxed", "margin-scheme"]
: ["taxable", "zero-rated", "exempt"];
let absSum = 0;
// deno-lint-ignore no-explicit-any
gst.lines.forEach((line: any, i: number) => {
const at = "gst.lines[" + i + "]";
if (blank(line?.description)) throw new Error(at + ".description is required");
if (!Number.isInteger(line.amountMinor)) throw new Error(at + ".amountMinor must be an integer");
if (!treatments.includes(line.treatment)) {
throw new Error(at + ".treatment must be one of " + treatments.join(", "));
}
absSum += Math.abs(line.amountMinor);
});
// Keeps 2 x 69 x sum a safe integer in the rounding below.
if (absSum > 1e13) throw new Error("gst.lines total is too large");
const registered = gst.sellerRegistered === true;
// deno-lint-ignore no-explicit-any
const taxable = gst.lines.filter((l: any) => l.treatment === "taxable");
const counted = registered && taxable.length > 0;
if (counted && typeof gst.pricesIncludeGst !== "boolean") {
throw new Error("gst.pricesIncludeGst is required when a line is taxable");
}
const inclusive = gst.pricesIncludeGst === true;
// Rate p/q; half a cent away from zero.
const p = au ? 1 : 3;
const q = au ? 10 : 20;
const div = (n: number, d: number) => Math.sign(n) * Math.floor((2 * Math.abs(n) + d) / (2 * d)) + 0;
const gstOf = (a: number, incl: boolean) => incl ? div(a * p, p + q) : div(a * p, q);
// deno-lint-ignore no-explicit-any
const sum = (ls: any[]) => ls.reduce((t: number, l: any) => t + l.amountMinor, 0);
const linesTotal = sum(gst.lines);
const taxableTotal = sum(taxable);
if (linesTotal < 0) throw new Error("gst.lines total is negative; adjustment notes are not supported");
if (taxableTotal < 0) throw new Error("gst.lines taxable total is negative; adjustment notes are not supported");
let gstMinor = 0;
if (counted) {
gstMinor = gst.rounding === "taxable-sale"
// deno-lint-ignore no-explicit-any
? taxable.reduce((t: number, l: any) => t + gstOf(l.amountMinor, inclusive), 0)
: gstOf(taxableTotal, inclusive);
}
const totalMinor = counted && !inclusive ? linesTotal + gstMinor : linesTotal;
// Checksums.
const digitsOf = (v: string, field: string, pattern: RegExp, what: string) => {
const d = v.replace(pattern, "");
if (!/^\d+$/.test(d)) throw new Error(field + " is not a valid " + what);
return d;
};
const abn = (v: string, field: string) => {
const d = digitsOf(v, field, /\s/g, "ABN");
const w = [10, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19];
let s = 0;
if (d.length === 11) {
for (let i = 0; i < 11; i++) s += (Number(d[i]) - (i === 0 ? 1 : 0)) * w[i];
}
if (d.length !== 11 || s % 89 !== 0) throw new Error(field + " is not a valid ABN");
return d.slice(0, 2) + " " + d.slice(2, 5) + " " + d.slice(5, 8) + " " + d.slice(8);
};
const ird = (v: string, field: string) => {
const d = digitsOf(v, field, /[\s-]/g, "IRD number");
const n = Number(d);
if ((d.length !== 8 && d.length !== 9) || n < 10000000 || n > 150000000) {
throw new Error(field + " is not a valid IRD number");
}
const nine = d.padStart(9, "0");
const check = (weights: number[]) => {
let s = 0;
for (let i = 0; i < 8; i++) s += Number(nine[i]) * weights[i];
const r = s % 11;
return r === 0 ? 0 : 11 - r;
};
let c = check([3, 2, 7, 6, 5, 4, 3, 2]);
if (c === 10) c = check([7, 4, 3, 2, 5, 2, 7, 6]);
if (c === 10 || c !== Number(nine[8])) throw new Error(field + " is not a valid IRD number");
return d.length === 8
? d.slice(0, 2) + "-" + d.slice(2, 5) + "-" + d.slice(5)
: d.slice(0, 3) + "-" + d.slice(3, 6) + "-" + d.slice(6);
};
const nzbn = (v: string, field: string) => {
const d = digitsOf(v, field, /\s/g, "NZBN");
let s = 0;
for (let i = 0; i < 12 && d.length === 13; i++) s += Number(d[i]) * (i % 2 === 0 ? 1 : 3);
if (d.length !== 13 || (10 - s % 10) % 10 !== Number(d[12])) throw new Error(field + " is not a valid NZBN");
};
// The seller's ID is printed whenever given: an AU ABN even when not
// registered; an NZ GST number only when registered.
let sellerTaxId: string | null = null;
const given = !blank(gst.sellerTaxId);
if (gst.sellerTaxId !== undefined && gst.sellerTaxId !== null && typeof gst.sellerTaxId !== "string") {
throw new Error("gst.sellerTaxId must be a string");
}
if (given && au) sellerTaxId = "ABN " + abn(gst.sellerTaxId, "gst.sellerTaxId");
if (given && !au && registered) sellerTaxId = "GST number " + ird(gst.sellerTaxId, "gst.sellerTaxId");
const buyer = gst.buyer ?? {};
const buyerNamed = !blank(buyer.name);
let buyerTaxId: string | null = null;
let buyerIdentifier: string | null = null;
let buyerIdentified = false;
if (au && !blank(buyer.abn)) {
buyerTaxId = "Buyer ABN " + abn(buyer.abn, "gst.buyer.abn");
}
if (!au && buyer.identifier !== undefined && buyer.identifier !== null) {
const labels: Record<string, string> = {
address: "Buyer address",
phone: "Buyer phone",
email: "Buyer email",
nzbn: "Buyer NZBN",
url: "Buyer website",
"trading-name": "Buyer trading name",
};
const kind = buyer.identifier.kind;
if (typeof kind !== "string" || !Object.prototype.hasOwnProperty.call(labels, kind)) {
throw new Error("gst.buyer.identifier.kind must be one of " + Object.keys(labels).join(", "));
}
if (!blank(buyer.identifier.value)) {
const value = buyer.identifier.value.trim();
if (kind === "nzbn") nzbn(value, "gst.buyer.identifier.value");
buyerIdentifier = labels[kind] + ": " + value;
buyerIdentified = true;
}
}
// Thresholds are tested against the GST-inclusive total.
if (counted) {
if (au) {
if (!given) throw new Error("gst.sellerTaxId: an AU tax invoice must show the supplier's ABN");
if (totalMinor >= 100000 && !buyerNamed && buyerTaxId === null) {
throw new Error("gst.buyer: an AU tax invoice of A$1,000 or more must show the buyer's name or ABN");
}
} else {
if (totalMinor > 20000 && !given) {
throw new Error("gst.sellerTaxId: an NZ tax invoice above NZ$200 must show the supplier's GST number");
}
if (totalMinor > 100000 && !(buyerNamed && buyerIdentified)) {
throw new Error(
"gst.buyer: an NZ tax invoice above NZ$1,000 must show the buyer's name and one identifier (gst.buyer.name and gst.buyer.identifier)",
);
}
}
}
const money = new Intl.NumberFormat(au ? "en-AU" : "en-NZ", { style: "currency", currency: au ? "AUD" : "NZD" });
const fmt = (minor: number) => money.format(minor / 100);
const mixed = counted && taxable.length < gst.lines.length;
// The statement stands in for the figure only when it is exactly true.
const exact = counted && inclusive && !mixed && gstMinor === gstOf(totalMinor, true);
const showGst = counted && !exact;
return {
heading: counted ? "Tax invoice" : "Invoice",
sellerTaxId,
buyerName: buyerNamed ? "Buyer: " + buyer.name.trim() : null,
buyerTaxId,
buyerIdentifier,
// deno-lint-ignore no-explicit-any
items: gst.lines.map((l: any) => ({
description: l.description.trim() + (mixed && l.treatment === "taxable" ? " *" : ""),
amount: fmt(l.amountMinor),
})),
subtotal: counted && !inclusive ? fmt(linesTotal) : null,
gst: showGst ? fmt(gstMinor) : null,
gstLabel: showGst ? (inclusive ? "GST included" : "GST") : "",
total: fmt(totalMinor),
totalLabel: showGst ? "Total including GST" : "Total",
statement: exact
? "Total price includes GST."
: registered && taxable.length === 0
? "No GST is charged on this invoice."
: null,
legend: mixed
? inclusive
? "* Taxable supply. The price of each marked item includes GST."
: "* Taxable supply. GST is charged on each marked item."
: null,
marginNote: registered && au && gst.lines.some((l: { treatment: string }) => l.treatment === "margin-scheme")
? "Items sold under the margin scheme carry no GST you can claim as a credit."
: null,
};
},
}); arrangements/gst-inclusive-statement/lines.ts /**
* What the GST statement hands each of its arrangements: items and totals,
* already decided and formatted. An arrangement places these and decides
* nothing, so both print the same lines under the same conditions.
*
* A `null` is a line this invoice does not carry. Test it in the JSX,
* `{lines.subtotal && …}`, so the test compiles into a condition the engine
* settles per document.
*
* Installed by `dxcl add gst-inclusive-statement`, with the arrangements.
*/
export interface GstInclusiveStatementLines {
/** Every line in input order; a taxable line on a mixed invoice already ends " *". */
items: Array<{ description: string; amount: string }>;
/** Total of the lines before GST; exclusive basis only, otherwise null. */
subtotal: string | null;
/** The GST figure when it is shown, otherwise null. */
gst: string | null;
/** "GST" or "GST included"; "" when gst is null. */
gstLabel: string;
/** The amount payable. */
total: string;
/** "Total including GST" or "Total". */
totalLabel: string;
} arrangements/gst-inclusive-statement/table.arrangement.tsx import { Cell, Row, Table, type Yield } from "docxcelerate/template";
import type { GstInclusiveStatementLines } from "./lines.ts";
/**
* Items and totals in two columns with the amounts right-aligned, so a reader
* adding them up finds every figure in one column. The header says nothing
* about GST, which keeps an unregistered invoice clean.
*/
export const table = (lines: GstInclusiveStatementLines): Yield => (
<Table id="gst-lines" columns={[{ width: "auto" }, { width: 36, align: "right" }]}>
<Row id="gst-lines-header">
<Cell>Item</Cell>
<Cell>Amount</Cell>
</Row>
{lines.items.map((item) => (
<Row id="gst-item">
<Cell>{item.description}</Cell>
<Cell>{item.amount}</Cell>
</Row>
))}
{lines.subtotal && (
<Row id="gst-subtotal">
<Cell>Subtotal excluding GST</Cell>
<Cell>{lines.subtotal}</Cell>
</Row>
)}
{lines.gst && (
<Row id="gst-amount">
<Cell>{lines.gstLabel}</Cell>
<Cell>{lines.gst}</Cell>
</Row>
)}
<Row id="gst-total">
<Cell>{lines.totalLabel}</Cell>
<Cell>{lines.total}</Cell>
</Row>
</Table>
); arrangements/gst-inclusive-statement/stacked.arrangement.tsx import { Paragraph, type Yield } from "docxcelerate/template";
import type { GstInclusiveStatementLines } from "./lines.ts";
/** One line per item and per total, for an invoice laid out as text. */
export const stacked = (lines: GstInclusiveStatementLines): Yield => (
<>
{lines.items.map((item) => <Paragraph id="gst-item">{item.description}: {item.amount}</Paragraph>)}
{lines.subtotal && <Paragraph id="gst-subtotal">Subtotal excluding GST: {lines.subtotal}</Paragraph>}
{lines.gst && <Paragraph id="gst-amount">{lines.gstLabel}: {lines.gst}</Paragraph>}
<Paragraph id="gst-total">{lines.totalLabel}: {lines.total}</Paragraph>
</>
);