Zum Inhalt springen

← Registry

Invoice totals

Body

The arithmetic every invoice gets subtly wrong, done once in a deriver. Money is worked in integer minor units at the currency's own exponent, so yen and dinars come out right and no float reaches a total. Each line is rounded once, lines are grouped by rate, and VAT is worked once per band on the band's sum. That is rate-level rounding, which HMRC and the EU accept, and it is the figure an accounting system recomputes; per-line VAT summed drifts a penny from it. Inclusive prices have their VAT backed out of the band's gross, so the total is exactly what was charged. Discounts reduce their own band. Exempt lines print under their own label rather than as 0%, and more than one band draws the per-rate VAT analysis an invoice is required to carry.

npx dxcl add invoice-totals
exportiert InvoiceTotals · löst zu 1 Nodes auf
invoice.pricing

Vorschaudaten

das JSON, mit dem diese Vorschau gebaut wurde
{
  "invoice": {
    "currency": "GBP",
    "pricing": "exclusive",
    "lines": [
      {
        "description": "Quarterly planned preventive maintenance of rooftop air handling units AHU-1 to AHU-6, including filter replacement and belt tensioning",
        "quantity": 4,
        "unit": "visits",
        "unitPrice": 12850,
        "taxRate": 20
      },
      {
        "description": "Out-of-hours emergency call-out, chilled water pump failure at Riverside House, 14–15 March",
        "quantity": 7.25,
        "unit": "h",
        "unitPrice": 142.5,
        "taxRate": 20
      },
      {
        "description": "Replacement variable-speed drive and mounting kit for pump P-03",
        "quantity": 1,
        "unitPrice": 3487.6,
        "taxRate": 20
      },
      {
        "description": "Communal heating fuel and power, March (reduced rate)",
        "quantity": 18432.5,
        "unit": "kWh",
        "unitPrice": 0.2847,
        "taxRate": 5
      },
      {
        "description": "Printed site logbooks and operating manuals",
        "quantity": 6,
        "unit": "copies",
        "unitPrice": 48.5,
        "taxRate": 0
      },
      {
        "description": "Plant breakdown insurance premium recharge, 2026–27",
        "quantity": 1,
        "unitPrice": 2310,
        "taxRate": null,
        "taxLabel": "Exempt"
      },
      {
        "description": "Loyalty discount on planned maintenance",
        "quantity": 1,
        "unitPrice": -2570,
        "taxRate": 20
      }
    ]
  }
}

Gerenderte Vorschau

die Komponente für sich gerendert
Von Docxcelerate beim Build gerendert Open ↗

Aufgelöste Nodes

was ein Renderer erhält — 1 Nodes
invoice-totals section

Die Dateien

was die Installation schreibt; die gewählte Datei ist die Komponente
your-project/
nodes/invoice-totals.node.tsx
import { Cell, Paragraph, Row, Section, type Section as SectionComponent, Table, useDeriver, useState } from "docxcelerate/template";
import invoiceTotals from "../derivers/invoice-totals.deriver.ts";
import type { InvoiceTotalsData } from "../derivers/invoice-totals.deriver.ts";

/**
 * An invoice's line table and the totals beneath it, from one calculation.
 *
 * The figures are worked once, in the deriver, so the page agrees with the
 * customer's accounting system to the penny. Money is integer minor units at
 * the currency's own exponent. A line is rounded once, lines are grouped by
 * rate, and VAT is worked once per band on the band's sum (rate-level rounding,
 * which HMRC and the EU accept); VAT rounded per line and summed drifts a penny
 * from that. Inclusive prices have their VAT backed out of the band's gross, so
 * the total is exactly what was charged. A discount reduces its own band and is
 * never pooled across rates. An exempt band prints under its own label, never
 * as 0%, because zero-rated is a different treatment. More than one band draws
 * the per-rate analysis an invoice is required to carry, and an invoice with
 * no lines says there are no charges rather than drawing a zero-total table.
 *
 * The node does no arithmetic: arithmetic on request data throws at publish, so
 * it only interpolates, loops and branches on what the deriver returned.
 *
 * Installed by `dxcl add invoice-totals`.
 */

export type { InvoiceTotalsData } from "../derivers/invoice-totals.deriver.ts";

export const InvoiceTotals: SectionComponent = async () => {
  const [invoice] = useState((d: InvoiceTotalsData) => d.invoice);
  const totals = await useDeriver(invoiceTotals, [invoice]);

  return (
    <Section id="invoice-totals" title="Charges">
      {totals.empty && <Paragraph id="invoice-totals-none">There are no charges on this invoice.</Paragraph>}
      {!totals.empty && (
        <Table
          id="invoice-totals-lines"
          columns={[
            { width: "auto" },
            { width: 22, align: "right" },
            { width: 26, align: "right" },
            { width: 18, align: "right" },
            { width: 28, align: "right" },
          ]}
        >
          <Row header>
            <Cell>Description</Cell>
            <Cell>Qty</Cell>
            <Cell>Unit price</Cell>
            <Cell>VAT</Cell>
            <Cell>{totals.amountHeading}</Cell>
          </Row>
          {totals.lines.map((line) => (
            <Row>
              <Cell>{line.description}</Cell>
              <Cell>{line.quantity}</Cell>
              <Cell>{line.unitPrice}</Cell>
              <Cell>{line.tax}</Cell>
              <Cell>{line.amount}</Cell>
            </Row>
          ))}
        </Table>
      )}
      {totals.multipleBands && (
        <Table
          id="invoice-totals-analysis"
          columns={[{ width: "auto" }, { width: 34, align: "right" }, { width: 34, align: "right" }]}
        >
          <Row header>
            <Cell>Rate</Cell>
            <Cell>Net</Cell>
            <Cell>VAT</Cell>
          </Row>
          {totals.bands.map((band) => (
            <Row>
              <Cell>{band.label}</Cell>
              <Cell>{band.net}</Cell>
              <Cell>{band.vat}</Cell>
            </Row>
          ))}
        </Table>
      )}
      {!totals.empty && (
        <Table id="invoice-totals-summary" columns={[{ width: "auto" }, { width: 40, align: "right" }]}>
          <Row id="invoice-totals-subtotal">
            <Cell>{totals.subtotalLabel}</Cell>
            <Cell>{totals.subtotal}</Cell>
          </Row>
          <Row id="invoice-totals-vat">
            <Cell>{totals.vatLabel}</Cell>
            <Cell>{totals.vat}</Cell>
          </Row>
          <Row id="invoice-totals-total">
            <Cell>Total</Cell>
            <Cell>{totals.total}</Cell>
          </Row>
        </Table>
      )}
    </Section>
  );
};