-- Inventory Tracker database schema -- Source: https://personalsoftwarefactory.com/blueprints/inventory-tracker (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 place where stock is kept: a store room, a van, a shelf, a site. -- App rule (R8): a location is deactivated only when empty and when it is no active person's default location. CREATE TABLE locations ( id TEXT PRIMARY KEY, name TEXT NOT NULL UNIQUE, code TEXT NOT NULL UNIQUE, -- used in location labels (/l/); up to 12 letters, digits or hyphens, stored in capitals active BOOLEAN NOT NULL DEFAULT TRUE, CHECK (code = upper(code) AND length(code) BETWEEN 1 AND 12) ); -- A person who can sign in. Never deleted, only deactivated. -- App rule (section 4): the email must be at the company domain; admins cannot demote or deactivate themselves; at least one active admin. CREATE TABLE people ( id TEXT PRIMARY KEY, name TEXT NOT NULL, email TEXT NOT NULL UNIQUE, -- stored lowercase role TEXT NOT NULL CHECK (role IN ('admin', 'staff')), default_location_id TEXT REFERENCES locations(id), -- where Use starts on a phone; NULL for admins in the seed active BOOLEAN NOT NULL DEFAULT TRUE, CHECK (email = lower(email)) ); -- A supplier that purchase orders are emailed to. CREATE TABLE suppliers ( id TEXT PRIMARY KEY, name TEXT NOT NULL, email TEXT NOT NULL, -- purchase orders are emailed here lead_time_days INTEGER NOT NULL CHECK (lead_time_days BETWEEN 0 AND 365) -- sets the default expected date on Send ); -- A stocked item. Archived, never deleted. -- App rule (R8): archive only with nothing on hand anywhere and nothing on sent or partially received orders. -- App rule (R10): unit cannot change once the item has any movement. CREATE TABLE items ( id TEXT PRIMARY KEY, sku TEXT NOT NULL UNIQUE, -- letters, digits, dots, underscores, hyphens; stored in capitals so uniqueness ignores case name TEXT NOT NULL, unit TEXT NOT NULL, -- one unit per item: bottle, box, case, each barcode TEXT UNIQUE, -- optional, no spaces photo TEXT, -- optional; a file path or URL unit_cost_cents INTEGER NOT NULL CHECK (unit_cost_cents >= 0), -- latest cost; set by each purchase order receipt (R6) reorder_point INTEGER NOT NULL DEFAULT 0 CHECK (reorder_point >= 0), -- low when total on hand is at or below this (R4) reorder_qty INTEGER NOT NULL DEFAULT 0 CHECK (reorder_qty >= 0), -- 0 means never suggested supplier_id TEXT NOT NULL REFERENCES suppliers(id), low_alerted BOOLEAN NOT NULL DEFAULT FALSE, -- TRUE after a low stock email, until the total rises above reorder_point archived BOOLEAN NOT NULL DEFAULT FALSE, CHECK (sku = upper(sku)), CHECK (barcode IS NULL OR barcode NOT LIKE '% %') ); -- A purchase order to one supplier. Numbers run PO-0001, PO-0002, one above the highest; cancelled orders keep theirs. -- App rule (section 7): status moves draft > sent > partially_received > received, or to cancelled from draft, or from sent with nothing received. CREATE TABLE purchase_orders ( id TEXT PRIMARY KEY, number TEXT NOT NULL UNIQUE, -- PO-0001 supplier_id TEXT NOT NULL REFERENCES suppliers(id), -- cannot change after creation status TEXT NOT NULL DEFAULT 'draft' CHECK (status IN ('draft', 'sent', 'partially_received', 'received', 'cancelled')), expected_date DATE, -- set on Send: today plus lead time by default, never in the past, fixed once sent sent_at TIMESTAMP, closed_short BOOLEAN NOT NULL DEFAULT FALSE, -- TRUE when an admin closed it with quantities outstanding notes TEXT, created_by TEXT NOT NULL REFERENCES people(id), CHECK (closed_short = FALSE OR status = 'received') ); -- One item on a purchase order. One line per item per order. CREATE TABLE po_lines ( id TEXT PRIMARY KEY, po_id TEXT NOT NULL REFERENCES purchase_orders(id), item_id TEXT NOT NULL REFERENCES items(id), qty_ordered INTEGER NOT NULL CHECK (qty_ordered > 0), qty_received INTEGER NOT NULL DEFAULT 0, -- never more than ordered: receiving more than is outstanding is refused unit_cost_cents INTEGER NOT NULL CHECK (unit_cost_cents >= 0), UNIQUE (po_id, item_id), CHECK (qty_received BETWEEN 0 AND qty_ordered) ); -- A stock count of one location. Numbers run C-0001, C-0002. -- App rule (R7): counts are blind; expected quantities are hidden while open and never shown to staff. -- App rule (R7): approval is refused at an inactive location or when an archived item's count differs. CREATE TABLE counts ( id TEXT PRIMARY KEY, number TEXT NOT NULL UNIQUE, -- C-0001 location_id TEXT NOT NULL REFERENCES locations(id), status TEXT NOT NULL DEFAULT 'open' CHECK (status IN ('open', 'submitted', 'approved', 'cancelled')), started_at TIMESTAMP NOT NULL, started_by TEXT NOT NULL REFERENCES people(id), submitted_by TEXT REFERENCES people(id), approved_by TEXT REFERENCES people(id) -- must be an admin (app rule) ); -- At most one open or submitted count per location (section 13). CREATE UNIQUE INDEX counts_one_active_per_location ON counts (location_id) WHERE status IN ('open', 'submitted'); -- One item on a count. -- App rule (R7): a flagged line must be recounted or confirmed by the approver before approval. CREATE TABLE count_lines ( count_id TEXT NOT NULL REFERENCES counts(id), item_id TEXT NOT NULL REFERENCES items(id), expected INTEGER NOT NULL, -- on hand at this location when the count started counted INTEGER CHECK (counted >= 0), -- NULL until entered; blank is not 0 changed_during_count BOOLEAN NOT NULL DEFAULT FALSE, -- set when the item moves at this location after the count started; stays set PRIMARY KEY (count_id, item_id) ); -- One change to the quantity of one item at one location. Append-only: never edited or deleted (R2). -- On hand at a location is the sum of its movements. App rule (R1): no movement may take a location below zero. CREATE TABLE movements ( id TEXT PRIMARY KEY, at TIMESTAMP NOT NULL, -- count movements are dated at approval item_id TEXT NOT NULL REFERENCES items(id), location_id TEXT NOT NULL REFERENCES locations(id), quantity INTEGER NOT NULL, -- positive in, negative out, in the item's unit type TEXT NOT NULL CHECK (type IN ('opening', 'receive', 'use', 'move', 'adjust', 'count')), reason TEXT CHECK (reason IN ('Damaged', 'Lost', 'Found', 'Correction', 'Opening balance')), -- required for adjust po_line_id TEXT REFERENCES po_lines(id), -- set on receive count_id TEXT REFERENCES counts(id), -- set on count transfer_id TEXT, -- joins the two sides of a move person_id TEXT NOT NULL REFERENCES people(id), -- for count movements, the approver CHECK (quantity <> 0 OR type = 'opening'), -- only opening balances may be 0 CHECK (type <> 'opening' OR quantity >= 0), CHECK (type <> 'use' OR quantity < 0), CHECK (type <> 'receive' OR (quantity > 0 AND po_line_id IS NOT NULL)), CHECK (type <> 'move' OR transfer_id IS NOT NULL), CHECK (type <> 'count' OR count_id IS NOT NULL), CHECK (type <> 'adjust' OR reason IS NOT NULL), CHECK (reason IS NULL OR reason NOT IN ('Damaged', 'Lost') OR quantity < 0), -- Damaged and Lost only remove CHECK (reason IS NULL OR reason <> 'Found' OR quantity > 0) -- Found only adds ); CREATE INDEX movements_item_location ON movements (item_id, location_id); -- Audit log entry. Append-only (R2). Unit cost changes (date, old and new cost, source) are read from here. CREATE TABLE events ( id TEXT PRIMARY KEY, at TIMESTAMP NOT NULL, actor TEXT REFERENCES people(id), -- NULL when the daily job acted type TEXT NOT NULL, data TEXT -- details as JSON text ); -- An email in the outbox. Written here first, then sent. CREATE TABLE notifications ( id TEXT PRIMARY KEY, kind TEXT NOT NULL, -- which email it is: one of the triggers in section 10 to_email TEXT NOT NULL, subject TEXT NOT NULL, body TEXT NOT NULL, status TEXT NOT NULL DEFAULT 'queued' CHECK (status IN ('queued', 'sent', 'failed', 'logged')), -- logged when no provider is configured error TEXT, written_at TIMESTAMP NOT NULL -- when the email was written; shown on the Outbox );