Sales quote management system database schema

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),
  • 102 seed rows from the blueprint
  • The blueprint's test build passed 29 of 29 acceptance checks

Copy the SQLDownload SQLDownload seed data

By Sam Claassen · From the Quote Builder blueprint · MIT licensed · Markdown version

Quote builder entity-relationship diagram

peopleidPK+ 4 more columnssettingsidPK+ 9 more columnsclientsidPK+ 7 more columnsproductsidPK+ 6 more columnstemplatesidPKcreated_by→ people+ 3 more columnstemplate_linesidPKtemplate_id→ templatesproduct_id→ products+ 3 more columnsquotesidPKowner_id→ peopleclient_id→ clientsduplicated_from_id→ quotes+ 26 more columnsquote_linesidPKquote_id→ quotesproduct_id→ products+ 7 more columnsapprovalsidPKquote_id→ quotesrequested_by→ peopledecided_by→ people+ 6 more columnsinvoicesidPKquote_id→ quotesclient_id→ clientscreated_by→ people+ 12 more columnsinvoice_linesidPKinvoice_id→ invoices+ 7 more columnseventsidPKquote_id→ quotesactor_id→ people+ 4 more columnsnotificationsidPKperson_id→ peoplequote_id→ quotes+ 7 more columnspeopleidPK+ 4 more columnssettingsidPK+ 9 more columnsclientsidPK+ 7 more columnsproductsidPK+ 6 more columnstemplatesidPKcreated_by→ people+ 3 more columnstemplate_linesidPKtemplate_id→ templatesproduct_id→ products+ 3 more columnsquotesidPKowner_id→ peopleclient_id→ clientsduplicated_from_id→ quotes+ 26 more columnsquote_linesidPKquote_id→ quotesproduct_id→ products+ 7 more columnsapprovalsidPKquote_id→ quotesrequested_by→ peopledecided_by→ people+ 6 more columnsinvoicesidPKquote_id→ quotesclient_id→ clientscreated_by→ people+ 12 more columnsinvoice_linesidPKinvoice_id→ invoices+ 7 more columnseventsidPKquote_id→ quotesactor_id→ people+ 4 more columnsnotificationsidPKperson_id→ peoplequote_id→ quotes+ 7 more columns
Each box is a table with its key columns. A line runs from a table up to a table it references, with the forked end on the side that has many rows; a dashed line is an optional reference.

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.

Quote builder tables

The schema has 13 tables. Every column below is read from the SQL file itself. Also described as: quote builder database design, CPQ quote database tables, sales proposal SQL schema.

TableImplementsWhat a row is
peoplePersonA sales rep or manager inside the company.
settingsSettingsThe single row with company details, the approval threshold, default validity, tax and terms, and the next quote and invoice numbers.
clientsClientA buying company with its contact, payment terms and tax rate.
productsProductOne item on the price list, with its unit and price in cents.
templatesTemplateA named bundle of price-list items.
template_lineschild table of TemplateOne product, quantity and discount in a template, in order.
quotesQuoteA quote with its status, validity, discounts, stored totals, client link and decision details.
quote_linesQuoteLineOne line on a quote, with the name, unit and price copied when it was added.
approvalsApprovalOne request for a manager to approve a quote's discount, and the decision.
invoicesInvoiceThe summary of an accepted quote handed to accounting, with copied bill-to details and totals.
invoice_lineschild table of InvoiceOne quote line copied onto the invoice.
eventsEventOne append-only entry in a quote's activity log, by a person, the client or the system.
notificationsNotificationOne in-app update or email in the outbox.

people

A person inside the company: a sales rep or a manager.

ColumnTypeNullDefaultKeyNotes
idTEXTnoPrimary key
nameTEXTno
emailTEXTnoUnique
roleTEXTnoCheck: role IN ('rep', 'manager')
activeBOOLEANno
  • App rule (section 4): deactivated people can't sign in and get no approval notifications.

settings

The single settings row for the seller.

ColumnTypeNullDefaultKeyNotes
idINTEGERno1Primary keyalways 1, so there can only be one rowCheck: id = 1
company_nameTEXTnoshown on quotes and invoices
company_addressTEXTno
company_emailTEXTno
approval_threshold_bpINTEGERno1500Check: approval_threshold_bp BETWEEN 0 AND 10000
validity_daysINTEGERno30Check: validity_days BETWEEN 1 AND 365
default_tax_rate_bpINTEGERno800used for new clientsCheck: default_tax_rate_bp BETWEEN 0 AND 2500
default_payment_terms_daysINTEGERno30Check: default_payment_terms_days IN (0, 15, 30, 45, 60)
next_quote_numberINTEGERno1001
next_invoice_numberINTEGERno2001

clients

A buying company.

