# Data model

The main entities an integration sees and the fields that link them. Every `_id` is a 24-character
string. How the terms differ: [glossary](https://docs.bito.uz/en/guide/glossary.md#confusions).

```text
organization ── warehouse ──┐
                            ├── stock (product × warehouse)
product ────────────────────┘
product × price list × organization ── price

customer ── order (sale-order) ── sale (trade) ── invoice ── transaction
```

## Organization and warehouse

| Entity | References | Read |
|---|---|---|
| Organization (`organization`) | `currency_id` — its accounting currency | [`GET /organization/get-all`](https://docs.bito.uz/en/api/organization.md#get-organization-get-all) |
| Warehouse (`warehouse`) | `organization_id` — belongs to one organization; `is_main` — the main warehouse | [`POST /warehouse/get-all`](https://docs.bito.uz/en/api/warehouse.md#post-warehouse-get-all) |

`warehouse/get-paging` returns an `organization` object instead of `organization_id`.

## Product, price, stock

| Entity | References | Read |
|---|---|---|
| Product (`product`) | `category`, `measure`; `organizations[]` — where it is available; `parent_id` on a variant | [`POST /product/get-paging`](https://docs.bito.uz/en/api/product.md#post-product-get-paging) |
| Price list (`price`) | `currency_id`; `type` — `sale` or `income` | [`GET /price/get-all`](https://docs.bito.uz/en/api/price.md#get-price-get-all) |
| Product price | `product` × `price_id` × organization → `amount` | [`POST /price/items/get-paging`](https://docs.bito.uz/en/api/price.md#post-price-items-get-paging) |
| Stock (`product-warehouse`) | `product_id` × `warehouse_id`, `organization_id` | [`POST /product-warehouse/get-paging`](https://docs.bito.uz/en/api/product-warehouse.md#post-product-warehouse-get-paging) |
| Batch (`product-stock`) | `product`, `warehouse`, cost and its currency | [`POST /product-stock/get-paging`](https://docs.bito.uz/en/api/product-stock.md#post-product-stock-get-paging) |

A product does not belong to a single organization: the catalog is shared, while availability, prices
and stock are per organization. How to read stock: [stock and prices](https://docs.bito.uz/en/guide/recipes/stock-prices.md); what
changes it: [warehouse documents](https://docs.bito.uz/en/guide/concepts/warehouse.md).

## Customer, order, sale, transaction

| Entity | References | Read |
|---|---|---|
| Customer (`customer`) | `organization_ids`, `category_id` | [`POST /customer/get-paging`](https://docs.bito.uz/en/api/customer.md#post-customer-get-paging) |
| Order (`sale-order`) | `organization_id`, `customer_id`, `price_id`; on a line `product_id`, `price_id`, `warehouse_id` | [`GET /sale-order/get-by-id/{id}`](https://docs.bito.uz/en/api/sale-order.md#get-sale-order-get-by-id-id) |
| Sale (`trade`) | `organization`, `customer_id`, `warehouse_id`, `price_id`; `order_id` — the order it came from; `refund_id` — the original sale of a refund | [`GET /trade/get-by-id/{id}`](https://docs.bito.uz/en/api/trade.md#get-trade-get-by-id-id) |
| Sale line | `_id` — the product, `item_id` — the line, `warehouse` | [`GET /trade/get-items/{id}`](https://docs.bito.uz/en/api/trade.md#get-trade-get-items-id) |
| Transaction (`transaction`) | `organization_id`, `cashbox_id`, `payment_method`, `payment_type`; `customer_id`, `trade_id`, `invoice_id` | [`POST /transaction/get-paging`](https://docs.bito.uz/en/api/transaction.md#post-transaction-get-paging) |

- `sale-order` is a customer's order, `trade` is the sale itself. How one becomes the other:
  [the sale lifecycle](https://docs.bito.uz/en/guide/concepts/sale-lifecycle.md).
- A transaction is a movement of money. On a credit sale the payment can arrive later or in parts, so
  count sale totals and cash movement separately.

## Storing it in your system

- Link records by `_id`. Names and numbers (`number`, `uuid`) are for display.
- Deletes are [soft](https://docs.bito.uz/en/guide/api.md#soft-delete): a stored `_id` can point at a deleted record. Products,
  warehouses, customers and orders have a `get-deleted` method.
- Track changes with the `updated_at` filter (where a method has it) and [webhooks](https://docs.bito.uz/en/guide/webhooks.md).
