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.
This commit is contained in:
Mattsson
2026-04-27 16:29:58 +02:00
committed by GitHub
parent ec5fd78e3e
commit fd1db89603
33 changed files with 931 additions and 131 deletions
@@ -217,20 +217,20 @@ describe('POST /api/pending-operations/:id/commit', () => {
enqueueMany([
{ data: pendingOp }, // fetch pending op
{ data: customer }, // fetch customer
{ data: '20260001' }, // generate invoice number (rpc)
{ data: { id: 'inv-1' } }, // insert invoice
{ data: { id: 'inv-1', invoice_number: null } }, // insert invoice (no number — assigned at send)
{ data: null, error: null }, // insert items
{ data: { id: 'inv-1', customer: customer, items: [] } }, // fetch complete invoice
{ data: { id: 'inv-1', invoice_number: null, customer: customer, items: [] } }, // fetch complete invoice
{ data: null, error: null }, // update pending op status
])
const request = createMockRequest('/api/pending-operations/op-1/commit', { method: 'POST' })
const response = await POST(request, routeParams)
const { status, body } = await parseJsonResponse<{ data: { invoice_id: string; invoice_number: string } }>(response)
const { status, body } = await parseJsonResponse<{ data: { invoice_id: string; invoice_number: string | null } }>(response)
expect(status).toBe(200)
expect(body.data.invoice_id).toBe('inv-1')
expect(body.data.invoice_number).toBe('20260001')
// Drafts no longer reserve a number — assigned at send time instead
expect(body.data.invoice_number).toBeNull()
})
it('returns 404 when customer not found', async () => {