# Business paper — invoices, quotes and reports to print

> Invoices, quotes, estimates, receipts, statements and monthly reports printed from the app's own records (jobs, clients, orders): one page per record with a letterhead, a for block, line items, subtotal, tax and total computed in plain code, a document number and date, and a Print or save as PDF button whose print style shows only the pages

Source: https://good.vibes.diy/docs/reference/business-paper

**Each record prints as its own page: a letterhead, who it is for, the line items, the totals, a notes line, a number and a date.**
The app keeps its own screens; a **Print or save as PDF** button calls `window.print()`, and a print style shows only the pages.
**The paper's classes and its print style live in one `SKIN` string rendered inside a `<div data-reskin>` wrapper around the Print view**: that wrapper is where the look's design check reads a `<style>` and its class names as the app's own.

Reach for it when someone who keeps jobs, clients or orders in an app asks for paper from those
records: "make this month's invoices", "print this quote", an estimate, a receipt, a statement, a
monthly report to send or file. The paper reads the records the app already keeps, in the app's own
field names. On a change turn, add a Print view to the app as it is; the jobs, clients and forms stay.

## The page

One `.paper-sheet` per record, inside one `.paper-stack`. On screen the sheets sit in the app like
sheets of paper (a fixed page width, a shadow); in print each one is a page. The Print view is one
`data-reskin` wrapper with a `SKIN` string rendered as its first child, the keepsake's technique, so
the paper's classes and its print style live in that one string and the rest of the app is untouched.
The sheets are plain elements (`article`, `div`, `table`); the kit's `Field`, `Input`, `Segmented`
and `Button` are the controls above them.

- **Letterhead:** the business name large, one contact line under it (phone, email, address,
  website). Kept in one settings document (`_id: "letterhead"`) with the tax rate and the notes line,
  edited in `Field`s on the Print view, so every page carries it.
- **Title, number and date** on the right of the letterhead: "Invoice", "Quote", "Estimate",
  "Receipt" or "Statement", the document number, the date.
- **The "for" block:** the client's name and whatever the record holds for reaching them.
- **Line items** as a `table`: description, quantity, unit price, line total, numbers right-aligned.
- **Totals** under the table, right-aligned: subtotal, tax (with its rate), and the total in bold.
- **A notes line** at the foot: payment terms on an invoice ("Payment due within 14 days"), how long
  a quote stands ("This quote is good for 30 days"), "Paid in full, thank you" on a receipt.

**Totals are plain code over whole cents, never a model's answer.** A price is read into cents
once (`toCents`), a line total is quantity times unit price, the subtotal adds the lines, tax is
`Math.round(subtotal × rate / 100)`, and the total adds the two. A model may help write a line's
description; it never computes a number that prints.

**The document number comes from the record**, so a reprint carries the same number: a prefix,
the record's date and the end of its `_id` (`INV-20261104-3F2A`). A record that already stores its
own number uses that one.

## One page per record, and only the pages in print

**A `@media print` block shows only the `.paper-stack`, and each `.paper-sheet` ends with a page break.**
The rule hides every element that neither holds the stack nor sits inside it (`display: none`, so
nothing hidden takes up a page), flattens the stack's ancestors so their padding and backgrounds
stay off the paper, and sets ink to black on white whatever the screen's look is: a dark theme's
light text would print pale grey. `@page` sets the paper size and margin. Rows avoid splitting
across pages; a long invoice runs on to a second page by itself.

## The monthly variant

A monthly report is a summary page and then one page per client. Pick the month (`Input
type="month"`), take the month's records in date order, and group them by client. The summary page
lists each client with their number of jobs and their total, and the month's total under them. Each
client's page is a statement: one line per job (its date and description, its total), and the
page's totals are the sums of the jobs' own totals (`sumTotals`), so a statement always agrees with
the invoices it covers.

## Worked example

A job book that already saves jobs with a client, a date and line items gains a Print view.
`lib/paper.js` is the money and the grouping; the Print view is in `App.jsx`. A job saved with one
price and no items prints as one line, so older records still print.

