# Brix Accounting API — Accounts

API reference for chart-of-accounts (general ledger account) management.

---

## Base URL

```
{BASE_URL}/api/v1/accounts
```

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

---

## 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.

**Additional middleware:** All account endpoints also require the `ADMIN` or `ACCOUNTANT` role (`role:ADMIN,ACCOUNTANT`). Unlike customers or vendors, cashiers and managers cannot list or view accounts through this API.

---

## Role permissions

| Action | ADMIN | ACCOUNTANT | MANAGER | CASHIER |
|--------|-------|------------|---------|---------|
| List accounts | ✓ | ✓ | ✗ | ✗ |
| View account | ✓ | ✓ | ✗ | ✗ |
| Create account | ✓ | ✓ | ✗ | ✗ |
| Update account | ✓ | ✓ | ✗ | ✗ |
| Delete account | ✓ | ✗ | ✗ | ✗ |

---

## Account object

| Field | Type | Read-only | Description |
|-------|------|-----------|-------------|
| `id` | integer | Yes | Primary key |
| `account_number` | string | No* | GL account code (max 20, unique per company) |
| `name` | string | No | Account display name (max 200) |
| `account_type` | string | No | `ASSET`, `LIABILITY`, `EQUITY`, `INCOME`, `EXPENSE` |
| `account_sub_type` | string | No | See [Account sub-types](#account-sub-types) below |
| `parent_account_id` | integer \| null | No | Parent account for hierarchical chart |
| `parent` | object \| null | Yes | Nested parent account when loaded |
| `active` | boolean | No | Inactive accounts can be hidden in UI |
| `created_at` | datetime | Yes | — |
| `updated_at` | datetime | Yes | — |

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

**Defaults on create:** `account_sub_type` = `OTHER`, `active` = `true`

### Account types

| Value | Normal balance | Typical use |
|-------|----------------|-------------|
| `ASSET` | Debit | Cash, inventory, receivables |
| `LIABILITY` | Credit | Payables, VAT payable |
| `EQUITY` | Credit | Owner's equity, retained earnings |
| `INCOME` | Credit | Sales revenue, other income |
| `EXPENSE` | Debit | COGS, operating expenses |

### Account sub-types

| Value | Typical `account_type` |
|-------|------------------------|
| `CASH` | `ASSET` |
| `BANK` | `ASSET` |
| `ACCOUNTS_RECEIVABLE` | `ASSET` |
| `INVENTORY_ASSET` | `ASSET` |
| `FIXED_ASSET` | `ASSET` |
| `ACCOUNTS_PAYABLE` | `LIABILITY` |
| `VAT_PAYABLE` | `LIABILITY` |
| `INPUT_VAT` | `ASSET` |
| `SALES_REVENUE` | `INCOME` |
| `COGS` | `EXPENSE` |
| `OPERATING_EXPENSE` | `EXPENSE` |
| `WRITE_OFF` | `EXPENSE` |
| `OTHER` | Any |

> `account_sub_type` is used by the ledger engine to auto-select system accounts (e.g. `INPUT_VAT`, `ACCOUNTS_RECEIVABLE`) when posting invoices and bills. Choose the correct sub-type when creating accounts that participate in automated journal entries.

### Nested parent object

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

```json
{
  "id": 12,
  "account_number": "6000",
  "name": "Operating Expenses",
  "account_type": "EXPENSE",
  "account_sub_type": "OPERATING_EXPENSE",
  "parent_account_id": null,
  "parent": null,
  "active": true,
  "created_at": "2026-06-11T10:00:00.000000Z",
  "updated_at": "2026-06-11T10:00:00.000000Z"
}
```

---

# Endpoints

## List accounts

```
GET /accounts
```

Returns all accounts for the authenticated user's company, sorted by `account_number`. Each record includes the `parent` relation when set.

### Example request

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

### Success response — `200 OK`

```json
{
  "data": [
    {
      "id": 1,
      "account_number": "1000",
      "name": "Cash on Hand",
      "account_type": "ASSET",
      "account_sub_type": "CASH",
      "parent_account_id": null,
      "parent": null,
      "active": true,
      "created_at": "2026-06-11T10:00:00.000000Z",
      "updated_at": "2026-06-11T10:00:00.000000Z"
    },
    {
      "id": 12,
      "account_number": "6100",
      "name": "Office Supplies",
      "account_type": "EXPENSE",
      "account_sub_type": "OPERATING_EXPENSE",
      "parent_account_id": 11,
      "parent": {
        "id": 11,
        "account_number": "6000",
        "name": "Operating Expenses",
        "account_type": "EXPENSE",
        "account_sub_type": "OPERATING_EXPENSE",
        "parent_account_id": null,
        "parent": null,
        "active": true,
        "created_at": "2026-06-11T10:00:00.000000Z",
        "updated_at": "2026-06-11T10:00:00.000000Z"
      },
      "active": true,
      "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 |

---

## Create account

```
POST /accounts
```

### Request body

| Field | Type | Required | Rules |
|-------|------|----------|-------|
| `account_number` | string | **Yes** | Max 20 characters, unique per company |
| `name` | string | **Yes** | Max 200 characters |
| `account_type` | string | **Yes** | `ASSET`, `LIABILITY`, `EQUITY`, `INCOME`, `EXPENSE` |
| `account_sub_type` | string | No | See [Account sub-types](#account-sub-types); default `OTHER` |
| `parent_account_id` | integer | No | Must exist in the same company |
| `active` | boolean | No | Default `true` |

### Example request

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

{
  "account_number": "4100",
  "name": "Other Income",
  "account_type": "INCOME",
  "account_sub_type": "OTHER",
  "active": true
}
```

### Example — child account

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

{
  "account_number": "6100",
  "name": "Office Supplies",
  "account_type": "EXPENSE",
  "account_sub_type": "OPERATING_EXPENSE",
  "parent_account_id": 11,
  "active": true
}
```

### Success response — `201 Created`

```json
{
  "data": {
    "id": 13,
    "account_number": "4100",
    "name": "Other Income",
    "account_type": "INCOME",
    "account_sub_type": "OTHER",
    "parent_account_id": null,
    "parent": null,
    "active": true,
    "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 (e.g. duplicate `account_number`, invalid enum) |

---

## Get account

```
GET /accounts/{id}
```

### Example request

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

### Success response — `200 OK`

Single account object wrapped in `data` (same shape as create response, with `parent` loaded when set).

### Error responses

| Status | Condition |
|--------|-----------|
| `403` | User is not ADMIN or ACCOUNTANT |
| `404` | Account not found or belongs to another company |

---

## Update account

```
PUT /accounts/{id}
PATCH /accounts/{id}
```

All fields are optional (partial update supported).

### Request body

| Field | Type | Required | Rules |
|-------|------|----------|-------|
| `account_number` | string | No | Max 20, unique per company (excluding current record) |
| `name` | string | No | Max 200 characters |
| `account_type` | string | No | `ASSET`, `LIABILITY`, `EQUITY`, `INCOME`, `EXPENSE` |
| `account_sub_type` | string | No | See [Account sub-types](#account-sub-types) |
| `parent_account_id` | integer \| null | No | Must exist in the same company; send `null` to clear |
| `active` | boolean | No | — |

### Example request

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

{
  "name": "Petty Cash",
  "active": false
}
```

### Success response — `200 OK`

Updated account object in `data`.

### Error responses

| Status | Condition |
|--------|-----------|
| `403` | User is not ADMIN or ACCOUNTANT |
| `404` | Account not found |
| `422` | Validation failed |

---

## Delete account

```
DELETE /accounts/{id}
```

Permanently removes the account record.

### Example request

```http
DELETE /api/v1/accounts/13
Authorization: Bearer {token}
Accept: application/json
```

### Success response — `204 No Content`

Empty body.

### Error responses

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

---

## Validation error format — `422`

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

---

## Endpoint summary

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

---

## Frontend notes

- Use `account_number` (not `code`) in all forms and table columns.
- Enum values are **uppercase** — send `ASSET`, not `asset`.
- Populate account dropdowns (e.g. on item forms for `income_account_id`, `expense_account_id`, `asset_account_id`) from `GET /accounts`; filter client-side by `account_type` or `account_sub_type` as needed.
- Build hierarchical chart views using `parent_account_id` and the nested `parent` object.
- System-seeded accounts (cash, AR, AP, VAT, etc.) are created during `php artisan migrate --seed`; avoid re-creating accounts with the same `account_number`.
- Accounts referenced by posted journal entries should be deactivated (`active: false`) rather than deleted when possible.
