Invoice billing periods
OpeningWorks out the periods an invoice states from the billing anchor and interval. Month-end anchors are clamped without being forgotten, so an anchor of the 31st bills on 31 Jan, 28 Feb and 31 Mar, and 29 Feb on an annual plan falls on 28 Feb in a common year. End dates are inclusive, the EN 16931 convention. Licensed items are billed in advance for the coming period and metered items in arrears for the previous one, so one invoice can state two periods; the header period then runs from the earliest start to the latest end, so it contains every line period, as Peppol requires. A first period that starts off the anchor is labelled a part period. Dates are calendar dates in the seller's billing time zone, never UTC. All of it is one deriver.
npx dxcl add invoice-billing-periods billing.interval Voorbeelddata
de JSON waarmee dit voorbeeld is gebouwd{
"billing": {
"anchor": "2025-12-31",
"interval": "week",
"intervalCount": 1,
"invoiceDate": "2027-01-01T02:30:00Z",
"timeZone": "America/Argentina/Buenos_Aires",
"signupDate": "2025-12-14",
"hasAdvanceItems": true,
"hasArrearsItems": true
}
} {
"billing": {
"anchor": "2025-12-31",
"interval": "month",
"intervalCount": 1,
"invoiceDate": "2027-01-01T02:30:00Z",
"timeZone": "America/Argentina/Buenos_Aires",
"signupDate": "2025-12-14",
"hasAdvanceItems": true,
"hasArrearsItems": true
}
} {
"billing": {
"anchor": "2025-12-31",
"interval": "year",
"intervalCount": 1,
"invoiceDate": "2027-01-01T02:30:00Z",
"timeZone": "America/Argentina/Buenos_Aires",
"signupDate": "2025-12-14",
"hasAdvanceItems": true,
"hasArrearsItems": true
}
} Opgeloste nodes
wat een renderer krijgt — 1 nodesbilling-periods section billing-periods section billing-periods section De bestanden
wat installeren schrijft; het gekozen bestand is de componentnodes/invoice-billing-periods.node.tsx import { Paragraph, Section, type Section as SectionComponent, useDeriver, useState } from "docxcelerate/template";
import invoiceBillingPeriods from "../derivers/invoice-billing-periods.deriver.ts";
/**
* The periods a subscription invoice bills for. Calendar arithmetic is easy to
* get nearly right and an invoice cannot be nearly right, so each decision is
* made once and written down here.
*
* Month-end anchors are clamped, but the anchor day is never lost. An anchor of
* the 31st bills on 31 Jan, 28 Feb, then 31 Mar, not 28 Mar: every period is
* counted from the original anchor, never from the previous one. An annual
* anchor of 29 Feb falls on 28 Feb in a common year. `Date.setMonth` turns 31
* Jan into 3 Mar, and code that clamps once stays on the 28th for ever.
*
* End dates are inclusive. A period ends the day before the next anchor,
* because under EN 16931 an end date is the last day of the period.
*
* Licensed items are billed in advance, for the period the invoice opens.
* Metered items are billed in arrears, for the period before it. One invoice
* can state both, so a usage line is never labelled with the subscription's
* period. The header invoicing period runs from the earliest start to the
* latest end, so it contains every line period, as Peppol R110 and R111
* require; a validator rejects the invoice otherwise.
*
* A first period that starts off the anchor is labelled a part period.
*
* Dates are calendar dates in the seller's billing time zone, not UTC
* instants: an invoice raised at 03:00 UTC on 1 Dec is still 30 Nov in New
* York.
*
* None of this arithmetic can run on request data at publish time, so it is
* all one deriver and the node prints what it returns. The deriver also
* returns the ISO dates (`header`, `advance`, `arrears`) for a caller writing
* UBL; the node does not print them.
*
* Installed by `dxcl add invoice-billing-periods`, which also installs its deriver.
*/
/** What this component reads. Add these fields to your document data type. */
export interface InvoiceBillingPeriodsData {
billing: {
/**
* The original billing anchor: the date the cycle counts from, never a
* date that has already been clamped. Its day of month, and its month for
* an annual plan, are kept for every period. An anchor of 31 Jan bills on
* 28 Feb and then 31 Mar. Pass `2026-01-31`, not the `2026-02-28` it last
* fell on. It may come before or after the invoice date; periods are
* counted from it both ways.
*
* Either a calendar date, `YYYY-MM-DD`, taken as it is, or an instant with
* `Z` or an offset, such as `2026-02-01T00:00:00Z`, which is converted to
* a calendar date in `timeZone`. Required. The deriver throws when it is
* missing, is not a real date (for example `2026-02-30`), or is a date-time
* with no offset.
*/
anchor: string;
/**
* The unit the cycle repeats in. Required. Anything else, including
* `"day"`, throws.
*/
interval: "week" | "month" | "year";
/**
* How many units make up one period. 3 with `"month"` is quarterly.
* Missing means 1. Throws when it is given but is not a whole number of at
* least 1 (0, -1 and 1.5 all throw).
*/
intervalCount?: number;
/**
* When the invoice is issued. This decides which period is billed in
* advance (the one containing this date) and which in arrears (the one
* before it). It accepts the same forms as `anchor`, and an instant is
* read in `timeZone`. Required. It throws on the same inputs as `anchor`.
*/
invoiceDate: string;
/**
* The seller's billing time zone, as an IANA name: `"America/New_York"`,
* `"Europe/London"`. Every instant is turned into a calendar date here, so
* a period never prints a day out for the customer. Required, even when
* every date is a plain calendar date: the deriver throws when it is
* missing or is not a zone the runtime knows.
*/
timeZone: string;
/**
* When the subscription started, in the same forms as `anchor`. When it
* falls strictly inside the period being billed, that period starts on
* this date and is labelled a part period. Missing means the subscription
* was running before every period stated, so nothing is a part period.
* Throws when it is after `invoiceDate`. Throws when `hasArrearsItems` is
* true and the subscription had not started by the end of the arrears
* period, because there is no earlier period to bill usage for.
*/
signupDate?: string;
/**
* Whether the invoice carries licensed items, which are billed in advance
* for the coming period. Only `true` counts, so a missing value is false.
* The deriver throws when neither this nor `hasArrearsItems` is true.
*/
hasAdvanceItems: boolean;
/**
* Whether the invoice carries metered items, which are billed in arrears
* for the previous period. Only `true` counts, so a missing value is false.
*/
hasArrearsItems: boolean;
};
}
export const InvoiceBillingPeriods: SectionComponent = async () => {
const [billing] = useState((data: InvoiceBillingPeriodsData) => data.billing);
const periods = await useDeriver(invoiceBillingPeriods, [billing]);
return (
<Section id="billing-periods" title="Billing period">
<Paragraph id="billing-periods-statement">{periods.statement}</Paragraph>
{periods.headerLine && <Paragraph id="billing-periods-header">{periods.headerLine}</Paragraph>}
</Section>
);
}; derivers/invoice-billing-periods.deriver.ts import { deriver } from "docxcelerate/template";
/**
* Works out the periods a subscription invoice bills for. Installed by
* `dxcl add invoice-billing-periods`.
*
* 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 month
* names, the helpers and the validation all live inside `run`.
*/
export default deriver({
name: "invoiceBillingPeriods",
run: (billing: unknown) => {
// deno-lint-ignore no-explicit-any
const b = billing as any;
type Period = { start: string; end: string; part: boolean };
if (typeof b !== "object" || b === null) throw new Error("billing must be an object");
const intervals = ["week", "month", "year"];
if (!intervals.includes(b.interval)) {
throw new Error("billing.interval must be one of " + intervals.join(", "));
}
const interval: string = b.interval;
const count: number = b.intervalCount === undefined || b.intervalCount === null ? 1 : b.intervalCount;
if (!Number.isInteger(count) || count < 1) {
throw new Error("billing.intervalCount must be a whole number of at least 1");
}
const timeZone = b.timeZone;
if (typeof timeZone !== "string" || timeZone === "") {
throw new Error("billing.timeZone is required: an IANA zone such as Europe/London");
}
try {
new Intl.DateTimeFormat("en-US", { timeZone });
} catch {
throw new Error("billing.timeZone is not a time zone this runtime knows: " + timeZone);
}
const advanceItems = b.hasAdvanceItems === true;
const arrearsItems = b.hasArrearsItems === true;
if (!advanceItems && !arrearsItems) {
throw new Error("billing.hasAdvanceItems or billing.hasArrearsItems must be true");
}
// Day arithmetic runs on serial day numbers. UTC is used here only as a
// calendar with no daylight saving, so a day is always exactly 86400000 ms.
// No instant is ever formatted in UTC: instants are turned into calendar
// dates in the billing time zone, below, before they get here.
const serial = (y: number, m: number, d: number) => Date.UTC(y, m - 1, d) / 86400000;
const daysIn = (y: number, m: number) => new Date(Date.UTC(y, m, 0)).getUTCDate();
const parts = (n: number) => {
const dt = new Date(n * 86400000);
return { y: dt.getUTCFullYear(), m: dt.getUTCMonth() + 1, d: dt.getUTCDate() };
};
const iso = (n: number) => {
const p = parts(n);
return String(p.y).padStart(4, "0") + "-" + String(p.m).padStart(2, "0") + "-" + String(p.d).padStart(2, "0");
};
const toDay = (value: unknown, path: string): number => {
if (typeof value !== "string") throw new Error(path + " is required, as a string");
if (/^\d{4}-\d{2}-\d{2}$/.test(value)) {
const y = Number(value.slice(0, 4));
const m = Number(value.slice(5, 7));
const d = Number(value.slice(8, 10));
if (m < 1 || m > 12 || d < 1 || d > daysIn(y, m)) throw new Error(path + " is not a real date: " + value);
return serial(y, m, d);
}
if (/^\d{4}-\d{2}-\d{2}T.+(Z|[+-]\d{2}:?\d{2})$/.test(value)) {
const instant = new Date(value);
if (Number.isNaN(instant.getTime())) throw new Error(path + " is not a real date: " + value);
const got: Record<string, number> = {};
for (
const part of new Intl.DateTimeFormat("en-US", {
timeZone,
year: "numeric",
month: "numeric",
day: "numeric",
}).formatToParts(instant)
) {
got[part.type] = Number(part.value);
}
return serial(got.year, got.month, got.day);
}
// A date-time with no offset would be read in the engine's own zone, which is unknown.
throw new Error(path + " must be YYYY-MM-DD, or a date-time with Z or an offset: " + value);
};
const anchor = toDay(b.anchor, "billing.anchor");
const invoice = toDay(b.invoiceDate, "billing.invoiceDate");
const signup = b.signupDate === undefined || b.signupDate === null ? null : toDay(b.signupDate, "billing.signupDate");
if (signup !== null && signup > invoice) throw new Error("billing.signupDate is after billing.invoiceDate");
const a = parts(anchor);
// The start of period k, for any integer k. Always counted from the
// original anchor, so 31 Mar follows 28 Feb rather than 28 Mar.
const occ = (k: number): number => {
if (interval === "week") return anchor + 7 * count * k;
if (interval === "year") {
const y = a.y + k * count;
return serial(y, a.m, Math.min(a.d, daysIn(y, a.m)));
}
const t = (a.m - 1) + k * count;
const y = a.y + Math.floor(t / 12);
const m = ((t % 12) + 12) % 12 + 1;
return serial(y, m, Math.min(a.d, daysIn(y, m)));
};
const periodIndex = (day: number): number => {
if (interval === "week") return Math.floor((day - anchor) / (7 * count));
const p = parts(day);
const diff = interval === "year" ? p.y - a.y : (p.y * 12 + p.m) - (a.y * 12 + a.m);
let k = Math.floor(diff / count);
while (occ(k) > day) k--;
while (occ(k + 1) <= day) k++;
return k;
};
const periodAt = (k: number): Period => {
const start = occ(k);
const end = occ(k + 1) - 1;
// A subscription that began inside the period is billed from its start.
if (signup !== null && start < signup && signup <= end) {
return { start: iso(signup), end: iso(end), part: true };
}
return { start: iso(start), end: iso(end), part: false };
};
const k = periodIndex(invoice);
const advance = advanceItems ? periodAt(k) : null;
let arrears: Period | null = null;
if (arrearsItems) {
if (signup !== null && signup > occ(k) - 1) {
throw new Error("billing.signupDate leaves no earlier period to bill usage for");
}
arrears = periodAt(k - 1);
}
const stated = [advance, arrears].filter((p): p is Period => p !== null);
const header = {
start: stated.map((p) => p.start).reduce((x, y) => (y < x ? y : x)),
end: stated.map((p) => p.end).reduce((x, y) => (y > x ? y : x)),
};
const months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
const range = (start: string, end: string) => {
const s = { y: Number(start.slice(0, 4)), m: Number(start.slice(5, 7)), d: Number(start.slice(8, 10)) };
const e = { y: Number(end.slice(0, 4)), m: Number(end.slice(5, 7)), d: Number(end.slice(8, 10)) };
const sm = months[s.m - 1];
const em = months[e.m - 1];
if (start === end) return s.d + " " + sm + " " + s.y;
if (s.y === e.y && s.m === e.m) return s.d + "–" + e.d + " " + em + " " + e.y;
if (s.y === e.y) return s.d + " " + sm + " – " + e.d + " " + em + " " + e.y;
return s.d + " " + sm + " " + s.y + " – " + e.d + " " + em + " " + e.y;
};
const say = (label: string, p: Period, how: string) =>
label + " " + range(p.start, p.end) + (p.part ? " (part period)" : "") + ", billed " + how + ".";
const sentences: string[] = [];
if (advance) sentences.push(say("Subscription", advance, "in advance"));
if (arrears) sentences.push(say("Usage", arrears, "in arrears"));
return {
statement: sentences.join(" "),
// With one period the header would only repeat the statement.
headerLine: advance && arrears ? "Invoicing period: " + range(header.start, header.end) + "." : null,
header,
advance,
arrears,
};
},
});