# Brix Accounting API — Company

API reference for viewing and updating the authenticated user's company profile (tenant settings).

Each user belongs to exactly one company. There is no list or create endpoint — company records are provisioned separately (e.g. via database seeding). Use these endpoints to read and maintain company details shown on invoices, reports, and settings screens.

---

## Base URL

```
{BASE_URL}/api/v1/company
```

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

---

## Authentication

All endpoints require:

| Header | Value |
|--------|-------|
| `Authorization` | `Bearer {token}` |
| `Accept` | `application/json` |
| `Content-Type` | `application/json` | On `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. The API always operates on the authenticated user's company; there is no `{id}` route parameter.

---

## Role permissions

| Action | ADMIN | ACCOUNTANT | MANAGER | CASHIER |
|--------|-------|------------|---------|---------|
| View company | ✓ | ✓ | ✓ | ✓ |
| Update company | ✓ | ✗ | ✗ | ✗ |

---

## Company object

| Field | Type | Read-only | Description |
|-------|------|-----------|-------------|
| `id` | integer | Yes | Primary key |
| `name` | string | No | Legal or trading name (max 200) |
| `email` | string \| null | No | Company contact email (max 150) |
| `phone` | string \| null | No | Phone number (max 30) |
| `address` | string \| null | No | Mailing or registered address |
| `tax_number` | string \| null | No | Tax identification number / TIN (max 50) |
| `currency` | string | No | ISO 4217 currency code (3 characters), e.g. `LKR`, `USD` |
| `active` | boolean | No | When `false`, company is inactive at the platform level |
| `allow_negative_inventory` | boolean | No | When `true`, ADMIN may create invoices with `allow_negative_stock` to sell below on-hand qty. See [negative-stock.md](./negative-stock.md). Default `false`. |
| `tax_enabled` | boolean | No | When `true`, draft invoices and bills calculate line tax using [tax codes](./tax-codes.md). When `false`, all line tax is zero and `lines.*.tax_code_id` is rejected. Default `true`. |
| `books_closed_through` | date \| null | No | Last day of the last **closed** fiscal year (e.g. `2025-12-31` after year-end close). `NULL` = no book close enforced. Affects GL reversal dating on [invoice](./invoices.md) void/amend. |
| `created_at` | datetime | Yes | — |
| `updated_at` | datetime | Yes | — |

**Defaults on provision:** `currency` = `LKR`, `active` = `true`, `tax_enabled` = `true`

> A subset of company fields (`id`, `name`, `email`, `phone`, `currency`, `tax_number`, `active`) is also embedded in the `company` object on `POST /login` and `GET /me`. Use `GET /company` for the full record including `address` and timestamps. See [auth-and-users.md](./auth-and-users.md).

---

# Endpoints

## Get company

```
GET /company
```

Returns the authenticated user's company.

### Example request

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

### Success response — `200 OK`

```json
{
  "data": {
    "id": 1,
    "name": "Brix Demo Ltd",
    "email": "info@brixdemo.com",
    "phone": "+94771234567",
    "address": "123 Galle Road, Colombo",
    "tax_number": "123456789V",
    "currency": "LKR",
    "active": true,
    "created_at": "2026-06-11T10:00:00.000000Z",
    "updated_at": "2026-06-11T10:00:00.000000Z"
  }
}
```

### Error responses

| Status | Condition |
|--------|-----------|
| `401` | Missing or invalid token |
| `403` | User has no company context |

---

## Update company

```
PUT /company
PATCH /company
```

Updates the authenticated user's company. Send only fields to change (`PATCH` partial update supported).

**ADMIN only.**

### Request body

| Field | Type | Required | Rules |
|-------|------|----------|-------|
| `name` | string | No | Max 200 characters |
| `email` | string | No | Valid email, max 150; send `null` to clear |
| `phone` | string | No | Max 30 characters; send `null` to clear |
| `address` | string | No | Send `null` to clear |
| `tax_number` | string | No | Max 50 characters; send `null` to clear |
| `currency` | string | No | Exactly 3 characters (ISO 4217 code) |
| `active` | boolean | No | Platform-level active flag |
| `allow_negative_inventory` | boolean | No | Enable admin oversell on invoices (default `false`) |
| `tax_enabled` | boolean | No | Enable VAT on draft invoices and bills (default `true`) |
| `books_closed_through` | date | No | Last day of closed fiscal year; send `null` to clear. See [invoices](./invoices.md#void-invoice) for GL impact. |

At least one field must be provided.

### Example request

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

{
  "name": "Brix Demo Ltd",
  "email": "info@brixdemo.com",
  "phone": "+94771234567",
  "address": "123 Galle Road, Colombo",
  "tax_number": "123456789V",
  "currency": "LKR"
}
```

### Success response — `200 OK`

Updated company object in `data` (same shape as `GET /company`).

### Error responses

| Status | Condition |
|--------|-----------|
| `401` | Missing or invalid token |
| `403` | User is not ADMIN |
| `422` | Validation failed |

---

## Validation error format — `422`

```json
{
  "message": "The currency field must be 3 characters.",
  "errors": {
    "currency": ["The currency field must be 3 characters."]
  }
}
```

---

## Endpoint summary

| Method | Path | Description |
|--------|------|-------------|
| `GET` | `/company` | Get current company profile |
| `PUT`/`PATCH` | `/company` | Update company profile (ADMIN) |

---

## Frontend notes

- Use `GET /company` on settings pages rather than relying only on the nested `company` object from `/me` — the dedicated endpoint returns `address` and timestamps.
- `currency` affects how monetary amounts are displayed; changing it does not convert existing transaction amounts.
- `tax_number` is printed on invoices and tax reports where applicable.
- Set `tax_enabled` to `false` for companies that do not charge VAT; invoice and bill forms should hide tax fields when disabled. Posted documents are not recalculated when this flag changes.
- After year-end close, set `books_closed_through` to the last day of the closed year (e.g. `2025-12-31`). This controls GL reversal dating on [invoice void/amend](./invoices.md#gl-reversal-dating-void-and-amend).
- There is no delete endpoint — company lifecycle is managed outside this API.
- After updating company details, refresh `/me` if the UI caches the nested `company` object from login.
