* 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.
40 lines
1.3 KiB
TypeScript
40 lines
1.3 KiB
TypeScript
import type { SupabaseClient } from '@supabase/supabase-js'
|
|
import type { Invoice, InvoiceDocumentType } from '@/types'
|
|
|
|
type InvoiceShape = Pick<Invoice, 'id' | 'invoice_number'> & {
|
|
invoice_number: string | null
|
|
document_type?: InvoiceDocumentType | null
|
|
}
|
|
|
|
/**
|
|
* Assign an invoice number to a draft invoice. Idempotent: if the row already
|
|
* has a number, returns it unchanged without consuming a sequence number.
|
|
*
|
|
* Concurrency is handled inside the generate_invoice_number RPC via row lock.
|
|
* Two callers racing on the same draft both return the same final number; the
|
|
* counter advances by exactly one. Proforma document_type produces a 'PF-'
|
|
* prefix; everything else uses the company's configured invoice_prefix.
|
|
*/
|
|
export async function ensureInvoiceNumber(
|
|
supabase: SupabaseClient,
|
|
companyId: string,
|
|
invoice: InvoiceShape,
|
|
): Promise<string> {
|
|
if (invoice.invoice_number) {
|
|
return invoice.invoice_number
|
|
}
|
|
|
|
const { data: assigned, error: rpcError } = await supabase.rpc('generate_invoice_number', {
|
|
p_company_id: companyId,
|
|
p_invoice_id: invoice.id,
|
|
p_document_type: invoice.document_type ?? 'invoice',
|
|
})
|
|
|
|
if (rpcError || !assigned) {
|
|
throw new Error(`Failed to assign invoice number: ${rpcError?.message ?? 'no value returned'}`)
|
|
}
|
|
|
|
invoice.invoice_number = assigned
|
|
return assigned
|
|
}
|