Accounted rebrand + swarm-skill cleanup + bank-reconciliation fixes (#643)

* feat(reconciliation): close the bank-feed loop on voucher links and re-tag mis-typed opening balances

Two related fixes to bank reconciliation correctness:

1. Auto-reconcile on voucher link. Linking an invoice or supplier invoice to
   an existing voucher previously advanced only the invoice — the bank
   transaction that paid it kept sitting in the Transactions inbox with a null
   journal_entry_id. linkInvoiceToVoucher / linkSupplierInvoiceToVoucher now
   call autoReconcileTransactionForLinkedVoucher (lib/reconciliation), which
   links the bank transaction to the same verifikat when exactly one unbooked
   line matches it. Best-effort and post-commit: a failure here never fails the
   link. The result surfaces reconciledTransactionId; the inbox row leaves the
   list and the UI shows link_success_tx_reconciled.

2. Re-tag mis-typed opening balances. getReconciliationStatus and the GL-line
   matching RPCs identify a cash account's ingående balans solely by
   journal_entries.source_type='opening_balance'. Companies migrated from other
   systems often booked the bank IB as an ordinary voucher (source_type
   'import' or 'manual'), so it was never excluded and surfaced as a phantom
   reconciliation difference equal to the opening balance. Adds:
   - migration mark_entry_as_opening_balance: a GUC-gated carve-out in the
     immutability trigger plus a SECURITY DEFINER RPC that validates the entry
     (balance-sheet lines only, dated on a fiscal-period boundary), flips the
     source_type, and writes an audit row — no blanket data sweep.
   - POST /api/reconciliation/bank/mark-opening-balance + MarkOpeningBalanceSchema.
   - BankReconciliationView action to trigger it from the IB diff.

The gnubok_create_voucher executor now accepts a typed is_opening_balance flag
and derives source_type='opening_balance' only after validating class 1/2 lines
on the period start, so new IBs land correctly typed.

Covered by lib/reconciliation auto-reconcile tests, voucher-executors tests,
and a mark-entry-as-opening-balance pg-real test.

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

* chore: rebrand gnubok → Accounted and prune swarm agent skills

Product rebrand and skills housekeeping. No runtime behaviour change.

Rebrand: replace user-visible "gnubok" with "Accounted" across docs, READMEs,
in-code comments, doc-site content, MCP skill/resource prose, and the
gnubok-mcp package description. The MCP resource URI scheme is moved gnubok://
→ Accounted:// consistently across resource registrations, the event-type
comment, and the resource/skill tests. Deliberately preserved as stable
identifiers (NOT rebranded): the gnubok-company-id cookie, gnubok_sk_ / gnubok_inv_
token prefixes, the gnubok-mcp npm bridge name, and the AGI <gem:Programnamn>
value (kept 'gnubok' per its source comment — it is the software identifier sent
to Skatteverket and must not churn across visual rebrands).

Skills: remove the 27 swarm-* agent SKILL.md atoms (no longer used; already
absent from the agent_atom_registry in prod), refresh the remaining skill docs,
add the .claude/rules/ path-scoped rule set, and regenerate the
seed_agent_atom_bodies migration + .skill-body-manifest.json via
`npm run skills:generate` so the DB-backed skill bodies match the trimmed set.

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

---------

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Jakob Wennberg
2026-06-03 10:52:01 +02:00
committed by GitHub
co-authored by Claude Opus 4.8
parent 331ae11867
commit c74b19df1b
183 changed files with 19621 additions and 4175 deletions
+1 -1
View File
@@ -1024,7 +1024,7 @@ describe('importVouchers — per-voucher series preservation', () => {
it('preserves original source series/number on each imported entry, even across skipped vouchers', async () => {
const { supabase, journalEntryInserts } = buildCapturingSupabase()
// A2 is an empty voucher (no lines) — will be skipped. A1 and A3 survive.
// Gnubok assigns target numbers 1 and 2 (contiguous), but source_voucher_number
// Accounted assigns target numbers 1 and 2 (contiguous), but source_voucher_number
// must preserve the SIE originals (1 and 3) so traceability is not lost.
const parsed = makeParsedFile({
vouchers: [
+31 -4
View File
@@ -355,7 +355,7 @@ export async function ensureFiscalPeriod(
if (!replaceableGateOpen || hasEntries) {
throw new Error(
`SIE-filens räkenskapsår (${startDate} – ${endDate}) överlappar men matchar inte ett befintligt räkenskapsår i gnubok ` +
`SIE-filens räkenskapsår (${startDate} – ${endDate}) överlappar men matchar inte ett befintligt räkenskapsår i Accounted ` +
`(${existing.name}: ${existing.period_start} – ${existing.period_end}). ` +
`Justera räkenskapsåret i Inställningar → Företag så att det matchar SIE-filen exakt, eller importera en SIE-fil som täcker exakt samma period.`
)
@@ -902,11 +902,28 @@ export async function importVouchers(
// traceability alongside the aggregate sie_imports.migration_documentation.
sourceSeries: string | null
sourceNumber: number | null
// 'import' for ordinary migrated vouchers; 'opening_balance' for a #VER that
// is really the year's ingående balans (see isLikelyOpeningBalance below).
sourceType: 'import' | 'opening_balance'
lines: { account_number: string; debit_amount: number; credit_amount: number; line_description: string | null }[]
}
const preparedVouchers: PreparedVoucher[] = []
// A SIE file represents the opening balance either as #IB records (handled
// separately by createOpeningBalanceEntry → source_type='opening_balance') or,
// in some source systems, as an ordinary #VER dated on the fiscal-year start.
// When there are NO current-year #IB records, detect a clearly-labelled IB
// voucher and tag it opening_balance so bank reconciliation excludes it from
// the period movement (otherwise it lands as 'import' and surfaces as a phantom
// difference equal to the IB). Deliberately conservative — requires the IB
// wording AND a balance-sheet-only voucher on FY start, and never a
// share-capital deposit. A missed IB still falls back to the manual "Märk som
// ingående balans" action in Bankavstämning, so we never risk hiding a real
// bank movement by over-classifying.
const hasCurrentYearIb = parsed.openingBalances.some((b) => b.yearIndex === 0)
const fyStart = parsed.stats.fiscalYearStart
for (const voucher of parsed.vouchers) {
const lines: PreparedVoucher['lines'] = []
let hasUnmappedAccount = false
@@ -1038,13 +1055,23 @@ export async function importVouchers(
const rawSourceSeries = voucher.series && voucher.series.trim() ? voucher.series.trim() : null
const rawSourceNumber = Number.isFinite(voucher.number) ? voucher.number : null
const voucherDateStr = formatDate(voucher.date)
const isLikelyOpeningBalance =
!hasCurrentYearIb &&
!!fyStart && fyStart.slice(0, 10) === voucherDateStr &&
lines.length > 0 &&
lines.every((l) => isBalanceSheetAccount(l.account_number)) &&
/ing[åa]ende balans|ing[åa]ende saldo|opening balance/i.test(voucher.description || '') &&
!/aktiekapital/i.test(voucher.description || '')
preparedVouchers.push({
sourceId: voucherId,
series: resolvedSeries,
date: formatDate(voucher.date),
date: voucherDateStr,
description: voucher.description || `Import: ${voucher.series}${voucher.number}`,
sourceSeries: rawSourceSeries,
sourceNumber: rawSourceNumber,
sourceType: isLikelyOpeningBalance ? 'opening_balance' : 'import',
lines,
})
}
@@ -1141,7 +1168,7 @@ export async function importVouchers(
voucher_series: series,
entry_date: v.date,
description: v.description,
source_type: 'import',
source_type: v.sourceType,
source_voucher_series: v.sourceSeries,
source_voucher_number: v.sourceNumber,
status: 'posted',
@@ -1778,7 +1805,7 @@ export async function loadMappings(supabase: SupabaseClient, companyId: string):
* updated data from Fortnox without manual cleanup.
*
* Replace only cancels journal entries with source_type='import' — entries
* the user created natively in gnubok (categorized transactions, invoices,
* the user created natively in Accounted (categorized transactions, invoices,
* etc.) are left alone. See the replace_sie_import RPC.
*/
export async function executeSIEImport(