# SalesInvoice

A sales invoice header: what a customer is billed, when it is due, and how far it has got through the accounting sync. The money on it is the sum of its rows, which are reached through `sales-invoices/{sales_invoice}/rows`; the API never writes rows inline. Invoices that predate the current invoicing flow are marked `is_legacy` and are read-only.

## Used by
- GET /api/v2/sales-invoices: List sales invoices (SalesInvoiceList)
- POST /api/v2/sales-invoices: Create a sales invoice (SalesInvoice)
- GET /api/v2/sales-invoices/{sales_invoice}: Read a sales invoice (SalesInvoice)
- PATCH /api/v2/sales-invoices/{sales_invoice}: Update a sales invoice (SalesInvoice)
- POST /api/v2/sales-invoices/{sales_invoice}/actions/restore: Restore a deleted sales invoice (SalesInvoice)

## SalesInvoice fields
- id (integer, required): Unique identifier.
- customer_id (integer, required): Customer this invoice is billed to.
- customer (Ref or CustomerSummary, required): The customer this invoice is billed to. Expanding it with `?include=customer` additionally requires the `customers::read` permission and a token scope covering the customers prefix.
- project_id (integer or null, required): Project this invoice was raised for, when it was raised from one.
- customer_subscription_id (integer or null, required): Customer subscription this invoice was generated from, when it was generated from a renewal.
- invoice_number (string ≤ 255, required, server-assigned): Invoice number, allocated by the server from the organisation's yearly sequence: B-26-00001 for an invoice raised in this flow, L-1234 for one converted from a legacy invoice. It also works in the path: `GET sales-invoices/B-26-00001` works. This is the field `?q=` searches.
- reference (string or null ≤ 255, required): The customer's own reference, typically a purchase-order number, reproduced on the invoice.
- description (string or null ≤ 255, required): One-line description of what was invoiced.
- invoice_date (date (YYYY-MM-DD), required): Invoice date, a zone-free calendar date. Newest first is the default sort.
- due_date (date (YYYY-MM-DD) or null, required): Date payment is due, a zone-free calendar date. Null when no term was applied.
- status (string, required, server-assigned): Lifecycle status. Resolved by the server on create and never writable: a caller without `sales_invoices::finalize` creates a `pending_approval` invoice, and any update by such a caller returns the invoice to `pending_approval`.
- should_email (boolean, required): Whether the invoice is emailed to the customer when it syncs. The email is sent through the accounting integration, so it is forced to `false` in an organisation with no integration configured. Writing it requires `sales_invoices::finalize`.
- subtotal (decimal (2 dp), required, server-assigned): Sum of the row line totals excluding VAT, as a decimal string. Recomputed from the rows; never written directly.
- vat_total (decimal (2 dp), required, server-assigned): Sum of the row VAT amounts, as a decimal string. Recomputed from the rows; never written directly.
- total (decimal (2 dp), required, server-assigned): Subtotal plus VAT, as a decimal string. Recomputed from the rows; never written directly.
- exact_invoice_id (string or null ≤ 255, required, server-assigned): The accounting system's own identifier for this invoice, written by the sync. Not a local id.
- exact_journal (string or null ≤ 255, required, server-assigned): Sales journal the invoice is booked in, resolved from the organisation's accounting settings on create. Null in an organisation with no integration configured.
- sync_status (string, required, server-assigned): How far the invoice has got through the accounting sync. Set on create: local in an organisation with no integration configured, pending otherwise.
- sync_error (string or null, required, detail only, server-assigned, multiline): Why the last sync attempt failed, verbatim from the accounting system. Null while nothing has failed.
- synced_at (timestamp (YYYY-MM-DDThh:mm:ssZ) or null, required, server-assigned): When the invoice last synced successfully.
- sync_attempted_at (timestamp (YYYY-MM-DDThh:mm:ssZ) or null, required, server-assigned): When the invoice was last handed to the sync, successfully or not.
- paid_at (timestamp (YYYY-MM-DDThh:mm:ssZ) or null, required, server-assigned): When the invoice was settled. Set on create for a local invoice that is immediately definitive; otherwise written when payment is registered.
- is_legacy (boolean, required, server-assigned): Whether this invoice was converted from a legacy invoice. Such an invoice is read-only: PATCH and DELETE answer `409` `state-conflict` with reason `locked_by_legacy`.
- legacy_invoice_id (integer or null, required, server-assigned): The id of the legacy invoice this invoice was converted from, so it can be matched with `GET /api/v2/invoices`. The legacy invoice may since have been removed.
- internal_notes (string or null, required, detail only, multiline): Notes for colleagues. Never rendered on the invoice the customer receives.
- rows_count (integer ≥ 0, required): Number of line rows on this invoice.
- created_at (timestamp (YYYY-MM-DDThh:mm:ssZ), required): Creation timestamp.
- updated_at (timestamp (YYYY-MM-DDThh:mm:ssZ), required): Last-write timestamp; use for incremental sync. It also feeds the `ETag` revision, because a sales invoice has no `version` field.
- deleted_at (timestamp (YYYY-MM-DDThh:mm:ssZ) or null, required): Soft-delete timestamp, `null` while live.
- links (SalesInvoiceLinks, required)
  - self (url, required): The canonical URI of this record.
  - rows (url, required): Line rows on this invoice.

Only on `?view=detail`: sync_error, internal_notes

## Variants

### SalesInvoiceList

What a collection returns, and what `?view=summary` returns on a single record.

28 fields.
Omits: sync_error, internal_notes

### SalesInvoiceSummary

The embedded form: what you get when another resource carries this one and you did not `include` it.

3 fields.
Omits: customer_id, customer, project_id, customer_subscription_id, reference, description, due_date, status, should_email, subtotal, vat_total, total, exact_invoice_id, exact_journal, sync_status, sync_error, synced_at, sync_attempted_at, paid_at, is_legacy, legacy_invoice_id, internal_notes, rows_count, created_at, updated_at, deleted_at, links

### SalesInvoiceLinks

The `links` object on this resource.

Keys: self, rows

## Example
```json
{
  "id": 1,
  "customer_id": 1,
  "customer": {
    "id": 1
  },
  "project_id": 1,
  "customer_subscription_id": 1,
  "invoice_number": "…",
  "reference": "PO-4471",
  "description": "Development work, January 2026",
  "invoice_date": "2026-02-01",
  "due_date": "2026-03-03",
  "status": "draft",
  "should_email": false,
  "subtotal": "1250.00",
  "vat_total": "1250.00",
  "total": "1250.00",
  "exact_invoice_id": "…",
  "exact_journal": "…",
  "sync_status": "pending",
  "sync_error": "…",
  "synced_at": "2026-03-04T09:15:00Z",
  "sync_attempted_at": "2026-03-04T09:15:00Z",
  "paid_at": "2026-03-04T09:15:00Z",
  "is_legacy": false,
  "legacy_invoice_id": 1,
  "internal_notes": "…",
  "rows_count": 1,
  "created_at": "2026-03-04T09:15:00Z",
  "updated_at": "2026-03-04T09:15:00Z",
  "deleted_at": "2026-03-04T09:15:00Z",
  "links": {
    "self": "https://<your-url>/api/v2/work-items/1",
    "rows": "https://<your-url>/api/v2/work-items/1"
  }
}
```
