# Brix Accounting API — Purchase Orders

API reference for purchase orders (informational POs with line items). Purchase orders are **isolated** — they do not post to the ledger, affect inventory, or link to bills.

---

## Base URL

```
{BASE_URL}/api/v1/purchase-orders
```

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

---

## Authentication

All endpoints require:

| Header | Value |
|--------|-------|
| `Authorization` | `Bearer {token}` |
| `Accept` | `application/json` |
| `Content-Type` | `application/json` | On `POST`, `PUT` |

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 | CASHIER |
|--------|-------|------------|---------|
| List purchase orders | ✓ | ✓ | ✓ |
| View purchase order | ✓ | ✓ | ✓ |
| Download PDF | ✓ | ✓ | ✓ |
| Send PDF email | ✓ | ✓ | ✗ |
| Create purchase order | ✓ | ✓ | ✗ |
| Update purchase order | ✓ | ✓ | ✗ |
| Delete purchase order | ✓ | ✗ | ✗ |

---

## Status values

| Status | Description |
|--------|-------------|
| `DRAFT` | Default on create; not yet sent |
| `SENT` | PO sent to vendor (set automatically by `POST /purchase-orders/{id}/send` when status was `DRAFT`, or manually via `PUT`) |
| `ACKNOWLEDGED` | Vendor acknowledged (informational only) |
| `CLOSED` | PO fulfilled or completed (manual label) |
| `CANCELLED` | PO cancelled |

Status changes are manual via `PUT` — no accounting or inventory side effects.

---

## Purchase order object

| Field | Type | Read-only | Description |
|-------|------|-----------|-------------|
| `id` | integer | Yes | Primary key |
| `vendor_id` | integer | No | Linked vendor (required on create) |
| `po_number` | string | Yes | Auto-generated per company (`PO-0001`, `PO-0002`, …) |
| `vendor_snapshot` | object | Yes | Frozen vendor details at save time (read-only in API) |
| `po_date` | date | No | PO date (`YYYY-MM-DD`) |
| `expected_delivery_date` | date \| null | No | Optional expected delivery date |
| `ship_to` | string \| null | No | Delivery address or warehouse note |
| `sub_total` | decimal string | Yes | Sum of line amounts before tax (computed) |
| `discount_total` | decimal string | Yes | Sum of line discounts (computed) |
| `tax_total` | decimal string | Yes | Sum of line tax (computed) |
| `total_amount` | decimal string | Yes | Sum of line totals (computed) |
| `status` | string | No | See status values above |
| `memo` | string \| null | No | Printed on PO |
| `notes` | string \| null | No | Internal notes |
| `lines` | array | No | Line items (included on show; optional on list) |
| `vendor` | object | Yes | Nested vendor when eager-loaded on show |
| `created_at` | datetime | Yes | — |
| `updated_at` | datetime | Yes | — |

On create, provide `vendor_id`; the service copies vendor details into `vendor_snapshot`. Snapshot-only create is not supported.

### Vendor snapshot shape (response)

| Field | Type | Required |
|-------|------|----------|
| `name` | string | Yes |
| `email` | string \| null | No |
| `phone` | string \| null | No |
| `address` | string \| null | No |

### Line object

| Field | Type | Required | Description |
|-------|------|----------|-------------|
| `id` | integer | — | Present on read only |
| `item_id` | integer | Yes | Catalog item ID (company-scoped) |
| `item_snapshot` | object | — | Read-only frozen `{ name, sku, unit_price }` at save time |
| `line_number` | integer | — | 1-based (read only) |
| `description` | string | No | Optional override; defaults to item name when omitted |
| `quantity` | decimal | Yes | Must be > 0 |
| `unit_price` | decimal | Yes | Unit price ≥ 0 |
| `discount_amount` | decimal | No | Default `0`; cannot exceed `quantity × unit_price` |
| `tax_amount` | decimal | No | Default `0` |
| `line_total` | decimal string | — | Computed: `quantity × unit_price - discount_amount + tax_amount` |

`item_id` links to the catalog for display and pricing reference only — no inventory or GL impact.

---

# Endpoints

## List purchase orders

```
GET /purchase-orders
```

### Query parameters

