-- One-time charges that are included once, on the first generated invoice. CREATE TABLE IF NOT EXISTS sag_subscription_first_invoice_items ( id BIGSERIAL PRIMARY KEY, subscription_id BIGINT NOT NULL REFERENCES sag_subscriptions(id) ON DELETE CASCADE, line_no INTEGER NOT NULL, product_id BIGINT REFERENCES products(id) ON DELETE SET NULL, description TEXT NOT NULL, quantity NUMERIC(14,4) NOT NULL DEFAULT 1 CHECK (quantity > 0), unit_price NUMERIC(14,2) NOT NULL DEFAULT 0 CHECK (unit_price >= 0), line_total NUMERIC(14,2) NOT NULL DEFAULT 0, billed_at TIMESTAMPTZ, billing_run_id BIGINT REFERENCES subscription_billing_runs(id) ON DELETE SET NULL, created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, UNIQUE (subscription_id, line_no) ); CREATE INDEX IF NOT EXISTS idx_subscription_first_invoice_unbilled ON sag_subscription_first_invoice_items(subscription_id) WHERE billed_at IS NULL;