ColumnTypeNullDefaultKeyNotes
idTEXTnoPrimary key
nameTEXTnounique ignoring case (index below)Check: LENGTH(name) <= 80
contact_nameTEXTno
contact_emailTEXTnothe app does full email validationCheck: contact_email LIKE '%_@_%'
addressTEXTyesCheck: address IS NULL OR LENGTH(address) <= 200
payment_terms_daysINTEGERno0 shows as Due on receiptCheck: payment_terms_days IN (0, 15, 30, 45, 60)
tax_rate_bpINTEGERnoCheck: tax_rate_bp BETWEEN 0 AND 2500
created_atTIMESTAMPnoCURRENT_TIMESTAMP
  • Unique index clients_name_unique on (LOWER(name))

products

One item on the price list.

ColumnTypeNullDefaultKeyNotes
idTEXTnoPrimary key
skuTEXTnoUniqueCheck: LENGTH(sku) BETWEEN 2 AND 20
nameTEXTnoCheck: LENGTH(name) <= 80
descriptionTEXTyesCheck: description IS NULL OR LENGTH(description) <= 200
unitTEXTnoe.g. per seat / year, each, per hour
unit_price_centsINTEGERnoCheck: unit_price_cents BETWEEN 0 AND 100000000
activeBOOLEANnoarchived products can't be added to quotes

templates

A named bundle of price-list items.

ColumnTypeNullDefaultKeyNotes
idTEXTnoPrimary key
nameTEXTnoUnique
descriptionTEXTyes
created_byTEXTyesReferences people.idthe manager who created it
updated_atTIMESTAMPnoCURRENT_TIMESTAMP

template_lines

One line of a template (Template.lines in the spec). Price-list items only (R16).

ColumnTypeNullDefaultKeyNotes
idTEXTnoPrimary key
template_idTEXTnoReferences templates.id, on delete cascade
positionINTEGERnoorder within the templateCheck: position >= 1
product_idTEXTnoReferences products.id
qtyNUMERICnoCheck: qty > 0 AND qty <= 100000
discount_bpINTEGERno0Check: discount_bp BETWEEN 0 AND 10000
  • Unique together: template_id, position

quotes

A quote. Never deleted (R7).

ColumnTypeNullDefaultKeyNotes
idTEXTnoPrimary key
numberINTEGERnoUniquesequential, shown as Q-1042 (R7)
owner_idTEXTnoReferences people.id
client_idTEXTnoReferences clients.id
titleTEXTyeslists show the client name when emptyCheck: title IS NULL OR LENGTH(title) <= 80
statusTEXTno'draft'Check: status IN ('draft', 'pending_approval', 'approved', 'sent', 'viewed', 'accepted', 'declined', 'expired')
valid_untilDATEnovalid through the end of this day (R10)
tax_rate_bpINTEGERnocopied from the client at creationCheck: tax_rate_bp BETWEEN 0 AND 2500
quote_discount_bpINTEGERno0applies after line discountsCheck: quote_discount_bp BETWEEN 0 AND 10000
payment_terms_daysINTEGERnocopied from the client at creationCheck: payment_terms_days IN (0, 15, 30, 45, 60)
notesTEXTyesshown to the clientCheck: notes IS NULL OR LENGTH(notes) <= 2000
list_centsINTEGERno0sum of line gross
discount_centsINTEGERno0line discounts plus quote discount
tax_centsINTEGERno0
total_centsINTEGERno0
public_tokenTEXTyesUniqueset on send, cleared on back to draftCheck: public_token IS NULL OR LENGTH(public_token) >= 24
sent_atTIMESTAMPyes
first_viewed_atTIMESTAMPyes
last_viewed_atTIMESTAMPyes
view_countINTEGERno0client link opens only (R12)Check: view_count >= 0
accepted_atTIMESTAMPyes
accepted_by_nameTEXTyestyped by the client (R11)Check: accepted_by_name IS NULL OR LENGTH(accepted_by_name) BETWEEN 2 AND 100
declined_atTIMESTAMPyes
decline_reasonTEXTyes
expired_atTIMESTAMPyes
expiry_reminder_forDATEyesthe valid_until a reminder was already sent for
duplicated_from_idTEXTyesReferences quotes.id
invoice_idTEXTyesthe invoice for this quote; no foreign key because invoices references quotes
created_atTIMESTAMPnoCURRENT_TIMESTAMP
updated_atTIMESTAMPnoCURRENT_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. product_id null means a custom line.

ColumnTypeNullDefaultKeyNotes
idTEXTnoPrimary key
quote_idTEXTnoReferences quotes.id
positionINTEGERno1, 2, 3 ...Check: position >= 1
product_idTEXTyesReferences products.id
nameTEXTnocopied from the product when added
descriptionTEXTyes
unitTEXTyes
unit_price_centsINTEGERnoCheck: unit_price_cents >= 0
qtyNUMERICnoCheck: qty > 0 AND qty <= 100000
discount_bpINTEGERno0Check: 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 total discount.

