# First sale

The step after [Quick start](https://docs.bito.uz/en/guide/quick-start.md): record one real sale and see it in the admin panel.

::: warning This is a real sale
Warehouse stock goes down and money lands in a cashbox. Use a test account, or
[cancel the sale](#cancel) at the end.
:::

## 0. Install the integration

A new custom integration is created but not yet **installed**. Open it in the **Integration** section
of the admin panel and press **Install**. Until then requests return `400`, `code: 115004`.

For this walkthrough leave the integration's `required_permissions` list empty: an integration that
declares a list cannot call `profile/getMe` from step 2.

All paths below are relative to the base URL, with the `api-key` header:

```
https://api.bito.uz/integration-api/integration/api/v2/
```

## 1. Organization

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

From the `data` list, take the organization's `_id` and `currency_id`.

## 2. Seller

```http
GET /profile/getMe
```

`data._id` is the account owner. It becomes the sale's `responsible_id`.

## 3. Currency and payment method

```http
GET /currency/get-by-id/{currency_id}
GET /payment-method/get-all
```

Take the currency's `name` from the first response, and a payment method's `_id` and `name` from the
second (`data.data`).

## 4. A product in stock

```json
POST /product-warehouse/get-paging

{ "page": 1, "limit": 50, "organization_id": "6712f0a9c4d1e87b3a0f9911" }
```

Pick a row with `amount` above zero and take its `product_id` and `warehouse_id`. Choose a plain
product: a marked one also needs `marks`. To see its name, call `GET /product/get-by-id/{id}`.

## 5. Record the sale

You send the price. The payment equals the sum of the lines:

```json
POST /trade/create

{
    "code": "TEST-1",
    "organization_id": "6712f0a9c4d1e87b3a0f9911",
    "responsible_id": "6712f0a9c4d1e87b3a0f9944",
    "state": "done",
    "currency": { "_id": "6712f0a9c4d1e87b3a0f9955", "name": "UZS", "value": 1 },
    "products": [
        {
            "product_id": "6712f0a9c4d1e87b3a0f9933",
            "warehouse_id": "6712f0a9c4d1e87b3a0f9966",
            "amount": 1,
            "price": 45000
        }
    ],
    "payments": [
        {
            "amount": 45000,
            "payment_method": { "_id": "6712f0a9c4d1e87b3a0f9977", "name": "Cash" },
            "currency": { "_id": "6712f0a9c4d1e87b3a0f9955", "name": "UZS", "value": 1 }
        }
    ]
}
```

`currency.value` is a required number, but the server sets the rate itself.

Keep `_id`, `number` and `uuid` from the response's `data`, and check that `state` is `done` and
`debt` is `0`.

::: warning Retries
`trade/create` is not idempotent. If no response came back, before retrying check with
`POST /trade/get-paging` (`{ "page": 1, "limit": 20, "search": "TEST-1" }`) that the sale was not
recorded.
:::

## 6. See the result

In the admin panel: **Sale › Sales**. Through the API:

```http
GET /trade/get-by-id/{id}
```

## Cancel the test sale

```json
PUT /trade/set-state

{ "_id": "6712f0a9c4d1e87b3a0f9988", "state": "canceled" }
```

Canceling a sale that has no customer returns the goods to the warehouse and cancels the payment too.

## If it fails

| Response | Cause |
|---|---|
| `400`, `code: 115004` | The integration is not installed — step 0 |
| `400`, `code: 10001` | A required field is missing or wrong — read `message` |
| `400`, `code: 149003` | Not enough stock in the warehouse |
| `400`, `code: 149021` | Sale settings require a customer — send `customer_id` |
| `400`, `code: 149005` or `149024` | The payment is below `total_to_pay` (for example, a tax was added to the product) |
| `400`, `code: 236014` | The organization requires a cashbox session — a sale cannot be recorded in it through the Integration API |

Other codes: [errors](https://docs.bito.uz/en/guide/errors.md).

## Next

- [Recipe: record an external sale](https://docs.bito.uz/en/guide/recipes/sale.md) — customer, duplicate protection, refund
- [Sale and order states](https://docs.bito.uz/en/guide/concepts/sale-lifecycle.md)
- [API reference: Trade](https://docs.bito.uz/en/api/trade.md#post-trade-create)
