# Brix Accounting API — Item Categories

API reference for organizing products and services into hierarchical categories. Categories are used for catalog grouping and reporting — they do **not** post to the general ledger.

Similar to QuickBooks Online, each item may belong to at most one category. Assign categories on items via `category_id` — see [items.md](./items.md).

---

## Base URL

```
{BASE_URL}/api/v1/item-categories
```

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

---

## 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 categories | ✓ | ✓ | ✓ | ✓ |
| View category | ✓ | ✓ | ✓ | ✓ |
| Create category | ✓ | ✓ | ✗ | ✗ |
| Update category | ✓ | ✓ | ✗ | ✗ |
| Delete category | ✓ | ✗ | ✗ | ✗ |

---

## Item category object

| Field | Type | Read-only | Description |
|-------|------|-----------|-------------|
| `id` | integer | Yes | Primary key |
| `name` | string | No | Category name (max 100, unique among siblings) |
| `parent_category_id` | integer \| null | No | Parent category for hierarchy; `null` = top level |
| `parent` | object \| null | Yes | Nested parent when loaded |
| `level` | integer | Yes | Depth in hierarchy (`1` = top level, max `3`) |
| `fully_qualified_name` | string | Yes | Colon-separated path (e.g. `Products:Hardware:Laptops`) |
| `active` | boolean | No | Inactive categories can be hidden in UI |
| `children` | array | Yes | Nested child categories (only when `?tree=1` on list) |
| `created_at` | datetime | Yes | — |
| `updated_at` | datetime | Yes | — |

**Defaults on create:** `active` = `true`, `parent_category_id` = `null`

### Hierarchy rules

| Rule | Detail |
|------|--------|
| Max depth | 3 levels (root → sub → sub-sub) |
| Sibling names | Must be unique under the same parent |
| Parent scope | `parent_category_id` must belong to the same company |
| Cycles | A category cannot be moved under its own descendant |

### Nested parent object

When `parent` is loaded (list, show, create, and update responses):

```json
{
  "id": 1,
  "name": "Products",
  "parent_category_id": null,
  "parent": null,
  "level": 1,
  "fully_qualified_name": "Products",
  "active": true,
  "created_at": "2026-06-12T10:00:00.000000Z",
  "updated_at": "2026-06-12T10:00:00.000000Z"
}
```

---

# Endpoints

## List categories

```
GET /item-categories
```

Returns all categories for the authenticated user's company, sorted by `name`. Includes the immediate `parent` object.

Unlike `GET /items`, this endpoint is **not paginated** — it returns the full collection in `data`.

### Query parameters

| Param | Type | Description |
|-------|------|-------------|
| `tree` | boolean | When `true` or `1`, returns top-level categories with nested `children` arrays |
| `parent_id` | integer \| `null` | Filter by parent. Use `parent_id=null` for top-level categories only |
| `active` | boolean | Filter by active status (`true` or `false`) |

### Example request — flat list

```http
GET /api/v1/item-categories
Authorization: Bearer {token}
Accept: application/json
```

### Success response — `200 OK`

```json
{
  "data": [
    {
      "id": 2,
      "name": "Hardware",
      "parent_category_id": 1,
      "parent": {
        "id": 1,
        "name": "Products",
        "parent_category_id": null,
        "parent": null,
        "level": 1,
        "fully_qualified_name": "Products",
        "active": true,
        "created_at": "2026-06-12T10:00:00.000000Z",
        "updated_at": "2026-06-12T10:00:00.000000Z"
      },
      "level": 2,
      "fully_qualified_name": "Products:Hardware",
      "active": true,
      "created_at": "2026-06-12T10:05:00.000000Z",
      "updated_at": "2026-06-12T10:05:00.000000Z"
    },
    {
      "id": 1,
      "name": "Products",
      "parent_category_id": null,
      "parent": null,
      "level": 1,
      "fully_qualified_name": "Products",
      "active": true,
      "created_at": "2026-06-12T10:00:00.000000Z",
      "updated_at": "2026-06-12T10:00:00.000000Z"
    }
  ]
}
```

### Example request — tree view

```http
GET /api/v1/item-categories?tree=1
Authorization: Bearer {token}
Accept: application/json
```

### Success response — `200 OK` (tree)

```json
{
  "data": [
    {
      "id": 1,
      "name": "Products",
      "parent_category_id": null,
      "parent": null,
      "level": 1,
      "fully_qualified_name": "Products",
      "active": true,
      "children": [
        {
          "id": 2,
          "name": "Hardware",
          "parent_category_id": 1,
          "parent": { "...": "..." },
          "level": 2,
          "fully_qualified_name": "Products:Hardware",
          "active": true,
          "children": []
        }
      ],
      "created_at": "2026-06-12T10:00:00.000000Z",
      "updated_at": "2026-06-12T10:00:00.000000Z"
    }
  ]
}
```

