-- Prevent deletion of paid client invoices for all roles.
create or replace function public.prevent_paid_invoice_delete()
returns trigger
language plpgsql
as $$
begin
  if old.status = 'paid' then
    raise exception 'Paid invoices cannot be deleted.';
  end if;
  return old;
end;
$$;

drop trigger if exists prevent_paid_invoice_delete on public.invoices;

create trigger prevent_paid_invoice_delete
before delete on public.invoices
for each row
execute function public.prevent_paid_invoice_delete();