### `lib/paper.js`
```js
// Money is whole cents from the moment it is read: "19.99" is 1999, and "$1,250.50" is 125050.
export function toCents(amount) {
  const n = typeof amount === "string" ? Number(amount.replace(/[^0-9.-]/g, "")) : Number(amount);
  return Number.isFinite(n) ? Math.round(n * 100) : 0;
}

// The job's lines in the app's own field names; a job saved with one price is one line.
export function linesOf(job) {
  const items = Array.isArray(job.items) && job.items.length > 0 ? job.items : [{ description: job.title, qty: 1, unitPrice: job.price }];
  return items.map((item) => {
    const qty = Number(item.qty) || 0;
    const unitCents = toCents(item.unitPrice);
    return { description: item.description ?? "", qty, unitCents, lineCents: Math.round(qty * unitCents) };
  });
}

export function totalsOf(lines, taxRatePercent) {
  const subtotalCents = lines.reduce((sum, line) => sum + line.lineCents, 0);
  const taxCents = Math.round((subtotalCents * (Number(taxRatePercent) || 0)) / 100);
  return { subtotalCents, taxCents, totalCents: subtotalCents + taxCents };
}

// A statement adds the totals of the jobs it covers, so it agrees with their invoices to the cent.
export function sumTotals(list) {
  return list.reduce(
    (sum, t) => ({
      subtotalCents: sum.subtotalCents + t.subtotalCents,
      taxCents: sum.taxCents + t.taxCents,
      totalCents: sum.totalCents + t.totalCents,
    }),
    { subtotalCents: 0, taxCents: 0, totalCents: 0 }
  );
}

const usd = new Intl.NumberFormat("en-US", { style: "currency", currency: "USD" });
export const money = (cents) => usd.format(cents / 100);

export function docNumber(prefix, job) {
  return `${prefix}-${job.date.replaceAll("-", "")}-${job._id.slice(-4).toUpperCase()}`;
}

// The month's jobs in date order; `month` is an `Input type="month"` value such as "2026-11".
export function monthJobs(jobs, month) {
  return jobs.filter((job) => (job.date ?? "").startsWith(month)).sort((a, b) => a.date.localeCompare(b.date));
}

export function monthByClient(jobs, month) {
  const groups = new Map();
  for (const job of monthJobs(jobs, month)) groups.set(job.client, [...(groups.get(job.client) ?? []), job]);
  return [...groups].sort(([a], [b]) => a.localeCompare(b)).map(([client, list]) => ({ client, jobs: list }));
}
```

### `App.jsx`
```jsx
import React, { useState } from "react";
import { useFireproof } from "use-fireproof";
import { Button, Field, Input, Row, Segmented, Stack, Text } from "@vibes.diy/ui";
import { docNumber, linesOf, money, monthByClient, monthJobs, sumTotals, totalsOf } from "./lib/paper.js";

const SKIN = `
[data-reskin] .paper-stack { display: grid; gap: 24px; justify-items: center; }
[data-reskin] .paper-sheet { width: min(100%, 8.5in); min-height: 11in; box-sizing: border-box; padding: 0.75in; display: flex; flex-direction: column; gap: 28px;
  background: var(--surface); color: var(--ink); box-shadow: 0 12px 32px color-mix(in srgb, var(--ink) 16%, transparent); font-size: 14px; }
