-- Invoice Reminders database schema -- Source: https://personalsoftwarefactory.com/blueprints/invoice-reminders (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. -- The single settings row for the one company, sender and time zone. -- App rule (R8): turning sending_enabled on is admin-only and needs a confirmation the server checks. CREATE TABLE settings ( id TEXT PRIMARY KEY CHECK (id = 'settings'), -- exactly one row company_name TEXT NOT NULL, company_domain TEXT NOT NULL, -- the sign-in domain, for example northwind.example from_name TEXT NOT NULL, -- used by the {from_name} merge field from_email TEXT NOT NULL, -- the shared address reminders are sent from reply_to TEXT, time_zone TEXT NOT NULL DEFAULT 'America/New_York', -- IANA name; business dates are calendar dates in this zone send_time TEXT NOT NULL DEFAULT '09:00' CHECK (send_time LIKE '__:__' AND send_time BETWEEN '00:00' AND '23:59' AND substr(send_time, 4, 2) < '60'), -- HH:MM, 24-hour, company time sending_enabled BOOLEAN NOT NULL DEFAULT FALSE, -- off: every customer reminder is written to the outbox as held account_owner_cc_on_final BOOLEAN NOT NULL DEFAULT TRUE -- copy the active account owner on the Final notice ); -- One row per person who signs in. -- App rule (section 4): an admin cannot deactivate or demote themselves, so there is always an active admin. CREATE TABLE people ( id TEXT PRIMARY KEY, name TEXT NOT NULL, email TEXT NOT NULL UNIQUE, -- must be on settings.company_domain to sign in role TEXT NOT NULL CHECK (role IN ('admin', 'member')), active BOOLEAN NOT NULL DEFAULT TRUE ); -- One row per customer who owes money. -- App rule (section 7): an import row finds its customer by name, trimmed and ignoring case, and its contact_email is used only when the customer is created. CREATE TABLE customers ( id TEXT PRIMARY KEY, name TEXT NOT NULL, -- unique, compared trimmed and ignoring case (index below) contact_email TEXT, -- may be empty; the run then skips the customer's invoices (No contact email) cc_emails TEXT NOT NULL DEFAULT '', -- comma-separated list of valid addresses, copied on every reminder account_owner_id TEXT REFERENCES people(id), -- may be empty for customers created by import paused BOOLEAN NOT NULL DEFAULT FALSE, -- paused customers get no reminders (R3) paused_reason TEXT, notes TEXT, CHECK (paused = FALSE OR paused_reason IS NOT NULL) ); -- One row per invoice, imported from the accounting software. -- App rule (R4): a promise date is from today to today + 30 days, on an open invoice with a balance; a broken promise is cleared and noted. -- App rule (R5): only open invoices can be disputed; resolving reopens the invoice and clears dispute_reason (the notes keep it). -- App rule (R6): a write-off sets the balance to zero and the status to written_off; amount_cents keeps the original amount. CREATE TABLE invoices ( id TEXT PRIMARY KEY, number TEXT NOT NULL UNIQUE, -- the accounting software's invoice number, for example INV-1032 customer_id TEXT NOT NULL REFERENCES customers(id), issue_date DATE NOT NULL, due_date DATE NOT NULL, amount_cents INTEGER NOT NULL CHECK (amount_cents >= 0), balance_cents INTEGER NOT NULL CHECK (balance_cents >= 0), -- never below zero (R1), never above the amount (section 7) status TEXT NOT NULL DEFAULT 'open' CHECK (status IN ('open', 'paid', 'disputed', 'written_off')), pay_link TEXT, -- optional URL, listed by the {pay_links} merge field dispute_reason TEXT, -- required while disputed promise_date DATE, -- no reminders while this is today or later (R3) not_in_latest_import BOOLEAN NOT NULL DEFAULT FALSE, -- open invoice missing from the latest file; never marks it paid last_imported_at TIMESTAMP, CHECK (due_date >= issue_date), CHECK (balance_cents <= amount_cents), CHECK (status NOT IN ('paid', 'written_off') OR balance_cents = 0), CHECK (status <> 'disputed' OR dispute_reason IS NOT NULL) ); -- One row per payment recorded in the app. Payments are never edited or deleted. -- App rule (R1): a payment is not more than the balance, dated today or earlier, on an open or disputed invoice; one equal to the balance makes the invoice paid. CREATE TABLE payments ( id TEXT PRIMARY KEY, invoice_id TEXT NOT NULL REFERENCES invoices(id), date DATE NOT NULL, -- business date of the payment amount_cents INTEGER NOT NULL CHECK (amount_cents > 0), note TEXT, recorded_by TEXT NOT NULL REFERENCES people(id) ); -- One row per email template used by a reminder step. -- App rule (R9): subject and body may use only {customer_name}, {invoice_table}, {total_due}, {oldest_days_overdue}, {pay_links} and {from_name}, case-sensitive. CREATE TABLE templates ( id TEXT PRIMARY KEY, name TEXT NOT NULL UNIQUE, subject TEXT NOT NULL, -- one line body TEXT NOT NULL -- plain text ); -- One row per step of the reminder schedule. Steps are turned off, never deleted, because reminders_sent refers to them. -- App rule (section 5): exactly one step is the Final notice; the index below allows at most one. CREATE TABLE steps ( id TEXT PRIMARY KEY, name TEXT NOT NULL UNIQUE, offset_days INTEGER NOT NULL UNIQUE CHECK (offset_days BETWEEN -365 AND 365), -- days from the due date; negative is before it template_id TEXT NOT NULL REFERENCES templates(id), position INTEGER NOT NULL, -- display order, following offset_days active BOOLEAN NOT NULL DEFAULT TRUE, is_final BOOLEAN NOT NULL DEFAULT FALSE -- the Final notice: copies the account owner and starts the Needs a call clock ); -- The email outbox: one row per email, written before it is sent. CREATE TABLE notifications ( id TEXT PRIMARY KEY, at TIMESTAMP NOT NULL, -- when it was written sent_at TIMESTAMP, kind TEXT NOT NULL CHECK (kind IN ('reminder', 'final_notice', 'promise_broken', 'import_finished')), to_email TEXT NOT NULL, cc TEXT, -- comma-separated addresses, duplicates removed subject TEXT NOT NULL, body TEXT NOT NULL, -- plain text status TEXT NOT NULL CHECK (status IN ('queued', 'sent', 'failed', 'logged', 'held')), -- held: written while sending was off; never sent later error TEXT -- failed emails are not retried automatically ); -- One row per invoice per step sent, so no invoice gets the same step twice (R2). One email can cover several invoices. CREATE TABLE reminders_sent ( id TEXT PRIMARY KEY, invoice_id TEXT NOT NULL REFERENCES invoices(id), step_id TEXT NOT NULL REFERENCES steps(id), notification_id TEXT NOT NULL REFERENCES notifications(id), -- the customer's email that listed this invoice sent_on DATE NOT NULL, -- business date of the run; a held email counts as sent UNIQUE (invoice_id, step_id) ); -- The append-only notes timeline. A note belongs to exactly one invoice or one customer. -- App rule (R10): notes are never edited or deleted. CREATE TABLE notes ( id TEXT PRIMARY KEY, invoice_id TEXT REFERENCES invoices(id), customer_id TEXT REFERENCES customers(id), at TIMESTAMP NOT NULL, by_id TEXT REFERENCES people(id), -- empty for notes the job writes kind TEXT NOT NULL CHECK (kind IN ('note', 'call', 'promise', 'promise_broken', 'dispute', 'dispute_resolved', 'pause', 'resume', 'payment', 'write_off', 'import')), text TEXT, -- may be empty only where the spec makes the note optional, such as resuming a customer CHECK ((invoice_id IS NULL AND customer_id IS NOT NULL) OR (invoice_id IS NOT NULL AND customer_id IS NULL)) ); -- One row per CSV import. CREATE TABLE import_runs ( id TEXT PRIMARY KEY, at TIMESTAMP NOT NULL, by_id TEXT NOT NULL REFERENCES people(id), file_name TEXT NOT NULL, new_count INTEGER NOT NULL CHECK (new_count >= 0), updated_count INTEGER NOT NULL CHECK (updated_count >= 0), unchanged_count INTEGER NOT NULL CHECK (unchanged_count >= 0), error_count INTEGER NOT NULL CHECK (error_count >= 0), missing_count INTEGER NOT NULL CHECK (missing_count >= 0) -- open invoices in the app but not in the file ); -- One row per business day's reminder run, written in the same transaction as its emails, which keeps the run to once a day. CREATE TABLE daily_runs ( id TEXT PRIMARY KEY, run_date DATE NOT NULL UNIQUE, at TIMESTAMP NOT NULL, run_by TEXT REFERENCES people(id), -- the admin who pressed Run now; empty when the job ran it snapshot TEXT -- JSON as text: what was sent and skipped, with reasons ); -- The append-only audit log: one row per change. -- App rule (R10): events are never edited or deleted. CREATE TABLE events ( id TEXT PRIMARY KEY, at TIMESTAMP NOT NULL, actor TEXT REFERENCES people(id), -- empty for the job type TEXT NOT NULL, data TEXT -- JSON as text ); -- Customer names are unique ignoring case (section 5). CREATE UNIQUE INDEX customers_name_idx ON customers (lower(name)); -- At most one step is the Final notice. CREATE UNIQUE INDEX steps_one_final_idx ON steps (is_final) WHERE is_final = TRUE; -- The invoice list, the aging report and the daily run read open invoices by due date. CREATE INDEX invoices_customer_idx ON invoices (customer_id); CREATE INDEX invoices_due_idx ON invoices (status, due_date); CREATE INDEX payments_invoice_idx ON payments (invoice_id); CREATE INDEX notes_invoice_idx ON notes (invoice_id, at); CREATE INDEX notes_customer_idx ON notes (customer_id, at);