-- Commission Calculator database schema -- Source: https://personalsoftwarefactory.com/blueprints/commission-calculator (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 and rates are integer basis points (1000 = 10%). INTEGER is 32-bit in PostgreSQL, -- so do tier math (cents x basis points) in the app or in BIGINT, not in INTEGER columns. -- A commission plan: a name and an ordered list of marginal tiers. CREATE TABLE plans ( id TEXT PRIMARY KEY, name TEXT NOT NULL UNIQUE ); -- One tier of a plan (1 to 6 per plan). Each tier runs until the start of the next tier; the last has no end. -- App rule: from_pct increases with tier. A tier starting at 100 or above is an accelerator. CREATE TABLE plan_tiers ( plan_id TEXT NOT NULL REFERENCES plans(id) ON DELETE CASCADE, tier INTEGER NOT NULL CHECK (tier BETWEEN 1 AND 6), -- order in the list; the 1 to 6 range caps a plan at 6 tiers from_pct INTEGER NOT NULL CHECK (from_pct >= 0), -- whole percent of quota where the tier starts rate_bps INTEGER NOT NULL CHECK (rate_bps BETWEEN 0 AND 10000), -- 0% to 100% PRIMARY KEY (plan_id, tier), UNIQUE (plan_id, from_pct), CHECK ((tier = 1 AND from_pct = 0) OR (tier > 1 AND from_pct > 0)) -- the first tier starts at 0 ); -- A person who signs in: a rep on a plan, or a finance admin. CREATE TABLE people ( id TEXT PRIMARY KEY, name TEXT NOT NULL UNIQUE, email TEXT NOT NULL, -- the address the person signs in with title TEXT, role TEXT NOT NULL CHECK (role IN ('rep', 'finance')), plan_id TEXT REFERENCES plans(id), -- reps only; the plan used for open periods quota_cents INTEGER CHECK (quota_cents > 0 AND quota_cents % 100 = 0), -- reps only; monthly, whole dollars active BOOLEAN NOT NULL DEFAULT TRUE, CHECK (role = 'rep' OR (plan_id IS NULL AND quota_cents IS NULL)) ); CREATE UNIQUE INDEX people_email_unique ON people (lower(email)); -- unique, case-insensitive -- A calendar month of deals. Statements are calculated live while open and frozen in snapshot at approval. -- App rule (R6): periods are approved in order, once; there is no unapprove. CREATE TABLE periods ( id TEXT PRIMARY KEY CHECK (length(id) = 7), -- 'YYYY-MM' status TEXT NOT NULL DEFAULT 'open' CHECK (status IN ('open', 'approved')), approved_by TEXT REFERENCES people(id), approved_at TIMESTAMP, snapshot TEXT, -- JSON stored as text: the full statement of every rep, frozen at approval; NULL while open CHECK ((status = 'open' AND approved_by IS NULL AND approved_at IS NULL AND snapshot IS NULL) OR (status = 'approved' AND approved_by IS NOT NULL AND approved_at IS NOT NULL)) ); -- A closed deal, credited to its owner or split between the owner and one other rep. -- App rule: period_id is the month of close_date, and that period must exist and be open when the deal is added or edited (R7). -- App rule: only reps with a plan and a quota can be credited. CREATE TABLE deals ( id TEXT PRIMARY KEY, number INTEGER NOT NULL UNIQUE CHECK (number > 0), -- shown as #1001; sequential, never reused name TEXT NOT NULL CHECK (length(name) BETWEEN 1 AND 80), amount_cents INTEGER NOT NULL CHECK (amount_cents > 0 AND amount_cents <= 1000000000), -- at most $10,000,000 close_date DATE NOT NULL, period_id TEXT NOT NULL REFERENCES periods(id), owner_id TEXT NOT NULL REFERENCES people(id), -- the rep who closed it; gets the amount minus the split credit split_rep_id TEXT REFERENCES people(id), split_pct INTEGER CHECK (split_pct BETWEEN 1 AND 99), -- whole percent credited to split_rep_id, rounded half up to the cent (R2) status TEXT NOT NULL DEFAULT 'closed' CHECK (status IN ('closed', 'cancelled')), -- cancelled is final cancel_reason TEXT, cancelled_at TIMESTAMP, created_by TEXT NOT NULL REFERENCES people(id), created_at TIMESTAMP NOT NULL, CHECK ((split_rep_id IS NULL AND split_pct IS NULL) OR (split_rep_id IS NOT NULL AND split_pct IS NOT NULL)), CHECK (split_rep_id <> owner_id), CHECK ((status = 'closed' AND cancel_reason IS NULL AND cancelled_at IS NULL) OR (status = 'cancelled' AND cancel_reason IS NOT NULL AND cancelled_at IS NOT NULL)) ); CREATE INDEX deals_period ON deals (period_id, close_date, number); -- credit order (R1) -- A signed line on the statement of a rep that does not count toward attainment. -- App rule: period_id must be open when the adjustment is created. -- App rule (R10): a clawback is minus exactly what the deal line paid in the approved source period. CREATE TABLE adjustments ( id TEXT PRIMARY KEY, period_id TEXT NOT NULL REFERENCES periods(id), rep_id TEXT NOT NULL REFERENCES people(id), kind TEXT NOT NULL CHECK (kind IN ('clawback', 'manual')), amount_cents INTEGER NOT NULL CHECK (amount_cents <> 0), -- signed reason TEXT NOT NULL CHECK (length(reason) >= 3), deal_id TEXT REFERENCES deals(id), -- clawbacks only: the cancelled deal source_period_id TEXT REFERENCES periods(id), -- clawbacks only: the approved period it was paid in created_by TEXT NOT NULL REFERENCES people(id), created_at TIMESTAMP NOT NULL, CHECK ((kind = 'clawback' AND amount_cents < 0 AND deal_id IS NOT NULL AND source_period_id IS NOT NULL) OR (kind = 'manual' AND deal_id IS NULL AND source_period_id IS NULL)) ); -- One change, in words. Append-only; every change writes exactly one event (R14). CREATE TABLE events ( id TEXT PRIMARY KEY, at TIMESTAMP NOT NULL, actor_id TEXT NOT NULL REFERENCES people(id), period_id TEXT REFERENCES periods(id), type TEXT NOT NULL CHECK (type IN ('deal_added', 'deal_edited', 'deal_cancelled', 'clawback_created', 'adjustment_added', 'plan_created', 'plan_updated', 'rep_added', 'rep_updated', 'period_opened', 'period_approved')), text TEXT NOT NULL -- a readable sentence, e.g. "Clawback -$1,540 for Tom Reed in October 2026: ..." );