# Sales invoices

A sales invoice gets into the books in three calls: **record** a draft,
**issue** it, and **post** it. Each step is a separate call on purpose. An
issued invoice is a legal document with a number that is never used again,
so you check the draft before you issue it, and nothing turns it back into a
draft after that.

## The flow in one table

| Step              | Call                                            | What it does                                                                                                                 |
| ----------------- | ----------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------- |
| 1. Record a draft | `POST /api/companies/{company_id}/invoices`     | Creates a draft (`recorded`) with no number. You can still change or delete it                                               |
| 2. Issue          | `POST .../invoices/{invoice_id}/issue`          | Gives the number, freezes the content and stores the PDF. It books nothing                                                   |
| 3. Post           | `POST .../invoices/{invoice_id}/post`           | Books the invoice: the receivable, the revenue and the VAT of its month (the month of `supply_date`, else of `invoice_date`) |
| 4. Get the PDF    | `GET .../invoices/{invoice_id}/`, then the file | The `original` document of the revision is the PDF that Oscar issued                                                         |
| 5. Send it        | Your own code, or a person in the Oscar app     | Oscar does not email an invoice for an API key                                                                               |

Every write needs the scope `write`. The [API reference](/docs/api) names the
permission of each call. Send an `Idempotency-Key` with each one (see
[Retries](#retries)).

## 0. The customer

An invoice names a party. Create the customer once, and keep its
`party_id`:

```http
POST /api/companies/{company_id}/parties HTTP/1.1
Authorization: Bearer osk_live_...
Content-Type: application/json
Idempotency-Key: 0b6f2f0e-3c1a-4d8e-9f57-6a1d2e4b8c90

{
  "name": "Epsilon Retail OÜ",
  "kind": "customer",
  "country": "EE",
  "reg_code": "12345678",
  "address": "Pikk 1, 10123 Tallinn"
}
```

The answer is 201, with `result.party_id`. A VAT-registered company issues no
invoice without the buyer's address: give `address` here, or set it later with
`POST .../parties/{party_id}/address`. Without it, the issue gets 400
`buyer_address_missing`. A customer party with `private_persons: true` (a pool
of private buyers) needs no address.

## 1. Record a draft

```http
POST /api/companies/{company_id}/invoices HTTP/1.1
Authorization: Bearer osk_live_...
Content-Type: application/json
Idempotency-Key: 7d1e4a52-9b0c-4f3e-8a6d-1c2b3e4f5a60

{
  "direction": "sales",
  "party_id": "01KBX3T7Q8M2N4P6R8S0V2W4Y6",
  "revision": {
    "invoice_date": "2026-12-01",
    "supply_date": "2026-11-30",
    "due_date": "2026-12-15",
    "currency": "EUR",
    "lines": [
      {
        "kind": "item",
        "description": "Consulting, November 2026",
        "quantity": "10",
        "unit": "h",
        "unit_price": "95.00"
      }
    ]
  }
}
```

- `direction` is `sales`. `revision` needs `invoice_date`, `currency` and
  `lines`.
- `supply_date` is the day the goods were delivered or the service was done.
  It decides the month of the revenue and the VAT. Without it, Oscar uses
  `invoice_date`.
- **A sales draft takes no `number`.** The number comes at the issue. A draft
  with a `number` gets 400 `invalid_payload`.
- `kind` is `invoice` by default. Use `credit_note` to correct an issued
  invoice (see [Correct an issued invoice](#correct-an-issued-invoice)).
- An `item` line needs `quantity` and `unit_price` (a decimal string in major
  units) or `unit_price_minor`. Oscar computes the net. A line with no `kind`
  is a `vat_summary` line, which needs `net_minor`.
- **Leave out `vat_code`**, and Oscar fills the code that it detects from the
  customer and the description. You can also send a code of the company
  (`GET /api/companies/{company_id}/vat-codes/`). Do not send
  `vat_code_source: "manual"`: that is a decision of a person, and an API key
  gets 403 `human_actor_required` (see
  [Human judgements](/docs/api/guides/human-judgements)).
- Leave out `origin`.

The answer is 201:

```json
{
  "command": "record_invoice",
  "command_id": "01KBX3V0A1B2C3D4E5F6G7H8J9",
  "commit_seq": 412,
  "replayed": false,
  "result": {
    "invoice_id": "01KBX3V0K2M4N6P8Q0R2S4T6V8",
    "revision_id": "01KBX3V0K2M4N6P8Q0R2S4T6V9",
    "line_ids": ["01KBX3V0K2M4N6P8Q0R2S4T6VA"],
    "vat_notes": [
      {
        "line": 1,
        "vat_code": "EE-SAL-STD",
        "choice": "ee_standard",
        "note": "detected",
        "reason": "domestic",
        "source": "rules"
      }
    ]
  }
}
```

`vat_notes` is there only when Oscar has something to say about the VAT of a
line. Each note names the line (`line`, from 1) and its `vat_code`:

| `note`     | Meaning                                                                                          |
| ---------- | ------------------------------------------------------------------------------------------------ |
| `detected` | The line had no code, and Oscar filled this one. `reason` says why, `source` how it was detected |
| `unusual`  | The code that you sent, or the default code of the customer, is unusual for this customer        |

A note never refuses the draft. Read it before you issue: after the issue,
the VAT code of the invoice cannot change.

### Change a draft

`POST .../invoices/{invoice_id}/revise` replaces the content of the draft. Send
a `reason` and the whole `revision` again, not only the changed fields:

```json
{
  "reason": "Wrong quantity",
  "revision": {
    "invoice_date": "2026-12-01",
    "supply_date": "2026-11-30",
    "due_date": "2026-12-15",
    "currency": "EUR",
    "lines": [
      {
        "kind": "item",
        "description": "Consulting, November 2026",
        "quantity": "12",
        "unit": "h",
        "unit_price": "95.00"
      }
    ]
  }
}
```

A sales invoice is revised only while it is a draft. An issued invoice gets
409 `invalid_state`.

### Delete a draft

`POST .../invoices/{invoice_id}/discard` deletes a draft that was never
issued. The body is `{}`, or `{"reason": "..."}`. The books keep the draft as
`void`, with no number. An issued invoice gets 409 `invalid_state`.

## 2. Issue

```http
POST /api/companies/{company_id}/invoices/{invoice_id}/issue HTTP/1.1
Authorization: Bearer osk_live_...
Content-Type: application/json
Idempotency-Key: 2a9c7e10-4b5d-4e6f-8a1b-3c4d5e6f7a80

{}
```

The answer is 200:

```json
{
  "command": "issue_invoice",
  "command_id": "01KBX3W2A1B2C3D4E5F6G7H8J9",
  "commit_seq": 413,
  "replayed": false,
  "result": {
    "invoice_id": "01KBX3V0K2M4N6P8Q0R2S4T6V8",
    "number": "ACME-2026-042",
    "reference_number": "20260422",
    "warnings": []
  }
}
```

- `number` comes from the gap-free series of the company. A number is never
  used again, also after a cancel.
- `reference_number` is the Estonian reference number (viitenumber) that the
  payer quotes.
- The issue freezes the content, and the seller and buyer data. Oscar takes
  the seller's address from the Business Register. Send `seller_address` in
  the body when the register has none: a VAT-registered company issues no
  invoice without the seller's and the buyer's address (400
  `seller_address_missing` or `buyer_address_missing`). A customer party with
  `private_persons: true` needs no buyer address.
- **The issue books nothing.** The invoice is `issued`, and its
  `settlement_state` is `unposted`. Post it next.
- An issued invoice never goes back to a draft. To correct it, see
  [Correct an issued invoice](#correct-an-issued-invoice).

## 3. Post

```http
POST /api/companies/{company_id}/invoices/{invoice_id}/post HTTP/1.1
Authorization: Bearer osk_live_...
Content-Type: application/json
Idempotency-Key: 9e8d7c6b-5a4f-4e3d-8c2b-1a0f9e8d7c6b

{}
```

The post books the receivable, the revenue and the VAT of the invoice in its
month: the month of `supply_date`, else of `invoice_date`. Until then the
invoice is in no report and in no VAT return. The answer is 200, with
`result.posted` true and the journal entry in `result.entry`. The invoice is
now `posted`, and its `settlement_state` is `unpaid`, or `overdue` when its due
date is before today. `result.posted` true, or `state: posted` on the read,
confirms the post.

Check `result.posted`. When it is `false`, `result.reason` says why: an
invoice with a zero total (`zero gross`) is not booked and keeps its state.

- A draft gets 409 `invalid_state`: issue it first.
- Nothing posts an issued invoice for you. An integration that issues and
  does not post leaves the invoice out of the books.
- 403 `permission_denied` on the post means that the caller does not have the
  permission `invoices.issue`, the same permission as the issue. Either the
  role of the caller does not have it, or the accountant issues and books the
  sales invoices of this company. Do not retry: a person with the permission,
  or the accountant, posts the invoice.

## 4. Get the PDF

Oscar stores the PDF of the invoice at the issue. Read the invoice:

```http
GET /api/companies/{company_id}/invoices/{invoice_id}/ HTTP/1.1
Authorization: Bearer osk_live_...
```

In `revision.documents`, the document with `role: "original"` is the issued
PDF. Take its `file_id`, and download the bytes:

```http
GET /api/companies/{company_id}/files/{file_id}/content/ HTTP/1.1
Authorization: Bearer osk_live_...
```

Oscar stores the PDF during the issue call, before the answer. When the issued
invoice has no `original` document, the store failed, and reading the invoice
again does not create it. A person opens the PDF of the invoice in the Oscar
app, which stores it; then read the invoice again. Else contact support.

## 5. Send it

Oscar does not email an invoice for an API key or an OAuth token. Sending an
invoice to the customer is a decision of a person: the email send of the
Oscar app answers 403 `session_required` to an API key. Send the PDF from your
own system, or a person sends it from the Oscar app.

## Correct an issued invoice

An issued invoice is corrected by a **credit note**, or, in one narrow case,
cancelled.

### Credit note

A credit note is a sales draft with `kind: "credit_note"` and
`corrects_invoice_id`, for the same customer. The amounts are **positive**:
the credit note carries the sign (`sign: -1` on the read). Credit part of the
invoice, or all of it.

```json
{
  "direction": "sales",
  "kind": "credit_note",
  "corrects_invoice_id": "01KBX3V0K2M4N6P8Q0R2S4T6V8",
  "party_id": "01KBX3T7Q8M2N4P6R8S0V2W4Y6",
  "revision": {
    "invoice_date": "2026-12-10",
    "currency": "EUR",
    "lines": [
      {
        "kind": "item",
        "description": "Consulting, November 2026: 2 hours not used",
        "quantity": "2",
        "unit": "h",
        "unit_price": "95.00",
        "vat_code": "EE-SAL-STD"
      }
    ]
  }
}
```

Then issue it and post it, as an invoice. A credit note takes its number from
the same series as the invoices. The issue refuses a credit note with no
`corrects_invoice_id`. Oscar does not fill the VAT code of a credit note:
send the code of the invoice that it corrects.

A credit note is booked in its own month, by its `invoice_date`, not in the
month of the invoice that it corrects. When the invoice was wrong from the
start (for example a wrong VAT rate) and the VAT return of its month is
already filed, record the credit note and tell your accountant: the return of
that month may need a correction.

Posting the credit note does not mark the invoice as settled. The invoice and
the credit note stay open (`unpaid`, or `overdue` past the due date) until
they are set off against each other. The public API has no call for that: a person with the permission
`allocations.write` does it in the Oscar app, usually the accountant.

### Cancel

`POST .../invoices/{invoice_id}/cancel`, with a `reason`, withdraws an issued
or posted invoice. The number stays used, and a posted entry is reversed.

**Cancel an invoice only when it was issued by mistake and the customer has
not received it, in the same month. Once the customer has it, record a credit
note that names it.** The accountant sees a cancellation of an earlier month
as a finding and asks for a credit note.

## States

`state` of an invoice:

| `state`     | Meaning                                                   |
| ----------- | --------------------------------------------------------- |
| `recorded`  | A draft. No number yet. Revise it, issue it or discard it |
| `issued`    | Numbered and frozen, not in the books yet. Post it        |
| `posted`    | In the books                                              |
| `cancelled` | Withdrawn after the issue. The number stays used          |
| `void`      | A draft that was discarded. It never had a number         |

`settlement_state` of an invoice:

| `settlement_state` | Meaning                                      |
| ------------------ | -------------------------------------------- |
| `unposted`         | Not in the books yet                         |
| `unpaid`           | Posted, nothing paid                         |
| `part_paid`        | Posted, paid in part                         |
| `paid`             | Posted and paid                              |
| `overdue`          | Unpaid or paid in part, and due before today |

`GET /api/companies/{company_id}/invoices/` filters by `state`, and by
`settlement` (also `open`: unpaid or paid in part).

## Retries

Use one new `Idempotency-Key` for each step: one for the record, one for the
issue, one for the post. Retry a step only with its own key. Without a key, a
retry of the record after a timeout can record the same invoice twice. See
[Idempotency](/docs/api/guides/idempotency).
