Files
accounted/lib/articles/ensure-article-number.ts
T
Jakob Wennberg c0b006fcc1 feat(invoicing): artikelregister (product/article catalog) with per-article revenue account (#703)
* feat(invoicing): artikelregister (product/article catalog) with per-article revenue account

Add a lean, non-inventory article catalog (artikelregister) so users can define
reusable invoice-line presets (name, unit, price excl VAT, VAT rate) with an
optional per-article BAS class-3 revenue-account override.

- DB: articles table (RLS via user_company_ids(), audit + updated_at triggers,
  unique-per-company article_number), generate_article_number RPC (atomic +
  idempotent), company_settings counter, nullable invoice_items.revenue_account
  + article_id, pending_operations CHECK expansion.
- Engine: generatePerRateLines groups revenue by (vat_rate, account) —
  byte-identical with no override, balance-safe when split (last account absorbs
  the rounding remainder), reverse_charge/export still force 3308/3305.
- API: /api/articles CRUD (soft-deactivate); override validated against
  chart_of_accounts (active class-3) and frozen onto invoice lines at create.
- Propagation: override carried through send/mark-sent/credit/convert/cash and
  the staged commit paths (recurring deferred — documented inline).
- MCP: gnubok_list/create/update_article (staged, scoped, risk-tiered).
- UI: articles register (list/detail/form) + nav + bilingual i18n + invoice-line
  article picker & "Spara som artikel" quick-create.
- Tests: engine regression, route, and pg-real (RPC/RLS/triggers).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* fix(mcp): strip ILIKE _ wildcard from gnubok_list_articles search

Underscore is a single-character ILIKE wildcard; stripping it (alongside the
existing %,()\* set) keeps a stray char in the article search from matching
every row. Read-only + RLS-scoped, so no security impact — addresses PR #703
reviewer + compliance-swarm CC6.3 notes.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-09 21:05:37 +02:00

31 lines
1.1 KiB
TypeScript

import type { SupabaseClient } from '@supabase/supabase-js'
/**
* Assign an article number to an article row via the generate_article_number
* RPC. Idempotent: if the row already has a number, the RPC returns it unchanged
* without consuming a sequence number. Concurrency is handled inside the RPC via
* a row lock on the article plus an atomic counter on company_settings — two
* callers racing on the same article both return the same number and the counter
* advances by exactly one.
*
* Mirrors lib/invoices/ensure-invoice-number.ts. Unlike invoice numbers, article
* numbers are master data and carry no BFL sequence/immutability obligation, so
* a gap is harmless.
*/
export async function ensureArticleNumber(
supabase: SupabaseClient,
companyId: string,
articleId: string,
): Promise<string> {
const { data, error } = await supabase.rpc('generate_article_number', {
p_company_id: companyId,
p_article_id: articleId,
})
if (error || !data) {
throw new Error(`Failed to assign article number: ${error?.message ?? 'no value returned'}`)
}
return data as string
}