# Brix Accounting API — Vendors

API reference for vendor (accounts payable sub-ledger) management.

---

## Base URL

```
{BASE_URL}/api/v1/vendors
```

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

---

## 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 vendors | ✓ | ✓ | ✓ | ✓ |
| View vendor | ✓ | ✓ | ✓ | ✓ |
| Create vendor | ✓ | ✓ | ✗ | ✗ |
| Update vendor | ✓ | ✓ | ✗ | ✗ |
| Delete vendor | ✓ | ✗ | ✗ | ✗ |

---

## Vendor object

| Field | Type | Read-only | Description |
|-------|------|-----------|-------------|
| `id` | integer | Yes | Primary key |
| `display_name` | string | No | Vendor 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 |
| `payment_terms` | string | No | `NET_30`, `NET_60`, `NET_90`, `IMMEDIATE` |
| `balance` | decimal string | **Yes** | Amount owed on open/partial bills |
| `credit_balance` | decimal string | **Yes** | Total unapplied bill payment credit (`SUM` of payment `unapplied_amount`) |
| `net_balance` | decimal string | **Yes** | `max(0, balance - credit_balance)` — amount still owed after available prepayment |
| `active` | boolean | No | Inactive vendors can be hidden in UI |
| `created_at` | datetime | Yes | — |
| `updated_at` | datetime | Yes | — |

**Defaults on create:** `payment_terms` = `NET_30`, `balance` = `0`, `active` = `true`

> Vendors do not have a `customer_type` field. Payment terms default to `NET_30` (vs `IMMEDIATE` for customers).

---

# Endpoints

## List vendors

```
GET /vendors
```

Returns vendors 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) |
| `payment_terms` | string | — | `NET_30`, `NET_60`, `NET_90`, or `IMMEDIATE` |
| `active` | boolean | — | When `true`, only active vendors; when `false`, only inactive |
| `has_balance` | boolean | — | When `true`, only vendors with `balance > 0`; when `false`, zero balance |
| `has_credit` | boolean | — | When `true`, only vendors with `credit_balance > 0`; when `false`, no unapplied prepayment |
| `sort` | string | `display_name` | Sort field. Prefix with `-` for descending. Allowed: `display_name`, `email`, `payment_terms`, `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 vendors (paginated).

### Example request

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

### Success response — `200 OK`

```json
{
  "data": [
    {
      "id": 1,
      "display_name": "Acme Suppliers",
      "email": "orders@acme.com",
      "phone": "+94771234567",
      "address": "456 Industrial Rd",
      "payment_terms": "NET_60",
      "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/vendors?page=1",
    "last": "http://127.0.0.1:8000/api/v1/vendors?page=1",
    "prev": null,
    "next": null
  },
  "meta": {
    "current_page": 1,
    "per_page": 50,
    "total": 1,
    "last_page": 1
  }
}
```

---

## Create vendor

```
POST /vendors
```

### 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 | — |
| `payment_terms` | string | No | `NET_30`, `NET_60`, `NET_90`, `IMMEDIATE` |
| `active` | boolean | No | Default `true` |

### Example request

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

{
  "display_name": "Acme Suppliers",
  "email": "orders@acme.com",
  "phone": "+94771234567",
  "address": "456 Industrial Rd",
  "payment_terms": "NET_60",
  "active": true
}
```

### Success response — `201 Created`

```json
{
  "data": {
    "id": 1,
    "display_name": "Acme Suppliers",
    "email": "orders@acme.com",
    "phone": "+94771234567",
    "address": "456 Industrial Rd",
    "payment_terms": "NET_60",
    "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 vendor

```
GET /vendors/{id}
```

### Example request

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

### Success response — `200 OK`

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

### Error responses

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

---

## Update vendor

```
PUT /vendors/{id}
PATCH /vendors/{id}
```

All fields are optional (partial update supported).

### Example request

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

{
  "display_name": "Acme Suppliers Ltd",
  "payment_terms": "NET_30",
  "active": false
}
```

### Success response — `200 OK`

Updated vendor object in `data`.

### Error responses

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

---

## Delete vendor

```
DELETE /vendors/{id}
```

Permanently removes the vendor record.

### Example request

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

### Success response — `204 No Content`

Empty body.

### Error responses

| Status | Condition |
|--------|-----------|
| `403` | User is not ADMIN |
| `404` | Vendor 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` | `/vendors` | List all vendors |
| `POST` | `/vendors` | Create vendor |
| `GET` | `/vendors/{id}` | Get vendor |
| `PUT`/`PATCH` | `/vendors/{id}` | Update vendor |
| `DELETE` | `/vendors/{id}` | Delete vendor |

---

## 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.
- Vendors are used when creating bills (`vendor_id` on bill forms).
- Default payment terms differ from customers: vendors default to `NET_30`.