ColumnTypeNullDefaultKeyNotes
idTEXTnoPrimary key
quote_idTEXTnoReferences quotes.id
requested_byTEXTnoReferences people.id
requested_atTIMESTAMPnoCURRENT_TIMESTAMP
discount_bpINTEGERnototal discount at request time, rounded half up; display onlyCheck: discount_bp BETWEEN 0 AND 10000
total_centsINTEGERnothe total the manager approved
statusTEXTno'pending'Check: status IN ('pending', 'approved', 'rejected', 'cancelled')
decided_byTEXTyesReferences people.id
decided_atTIMESTAMPyes
commentTEXTyesrequired 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. Copies, never recomputes (R13).

ColumnTypeNullDefaultKeyNotes
idTEXTnoPrimary key
numberINTEGERnoUniquesequential, shown as INV-2001 (R7)
quote_idTEXTnoReferences quotes.id; Uniqueone invoice per accepted quote
client_idTEXTnoReferences clients.id
issued_onDATEno
due_onDATEno
bill_to_nameTEXTnobill_to: copied from the client
bill_to_contact_nameTEXTno
bill_to_emailTEXTno
bill_to_addressTEXTyes
list_centsINTEGERnocopied from the quote
discount_centsINTEGERno
tax_centsINTEGERno
total_centsINTEGERno
created_byTEXTnoReferences people.id
created_atTIMESTAMPnoCURRENT_TIMESTAMP
  • Table check: due_on >= issued_on
  • App rule (R13): due_on = issued_on + the quote's payment_terms_days.

invoice_lines

One line copied from the quote onto the invoice (Invoice.lines in the spec).

ColumnTypeNullDefaultKeyNotes
idTEXTnoPrimary key
invoice_idTEXTnoReferences invoices.id
positionINTEGERnoCheck: position >= 1
nameTEXTno
descriptionTEXTyes
unitTEXTyes
unit_price_centsINTEGERnoCheck: unit_price_cents >= 0
qtyNUMERICnoCheck: qty > 0
discount_bpINTEGERnoCheck: discount_bp BETWEEN 0 AND 10000
  • Unique together: invoice_id, position

events

One entry in a quote's activity log. Append-only.

ColumnTypeNullDefaultKeyNotes
idTEXTnoPrimary key
quote_idTEXTnoReferences quotes.id
actor_kindTEXTnoCheck: actor_kind IN ('person', 'client', 'system')
actor_idTEXTyesReferences people.idset only when actor_kind is person
typeTEXTnoCheck: type IN ('created', 'edited', 'approval_requested', 'approved', 'rejected', 'approval_cancelled', 'sent', 'viewed', 'accepted', 'declined', 'expired', 'extended', 'withdrawn', 'duplicated', 'invoiced', 'reminder_sent')
dataTEXTyesJSON text
created_atTIMESTAMPnoCURRENT_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, written before sending.

ColumnTypeNullDefaultKeyNotes
idTEXTnoPrimary key
person_idTEXTyesReferences people.idan internal recipient
emailTEXTyesor an outside address, such as the client contact
channelTEXTnoCheck: channel IN ('in_app', 'email')
kindTEXTno
quote_idTEXTnoReferences quotes.id
textTEXTno
created_atTIMESTAMPnoCURRENT_TIMESTAMP
sent_atTIMESTAMPyes
errorTEXTyes
  • 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

The whole schema in one file. It runs top to bottom, unmodified, on SQLite and PostgreSQL. Download quote-builder.sql or copy it from here.

quote-builder.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.

Download quote-builder-seed.sql (102 rows). It loads with foreign keys enforced on both engines.

Load it

SQLite, into a new file:

sqlite3
curl -s https://personalsoftwarefactory.com/schemas/quote-builder.sql https://personalsoftwarefactory.com/schemas/quote-builder-seed.sql | sqlite3 quote_builder.db

PostgreSQL, into a new database:

psql
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

SQLite only enforces foreign keys when the connection asks for it: run PRAGMA foreign_keys = ON; when your app opens the database.

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

From the build test · field report

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).

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

From the build test · field report

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.

Example queries

The queries behind the tool's main screens. Each returned the same rows on both engines against the seed data.

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 comes from

Tables are the easy half. This schema is section 5 of the Quote Builder blueprint, a free spec that also has the business rules, the screens, 4 phased build prompts and 29 acceptance checks for building the whole tool with an AI app builder. We built it from those prompts and the result passed 29 of 29 acceptance checks in a browser.

Follow new blueprints

Every new blueprint and build test appears in the feed and in the public GitHub repository. No account, no email list.