CREATE SEQUENCE IF NOT EXISTS public.invoice_no_seq START 1;

CREATE OR REPLACE FUNCTION public.next_invoice_no()
RETURNS text
LANGUAGE sql
SECURITY DEFINER
SET search_path = public
AS $$
  SELECT LPAD(nextval('public.invoice_no_seq')::text, 8, '0');
$$;

GRANT EXECUTE ON FUNCTION public.next_invoice_no() TO anon, authenticated;