CIS invoice deduction
BodyCIS comes off the labour only: the ex-VAT gross less materials at cost, plant hire and consumables, with any markup left in labour. VAT is worked out on the whole gross and is never reduced by CIS. The rate follows HMRC verification: nothing under gross payment status, 20% registered, 30% unverified with a warning to verify. The deduction is floored to the penny, so it never over-deducts. The domestic reverse charge applies when both parties are VAT-registered, the customer is CIS-registered and has not notified end-user or intermediary status: silence means reverse charge. The 5% disregard and zero-rated work stay standard. Under the reverse charge no VAT is added, and the notional VAT is returned for tax-treatment-notice. Unclear data fails the document instead of guessing.
npx dxcl add cis-invoice-deduction supplier.cisStatus Vorschaudaten
das JSON, mit dem diese Vorschau gebaut wurde{
"invoice": {
"grossExVat": 486250.75,
"materialsCost": 172430.18,
"plantHire": 38615.4,
"consumables": 9872.65,
"vatRate": 0.2
},
"supplier": {
"vatRegistered": true,
"cisStatus": "gross",
"verificationNumber": "V1234567890AB"
},
"customer": {
"vatRegistered": true,
"cisRegistered": true
}
} {
"invoice": {
"grossExVat": 486250.75,
"materialsCost": 172430.18,
"plantHire": 38615.4,
"consumables": 9872.65,
"vatRate": 0.2
},
"supplier": {
"vatRegistered": true,
"cisStatus": "net",
"verificationNumber": "V1234567890AB"
},
"customer": {
"vatRegistered": true,
"cisRegistered": true
}
} {
"invoice": {
"grossExVat": 486250.75,
"materialsCost": 172430.18,
"plantHire": 38615.4,
"consumables": 9872.65,
"vatRate": 0.2
},
"supplier": {
"vatRegistered": true,
"cisStatus": "unverified",
"verificationNumber": "V1234567890AB"
},
"customer": {
"vatRegistered": true,
"cisRegistered": true
}
} Gerenderte Vorschau
jede Anordnung für sich gerendertarrangement="table" Standard arrangement="stacked" Aufgelöste Nodes
was ein Renderer erhält — 1 Nodescis-deduction section cis-deduction section cis-deduction section Die Dateien
was die Installation schreibt; die gewählte Datei ist die Komponentenodes/cis-invoice-deduction.node.tsx import { arrange, Paragraph, Section, type Section as SectionComponent, useDeriver, useState } from "docxcelerate/template";
import type { CisInvoiceDeductionLines } from "../arrangements/cis-invoice-deduction/lines.ts";
import { stacked } from "../arrangements/cis-invoice-deduction/stacked.arrangement.tsx";
import { table } from "../arrangements/cis-invoice-deduction/table.arrangement.tsx";
import cisInvoiceDeduction from "../derivers/cis-invoice-deduction.deriver.ts";
/**
* What a construction subcontractor is actually paid on one invoice, and the
* working that shows it. Every usual mistake here gives a payable figure that
* HMRC or the contractor will dispute, so the decisions are made once.
*
* Decisions already made for you:
*
* - CIS comes off the labour only: the ex-VAT gross less materials at cost,
* plant hire and consumables. Never off materials, and never off VAT.
* - VAT is worked out on the whole gross and added before CIS comes off. CIS
* never reduces it, and it never reduces CIS.
* - Silence from the customer means the reverse charge applies. Only written
* end-user or intermediary status takes the invoice out of it.
* - The 5% disregard: when reverse-charge services are 5% or less of the
* invoice the whole invoice is standard-rated. Zero-rated work is never
* reverse-charged.
* - The deduction is floored to the penny, so it never over-deducts.
* - An unverified subcontractor is deducted at 30% and the document prints a
* warning to verify them.
* - Unclear data fails the document rather than guessing. A guessed treatment
* gives a disputed figure either way.
* - Everything is settled in one deriver, because every figure is arithmetic
* on request data and so cannot run at publish.
*
* Under the reverse charge the notional VAT is returned by the deriver for
* `tax-treatment-notice`, which reads it from request data: the calling
* application passes the same figure.
*
* Arrangement: where the lines sit is a separate choice from what they say.
* This file decides every line; each arrangement, in
* `arrangements/cis-invoice-deduction/`, only places them, so every
* arrangement prints the same lines under the same conditions.
*
* Installed by `dxcl add cis-invoice-deduction`, which also installs its deriver.
*/
/** What this component reads. Add these fields to your document data type. */
export interface CisInvoiceDeductionData {
invoice: {
/**
* The invoice value before VAT, materials included: what VAT is worked out
* on. Required: the deriver throws when it is missing, not a finite number,
* or negative. A credit note is not handled here.
*/
grossExVat: number;
/**
* What the subcontractor paid for materials, at cost and net of any VAT
* they can reclaim. A subcontractor not registered for VAT includes the VAT
* they paid. Any markup belongs in labour, not here. Required, and 0 when
* there are none: the deriver throws when it is missing or negative, and
* when materials, plant hire and consumables together exceed `grossExVat`.
*/
materialsCost: number;
/** Plant hired in for the job, at cost. Missing means 0, and the line is not printed. Throws when negative. */
plantHire?: number;
/** Consumable stores and fuel used on the job (not travel), at cost. Missing means 0, and the line is not printed. Throws when negative. */
consumables?: number;
/**
* The VAT rate the supply carries: 0.2 standard, 0.05 reduced, 0 zero-rated.
* Required when the supplier is VAT-registered; ignored when not. Any other
* value throws.
*/
vatRate: 0.2 | 0.05 | 0;
/**
* How much of the invoice value is reverse-charge construction services,
* as a fraction from 0 to 1. At 0.05 or less the 5% disregard applies and
* the whole invoice is standard-rated; above it the whole invoice is
* reverse-charged. Missing means 1: the whole invoice is construction
* services. Outside 0 to 1, or not a number, throws.
*/
reverseChargeShare?: number;
};
supplier: {
/**
* Whether the subcontractor is registered for VAT. Required: anything but a
* boolean throws. A guessed answer either drops the VAT from the payable or
* charges VAT that cannot lawfully be charged. Not registered means no VAT
* line and no reverse charge.
*/
vatRegistered: boolean;
/**
* Their CIS status as HMRC verified it: `gross` (gross payment status, 0%),
* `net` (registered, 20%), `unverified` (unmatched or not verified, 30%,
* with a warning). Required: anything else throws.
*/
cisStatus: "gross" | "net" | "unverified";
/** The verification number HMRC gave the contractor. Printed as given when present; nothing is printed when it is missing or empty. */
verificationNumber?: string;
};
customer: {
/** Whether the contractor is VAT-registered. Required as a boolean when the supplier is VAT-registered and `vatRate` is not 0; otherwise not read. Throws when required and not a boolean. */
vatRegistered: boolean;
/** Whether the contractor is registered for CIS. The same requirement as `vatRegistered`. */
cisRegistered: boolean;
/**
* Whether the customer has told the supplier in writing that it is an end
* user or an intermediary supplier. Either one takes the invoice out of the
* reverse charge. Missing, `null` or `""` is silence, and silence means the
* reverse charge applies. Any other value throws.
*/
endUserNotified?: "end-user" | "intermediary";
};
}
/**
* Where the lines sit.
*
* - `table`: labels in a column, figures right-aligned beside them, so the
* sums can be checked down the page. The default.
* - `stacked`: one "Label: figure" line under another, for a narrow column.
*/
export type CisInvoiceDeductionArrangement = "table" | "stacked";
/** Props for {@linkcode CisInvoiceDeduction}. */
export interface CisInvoiceDeductionProps {
/** Where the lines sit. Defaults to `"table"`. */
arrangement?: CisInvoiceDeductionArrangement;
}
/**
* Prints the CIS invoice breakdown: gross, materials, plant hire, labour, VAT,
* the CIS deduction and the amount payable, plus a warning when it applies.
*/
export const CisInvoiceDeduction: SectionComponent<CisInvoiceDeductionProps> = async ({ arrangement }) => {
const [invoice] = useState((data: CisInvoiceDeductionData) => data.invoice);
const [supplier] = useState((data: CisInvoiceDeductionData) => data.supplier);
const [customer] = useState((data: CisInvoiceDeductionData) => data.customer);
const cis = await useDeriver(cisInvoiceDeduction, [invoice, supplier, customer]);
const lines: CisInvoiceDeductionLines = {
gross: cis.gross,
materials: cis.materials,
plantHire: cis.plantHire,
consumables: cis.consumables,
labour: cis.labour,
vatLabel: cis.vatLabel,
vatValue: cis.vatValue,
cisLabel: cis.cisLabel,
cisDeduction: cis.cisDeduction,
payable: cis.payable,
};
return (
<Section id="cis-deduction" title="CIS deduction">
{cis.warning && <Paragraph id="cis-deduction-warning">{cis.warning}</Paragraph>}
{arrange(arrangement, {
table: () => table(lines),
stacked: () => stacked(lines),
})}
{supplier.verificationNumber && (
<Paragraph id="cis-deduction-verification">
CIS verification number: {supplier.verificationNumber}
</Paragraph>
)}
</Section>
);
}; derivers/cis-invoice-deduction.deriver.ts import { deriver } from "docxcelerate/template";
/**
* What a UK construction subcontractor is paid on one invoice, and every line
* of the working, as strings ready to print.
*
* Self-contained on purpose: a deriver is shipped to the engine as source
* text, so the formatter, the helpers and every constant live inside `run`.
* Anything outside it is undefined where it runs. It has no placeholder,
* because it is cheap and previews should show real figures.
*
* Money is whole pence from the first line, so no sum can drift. CIS is
* floored, because HMRC prescribes no rounding and flooring never
* over-deducts. VAT is rounded to the nearest penny with half going up, in
* integers: `Math.round(gross * 0.2)` misrounds at .5 in floating point.
*/
export default deriver({
name: "cisInvoiceDeduction",
run: (invoice: unknown, supplier: unknown, customer: unknown) => {
const fail = (message: string): never => {
throw new Error(`cisInvoiceDeduction: ${message}`);
};
const inv = (invoice ?? {}) as Record<string, unknown>;
const sup = (supplier ?? {}) as Record<string, unknown>;
const cust = (customer ?? {}) as Record<string, unknown>;
const money = (field: string, value: unknown, required: boolean): number => {
if (value == null && !required) return 0;
if (typeof value !== "number" || !Number.isFinite(value) || value < 0) {
return fail(`invoice.${field} must be a finite number of pounds, 0 or more; got ${String(value)}.`);
}
return Math.round(value * 100);
};
const gbp = new Intl.NumberFormat("en-GB", { style: "currency", currency: "GBP" });
const pounds = (pence: number) => gbp.format(pence / 100);
// 1. Validate. Unclear data fails the document: a guessed treatment gives
// a disputed figure either way.
const grossP = money("grossExVat", inv.grossExVat, true);
const materialsP = money("materialsCost", inv.materialsCost, true);
const plantP = money("plantHire", inv.plantHire, false);
const consumablesP = money("consumables", inv.consumables, false);
if (typeof sup.vatRegistered !== "boolean") fail("supplier.vatRegistered must be true or false.");
if (sup.cisStatus !== "gross" && sup.cisStatus !== "net" && sup.cisStatus !== "unverified") {
fail(`supplier.cisStatus must be "gross", "net" or "unverified"; got ${String(sup.cisStatus)}.`);
}
const share = inv.reverseChargeShare;
if (share != null && (typeof share !== "number" || !Number.isFinite(share) || share < 0 || share > 1)) {
fail(`invoice.reverseChargeShare must be a number from 0 to 1; got ${String(share)}.`);
}
const notified = cust.endUserNotified;
if (notified != null && notified !== "" && notified !== "end-user" && notified !== "intermediary") {
fail(`customer.endUserNotified must be "end-user", "intermediary" or missing; got ${String(notified)}.`);
}
const vatRate = inv.vatRate;
const registered = sup.vatRegistered === true;
if (registered && vatRate !== 0.2 && vatRate !== 0.05 && vatRate !== 0) {
fail(`invoice.vatRate must be 0.2, 0.05 or 0; got ${String(vatRate)}.`);
}
if (registered && vatRate !== 0 && (typeof cust.vatRegistered !== "boolean" || typeof cust.cisRegistered !== "boolean")) {
fail("customer.vatRegistered and customer.cisRegistered must both be true or false when VAT is charged.");
}
// 2. The costs together cannot exceed the gross.
const costsP = materialsP + plantP + consumablesP;
if (costsP > grossP) fail("materials, plant hire and consumables exceed the gross.");
// 3. VAT treatment: the first rule that matches wins. Silence from the
// customer is not end-user status, so it stays in the reverse charge.
const treatment = !registered
? "not-registered"
: vatRate === 0
? "zero-rated"
: cust.vatRegistered === true && cust.cisRegistered === true && (notified == null || notified === "") &&
((share as number | undefined) ?? 1) > 0.05
? "reverse-charge"
: "standard";
// 4. Any markup on materials is already inside the gross, so it stays in labour.
const labourP = grossP - costsP;
// 5. CIS comes off labour only. Integer operands, so the floor is exact.
const pct = sup.cisStatus === "gross" ? 0 : sup.cisStatus === "net" ? 20 : 30;
const deductionP = Math.floor(labourP * pct / 100);
// 6. VAT is on the whole gross, never on labour, and CIS never reduces it.
const vatPct = vatRate === 0.05 ? 5 : 20;
const vatP = Math.floor((grossP * vatPct + 50) / 100);
const vatChargedP = treatment === "standard" ? vatP : 0;
let vatLabel = "";
let vatValue = "";
if (treatment === "standard") {
vatLabel = `VAT at ${vatPct}%`;
vatValue = pounds(vatP);
} else if (treatment === "zero-rated") {
vatLabel = "VAT at 0% (zero-rated)";
vatValue = pounds(0);
} else if (treatment === "reverse-charge") {
vatLabel = "VAT (domestic reverse charge)";
// Never "£0.00": a zero reads as zero-rating, a different treatment.
vatValue = "Not charged: customer accounts to HMRC";
}
return {
vatTreatment: treatment,
gross: pounds(grossP),
materials: pounds(materialsP),
plantHire: plantP ? pounds(plantP) : "",
consumables: consumablesP ? pounds(consumablesP) : "",
labour: pounds(labourP),
cisLabel: pct === 0
? "CIS deduction (gross payment status)"
: pct === 30
? "CIS deduction at 30% (unverified)"
: "CIS deduction at 20%",
cisDeduction: pounds(deductionP),
vatLabel,
vatValue,
// 7. Integer pence, so adding VAT before taking CIS off cannot change the result.
payable: pounds(grossP + vatChargedP - deductionP),
notionalVat: treatment === "reverse-charge" ? vatP / 100 : null,
warning: pct === 30
? "CIS has been deducted at the higher rate of 30% because HMRC could not verify this subcontractor as registered. If they have registered since, verify them again before paying: the deduction falls to 20%, or to nothing under gross payment status."
: "",
};
},
}); arrangements/cis-invoice-deduction/lines.ts /**
* What the CIS invoice deduction hands each of its arrangements: every line it
* may print, already worked out. An arrangement places these and decides
* nothing, so both print the same lines under the same conditions.
*
* A field that is `""` is a line this invoice does not carry. Test it in the
* JSX, `{lines.plantHire && …}`, so the test is compiled into a condition the
* engine settles per document.
*
* Installed by `dxcl add cis-invoice-deduction`, with the arrangements.
*/
export interface CisInvoiceDeductionLines {
/** The invoice value before VAT, formatted. */
gross: string;
/** Materials at cost, formatted. */
materials: string;
/** Plant hire, formatted, or `""` when there is none. */
plantHire: string;
/** Consumables, formatted, or `""` when there are none. */
consumables: string;
/** The labour CIS is taken from, formatted. */
labour: string;
/** The VAT line's label, or `""` when no VAT line is printed. */
vatLabel: string;
/** The VAT line's value. */
vatValue: string;
/** The CIS line's label, naming the rate. */
cisLabel: string;
/** The CIS deduction, formatted. */
cisDeduction: string;
/** The amount payable, formatted. */
payable: string;
} arrangements/cis-invoice-deduction/table.arrangement.tsx import { Cell, Row, Table, type Yield } from "docxcelerate/template";
import type { CisInvoiceDeductionLines } from "./lines.ts";
/**
* Labels in a column with the figures right-aligned beside them, so the sums
* can be checked down the page. The default.
*/
export const table = (lines: CisInvoiceDeductionLines): Yield => (
<Table id="cis-deduction-lines" columns={[{ width: "auto" }, { width: 45, align: "right" }]}>
<Row id="cis-deduction-gross">
<Cell>Gross amount, excluding VAT</Cell>
<Cell>{lines.gross}</Cell>
</Row>
<Row id="cis-deduction-materials">
<Cell>Less materials at cost</Cell>
<Cell>{lines.materials}</Cell>
</Row>
{lines.plantHire && (
<Row id="cis-deduction-plant-hire">
<Cell>Less plant hire</Cell>
<Cell>{lines.plantHire}</Cell>
</Row>
)}
{lines.consumables && (
<Row id="cis-deduction-consumables">
<Cell>Less consumables</Cell>
<Cell>{lines.consumables}</Cell>
</Row>
)}
<Row id="cis-deduction-labour">
<Cell>Labour subject to CIS</Cell>
<Cell>{lines.labour}</Cell>
</Row>
{lines.vatLabel && (
<Row id="cis-deduction-vat">
<Cell>{lines.vatLabel}</Cell>
<Cell>{lines.vatValue}</Cell>
</Row>
)}
<Row id="cis-deduction-cis">
<Cell>Less {lines.cisLabel}</Cell>
<Cell>{lines.cisDeduction}</Cell>
</Row>
<Row id="cis-deduction-payable">
<Cell>Amount payable</Cell>
<Cell>{lines.payable}</Cell>
</Row>
</Table>
); arrangements/cis-invoice-deduction/stacked.arrangement.tsx import { Paragraph, type Yield } from "docxcelerate/template";
import type { CisInvoiceDeductionLines } from "./lines.ts";
/** One "Label: figure" line under another, for a narrow column. */
export const stacked = (lines: CisInvoiceDeductionLines): Yield => (
<>
<Paragraph id="cis-deduction-gross">Gross amount, excluding VAT: {lines.gross}</Paragraph>
<Paragraph id="cis-deduction-materials">Less materials at cost: {lines.materials}</Paragraph>
{lines.plantHire && <Paragraph id="cis-deduction-plant-hire">Less plant hire: {lines.plantHire}</Paragraph>}
{lines.consumables && (
<Paragraph id="cis-deduction-consumables">Less consumables: {lines.consumables}</Paragraph>
)}
<Paragraph id="cis-deduction-labour">Labour subject to CIS: {lines.labour}</Paragraph>
{lines.vatLabel && <Paragraph id="cis-deduction-vat">{lines.vatLabel}: {lines.vatValue}</Paragraph>}
<Paragraph id="cis-deduction-cis">Less {lines.cisLabel}: {lines.cisDeduction}</Paragraph>
<Paragraph id="cis-deduction-payable">Amount payable: {lines.payable}</Paragraph>
</>
);