# updateSalesOrderItems mutation


`updateSalesOrderItems` is a GraphQL mutation of the Chameleoon MAX API. Send it as `POST https://max-api.chameleoon.sk/api/v1/graphql` with your API key, see [Chameleoon MAX](/chameleoon-max).

Updates the order's items and charges to match the external source's current ones. Call it
when items, quantities, prices or charges change in the external source after the order was
placed.

Send all of the order's items and charges as the external source has them now, priced as for
[`createSalesOrder`](/chameleoon-max/mutation/create-sales-order), together with the total. MAX matches them to the order as
follows:

- Items match lines by each item's `externalId`, as the order was created with it. The ids
  belong to the external source's order that this order was placed as. When you name the order
  by its MAX `id`, that is the external order the MAX order holds.
- Charges match by `kind` and `code`, or by `kind` and `name` when a charge has no code.

Before packing, MAX applies the change. It adds a new item at your price, removes an item you
no longer send, and changes a quantity, price or charge. It leaves alone any line or charge
that the external source never sent.

In the following cases MAX keeps its own items and opens a `SHOP_CHANGE_*` exception on the
order, with your items in the exception's data.

| Outcome | Blocks the order | When |
| --- | --- | --- |
| `WARNING` | No | The order has shipped, or a consignment has a label without cash on delivery. |
| `UNMAPPED` | Yes | A new item matches no product in MAX. |
| `CONFLICT` | Yes | A user changed the items in MAX. |
| `NEEDS_REVIEW` | Yes | A consignment is packed or has a label. |
| `REFUSED` | Yes | MAX refused a step part way through, or the currency cannot change (see Currency). |

After the change, MAX compares `expectedTotal` with the order's total (`TOTAL_MISMATCH`).
Money paid above the new total gives the warning `SHOP_ORDER_OVERPAID`.

#### Currency

Send `currencyCode` when the external source restates the order's currency, for example after it
read its own document in the wrong currency. MAX changes the currency together with your prices,
and only while the order merely waits:

- the warehouse has not started on it: every consignment is open, not being picked and not claimed
  by a packer, and there is no shipment or shipment draft;
- no payment or refund is recorded on it;
- no user changed its items in MAX;
- MAX did not convert its prices under a VAT regime;
- its price list, if any, is in the new currency.

Otherwise MAX keeps the order as it is and opens the blocking exception `SHOP_CHANGE_CURRENCY`
with the outcome `REFUSED`. After the order has shipped, or once another order was merged into
it, the change is a warning only (`SHOP_CHANGE_AFTER_SHIPPING`, `SHOP_CHANGE_CONFLICT`). If a later step is refused after MAX took the currency, the
exception `SHOP_CHANGE_REFUSED` says so; send the items again, or a user in MAX takes them from
the exception.

#### Permissions

Requires the `order.write` and `order.read` permissions.

#### Repeated calls

A repeat changes nothing and returns `UNCHANGED`.

#### Errors

- `VALIDATION` when an item is invalid by the rules of [`createSalesOrder`](/chameleoon-max/mutation/create-sales-order) or has
  no `externalId`.
- `VALIDATION` when you name the order by `id` and it holds orders from several external
  sources. Name it by `source` and `externalId` instead.
- `CONFLICT` when the order is cancelled or merged.
- `CONFLICT` with `extensions.reason` `ITEMS_SYNC_IN_PROGRESS` when another change of the
  order's items is being applied. Send the request again shortly.
- `NOT_FOUND` when the order does not exist or your API key cannot see it.

> An error whose path runs below this field does not mean the call failed: the items were
> processed, so do not retry. See [`Mutation`](/chameleoon-max/mutations).

Costs 1 point plus 1 per item and 1 per charge, plus its selection.

## Arguments

- `ref`: [`SalesOrderRefInput!`](/chameleoon-max/type/sales-order-ref-input) · required
- `input`: [`SalesOrderItemsInput!`](/chameleoon-max/type/sales-order-items-input) · required

## Returns

[`SalesOrderItemsResult!`](/chameleoon-max/type/sales-order-items-result)

## Example

**Mutation**

```graphql
mutation updateSalesOrderItems($ref: SalesOrderRefInput!, $input: SalesOrderItemsInput!) {
  updateSalesOrderItems(ref: $ref, input: $input) {
    salesOrder {
      id
      number
      status
      legalEntityId
      source
      externalId
    }
    items
    warnings {
      code
      message
      field
    }
  }
}
```

**Variables**

```json
{
  "ref": {
    "id": "00000000-0000-4000-8000-000000000001"
  },
  "input": {
    "pricesIncludeVat": true,
    "items": [
      {
        "name": "example",
        "quantity": "10.00",
        "unitPrice": "10.00",
        "vatPercent": "10.00"
      }
    ]
  }
}
```

**Response**

```json
{
  "data": {
    "updateSalesOrderItems": {
      "salesOrder": {
        "id": "00000000-0000-4000-8000-000000000001",
        "number": "example",
        "status": "DRAFT",
        "legalEntityId": "00000000-0000-4000-8000-000000000001",
        "source": "example",
        "externalId": "example"
      },
      "items": "APPLIED",
      "warnings": [
        {
          "code": "example",
          "message": "example",
          "field": "example"
        }
      ]
    }
  }
}
```

Values are illustrative: replace IDs and codes with your own before you send the call.
