Prompt payment discount terms
ClosingSince April 2015 UK VAT is charged on the full price, so this never recomputes the invoice's VAT on the discounted price and never takes the discount off the gross total. A UK invoice gets, per band, the discounted net, the VAT on it rounded once per band, and what is payable if paid early, followed by HMRC's wording when no credit note will be issued, without which the supplier has to issue one. A German invoice states the Skonto with a fixed date and, on a mixed-rate invoice, the reduction per rate, split to the cent by largest remainder; the invoice's own VAT stays on the full price. Zero-rated, exempt and reverse-charge bands have their net reduced and their VAT left at nothing. A US invoice gets the usual '2/10, net 30' and the early-payment amount, with sales tax on the full or discounted price looked up per state, never assumed. No discount prints nothing; a discount deadline after the due date fails the document. All of it is one deriver.
npx dxcl add prompt-payment-discount invoice.jurisdiction Данные предпросмотра
JSON, на котором построен этот предпросмотр{
"invoice": {
"issueDate": "2026-11-30",
"currency": "GBP",
"jurisdiction": "UK",
"dueDays": 30,
"bands": [
{
"rate": 20,
"treatment": "standard",
"net": 1284750,
"vat": 256950
},
{
"rate": 5,
"treatment": "reduced",
"net": 48215.4,
"vat": 2410.77
},
{
"rate": 0,
"treatment": "zero",
"net": 12480,
"vat": 0
},
{
"rate": null,
"treatment": "reverse-charge",
"net": 96300,
"vat": 0
}
],
"promptPaymentDiscount": {
"percent": 2.5,
"days": 14,
"issuesCreditNote": false
}
}
} {
"invoice": {
"issueDate": "2026-11-30",
"currency": "GBP",
"jurisdiction": "EU",
"dueDays": 30,
"bands": [
{
"rate": 20,
"treatment": "standard",
"net": 1284750,
"vat": 256950
},
{
"rate": 5,
"treatment": "reduced",
"net": 48215.4,
"vat": 2410.77
},
{
"rate": 0,
"treatment": "zero",
"net": 12480,
"vat": 0
},
{
"rate": null,
"treatment": "reverse-charge",
"net": 96300,
"vat": 0
}
],
"promptPaymentDiscount": {
"percent": 2.5,
"days": 14,
"issuesCreditNote": false
}
}
} {
"invoice": {
"issueDate": "2026-11-30",
"currency": "GBP",
"jurisdiction": "DE",
"dueDays": 30,
"bands": [
{
"rate": 20,
"treatment": "standard",
"net": 1284750,
"vat": 256950
},
{
"rate": 5,
"treatment": "reduced",
"net": 48215.4,
"vat": 2410.77
},
{
"rate": 0,
"treatment": "zero",
"net": 12480,
"vat": 0
},
{
"rate": null,
"treatment": "reverse-charge",
"net": 96300,
"vat": 0
}
],
"promptPaymentDiscount": {
"percent": 2.5,
"days": 14,
"issuesCreditNote": false
}
}
} {
"invoice": {
"issueDate": "2026-11-30",
"currency": "GBP",
"jurisdiction": "FR",
"dueDays": 30,
"bands": [
{
"rate": 20,
"treatment": "standard",
"net": 1284750,
"vat": 256950
},
{
"rate": 5,
"treatment": "reduced",
"net": 48215.4,
"vat": 2410.77
},
{
"rate": 0,
"treatment": "zero",
"net": 12480,
"vat": 0
},
{
"rate": null,
"treatment": "reverse-charge",
"net": 96300,
"vat": 0
}
],
"promptPaymentDiscount": {
"percent": 2.5,
"days": 14,
"issuesCreditNote": false
}
}
} Разрешённые узлы
что получает рендерер — узлов: 1prompt-payment-discount section prompt-payment-discount section prompt-payment-discount section prompt-payment-discount section Файлы
что записывает установка; выбранный файл и есть компонентnodes/prompt-payment-discount.node.tsx import { Cell, type Nodes, Paragraph, Row, Section, Table, useDeriver, useState } from "docxcelerate/template";
import promptPaymentDiscount from "../derivers/prompt-payment-discount.deriver.ts";
/**
* The early-payment terms block of an invoice, and the figures behind it,
* worked per VAT band under the rule of the invoice's jurisdiction.
*
* The decision already made for you: VAT is never simply recomputed on the
* discounted price, and the discount is never taken off the gross total.
*
* - UK, from 1 April 2015: VAT is charged on the full price. Per band the
* block shows the discounted net, the VAT on it (rounded once per band) and
* the amount payable if paid early. It then prints HMRC's wording when no
* credit note will be issued. That sentence matters: without it the
* supplier has to issue a credit note. `issuesCreditNote` defaults to false
* because printing it carries no obligation the seller can forget.
* - Germany (Skonto, UStG s.14(4) no.7): the reduction and a fixed date are
* stated and the invoice's VAT stays on the full price. On a mixed-rate
* invoice the reduction is split per rate by largest remainder, because
* s.17 corrects VAT per rate. EU and FR follow the same rule.
* - Zero-rated, exempt and reverse-charge bands: the discount reduces the net
* only. Their VAT stays 0.
* - US: the terms are printed in the usual shorthand ("2/10, net 30"). Whether
* sales tax on the early-payment amount is on the full or the discounted
* price is looked up per state in a table in the deriver (NY, MA, CA, TX),
* never assumed. An unknown state throws until the owner adds it.
*
* No discount, no deadline or a deadline before the issue date prints
* nothing. A deadline after the due date fails the document.
*
* All of it is one deriver, because every figure is 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 band rows are six 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 rows would publish and then
* resolve to no rows; conditions and `{{derived.…}}` tokens do honour the
* derived scope.
*
* The node publishes 9 branches against the default `branchLimit` of 32: the
* section, the statement, the table and six rows.
*
* Installed by `dxcl add prompt-payment-discount`, which also installs its
* deriver.
*/
/** What this component reads. Add these fields to your document data type. */
export interface PromptPaymentDiscountData {
invoice: {
/**
* The date of issue, ISO 8601 (`2026-11-30`). The discount deadline is
* counted from it. Anything after the tenth character is ignored.
* Required when a discount applies: the deriver throws when it is missing
* or does not start with YYYY-MM-DD. Not read when there is no discount.
*/
issueDate: string;
/**
* ISO 4217 code the amounts are in: "GBP", "EUR", "USD". It also sets the
* minor units (two for GBP, none for JPY). Required when a discount
* applies: missing, or a code Intl rejects, throws.
*/
currency: string;
/**
* Whose rules apply. `UK`: VAT stays on the full price, per-band figures
* and HMRC's wording. `DE`: Skonto line in German, per-rate split when
* there is more than one band. `EU` (any member state without rules of
* its own here) and `FR`: the same rule as DE, in English and in French.
* `US`: shorthand terms, sales tax per `usState`. Required when a
* discount applies: anything else throws.
*/
jurisdiction: "UK" | "EU" | "DE" | "FR" | "US";
/**
* US only: the two-letter state code ("NY"), trimmed and upper-cased
* before lookup. It decides whether sales tax on the early-payment amount
* is on the full or the discounted price. Required for US: a missing code,
* or a state not in the deriver's table, throws. The table is meant to be
* extended by the owner. Ignored outside the US.
*/
usState?: string;
/**
* The invoice's payment term in days ("net 30" is 30). Used to reject a
* discount deadline after the due date, and printed in the US shorthand.
* Missing means no check, and the shorthand prints "2/10" with no ", net N".
* Present but not a positive integer throws.
*/
dueDays?: number;
/**
* The invoice's VAT analysis, one entry per rate and treatment, in the
* order the invoice prints them. Rows print in this order. Required and
* non-empty when a discount applies: missing or empty throws. More than
* six bands with a non-zero net throws (six fixed slots).
*/
bands: Array<{
/**
* The rate as a percentage: 20, 5, 8.875. At most 4 decimal places.
* Required for `standard` and `reduced`: missing, ≤ 0, ≥ 100, not
* finite or more than 4 decimal places throws. Ignored for the other
* treatments, which are labelled by treatment.
*/
rate: number | null;
/**
* How the band is taxed. `zero`, `exempt` and `reverse-charge` have
* their net reduced and their VAT left at 0. On a US invoice only
* `standard`/`reduced` bands carry sales tax. Anything outside the
* union throws. Two bands with the same treatment (and, for
* standard/reduced, the same rate) throw.
*/
treatment: "standard" | "reduced" | "zero" | "exempt" | "reverse-charge";
/**
* The band's net at full price, in major units, as the invoice prints
* it. No more decimals than the currency allows: more throws. Negative
* or not finite throws. 0 is allowed and prints no row.
*/
net: number;
/**
* The VAT (on a US invoice: the sales tax) charged on the full-price
* net, in major units, as printed. Read by DE/EU/FR, where the band's
* gross is net + vat, and by US states that tax the full price. UK
* recomputes from `rate` instead. Same number rules as `net`. Non-zero
* on a zero, exempt or reverse-charge band throws.
*/
vat: number;
}>;
/**
* The early-payment terms. Missing means no discount: the component
* prints nothing and checks nothing else.
*/
promptPaymentDiscount?: {
/**
* The discount as a percentage of the full price: 2, 2.5. Missing,
* null or 0 prints nothing. Negative, ≥ 100, not finite or more than
* 2 decimal places throws.
*/
percent: number;
/**
* Days after `issueDate` within which payment earns the discount. The
* deadline is `issueDate` + `days`, printed as a date. Missing or null
* prints nothing (no deadline). 0 or negative prints nothing (a
* deadline on or before the issue date is not an early-payment term).
* Not an integer throws. Greater than `dueDays` throws.
*/
days: number;
/**
* UK only: whether the seller issues a credit note when the discount is
* taken. Missing or false prints HMRC's "No credit note will be
* issued…" wording, which makes the invoice enough on its own. True
* prints a sentence promising a credit note instead. Ignored outside
* the UK.
*/
issuesCreditNote?: boolean;
};
};
}
/**
* Prints the early-payment terms and, where the jurisdiction needs them, the
* figures per band. Prints nothing when there is no discount to state.
*/
export const PromptPaymentDiscount: Nodes = async () => {
const [invoice] = useState((d: PromptPaymentDiscountData) => d.invoice);
const d = await useDeriver(promptPaymentDiscount, [invoice]);
return (
<>
{d.show && (
<Section id="prompt-payment-discount" title={d.heading}>
<Paragraph id="prompt-payment-discount-terms">{d.terms}</Paragraph>
{d.statement && <Paragraph id="prompt-payment-discount-statement">{d.statement}</Paragraph>}
{d.showTable && (
<Table
id="prompt-payment-discount-figures"
columns={[{ width: "auto" }, { width: 34, align: "right" }, { width: 34, align: "right" }, { width: 34, align: "right" }]}
>
<Row id="prompt-payment-discount-figures-header" header>
<Cell>{d.columns.rate}</Cell>
<Cell>{d.columns.first}</Cell>
<Cell>{d.columns.second}</Cell>
<Cell>{d.columns.third}</Cell>
</Row>
{d.rows[0].label && (
<Row id="prompt-payment-discount-band-1">
<Cell>{d.rows[0].label}</Cell>
<Cell>{d.rows[0].first}</Cell>
<Cell>{d.rows[0].second}</Cell>
<Cell>{d.rows[0].third}</Cell>
</Row>
)}
{d.rows[1].label && (
<Row id="prompt-payment-discount-band-2">
<Cell>{d.rows[1].label}</Cell>
<Cell>{d.rows[1].first}</Cell>
<Cell>{d.rows[1].second}</Cell>
<Cell>{d.rows[1].third}</Cell>
</Row>
)}
{d.rows[2].label && (
<Row id="prompt-payment-discount-band-3">
<Cell>{d.rows[2].label}</Cell>
<Cell>{d.rows[2].first}</Cell>
<Cell>{d.rows[2].second}</Cell>
<Cell>{d.rows[2].third}</Cell>
</Row>
)}
{d.rows[3].label && (
<Row id="prompt-payment-discount-band-4">
<Cell>{d.rows[3].label}</Cell>
<Cell>{d.rows[3].first}</Cell>
<Cell>{d.rows[3].second}</Cell>
<Cell>{d.rows[3].third}</Cell>
</Row>
)}
{d.rows[4].label && (
<Row id="prompt-payment-discount-band-5">
<Cell>{d.rows[4].label}</Cell>
<Cell>{d.rows[4].first}</Cell>
<Cell>{d.rows[4].second}</Cell>
<Cell>{d.rows[4].third}</Cell>
</Row>
)}
{d.rows[5].label && (
<Row id="prompt-payment-discount-band-6">
<Cell>{d.rows[5].label}</Cell>
<Cell>{d.rows[5].first}</Cell>
<Cell>{d.rows[5].second}</Cell>
<Cell>{d.rows[5].third}</Cell>
</Row>
)}
</Table>
)}
<Paragraph id="prompt-payment-discount-payable">{d.payable}</Paragraph>
</Section>
)}
</>
);
}; derivers/prompt-payment-discount.deriver.ts // deno-lint-ignore-file no-explicit-any
import { deriver } from "docxcelerate/template";
/** What `promptPaymentDiscount` returns: every string the block prints, already chosen. */
export interface PromptPaymentDiscountResult {
/** False means print nothing. */
show: boolean;
/** The section title. */
heading: string;
/** The terms sentence, always printed when `show`. */
terms: string;
/** The UK credit-note sentence; empty everywhere else. */
statement: string;
/** Whether the per-band figures table is printed. */
showTable: boolean;
/** The table's column titles. */
columns: { rate: string; first: string; second: string; third: string };
/** Exactly six rows; unused slots are all empty strings. */
rows: Array<{ label: string; first: string; second: string; third: string }>;
/** The closing sentence with the early-payment amount. */
payable: string;
/** The early-payment amount in minor units, for tests. */
payableMinor: number;
/** The discount deadline, ISO YYYY-MM-DD; empty when nothing is shown. */
deadline: string;
}
/**
* Works the early-payment terms of an invoice per VAT band under its
* jurisdiction's rule. Installed by `dxcl add prompt-payment-discount`.
*
* 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 table is declared inside `run`.
*/
export default deriver({
name: "promptPaymentDiscount",
run: (invoiceInput: unknown): PromptPaymentDiscountResult => {
const invoice = invoiceInput as any;
const slot = () => ({ label: "", first: "", second: "", third: "" });
const none = (): PromptPaymentDiscountResult => ({
show: false,
heading: "",
terms: "",
statement: "",
showTable: false,
columns: { rate: "", first: "", second: "", third: "" },
rows: [slot(), slot(), slot(), slot(), slot(), slot()],
payable: "",
payableMinor: 0,
deadline: "",
});
// Decisions that need nothing else come first: an invoice with no discount
// must never throw, whatever else is missing from it.
const terms = invoice?.promptPaymentDiscount;
const percent = terms?.percent;
if (percent === undefined || percent === null || percent === 0) return none();
if (typeof percent !== "number" || !Number.isFinite(percent) || percent < 0 || percent >= 100) {
throw new Error("invoice.promptPaymentDiscount.percent must be at least 0 and under 100");
}
if (Math.abs(percent * 100 - Math.round(percent * 100)) > 1e-6) {
throw new Error("invoice.promptPaymentDiscount.percent has more than 2 decimal places");
}
const days = terms.days;
if (days === undefined || days === null) return none();
if (typeof days !== "number" || !Number.isInteger(days)) {
throw new Error("invoice.promptPaymentDiscount.days must be a whole number of days");
}
if (days <= 0) return none();
const issue = invoice.issueDate;
if (typeof issue !== "string" || !/^\d{4}-\d{2}-\d{2}/.test(issue)) {
throw new Error("invoice.issueDate must be an ISO 8601 date");
}
const [y, m, d] = issue.slice(0, 10).split("-").map(Number);
const issueMs = Date.UTC(y, m - 1, d);
if (new Date(issueMs).toISOString().slice(0, 10) !== issue.slice(0, 10)) {
throw new Error("invoice.issueDate must be an ISO 8601 date");
}
const currency = invoice.currency;
let exponent: number;
try {
// Intl accepts any three letters, so a code it does not list is rejected here.
if (!(Intl as any).supportedValuesOf("currency").includes(currency)) throw new Error();
exponent = new Intl.NumberFormat("en-GB", { style: "currency", currency }).resolvedOptions()
.maximumFractionDigits as number;
} catch {
throw new Error("invoice.currency must be an ISO 4217 code");
}
const jurisdiction = invoice.jurisdiction;
if (!["UK", "EU", "DE", "FR", "US"].includes(jurisdiction)) {
throw new Error('invoice.jurisdiction must be "UK", "EU", "DE", "FR" or "US"');
}
// Sales tax on the early-payment amount, per state. The table is the
// owner's to extend; an unlisted state throws rather than being guessed.
const usStates: Record<string, "full" | "discounted"> = {
// New York: a prompt-payment discount does not reduce the tax base (NY Tax Law s.1101(b)(3)).
NY: "full",
// Massachusetts: sales tax is on the price before a prompt-payment discount (830 CMR 64H.1.1).
MA: "full",
// California: the measure is reduced when the discount is taken (Regulation 1655); the amount shown assumes it is.
CA: "discounted",
// Texas: a true cash discount reduces the sales price (34 TAC s.3.291).
TX: "discounted",
};
let usRule: "full" | "discounted" = "full";
if (jurisdiction === "US") {
const state = typeof invoice.usState === "string" ? invoice.usState.trim().toUpperCase() : "";
if (state === "") throw new Error("invoice.usState is required for a US invoice");
if (!Object.prototype.hasOwnProperty.call(usStates, state)) {
throw new Error(
"invoice.usState " + state + " is not in the prompt-payment sales-tax table; add it to the deriver",
);
}
usRule = usStates[state];
}
const dueDays = invoice.dueDays;
if (dueDays !== undefined && dueDays !== null && (typeof dueDays !== "number" || !Number.isInteger(dueDays) || dueDays <= 0)) {
throw new Error("invoice.dueDays must be a positive whole number");
}
const toMinor = (value: unknown, field: string): number => {
if (typeof value !== "number" || !Number.isFinite(value) || value < 0) {
throw new Error(field + " must be a finite number of 0 or more");
}
const scaled = value * 10 ** exponent;
if (Math.round(scaled) / 10 ** exponent !== value) {
throw new Error(field + " has more decimals than " + currency + " allows");
}
return Math.round(scaled);
};
const bands = invoice.bands;
if (!Array.isArray(bands) || bands.length === 0) throw new Error("invoice.bands must not be empty");
const treatments = ["standard", "reduced", "zero", "exempt", "reverse-charge"];
const seen = new Set<string>();
const all = bands.map((b: any, i: number) => {
const at = "invoice.bands[" + i + "]";
if (!treatments.includes(b?.treatment)) throw new Error(at + ".treatment is not a known treatment");
const taxed = b.treatment === "standard" || b.treatment === "reduced";
let rate4 = 0;
if (taxed) {
const r = b.rate;
if (typeof r !== "number" || !Number.isFinite(r) || r <= 0 || r >= 100) {
throw new Error(at + ".rate must be above 0 and under 100");
}
if (Math.abs(r * 10000 - Math.round(r * 10000)) > 1e-6) {
throw new Error(at + ".rate has more than 4 decimal places");
}
rate4 = Math.round(r * 10000);
}
const key = taxed ? b.treatment + ":" + rate4 : b.treatment;
if (seen.has(key)) throw new Error(at + " repeats an earlier band with the same treatment and rate");
seen.add(key);
const net = toMinor(b.net, at + ".net");
const vat = toMinor(b.vat, at + ".vat");
if (!taxed && vat !== 0) throw new Error(at + ".vat must be 0 on a " + b.treatment + " band");
return { treatment: b.treatment as string, taxed, rate: b.rate as number, rate4, net, vat };
});
if (dueDays !== undefined && dueDays !== null && days > dueDays) {
throw new Error(
"invoice.promptPaymentDiscount.days (" + days + ") is after the due date (dueDays " + dueDays + ")",
);
}
const active = all.filter((b) => b.net > 0);
if (active.length === 0) return none();
if (active.length > 6) throw new Error("invoice.bands has more than 6 bands with a net; there are 6 slots");
// Products can pass 2^53 (a 1bn net times a rate scaled by 10,000), so they
// are BigInt. Amounts are never negative, so half up is half away from zero.
const mulDivHalfUp = (a: number, b: number, den: number): number => {
const n = BigInt(a) * BigInt(b);
const D = BigInt(den);
const q = n / D;
return Number((n % D) * BigInt(2) >= D ? q + BigInt(1) : q);
};
const pctBp = Math.round(percent * 100);
const locale = { UK: "en-GB", EU: "en-GB", DE: "de-DE", FR: "fr-FR", US: "en-US" }[jurisdiction as string] as string;
const money = new Intl.NumberFormat(locale, { style: "currency", currency });
const fmt = (minor: number) => money.format(minor / 10 ** exponent);
const pct = new Intl.NumberFormat(locale, { maximumFractionDigits: 2 }).format(percent);
const rateFmt = new Intl.NumberFormat(locale, { maximumFractionDigits: 4 });
const spaced = jurisdiction === "DE" || jurisdiction === "FR";
const pctSign = spaced ? " %" : "%";
const exemptLabel = jurisdiction === "DE" ? "steuerfrei" : jurisdiction === "FR" ? "Exonéré" : "Exempt";
const reverseLabel =
jurisdiction === "DE" ? "Reverse-Charge" : jurisdiction === "FR" ? "Autoliquidation" : "Reverse charge";
const labelOf = (b: { treatment: string; rate: number }) =>
b.treatment === "standard" || b.treatment === "reduced"
? rateFmt.format(b.rate) + pctSign
: b.treatment === "zero"
? "0" + pctSign
: b.treatment === "exempt"
? exemptLabel
: reverseLabel;
const deadlineMs = issueMs + days * 86_400_000;
const deadline = new Date(deadlineMs).toISOString().slice(0, 10);
const dateOptions: Intl.DateTimeFormatOptions =
jurisdiction === "DE"
? { day: "2-digit", month: "2-digit", year: "numeric", timeZone: "UTC" }
: jurisdiction === "FR"
? { timeZone: "UTC" }
: { dateStyle: "long", timeZone: "UTC" };
const deadlineText = new Intl.DateTimeFormat(locale, dateOptions).format(new Date(deadlineMs));
let payableMinor = 0;
let showTable = false;
let statement = "";
const lines: Array<{ label: string; first: string; second: string; third: string }> = [];
const result = none();
if (jurisdiction === "UK") {
showTable = true;
for (const b of active) {
const discountedNet = b.net - mulDivHalfUp(b.net, pctBp, 10000);
// One rounding per band, never per line, and never for an untaxed band.
const vat = b.taxed ? mulDivHalfUp(discountedNet, b.rate4, 1_000_000) : 0;
payableMinor += discountedNet + vat;
lines.push({ label: labelOf(b), first: fmt(discountedNet), second: fmt(vat), third: fmt(discountedNet + vat) });
}
result.heading = "Prompt payment discount";
result.columns = { rate: "Rate", first: "Net after discount", second: "VAT on discounted net", third: "Payable if paid early" };
result.terms =
"A discount of " + pct + "% of the full price applies if payment is made within " + days +
" days of the invoice date.";
statement = terms.issuesCreditNote === true
? "If payment is made within " + days + " days, we will issue a credit note for the discount and the VAT on it."
: "No credit note will be issued. Following payment you must ensure you have only recovered the VAT actually paid.";
result.payable = "Total payable if paid by " + deadlineText + ": " + fmt(payableMinor) + ".";
} else if (jurisdiction === "US") {
let taxMinor = 0;
for (const b of active) {
const discountedNet = b.net - mulDivHalfUp(b.net, pctBp, 10000);
const tax = !b.taxed ? 0 : usRule === "full" ? b.vat : mulDivHalfUp(discountedNet, b.rate4, 1_000_000);
payableMinor += discountedNet + tax;
taxMinor += tax;
}
result.heading = "Early payment discount";
result.terms =
"Terms: " + pct + "/" + days + (typeof dueDays === "number" ? ", net " + dueDays : "") + ". A " + pct +
"% discount applies if payment is made by " + deadlineText + ".";
result.payable = "Amount due if paid by " + deadlineText + ": " + fmt(payableMinor) +
(taxMinor > 0 ? ", including " + fmt(taxMinor) + " sales tax on the " + usRule + " price." : ".");
} else {
// The invoice's own VAT stays on the full price; the reduction is taken
// off each band's gross, split by largest remainder so the cents add up.
const gross = active.map((b) => b.net + b.vat);
const G = gross.reduce((a, b) => a + b, 0);
const totalDiscount = mulDivHalfUp(G, pctBp, 10000);
const products = gross.map((g) => BigInt(totalDiscount) * BigInt(g));
const shares = products.map((p) => Number(p / BigInt(G)));
let leftover = totalDiscount - shares.reduce((a, b) => a + b, 0);
const order = products
.map((p, i) => ({ i, rem: p % BigInt(G) }))
.sort((a, b) => (a.rem === b.rem ? a.i - b.i : a.rem > b.rem ? -1 : 1));
for (const o of order) {
if (leftover <= 0) break;
shares[o.i] += 1;
leftover -= 1;
}
active.forEach((b, i) => {
lines.push({ label: labelOf(b), first: fmt(gross[i]), second: fmt(shares[i]), third: fmt(gross[i] - shares[i]) });
});
payableMinor = G - totalDiscount;
// A single rate needs no split: the terms line is enough.
showTable = active.length > 1;
if (jurisdiction === "DE") {
result.heading = "Skonto";
result.columns = { rate: "Steuersatz", first: "Brutto", second: "Skonto", third: "Brutto nach Skonto" };
result.terms = pct + " % Skonto bei Zahlung bis " + deadlineText + ".";
result.payable = "Zahlbetrag abzüglich Skonto: " + fmt(payableMinor) + ".";
} else if (jurisdiction === "FR") {
result.heading = "Escompte pour paiement anticipé";
result.columns = { rate: "Taux", first: "Montant TTC", second: "Escompte", third: "TTC après escompte" };
result.terms = "Escompte de " + pct + " % pour paiement au plus tard le " + deadlineText + ".";
result.payable = "Net à payer après escompte : " + fmt(payableMinor) + ".";
} else {
result.heading = "Prompt payment discount";
result.columns = { rate: "Rate", first: "Gross", second: "Discount", third: "Gross after discount" };
result.terms = "A discount of " + pct + "% applies if payment is made by " + deadlineText + ".";
result.payable = "Amount payable if paid by " + deadlineText + ": " + fmt(payableMinor) + ".";
}
}
if (!showTable) result.columns = { rate: "", first: "", second: "", third: "" };
result.show = true;
result.statement = statement;
result.showTable = showTable;
result.rows = [0, 1, 2, 3, 4, 5].map((i) => (showTable && lines[i]) || slot());
result.payableMinor = payableMinor;
result.deadline = deadline;
return result;
},
});