# Brix Accounting API — Items

API reference for product and service catalog management (inventory, services, non-inventory items).

**Negative stock:** When oversell is enabled, `qty_on_hand` may be negative and `backorder_qty` tracks open commitments — see [negative-stock.md](./negative-stock.md).

---

## Base URL

```
{BASE_URL}/api/v1/items
```

| Environment | Example |
|-------------|---------|
| Local | `http://127.0.0.1:8000/api/v1/items` |

---

## Authentication

All endpoints require:

| Header | Value |
|--------|-------|
| `Authorization` | `Bearer {token}` |
| `Accept` | `application/json` |
| `Content-Type` | `application/json` | On `POST`, `PUT`, `PATCH` |

Obtain a token via `POST /api/v1/login`. See [auth-and-users.md](./auth-and-users.md).

**Middleware:** `auth:sanctum`, `company` — the user must belong to a company. Records are automatically scoped to that company.

---

## Role permissions

| Action | ADMIN | ACCOUNTANT | MANAGER | CASHIER |
|--------|-------|------------|---------|---------|
| List items | ✓ | ✓ | ✓ | ✓ |
| View item | ✓ | ✓ | ✓ | ✓ |
| Create item | ✓ | ✓ | ✗ | ✗ |
| Update item | ✓ | ✓ | ✗ | ✗ |
| Delete item | ✓ | ✗ | ✗ | ✗ |

---

## Item object

| Field | Type | Read-only | Description |
|-------|------|-----------|-------------|
| `id` | integer | Yes | Primary key |
| `sku` | string | No* | Stock keeping unit (max 80, unique per company) |
| `name` | string | No | Item name (max 250) |
| `description` | string \| null | No | Long-form catalog notes (max 5000); informational only |
| `item_type` | string | No | `INVENTORY`, `SERVICE`, `NON_INVENTORY` |
| `unit_of_measure` | string | No | `UNIT`, `BOX`, `SET`, `PAIR`, `KG`, `LITRE`, `METRE` |
| `unit_price` | decimal string | No | Default retail selling price |
| `standard_cost` | decimal string | No | Reference purchase cost; auto-updated to the bill line `unit_price` when inventory is received; **not** used for FIFO or COGS |
| `dealer_selling_price` | decimal string | No | Reference dealer sell price; **not** used on invoices or bills |
| `cash_price` | decimal string | No | Reference cash sell price; **not** used on invoices or bills |
| `qty_on_hand` | decimal string | **Yes** | Current stock (layers minus open backorders; may be negative when oversold) |
| `backorder_qty` | decimal string | **Yes** | Open backorder units committed by posted oversell invoices |
| `reorder_point` | decimal string | No | Low-stock alert threshold |
| `taxable` | boolean | No | Whether item is subject to tax on sales |
| `active` | boolean | No | Inactive items can be hidden in UI |
| `income_account_id` | integer \| null | No | Sales revenue GL account |
| `expense_account_id` | integer \| null | No | COGS / expense GL account |
| `asset_account_id` | integer \| null | No | Inventory asset GL account |
| `income_account` | object \| null | Yes | Nested account when loaded |
| `expense_account` | object \| null | Yes | Nested account when loaded |
| `asset_account` | object \| null | Yes | Nested account when loaded |
| `image_url` | string \| null | Yes | Authenticated download URL for the catalog image; `null` when no image. Upload via [attachments.md](./attachments.md) (`POST /items/{item}/attachments`). Requires `Authorization: Bearer {token}` to fetch the file. |
| `created_at` | datetime | Yes | — |
| `updated_at` | datetime | Yes | — |

\* `sku` can be updated but must remain unique within the company.

**Defaults on create:** `item_type` = `INVENTORY`, `unit_of_measure` = `UNIT`, `unit_price` = `0`, `standard_cost` = `0`, `dealer_selling_price` = `0`, `cash_price` = `0`, `description` = `null`, `qty_on_hand` = `0`, `reorder_point` = `0`, `taxable` = `true`, `active` = `true`

### Nested account object

When `income_account`, `expense_account`, or `asset_account` is loaded:

```json
{
  "id": 4,
  "account_number": "4000",
  "name": "Sales Revenue",
  "account_type": "INCOME",
  "account_sub_type": "SALES_REVENUE",
  "parent_account_id": null,
  "active": true
}
```

### Item type guide