| Parameter | Type | Default | Description |
|-----------|------|---------|-------------|
| `search` | string | — | Match `po_number`, `memo`, or vendor name in snapshot |
| `vendor_id` | integer | — | Filter by vendor |
| `status` | string | — | Comma-separated statuses, e.g. `DRAFT,SENT` |
| `po_date_from` | date | — | Minimum PO date |
| `po_date_to` | date | — | Maximum PO date |
| `sort` | string | `-po_date` | `po_date`, `po_number`, `total_amount`, `status` (prefix `-` for desc) |
| `per_page` | integer | `25` | Page size (1–100) |
| `page` | integer | `1` | Page number |

### Success response — `200 OK`

Paginated collection of purchase order objects (lines omitted on list by default).

---

## Create purchase order

```
POST /purchase-orders
```

### Request body

`vendor_id` is **required**. The API stores the FK and freezes `vendor_snapshot` from the vendor record.

```json
{
  "vendor_id": 1,
  "po_date": "2026-06-24",
  "expected_delivery_date": "2026-07-01",
  "ship_to": "Main warehouse",
  "status": "DRAFT",
  "memo": "Q3 restock",
  "notes": "Internal note",
  "lines": [
    {
      "item_id": 1,
      "description": "Office chairs",
      "quantity": 10,
      "unit_price": 85,
      "discount_amount": 0,
      "tax_amount": 0
    }
  ]
}
```

### Success response — `201 Created`

Returns the full purchase order object with `lines`.

---

## Show purchase order

```
GET /purchase-orders/{id}
```

### Success response — `200 OK`

Returns purchase order with nested `lines`.

---

## Update purchase order

```
PUT /purchase-orders/{id}
```

Partial updates supported. When `lines` is included, all existing lines are replaced.

```json
{
  "status": "SENT",
  "memo": "Updated memo",
  "lines": [
    {
      "item_id": 1,
      "quantity": 2,
      "unit_price": 200
    }
  ]
}
```

### Success response — `200 OK`

---

## Download purchase order PDF

```
GET /purchase-orders/{id}/pdf
```

Streams a generated PDF of the purchase order. Uses company letterhead, **`vendor_snapshot`**, optional **`ship_to`**, and line data (not live vendor/item masters). Internal `notes` and `status` are omitted. Line columns: Product/service, Description, Qty, Rate, Amount.

**Roles:** any authenticated tenant user who can view the purchase order.

### Example request

```http
GET /api/v1/purchase-orders/1/pdf
Authorization: Bearer {token}
```

### Success response — `200 OK`

| Header | Value |
|--------|-------|
| `Content-Type` | `application/pdf` |
| `Content-Disposition` | `attachment; filename="PO-0001.pdf"` |

Body: PDF binary.

### Error responses

| Status | Condition |
|--------|-----------|
| `403` | User cannot view the purchase order |
| `404` | Purchase order not found or wrong company |

---

## Send purchase order PDF by email

```
POST /purchase-orders/{id}/send
```

Generates the same PDF as download, emails it to the vendor, and sets status from **`DRAFT` → `SENT`** when send succeeds. Non-`DRAFT` statuses are left unchanged.

**Roles:** ADMIN, ACCOUNTANT.

### Request body

All fields optional.

| Field | Type | Description |
|-------|------|-------------|
| `to` | email | Recipient; defaults to `vendor_snapshot.email` |
| `cc` | email or email[] | Optional CC recipients |
| `subject` | string | Defaults to `Purchase order {number} from {company name}` |
| `message` | string | Optional plain-text body above the default notice |

```json
{
  "to": "orders@supplier.com",
  "cc": ["ops@example.com"],
  "subject": "Purchase order PO-0001",
  "message": "Please fulfill the attached PO."
}
```

### Success response — `200 OK`

Returns the purchase order object (status may be `SENT`).

### Error responses

| Status | Condition |
|--------|-----------|
| `403` | User cannot send purchase orders |
| `404` | Purchase order not found or wrong company |
| `422` | No recipient (`to` omitted and snapshot email empty) |

---

## Delete purchase order

```
DELETE /purchase-orders/{id}
```

### Success response — `204 No Content`

ADMIN only. Deletes the purchase order and all lines. No ledger reversal (purchase orders are not posted).

---

## What purchase orders do not do

- No `transaction_id` or journal entries
- No inventory or stock impact
- No vendor balance / AP updates
- Inactive catalog items are rejected
- No convert-to-bill endpoint (deferred)
