Files
accounted/supabase/migrations/20260510120100_peek_next_invoice_number.sql
T
Mattsson 0ee5219b6c feat(invoices): implement öresavrundning logic and next invoice numbe… (#429)
* feat(invoices): implement öresavrundning logic and next invoice number preview

- Added `getDisplayTotal` utility to handle rounding for SEK invoices based on company settings.
- Updated `InvoicesPage` to utilize the new rounding logic when displaying totals.
- Introduced `peek_next_invoice_number` function to allow previewing the next invoice number without consuming the sequence.
- Modified invoice number generation to remove the year prefix and prevent truncation of numbers exceeding three digits.
- Enhanced tests for invoice number generation and rounding functionality to ensure correctness.
- Updated PDF template to reflect new rounding logic for totals and display appropriate values.
- Adjusted company switcher to hide options in sandbox mode.
- Improved error handling and logging in sandbox seeding process.

* fix(skatteverket): remove unused scope labels from SCOPE_LABELS and DEFAULT_SCOPES
2026-05-10 14:21:26 +02: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';