| `item_type` | Use case | Stock tracked | Typical accounts |
|-------------|----------|---------------|------------------|
| `INVENTORY` | Physical goods | Yes (FIFO) | Income + Asset (+ COGS on post) |
| `SERVICE` | Labour, consulting | No | Income only |
| `NON_INVENTORY` | Expense-like resale items | No | Income + Expense |

---

# Endpoints

## List items

```
GET /items
```

Returns items for the authenticated user's company. Default sort is `name` ascending. Includes linked GL accounts, category, and `image_url` when a catalog image exists. Results are paginated.

### Query parameters

| Parameter | Type | Default | Description |
|-----------|------|---------|-------------|
| `search` | string | — | Match `name`, `sku`, or `description` (partial match) |
| `category_id` | integer | — | Limit to items in this category (same company) |
| `item_type` | string | — | `INVENTORY`, `SERVICE`, or `NON_INVENTORY` |
| `active` | boolean | — | When `true`, only active items; when `false`, only inactive |
| `sort` | string | `name` | Sort field. Prefix with `-` for descending. Allowed: `name`, `sku`, `item_type`, `unit_price`, `standard_cost`, `dealer_selling_price`, `cash_price`, `qty_on_hand`, `active`, `created_at` |
| `per_page` | integer | `50` | Page size (1–100) |
| `page` | integer | `1` | Page number |

All filters are optional. Omit them to list all company items (paginated).

### Example request

```http
GET /api/v1/items?search=widget&item_type=INVENTORY&active=true&per_page=25
Authorization: Bearer {token}
Accept: application/json
```

### Success response — `200 OK`

```json
{
  "data": [
    {
      "id": 1,
      "sku": "WDG-001",
      "name": "Test Widget",
      "description": "Heavy-duty widget for industrial use.",
      "item_type": "INVENTORY",
      "unit_of_measure": "UNIT",
      "unit_price": "150.0000",
      "standard_cost": "80.0000",
      "dealer_selling_price": "120.0000",
      "cash_price": "110.0000",
      "qty_on_hand": "0.0000",
      "reorder_point": "5.0000",
      "taxable": true,
      "active": true,
      "income_account_id": 4,
      "expense_account_id": null,
      "asset_account_id": 3,
      "income_account": {
        "id": 4,
        "account_number": "4000",
        "name": "Sales Revenue",
        "account_type": "INCOME",
        "account_sub_type": "SALES_REVENUE",
        "parent_account_id": null,
        "active": true
      },
      "expense_account": null,
      "asset_account": {
        "id": 3,
        "account_number": "1200",
        "name": "Inventory",
        "account_type": "ASSET",
        "account_sub_type": "INVENTORY_ASSET",
        "parent_account_id": null,
        "active": true
      },
      "image_url": null,
      "created_at": "2026-06-11T10:00:00.000000Z",
      "updated_at": "2026-06-11T10:00:00.000000Z"
    }
  ],
  "links": {
    "first": "http://127.0.0.1:8000/api/v1/items?page=1",
    "last": "http://127.0.0.1:8000/api/v1/items?page=1",
    "prev": null,
    "next": null
  },
  "meta": {
    "current_page": 1,
    "per_page": 50,
    "total": 1,
    "last_page": 1
  }
}
```

---

## Create item

```
POST /items
```

### Request body

| Field | Type | Required | Rules |
|-------|------|----------|-------|
| `sku` | string | **Yes** | Max 80, unique within company |
| `name` | string | **Yes** | Max 250 characters |
| `description` | string | No | Max 5000 characters |
| `unit_price` | number | **Yes** | Min 0 |
| `standard_cost` | number | No | Min 0; reference purchase cost only |
| `dealer_selling_price` | number | No | Min 0; reference dealer price only |
| `cash_price` | number | No | Min 0; reference cash price only |
| `item_type` | string | No | `INVENTORY`, `SERVICE`, `NON_INVENTORY` |
| `unit_of_measure` | string | No | See enum values above |
| `reorder_point` | number | No | Min 0 |
| `taxable` | boolean | No | Default `true` |
| `active` | boolean | No | Default `true` |
| `income_account_id` | integer | No | Must exist in company's chart of accounts |
| `expense_account_id` | integer | No | Must exist in company's chart of accounts |
| `asset_account_id` | integer | No | Must exist in company's chart of accounts |

> Do **not** send `qty_on_hand` — it is set by the system when stock is received or sold.

### Example request — inventory item