[data-reskin] .paper-head { display: flex; justify-content: space-between; gap: 24px; align-items: start; }
[data-reskin] .paper-business { font-size: 26px; font-weight: 700; }
[data-reskin] .paper-muted { color: var(--text-secondary); }
[data-reskin] .paper-kind { font-size: 22px; font-weight: 700; text-align: right; }
[data-reskin] .paper-table { width: 100%; border-collapse: collapse; }
[data-reskin] .paper-table th { text-align: left; font-weight: 600; border-bottom: 1px solid currentColor; padding: 6px 4px; }
[data-reskin] .paper-table td { border-bottom: 1px solid color-mix(in srgb, currentColor 18%, transparent); padding: 6px 4px; }
[data-reskin] .paper-table .num { text-align: right; font-variant-numeric: tabular-nums; }
[data-reskin] .paper-table tr { break-inside: avoid; }
[data-reskin] .paper-totals { margin-left: auto; min-width: 16rem; display: grid; grid-template-columns: 1fr auto; gap: 4px 24px; font-variant-numeric: tabular-nums; }
[data-reskin] .paper-total { font-weight: 700; font-size: 17px; border-top: 1px solid currentColor; padding-top: 4px; }
[data-reskin] .paper-notes { margin-top: auto; }
@media print {
  @page { size: letter; margin: 0.5in; }
  body:has(.paper-stack) *:not(:has(.paper-stack)):not(.paper-stack):not(.paper-stack *) { display: none !important; }
  body:has(.paper-stack), body:has(.paper-stack) *:has(.paper-stack) { display: block !important; margin: 0 !important; padding: 0 !important; border: 0 !important; background: none !important; box-shadow: none !important; }
  [data-reskin] .paper-stack { display: block; }
  [data-reskin] .paper-sheet { width: 100%; min-height: 0; padding: 0; box-shadow: none; background: white; color: black; break-after: page; }
  [data-reskin] .paper-sheet:last-child { break-after: auto; }
  [data-reskin] .paper-muted { color: black; }
}
`;

function Letterhead({ letterhead, kind, number, date }) {
  return (
    <div className="paper-head">
      <div>
        <div className="paper-business">{letterhead.name || "Your business"}</div>
        <div className="paper-muted">{letterhead.contact}</div>
      </div>
      <div>
        <div className="paper-kind">{kind}</div>
        {number && <div>No. {number}</div>}
        <div>{date}</div>
      </div>
    </div>
  );
}

function Totals({ totals, taxRate }) {
  return (
    <div className="paper-totals">
      <span>Subtotal</span>
      <span>{money(totals.subtotalCents)}</span>
      <span>Tax ({Number(taxRate) || 0}%)</span>
      <span>{money(totals.taxCents)}</span>
      <span className="paper-total">Total</span>
      <span className="paper-total">{money(totals.totalCents)}</span>
    </div>
  );
}

function InvoiceSheet({ job, letterhead }) {
  const lines = linesOf(job);
  return (
    <article className="paper-sheet">
      <Letterhead letterhead={letterhead} kind="Invoice" number={docNumber("INV", job)} date={job.date} />
      <div>
        <div className="paper-muted">For</div>
        <div>{job.client}</div>
        <div className="paper-muted">{job.clientContact}</div>
      </div>
      <table className="paper-table">
        <thead>
          <tr>
            <th>Description</th>
            <th className="num">Qty</th>
            <th className="num">Unit price</th>
            <th className="num">Amount</th>
          </tr>
        </thead>
        <tbody>
          {lines.map((line, i) => (
            <tr key={i}>
              <td>{line.description}</td>
              <td className="num">{line.qty}</td>
              <td className="num">{money(line.unitCents)}</td>
              <td className="num">{money(line.lineCents)}</td>
            </tr>
          ))}
        </tbody>
      </table>
      <Totals totals={totalsOf(lines, letterhead.taxRate)} taxRate={letterhead.taxRate} />
      <div className="paper-notes">{letterhead.notes}</div>
    </article>
  );
}

function MonthReport({ jobs, month, letterhead }) {
  const clients = monthByClient(jobs, month).map((group) => {
    const rows = group.jobs.map((job) => ({ job, totals: totalsOf(linesOf(job), letterhead.taxRate) }));
    return { ...group, rows, totals: sumTotals(rows.map((r) => r.totals)) };
  });
  const monthTotals = sumTotals(clients.map((c) => c.totals));
  return (
    <>
      <article className="paper-sheet">
        <Letterhead letterhead={letterhead} kind="Monthly report" date={month} />
        <table className="paper-table">
          <thead>
            <tr>
              <th>Client</th>
              <th className="num">Jobs</th>
              <th className="num">Total</th>
            </tr>
          </thead>
          <tbody>
            {clients.map((c) => (
              <tr key={c.client}>
                <td>{c.client}</td>
                <td className="num">{c.rows.length}</td>
                <td className="num">{money(c.totals.totalCents)}</td>
              </tr>
            ))}
          </tbody>
        </table>
        <Totals totals={monthTotals} taxRate={letterhead.taxRate} />
      </article>
      {clients.map((c) => (
        <article className="paper-sheet" key={c.client}>
          <Letterhead letterhead={letterhead} kind="Statement" date={month} />
          <div>
            <div className="paper-muted">For</div>
            <div>{c.client}</div>
          </div>
          <table className="paper-table">
            <thead>
              <tr>
                <th>Date</th>
                <th>Job</th>
                <th className="num">Amount</th>
              </tr>
            </thead>
            <tbody>
              {c.rows.map(({ job, totals }) => (
                <tr key={job._id}>
                  <td>{job.date}</td>
                  <td>{job.title || linesOf(job).map((l) => l.description).join(", ")}</td>
                  <td className="num">{money(totals.totalCents)}</td>
                </tr>
              ))}
            </tbody>
          </table>
          <Totals totals={c.totals} taxRate={letterhead.taxRate} />
          <div className="paper-notes">{letterhead.notes}</div>
        </article>
      ))}
    </>
  );
}

function PrintView({ jobs }) {
  const { useDocument } = useFireproof("jobs");
  const { doc: letterhead, merge, save } = useDocument({ _id: "letterhead", name: "", contact: "", taxRate: "0", notes: "Payment due within 14 days. Thank you!" });
  const [month, setMonth] = useState(new Date().toISOString().slice(0, 7));
  const [paper, setPaper] = useState("invoices");
  const shown = monthJobs(jobs, month);
  const field = (key) => (value) => merge({ [key]: value });

  return (
    <div data-reskin>
      <style>{SKIN}</style>
      <Stack gap={5}>
        <Stack gap={3}>
          <Row gap={3}>
            <Field label="Business name">{(id, d) => <Input id={id} describedBy={d} value={letterhead.name} onChange={field("name")} onBlur={() => save()} />}</Field>
            <Field label="Contact line">{(id, d) => <Input id={id} describedBy={d} value={letterhead.contact} onChange={field("contact")} onBlur={() => save()} />}</Field>
            <Field label="Tax rate (%)">{(id, d) => <Input id={id} describedBy={d} inputMode="decimal" value={letterhead.taxRate} onChange={field("taxRate")} onBlur={() => save()} />}</Field>
          </Row>
          <Field label="Notes line">{(id, d) => <Input id={id} describedBy={d} value={letterhead.notes} onChange={field("notes")} onBlur={() => save()} />}</Field>
          <Row gap={3}>
            <Field label="Month">{(id, d) => <Input id={id} describedBy={d} type="month" value={month} onChange={setMonth} />}</Field>
            <Segmented label="Paper" value={paper} onChange={setPaper} options={[{ value: "invoices", label: "An invoice per job" }, { value: "report", label: "Monthly report" }]} />
            <Button onClick={() => window.print()} disabled={shown.length === 0}>
              Print or save as PDF
            </Button>
          </Row>
          <Text muted>{shown.length === 1 ? "1 job this month, on one page." : `${shown.length} jobs this month, a page each.`}</Text>
        </Stack>
        <div className="paper-stack">
          {paper === "invoices" ? shown.map((job) => <InvoiceSheet key={job._id} job={job} letterhead={letterhead} />) : <MonthReport jobs={jobs} month={month} letterhead={letterhead} />}
        </div>
      </Stack>
    </div>
  );
}

export default function App() {
  const { useLiveQuery } = useFireproof("jobs");
  const { docs: jobs } = useLiveQuery("type", { key: "job" });
  const [view, setView] = useState("jobs");

  return (
    <main className="mx-auto max-w-4xl px-4 py-8">
      <Stack gap={5}>
        <Segmented label="View" value={view} onChange={setView} options={[{ value: "jobs", label: "Jobs" }, { value: "print", label: "Print" }]} />
        {view === "print" ? <PrintView jobs={jobs} /> : <Text muted>The app's own job list and job form stay here.</Text>}
      </Stack>
    </main>
  );
}
```

## Adapting

- **A quote or estimate** is the same sheet with `kind="Quote"` and a `QUO` prefix, read from the
  quote records, and a notes line saying how long the price stands. A receipt reads the paid records
  and says so in its notes line.
- **One record's paper** ("print this quote") is a Print button on that record's own screen that
  shows one sheet in the stack and prints it.
- **Other currencies:** set the `Intl.NumberFormat` currency to the one the app's prices are in.
- **A4:** `@page { size: A4; }` and a `210mm` sheet width on screen.
