Files
accounted/supabase/migrations/20260427150000_invoice_sent_requires_number_check.sql
Mattsson fd1db89603 Fix/invoice numbers (#365)
* feat: make invoice_number nullable and assign on send

- Updated the invoices table to allow invoice_number to be nullable.
- Modified the logic to assign invoice numbers only when the invoice status transitions to 'sent'.
- Refactored related code to handle nullable invoice numbers, including UI components and API routes.
- Added tests to ensure correct behavior when handling invoices with null invoice numbers.
- Introduced a utility function to display invoice numbers, defaulting to '(Utkast)' for drafts.

* fix: update fiscal period handling to return names of open periods in error messages

* fix: enhance period creation logic to account for company-wide bookkeeping lock-through

* fix: remove unnecessary customer_type field from customer insertion query

* fix: scope invoice number count query to specific companies to avoid test interference

* feat: Implement atomic invoice number generation and ensure compliance with invoice numbering rules

- Introduced `ensureInvoiceNumber` function to assign invoice numbers atomically, handling concurrency and ensuring compliance with document types.
- Updated invoice-related components to utilize the new `invoiceNumberDisplay` utility for consistent invoice number formatting.
- Added checks to ensure that invoices in non-draft statuses have valid invoice numbers, preventing violations of legal requirements.
- Created tests for the new invoice number generation logic, ensuring correct behavior under various scenarios, including concurrent requests.
- Added a draft banner to PDF templates for invoices without assigned numbers, clarifying their status to users.
- Updated database migrations to support the new atomic invoice number generation logic and enforce constraints on invoice statuses.
2026-04-27 16:29:58 +02:00

18 lines
939 B
SQL

-- Belt-and-suspenders for the nullable invoice_number column.
-- Invoices in any status that implies they have left the draft stage must carry
-- a number. ensureInvoiceNumber covers known send paths in application code,
-- but a future caller could transition status without going through that helper
-- and silently produce a sent invoice with no löpnummer (ML 17 kap 24§ violation).
--
-- 'draft' and 'cancelled' are the only statuses where invoice_number may legally
-- be NULL — drafts have not been numbered yet, and cancelled-from-draft never
-- needed one. Cancelled-after-send retains its existing number, so the rule
-- still holds. Status enum from invoices_status_check:
-- draft, sent, paid, partially_paid, overdue, cancelled, credited
ALTER TABLE public.invoices
ADD CONSTRAINT invoices_sent_requires_number
CHECK (status IN ('draft', 'cancelled') OR invoice_number IS NOT NULL);
NOTIFY pgrst, 'reload schema';