```http
POST /api/v1/items
Authorization: Bearer {token}
Content-Type: application/json

{
  "sku": "WDG-001",
  "name": "Test Widget",
  "description": "Heavy-duty widget for industrial use.",
  "item_type": "INVENTORY",
  "unit_of_measure": "UNIT",
  "unit_price": 150.00,
  "standard_cost": 80.00,
  "dealer_selling_price": 120.00,
  "cash_price": 110.00,
  "reorder_point": 5,
  "taxable": true,
  "active": true,
  "income_account_id": 4,
  "asset_account_id": 3
}
```

### Example request — service item

```http
POST /api/v1/items
Authorization: Bearer {token}
Content-Type: application/json

{
  "sku": "SRV-001",
  "name": "Consulting Hour",
  "item_type": "SERVICE",
  "unit_of_measure": "UNIT",
  "unit_price": 75.00,
  "taxable": true,
  "income_account_id": 4
}
```

### Success response — `201 Created`

```json
{
  "data": {
    "id": 1,
    "sku": "WDG-001",
    "name": "Test Widget",
    "item_type": "INVENTORY",
    "unit_of_measure": "UNIT",
    "unit_price": "150.0000",
    "qty_on_hand": "0.0000",
    "reorder_point": "5.0000",
    "taxable": true,
    "active": true,
    "income_account_id": 4,
    "expense_account_id": null,
    "asset_account_id": 3,
    "income_account": { "..." : "..." },
    "expense_account": null,
    "asset_account": { "..." : "..." },
    "image_url": null,
    "created_at": "2026-06-11T10:00:00.000000Z",
    "updated_at": "2026-06-11T10:00:00.000000Z"
  }
}
```

### Error responses

| Status | Condition |
|--------|-----------|
| `403` | User is not ADMIN or ACCOUNTANT |
| `422` | Validation failed (duplicate SKU, invalid account ID, etc.) |

---

## Get item

```
GET /items/{id}
```

### Example request

```http
GET /api/v1/items/1
Authorization: Bearer {token}
Accept: application/json
```

### Success response — `200 OK`

Single item object wrapped in `data` (same shape as create response, with nested accounts).

### Error responses

| Status | Condition |
|--------|-----------|
| `404` | Item not found or belongs to another company |

---

## Update item

```
PUT /items/{id}
PATCH /items/{id}
```

All fields are optional (partial update supported).

### Example request

```http
PATCH /api/v1/items/1
Authorization: Bearer {token}
Content-Type: application/json

{
  "unit_price": 165.00,
  "reorder_point": 10,
  "active": true
}
```

### Success response — `200 OK`

Updated item object in `data`.

### Error responses

| Status | Condition |
|--------|-----------|
| `403` | User is not ADMIN or ACCOUNTANT |
| `404` | Item not found |
| `422` | Validation failed (e.g. duplicate SKU) |

---

## Delete item

```
DELETE /items/{id}
```

Permanently removes the item record.

### Example request

```http
DELETE /api/v1/items/1
Authorization: Bearer {token}
Accept: application/json
```

### Success response — `204 No Content`

Empty body.

### Error responses

| Status | Condition |
|--------|-----------|
| `403` | User is not ADMIN |
| `404` | Item not found |

---

## Validation error format — `422`

```json
{
  "message": "The sku has already been taken.",
  "errors": {
    "sku": ["The sku has already been taken."]
  }
}
```

---

## Endpoint summary

| Method | Path | Description |
|--------|------|-------------|
| `GET` | `/items` | List all items |
| `POST` | `/items` | Create item |
| `GET` | `/items/{id}` | Get item |
| `PUT`/`PATCH` | `/items/{id}` | Update item |
| `DELETE` | `/items/{id}` | Delete item |

---

## Frontend notes

- Seed the chart of accounts first (`php artisan migrate --seed`) before linking `*_account_id` fields.
- Use `GET /api/v1/accounts` (ADMIN/ACCOUNTANT) to populate account dropdowns.
- For `INVENTORY` items, set `asset_account_id` to an `INVENTORY_ASSET` account and `income_account_id` to a `SALES_REVENUE` account.
- `qty_on_hand` updates automatically when bills are received and invoices are posted — show it as read-only in the UI.
- Tax codes are applied at the invoice/bill line level, not on the item record.
- `description`, `dealer_selling_price`, and `cash_price` are catalog reference fields only — they do not affect FIFO, COGS, invoices, or bills. `standard_cost` is also reference-only for costing, but is refreshed to the latest bill line `unit_price` when inventory is received (last line wins if the same item appears twice on one bill). Use `unit_price` as the default retail sell price on new invoice lines.
- Decimal amounts are returned as strings with 4 decimal places (e.g. `"150.0000"`).
