# Suppliers and bills to pay

Who a customer pays and what it owes. Skerry owns the supplier-as-payee and the bill-to-pay; a customer's books hold the accounting entry and link to the bill by id (`external_refs`). `docs/books.md` says Skerry keeps no invoices for accounting; this is not that: a bill here is a payable, kept so it can be paid, scheduled, held and explained.

Migration: `db/049_suppliers_bills.sql` (additive, idempotent, `lock_timeout` 5s; apply after 048).

## Records

- **Supplier** (`supplier`): profile (contacts, `sender_domains`, `sender_addresses`, `default_terms`, currency, tax id, category, notes, documents) linked to a payee once there are details to pay. `verification` is `unverified`, `verified` or `held`.
- **Bill** (`bill`): supplier, number, issue and due date, amount, tax, currency, terms, discount deadline and amount, line items, `source` (the message and file it came from), `extraction` (confidence per field), `state`.

## Terms

`{ kind: "net" | "eom" | "on_receipt", days, discount?: { pct, days } }`. "2/10 net 30" is `net`, 30 days, discount 2 percent inside 10 days. A bill's due date is worked out from its terms (or the supplier's defaults) when it is not sent. The discount deadline is kept only when it falls before the due date. `annualised_discount` on a new bill is what the discount is worth as a yearly rate (2/10 net 30 is about 37 percent), for deciding whether to pay early. Pure code: `lib/pure/terms.ts`.

## States

`to_review` (read from a file, waiting for a person) -> `to_pay` -> `scheduled` -> `paid`; `held`, `disputed` and `void` on the side. `paid` and `void` are final. `lib/pure/bill.ts`.

## Paying

`POST /bills/{id}/pay` makes a payout (`lib/ops/payout.ts`) with `external_kind: "bill"` and `external_ref` the bill id. It is checked as `payout.create`: spend policies, the new-payee hold, cooling-off, two-approver and passkey rules all apply. A payout held for approval leaves the bill `scheduled`. The key needs `payouts:write` as well as the payee's rules; `bills:write` alone cannot move money.

## Changed payment details

Linking a different payee to a supplier that has been paid holds it for 72 hours and sends `payee.details_changed`. A bill to a held supplier is refused as `supplier_held`. A person confirms through a contact they already knew with `POST /suppliers/{id}/verify`, after the cooling-off; an agent cannot.

## Duplicates

Same supplier, number (ignoring case, spaces, punctuation and leading zeros), amount and currency is `duplicate_bill`, until the first is voided.

## To pay

`GET /to-pay?customer_id=` totals open bills (`to_pay`, `scheduled`) by bucket (overdue, this week, next week, later), currency, due week and supplier, and lists open discount deadlines with what each saves.

## Events

`supplier.created`, `payee.details_changed`, `bill.found`, `bill.due_soon`, `bill.paid`. Scopes: `suppliers:read`, `suppliers:write`, `bills:read`, `bills:write`.
