From 9a47bcd2faa82348fbe8d08a0da0073dea13b7e9 Mon Sep 17 00:00:00 2001 From: bouni Date: Fri, 20 Nov 2026 13:41:55 +0100 Subject: [PATCH] Allow a fixed width for the invoice number counter {SEQ} and {CSEQ} were always padded to 3 digits, so formats that require a fixed number of digits (e.g. Bulgaria: exactly 10 digits, 0000000001) could not be produced. {SEQ:N} and {CSEQ:N} now pad the counter to N digits; plain {SEQ}/{CSEQ} keep 3 digits. Patterns with a width also allocate the real number when the invoice is created, like plain {SEQ}. Refs #152 Co-Authored-By: Claude Opus 5.5 --- backend/src/controllers/invoices.ts | 3 +- backend/src/database/init.ts | 41 +++++++++++++++++++---- frontend/src/routes/settings/+page.svelte | 3 ++ 3 files changed, 40 insertions(+), 7 deletions(-) diff --git a/backend/src/controllers/invoices.ts b/backend/src/controllers/invoices.ts index 7d797362..7db57e7e 100644 --- a/backend/src/controllers/invoices.ts +++ b/backend/src/controllers/invoices.ts @@ -3,6 +3,7 @@ import { generateDraftInvoiceNumber, getDatabase, getNextInvoiceNumber, + patternNeedsAllocation, } from "../database/init.ts"; import { getSetting } from "./settings.ts"; import { getProductById } from "./products.ts"; @@ -318,7 +319,7 @@ export const createInvoice = ( ); if (rows.length > 0) { const pattern = String((rows[0] as unknown[])[0] || "").trim(); - if (pattern && /\{(C?SEQ|CNUM)\}/.test(pattern)) { + if (pattern && patternNeedsAllocation(pattern)) { invoiceNumber = getNextInvoiceNumber(data.customerId); } else { invoiceNumber = generateDraftInvoiceNumber(); diff --git a/backend/src/database/init.ts b/backend/src/database/init.ts index 914ffac9..b311a2b2 100644 --- a/backend/src/database/init.ts +++ b/backend/src/database/init.ts @@ -858,14 +858,43 @@ function expandPatternTokens(pattern: string): string { .replace(/\{RAND4\}/g, () => cryptoRandom(4)); } +/** + * Counter placeholders: {SEQ} / {CSEQ}, optionally with a minimum width, + * e.g. {SEQ:10} -> 0000000001 (some countries require a fixed digit count). + */ +const SEQ_TOKEN = /\{SEQ(?::(\d{1,2}))?\}/; +const CSEQ_TOKEN = /\{CSEQ(?::(\d{1,2}))?\}/; +const DEFAULT_SEQ_WIDTH = 3; +const MAX_SEQ_WIDTH = 20; + +/** True when a pattern contains a placeholder that needs a real (non-draft) number. */ +export function patternNeedsAllocation(pattern: string): boolean { + return SEQ_TOKEN.test(pattern) || CSEQ_TOKEN.test(pattern) || + /\{CNUM\}/.test(pattern); +} + +/** Replace every occurrence of a counter token, padded to its own width. */ +function fillCounter( + pattern: string, + token: RegExp, + value: number, +): string { + return pattern.replace(new RegExp(token.source, "g"), (_m, w?: string) => { + const width = w + ? Math.min(Math.max(parseInt(w, 10), 1), MAX_SEQ_WIDTH) + : DEFAULT_SEQ_WIDTH; + return String(value).padStart(width, "0"); + }); +} + export function getNextInvoiceNumber(customerId?: string): string { const cfg = getNumberingSettings(); // Advanced pattern mode (when enabled and configured) if (cfg.pattern && cfg.enabled) { const expanded = expandPatternTokens(cfg.pattern); - const hasSeq = /\{SEQ\}/.test(cfg.pattern); - const hasClientSeq = /\{CSEQ\}/.test(cfg.pattern); + const hasSeq = SEQ_TOKEN.test(cfg.pattern); + const hasClientSeq = CSEQ_TOKEN.test(cfg.pattern); const hasCustomerNum = /\{CNUM\}/.test(cfg.pattern); if (!hasSeq && !hasClientSeq && !hasCustomerNum) return expanded; @@ -885,15 +914,15 @@ export function getNextInvoiceNumber(customerId?: string): string { } if (hasSeq) { // {SEQ}: global counter, shared across all customers - const prefix = result.split("{SEQ}")[0]; + const prefix = result.split(SEQ_TOKEN)[0]; const next = findMaxSequence(prefix) + 1; - result = result.replace(/\{SEQ\}/g, String(next).padStart(3, "0")); + result = fillCounter(result, SEQ_TOKEN, next); } if (hasClientSeq) { // {CSEQ}: counter scoped to this customer's own invoices - const prefix = result.split("{CSEQ}")[0]; + const prefix = result.split(CSEQ_TOKEN)[0]; const next = findMaxSequence(prefix, customerId) + 1; - result = result.replace(/\{CSEQ\}/g, String(next).padStart(3, "0")); + result = fillCounter(result, CSEQ_TOKEN, next); } return result; } diff --git a/frontend/src/routes/settings/+page.svelte b/frontend/src/routes/settings/+page.svelte index 27c9f3b7..6b640100 100644 --- a/frontend/src/routes/settings/+page.svelte +++ b/frontend/src/routes/settings/+page.svelte @@ -659,6 +659,9 @@ {"{MM}"}, {"{DD}"}, {"{DATE}"}, {"{RAND4}"}

+

+ {"{SEQ:10}"} / {"{CSEQ:10}"} (counter padded to 10 digits, e.g. 0000000001) +