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