-- Soft-delete support for qbo_invoices.
--
-- QBO's Invoice query endpoint only returns rows that still exist in QBO.
-- When an invoice is voided or deleted in QBO, it simply stops appearing in
-- responses — there is no "deleted" flag we can subscribe to.  Without a
-- tombstone pass these orphan rows linger in Pulse forever and keep
-- inflating Total A/R (see the TNT Pizza incident, 2026-05-15).
--
-- A full QBO sync now compares the set of returned invoice IDs against
-- what Pulse has on file and flips is_deleted = true for any row the
-- query no longer returns.  Finance queries filter on is_deleted = false.

ALTER TABLE qbo_invoices
  ADD COLUMN IF NOT EXISTS is_deleted BOOLEAN NOT NULL DEFAULT false,
  ADD COLUMN IF NOT EXISTS deleted_at TIMESTAMPTZ;

CREATE INDEX IF NOT EXISTS idx_qbo_invoices_is_deleted ON qbo_invoices(is_deleted);
