# Ship and invoice a dispatch

Use this recipe to ship a dispatch and raise its invoice from your own
system. In Omono, a sales order ships in one or more dispatches, and each
dispatch moves through your organisation's workflow statuses. The last one
is the final status, which is the status whose type is `final`. Moving a
dispatch to it ships the dispatch and invoices it.

Every request in this recipe sends your API key in an
`Authorization: Bearer <key>` header, and every `POST` and `PATCH` also sends
an `Idempotency-Key`, so a prompt retry doesn't do the work twice. Paths are relative
to `https://<host>/api/v1`, and the examples show the full path.

> **Note:** The sales order write operations are in beta, and you can use
> this recipe once they appear in the [API reference](/docs#tag/dispatches)
> for your platform.

A dispatch can come from your staff in Omono, or from the API, as
[Plan the dispatches for an order](/docs/recipes/plan-dispatches) shows.
Your staff then pick it in Omono. Picking is when staff collect a
dispatch's goods and record which stock they took. This recipe moves a
dispatch to another status, including the final one.
[Dispatches and invoicing](/docs/workflow) explains the rules in full.

You need the scopes `sales:read`, `sales:write` and `reference:read`. The
reference scope lets you look up courier IDs with `GET /couriers`. To find
a task that stops a dispatch shipping, your key also needs `tasks:read`.

## Steps

1. **Wait until the dispatch can ship.** When staff save a dispatch's
   picking in Omono, the order's `updated` time changes. So you can find
   orders that may be ready by polling
   `GET /sales-orders?updated_since=<time>&sort=updated&include=dispatches`,
   as [Keeping in sync](/docs/syncing#incremental-sync) explains.
2. **Check that the dispatch can ship.** Just before you ship, read the
   order with its dispatches, with `GET /sales-orders/{id}?include=dispatches`,
   and save the `ETag` header from the response. Read it again at this
   point even if you already have a copy, because a change to a task or to
   the customer can stop a dispatch shipping without changing the order's
   `updated` time. This read always returns the whole order, even if you
   send `If-None-Match`. Each dispatch has a `legalTransitions` list, which
   holds the statuses it can move to right now. The one whose `type` is
   `final` ships it. If that status's `blockedBy` list isn't empty, the
   dispatch can't ship yet. For example, `dispatch-not-fully-picked` means
   that the goods aren't all picked in Omono, and `dispatch-pick-not-saved`
   means that staff must open the picking and press Save. Check whether the list
   is empty, rather than looking for particular codes, because Omono can
   add new ones.
3. **Ship it.** Move the dispatch to the final status, with the order's
   `ETag` in `If-Match`:

   ```http
   POST /api/v1/sales-orders/{id}/transitions
   Idempotency-Key: ship-SO-1042-1
   If-Match: "1758879330123"
   Content-Type: application/json

   {
     "dispatchId": "…",
     "statusId": "<the ID of the final status>",
     "courierId": "…",
     "trackingNo": "1Z999AA10123456784"
   }
   ```

   Take the `courierId` from `GET /couriers`. You can also send
   `invoiceDate`, which can be any day from 31 days ago up to today. If you
   leave it out, Omono uses today's date in your organisation's time zone.
   If you send the invoice email to the customer yourself, send
   `"suppressCustomerEmail": true` so that Omono doesn't send it too. The
   `200` response returns `{ "applied": true, "salesOrder": … }`, and the
   shipped dispatch in `salesOrder.dispatches` now has its `invoiceNo`.

If the response has `"applied": false`, the dispatch was already at that
status, for example because this was a retry. That isn't an error, and
there's nothing more to do.

## Errors to handle

| Status | Code                                                                                                             | What to do                                                                                                                                                                                                                                                                                                                                                                                                                                                     |
| ------ | ---------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| 409    | `dispatch-pick-not-saved`, `dispatch-not-fully-picked`, `blocking-tasks`, `company-on-stop`, `proforma-not-paid` | The dispatch can't ship yet. The error names only one reason, so read the order's `blockedBy` list to see them all, and show them to a person, who fixes the problems in Omono. A proforma customer pays before the goods ship, so for `proforma-not-paid` you can also mark the dispatch as paid with `/proforma-paid`. If Omono adds a new reason a dispatch can't ship, it comes as a new code, both here and in `blockedBy`, so handle it in the same way. |
| 409    | `dispatch-state-changed`                                                                                         | Someone moved the dispatch at the same time. Read the order again and decide again.                                                                                                                                                                                                                                                                                                                                                                            |
| 409    | `dispatch-shipped`                                                                                               | The dispatch has already shipped. There's nothing to do.                                                                                                                                                                                                                                                                                                                                                                                                       |
| 412    | `precondition-failed`                                                                                            | The order has changed since you read it. Read it again, check the dispatch again, and retry with the new `ETag`.                                                                                                                                                                                                                                                                                                                                               |
| 422    | `validation-failed`, `invalid-reference`, `invalid-transition-target`                                            | Your request is wrong, and `errors[].field` names the field. `invoiceDate` is outside the allowed range, `courierId` or `dispatchId` isn't one you can use, or `statusId` isn't one of your statuses. Take the IDs from the order's `legalTransitions` and `dispatches`, and from `GET /couriers`. Nothing changed, and no invoice number was used.                                                                                                            |
| 404    | `not-found`                                                                                                      | There's no sales order with the ID in the path.                                                                                                                                                                                                                                                                                                                                                                                                                |
| 500    | `internal-error`                                                                                                 | Read the order again. If the dispatch is shipped but has no `invoiceNo`, send the same request again, with the same body and key, within 31 days. This completes the invoicing.                                                                                                                                                                                                                                                                                |

[Other reasons a move fails](/docs/workflow#other-refusals) lists the rest,
such as a dispatch that belongs to another order.