### Example request — top-level only

```http
GET /api/v1/item-categories?parent_id=null
Authorization: Bearer {token}
Accept: application/json
```

---

## Create category

```
POST /item-categories
```

### Request body

| Field | Type | Required | Rules |
|-------|------|----------|-------|
| `name` | string | **Yes** | Max 100, unique among siblings under the same parent |
| `parent_category_id` | integer | No | Must exist in the same company; omit or send `null` for top level |
| `active` | boolean | No | Default `true` |

### Example request — top-level category

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

{
  "name": "Products",
  "active": true
}
```

### Example request — subcategory

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

{
  "name": "Hardware",
  "parent_category_id": 1,
  "active": true
}
```

### Success response — `201 Created`

```json
{
  "data": {
    "id": 2,
    "name": "Hardware",
    "parent_category_id": 1,
    "parent": {
      "id": 1,
      "name": "Products",
      "parent_category_id": null,
      "parent": null,
      "level": 1,
      "fully_qualified_name": "Products",
      "active": true,
      "created_at": "2026-06-12T10:00:00.000000Z",
      "updated_at": "2026-06-12T10:00:00.000000Z"
    },
    "level": 2,
    "fully_qualified_name": "Products:Hardware",
    "active": true,
    "created_at": "2026-06-12T10:05:00.000000Z",
    "updated_at": "2026-06-12T10:05:00.000000Z"
  }
}
```

### Error responses

| Status | Condition |
|--------|-----------|
| `403` | User is not ADMIN or ACCOUNTANT |
| `422` | Validation failed (duplicate sibling name, max depth exceeded, invalid parent, etc.) |

---

## Get category

```
GET /item-categories/{id}
```

### Example request

```http
GET /api/v1/item-categories/2
Authorization: Bearer {token}
Accept: application/json
```

### Success response — `200 OK`

Single category object wrapped in `data` (same shape as create response).

### Error responses

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

---

## Update category

```
PUT /item-categories/{id}
PATCH /item-categories/{id}
```

All fields are optional (partial update supported).

### Example request — rename

```http
PATCH /api/v1/item-categories/2
Authorization: Bearer {token}
Content-Type: application/json

{
  "name": "Computer Hardware"
}
```

### Example request — move to top level

```http
PATCH /api/v1/item-categories/2
Authorization: Bearer {token}
Content-Type: application/json

{
  "parent_category_id": null
}
```

### Success response — `200 OK`

Updated category object in `data`.

### Error responses

| Status | Condition |
|--------|-----------|
| `403` | User is not ADMIN or ACCOUNTANT |
| `404` | Category not found |
| `422` | Validation failed (duplicate name, cycle, depth exceeded, etc.) |

---

## Delete category

```
DELETE /item-categories/{id}
```

Permanently removes the category record.

**Side effects:**

| Affected record | Behavior |
|-----------------|----------|
| Child categories | Re-parented to the deleted category's parent (or promoted to top level if deleting a root) |
| Items with `category_id` | Set to `null` (uncategorized) |

### Example request

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

### Success response — `204 No Content`

Empty body.

### Error responses

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

---

## Validation error format — `422`

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

### Common hierarchy validation messages

| Field | Message |
|-------|---------|
| `parent_category_id` | Categories cannot be nested deeper than three levels. |
| `parent_category_id` | A category cannot be moved under one of its descendants. |
| `parent_category_id` | Moving this category would exceed the maximum depth of three levels. |

---

## Endpoint summary

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

---

## Linking categories to items

Categories are assigned on the item record, not on invoices or bills.

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

{
  "category_id": 2
}
```

To remove a category from an item, send `"category_id": null`.

See [items.md](./items.md) for full item CRUD documentation.

---

## Frontend notes

- Use `GET /api/v1/item-categories?tree=1` to populate a hierarchical category picker.
- Use the flat list (`GET /item-categories`) when you only need a simple dropdown of all categories.
- Display `fully_qualified_name` in item lists when items belong to nested categories (e.g. show `Products:Hardware` instead of just `Hardware`).
- Categories are organizational only — they do not affect stock costing, tax, or journal entries.
- Create your category tree before bulk-assigning `category_id` on items.
- Deleting a category does not delete items; they become uncategorized and can be reassigned later.
