# Brix Accounting API — Global search

QBO-style company-wide search for the dashboard search bar. One request returns grouped hits across contacts, transactions, and items.

---

## Base URL

```
{BASE_URL}/api/v1/search
```

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

---

## Authentication

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

**Middleware:** `auth:sanctum`, `company` — results are scoped to the authenticated user's company.

All company roles with an active user account may search. Individual entity providers respect existing `viewAny` policies; denied slices are omitted (the endpoint does not 403).

---

## Search

```
GET /search
```

### Query parameters

| Parameter | Type | Default | Description |
|-----------|------|---------|-------------|
| `q` | string | **required** | Search term (1–100 chars) |
| `types` | string | all groups | Comma-separated: `contacts`, `transactions`, `items` |
| `limit` | integer | `5` | Max hits **per group** (1–10) |
| `txn_start_date` | date | today − 365 days | Minimum `txn_date` for transaction entities |
| `txn_end_date` | date | today | Maximum `txn_date`; must be ≥ `txn_start_date` |

### Example

```http
GET /api/v1/search?q=acme&types=contacts,transactions&limit=5
Authorization: Bearer {token}
Accept: application/json
```

### Success response — `200 OK`

```json
{
  "query": "acme",
  "groups": [
    {
      "type": "contacts",
      "label": "Contacts",
      "total": 2,
      "items": [
        {
          "entity": "customer",
          "id": 3,
          "title": "Acme Corp",
          "subtitle": "billing@acme.com",
          "url": "/customers/3",
          "score": 50,
          "meta": {
            "balance": "1250.0000",
            "active": true
          }
        }
      ]
    },
    {
      "type": "transactions",
      "label": "Transactions",
      "total": 1,
      "items": [
        {
          "entity": "invoice",
          "id": 12,
          "title": "INV-0012",
          "subtitle": "Acme Corp · 2026-06-11 · OPEN · 236.0000",
          "url": "/invoices/12",
          "score": 100,
          "meta": {
            "txn_date": "2026-06-11",
            "amount": "236.0000",
            "status": "OPEN",
            "party_name": "Acme Corp"
          }
        }
      ]
    }
  ]
}
```

| Field | Description |
|-------|-------------|
| `groups[].total` | Full match count before `limit` cap |
| `groups[].items` | Top hits sorted by `score` desc (transactions also by `txn_date` desc) |
| `url` | Suggested **frontend route** — map to your router |
| `score` | Relevance ranking (higher = better match) |

### Error responses

| Status | Condition |
|--------|-----------|
| `401` | Missing or invalid token |
| `422` | Missing/invalid `q`, invalid dates, or `txn_end_date` before `txn_start_date` |

---

## Searchable fields (Phase 1)

### Contacts

| Entity | Fields | Match |
|--------|--------|-------|
| Customer | `display_name`, `email`, `phone`, `address` | `LIKE %q%` |
| Vendor | `display_name`, `email`, `phone`, `address` | `LIKE %q%` |

### Transactions

Date-filtered by `txn_start_date` / `txn_end_date` (default last 365 days).

| Entity | Fields | Match | Notes |
|--------|--------|-------|-------|
| Invoice | `invoice_number` | `LIKE` | e.g. `INV-0001` |
| | `customer.display_name` | `LIKE` | Live customer name |
| | `customer_snapshot.name` | `LIKE` | Print / Bill-to name |
| | `memo` | `LIKE` | Printed on invoice |
| | `notes` | `LIKE` | Internal only |
| | `lines.description` | `LIKE` | Line detail text |
| | `total_amount`, `balance` | **exact** | When `q` is numeric |
| Bill | `bill_number` | `LIKE` | Internal `BILL-0001` |
| | `vendor_bill_number` | `LIKE` | **Vendor invoice / Bill No.** |
| | `vendor.display_name` | `LIKE` | |
| | `vendor_snapshot.name` | `LIKE` | Print vendor name |
| | `memo` | `LIKE` | |
| | `lines.description` | `LIKE` | Line detail text |
| | `lines.notes` | `LIKE` | Per-line notes |
| | `total_amount`, `balance` | exact numeric | |
| Customer payment | `payment_number` | `LIKE` | e.g. `PAY-0001` |
| | `ref_number` | `LIKE` | **Cheque / bank reference** |
| | `memo` | `LIKE` | |
| | `customer.display_name` | `LIKE` | |
| | `payment_method` | `LIKE` | e.g. `CASH` |
| | `total_amount` | exact numeric | |
| Bill payment | `payment_number` | `LIKE` | e.g. `BPAY-0001` |
| | `ref_number` | `LIKE` | **Cheque / bank reference** |
| | `memo` | `LIKE` | |
| | `vendor.display_name` | `LIKE` | |
| | `payment_method` | `LIKE` | |
| | `total_amount` | exact numeric | |

### Items

| Entity | Fields | Match |
|--------|--------|-------|
| Item | `name`, `sku`, `description` | `LIKE %q%` |

### Not searchable in Phase 1

- Invoice/bill line item names (`item_snapshot.name`)
- Quotations, purchase orders
- Journal entries, chart of accounts, reports
- Natural-language queries
- Cross-company / platform-wide search

---

## Entity `url` conventions

| `entity` | `url` pattern |
|----------|---------------|
| `customer` | `/customers/{id}` |
| `vendor` | `/vendors/{id}` |
| `invoice` | `/invoices/{id}` |
| `bill` | `/bills/{id}` |
| `customer_payment` | `/customer-payments/{id}` |
| `bill_payment` | `/bill-payments/{id}` |
| `item` | `/items/{id}` |

---

## Ranking

Results are scored server-side. Higher `score` wins within each group.

| Condition | Score boost |
|-----------|-------------|
| Exact doc # / payment # | +100 |
| Field starts with `q` | +50 |
| `vendor_bill_number` or `ref_number` match | +40 |
| Party name / snapshot match | +30 |
| Exact amount match | +80 |
| Memo / notes / line description / line notes | +10 |

---

## Frontend notes

- **Debounce** ~250ms while typing; call when `q.length >= 1`.
- **Empty state:** show recent searches / recent records from `localStorage` (client-only; no API yet).
- **Tabs:** map `types=contacts`, `types=transactions`, `types=items`, or omit for all groups.
- **“See all”:** use existing hub/list endpoints with the same `q` as `search` param:
  - Sales: `GET /sales-transactions?search=`
  - Purchasing: `GET /purchase-transactions?search=`
  - Customers: `GET /customers?search=`
  - Items: `GET /items?search=`
- **Older transactions:** widen `txn_start_date` (default window is 365 days).
- **Enter key:** navigate to a full search results page using the same `q` and optional `types`.

---

## Related docs

- [invoices.md](./invoices.md)
- [bills.md](./bills.md)
- [customer-payments.md](./customer-payments.md)
- [bill-payments.md](./bill-payments.md)
- [customers.md](./customers.md)
- [sales-transactions.md](./sales-transactions.md)
- [purchase-transactions.md](./purchase-transactions.md)
