# Brix Accounting API — Purchase transactions hub

Unified read API for a QuickBooks-style **Expenses & Bills** page: one list of vendor bills and bill payments, plus summary totals (Money Bar).

Create, edit, receive, void, and pay bills still use the existing [bills](./bills.md) and [bill payments](./payments.md) endpoints.

---

## Base URL

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

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

---

## Authentication

| Header | Value |
|--------|-------|
| `Authorization` | `Bearer {token}` |
| `Accept` | `application/json` |

**Middleware:** `auth:sanctum`, `company`

---

## Role permissions

| Action | ADMIN | ACCOUNTANT | MANAGER | CASHIER |
|--------|-------|------------|---------|---------|
| List purchase transactions | ✓ | ✓ | ✓ | ✓ |

Row-level `available_actions` respect bill and bill payment policies (e.g. only ADMIN can void).

---

## List purchase transactions

```
GET /purchase-transactions
```

### Query parameters

| Parameter | Type | Default | Description |
|-----------|------|---------|-------------|
| `type` | string | `bill,bill_payment` | Comma-separated: `bill`, `bill_payment` |
| `status` | string | *(all)* | Comma-separated: `draft`, `open`, `partial`, `paid`, `voided`, `overdue` |
| `vendor_id` | integer | — | Filter by vendor |
| `start_date` | date | 365 days ago | Minimum `txn_date` |
| `end_date` | date | today | Maximum `txn_date` |
| `search` | string | — | Match internal bill number, vendor bill number, payment number, or vendor name |
| `paid_days` | integer | `30` | Window for `summary.paid_recent` |
| `per_page` | integer | `25` | Page size (1–100) |
| `page` | integer | `1` | Page number |

**Status notes**

- `open` — `OPEN` or `PARTIAL` with `balance > 0`
- `overdue` — open bill with `due_date` before today
- `paid` on bill payments — includes `BILL_PAYMENT` rows with status `POSTED`

**Balance meaning (QBO-style)**

| `transaction_type` | `balance` |
|--------------------|-----------|
| `BILL` | Remaining amount owed on the bill |
| `BILL_PAYMENT` | Remaining **unapplied** vendor credit (`total_amount − applied allocations`). Fully applied = `0.0000`. Voided = `0.0000`. |

UI cue: `BILL_PAYMENT` + `balance > 0` means the payment still has prepaid credit to apply to bills.

### Example request

```http
GET /api/v1/purchase-transactions?status=open,overdue&per_page=25
Authorization: Bearer {token}
Accept: application/json
```

### Success response — `200 OK`

```json
{
  "summary": {
    "open": { "count": 2, "total": "1180.0000" },
    "overdue": { "count": 1, "total": "590.0000" },
    "draft": { "count": 1, "total": "590.0000" },
    "paid_recent": { "count": 1, "total": "590.0000", "days": 30 }
  },
  "data": [
    {
      "transaction_type": "BILL",
      "id": 12,
      "transaction_id": 88,
      "number": "BILL-0001",
      "vendor_bill_number": "INV-8842",
      "vendor_id": 3,
      "vendor_name": "Acme Suppliers",
      "txn_date": "2026-06-01",
      "created_at": "2026-06-01T14:32:00.000000Z",
      "due_date": "2026-07-01",
      "total_amount": "590.0000",
      "balance": "590.0000",
      "status": "OPEN",
      "payment_method": null,
      "received_into_inventory": true,
      "is_overdue": false,
      "available_actions": ["view", "void", "pay_bill", "view_journal"]
    },
    {
      "transaction_type": "BILL_PAYMENT",
      "id": 5,
      "transaction_id": 91,
      "number": "BPAY-0001",
      "vendor_bill_number": null,
      "vendor_id": 3,
      "vendor_name": "Acme Suppliers",
      "txn_date": "2026-06-15",
      "created_at": "2026-06-15T09:10:00.000000Z",
      "due_date": null,
      "total_amount": "800.0000",
      "balance": "210.0000",
      "status": "POSTED",
      "payment_method": "BANK_TRANSFER",
      "received_into_inventory": false,
      "is_overdue": false,
      "available_actions": ["view", "view_journal", "void"]
    }
  ],
  "meta": {
    "current_page": 1,
    "per_page": 25,
    "total": 2,
    "last_page": 1
  },
  "links": {
    "first": "http://127.0.0.1:8000/api/v1/purchase-transactions?page=1",
    "last": "http://127.0.0.1:8000/api/v1/purchase-transactions?page=1",
    "prev": null,
    "next": null
  }
}
```

### Row actions → API endpoints

| `available_actions` | Endpoint |
|---------------------|----------|
| `view` | `GET /bills/{id}` or `GET /bill-payments/{id}` |
| `edit` | `PATCH /bills/{id}` |
| `receive` | `POST /bills/{id}/receive` |
| `void` | `POST /bills/{id}/void` |
| `void` (bill payment) | `POST /bill-payments/{id}/void` |
| `delete` | `DELETE /bills/{id}` |
| `pay_bill` | `POST /bill-payments` |
| `view_journal` | `GET /journal-entries?reference_transaction_id={transaction_id}` |

---

## Related endpoints

| Purpose | Endpoint |
|---------|----------|
| Create bill | `POST /bills` |
| Pay bill | `POST /bill-payments` |
| AP aging detail | `GET /reports/ap-aging` |
| Vendor balance summary | `GET /reports/vendor-balance-summary` |
| Vendor balance detail | `GET /reports/vendor-balance-detail` |
| Expenses by vendor summary | `GET /reports/expenses-by-vendor-summary` |
| Expenses by vendor detail | `GET /reports/expenses-by-vendor-detail` |
| Journal drill-down | `GET /journal-entries` |

---

## Error responses

| Status | Condition |
|--------|-----------|
| `401` | Missing or invalid token |
| `422` | Invalid query parameters |
