Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion backend/src/controllers/invoices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
generateDraftInvoiceNumber,
getDatabase,
getNextInvoiceNumber,
patternNeedsAllocation,
} from "../database/init.ts";
import { getSetting } from "./settings.ts";
import { getProductById } from "./products.ts";
Expand Down Expand Up @@ -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();
Expand Down
41 changes: 35 additions & 6 deletions backend/src/database/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
}
Expand Down
3 changes: 3 additions & 0 deletions frontend/src/routes/settings/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,9 @@
<code>{"{MM}"}</code>, <code>{"{DD}"}</code>,
<code>{"{DATE}"}</code>, <code>{"{RAND4}"}</code>
</p>
<p>
<code>{"{SEQ:10}"}</code> / <code>{"{CSEQ:10}"}</code> (counter padded to 10 digits, e.g. 0000000001)
</p>
</div>
</label>
</div>
Expand Down
Loading