# Warehouse documents

You cannot write stock directly through the API — it changes as a result of documents. The counters per
product × warehouse are `amount` (available to sell), `booked`, `in_transit`, `on_the_way` and
`in_trash` — see [stock and prices](https://docs.bito.uz/en/guide/recipes/stock-prices.md).

## What the API exposes

| Document | Create | State changes | Read permission |
|---|---|---|---|
| Order to a supplier (`purchase-order`) | Yes | `set-state` | `purchase_order` |
| Purchase (`purchase`) | Yes | `set-state` | `purchase` |
| Income (`income`) | Yes, against a purchase | `cancel` | `income` |
| Return to a supplier (`purchase-return`) | No | No | `purchase_return` |
| Internal transfer (`internal-transfer`) | Yes | `set-status`, `receive` | `internal_transfer` |
| Transfer between organizations (`organizational-transfer`) | Yes | `set-status`, `receive` | `organizational_transfer` |
| Write-off (`write-off`) | Yes | `set-status` | `write_off` |
| Stocktaking (`revision`) | Yes | `set-status` | `revision_customer_cashback` |

The exact permission of every method is in [Permissions](https://docs.bito.uz/en/api/permissions.md). All of them have
`get-paging` and `get-by-id/{id}`. `create` methods are not idempotent.

## Order to a supplier

`new` → `accepted` or `in_progress` → `done`; `canceled` only from `new`. `state: "new"` is required on
create. **No effect on stock.**

Methods: [`create`](https://docs.bito.uz/en/api/purchase-order.md#post-purchase-order-create),
[`set-state`](https://docs.bito.uz/en/api/purchase-order.md#put-purchase-order-set-state).

## Purchase and income

A purchase goes `new` → `in_progress` → `done`; `canceled` from any state. It is created as `new` or
`in_progress`, and `update` works only while `new`.

| Action | Stock |
|---|---|
| Purchase → `in_progress` | `on_the_way` goes up |
| Purchase → `done` | Stock does not change: the supplier balance and an invoice are recorded |
| Creating an income | `on_the_way` goes down, `amount` goes up in the income's warehouse |
| Cancelling an income | The reverse. If the goods were already used, it fails |
| Cancelling a purchase | `on_the_way` is rolled back; for a `done` purchase its incomes are cancelled too. If an `in_progress` purchase is already partly received, cancel those incomes first |

::: tip Which warehouse holds on_the_way
`on_the_way` is recorded on the organization's first warehouse, which may not be the warehouse named in
the purchase. If you need a product total, add the warehouses up.
:::

Through the API an income is created only against a purchase: it needs `purchases[].purchase_id` and a
`purchase_item_id` on every line, and the purchase must have been moved to `in_progress`. An income is
`done` at once. A purchase created with `is_auto_income: true` becomes `done` and records its income
itself.

Methods: [`purchase/create`](https://docs.bito.uz/en/api/purchase.md#post-purchase-create),
[`purchase/set-state`](https://docs.bito.uz/en/api/purchase.md#put-purchase-set-state),
[`income/create`](https://docs.bito.uz/en/api/income.md#post-income-create),
[`income/cancel`](https://docs.bito.uz/en/api/income.md#put-income-cancel).

## Return to a supplier

States: `done`, `canceled`. A return lowers `amount` in the warehouse. The API is read-only here:
[`get-paging`](https://docs.bito.uz/en/api/purchase-return.md#post-purchase-return-get-paging) — the document is created in
Bito.

## Transfers

An internal transfer moves goods between the warehouses of one organization; an organizational transfer
sends them to another organization's warehouse. Both are created with `state: "new"` and can be edited
while `new`.

| Transition | How | Stock |
|---|---|---|
| `new` → `accepted` | `set-status` | In the sending warehouse `amount` goes down, `in_transit` goes up |
| `accepted` → `in_progress` | `set-status` | No change |
| `accepted`, `in_progress` → `done` | `receive` | Sender's `in_transit` goes down, receiver's `amount` goes up |
| → `canceled` | `set-status` | From `new`, no change; otherwise the goods return to the sending warehouse |

`done` cannot be set with `set-status` — only `receive` does it.

Mind two differences:

| | `set-status` body | `receive` body |
|---|---|---|
| Internal | `{ "status": "accepted" }` | empty |
| Organizational | `{ "state": "accepted" }` | `{ "receiver_warehouse_id": "…" }` — a warehouse of the receiving organization |

Methods: [Internal transfer](https://docs.bito.uz/en/api/internal-transfer.md),
[Organizational transfer](https://docs.bito.uz/en/api/organizational-transfer.md).

## Write-off

Created directly as `done`; the only transition is to `canceled`. `amount` goes down; for `defect`,
`in_trash` goes up. Step by step: [write-off recipe](https://docs.bito.uz/en/guide/recipes/write-off.md).

## Stocktaking

`pending` → `in_progress` → `done`; `canceled` from `pending` or `in_progress`. A `done` document cannot
be cancelled. `update` works only while `pending`. Pass the state to `set-status` as
`{ "status": "…" }`.

Creating it and moving it to `in_progress` do not change stock. `done` sets the stock of every listed
product to its **counted quantity** and issues invoices for the surplus or shortage.

::: danger Do not complete it uncounted
The API has no method for entering counted quantities. Without `set_counted`, the counted quantity of
every line is `0`: if you set `done` before staff have counted in Bito, **the stock of the listed
products drops to zero**, and it cannot be cancelled.
:::

Methods: [Revision](https://docs.bito.uz/en/api/revision.md).

## Webhooks

There are events for `purchaseOrders`, `purchases`, `purchaseReturns` and `incomes`
([webhooks](https://docs.bito.uz/en/guide/webhooks.md)). There are none for transfers, write-offs or stocktaking — poll them with
`get-paging`. An app that declares `required_permissions` cannot subscribe to webhooks.

Sales documents are not covered here — see [the sale lifecycle](https://docs.bito.uz/en/guide/concepts/sale-lifecycle.md).
