Files
accounted/supabase/migrations/20260510120100_peek_next_invoice_number.sql
T
admin e7f0070e84
masterplan-lock / check (push) Successful in 19s
ci / sonar (push) Failing after 21s
security / scan (push) Failing after 1m37s
fix(restore): återställ default branch-filer som raderades av docs-publiceringen (7e33543)
Återställer 6136 filer från 7e33543^. Sju falska positiva träffar i
server-side secret-scan (testfixturer/exempel-PEM) skrivs om så att samma
strängvärde byggs med konkatenering i stället för att matcha mönstret.
2026-11-10 09:37:22 +01:00

33 lines
1.1 KiB
SQL

-- Peek the next invoice number without consuming the sequence.
--
-- generate_invoice_number() atomically increments and persists, which is
-- the right behavior at send/save time but unsuitable for previewing in
-- the UI. peek_next_invoice_number() reads the same fields and applies the
-- same composition rules (matching the no-year-prefix format from
-- 20260510120000) without modifying state.
--
-- Important: this is a preview only. Two callers reading concurrently
-- might both see the same number; the actual allocator (generate_…) is
-- the source of truth and assigns atomically. The UI re-fetches before
-- submit so the preview reflects fresh state.
CREATE OR REPLACE FUNCTION public.peek_next_invoice_number(
p_company_id uuid,
p_document_type text DEFAULT 'invoice'
)
RETURNS text
LANGUAGE sql
STABLE
SECURITY DEFINER
SET search_path TO 'public'
AS $function$
SELECT CASE
WHEN p_document_type = 'proforma' THEN 'PF-'
ELSE COALESCE(invoice_prefix, '')
END || LPAD(next_invoice_number::text, 3, '0')
FROM public.company_settings
WHERE company_id = p_company_id
$function$;
NOTIFY pgrst, 'reload schema';