-- Originals in Lanna Desk's care (phase 12): what clients hand over for a -- while, such as a passport for an extension, a signed contract or a title -- deed, and where each one is now. The code goes on its label; every intake -- and move is a journal entry, with the code first. CREATE SEQUENCE original_codes; CREATE TABLE originals ( id BIGSERIAL PRIMARY KEY, -- OR-2026-0001, on the label and in its QR code code TEXT NOT NULL UNIQUE CHECK (code ~ '^OR-[0-9]{4}-[0-9]{4,}$') DEFAULT ( 'OR-' || to_char(now() AT TIME ZONE 'Asia/Bangkok', 'YYYY') || '-' || lpad(nextval('original_codes')::text, 4, '0') ), -- whose it is: a client of the legal practice, or a person of the visa -- side (until phase 10 makes every person a client) client_id BIGINT REFERENCES clients (id), person_id BIGINT REFERENCES people (id), CONSTRAINT originals_one_owner CHECK (num_nonnulls(client_id, person_id) = 1), kind TEXT NOT NULL CHECK (kind IN ( 'passport', 'id-card', 'house-registration', 'title-deed', 'contract', 'certificate', 'bank-book', 'work-permit', 'other' )), -- in words: "Passport C01X00T47, Germany", "Chanote no. 12345" what TEXT NOT NULL CHECK (what <> ''), -- the contract it belongs to, if any contract_id BIGINT REFERENCES contracts (id), received_at TIMESTAMPTZ NOT NULL DEFAULT now(), -- who handed it over, and who took it in received_from TEXT NOT NULL CHECK (received_from <> ''), received_by TEXT NOT NULL, state TEXT NOT NULL DEFAULT 'held' CHECK (state IN ('held', 'out', 'returned')), -- where it lies while held: "Office, safe, tray 2" location TEXT NOT NULL DEFAULT '', -- with whom while out ("Chiang Mai Immigration"), to whom once returned holder TEXT NOT NULL DEFAULT '', -- when it should be back (out), or go back to the client (held) due_on DATE, moved_at TIMESTAMPTZ NOT NULL DEFAULT now(), CONSTRAINT originals_held_somewhere CHECK (state <> 'held' OR location <> ''), CONSTRAINT originals_out_with_someone CHECK (state = 'held' OR holder <> '') ); CREATE INDEX originals_client ON originals (client_id) WHERE client_id IS NOT NULL; CREATE INDEX originals_person ON originals (person_id) WHERE person_id IS NOT NULL; CREATE INDEX originals_open ON originals (state) WHERE state <> 'returned'; -- An original stays on record. What it is, whose it is and how it came -- never change; where it is does. CREATE FUNCTION originals_fixed() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN IF TG_OP = 'DELETE' THEN RAISE EXCEPTION 'an original stays on record: mark it returned'; END IF; IF NEW.code <> OLD.code OR NEW.client_id IS DISTINCT FROM OLD.client_id OR NEW.person_id IS DISTINCT FROM OLD.person_id OR NEW.kind <> OLD.kind OR NEW.what <> OLD.what OR NEW.contract_id IS DISTINCT FROM OLD.contract_id OR NEW.received_at <> OLD.received_at OR NEW.received_from <> OLD.received_from OR NEW.received_by <> OLD.received_by THEN RAISE EXCEPTION 'what an original is, whose it is and how it came never change'; END IF; RETURN NEW; END $$; CREATE TRIGGER originals_no_change BEFORE UPDATE OR DELETE ON originals FOR EACH ROW EXECUTE FUNCTION originals_fixed(); ALTER TABLE audit_log DROP CONSTRAINT audit_log_entity_check; ALTER TABLE audit_log ADD CONSTRAINT audit_log_entity_check CHECK ( entity IN ( 'person', 'passport', 'address', 'session', 'company', 'visa', 'arrival', 'extension', 'reentry-permit', 'report', 'work-permit', 'document', 'form', 'link', 'agreement', 'signature', 'lawyer', 'client', 'contract', 'contract-version', 'paper-copy', 'contract-party', 'original' ) );