Deposit outcome on cancellation
BodySecurity deposits, refundable advances and non-refundable advances look alike and are taxed differently. A security deposit carries no VAT and comes back less itemised deductions, each marked as a VAT-bearing supply or as compensation outside the scope. In the UK since March 2019, VAT on a retained advance stays due with no credit note, and the refunded part gets one at the original rate. In Germany the VAT is corrected only when the refund is actually paid. In the US a state flag says whether a forfeit is taxed, rather than a guess. A partly retained advance is split per VAT band, with VAT worked on the retained part and the remainder refunded, so the halves always add up to the VAT charged. No amount is printed negative. One deriver does all of it.
npx dxcl add deposit-cancellation-outcome order.jurisdiction Preview data
the JSON this preview was built with{
"order": {
"reference": "BK-2026-HALL-0048817/WEDDING-RECEPTION-REV-3",
"currency": "GBP",
"jurisdiction": "UK",
"cancelledOn": "2026-11-03",
"payments": [
{
"kind": "security",
"gross": 25000
},
{
"kind": "refundable-advance",
"gross": 1284750,
"bands": [
{
"rate": 20,
"net": 1000000,
"vat": 200000
},
{
"rate": 5,
"net": 80714.29,
"vat": 4035.71
}
],
"invoiceNumber": "SI-2026-07-004817/DEP-1"
},
{
"kind": "non-refundable-advance",
"gross": 48000,
"bands": [
{
"rate": 20,
"net": 40000,
"vat": 8000
}
],
"invoiceNumber": "SI-2026-05-003302/RES"
}
],
"retention": {
"percent": 25,
"reason": "cancellation less than 90 days before the event, clause 14.2"
},
"deductions": [
{
"label": "Specialist cleaning of the main hall carpets after the rehearsal dinner",
"amount": 1850.4,
"isSupply": true,
"vatRate": 20
},
{
"label": "Replacement of two damaged antique Venetian glass chandeliers in the Long Gallery",
"amount": 12600,
"isSupply": false
}
],
"creditNoteNumber": "CN-2026-11-000913/BK-0048817"
}
} {
"order": {
"reference": "BK-2026-HALL-0048817/WEDDING-RECEPTION-REV-3",
"currency": "GBP",
"jurisdiction": "DE",
"cancelledOn": "2026-11-03",
"payments": [
{
"kind": "security",
"gross": 25000
},
{
"kind": "refundable-advance",
"gross": 1284750,
"bands": [
{
"rate": 20,
"net": 1000000,
"vat": 200000
},
{
"rate": 5,
"net": 80714.29,
"vat": 4035.71
}
],
"invoiceNumber": "SI-2026-07-004817/DEP-1"
},
{
"kind": "non-refundable-advance",
"gross": 48000,
"bands": [
{
"rate": 20,
"net": 40000,
"vat": 8000
}
],
"invoiceNumber": "SI-2026-05-003302/RES"
}
],
"retention": {
"percent": 25,
"reason": "cancellation less than 90 days before the event, clause 14.2"
},
"deductions": [
{
"label": "Specialist cleaning of the main hall carpets after the rehearsal dinner",
"amount": 1850.4,
"isSupply": true,
"vatRate": 20
},
{
"label": "Replacement of two damaged antique Venetian glass chandeliers in the Long Gallery",
"amount": 12600,
"isSupply": false
}
],
"creditNoteNumber": "CN-2026-11-000913/BK-0048817"
}
} Rendered preview
each arrangement, rendered on its ownResolved nodes
what a renderer receives — 1 nodesdeposit-cancellation section deposit-cancellation section The files
what installing writes; the selected file is the componentnodes/deposit-cancellation-outcome.node.tsx import { arrange, Paragraph, Section, type Section as SectionComponent, useDeriver, useState } from "docxcelerate/template";
import type { DepositCancellationOutcomeLines } from "../arrangements/deposit-cancellation-outcome/lines.ts";
import { stacked } from "../arrangements/deposit-cancellation-outcome/stacked.arrangement.tsx";
import { table } from "../arrangements/deposit-cancellation-outcome/table.arrangement.tsx";
import depositCancellationOutcome from "../derivers/deposit-cancellation-outcome.deriver.ts";
/**
* The body section for a cancelled order: what happens to each kind of money
* paid in advance, how much is refunded, how much is kept and why, and the VAT
* or sales-tax position of each part.
*
* Three kinds of money that look alike are taxed differently. A security
* deposit is held against damage and carries no VAT; it comes back less
* itemised deductions. A refundable advance is part-payment of the price, and a
* contractual cancellation fee may be retained from it. A non-refundable
* advance is retained in full.
*
* UK, since 1 March 2019 (Revenue and Customs Brief 13/2018): VAT already
* accounted for on a retained advance stays due and no credit note is issued
* for it; the refunded part gets a credit note at the advance's original rate.
* The older advice that a forfeited deposit is compensation outside the scope
* of VAT is deliberately not followed.
*
* Germany: VAT on a refunded advance is corrected when the money is actually
* paid back, not on cancellation (UStG s.17(2) no.2), and an agreed
* cancellation fee is consideration with VAT backed out at the advance's rate
* (CJEU C-622/23). Damages not agreed in the contract are not covered.
*
* US: whether a forfeited deposit bears sales tax depends on the state, so the
* data carries an explicit flag rather than the component guessing.
*
* A partly retained advance is split per VAT band, VAT is worked on the
* retained part, and the refunded VAT is whatever is left of the VAT charged,
* so the two halves always add up to it. No amount is printed negative; the
* labels say which way the money goes.
*
* The statute references have not been checked against primary sources. An
* adviser needs to review them before this goes in front of a customer.
*
* Everything is decided once, in one deriver, and the node prints what it
* returns. Deductions are fixed slots, not a loop: the resolver reads a
* published loop's collection from request data and ignores derived scope, so a
* loop over derived rows would print nothing. Published, the section is 19
* branches (the nothing-paid arm, the arrangement's wrapper, 11 rows and 6
* notes) against the default `branchLimit` of 32.
*
* Arrangement: where the amounts sit is the `arrangement` prop. This file
* decides every line; each arrangement, in
* `arrangements/deposit-cancellation-outcome/`, only places them.
*
* Installed by `dxcl add deposit-cancellation-outcome`, which also installs its deriver.
*/
/** What this component reads. Add these fields to your document data type. */
export interface DepositCancellationOutcomeData {
order: {
/**
* The order, booking or contract that was cancelled, printed as given
* (trimmed). Required: the deriver throws when it is missing or blank.
*/
reference: string;
/**
* ISO 4217 code: "GBP", "EUR", "USD". Required, no default. Its minor-unit
* exponent comes from Intl; a code Intl rejects throws, naming
* `order.currency`.
*/
currency: string;
/**
* Whose rules apply. Required: anything else throws. There is no generic
* "EU" arm, because the German repayment rule is not the Directive's default.
*/
jurisdiction: "UK" | "DE" | "US";
/**
* For US only: the state whose sales-tax rule decides a forfeited deposit,
* printed as given ("California" or "CA"). Required when jurisdiction is US
* and anything is retained from an advance; throws otherwise. Ignored
* outside the US.
*/
usState?: string;
/**
* For US only: whether `usState` charges sales tax on a forfeited deposit.
* Required when jurisdiction is US and anything is retained from an
* advance: missing throws, because guessing is wrong in roughly half the
* states. Ignored outside the US.
*/
usForfeitTaxable?: boolean;
/**
* The day the order was cancelled, ISO 8601 (`2026-11-03`). Anything after
* the tenth character is ignored. Required: throws when missing or not
* YYYY-MM-DD. For UK, a date before 2019-03-01 throws: the
* pre-Brief-13/2018 rule is out of scope.
*/
cancelledOn: string;
/**
* Every payment taken in advance, in the order they should be read. Missing
* or empty means nothing was paid: the section prints the intro and the
* "nothing paid" line.
*/
payments?: Array<{
/** Which kind of money it is. Required: anything else throws, naming `order.payments[i].kind`. */
kind: "security" | "refundable-advance" | "non-refundable-advance";
/**
* What was paid, VAT-inclusive, in major units (1250.5 for 1,250.50).
* Required: not a finite number, negative, or more decimals than the
* currency allows → throws. Zero is allowed and contributes nothing.
*/
gross: number;
/**
* The VAT analysis of this payment as invoiced, one entry per rate. Each
* net + vat must sum, across bands, to `gross` exactly in minor units, or
* it throws. Missing on an advance means it carried no VAT (one 0% band
* of the whole gross). On a security deposit, and on any US payment, a
* band with vat > 0 throws: a security deposit carries no VAT, and
* refunding collected US sales tax is out of scope.
*/
bands?: Array<{
/** The VAT rate in percent, 0–100, at most two decimals: 20, 19, 7, 5.5. */
rate: number;
/** Net in major units, ≥ 0. */
net: number;
/** VAT in major units, ≥ 0, as charged on the original invoice. */
vat: number;
}>;
/**
* The invoice this advance was billed on. Named in the credit-note line
* when part of it is refunded with VAT. Missing: the line names no invoice.
*/
invoiceNumber?: string;
}>;
/**
* The cancellation fee kept from the refundable advances, as the contract
* sets it. Missing means none is kept: refundable advances come back in full.
*/
retention?: {
/**
* A share of the pooled refundable-advance gross, 0–100, at most two
* decimals. Give exactly one of `percent` and `amount`; both or neither throws.
*/
percent?: number;
/**
* A fixed sum in major units, ≥ 0. More than the pooled refundable-advance
* gross throws (the section never prints a negative refund).
*/
amount?: number;
/**
* Why it is kept, printed in the "Retained: <reason>" line. Required when
* `retention` is given; missing or blank throws.
*/
reason: string;
};
/**
* Itemised deductions from the security deposit, in print order. At most 6;
* more throws. Missing or empty means the deposit comes back whole. Given
* when no security deposit was paid, or summing to more than it, throws.
*/
deductions?: Array<{
/** What it is for, printed as given (trimmed). Required, non-blank. */
label: string;
/** VAT-inclusive, major units, > 0. Zero or negative throws. */
amount: number;
/**
* True when the deduction pays for a supply (cleaning, a service), so VAT
* is due on it. False means compensation, outside the scope of VAT.
* Required for UK and DE: missing throws, because either default
* misstates VAT. Ignored for US.
*/
isSupply?: boolean;
/**
* The rate VAT is backed out at, percent. Required when `isSupply` is
* true for UK and DE; missing throws. Ignored otherwise.
*/
vatRate?: number;
}>;
/**
* The number of the credit note issued for the refunded part of the
* advances. Printed in the credit-note line. Missing (UK or DE, refunded
* VAT > 0) prints "will follow" instead of "issued".
*/
creditNoteNumber?: string;
/**
* When the refund was actually paid, ISO 8601. Missing means not yet paid.
* In DE it also decides the VAT-correction sentence. Throws when present
* but not a date.
*/
refundPaidOn?: string;
};
}
/**
* Where the amounts sit.
*
* - `table`: label and amount in two columns, amounts right-aligned. The default.
* - `stacked`: one "Label: amount" line under another.
*/
export type DepositCancellationOutcomeArrangement = "table" | "stacked";
/** Props for {@linkcode DepositCancellationOutcome}. */
export interface DepositCancellationOutcomeProps {
/** Where the amounts sit. Defaults to `"table"`. */
arrangement?: DepositCancellationOutcomeArrangement;
}
export const DepositCancellationOutcome: SectionComponent<DepositCancellationOutcomeProps> = async (
{ arrangement },
) => {
const [order] = useState((data: DepositCancellationOutcomeData) => data.order);
const c = await useDeriver(depositCancellationOutcome, [order]);
const lines: DepositCancellationOutcomeLines = c.lines;
return (
<Section id="deposit-cancellation" title="Your payments after cancellation">
<Paragraph id="deposit-cancellation-intro">Order {c.reference} was cancelled on {c.cancelledOn}.</Paragraph>
{c.nothingPaid && (
<Paragraph id="deposit-cancellation-nothing-paid">
Nothing was paid in advance on this order, so nothing is refunded or retained.
</Paragraph>
)}
{arrange(arrangement, {
table: () => table(lines),
stacked: () => stacked(lines),
})}
{c.refundPaid && <Paragraph id="deposit-cancellation-refund-paid">{c.refundPaid}</Paragraph>}
{c.creditNote && <Paragraph id="deposit-cancellation-credit-note">{c.creditNote}</Paragraph>}
{c.vatCorrection && <Paragraph id="deposit-cancellation-vat-correction">{c.vatCorrection}</Paragraph>}
{c.retainedVat && <Paragraph id="deposit-cancellation-retained-vat">{c.retainedVat}</Paragraph>}
{c.securityVat && <Paragraph id="deposit-cancellation-security-vat">{c.securityVat}</Paragraph>}
{c.salesTax && <Paragraph id="deposit-cancellation-sales-tax">{c.salesTax}</Paragraph>}
</Section>
);
}; derivers/deposit-cancellation-outcome.deriver.ts // deno-lint-ignore-file no-explicit-any
import { deriver } from "docxcelerate/template";
/**
* Decides, for a cancelled order, what happens to each kind of money paid in
* advance and how VAT or sales tax treats each part. Installed by
* `dxcl add deposit-cancellation-outcome`.
*
* 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. Every
* helper, and the cap of six deduction slots, is declared inside `run`.
*
* Money never meets a float. Each amount is read from the number's own decimal
* string into integer minor units, a rate into basis points, and every share
* is an integer division rounded half up. The VAT on a partly retained advance
* is worked on the retained part and the refunded side takes whatever is left
* of the VAT charged, so the two halves always add back to it.
*/
export default deriver({
name: "depositCancellationOutcome",
run: (input: unknown) => {
const P = "depositCancellationOutcome: ";
const fail = (message: string): never => {
throw new Error(P + message);
};
const order = input as any;
if (order === null || typeof order !== "object" || Array.isArray(order)) fail("order must be an object");
const reference = typeof order.reference === "string" ? order.reference.trim() : "";
if (reference === "") fail("order.reference is required");
let probe: Intl.NumberFormat | undefined;
try {
probe = new Intl.NumberFormat("en", { style: "currency", currency: order.currency });
} catch {
fail("order.currency must be an ISO 4217 code");
}
const exponent = probe!.resolvedOptions().maximumFractionDigits as number;
const jurisdiction = order.jurisdiction;
if (jurisdiction !== "UK" && jurisdiction !== "DE" && jurisdiction !== "US") {
fail('order.jurisdiction must be "UK", "DE" or "US"');
}
const US = jurisdiction === "US";
const locale = US ? "en-US" : "en-GB";
const money = new Intl.NumberFormat(locale, { style: "currency", currency: order.currency });
const fmt = (minor: number) => money.format(minor / 10 ** exponent);
const dates = new Intl.DateTimeFormat(locale, { dateStyle: "long", timeZone: "UTC" });
const isoDay = (value: unknown, field: string): string => {
if (typeof value !== "string" || !/^\d{4}-\d{2}-\d{2}/.test(value)) fail(field + " must be an ISO 8601 date");
const day = (value as string).slice(0, 10);
const d = new Date(day + "T00:00:00Z");
if (Number.isNaN(d.getTime()) || d.toISOString().slice(0, 10) !== day) fail(field + " is not a real date");
return day;
};
const fmtDay = (iso: string) => dates.format(new Date(iso + "T00:00:00Z"));
const cancelledIso = isoDay(order.cancelledOn, "order.cancelledOn");
if (jurisdiction === "UK" && cancelledIso < "2019-03-01") {
fail("order.cancelledOn is before 2019-03-01, when the current UK rule began; the earlier rule is not covered");
}
const hasPaidOn = order.refundPaidOn !== undefined && order.refundPaidOn !== null;
const paidIso = hasPaidOn ? isoDay(order.refundPaidOn, "order.refundPaidOn") : "";
// Parsed from the number's own digits: 1250.5 is 125050 whatever the float says.
const minorOf = (value: unknown, field: string): number => {
if (typeof value !== "number" || !Number.isFinite(value) || value < 0 || Object.is(value, -0)) {
fail(field + " must be a non-negative number");
}
const text = String(value);
if (/e/i.test(text)) fail(field + " is too large or too small to read exactly");
const [whole, fraction = ""] = text.split(".");
if (fraction.length > exponent) fail(field + " has more decimals than " + order.currency + " allows");
const minor = Number(whole + fraction.padEnd(exponent, "0"));
if (!Number.isSafeInteger(minor)) fail(field + " is too large");
return minor;
};
const bpOf = (value: unknown, field: string): number => {
if (typeof value !== "number" || !Number.isFinite(value) || value < 0 || value > 100) {
fail(field + " must be a number from 0 to 100");
}
const bp = Math.round((value as number) * 100);
if (bp / 100 !== value) fail(field + " has more than two decimals");
return bp;
};
// Half away from zero, on non-negative integers.
const roundHalf = (n: number, d: number): number => {
let q = Math.floor(n / d);
while (q * d > n) q--;
const r = n - q * d;
return 2 * r >= d ? q + 1 : q;
};
// Pooling, in input order. Each band keeps the payment it came from.
const payments = order.payments === undefined || order.payments === null ? [] : order.payments;
if (!Array.isArray(payments)) fail("order.payments must be an array");
let S = 0;
let R = 0;
let N = 0;
const refundable: Array<{ gross: number; vat: number; rateBp: number; payment: number }> = [];
let nonRefundableVat = 0;
payments.forEach((payment: any, i: number) => {
const at = "order.payments[" + i + "]";
const kind = payment?.kind;
if (kind !== "security" && kind !== "refundable-advance" && kind !== "non-refundable-advance") {
fail(at + '.kind must be "security", "refundable-advance" or "non-refundable-advance"');
}
const gross = minorOf(payment.gross, at + ".gross");
let bands = payment.bands;
if (bands === undefined || bands === null) bands = [{ rate: 0, net: gross / 10 ** exponent, vat: 0 }];
if (!Array.isArray(bands)) fail(at + ".bands must be an array");
let sum = 0;
const parsed = bands.map((band: any, j: number) => {
const bandAt = at + ".bands[" + j + "]";
const rateBp = bpOf(band?.rate, bandAt + ".rate");
const net = minorOf(band.net, bandAt + ".net");
const vat = minorOf(band.vat, bandAt + ".vat");
if (vat > 0 && kind === "security") fail(bandAt + ".vat: a security deposit carries no VAT");
if (vat > 0 && US) fail(bandAt + ".vat: refunding sales tax collected on an advance is not covered");
sum += net + vat;
return { gross: net + vat, vat, rateBp };
});
if (sum !== gross) fail(at + ".bands do not add up to " + at + ".gross");
if (kind === "security") S += gross;
else if (kind === "refundable-advance") {
R += gross;
for (const band of parsed) refundable.push({ ...band, payment: i });
} else {
N += gross;
for (const band of parsed) nonRefundableVat += band.vat;
}
if (payment.invoiceNumber !== undefined && payment.invoiceNumber !== null && typeof payment.invoiceNumber !== "string") {
fail(at + ".invoiceNumber must be a string");
}
});
const paid = S + R + N;
// Retention from the refundable advances.
let T = 0;
let reason = "";
const retention = order.retention;
if (retention !== undefined && retention !== null) {
if (typeof retention !== "object") fail("order.retention must be an object");
const hasPercent = retention.percent !== undefined && retention.percent !== null;
const hasAmount = retention.amount !== undefined && retention.amount !== null;
if (hasPercent === hasAmount) fail("order.retention needs exactly one of percent and amount");
if (typeof retention.reason !== "string" || retention.reason.trim() === "") {
fail("order.retention.reason is required");
}
reason = retention.reason.trim();
if (hasPercent) T = roundHalf(R * bpOf(retention.percent, "order.retention.percent"), 10000);
else {
T = minorOf(retention.amount, "order.retention.amount");
if (T > R) fail("order.retention.amount is more than the refundable advances paid");
}
}
// Largest remainder: floor shares, then the spare minor units go one each
// to the biggest remainders, the earlier band on a tie.
const shares = refundable.map((band) =>
R === 0 ? 0 : Number((BigInt(T) * BigInt(band.gross)) / BigInt(R))
);
const remainders = refundable.map((band) => R === 0 ? BigInt(0) : (BigInt(T) * BigInt(band.gross)) % BigInt(R));
let spare = T - shares.reduce((a, b) => a + b, 0);
const byRemainder = refundable.map((_, k) => k).sort((a, b) =>
remainders[a] === remainders[b] ? a - b : remainders[a] > remainders[b] ? -1 : 1
);
for (const k of byRemainder) {
if (spare <= 0) break;
shares[k] += 1;
spare -= 1;
}
let retainedVatRefundable = 0;
let refundedVat = 0;
const refundedByPayment = new Map<number, number>();
refundable.forEach((band, k) => {
const t = shares[k];
const retainedVat = t === band.gross
? band.vat
: Math.min(band.vat, Math.max(0, roundHalf(t * band.rateBp, 10000 + band.rateBp)));
retainedVatRefundable += retainedVat;
refundedVat += band.vat - retainedVat;
refundedByPayment.set(band.payment, (refundedByPayment.get(band.payment) ?? 0) + band.gross - t);
});
const retainedVat = retainedVatRefundable + nonRefundableVat;
// Deductions from the security deposit.
const deductions = order.deductions === undefined || order.deductions === null ? [] : order.deductions;
if (!Array.isArray(deductions)) fail("order.deductions must be an array");
if (deductions.length > 6) fail("order.deductions has more than 6 entries");
let D = 0;
const slots: Array<{ description: string | null; amount: string | null; line: string | null }> = [];
deductions.forEach((item: any, i: number) => {
const at = "order.deductions[" + i + "]";
if (typeof item?.label !== "string" || item.label.trim() === "") fail(at + ".label is required");
const label = item.label.trim();
const amount = minorOf(item.amount, at + ".amount");
if (amount === 0) fail(at + ".amount must be more than zero");
D += amount;
let treatment = "";
if (!US) {
if (typeof item.isSupply !== "boolean") fail(at + ".isSupply is required: VAT depends on it");
if (item.isSupply) {
if (item.vatRate === undefined || item.vatRate === null) fail(at + ".vatRate is required when isSupply is true");
const rateBp = bpOf(item.vatRate, at + ".vatRate");
treatment = "including " + fmt(roundHalf(amount * rateBp, 10000 + rateBp)) + " VAT";
} else treatment = "compensation, outside the scope of VAT";
}
slots.push({
description: label + (treatment ? " (" + treatment + ")" : ""),
amount: fmt(amount),
line: label + ": " + fmt(amount) + (treatment ? ", " + treatment : ""),
});
});
if (D > S) fail(S === 0 ? "order.deductions given but no security deposit was paid" : "order.deductions exceed the security deposit");
while (slots.length < 6) slots.push({ description: null, amount: null, line: null });
const kept = T + N;
if (US && kept > 0) {
if (typeof order.usState !== "string" || order.usState.trim() === "") fail("order.usState is required when an advance is retained");
if (typeof order.usForfeitTaxable !== "boolean") {
fail("order.usForfeitTaxable is required when an advance is retained");
}
}
const refunded = S - D + (R - T);
const retained = D + T + N;
if (refunded + retained !== paid) fail("internal error: refunded and retained do not add up to what was paid");
const nothingPaid = paid === 0;
const some = (minor: number) => (!nothingPaid && minor > 0 ? fmt(minor) : null);
const lines = {
paid: some(paid),
security: some(S),
deductions: slots,
refundable: some(R),
retentionLabel: some(T) ? "Retained: " + reason : null,
retention: some(T),
nonRefundable: some(N),
refunded: nothingPaid ? null : fmt(refunded),
retained: some(retained),
};
const refundedGross = R - T;
const invoices: string[] = [];
payments.forEach((payment: any, i: number) => {
const number = typeof payment.invoiceNumber === "string" ? payment.invoiceNumber.trim() : "";
if (payment.kind === "refundable-advance" && number && (refundedByPayment.get(i) ?? 0) > 0 && !invoices.includes(number)) {
invoices.push(number);
}
});
const against = invoices.length === 0
? ""
: invoices.length === 1
? " against invoice " + invoices[0]
: " against invoices " + invoices.slice(0, -1).join(", ") + " and " + invoices[invoices.length - 1];
const paidOn = hasPaidOn ? fmtDay(paidIso) : "";
const creditsVat = !US && refundedVat > 0;
return {
nothingPaid,
cancelledOn: fmtDay(cancelledIso),
reference,
lines,
refundPaid: nothingPaid || refunded === 0
? null
: hasPaidOn
? "The refund of " + fmt(refunded) + " was paid on " + paidOn + "."
: "The refund of " + fmt(refunded) + " has not been paid yet.",
creditNote: !creditsVat || nothingPaid
? null
: String(order.creditNoteNumber ?? "").trim()
? "Credit note " + String(order.creditNoteNumber).trim() + " issued for " + fmt(refundedGross) + ", including " +
fmt(refundedVat) + " VAT" + against + "."
: "A credit note for " + fmt(refundedGross) + ", including " + fmt(refundedVat) + " VAT" + against +
", will follow.",
vatCorrection: jurisdiction !== "DE" || refundedVat === 0
? null
: hasPaidOn
? "The " + fmt(refundedVat) + " VAT on the refunded amount is corrected in the period the refund was paid, " +
paidOn + "."
: "The " + fmt(refundedVat) + " VAT on the refunded amount is corrected when the refund is paid, not on cancellation.",
retainedVat: US || retainedVat === 0
? null
: jurisdiction === "UK"
? "The " + fmt(kept) + " retained includes " + fmt(retainedVat) +
" VAT, accounted for when you paid. It remains due, so no credit note is issued for it."
: "The " + fmt(kept) + " retained is the cancellation fee agreed in your contract and includes " +
fmt(retainedVat) + " VAT.",
securityVat: US || S === 0 ? null : "The " + fmt(S) + " security deposit carried no VAT.",
salesTax: !US || kept === 0
? null
: order.usForfeitTaxable
? "Under " + order.usState.trim() + " rules, sales tax is due on the " + fmt(kept) +
" retained. It is not included in the amounts above."
: order.usState.trim() + " charges no sales tax on the " + fmt(kept) + " retained.",
totals: {
paid,
security: S,
deductions: D,
refundable: R,
retention: T,
nonRefundable: N,
refunded,
retained,
refundedVat,
retainedVat,
},
};
},
}); arrangements/deposit-cancellation-outcome/lines.ts /**
* What the deposit outcome hands each of its arrangements: every line it may
* print, already formatted and already decided. An arrangement places these
* and decides nothing, so both print the same lines under the same conditions.
*
* A `null` is a line this order does not carry. Test it in the JSX,
* `{lines.security && …}`, so the test is compiled into a condition the engine
* settles per document.
*
* Installed by `dxcl add deposit-cancellation-outcome`, with the arrangements.
*/
export interface DepositCancellationOutcomeLines {
/** Total paid in advance, formatted; null when nothing was paid (the arrangement prints nothing). */
paid: string | null;
/** Security deposit held; null when none. */
security: string | null;
/** Exactly 6 slots. An unused slot has every field null. */
deductions: Array<{
/** "Specialist cleaning (including £308.40 VAT)"; US: the label alone. */
description: string | null;
/** The deduction, formatted; null on an unused slot. */
amount: string | null;
/** "Specialist cleaning: £1,850.40, including £308.40 VAT", for stacked. */
line: string | null;
}>;
/** Pooled refundable advance paid; null when none. */
refundable: string | null;
/** "Retained: <reason>"; null when nothing is retained from the refundable advances. */
retentionLabel: string | null;
/** The amount retained from the refundable advances; null when zero. */
retention: string | null;
/** Non-refundable advance, retained in full; null when none. */
nonRefundable: string | null;
/** Refunded to you. Non-null whenever anything was paid, even "£0.00". */
refunded: string | null;
/** Total retained (deductions + retention + non-refundable); null when zero. */
retained: string | null;
} arrangements/deposit-cancellation-outcome/table.arrangement.tsx import { Cell, Row, Table, type Yield } from "docxcelerate/template";
import type { DepositCancellationOutcomeLines } from "./lines.ts";
/**
* Each label beside its amount, amounts right-aligned. The default: the
* section is arithmetic the reader checks down a column, paid, less what is
* retained, equals what is refunded.
*/
export const table = (lines: DepositCancellationOutcomeLines): Yield => (
<>
{lines.paid && (
/* Amounts get what a figure like £1,284,750.00 needs; descriptions, which run long, get the rest. */
<Table id="deposit-cancellation-amounts" columns={[{ width: "auto" }, { width: 40, align: "right" }]}>
<Row id="deposit-cancellation-paid">
<Cell>Paid in advance</Cell>
<Cell>{lines.paid}</Cell>
</Row>
{lines.security && (
<Row id="deposit-cancellation-security">
<Cell>Security deposit</Cell>
<Cell>{lines.security}</Cell>
</Row>
)}
{lines.deductions[0].amount && (
<Row id="deposit-cancellation-deduction-1">
<Cell>{lines.deductions[0].description}</Cell>
<Cell>{lines.deductions[0].amount}</Cell>
</Row>
)}
{lines.deductions[1].amount && (
<Row id="deposit-cancellation-deduction-2">
<Cell>{lines.deductions[1].description}</Cell>
<Cell>{lines.deductions[1].amount}</Cell>
</Row>
)}
{lines.deductions[2].amount && (
<Row id="deposit-cancellation-deduction-3">
<Cell>{lines.deductions[2].description}</Cell>
<Cell>{lines.deductions[2].amount}</Cell>
</Row>
)}
{lines.deductions[3].amount && (
<Row id="deposit-cancellation-deduction-4">
<Cell>{lines.deductions[3].description}</Cell>
<Cell>{lines.deductions[3].amount}</Cell>
</Row>
)}
{lines.deductions[4].amount && (
<Row id="deposit-cancellation-deduction-5">
<Cell>{lines.deductions[4].description}</Cell>
<Cell>{lines.deductions[4].amount}</Cell>
</Row>
)}
{lines.deductions[5].amount && (
<Row id="deposit-cancellation-deduction-6">
<Cell>{lines.deductions[5].description}</Cell>
<Cell>{lines.deductions[5].amount}</Cell>
</Row>
)}
{lines.refundable && (
<Row id="deposit-cancellation-refundable">
<Cell>Refundable advance</Cell>
<Cell>{lines.refundable}</Cell>
</Row>
)}
{lines.retention && (
<Row id="deposit-cancellation-retention">
<Cell>{lines.retentionLabel}</Cell>
<Cell>{lines.retention}</Cell>
</Row>
)}
{lines.nonRefundable && (
<Row id="deposit-cancellation-non-refundable">
<Cell>Non-refundable advance, retained</Cell>
<Cell>{lines.nonRefundable}</Cell>
</Row>
)}
<Row id="deposit-cancellation-refunded">
<Cell>Refunded to you</Cell>
<Cell>{lines.refunded}</Cell>
</Row>
{lines.retained && (
<Row id="deposit-cancellation-retained">
<Cell>Retained</Cell>
<Cell>{lines.retained}</Cell>
</Row>
)}
</Table>
)}
</>
); arrangements/deposit-cancellation-outcome/stacked.arrangement.tsx import { Paragraph, type Yield } from "docxcelerate/template";
import type { DepositCancellationOutcomeLines } from "./lines.ts";
/** One "Label: amount" line under another, down the text column. */
export const stacked = (lines: DepositCancellationOutcomeLines): Yield => (
<>
{lines.paid && (
<>
<Paragraph id="deposit-cancellation-paid">Paid in advance: {lines.paid}</Paragraph>
{lines.security && <Paragraph id="deposit-cancellation-security">Security deposit: {lines.security}</Paragraph>}
{lines.deductions[0].line && <Paragraph id="deposit-cancellation-deduction-1">{lines.deductions[0].line}</Paragraph>}
{lines.deductions[1].line && <Paragraph id="deposit-cancellation-deduction-2">{lines.deductions[1].line}</Paragraph>}
{lines.deductions[2].line && <Paragraph id="deposit-cancellation-deduction-3">{lines.deductions[2].line}</Paragraph>}
{lines.deductions[3].line && <Paragraph id="deposit-cancellation-deduction-4">{lines.deductions[3].line}</Paragraph>}
{lines.deductions[4].line && <Paragraph id="deposit-cancellation-deduction-5">{lines.deductions[4].line}</Paragraph>}
{lines.deductions[5].line && <Paragraph id="deposit-cancellation-deduction-6">{lines.deductions[5].line}</Paragraph>}
{lines.refundable && <Paragraph id="deposit-cancellation-refundable">Refundable advance: {lines.refundable}</Paragraph>}
{lines.retention && (
<Paragraph id="deposit-cancellation-retention">{lines.retentionLabel}: {lines.retention}</Paragraph>
)}
{lines.nonRefundable && (
<Paragraph id="deposit-cancellation-non-refundable">Non-refundable advance, retained: {lines.nonRefundable}</Paragraph>
)}
<Paragraph id="deposit-cancellation-refunded">Refunded to you: {lines.refunded}</Paragraph>
{lines.retained && <Paragraph id="deposit-cancellation-retained">Retained: {lines.retained}</Paragraph>}
</>
)}
</>
);