# Change a document's line items

Use this recipe to add, change or delete line items on a quote, sales
order, credit note or purchase order that already exists. The example uses
a sales order, and the steps are the same for the other documents:

| Document       | Read and change it with                                       | Scopes                                   |
| -------------- | ------------------------------------------------------------- | ---------------------------------------- |
| Quote          | `GET /quotes/{id}` and `PATCH /quotes/{id}`                   | `sales:read` and `sales:write`           |
| Sales order    | `GET /sales-orders/{id}` and `PATCH /sales-orders/{id}`       | `sales:read` and `sales:write`           |
| Credit note    | `GET /credit-notes/{id}` and `PATCH /credit-notes/{id}`       | `sales:read` and `sales:write`           |
| Purchase order | `GET /purchase-orders/{id}` and `PATCH /purchase-orders/{id}` | `purchasing:read` and `purchasing:write` |

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:** These write operations are in beta, and you can use this
> recipe once they appear in the [API reference](/docs#tag/sales-orders)
> for your platform.

## How a line item change works

In a `PATCH`, the `lineItems` array is a list of changes. Any line item you
leave out stays as it is.

| You send                                                                              | What Omono does                                                     |
| ------------------------------------------------------------------------------------- | ------------------------------------------------------------------- |
| `{ "id": "…", "quantity": 5 }`                                                        | Updates that line item. Any fields you leave out keep their values. |
| `{ "id": "…", "delete": true }`                                                       | Deletes that line item. Don't send any other fields with it.        |
| An item without an `id`, such as `{ "stockItemId": "…", "quantity": 1, "price": 10 }` | Adds a new line item.                                               |

In an entry that updates a line item, send only the fields you want to
change, not the whole line item you read. A new line item needs
`stockItemId`, `quantity` and `price`.

## Steps

1. **Read the document and keep its `ETag`.** Call
   `GET /sales-orders/{id}`, and save the `ETag` header from the response.
2. **Send only your changes**, with the `ETag` in `If-Match` and a new
   `Idempotency-Key`. Every `PATCH` that includes `lineItems` must send
   `If-Match`.

   ```http
   PATCH /api/v1/sales-orders/{id}
   If-Match: "1758879330123"
   Idempotency-Key: so-1042-edit-7
   Content-Type: application/json

   {
     "lineItems": [
       { "id": "line-a", "quantity": 5 },
       { "id": "line-b", "delete": true },
       { "stockItemId": "…", "quantity": 1, "price": 10 }
     ]
   }
   ```

3. **Find your new line items.** The `createdLineItems` field in the `200`
   response gives the IDs of the line items you added. Use those IDs, and
   never rely on a line item's position, because someone may add a line
   item in Omono at the same time.

The `lineItems` array never changes the order of the line items: new line
items go after the existing ones. To change the order, send
`lineItemOrder` with the IDs of every line item that stays, in the order you
want, for example `{ "lineItemOrder": ["line-c", "line-a"] }`. Once a sales
order has a dispatch, or a purchase order has a delivery, the order of its
line items is fixed. [Change line items](/docs/writing#line-changes)
explains the rules in more depth.

## Errors to handle

| Status | Code                                                                                                                                                         | What to do                                                                                                                                                                                                                                                                                                                                                                                     |
| ------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| 412    | `precondition-failed`                                                                                                                                        | The document has changed since you read it. Read it again and rebuild your changes from the fresh copy: check that the line item IDs you send still exist, and build any `lineItemOrder` from every line item in the fresh copy, including ones someone else added. Omono doesn't store a failed request, so you can retry with the same `Idempotency-Key`, even though your body has changed. |
| 428    | `precondition-required`                                                                                                                                      | You didn't send `If-Match`. Add it and send the request again.                                                                                                                                                                                                                                                                                                                                 |
| 409    | `quote-revision-stale`                                                                                                                                       | Someone made a new revision of the quote in Omono while your request ran. Read the quote again and apply your changes to its latest revision.                                                                                                                                                                                                                                                  |
| 409    | `line-order-fixed`                                                                                                                                           | Your `lineItemOrder` would change the order of the line items on a sales order that has a dispatch, or a purchase order that has a delivery. Leave out `lineItemOrder`.                                                                                                                                                                                                                        |
| 409    | `quote-locked`, `sales-order-locked`, `credit-note-complete`, `purchase-order-cancelled`                                                                     | The document can't change any more, because it's a Won or Lost quote, a Complete or Cancelled sales order, a Complete credit note or a cancelled purchase order. A request that sends back its current values still succeeds. Show the `detail` to a person.                                                                                                                                   |
| 409    | `line-item-allocated`, `line-item-on-invoiced-dispatch`, `below-delivered-quantity`, `line-item-invoiced-locked`, `purchase-order-locked`, `immutable-field` | The line item can't change in that way any more, for example because it's on a dispatch, a delivery or an invoice, or because you tried to change its stock item. `errors[].field` names the entry in your array, such as `lineItems[1]`. Show the `detail` to a person. Each document's update operation in the [API reference](/docs#tag/sales-orders) says exactly when each code happens.  |
| 422    | Any code                                                                                                                                                     | A field is wrong, and `errors[].field` names the entry in your array and the field, such as `lineItems[1].quantity`. Fix the data before you send the request again.                                                                                                                                                                                                                           |
