# Brix Accounting API — Customers

API reference for customer (accounts receivable sub-ledger) management.

---

## Base URL

```
{BASE_URL}/api/v1/customers
```

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

---

## 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 customers | ✓ | ✓ | ✓ | ✓ |
| View customer | ✓ | ✓ | ✓ | ✓ |
| Create customer | ✓ | ✓ | ✗ | ✗ |
| Update customer | ✓ | ✓ | ✗ | ✗ |
| Delete customer | ✓ | ✗ | ✗ | ✗ |

---

## Customer object

| Field | Type | Read-only | Description |
|-------|------|-----------|-------------|
| `id` | integer | Yes | Primary key |
| `display_name` | string | No | Customer name (max 200) |
| `email` | string \| null | No | Contact email (max 150) |
| `phone` | string \| null | No | Phone number (max 30) |
| `address` | string \| null | No | Mailing address |
| `customer_type` | string | No | `RETAIL`, `WHOLESALE`, `CORPORATE`, `GOVERNMENT` |
| `payment_terms` | string | No | `NET_30`, `NET_60`, `NET_90`, `IMMEDIATE` |
| `balance` | decimal string | **Yes** | Amount owed on open/partial invoices |
| `credit_balance` | decimal string | **Yes** | Total unapplied payment credit (`SUM` of payment `unapplied_amount`) |
| `net_balance` | decimal string | **Yes** | `max(0, balance - credit_balance)` — amount still due after applying available credit |
| `active` | boolean | No | Inactive customers can be hidden in UI |
| `created_at` | datetime | Yes | — |
| `updated_at` | datetime | Yes | — |

**Defaults on create:** `customer_type` = `RETAIL`, `payment_terms` = `IMMEDIATE`, `balance` = `0`, `active` = `true`

---

# Endpoints

## List customers

```
GET /customers
```

Returns customers for the authenticated user's company. Default sort is `display_name` ascending. Results are paginated.

### Query parameters

| Parameter | Type | Default | Description |
|-----------|------|---------|-------------|
| `search` | string | — | Match `display_name`, `email`, or `phone` (partial match) |
| `customer_type` | string | — | `RETAIL`, `WHOLESALE`, `CORPORATE`, or `GOVERNMENT` |
| `active` | boolean | — | When `true`, only active customers; when `false`, only inactive |
| `has_balance` | boolean | — | When `true`, only customers with `balance > 0`; when `false`, zero balance |
| `has_credit` | boolean | — | When `true`, only customers with `credit_balance > 0`; when `false`, no unapplied payment credit |
| `sort` | string | `display_name` | Sort field. Prefix with `-` for descending. Allowed: `display_name`, `email`, `customer_type`, `balance`, `credit_balance`, `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 customers (paginated).

### Example request

```http
GET /api/v1/customers?search=acme&customer_type=CORPORATE&active=true&per_page=25
Authorization: Bearer {token}
Accept: application/json
```

### Success response — `200 OK`

```json
{
  "data": [
    {
      "id": 1,
      "display_name": "John Doe Corp",
      "email": "john@example.com",
      "phone": "+94771234567",
      "address": "123 Main St",
      "customer_type": "CORPORATE",
      "payment_terms": "NET_30",
      "balance": "0.0000",
      "credit_balance": "0.0000",
      "net_balance": "0.0000",
      "active": true,
      "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/customers?page=1",
    "last": "http://127.0.0.1:8000/api/v1/customers?page=1",
    "prev": null,
    "next": null
  },
  "meta": {
    "current_page": 1,
    "per_page": 50,
    "total": 1,
    "last_page": 1
  }
}
```

---

## Create customer

```
POST /customers
```

### Request body

| Field | Type | Required | Rules |
|-------|------|----------|-------|
| `display_name` | string | **Yes** | Max 200 characters |
| `email` | string | No | Valid email, max 150 |
| `phone` | string | No | Max 30 characters |
| `address` | string | No | — |
| `customer_type` | string | No | `RETAIL`, `WHOLESALE`, `CORPORATE`, `GOVERNMENT` |
| `payment_terms` | string | No | `NET_30`, `NET_60`, `NET_90`, `IMMEDIATE` |
| `active` | boolean | No | Default `true` |

### Example request

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

{
  "display_name": "John Doe Corp",
  "email": "john@example.com",
  "phone": "+94771234567",
  "address": "123 Main St",
  "customer_type": "CORPORATE",
  "payment_terms": "NET_30",
  "active": true
}
```

### Success response — `201 Created`

```json
{
  "data": {
    "id": 1,
    "display_name": "John Doe Corp",
    "email": "john@example.com",
    "phone": "+94771234567",
    "address": "123 Main St",
    "customer_type": "CORPORATE",
    "payment_terms": "NET_30",
    "balance": "0.0000",
    "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 |

---

## Get customer

```
GET /customers/{id}
```

### Example request

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

### Success response — `200 OK`

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

### Error responses

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

---

## Update customer

```
PUT /customers/{id}
PATCH /customers/{id}
```

All fields are optional (partial update supported).

### Request body

Same fields as create, but all optional. Use `active: false` to deactivate.

### Example request

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

{
  "display_name": "John Doe Corporation",
  "payment_terms": "NET_60",
  "active": true
}
```

### Success response — `200 OK`

Updated customer object in `data`.

### Error responses

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

---

## Delete customer

```
DELETE /customers/{id}
```

Permanently removes the customer record.

### Example request

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

### Success response — `204 No Content`

Empty body.

### Error responses

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

---

## Validation error format — `422`

```json
{
  "message": "The display name field is required.",
  "errors": {
    "display_name": ["The display name field is required."]
  }
}
```

---

## Endpoint summary

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

---

## Frontend notes

- Use `display_name` (not `name`) in all forms and table columns.
- `balance` is system-managed — display only; do not send on create/update.
- Customers are used when creating invoices (`customer_id` on invoice forms).
- Email uniqueness is not enforced at the API level; only `company_id` tenant scoping applies.
