# Brix Accounting API — Quotations

API reference for sales quotations (informational quotes with line items). Quotations are **isolated** — they do not post to the ledger, affect inventory, or link to invoices.

---

## Base URL

```
{BASE_URL}/api/v1/quotations
```

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

---

## 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 quotations | ✓ | ✓ | ✓ |
| View quotation | ✓ | ✓ | ✓ |
| Download PDF | ✓ | ✓ | ✓ |
| Send PDF email | ✓ | ✓ | ✗ |
| Create quotation | ✓ | ✓ | ✗ |
| Update quotation | ✓ | ✓ | ✗ |
| Delete quotation | ✓ | ✗ | ✗ |
| Copy quotation | ✓ | ✓ | ✗ |
| Convert to invoice | ✓ | ✓ | ✓ |

---

## Status values

| Status | Description |
|--------|-------------|
| `DRAFT` | Default on create; not yet sent |
| `SENT` | Quote sent to customer (set automatically by `POST /quotations/{id}/send` when status was `DRAFT`, or manually via `PUT`) |
| `ACCEPTED` | Customer accepted (informational only) |
| `REJECTED` | Customer declined |
| `EXPIRED` | Past validity date or manually marked expired |

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

---

## Quotation object

| Field | Type | Read-only | Description |
|-------|------|-----------|-------------|
| `id` | integer | Yes | Primary key |
| `customer_id` | integer | No | Linked customer (required on create) |
| `quotation_number` | string | Yes | Auto-generated per company (`Q-0001`, `Q-0002`, …) |
| `customer_snapshot` | object | Yes | Frozen customer details at save time (read-only in API) |
| `quote_date` | date | No | Quote date (`YYYY-MM-DD`) |
| `valid_until` | date \| null | No | Optional expiry date |
| `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 quote |
| `notes` | string \| null | No | Internal notes |
| `lines` | array | No | Line items (included on show; optional on list) |
| `customer` | object | Yes | Nested customer when eager-loaded on show |
| `created_at` | datetime | Yes | — |
| `updated_at` | datetime | Yes | — |

On create, provide `customer_id`; the service copies customer details into `customer_snapshot`. Snapshot-only create is not supported.

### Customer 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 quotations

```
GET /quotations
```

### Query parameters

| Parameter | Type | Default | Description |
|-----------|------|---------|-------------|
| `search` | string | — | Match `quotation_number`, `memo`, or customer name in snapshot |
| `customer_id` | integer | — | Filter by customer |
| `status` | string | — | Comma-separated statuses, e.g. `DRAFT,SENT` |
| `quote_date_from` | date | — | Minimum quote date |
| `quote_date_to` | date | — | Maximum quote date |
| `sort` | string | `-quote_date` | `quote_date`, `quotation_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 quotation objects (lines omitted on list by default).

---

## Create quotation

```
POST /quotations
```

### Request body

`customer_id` is **required**. The API stores the FK and freezes `customer_snapshot` from the customer record.

```json
{
  "customer_id": 1,
  "quote_date": "2026-06-24",
  "valid_until": "2026-07-24",
  "status": "DRAFT",
  "memo": "Project quote",
  "notes": "Internal note",
  "lines": [
    {
      "item_id": 1,
      "description": "Consulting — 10 hrs",
      "quantity": 10,
      "unit_price": 150,
      "discount_amount": 0,
      "tax_amount": 0
    }
  ]
}
```

### Success response — `201 Created`

Returns the full quotation object with `lines`.

---

## Show quotation

```
GET /quotations/{id}
```

### Success response — `200 OK`

Returns quotation with nested `lines`.

---

## Update quotation

```
PUT /quotations/{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`

---

## Copy quotation

```
POST /quotations/{id}/copy
```

Creates a new **DRAFT** quotation from any source quotation. Copies customer, lines, memo, and notes. Assigns a new quotation number and resets status to `DRAFT`. The source quotation is unchanged.

**Roles:** ADMIN, ACCOUNTANT.

### Optional request body

| Field | Type | Description |
|-------|------|-------------|
| `quote_date` | date | Quote date (default: today) |
| `valid_until` | date \| null | Validity end (default: `null`) |
| `memo` | string \| null | Override memo |

### Success response — `201 Created`

Same shape as create quotation.

---

## Convert quotation to invoice

```
POST /quotations/{id}/convert-to-invoice
```

Creates a new **DRAFT** invoice from any quotation (any status). Maps quotation lines to invoice lines (`quantity` → `qty`). Tax is recalculated using current company/item tax rules. The source quotation is unchanged.

**Roles:** same as create invoice (ADMIN, ACCOUNTANT, CASHIER).

### Optional request body

| Field | Type | Description |
|-------|------|-------------|
| `txn_date` | date | Invoice date (default: today) |
| `due_date` | date | Due date (default: from customer `payment_terms`) |
| `memo` | string \| null | Override memo |

### Success response — `201 Created`

Standard [invoice](./invoices.md) object with `status` = `DRAFT`.

### Error responses

| Status | Condition |
|--------|-----------|
| `403` | User cannot create invoices |
| `404` | Quotation not found |
| `422` | No lines, inactive item, insufficient stock, or validation failed |

---

## Download quotation PDF

```
GET /quotations/{id}/pdf
```

Streams a generated PDF of the quotation. Uses company letterhead and **`customer_snapshot`** / line data (not live customer master records). Internal `notes` are omitted.

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

### Example request

```http
GET /api/v1/quotations/1/pdf
Authorization: Bearer {token}
```

### Success response — `200 OK`

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

Body: PDF binary.

### Error responses

| Status | Condition |
|--------|-----------|
| `403` | User cannot view the quotation |
| `404` | Quotation not found or wrong company |

---

## Send quotation PDF by email

```
POST /quotations/{id}/send
```

Generates the same PDF as download, emails it to the customer, 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 `customer_snapshot.email` |
| `cc` | email or email[] | Optional CC recipients |
| `subject` | string | Defaults to `Quotation {number} from {company name}` |
| `message` | string | Optional plain-text body above the default notice |

```json
{
  "to": "buyer@example.com",
  "cc": ["ops@example.com"],
  "subject": "Your quotation Q-0001",
  "message": "Please review the attached quote."
}
```

### Success response — `200 OK`

Returns the quotation object (status may be `SENT`).

### Error responses

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

---

## Delete quotation

```
DELETE /quotations/{id}
```

### Success response — `204 No Content`

ADMIN only. Deletes the quotation and all lines. No ledger reversal (quotations are not posted).

---

## What quotations do not do

- No `transaction_id` or journal entries
- No inventory or stock impact on the quotation itself
- No customer balance / AR updates on the quotation itself
- Inactive catalog items are rejected on create, copy, and convert

---

## Sales transactions hub

Quotations also appear in the unified sales list: `GET /api/v1/sales-transactions` (default includes `quotation` alongside `invoice` and `payment`). See [sales-transactions.md](./sales-transactions.md).
