-- Expense Reports database schema -- Source: https://personalsoftwarefactory.com/blueprints/expense-reports (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. -- A person who files reports. manager_id is the usual approver (R7). -- App rule (R10): no manager loops; a person who is the approver of a submitted report cannot be deactivated until it is reassigned. CREATE TABLE people ( id TEXT PRIMARY KEY, name TEXT NOT NULL, email TEXT NOT NULL UNIQUE CHECK (email = lower(email)), -- stored lowercase manager_id TEXT REFERENCES people(id) CHECK (manager_id <> id), -- never the person themselves is_finance BOOLEAN NOT NULL DEFAULT FALSE, active BOOLEAN NOT NULL DEFAULT TRUE ); -- The single settings row. id is fixed at 1 so the table can never hold a second row. CREATE TABLE settings ( id INTEGER PRIMARY KEY CHECK (id = 1), company_name TEXT NOT NULL, time_zone TEXT NOT NULL DEFAULT 'America/New_York', -- IANA name; decides what "today" is receipt_required_cents INTEGER NOT NULL DEFAULT 7500 CHECK (receipt_required_cents >= 0), -- receipts required at or above this amount mileage_rate_cents_per_mile INTEGER NOT NULL DEFAULT 70 CHECK (mileage_rate_cents_per_mile >= 0), -- set to the rate your company pays max_age_days INTEGER NOT NULL DEFAULT 90 CHECK (max_age_days >= 1), -- older expenses are flagged reminder_days INTEGER NOT NULL DEFAULT 3 CHECK (reminder_days >= 1) -- remind the approver after this many whole days ); -- An expense category with an optional limit. Exactly one category is the mileage category. CREATE TABLE categories ( id TEXT PRIMARY KEY, name TEXT NOT NULL UNIQUE, limit_cents INTEGER CHECK (limit_cents >= 0), -- NULL means no limit; above it the expense is flagged Over limit requires_receipt BOOLEAN NOT NULL DEFAULT TRUE, is_mileage BOOLEAN NOT NULL DEFAULT FALSE, active BOOLEAN NOT NULL DEFAULT TRUE ); CREATE UNIQUE INDEX categories_one_mileage ON categories (is_mileage) WHERE is_mileage = TRUE; -- at most one mileage category -- An expense report: a batch of expenses that is submitted, approved and reimbursed together. -- App rule: number is sequential from 101 (shown as R-101). Only draft reports can be edited; reimbursed is final. -- App rule (R7): approver_id is set at submission: the active manager of the owner, else the first active finance person by name. CREATE TABLE reports ( id TEXT PRIMARY KEY, number INTEGER NOT NULL UNIQUE CHECK (number >= 101), title TEXT NOT NULL CHECK (length(title) BETWEEN 1 AND 100), owner_id TEXT NOT NULL REFERENCES people(id), status TEXT NOT NULL DEFAULT 'draft' CHECK (status IN ('draft', 'submitted', 'approved', 'reimbursed')), approver_id TEXT REFERENCES people(id), submitted_at TIMESTAMP, approved_at TIMESTAMP, reimbursed_at TIMESTAMP, payment_reference TEXT CHECK (length(payment_reference) BETWEEN 1 AND 100), -- set when reimbursed paid_on DATE, -- set when reimbursed; today or earlier version INTEGER NOT NULL DEFAULT 1, -- goes up on every change (R9) CHECK (approver_id <> owner_id), -- nobody approves their own report CHECK (status = 'draft' OR (approver_id IS NOT NULL AND submitted_at IS NOT NULL)), CHECK (status NOT IN ('approved', 'reimbursed') OR approved_at IS NOT NULL), CHECK ((status = 'reimbursed' AND reimbursed_at IS NOT NULL AND payment_reference IS NOT NULL AND paid_on IS NOT NULL) OR (status <> 'reimbursed' AND reimbursed_at IS NULL AND payment_reference IS NULL AND paid_on IS NULL)) ); -- One expense on a report. Flags (over limit, missing receipt, old, possible duplicate) are computed, never stored. -- App rule (R2): date not in the future and the category active when saved. Mileage fields only on the mileage category. CREATE TABLE expenses ( id TEXT PRIMARY KEY, report_id TEXT NOT NULL REFERENCES reports(id), category_id TEXT NOT NULL REFERENCES categories(id), date DATE NOT NULL, merchant TEXT NOT NULL CHECK (length(merchant) BETWEEN 1 AND 100), -- for mileage, the trip amount_cents INTEGER NOT NULL CHECK (amount_cents BETWEEN 1 AND 1000000), -- $0.01 to $10,000.00 miles_tenths INTEGER CHECK (miles_tenths BETWEEN 1 AND 20000), -- mileage only; 0.1 to 2,000 miles rate_cents_per_mile INTEGER CHECK (rate_cents_per_mile >= 0), -- mileage only; copied from settings when created, never changed description TEXT CHECK (length(description) <= 300), explanation TEXT CHECK (length(explanation) BETWEEN 5 AND 300), -- required before submitting when the expense has a flag (R4) receipt_file TEXT, -- reference to a private file (JPG, PNG or PDF up to 10 MB), never a public URL created_at TIMESTAMP NOT NULL, CHECK ((miles_tenths IS NULL AND rate_cents_per_mile IS NULL) OR (miles_tenths IS NOT NULL AND rate_cents_per_mile IS NOT NULL)), CHECK (miles_tenths IS NULL OR amount_cents = (2 * miles_tenths * rate_cents_per_mile + 10) / 20) -- R5: tenths x rate / 10, half up ); CREATE INDEX expenses_report ON expenses (report_id); CREATE INDEX expenses_date_amount ON expenses (date, amount_cents); -- for the possible-duplicate check -- A comment on a report, including rejection and reassignment comments. CREATE TABLE comments ( id TEXT PRIMARY KEY, report_id TEXT NOT NULL REFERENCES reports(id), author_id TEXT NOT NULL REFERENCES people(id), text TEXT NOT NULL CHECK (length(text) BETWEEN 1 AND 1000), created_at TIMESTAMP NOT NULL ); -- One action on a report. Append-only (R11). CREATE TABLE events ( id TEXT PRIMARY KEY, report_id TEXT NOT NULL REFERENCES reports(id), actor_id TEXT REFERENCES people(id), -- NULL for the system type TEXT NOT NULL CHECK (type IN ('created', 'expense_added', 'expense_edited', 'expense_removed', 'submitted', 'retracted', 'approved', 'rejected', 'reimbursed', 'reminded', 'commented', 'reassigned')), data TEXT, -- JSON stored as text created_at TIMESTAMP NOT NULL ); -- One email in the outbox. Written first, then sent; a failure never blocks the action. CREATE TABLE notifications ( id TEXT PRIMARY KEY, kind TEXT NOT NULL, to_email TEXT NOT NULL, report_id TEXT NOT NULL REFERENCES reports(id), subject TEXT NOT NULL, body TEXT NOT NULL, status TEXT NOT NULL DEFAULT 'pending' CHECK (status IN ('pending', 'sent', 'logged', 'failed')), error TEXT, created_at TIMESTAMP NOT NULL, sent_at TIMESTAMP ); CREATE INDEX notifications_status ON notifications (status);