# Recipe: online order

A website, bot or courier app takes an order, and it has to show up in Bito under **CRM › Orders**. In
Bito that is a `sale-order` — a customer's order, not yet a sale ([glossary](https://docs.bito.uz/en/guide/glossary.md#confusions)).

**Permissions needed:** `customer`, `customer_create`, `price`, `sale_order_create`.
`organization/get-all` is always allowed.

## 1. Find your organization

An order belongs to one organization. Fetch its id once and keep it.

```http
GET /organization/get-all
```

## 2. Find the customer

```http
GET /customer/get-by-phone-number?phone_number=901234567
```

The number is normalised: 9 digits get `+998`, anything else gets a leading `+`. When no customer
matches, `data` is `null`.

## 3. Not found — create the customer

```json
POST /customer/create

{
    "type": "natural",
    "name": "Aziz Karimov",
    "phone_number": "+998901234567",
    "organization_ids": ["6712f0a9c4d1e87b3a0f9911"]
}
```

`type` is `natural` (a person) or `legal` (a company). Send the phone, or step 2 will not find the
customer next time. Keep the `_id` from the response.

## 4. Get the price

Bito does **not** look up the price of an order line — you send `price`. Take it from a price list:

```json
POST /price/items/get-paging

{ "page": 1, "limit": 200, "organization_id": "…", "price_id": "…" }
```

Each row has the product's `amount`. The row carries no currency: it is the price list's `currency_id`
(`price/get-by-id`). Syncing prices with the catalog: [catalog recipe](https://docs.bito.uz/en/guide/recipes/catalog-sync.md).

## 5. Create the order

The smallest valid body:

```json
POST /sale-order/create

{
    "organization_id": "6712f0a9c4d1e87b3a0f9911",
    "customer_id": "6712f0a9c4d1e87b3a0f9922",
    "products": [
        { "product_id": "6712f0a9c4d1e87b3a0f9933", "amount": 2, "price": 45000 }
    ],
    "delivery_address": "Tashkent, Chilanzar 9",
    "note": "Web #10482"
}
```

The server fills in the rest:

| Field | When you leave it out |
|---|---|
| `state`, `state_id` | `in_progress`, in the pipeline's first regular column |
| `responsible_id` | The user the call runs as — usually the account owner |
| `currency_id` | The organization's currency |
| `date` | Now |
| Line `price_id` | The main sale price list |
| Line `warehouse_id` | The organization's main warehouse |

To pick another column, send `state` and `state_id` **together** — a lone `state_id` is overwritten.
If the organization has no pipeline columns, `create` returns an error.

Store the response's `_id` next to your own order; `uuid` (like `A123`) is the order number in Bito.

::: warning Retries
`create` is not idempotent: a repeated call creates a second order. If no response came back, first
read to check whether the order exists (for example by your own order number in `note`).
:::

## 6. Is stock reserved?

Only when Bito's sale setting to reserve goods in orders (`booking_product_in_order`) is on and the
order is `new`, `accepted` or `in_progress`. Then the warehouse `amount` goes down and `booked` goes up.
The setting is off by default, so an order does not touch stock.

## 7. Follow the status

Subscribe to the `saleOrders.status_change` and `saleOrders.update` webhooks ([webhooks](https://docs.bito.uz/en/guide/webhooks.md))
and re-read the order with `GET /sale-order/get-by-id/{id}` when one arrives. A webhook is a signal,
not the data: reconcile with `sale-order/get-paging` from time to time.

## 8. Turn it into a sale (optional)

Usually Bito staff sell the order. If your system records the sale too, send `order_id` to
`POST /trade/create`, and each line's `order_item_id` from the order. The reservation is released, the
sold quantities are written to the order lines, and a fully sold order becomes `done`. Full body:
[Trade](https://docs.bito.uz/en/api/trade.md#post-trade-create). `order_item_id` is the line's `item_id` in the order response (the line's `_id` is the product id).

## Errors

| Response | Cause |
|---|---|
| `400` validation | A required field is missing or an id is wrong — read `message` |
| `403`, `code: 10003` | One of the permissions was not approved; `data` names it |
| `404` | Wrong method: `create` is `POST`, `get-by-phone-number` is `GET` |

## Methods

| Step | Method |
|---|---|
| Organization | [`GET /organization/get-all`](https://docs.bito.uz/en/api/organization.md) |
| Find customer | [`GET /customer/get-by-phone-number`](https://docs.bito.uz/en/api/customer.md#get-customer-get-by-phone-number) |
| Create customer | [`POST /customer/create`](https://docs.bito.uz/en/api/customer.md#post-customer-create) |
| Price | [`POST /price/items/get-paging`](https://docs.bito.uz/en/api/price.md#post-price-items-get-paging) |
| Order | [`POST /sale-order/create`](https://docs.bito.uz/en/api/sale-order.md#post-sale-order-create) |
| Check | [`POST /sale-order/get-paging`](https://docs.bito.uz/en/api/sale-order.md#post-sale-order-get-paging) |
| Sale | [`POST /trade/create`](https://docs.bito.uz/en/api/trade.md#post-trade-create) |
