feat: render billing usage pdf

Add the immutable PDF renderer for "Nutzungsnachweis" billing reports.

- src/modules/billing/types.ts: BillingReportSnapshot self-contained type
  (address, selected positions, repeat downloads, net total in cents,
  report/invoice metadata, period, document ID, legal notice).
- src/modules/pdf/document.ts: pdfmake document definition builder with
  monochrome invoice layout (black logo, sender/recipient blocks, rounded
  black summary panel, repeated black table headers, light-gray rows,
  German dates, cents-derived totals, page numbers, document ID, invoice
  reference, legal separation notice).
- src/modules/pdf/render.ts: renderBillingPdf → { bytes, sha256 } using
  pdfmake 0.3 with IBM Plex Mono (OFL) embedded via the VFS.
- scripts/render-pdf-fixture.ts: stress fixture generator (1 row, 60 rows,
  long address, long fields, umlauts, 3-page case) into tmp/pdfs/.
- tests/fixtures/billing-report.ts: deterministic reportFixture (60,00 EUR).
- tsconfig.json: server-only path alias for tsx script execution.

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Leon TonhäuserandClaude committed 2026-08-10 17:54:19 +02:00
1 parent d37affc80f
commit 89d76e2711
13 files changed
+1265 -1

No files matched your search

+75
View File
@@ -0,0 +1,75 @@
import type { BillingPeriod } from "./period";
/**
* Recipient address for a billing report, mirroring the editable fields of
* `billingAddressVersions`. The address is snapshotted at report creation time
* so the PDF remains immutable even if the editorial account's address changes
* later.
*/
export type BillingReportAddress = {
companyName: string;
contactName: string;
addressLine1: string;
addressLine2: string | null;
postalCode: string;
city: string;
countryCode: string;
};
/**
* A billable usage position included in the report. Corresponds to a
* `BillingPosition` from the billing queries module but is duplicated here so
* the snapshot type does not pull in the database layer.
*/
export type BillingReportPosition = {
id: string;
assetId: string;
filename: string;
albumName: string;
purpose: string;
reference: string | null;
priceCents: number;
createdAt: Date;
};
/**
* A repeat-download position that was authorised during the period but not
* billed again. Corresponds to a `RepeatDownloadPosition` from the billing
* queries module.
*/
export type BillingReportRepeatDownload = {
id: string;
assetId: string;
usageId: string;
authorizedAt: Date;
repeatDownloadNote: string | null;
};
/**
* Immutable snapshot of everything the PDF renderer needs to produce a
* "Nutzungsnachweis" document. Task 12 builds this from database rows and
* passes it to `renderBillingPdf`. The renderer never reads the live database
* or Immich — it accepts only this self-contained value.
*/
export type BillingReportSnapshot = {
/** Recipient address block. */
address: BillingReportAddress;
/** Selected, paid billing positions for the period. */
selectedPositions: BillingReportPosition[];
/** Repeat downloads authorised during the period (not re-billed). */
repeatDownloads: BillingReportRepeatDownload[];
/** Net total in integer Euro cents (sum of selected position prices). */
netTotalCents: number;
/** Sequential report number. */
reportNumber: number;
/** Human-readable invoice reference. */
invoiceNumber: string;
/** Half-open UTC billing period. */
period: BillingPeriod;
/** Unique document identifier (UUID). */
documentId: string;
/** Moment the report was created (UTC). */
createdAt: Date;
/** Legal notice clarifying that the report is not a VAT invoice. */
legalSeparationNotice: string;
};