# Brix Accounting API — Tax Codes

API reference for company tax codes (VAT rates and exemptions) used on invoice and bill line items.

Tax amounts are calculated server-side when creating or updating draft invoices and bills, **only when the company has `tax_enabled` = `true`** (see [company.md](./company.md)). Pass `tax_code_id` on each line, or omit it to use the default active taxable code. See [invoices.md](./invoices.md) and [bills.md](./bills.md).

---

## Base URL

```
{BASE_URL}/api/v1/tax-codes
```

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

---

## 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` — records are scoped to the authenticated user's company.

---

## Role permissions

| Action | ADMIN | ACCOUNTANT | MANAGER | CASHIER |
|--------|-------|------------|---------|---------|
| List tax codes | ✓ | ✓ | ✓ | ✓ |
| View tax code | ✓ | ✓ | ✓ | ✓ |
| Create tax code | ✓ | ✓ | ✗ | ✗ |
| Update tax code | ✓ | ✓ | ✗ | ✗ |
| Delete tax code | ✓ | ✗ | ✗ | ✗ |

---

## Tax code object

| Field | Type | Read-only | Description |
|-------|------|-----------|-------------|
| `id` | integer | Yes | Primary key |
| `name` | string | No | Short label (max 50, unique per company), e.g. `VAT18`, `EXEMPT` |
| `rate_percent` | decimal string | No | Tax rate applied to line net amount (0–9999.99) |
| `is_taxable` | boolean | No | When `false`, lines using this code get `tax_amount` = `0` regardless of rate |
| `active` | boolean | No | Inactive codes are excluded from auto-selection on invoice/bill lines |
| `created_at` | datetime | Yes | — |

**Defaults on create:** `is_taxable` = `true`, `active` = `true`

### Seeded codes

After `php artisan migrate --seed`, each company receives:

| Name | `rate_percent` | `is_taxable` |
|------|----------------|--------------|
| `VAT18` | `18.00` | `true` |
| `VAT5` | `5.00` | `true` |
| `ZERO_RATED` | `0.00` | `false` |
| `EXEMPT` | `0.00` | `false` |

### How codes affect line tax

1. `gross` = `qty` × `unit_price`
2. `net` = `gross` − line discount
3. Tax applies only when the **item** is `taxable` (invoices) and the **tax code** has `is_taxable` = `true`
4. `tax_amount` = `net` × `rate_percent` / 100

On bills, lines without an item can still be taxed when an explicit `tax_code_id` with `is_taxable` = `true` is set.

**Auto-selection:** When `tax_code_id` is omitted on a taxable item line, the API uses the first active code where `is_taxable` = `true` (ordered by `id`).

---

# Endpoints

## List tax codes

```
GET /tax-codes
```

Returns all tax codes for the company, sorted by `name`. Not paginated.

### Query parameters

| Parameter | Type | Description |
|-----------|------|-------------|
| `active` | boolean | When `true`, only active codes; when `false`, only inactive |
| `is_taxable` | boolean | When `true`, only taxable codes; when `false`, only exempt/zero-rated codes |

### Example request

```http
GET /api/v1/tax-codes?active=true&is_taxable=true
Authorization: Bearer {token}
Accept: application/json
```

### Success response — `200 OK`

```json
{
  "data": [
    {
      "id": 1,
      "name": "VAT18",
      "rate_percent": "18.00",
      "is_taxable": true,
      "active": true,
      "created_at": "2026-06-11T10:00:00.000000Z"
    },
    {
      "id": 2,
      "name": "VAT5",
      "rate_percent": "5.00",
      "is_taxable": true,
      "active": true,
      "created_at": "2026-06-11T10:00:00.000000Z"
    }
  ]
}
```

---

## Create tax code

```
POST /tax-codes
```

**ADMIN or ACCOUNTANT only.**

### Request body

| Field | Type | Required | Rules |
|-------|------|----------|-------|
| `name` | string | **Yes** | Max 50; unique per company |
| `rate_percent` | number | **Yes** | 0–9999.99 |
| `is_taxable` | boolean | No | Default `true` |
| `active` | boolean | No | Default `true` |

### Example request

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

{
  "name": "VAT18",
  "rate_percent": 18,
  "is_taxable": true,
  "active": true
}
```

### Success response — `201 Created`

```json
{
  "data": {
    "id": 1,
    "name": "VAT18",
    "rate_percent": "18.00",
    "is_taxable": true,
    "active": true,
    "created_at": "2026-06-11T10:00:00.000000Z"
  }
}
```

### Error responses

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

---

## Get tax code

```
GET /tax-codes/{id}
```

### Example request

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

### Success response — `200 OK`

Single tax code object in `data`.

### Error responses

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

---

## Update tax code

```
PUT /tax-codes/{id}
PATCH /tax-codes/{id}
```

**ADMIN or ACCOUNTANT only.** Send only fields to change.

### Request body

| Field | Type | Required | Rules |
|-------|------|----------|-------|
| `name` | string | No | Max 50; unique per company |
| `rate_percent` | number | No | 0–9999.99 |
| `is_taxable` | boolean | No | — |
| `active` | boolean | No | — |

### Example request

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

{
  "rate_percent": 18.5,
  "active": true
}
```

### Success response — `200 OK`

Updated tax code object in `data`.

### Error responses

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

---

## Delete tax code

```
DELETE /tax-codes/{id}
```

**ADMIN only.** Permanently removes the tax code. Existing invoice/bill lines that referenced it will have `tax_code_id` set to `null` (historical `tax_amount` values are unchanged).

### Example request

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

### Success response — `204 No Content`

Empty body.

### Error responses

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

---

## Validation error format — `422`

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

---

## Endpoint summary

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

---

## Frontend notes

- Load `GET /tax-codes?active=true` to populate tax dropdowns on invoice and bill forms.
- Display `name` with `rate_percent` (e.g. `VAT18 (18%)`) for user-friendly labels.
- Use `is_taxable: false` codes (`EXEMPT`, `ZERO_RATED`) to force zero tax on a taxable item line.
- Changing a code's `rate_percent` does not recalculate existing posted documents — only affects new drafts.
- GL posting uses company-wide **VAT Payable** (sales) and **Input VAT** (purchases) accounts; tax codes do not map to separate GL accounts.
