# Sales quote management system database schema

Source: https://personalsoftwarefactory.com/schemas/quote-builder
Updated: 2026-12-04

> Thirteen tables. quotes carries the status, the client link token and the stored totals, quote_lines copies each price-list item's name and price when it is added, and approvals records each manager decision on a discount over the threshold. Money is integer cents and percentages are integer basis points, and an invoice copies the quote's lines and totals instead of recomputing them.

- 13 tables, 6 indexes
- Verified on SQLite 3.53.4 and PostgreSQL 19.0 (PGlite 0.6.1) on 2026-12-04
- 102 seed rows from the blueprint
- The blueprint's test build passed 29 of 29 acceptance checks

Files: [quote-builder.sql](https://personalsoftwarefactory.com/schemas/quote-builder.sql) · [quote-builder-seed.sql](https://personalsoftwarefactory.com/schemas/quote-builder-seed.sql) · [the Quote Builder blueprint](https://personalsoftwarefactory.com/blueprints/quote-builder)

## Relationships

- templates.created_by references people.id: each templates row points to at most one people row, and one people row can have many templates rows pointing to it.
- template_lines.template_id references templates.id: each template_lines row points to exactly one templates row, and one templates row can have many template_lines rows pointing to it. On delete: cascade.
- template_lines.product_id references products.id: each template_lines row points to exactly one products row, and one products row can have many template_lines rows pointing to it.
- quotes.owner_id references people.id: each quotes row points to exactly one people row, and one people row can have many quotes rows pointing to it.
- quotes.client_id references clients.id: each quotes row points to exactly one clients row, and one clients row can have many quotes rows pointing to it.
- quotes.duplicated_from_id references quotes.id: each quotes row points to at most one another quotes row, and one quotes row can have many quotes rows pointing to it.
- quote_lines.quote_id references quotes.id: each quote_lines row points to exactly one quotes row, and one quotes row can have many quote_lines rows pointing to it.
- quote_lines.product_id references products.id: each quote_lines row points to at most one products row, and one products row can have many quote_lines rows pointing to it.
- approvals.quote_id references quotes.id: each approvals row points to exactly one quotes row, and one quotes row can have many approvals rows pointing to it.
- approvals.requested_by references people.id: each approvals row points to exactly one people row, and one people row can have many approvals rows pointing to it.
- approvals.decided_by references people.id: each approvals row points to at most one people row, and one people row can have many approvals rows pointing to it.
- invoices.quote_id references quotes.id: each invoices row points to exactly one quotes row, and one quotes row can have at most one invoices row pointing to it.
- invoices.client_id references clients.id: each invoices row points to exactly one clients row, and one clients row can have many invoices rows pointing to it.
- invoices.created_by references people.id: each invoices row points to exactly one people row, and one people row can have many invoices rows pointing to it.
- invoice_lines.invoice_id references invoices.id: each invoice_lines row points to exactly one invoices row, and one invoices row can have many invoice_lines rows pointing to it.
- events.quote_id references quotes.id: each events row points to exactly one quotes row, and one quotes row can have many events rows pointing to it.
- events.actor_id references people.id: each events row points to at most one people row, and one people row can have many events rows pointing to it.
- notifications.person_id references people.id: each notifications row points to at most one people row, and one people row can have many notifications rows pointing to it.
- notifications.quote_id references quotes.id: each notifications row points to exactly one quotes row, and one quotes row can have many notifications rows pointing to it.

## Tables

Also described as: quote builder database design, CPQ quote database tables, sales proposal SQL schema.

### people

A sales rep or manager inside the company. Implements: Person.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `id` | TEXT | no |  | Primary key |  |
| `name` | TEXT | no |  |  |  |
| `email` | TEXT | no |  | Unique |  |
| `role` | TEXT | no |  |  | Check: `role IN ('rep', 'manager')` |
| `active` | BOOLEAN | no |  |  |  |

- App rule (section 4): deactivated people can't sign in and get no approval notifications.

### settings

The single row with company details, the approval threshold, default validity, tax and terms, and the next quote and invoice numbers. Implements: Settings.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `id` | INTEGER | no | `1` | Primary key | always 1, so there can only be one row Check: `id = 1` |
| `company_name` | TEXT | no |  |  | shown on quotes and invoices |
| `company_address` | TEXT | no |  |  |  |
| `company_email` | TEXT | no |  |  |  |
| `approval_threshold_bp` | INTEGER | no | `1500` |  | Check: `approval_threshold_bp BETWEEN 0 AND 10000` |
| `validity_days` | INTEGER | no | `30` |  | Check: `validity_days BETWEEN 1 AND 365` |
| `default_tax_rate_bp` | INTEGER | no | `800` |  | used for new clients Check: `default_tax_rate_bp BETWEEN 0 AND 2500` |
| `default_payment_terms_days` | INTEGER | no | `30` |  | Check: `default_payment_terms_days IN (0, 15, 30, 45, 60)` |
| `next_quote_number` | INTEGER | no | `1001` |  |  |
| `next_invoice_number` | INTEGER | no | `2001` |  |  |

### clients

A buying company with its contact, payment terms and tax rate. Implements: Client.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `id` | TEXT | no |  | Primary key |  |
| `name` | TEXT | no |  |  | unique ignoring case (index below) Check: `LENGTH(name) <= 80` |
| `contact_name` | TEXT | no |  |  |  |
| `contact_email` | TEXT | no |  |  | the app does full email validation Check: `contact_email LIKE '%_@_%'` |
| `address` | TEXT | yes |  |  | Check: `address IS NULL OR LENGTH(address) <= 200` |
| `payment_terms_days` | INTEGER | no |  |  | 0 shows as Due on receipt Check: `payment_terms_days IN (0, 15, 30, 45, 60)` |
| `tax_rate_bp` | INTEGER | no |  |  | Check: `tax_rate_bp BETWEEN 0 AND 2500` |
| `created_at` | TIMESTAMP | no | `CURRENT_TIMESTAMP` |  |  |

- Unique index `clients_name_unique` on (LOWER(name))

### products

One item on the price list, with its unit and price in cents. Implements: Product.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `id` | TEXT | no |  | Primary key |  |
| `sku` | TEXT | no |  | Unique | Check: `LENGTH(sku) BETWEEN 2 AND 20` |
| `name` | TEXT | no |  |  | Check: `LENGTH(name) <= 80` |
| `description` | TEXT | yes |  |  | Check: `description IS NULL OR LENGTH(description) <= 200` |
| `unit` | TEXT | no |  |  | e.g. per seat / year, each, per hour |
| `unit_price_cents` | INTEGER | no |  |  | Check: `unit_price_cents BETWEEN 0 AND 100000000` |
| `active` | BOOLEAN | no |  |  | archived products can't be added to quotes |

### templates

A named bundle of price-list items. Implements: Template.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `id` | TEXT | no |  | Primary key |  |
| `name` | TEXT | no |  | Unique |  |
| `description` | TEXT | yes |  |  |  |
| `created_by` | TEXT | yes |  | References people.id | the manager who created it |
| `updated_at` | TIMESTAMP | no | `CURRENT_TIMESTAMP` |  |  |

### template_lines

One product, quantity and discount in a template, in order. Implements: child table of Template.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `id` | TEXT | no |  | Primary key |  |
| `template_id` | TEXT | no |  | References templates.id, on delete cascade |  |
| `position` | INTEGER | no |  |  | order within the template Check: `position >= 1` |
| `product_id` | TEXT | no |  | References products.id |  |
| `qty` | NUMERIC | no |  |  | Check: `qty > 0 AND qty <= 100000` |
| `discount_bp` | INTEGER | no | `0` |  | Check: `discount_bp BETWEEN 0 AND 10000` |

- Unique together: `template_id`, `position`

### quotes

A quote with its status, validity, discounts, stored totals, client link and decision details. Implements: Quote.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `id` | TEXT | no |  | Primary key |  |
| `number` | INTEGER | no |  | Unique | sequential, shown as Q-1042 (R7) |
| `owner_id` | TEXT | no |  | References people.id |  |
| `client_id` | TEXT | no |  | References clients.id |  |
| `title` | TEXT | yes |  |  | lists show the client name when empty Check: `title IS NULL OR LENGTH(title) <= 80` |
| `status` | TEXT | no | `'draft'` |  | Check: `status IN ('draft', 'pending_approval', 'approved', 'sent', 'viewed', 'accepted', 'declined', 'expired')` |
| `valid_until` | DATE | no |  |  | valid through the end of this day (R10) |
| `tax_rate_bp` | INTEGER | no |  |  | copied from the client at creation Check: `tax_rate_bp BETWEEN 0 AND 2500` |
| `quote_discount_bp` | INTEGER | no | `0` |  | applies after line discounts Check: `quote_discount_bp BETWEEN 0 AND 10000` |
| `payment_terms_days` | INTEGER | no |  |  | copied from the client at creation Check: `payment_terms_days IN (0, 15, 30, 45, 60)` |
| `notes` | TEXT | yes |  |  | shown to the client Check: `notes IS NULL OR LENGTH(notes) <= 2000` |
| `list_cents` | INTEGER | no | `0` |  | sum of line gross |
| `discount_cents` | INTEGER | no | `0` |  | line discounts plus quote discount |
| `tax_cents` | INTEGER | no | `0` |  |  |
| `total_cents` | INTEGER | no | `0` |  |  |
| `public_token` | TEXT | yes |  | Unique | set on send, cleared on back to draft Check: `public_token IS NULL OR LENGTH(public_token) >= 24` |
| `sent_at` | TIMESTAMP | yes |  |  |  |
| `first_viewed_at` | TIMESTAMP | yes |  |  |  |
| `last_viewed_at` | TIMESTAMP | yes |  |  |  |
| `view_count` | INTEGER | no | `0` |  | client link opens only (R12) Check: `view_count >= 0` |
| `accepted_at` | TIMESTAMP | yes |  |  |  |
| `accepted_by_name` | TEXT | yes |  |  | typed by the client (R11) Check: `accepted_by_name IS NULL OR LENGTH(accepted_by_name) BETWEEN 2 AND 100` |
| `declined_at` | TIMESTAMP | yes |  |  |  |
| `decline_reason` | TEXT | yes |  |  |  |
| `expired_at` | TIMESTAMP | yes |  |  |  |
| `expiry_reminder_for` | DATE | yes |  |  | the valid_until a reminder was already sent for |
| `duplicated_from_id` | TEXT | yes |  | References quotes.id |  |
| `invoice_id` | TEXT | yes |  |  | the invoice for this quote; no foreign key because invoices references quotes |
| `created_at` | TIMESTAMP | no | `CURRENT_TIMESTAMP` |  |  |
| `updated_at` | TIMESTAMP | no | `CURRENT_TIMESTAMP` |  |  |

- Table check: `status NOT IN ('draft', 'pending_approval', 'approved') OR public_token IS NULL`
- Table check: `status NOT IN ('sent', 'viewed') OR (public_token IS NOT NULL AND sent_at IS NOT NULL)`
- Table check: `status <> 'accepted' OR (accepted_at IS NOT NULL AND accepted_by_name IS NOT NULL)`
- Table check: `status <> 'declined' OR declined_at IS NOT NULL`
- Table check: `status <> 'expired' OR expired_at IS NOT NULL`
- Table check: `invoice_id IS NULL OR status = 'accepted'`
- Index `quotes_status` on (status)
- Index `quotes_owner` on (owner_id)
- Index `quotes_client` on (client_id)
- App rule (section 7): status moves along the transitions in section 7; accepted, declined and expired are final.
- App rule (R2): list_cents, discount_cents, tax_cents and total_cents are recomputed by the section 6 math on every save.
- App rule (R6, R9): only drafts can be edited; the quote is locked while pending_approval or approved.

### quote_lines

One line on a quote, with the name, unit and price copied when it was added. Implements: QuoteLine.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `id` | TEXT | no |  | Primary key |  |
| `quote_id` | TEXT | no |  | References quotes.id |  |
| `position` | INTEGER | no |  |  | 1, 2, 3 ... Check: `position >= 1` |
| `product_id` | TEXT | yes |  | References products.id |  |
| `name` | TEXT | no |  |  | copied from the product when added |
| `description` | TEXT | yes |  |  |  |
| `unit` | TEXT | yes |  |  |  |
| `unit_price_cents` | INTEGER | no |  |  | Check: `unit_price_cents >= 0` |
| `qty` | NUMERIC | no |  |  | Check: `qty > 0 AND qty <= 100000` |
| `discount_bp` | INTEGER | no | `0` |  | Check: `discount_bp BETWEEN 0 AND 10000` |

- Unique together: `quote_id`, `position`
- App rule (R3): price-list lines keep the unit price copied when added; nobody can change it on the quote.
- App rule (R9): at most 50 lines per quote.
- App rule (section 5): qty has at most 2 decimal places.

### approvals

One request for a manager to approve a quote's discount, and the decision. Implements: Approval.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `id` | TEXT | no |  | Primary key |  |
| `quote_id` | TEXT | no |  | References quotes.id |  |
| `requested_by` | TEXT | no |  | References people.id |  |
| `requested_at` | TIMESTAMP | no | `CURRENT_TIMESTAMP` |  |  |
| `discount_bp` | INTEGER | no |  |  | total discount at request time, rounded half up; display only Check: `discount_bp BETWEEN 0 AND 10000` |
| `total_cents` | INTEGER | no |  |  | the total the manager approved |
| `status` | TEXT | no | `'pending'` |  | Check: `status IN ('pending', 'approved', 'rejected', 'cancelled')` |
| `decided_by` | TEXT | yes |  | References people.id |  |
| `decided_at` | TIMESTAMP | yes |  |  |  |
| `comment` | TEXT | yes |  |  | required on reject |

- Table check: `decided_by IS NULL OR decided_by <> requested_by`
- Table check: `status NOT IN ('approved', 'rejected') OR (decided_by IS NOT NULL AND decided_at IS NOT NULL)`
- Table check: `status <> 'rejected' OR COALESCE(LENGTH(comment), 0) >= 3`
- Index `approvals_quote` on (quote_id)
- App rule (R5): the decider is a manager who doesn't own the quote.

### invoices

The summary of an accepted quote handed to accounting, with copied bill-to details and totals. Implements: Invoice.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `id` | TEXT | no |  | Primary key |  |
| `number` | INTEGER | no |  | Unique | sequential, shown as INV-2001 (R7) |
| `quote_id` | TEXT | no |  | References quotes.id; Unique | one invoice per accepted quote |
| `client_id` | TEXT | no |  | References clients.id |  |
| `issued_on` | DATE | no |  |  |  |
| `due_on` | DATE | no |  |  |  |
| `bill_to_name` | TEXT | no |  |  | bill_to: copied from the client |
| `bill_to_contact_name` | TEXT | no |  |  |  |
| `bill_to_email` | TEXT | no |  |  |  |
| `bill_to_address` | TEXT | yes |  |  |  |
| `list_cents` | INTEGER | no |  |  | copied from the quote |
| `discount_cents` | INTEGER | no |  |  |  |
| `tax_cents` | INTEGER | no |  |  |  |
| `total_cents` | INTEGER | no |  |  |  |
| `created_by` | TEXT | no |  | References people.id |  |
| `created_at` | TIMESTAMP | no | `CURRENT_TIMESTAMP` |  |  |

- Table check: `due_on >= issued_on`
- App rule (R13): due_on = issued_on + the quote's payment_terms_days.

### invoice_lines

One quote line copied onto the invoice. Implements: child table of Invoice.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `id` | TEXT | no |  | Primary key |  |
| `invoice_id` | TEXT | no |  | References invoices.id |  |
| `position` | INTEGER | no |  |  | Check: `position >= 1` |
| `name` | TEXT | no |  |  |  |
| `description` | TEXT | yes |  |  |  |
| `unit` | TEXT | yes |  |  |  |
| `unit_price_cents` | INTEGER | no |  |  | Check: `unit_price_cents >= 0` |
| `qty` | NUMERIC | no |  |  | Check: `qty > 0` |
| `discount_bp` | INTEGER | no |  |  | Check: `discount_bp BETWEEN 0 AND 10000` |

- Unique together: `invoice_id`, `position`

### events

One append-only entry in a quote's activity log, by a person, the client or the system. Implements: Event.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `id` | TEXT | no |  | Primary key |  |
| `quote_id` | TEXT | no |  | References quotes.id |  |
| `actor_kind` | TEXT | no |  |  | Check: `actor_kind IN ('person', 'client', 'system')` |
| `actor_id` | TEXT | yes |  | References people.id | set only when actor_kind is person |
| `type` | TEXT | no |  |  | Check: `type IN ('created', 'edited', 'approval_requested', 'approved', 'rejected', 'approval_cancelled', 'sent', 'viewed', 'accepted', 'declined', 'expired', 'extended', 'withdrawn', 'duplicated', 'invoiced', 'reminder_sent')` |
| `data` | TEXT | yes |  |  | JSON text |
| `created_at` | TIMESTAMP | no | `CURRENT_TIMESTAMP` |  |  |

- Table check: `(actor_kind = 'person' AND actor_id IS NOT NULL) OR (actor_kind <> 'person' AND actor_id IS NULL)`
- Index `events_quote` on (quote_id, created_at)
- App rule (R14): every action writes one event on each quote it changes; never edited or deleted.

### notifications

One in-app update or email in the outbox. Implements: Notification.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `id` | TEXT | no |  | Primary key |  |
| `person_id` | TEXT | yes |  | References people.id | an internal recipient |
| `email` | TEXT | yes |  |  | or an outside address, such as the client contact |
| `channel` | TEXT | no |  |  | Check: `channel IN ('in_app', 'email')` |
| `kind` | TEXT | no |  |  |  |
| `quote_id` | TEXT | no |  | References quotes.id |  |
| `text` | TEXT | no |  |  |  |
| `created_at` | TIMESTAMP | no | `CURRENT_TIMESTAMP` |  |  |
| `sent_at` | TIMESTAMP | yes |  |  |  |
| `error` | TEXT | yes |  |  |  |

- Table check: `person_id IS NOT NULL OR email IS NOT NULL`
- App rule (section 10): a failed email keeps its error and is retried by the next job run.

## The SQL

```sql
-- Quote Builder database schema
-- Source: https://personalsoftwarefactory.com/blueprints/quote-builder (data model, section 5 of the blueprint)
-- Verified on SQLite 3.53.4 and PostgreSQL 19.0 (PGlite). All timestamps are UTC.
-- License: MIT. Free to copy and adapt.
--
-- Money is integer cents. Percentages are integer basis points: 1% = 100, 15% = 1500, 7.5% = 750.

-- A person inside the company: a sales rep or a manager.
-- App rule (section 4): deactivated people can't sign in and get no approval notifications.
CREATE TABLE people (
  id TEXT PRIMARY KEY,
  name TEXT NOT NULL,
  email TEXT NOT NULL UNIQUE,
  role TEXT NOT NULL CHECK (role IN ('rep', 'manager')),
  active BOOLEAN NOT NULL
);

-- The single settings row for the seller.
CREATE TABLE settings (
  id INTEGER PRIMARY KEY DEFAULT 1 CHECK (id = 1), -- always 1, so there can only be one row
  company_name TEXT NOT NULL, -- shown on quotes and invoices
  company_address TEXT NOT NULL,
  company_email TEXT NOT NULL,
  approval_threshold_bp INTEGER NOT NULL DEFAULT 1500 CHECK (approval_threshold_bp BETWEEN 0 AND 10000),
  validity_days INTEGER NOT NULL DEFAULT 30 CHECK (validity_days BETWEEN 1 AND 365),
  default_tax_rate_bp INTEGER NOT NULL DEFAULT 800 CHECK (default_tax_rate_bp BETWEEN 0 AND 2500), -- used for new clients
  default_payment_terms_days INTEGER NOT NULL DEFAULT 30 CHECK (default_payment_terms_days IN (0, 15, 30, 45, 60)),
  next_quote_number INTEGER NOT NULL DEFAULT 1001,
  next_invoice_number INTEGER NOT NULL DEFAULT 2001
);

-- A buying company.
CREATE TABLE clients (
  id TEXT PRIMARY KEY,
  name TEXT NOT NULL CHECK (LENGTH(name) <= 80), -- unique ignoring case (index below)
  contact_name TEXT NOT NULL,
  contact_email TEXT NOT NULL CHECK (contact_email LIKE '%_@_%'), -- the app does full email validation
  address TEXT CHECK (address IS NULL OR LENGTH(address) <= 200),
  payment_terms_days INTEGER NOT NULL CHECK (payment_terms_days IN (0, 15, 30, 45, 60)), -- 0 shows as Due on receipt
  tax_rate_bp INTEGER NOT NULL CHECK (tax_rate_bp BETWEEN 0 AND 2500),
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);

CREATE UNIQUE INDEX clients_name_unique ON clients (LOWER(name));

-- One item on the price list.
CREATE TABLE products (
  id TEXT PRIMARY KEY,
  sku TEXT NOT NULL UNIQUE CHECK (LENGTH(sku) BETWEEN 2 AND 20),
  name TEXT NOT NULL CHECK (LENGTH(name) <= 80),
  description TEXT CHECK (description IS NULL OR LENGTH(description) <= 200),
  unit TEXT NOT NULL, -- e.g. per seat / year, each, per hour
  unit_price_cents INTEGER NOT NULL CHECK (unit_price_cents BETWEEN 0 AND 100000000),
  active BOOLEAN NOT NULL -- archived products can't be added to quotes
);

-- A named bundle of price-list items.
CREATE TABLE templates (
  id TEXT PRIMARY KEY,
  name TEXT NOT NULL UNIQUE,
  description TEXT,
  created_by TEXT REFERENCES people(id), -- the manager who created it
  updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);

-- One line of a template (Template.lines in the spec). Price-list items only (R16).
CREATE TABLE template_lines (
  id TEXT PRIMARY KEY,
  template_id TEXT NOT NULL REFERENCES templates(id) ON DELETE CASCADE,
  position INTEGER NOT NULL CHECK (position >= 1), -- order within the template
  product_id TEXT NOT NULL REFERENCES products(id),
  qty NUMERIC NOT NULL CHECK (qty > 0 AND qty <= 100000),
  discount_bp INTEGER NOT NULL DEFAULT 0 CHECK (discount_bp BETWEEN 0 AND 10000),
  UNIQUE (template_id, position)
);

-- A quote. Never deleted (R7).
-- App rule (section 7): status moves along the transitions in section 7; accepted, declined and expired are final.
-- App rule (R2): list_cents, discount_cents, tax_cents and total_cents are recomputed by the section 6 math on every save.
-- App rule (R6, R9): only drafts can be edited; the quote is locked while pending_approval or approved.
CREATE TABLE quotes (
  id TEXT PRIMARY KEY,
  number INTEGER NOT NULL UNIQUE, -- sequential, shown as Q-1042 (R7)
  owner_id TEXT NOT NULL REFERENCES people(id),
  client_id TEXT NOT NULL REFERENCES clients(id),
  title TEXT CHECK (title IS NULL OR LENGTH(title) <= 80), -- lists show the client name when empty
  status TEXT NOT NULL DEFAULT 'draft' CHECK (status IN ('draft', 'pending_approval', 'approved', 'sent', 'viewed', 'accepted', 'declined', 'expired')),
  valid_until DATE NOT NULL, -- valid through the end of this day (R10)
  tax_rate_bp INTEGER NOT NULL CHECK (tax_rate_bp BETWEEN 0 AND 2500), -- copied from the client at creation
  quote_discount_bp INTEGER NOT NULL DEFAULT 0 CHECK (quote_discount_bp BETWEEN 0 AND 10000), -- applies after line discounts
  payment_terms_days INTEGER NOT NULL CHECK (payment_terms_days IN (0, 15, 30, 45, 60)), -- copied from the client at creation
  notes TEXT CHECK (notes IS NULL OR LENGTH(notes) <= 2000), -- shown to the client
  list_cents INTEGER NOT NULL DEFAULT 0, -- sum of line gross
  discount_cents INTEGER NOT NULL DEFAULT 0, -- line discounts plus quote discount
  tax_cents INTEGER NOT NULL DEFAULT 0,
  total_cents INTEGER NOT NULL DEFAULT 0,
  public_token TEXT UNIQUE CHECK (public_token IS NULL OR LENGTH(public_token) >= 24), -- set on send, cleared on back to draft
  sent_at TIMESTAMP,
  first_viewed_at TIMESTAMP,
  last_viewed_at TIMESTAMP,
  view_count INTEGER NOT NULL DEFAULT 0 CHECK (view_count >= 0), -- client link opens only (R12)
  accepted_at TIMESTAMP,
  accepted_by_name TEXT CHECK (accepted_by_name IS NULL OR LENGTH(accepted_by_name) BETWEEN 2 AND 100), -- typed by the client (R11)
  declined_at TIMESTAMP,
  decline_reason TEXT,
  expired_at TIMESTAMP,
  expiry_reminder_for DATE, -- the valid_until a reminder was already sent for
  duplicated_from_id TEXT REFERENCES quotes(id),
  invoice_id TEXT, -- the invoice for this quote; no foreign key because invoices references quotes
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  CHECK (status NOT IN ('draft', 'pending_approval', 'approved') OR public_token IS NULL), -- no client link before sending
  CHECK (status NOT IN ('sent', 'viewed') OR (public_token IS NOT NULL AND sent_at IS NOT NULL)),
  CHECK (status <> 'accepted' OR (accepted_at IS NOT NULL AND accepted_by_name IS NOT NULL)),
  CHECK (status <> 'declined' OR declined_at IS NOT NULL),
  CHECK (status <> 'expired' OR expired_at IS NOT NULL),
  CHECK (invoice_id IS NULL OR status = 'accepted') -- only accepted quotes are invoiced (R13)
);

CREATE INDEX quotes_status ON quotes (status);
CREATE INDEX quotes_owner ON quotes (owner_id);
CREATE INDEX quotes_client ON quotes (client_id);

-- One line on a quote. product_id null means a custom line.
-- App rule (R3): price-list lines keep the unit price copied when added; nobody can change it on the quote.
-- App rule (R9): at most 50 lines per quote.
-- App rule (section 5): qty has at most 2 decimal places.
CREATE TABLE quote_lines (
  id TEXT PRIMARY KEY,
  quote_id TEXT NOT NULL REFERENCES quotes(id),
  position INTEGER NOT NULL CHECK (position >= 1), -- 1, 2, 3 ...
  product_id TEXT REFERENCES products(id),
  name TEXT NOT NULL, -- copied from the product when added
  description TEXT,
  unit TEXT,
  unit_price_cents INTEGER NOT NULL CHECK (unit_price_cents >= 0),
  qty NUMERIC NOT NULL CHECK (qty > 0 AND qty <= 100000),
  discount_bp INTEGER NOT NULL DEFAULT 0 CHECK (discount_bp BETWEEN 0 AND 10000),
  UNIQUE (quote_id, position)
);

-- One request for a manager to approve a quote's total discount.
-- App rule (R5): the decider is a manager who doesn't own the quote.
CREATE TABLE approvals (
  id TEXT PRIMARY KEY,
  quote_id TEXT NOT NULL REFERENCES quotes(id),
  requested_by TEXT NOT NULL REFERENCES people(id),
  requested_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  discount_bp INTEGER NOT NULL CHECK (discount_bp BETWEEN 0 AND 10000), -- total discount at request time, rounded half up; display only
  total_cents INTEGER NOT NULL, -- the total the manager approved
  status TEXT NOT NULL DEFAULT 'pending' CHECK (status IN ('pending', 'approved', 'rejected', 'cancelled')),
  decided_by TEXT REFERENCES people(id),
  decided_at TIMESTAMP,
  comment TEXT, -- required on reject
  CHECK (decided_by IS NULL OR decided_by <> requested_by), -- R5: nobody decides their own request
  CHECK (status NOT IN ('approved', 'rejected') OR (decided_by IS NOT NULL AND decided_at IS NOT NULL)),
  CHECK (status <> 'rejected' OR COALESCE(LENGTH(comment), 0) >= 3) -- R5
);

CREATE INDEX approvals_quote ON approvals (quote_id);

-- The summary of an accepted quote handed to accounting. Copies, never recomputes (R13).
-- App rule (R13): due_on = issued_on + the quote's payment_terms_days.
CREATE TABLE invoices (
  id TEXT PRIMARY KEY,
  number INTEGER NOT NULL UNIQUE, -- sequential, shown as INV-2001 (R7)
  quote_id TEXT NOT NULL UNIQUE REFERENCES quotes(id), -- one invoice per accepted quote
  client_id TEXT NOT NULL REFERENCES clients(id),
  issued_on DATE NOT NULL,
  due_on DATE NOT NULL,
  bill_to_name TEXT NOT NULL, -- bill_to: copied from the client
  bill_to_contact_name TEXT NOT NULL,
  bill_to_email TEXT NOT NULL,
  bill_to_address TEXT,
  list_cents INTEGER NOT NULL, -- copied from the quote
  discount_cents INTEGER NOT NULL,
  tax_cents INTEGER NOT NULL,
  total_cents INTEGER NOT NULL,
  created_by TEXT NOT NULL REFERENCES people(id),
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  CHECK (due_on >= issued_on)
);

-- One line copied from the quote onto the invoice (Invoice.lines in the spec).
CREATE TABLE invoice_lines (
  id TEXT PRIMARY KEY,
  invoice_id TEXT NOT NULL REFERENCES invoices(id),
  position INTEGER NOT NULL CHECK (position >= 1),
  name TEXT NOT NULL,
  description TEXT,
  unit TEXT,
  unit_price_cents INTEGER NOT NULL CHECK (unit_price_cents >= 0),
  qty NUMERIC NOT NULL CHECK (qty > 0),
  discount_bp INTEGER NOT NULL CHECK (discount_bp BETWEEN 0 AND 10000),
  UNIQUE (invoice_id, position)
);

-- One entry in a quote's activity log. Append-only.
-- App rule (R14): every action writes one event on each quote it changes; never edited or deleted.
CREATE TABLE events (
  id TEXT PRIMARY KEY,
  quote_id TEXT NOT NULL REFERENCES quotes(id),
  actor_kind TEXT NOT NULL CHECK (actor_kind IN ('person', 'client', 'system')),
  actor_id TEXT REFERENCES people(id), -- set only when actor_kind is person
  type TEXT NOT NULL CHECK (type IN ('created', 'edited', 'approval_requested', 'approved', 'rejected', 'approval_cancelled', 'sent', 'viewed', 'accepted', 'declined', 'expired', 'extended', 'withdrawn', 'duplicated', 'invoiced', 'reminder_sent')),
  data TEXT, -- JSON text
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  CHECK ((actor_kind = 'person' AND actor_id IS NOT NULL) OR (actor_kind <> 'person' AND actor_id IS NULL))
);

CREATE INDEX events_quote ON events (quote_id, created_at);

-- One in-app update or email in the outbox, written before sending.
-- App rule (section 10): a failed email keeps its error and is retried by the next job run.
CREATE TABLE notifications (
  id TEXT PRIMARY KEY,
  person_id TEXT REFERENCES people(id), -- an internal recipient
  email TEXT, -- or an outside address, such as the client contact
  channel TEXT NOT NULL CHECK (channel IN ('in_app', 'email')),
  kind TEXT NOT NULL,
  quote_id TEXT NOT NULL REFERENCES quotes(id),
  text TEXT NOT NULL,
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  sent_at TIMESTAMP,
  error TEXT,
  CHECK (person_id IS NOT NULL OR email IS NOT NULL)
);
```

## Seed data

Loads section 14: 5 people, the settings row, 7 clients, 8 products (Legacy setup fee archived), 3 templates with 13 lines, the 9 quotes Q-1036 to Q-1044 with 23 lines whose totals follow section 6 and match the spec, 2 approvals (Q-1040 pending, Q-1043 approved by Dana), invoice INV-2001 with its 3 copied lines, and 27 history events. The spec's dates are relative to today with no fixed date, so the seed fixes today to Tue 13 Oct 2026, the date its own examples imply (Q-1042 valid until Nov 10 at T+28), at 12:00 UTC. Where the spec gives no date, a quote was created on its first stated day with valid_until 30 days later, sent the same day, viewed once before an accept or decline, and invoiced on acceptance; times of day, client links and the template and client creation times are placeholders. The outbox is empty because the spec seeds none.

## Load it

```sh
# SQLite
curl -s https://personalsoftwarefactory.com/schemas/quote-builder.sql https://personalsoftwarefactory.com/schemas/quote-builder-seed.sql | sqlite3 quote_builder.db
# PostgreSQL
createdb quote_builder && curl -s https://personalsoftwarefactory.com/schemas/quote-builder.sql https://personalsoftwarefactory.com/schemas/quote-builder-seed.sql | psql -v ON_ERROR_STOP=1 quote_builder
```

## Design decisions

### Cents and basis points, never decimals

Section 5 and R1 store money as integer cents and percentages as integer basis points (7.5% is 750). The approval test in section 6, step 10 compares total discount times 10000 with the threshold times the list subtotal, all integers, so exactly 15.0% does not need approval and 15.5% does. Floating-point percentages would make that boundary unreliable.

### Lines and invoices copy values instead of referencing them

quote_lines copies the product's name, unit and price when the line is added (R3, R15), so a later price change never touches an existing quote. invoice_lines and the invoice's bill_to columns copy the quote again, and the invoice totals are copied, never recomputed (R13). The spec's bill_to list (client name, contact name, email, address) is stored as four bill_to_ columns.

### Totals are stored, but recomputed on every save

Section 5 stores list_cents, discount_cents, tax_cents and total_cents so lists don't recompute, and R2 says the server recomputes them with the section 6 math on every save and never trusts totals from the browser. The rounding is per step, half up, which plain SQL division does differently on SQLite and PostgreSQL, so the math belongs in one app function, not in the database.

### The client link exists only while a quote is out

public_token is set on send and cleared on back to draft (section 7). CHECK constraints keep it null in draft, pending_approval and approved, and require it with sent_at in sent and viewed. accepted, declined and expired keep their token so the client page can still show the outcome.

### The approval's discount is for display only

Our build test found that Approval stored the discount as whole basis points while section 6 says never to round percentages. approvals.discount_bp is now display-only, rounded half up (17.24% shows as 1724), and whether a quote needs approval is always decided by the integer comparison in section 6, step 10. A CHECK also stops anyone deciding an approval they requested (R5). (This point comes from a gap the blueprint's build test found.)

### One foreign key is dropped to break a cycle

An invoice references its quote and the quote stores its invoice_id, a true cycle. invoices.quote_id keeps the foreign key and is UNIQUE (one invoice per accepted quote, R13); quotes.invoice_id is a plain column with a comment, and a CHECK allows it only on accepted quotes.

### Events name who acted, including the client and the system

events has actor_kind (person, client or system) and actor_id only for people, enforced by a CHECK, because the client has no account and the daily job expires quotes. Our build test found R14's "exactly one event per action" contradicted Duplicate and repeat client views; the spec now names both exceptions (duplicated is written on both quotes, and only the first view writes viewed), which the app enforces. (This point comes from a gap the blueprint's build test found.)

## Example queries

### Home tiles

```sql
SELECT
  (SELECT COUNT(*) FROM quotes WHERE status IN ('sent', 'viewed')) AS open_count,
  (SELECT COALESCE(SUM(total_cents), 0) FROM quotes WHERE status IN ('sent', 'viewed')) AS open_total_cents,
  (SELECT COUNT(*) FROM quotes WHERE status = 'pending_approval') AS awaiting_approval,
  (SELECT COUNT(*) FROM quotes WHERE status = 'accepted' AND accepted_at >= '2026-09-13 00:00:00') AS accepted_30d,
  (SELECT COALESCE(SUM(total_cents), 0) FROM quotes WHERE status = 'accepted' AND accepted_at >= '2026-09-13 00:00:00') AS accepted_30d_cents,
  (SELECT COUNT(*) FROM quotes WHERE accepted_at >= '2026-07-15 00:00:00') AS won_90d,
  (SELECT COUNT(*) FROM quotes WHERE accepted_at >= '2026-07-15 00:00:00' OR declined_at >= '2026-07-15 00:00:00' OR expired_at >= '2026-07-15 00:00:00') AS closed_90d;
```

On the seed data: 1 row. Open quotes 2 (1889600 cents, $18,896), awaiting approval 1, accepted in 30 days 2 (1449360 cents, $14,493.60), and 2 won of 4 closed in 90 days (win rate 50%), as section 14 says.

### Open quotes with countdown dates

```sql
SELECT
  q.number,
  c.name AS client,
  p.name AS owner,
  q.status,
  q.total_cents,
  q.valid_until,
  q.view_count
FROM quotes q
JOIN clients c ON c.id = q.client_id
JOIN people p ON p.id = q.owner_id
WHERE q.status IN ('sent', 'viewed')
ORDER BY q.valid_until, q.number;
```

On the seed data: 2 rows. Q-1041 for Bluebird Health (viewed 3 times, valid until 2026-10-15) and Q-1042 for Kestrel Schools (sent, valid until 2026-11-10).

### Approvals a manager can decide, oldest first

```sql
SELECT q.number, c.name AS client, p.name AS rep, a.total_cents, a.discount_bp, a.requested_at
FROM approvals a
JOIN quotes q ON q.id = a.quote_id
JOIN clients c ON c.id = q.client_id
JOIN people p ON p.id = q.owner_id
WHERE a.status = 'pending' AND q.owner_id <> 'person-dana' AND a.requested_by <> 'person-dana'
ORDER BY a.requested_at;
```

On the seed data: 1 row for Dana Ruiz, Q-1040 for Ridgeway Partners by Lee Park, 1555200 cents with a 1724 basis-point (17.2%) discount.

### Invoice CSV rows

```sql
SELECT
  i.number,
  i.issued_on,
  i.due_on,
  i.bill_to_name AS client,
  l.name AS item,
  l.qty,
  l.unit_price_cents,
  l.discount_bp,
  i.total_cents
FROM invoices i
JOIN invoice_lines l ON l.invoice_id = i.id
ORDER BY i.number, l.position;
```

On the seed data: 3 rows for INV-2001 (Olsen & Co, Starter package lines, due 15 days after issue), each with the invoice total 464400 cents. The line total is computed in the app with the section 6 rounding.

### Clients with open quotes and accepted totals

```sql
SELECT
  c.name,
  c.contact_name,
  c.payment_terms_days,
  c.tax_rate_bp,
  SUM(CASE WHEN q.status IN ('sent', 'viewed') THEN 1 ELSE 0 END) AS open_quotes,
  SUM(CASE WHEN q.status = 'accepted' THEN q.total_cents ELSE 0 END) AS accepted_cents
FROM clients c
LEFT JOIN quotes q ON q.client_id = c.id
GROUP BY c.name, c.contact_name, c.payment_terms_days, c.tax_rate_bp
ORDER BY c.name;
```

On the seed data: 7 rows. Acme Logistics has 0 open quotes and 984960 cents accepted; Kestrel Schools has 1 open quote and a 0 tax rate.

## What was not tested

- MySQL, MariaDB, SQL Server and Oracle. The file avoids engine-specific syntax, but it was not run on them.
- Other versions of SQLite and PostgreSQL than the two named above.
- PostgreSQL as a server. The PostgreSQL run used PGlite, which is PostgreSQL compiled to run in-process.
- Production load. The check loads the seed rows and runs the example queries; it says nothing about performance with real volumes.
- Migrations. This is a schema for a new database, not a path from an existing one.

## The blueprint

This schema is section 5 of the [Quote Builder blueprint](https://personalsoftwarefactory.com/blueprints/quote-builder), which also has the business rules, the screens, the phased build prompts and the acceptance checks. Paste-ready spec: https://personalsoftwarefactory.com/blueprints/quote-builder/blueprint.md
