# syncInvoice mutation


`syncInvoice` 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).

Writes the accounting system's document into MAX. Call it whenever the document is created or
changes in the accounting system, and always send the whole document.

The first call creates the invoice. Every later call replaces every field of the input, the
lines, the VAT summary and the settlements. The internal note and the tags, set in MAX or
through [`modifyInvoiceTags`](/chameleoon-max/mutation/modify-invoice-tags), are never touched.

#### Permissions

Requires the `invoice.sync` permission. MAX creates a partner only when your API key also has
the `partner.write` permission. Without `invoice.read`, the result's `internalNote` is null.

#### Repeated calls

`source` and `externalId` identify the document, so a retry is safe.

- A call with the same document writes nothing and returns `changed: false`.
- A call whose `sourceChangedAt` is older than the one MAX holds writes nothing and returns
  `changed: false` with `reason` `STALE`.

#### Errors

- `CONFLICT` with `extensions.reason` `KIND_CHANGED` when `kind` or `direction` differs from
  the first call.
- `VALIDATION`, with `extensions.field` naming the input, when the input is invalid. For
  example: `source` is unknown, archived or not of kind `ERP`; on an issued `INVOICE`,
  `number` is over 100 characters or `externalId` over 200; correction fields are sent on a
  document that is not a credit note; `supplierNumber` or `receivedDate` is sent on an issued
  invoice; `partner.id` names a partner your API key cannot see; or `links` names a document
  your API key cannot see.
- `INTERNAL` when the invoice's links to sales orders kept changing during the call. Send the
  same request again.

#### Warnings

| Code | When |
| --- | --- |
| `PARTNER_MATCHED_BY_REGISTRATION_ID` | MAX found the partner by the party's company registration number (IČO). |
| `PARTNER_MATCHED_BY_EMAIL` | MAX found the partner by the party's e-mail. |
| `PARTNER_MATCHED_SEVERAL` | Several partners matched. MAX took the oldest. |
| `PARTNER_NOT_CREATED` | No partner matched, and MAX could not create one from the party, or your API key lacks `partner.write`. The invoice has no partner. |
| `ORDER_MATCHED_SEVERAL` | `orderNumber` names several sales orders. MAX linked none. |
| `LINE_NOT_MATCHED` | No code of the line matched a product variant. |

#### Good to know

- Dates are calendar days. MAX keeps the date part of each value and returns it as midnight
  UTC.
- When an issued invoice's `orderNumber` changes, MAX removes its `AUTO` links to sales orders
  that the new number does not name.
- This call may change a linked sales order's invoice reference, which follows the order's
  governing invoice (`SalesOrder.governingInvoice`).

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

## Arguments

- `input`: [`SyncInvoiceInput!`](/chameleoon-max/type/sync-invoice-input) · required

## Returns

[`InvoiceSyncResult!`](/chameleoon-max/type/invoice-sync-result)

## Example

**Mutation**

```graphql
mutation syncInvoice($input: SyncInvoiceInput!) {
  syncInvoice(input: $input) {
    invoice {
      id
      direction
      kind
      status
      failureReason
      source
    }
    changed
    reason
    warnings {
      code
      message
      field
    }
  }
}
```

**Variables**

```json
{
  "input": {
    "source": "example",
    "externalId": "example",
    "direction": "ISSUED",
    "kind": "INVOICE",
    "number": "example",
    "currencyCode": "example",
    "totalNet": "10.00",
    "totalVat": "10.00",
    "totalGross": "10.00",
    "amountDue": "10.00"
  }
}
```

**Response**

```json
{
  "data": {
    "syncInvoice": {
      "invoice": {
        "id": "00000000-0000-4000-8000-000000000001",
        "direction": "ISSUED",
        "kind": "INVOICE",
        "status": "PENDING",
        "failureReason": "example",
        "source": "example"
      },
      "changed": true,
      "reason": "STALE",
      "warnings": [
        {
          "code": "example",
          "message": "example",
          "field": "example"
        }
      ]
    }
  }
}
```

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