# Recipe: external sale

The sale has **already happened** at your till or on a marketplace, and it has to show up in Bito under
**Sale › Sales**, in stock and in the cashbox. That is a `trade`. For an order that is not sold yet, see
the [online order recipe](https://docs.bito.uz/en/guide/recipes/online-order.md).

**Permissions needed:** `sale_create`, `sale`, `employee`, `currency`, `payment_methods`,
`product_stock`; `customer`, `customer_create` if you write customers; `sale_update` for refunds.
`organization/get-all` is always allowed.

## 1. Collect the ids

Fetch them once and map them to your own values:

| What | Method |
|---|---|
| `organization_id` and currency | `GET /organization/get-all`, `GET /currency/get-by-id/{id}` |
| `responsible_id` — the seller | `POST /employee/get-paging` |
| Payment methods | `GET /payment-method/get-all` |
| Product, warehouse, stock | `POST /product-warehouse/get-paging` |

If you need a customer, find or create one as in
[steps 2–3 of the online order recipe](https://docs.bito.uz/en/guide/recipes/online-order.md).

## 2. First check it is not recorded yet

`trade/create` is not idempotent. Put your system's sale number in `code` and search for it before
every send (especially a retry):

```json
POST /trade/get-paging

{ "page": 1, "limit": 20, "search": "MP-10482" }
```

`search` matches part of `code`, `number` and `uuid`, so check yourself that `code` is an exact match.
Bito does not enforce a unique `code`. Do not send one sale from two processes in parallel.

## 3. Record the sale

```json
POST /trade/create

{
    "code": "MP-10482",
    "organization_id": "6712f0a9c4d1e87b3a0f9911",
    "responsible_id": "6712f0a9c4d1e87b3a0f9944",
    "customer_id": "6712f0a9c4d1e87b3a0f9922",
    "state": "done",
    "date": "2026-11-30T14:25:00+05:00",
    "currency": { "_id": "6712f0a9c4d1e87b3a0f9955", "name": "UZS", "value": 1 },
    "products": [
        {
            "product_id": "6712f0a9c4d1e87b3a0f9933",
            "warehouse_id": "6712f0a9c4d1e87b3a0f9966",
            "amount": 2,
            "price": 45000
        }
    ],
    "payments": [
        {
            "amount": 90000,
            "payment_method": { "_id": "6712f0a9c4d1e87b3a0f9977", "name": "Card" },
            "currency": { "_id": "6712f0a9c4d1e87b3a0f9955", "name": "UZS", "value": 1 }
        }
    ],
    "note": "Marketplace #10482"
}
```

Required: `organization_id`, `responsible_id`, `state`, `currency` (`_id`, `name`, `value`),
`payments` (`[]` when nothing was paid) and at least one `products` line (`product_id`, `amount`,
`price`).

| Field | Rule |
|---|---|
| `state` | `done` for a completed sale. Other states: [lifecycle](https://docs.bito.uz/en/guide/concepts/sale-lifecycle.md#trade) |
| `price` | Unit price — the one actually sold at |
| Line `warehouse_id` | When left out, the organization's main warehouse |
| `date` | When left out, now. A future date is rejected (`10031`); a past date may be disabled in sale settings (`149030`) |
| `currency.value` | A required number; the server sets the rate itself |
| `payments[]` | Several payments are allowed (cash + card). For a payment in another currency, `amount` is in that currency and `paid` in the sale currency; without `paid` the server converts at the current rate |
| Cashbox | Cannot be chosen: money goes to the organization cashbox of the user the call runs as (the owner, or the `user_id` header) |

::: warning Cashbox session
If the organization requires a cashbox session, `trade/create` and `trade/refund` return `code: 236014`:
these methods do not accept `cashbox_session_id`.
:::

## 4. Check the response

Keep `_id`, `number`, `total_to_pay` and `debt` from `data`. The **server** computes `total_to_pay`:
a tax, a promotion or the minimum price rule can make it differ from your amount. So compare
`total_to_pay` with the amount in your system.

When the payment is below `total_to_pay`, the difference becomes **debt**: a customer is required
(`149005`), the call is rejected if debt sales are off (`149024`), and `paying_date` is needed if a
debt return date is required (`149036`).

## 5. What changes

- **Stock:** for a `done` sale `amount` goes down. For `new` or `in_progress` the goods move to
  `booked`. With not enough stock the sale is not recorded (`149003`).
- **Money:** each `payments` entry creates an income transaction into the cashbox — whatever the state.
- **Customer:** debt is written to the balance; cashback is earned if enabled.
- **Promotions:** active promotions apply automatically — a free gift line may be added.
- **Fiscalization:** if the organization has it enabled, a `done` sale is also sent for fiscalization.
  If the marketplace already issues the receipt, agree this with your customer first.

## Refund

A refund is a separate `trade` (`is_refund: true`); it does not replace the original. If the sale was
recorded by mistake, [cancel](https://docs.bito.uz/en/guide/concepts/sale-lifecycle.md#cancel) it instead.

Get a refund reason (an integration with declared permissions cannot call this method):

```json
POST /reason/get-paging

{ "page": 1, "limit": 50, "type": "customer_return" }
```

Read the original with `GET /trade/get-by-id/{id}`. The `_id` of a refund line is the original line's
**`item_id`** (`products[]._id` in the response is the product id):

```json
POST /trade/refund

{
    "refund_id": "6712f0a9c4d1e87b3a0f9988",
    "reason_id": "6712f0a9c4d1e87b3a0f99aa",
    "organization_id": "6712f0a9c4d1e87b3a0f9911",
    "responsible_id": "6712f0a9c4d1e87b3a0f9944",
    "currency": { "_id": "6712f0a9c4d1e87b3a0f9955", "name": "UZS", "value": 1 },
    "products": [
        {
            "_id": "6712f0a9c4d1e87b3a0f99bb",
            "product_id": "6712f0a9c4d1e87b3a0f9933",
            "warehouse_id": "6712f0a9c4d1e87b3a0f9966",
            "amount": 1,
            "price": 45000
        }
    ],
    "payments": [
        {
            "amount": 45000,
            "payment_method": { "_id": "6712f0a9c4d1e87b3a0f9977", "name": "Card" },
            "currency": { "_id": "6712f0a9c4d1e87b3a0f9955", "name": "UZS", "value": 1 }
        }
    ]
}
```

`payments` is the money returned to the customer. The goods go back to the warehouse; if the reason
has `is_trash` on, they go to defective stock instead of sellable stock. You cannot return more than
was sold (`149004`). `trade/refund` is not idempotent either.

## Errors

| Response | Cause |
|---|---|
| `400`, `code: 10001` | Validation — read `message` |
| `403`, `code: 10003` | One of the permissions was not approved; `data` names it |
| `400`, `code: 149003` | Not enough stock |
| `400`, `code: 149016` | For a marked product, the number of `marks` differs from `amount` |
| `400`, `code: 149021` | Sale settings require a customer |
| `400`, `code: 26022` | The product is not available in this organization |

Full list: [errors](https://docs.bito.uz/en/guide/errors.md).

## Methods

| Step | Method |
|---|---|
| Seller | [`POST /employee/get-paging`](https://docs.bito.uz/en/api/employee.md#post-employee-get-paging) |
| Payment method | [`GET /payment-method/get-all`](https://docs.bito.uz/en/api/payment-method.md#get-payment-method-get-all) |
| Stock | [`POST /product-warehouse/get-paging`](https://docs.bito.uz/en/api/product-warehouse.md#post-product-warehouse-get-paging) |
| Duplicate check | [`POST /trade/get-paging`](https://docs.bito.uz/en/api/trade.md#post-trade-get-paging) |
| Sale | [`POST /trade/create`](https://docs.bito.uz/en/api/trade.md#post-trade-create) |
| Check | [`GET /trade/get-by-id/{id}`](https://docs.bito.uz/en/api/trade.md#get-trade-get-by-id-id) |
| Refund | [`POST /trade/refund`](https://docs.bito.uz/en/api/trade.md#post-trade-refund) |
