feat(reconciliation): agent surfaces, skattekonto notice, bank icons and fair sync order (#1836)
* feat(reconciliation): skattekonto bridge engine, sync-time twin proposals, account-keyed facade The engine half of the reconciliation page (design: Avstämningsmotorn). - lib/reconciliation/skattekonto-reconciliation.ts: getSkattekontoReconciliationStatus anchors at the saldo snapshot and returns the bridge (saldo hos Skatteverket, händelser som saknas, 1630-rader utan händelse, ignorerade, ingående skillnad, bokfört), the item buckets the page shows (proposed, unmatched external, unmatched ledger, matched, ignored, upcoming), opening_difference, unexplained_difference (0,00 by construction when data is consistent), dead-link handling (a link to a reversed/draft entry counts as unlinked and is flagged), awaiting_external for ledger lines within 5 days of the snapshot, staleness, and a window that scopes item lists without hiding older rows. Core reads skattekonto_transactions and the extension's snapshot row directly; no @/extensions import. - lib/reconciliation/gl-balance.ts: one ledger-balance helper with the trial-balance predicate status IN (posted, reversed). The drift check summed posted only, which misstated 1630 for any company with a storno on the account; skattekonto-drift.ts now delegates to the helper. - Proposals at sync: migration 20260823120000 adds suggested_journal_entry_id / suggested_at (ON DELETE SET NULL, partial index on open rows); the sync calls refreshSkattekontoProposals after the upsert. findMatchSuggestionsBulk now assigns one-to-one across rows (AGI period first, then nearest date) and falls back to an entry whose 1630 lines net to the amount (split lines); a proposal is never a link. - lib/reconciliation/service.ts + schemas.ts: the account-keyed facade (bank:<cash_account_id> | skattekonto | manual:NNNN) with listReconciliationAccounts (enabled cash accounts folded per IBAN, skattekonto when configured) and getAccountStatus dispatching to the bank engine or the new one; shared Zod shapes for the v1 registry, MCP schemas and the UI (PR 2). Tests: identity on a mixed fixture, storno pair, stale snapshot, awaiting window, window scoping, failed ledger read, live-linked entries never proposed; matcher one-to-one and split-line cases; proposal refresh writes/clears; service dedupe and dispatch. No UI in this PR. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(reconciliation): roundOre instead of inline öre rounding (guard ratchet) The antipattern ratchet counts Math.round(x*100)/100; the new engine used it in five places. Switch to roundOre from @/lib/money and ratchet the baseline down by the three occurrences this removes net of the matcher rewrite. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat(reconciliation): three doors over one engine: dashboard routes, v1 API and MCP tools for account-keyed reconciliation PR 2 of the Avstämning build (design: Avstämning via API och MCP). Every door calls lib/reconciliation/{service,items,actions}.ts; none re-implements a link. - lib/reconciliation/items.ts: listAccountItems per account_key, the page's buckets (proposed, unmatched_external, unmatched_ledger, matched, ignored, upcoming), limit/offset; skattekonto from the engine, bank from the scoped transactions + unlinked GL lines (netted per entry). - lib/reconciliation/actions.ts: matchPairs (pairs or use_proposals, dry run, partial success with codes), unmatchLink, setItemIgnored; emits reconciliation.matched / reconciliation.unmatched. - lib/skatteverket/skattekonto-link.ts: canonical core link semantics for a skattekonto row (single line or entry net on 1630, live-link guard, race-safe update, unlink, ignore); the extension keeps its own matchSkattekontoToEntry until its tests are ported. - Dashboard routes /api/reconciliation/accounts[...]: list, status, items, links (POST), links/{linkId} (DELETE), items/{itemId}/ignore (POST); apply directly (a human clicked). - v1 routes /api/v1/companies/{id}/reconciliation/accounts[...]: same six, withApiV1, new scopes reconciliation:read / reconciliation:write (write is a staging scope for SoD), Idempotency-Key + dry_run on writes, registered for OpenAPI, load-routes, skills/accounted-api regenerated. Legacy bank routes and their transactions:* scopes unchanged. - MCP: gnubok_get_reconciliation_status takes account_key (legacy bank path untouched), new gnubok_list_reconciliation_items (default catalog), gnubok_reconcile_match (stages reconciliation_match, preflight = status) and gnubok_reconcile_unmatch (stages reconciliation_unmatch), both search-only to stay under the tools/list payload ceiling; gnubok_link_transaction_to_journal_entry moved to search. Executors in commit.ts; risk tiers medium/low; migration pair 20260823130000/130001 adds the two op types to the CHECK constraint (value list = live prod as of 2026-08-23 + the two); close_period loadout updated. Tests: service/actions/items/link unit tests, v1 route tests (401/403/400/404/ happy, idempotency, dry run), dashboard route tests, MCP tool tests + the guard suite (payload ceiling, descriptions, staging meta, qualified ids). Guards and apiskill:check green; no type errors in changed files. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(reconciliation): refresh the v1 spec snapshot and keep the ignore update readable by the phantom-column guard The six new v1 reconciliation endpoints and the two new scopes were not recorded in the spec snapshot, and setSkattekontoRowIgnored updated through one conditional payload, which the phantom-column scanner cannot read (ceiling 380 -> 381). Two literal payloads instead; snapshot updated. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat(reconciliation): the Avstämning page, one body for every account with an outside truth /reconciliation in Arbeta (after Transaktioner), on the approved layout: an account rail on the left (bank accounts and the skattekonto, logo or monogram, last fetch, status dot, URL-owned selection), and for the selected account four tiles (outside, ledger, difference, unexplained), the bridge that explains the difference, an actions row (link the proposed pairs, book the unbooked skattekonto events, run the bank matcher) and a full-width table banded by bucket with proposal rows linkable one by one. Every read and write goes through the PR 2 dashboard routes, so the page shows exactly what the v1 API and the MCP tools see. Also: nav item, command palette entry, sv/en strings. Period picker, manual match mode and sign-off are deliberately not here (PR 4/5). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat(reconciliation): sign-off, period picker, Hem row and the three doors for it "Markera som avstämd t.o.m. <datum>" as an append-only attestation: account_reconciliations (who signed which account through which date, with the numbers as they stood; reopen stamps instead of deletes; RLS members write as themselves, viewers read). Policy in one place (lib/reconciliation/signoff.ts): refused with an unexplained difference unless forced with a note, refused past today or past the skattekonto snapshot, refused at or before an active sign-off; reopen is the undo. Every status read now carries the latest active sign-off and the rail shows "avstämt t.o.m.". Three doors: dashboard routes (GET/POST .../signoff, POST .../reopen), v1 (same, scope reconciliation:signoff, Idempotency-Key, dry-run, registry + regenerated API skill), MCP gnubok_reconcile_signoff (search catalog, stages reconciliation_signoff after a policy dry run; executor + risk tier + op-type CHECK migration pair). Events reconciliation.signed_off / reconciliation.reopened, and the four reconciliation events join the public webhook set (additive; API version unchanged, changelog section added). Page: räkenskapsår + range picker in the header (own preset memory, opens on this month) scoping the bridge, the items and the default sign-off date; sign-off dialog with the forced-with-note path; reopen on hover. Hem: worklist category reconciliation_due ("Konton att stämma av"), zero until the company has signed anything off. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(reconciliation): classify reconciliation:signoff as a tenant write for the MCP role guard gnubok_reconcile_signoff carries the deliberately separate reconciliation:signoff scope; the central viewer guard keys on the :write/:approve/:manage suffixes, so a viewer could reach the tool (RLS would still refuse the row, but the guard is the intended layer). Add :signoff to the classifier; the strictness test that caught it now passes. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(providers): serve local rate-limiter waiters in arrival order Two callers that both found the in-memory bucket empty each set their own timeout; the timeouts expired at the same instant from different timer lists and which woke first was platform-dependent. hydrateInvoices relies on "started first, requested first" to serve open invoices before paid ones, so lib/providers/__tests__/hydrate-invoices.test.ts flipped on CI (twice on #1817) while holding locally. A promise queue makes the local waiters FIFO without changing the rate; the Upstash path is untouched. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> (cherry picked from commit 14a7599bf2c6fa7f97de6ffab3dc4cf4d0e1827d) * feat(reconciliation): agent surfaces: summary resource, attention category, reconcile-month skill, skattekonto notice, fair sync order Accounted://reconciliation/summary: every reconcilable account with its state, unexplained difference, open counts, last fetch and latest sign-off, plus a next step; the rail as a resource, on the same service function the page and v1 use. Accounted://attention gains reconciliation_due (shared predicate with the Hem row). A reconcile-month workflow skill and the reconcile_month loadout describe the account-keyed flow (summary -> bridge -> buckets -> sign-off). The skattekonto sync persists its reconciliation summary (skattekonto_reconciliation_latest) so the new Hem notice skv_unexplained ("Skattekontot stämmer inte med bokföringen: X är oförklarat", link to /reconciliation?account=skattekonto) costs one small read instead of a bridge computation per render; it honours the drift tolerance and its id carries the whole-krona amount so öre noise never resurfaces a dismissal. The skattekonto sync cron orders eligible companies by stalest sync (never-synced first) before its per-run cap, so the tail is no longer starved by a fixed order. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * chore: retrigger preview build (builder OOM during Running TypeScript, not the diff) * fix(reconciliation): visual pass round 1: full-width table, bank tile shows the period sum From Jakob's first look at the page on real data: - The items table now spans the full page width (the approved layout); the rail + tiles + bridge + actions stay in the two-column grid above it, which now lives inside AccountOverview (the rail rides in as a prop) so the table can break out below. - The bank account's first tile said "okänt": it read external_balance (the reported bank balance, often unknown) while its label says Banktransaktioner i perioden. It now shows the bridge's period sum, matching the label, the difference and the bridge line. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat(reconciliation): bank brand icons in the rail The rail resolves each bank account's icon from its connection's bank_name (falling back to the account name) against square brand icons committed under public/logos/banks/: the set covers every bank with a live connection in prod as of 2026-08-24 (SEB, Lunar, Handelsbanken, Swedbank, Nordea, Svea, Länsförsäkringar, Revolut, Wise, Danske, Klarna, Northmill, PayPal, plus Stripe for named accounts). Word-boundary matching so lookalike names never hijack a logo; anything unmatched (the small sparbanker, file imports) keeps the monogram. The skattekonto already had its Skatteverket mark. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(reconciliation): label the bank period sum as netto Jakob read 'Banktransaktioner i perioden 399 941 kr' as gross activity (his is ~1,9 MSEK) and rightly asked why it was so low: the value is the net movement (in - out), which is what the bridge compares against the net booked movement on the ledger account. Verified against raw prod data (237 rows, 1 169 126,40 in, -769 185,04 out = 399 941,36). The tile and the bridge line now say '(netto)' / '(net)'. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> --------- Co-authored-by: Jakob Wennberg <311770904+jakobwennberg-oss@users.noreply.github.com> Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Fable 5
Jakob Wennberg
parent
f40795896f
commit
150e2a3f14
@@ -330,3 +330,30 @@ describe('Accounted://attention', () => {
|
||||
expect(result.summary.total_items).toBe(3)
|
||||
})
|
||||
})
|
||||
|
||||
describe('Accounted://attention: reconciliation_due', () => {
|
||||
it('adds the category when signed-off accounts have fallen behind the previous month end', async () => {
|
||||
const { supabase, enqueue } = createQueuedMockSupabase()
|
||||
enqueueEmpty(enqueue)
|
||||
// countReconciliationDue: sign-offs (adoption + coverage), cash accounts, skattekonto rows.
|
||||
enqueue({
|
||||
data: [{ account_key: 'skattekonto', through_date: '2020-01-31', reopened_at: null }],
|
||||
})
|
||||
enqueue({ data: [{ id: '11111111-1111-4111-8111-111111111111', iban: null, currency: 'SEK', updated_at: null }] })
|
||||
enqueue({ count: 3 })
|
||||
const out = (await attentionResource.read(ctx(supabase))) as AttentionResponse
|
||||
const cat = out.categories.find((c) => c.key === 'reconciliation_due')
|
||||
expect(cat).toBeDefined()
|
||||
// The bank account and the skattekonto are both due.
|
||||
expect(cat?.count).toBe(2)
|
||||
expect(cat?.next?.resource).toBe('Accounted://reconciliation/summary')
|
||||
})
|
||||
|
||||
it('stays silent for a company that never signed anything off', async () => {
|
||||
const { supabase, enqueue } = createQueuedMockSupabase()
|
||||
enqueueEmpty(enqueue)
|
||||
enqueue({ data: [] })
|
||||
const out = (await attentionResource.read(ctx(supabase))) as AttentionResponse
|
||||
expect(out.categories.find((c) => c.key === 'reconciliation_due')).toBeUndefined()
|
||||
})
|
||||
})
|
||||
|
||||
@@ -0,0 +1,94 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
||||
import { createQueuedMockSupabase } from '@/tests/helpers'
|
||||
|
||||
const listMock = vi.fn()
|
||||
vi.mock('@/lib/reconciliation/service', () => ({
|
||||
listReconciliationAccounts: (...args: unknown[]) => listMock(...args),
|
||||
getAccountStatus: vi.fn(),
|
||||
}))
|
||||
|
||||
import { reconciliationSummaryResource } from '../resources/reconciliation-summary'
|
||||
|
||||
const CASH = '11111111-1111-4111-8111-111111111111'
|
||||
|
||||
function account(overrides: Record<string, unknown> = {}) {
|
||||
return {
|
||||
account_key: `bank:${CASH}`,
|
||||
kind: 'bank',
|
||||
account_number: '1930',
|
||||
name: 'Företagskonto',
|
||||
currency: 'SEK',
|
||||
logo_url: null,
|
||||
source: { type: 'psd2', synced_at: '2026-08-22T06:00:00Z', stale: false },
|
||||
status: {
|
||||
state: 'open',
|
||||
as_of: '2026-08-23T00:00:00Z',
|
||||
unexplained_difference: 250,
|
||||
open_counts: { proposed: 2, unmatched_external: 1, unmatched_ledger: 0 },
|
||||
},
|
||||
superseded_by: null,
|
||||
signed_off_through: '2026-06-30',
|
||||
...overrides,
|
||||
}
|
||||
}
|
||||
|
||||
describe('Accounted://reconciliation/summary', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
listMock.mockReset()
|
||||
})
|
||||
|
||||
it('lists accounts with state, counts and sign-off, totals them, and points at the account with proposals', async () => {
|
||||
const { supabase } = createQueuedMockSupabase()
|
||||
listMock.mockResolvedValue([
|
||||
account(),
|
||||
account({
|
||||
account_key: 'skattekonto',
|
||||
kind: 'skattekonto',
|
||||
account_number: '1630',
|
||||
name: 'Skattekonto',
|
||||
status: { state: 'reconciled', as_of: '2026-08-23T04:00:00Z', unexplained_difference: 0, open_counts: { proposed: 0, unmatched_external: 0, unmatched_ledger: 0 } },
|
||||
signed_off_through: '2026-07-31',
|
||||
}),
|
||||
// A reconnect duplicate is listed but not counted.
|
||||
account({ account_key: 'bank:22222222-2222-4222-8222-222222222222', superseded_by: `bank:${CASH}` }),
|
||||
])
|
||||
const out = (await reconciliationSummaryResource.read({
|
||||
supabase: supabase as never,
|
||||
companyId: 'company-1',
|
||||
userId: 'user-1',
|
||||
scopes: [],
|
||||
query: new URLSearchParams('date_from=2026-07-01&date_to=2026-07-31'),
|
||||
})) as Record<string, unknown>
|
||||
expect(listMock).toHaveBeenCalledWith(supabase, 'company-1', { withStatus: true, windowFrom: '2026-07-01', windowTo: '2026-07-31' })
|
||||
expect(out.totals).toMatchObject({ accounts: 2, reconciled: 1, open: 1, proposed: 2, unmatched_external: 1 })
|
||||
const accounts = out.accounts as Array<Record<string, unknown>>
|
||||
expect(accounts).toHaveLength(3)
|
||||
expect(accounts[0]).toMatchObject({ account_key: `bank:${CASH}`, state: 'open', signed_off_through: '2026-06-30' })
|
||||
expect(out.next).toMatchObject({ tool: 'gnubok_reconcile_match', args: { account_key: `bank:${CASH}`, use_proposals: true, dry_run: true } })
|
||||
})
|
||||
|
||||
it('suggests signing off when everything is reconciled, and rejects a malformed window', async () => {
|
||||
const { supabase } = createQueuedMockSupabase()
|
||||
listMock.mockResolvedValue([
|
||||
account({ status: { state: 'reconciled', as_of: '2026-08-23T00:00:00Z', unexplained_difference: 0, open_counts: { proposed: 0, unmatched_external: 0, unmatched_ledger: 0 } } }),
|
||||
])
|
||||
const out = (await reconciliationSummaryResource.read({
|
||||
supabase: supabase as never,
|
||||
companyId: 'company-1',
|
||||
userId: 'user-1',
|
||||
scopes: [],
|
||||
})) as Record<string, unknown>
|
||||
expect(out.next).toMatchObject({ tool: 'gnubok_reconcile_signoff' })
|
||||
|
||||
await expect(
|
||||
reconciliationSummaryResource.read({
|
||||
supabase: supabase as never,
|
||||
companyId: 'company-1',
|
||||
userId: 'user-1',
|
||||
scopes: [],
|
||||
query: new URLSearchParams('date_from=20260701'),
|
||||
}),
|
||||
).rejects.toThrow(/YYYY-MM-DD/)
|
||||
})
|
||||
})
|
||||
@@ -4,7 +4,7 @@ import { dataResources, findResource, parseResourceQuery } from '../resources'
|
||||
|
||||
describe('mcp resource registry', () => {
|
||||
it('exposes all data resources with required fields', () => {
|
||||
expect(dataResources).toHaveLength(9)
|
||||
expect(dataResources).toHaveLength(10)
|
||||
const uris = dataResources.map((r) => r.uri).sort()
|
||||
expect(uris).toEqual([
|
||||
'Accounted://attention',
|
||||
@@ -15,6 +15,7 @@ describe('mcp resource registry', () => {
|
||||
'Accounted://ledger/context',
|
||||
'Accounted://period/active',
|
||||
'Accounted://recent-activity',
|
||||
'Accounted://reconciliation/summary',
|
||||
'Accounted://settings/vat-treatments',
|
||||
])
|
||||
|
||||
|
||||
@@ -75,6 +75,23 @@ export const RECOMMENDED_WORKFLOW_LOADOUTS: readonly WorkflowLoadout[] = [
|
||||
'gnubok_approve_pending_operation',
|
||||
],
|
||||
},
|
||||
{
|
||||
workflow: 'reconcile_month',
|
||||
description: 'Reconcile every account with an outside truth (bank accounts, skattekonto) for a month and sign it off.',
|
||||
skill: 'reconcile-month',
|
||||
tools: [
|
||||
'gnubok_get_reconciliation_status',
|
||||
'gnubok_list_reconciliation_items',
|
||||
'gnubok_reconcile_match',
|
||||
'gnubok_reconcile_unmatch',
|
||||
// Rows with no counterpart: book them (bank side) or link to the
|
||||
// verifikat that already holds the affärshändelse.
|
||||
'gnubok_categorize_transaction',
|
||||
'gnubok_link_transaction_to_journal_entry',
|
||||
'gnubok_reconcile_signoff',
|
||||
'gnubok_approve_pending_operation',
|
||||
],
|
||||
},
|
||||
{
|
||||
workflow: 'invoice_run',
|
||||
description: 'Create and send customer invoices.',
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import type { McpResource } from './types'
|
||||
import { ACTION_NEEDED_THRESHOLD_DAYS } from '@/lib/deadlines/status-engine'
|
||||
import { countReconciliationDue } from '@/lib/worklist/categories'
|
||||
|
||||
type Severity = 'critical' | 'warning' | 'info'
|
||||
|
||||
@@ -341,6 +342,25 @@ export const attentionResource: McpResource = {
|
||||
})
|
||||
}
|
||||
|
||||
// ── Accounts not signed off through the previous month end ──────
|
||||
// Cheap by construction (lib/worklist countReconciliationDue: no bridge
|
||||
// computation) and zero until the company has signed anything off.
|
||||
const reconciliationDue = await countReconciliationDue(supabase, companyId, now)
|
||||
if (reconciliationDue > 0) {
|
||||
categories.push({
|
||||
key: 'reconciliation_due',
|
||||
label_sv: 'Konton som inte är avstämda t.o.m. förra månadsskiftet',
|
||||
severity: 'warning',
|
||||
count: reconciliationDue,
|
||||
samples: [],
|
||||
next: {
|
||||
description:
|
||||
'Läs Accounted://reconciliation/summary för bryggan per konto; koppla föreslagna par, bokför det som saknas och signera med gnubok_reconcile_signoff när oförklarat är 0.',
|
||||
resource: 'Accounted://reconciliation/summary',
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// ── Period lock approaching ─────────────────────────────────────
|
||||
const lockDate = companySettingsRow.data?.bookkeeping_locked_through ?? null
|
||||
if (lockDate && activePeriodRow.data) {
|
||||
|
||||
@@ -8,6 +8,7 @@ import { vatTreatmentsResource } from './vat-treatments'
|
||||
import { attentionResource } from './attention'
|
||||
import { ledgerContextResource } from './ledger-context'
|
||||
import { bookingPacksResource } from './booking-packs'
|
||||
import { reconciliationSummaryResource } from './reconciliation-summary'
|
||||
|
||||
export const dataResources: McpResource[] = [
|
||||
companyCurrentResource,
|
||||
@@ -19,6 +20,7 @@ export const dataResources: McpResource[] = [
|
||||
attentionResource,
|
||||
ledgerContextResource,
|
||||
bookingPacksResource,
|
||||
reconciliationSummaryResource,
|
||||
]
|
||||
|
||||
export function findResource(uri: string): McpResource | null {
|
||||
|
||||
@@ -0,0 +1,99 @@
|
||||
import type { McpResource } from './types'
|
||||
import { listReconciliationAccounts } from '@/lib/reconciliation/service'
|
||||
import { ISO_DATE_RE } from '@/lib/invariants'
|
||||
|
||||
/**
|
||||
* Accounted://reconciliation/summary
|
||||
*
|
||||
* Every account with an outside truth (bank accounts, the skattekonto) with
|
||||
* its reconciliation state, open counts and latest sign-off, in one read: the
|
||||
* rail of the Avstämning page as a resource. Optional ?date_from / ?date_to
|
||||
* scope the bank bridges (the skattekonto bridge is anchored at its saldo
|
||||
* snapshot). Same service function the page and the v1 API use, so the
|
||||
* agent sees exactly what the user sees.
|
||||
*/
|
||||
export const reconciliationSummaryResource: McpResource = {
|
||||
uri: 'Accounted://reconciliation/summary',
|
||||
name: 'Reconciliation Summary',
|
||||
description:
|
||||
'Per-account reconciliation state for the active company: bank accounts (bank:<cash_account_id>) and the skattekonto, each with state (reconciled / open / stale / not_configured), unexplained_difference, open counts (proposed, unmatched_external, unmatched_ledger), last outside fetch, and the latest sign-off date. Optional ?date_from=YYYY-MM-DD&date_to=YYYY-MM-DD scope the bank bridges. Read this before gnubok_get_reconciliation_status / gnubok_list_reconciliation_items to pick the account that needs work.',
|
||||
mimeType: 'application/json',
|
||||
read: async ({ supabase, companyId, query }) => {
|
||||
const dateFrom = query?.get('date_from') ?? undefined
|
||||
const dateTo = query?.get('date_to') ?? undefined
|
||||
if ((dateFrom && !ISO_DATE_RE.test(dateFrom)) || (dateTo && !ISO_DATE_RE.test(dateTo))) {
|
||||
throw new Error('date_from / date_to must be YYYY-MM-DD')
|
||||
}
|
||||
const accounts = await listReconciliationAccounts(supabase, companyId, {
|
||||
withStatus: true,
|
||||
windowFrom: dateFrom,
|
||||
windowTo: dateTo,
|
||||
})
|
||||
|
||||
const rows = accounts.map((a) => ({
|
||||
account_key: a.account_key,
|
||||
kind: a.kind,
|
||||
name: a.name,
|
||||
account_number: a.account_number,
|
||||
currency: a.currency,
|
||||
state: a.status?.state ?? 'not_configured',
|
||||
unexplained_difference: a.status?.unexplained_difference ?? null,
|
||||
open_counts: a.status?.open_counts ?? { proposed: 0, unmatched_external: 0, unmatched_ledger: 0 },
|
||||
as_of: a.status?.as_of ?? null,
|
||||
synced_at: a.source.synced_at,
|
||||
stale: a.source.stale,
|
||||
signed_off_through: a.signed_off_through ?? null,
|
||||
superseded_by: a.superseded_by,
|
||||
}))
|
||||
|
||||
const live = rows.filter((r) => !r.superseded_by)
|
||||
const totals = {
|
||||
accounts: live.length,
|
||||
reconciled: live.filter((r) => r.state === 'reconciled').length,
|
||||
open: live.filter((r) => r.state === 'open' || r.state === 'stale').length,
|
||||
not_configured: live.filter((r) => r.state === 'not_configured').length,
|
||||
proposed: live.reduce((s, r) => s + r.open_counts.proposed, 0),
|
||||
unmatched_external: live.reduce((s, r) => s + r.open_counts.unmatched_external, 0),
|
||||
unmatched_ledger: live.reduce((s, r) => s + r.open_counts.unmatched_ledger, 0),
|
||||
}
|
||||
|
||||
// Point at the account with the most open work; proposals first since
|
||||
// they are one staged call away from done.
|
||||
const target =
|
||||
[...live]
|
||||
.filter((r) => r.state === 'open' || r.state === 'stale')
|
||||
.sort(
|
||||
(x, y) =>
|
||||
y.open_counts.proposed - x.open_counts.proposed ||
|
||||
y.open_counts.unmatched_external +
|
||||
y.open_counts.unmatched_ledger -
|
||||
(x.open_counts.unmatched_external + x.open_counts.unmatched_ledger),
|
||||
)[0] ?? null
|
||||
|
||||
return {
|
||||
generated_at: new Date().toISOString(),
|
||||
window: { from: dateFrom ?? null, to: dateTo ?? null },
|
||||
totals,
|
||||
accounts: rows,
|
||||
next: target
|
||||
? target.open_counts.proposed > 0
|
||||
? {
|
||||
description: `${target.name}: ${target.open_counts.proposed} föreslagna par väntar. Koppla dem, sedan bokför det som saknas.`,
|
||||
tool: 'gnubok_reconcile_match',
|
||||
args: { account_key: target.account_key, use_proposals: true, dry_run: true },
|
||||
}
|
||||
: {
|
||||
description: `${target.name}: läs raderna bakom bryggan och bokför eller koppla dem.`,
|
||||
tool: 'gnubok_list_reconciliation_items',
|
||||
args: { account_key: target.account_key },
|
||||
}
|
||||
: {
|
||||
description:
|
||||
totals.accounts === 0
|
||||
? 'Inga konton med en sanning utanför bokföringen (koppla bank eller Skatteverket).'
|
||||
: 'Alla konton är förklarade. Signera månaden med gnubok_reconcile_signoff per konto.',
|
||||
tool: totals.accounts === 0 ? undefined : 'gnubok_reconcile_signoff',
|
||||
},
|
||||
}
|
||||
},
|
||||
}
|
||||
@@ -8,6 +8,7 @@ import { payrollMonthlySkill } from './payroll-monthly'
|
||||
import { bankReconciliationSkill } from './bank-reconciliation'
|
||||
import { kreditfakturaProcessSkill } from './kreditfaktura-process'
|
||||
import { customerOnboardingSkill } from './customer-onboarding'
|
||||
import { reconcileMonthSkill } from './reconcile-month'
|
||||
import { loadAtomsAsSkills, loadReferenceById } from './atoms'
|
||||
|
||||
/** Static workflow skills the server ships with. Tier: 'workflow'. */
|
||||
@@ -20,6 +21,7 @@ export const workflowSkills: Skill[] = [
|
||||
bankReconciliationSkill,
|
||||
kreditfakturaProcessSkill,
|
||||
customerOnboardingSkill,
|
||||
reconcileMonthSkill,
|
||||
]
|
||||
|
||||
/** @deprecated Use `workflowSkills` for the static set, or `loadAllSkills(supabase)`
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
import type { Skill } from './types'
|
||||
|
||||
const body = `# Reconcile a Month: Accounted
|
||||
|
||||
Reconcile every account that has a truth outside the ledger (bank accounts and the skattekonto) for a month, then sign it off. This is the account-keyed flow: one engine, the same numbers the user sees on /reconciliation.
|
||||
|
||||
## When to use
|
||||
|
||||
- "Stäm av månaden" / "Stäm av banken och skattekontot"
|
||||
- "Är juli avstämt?" / "Markera juli som avstämd"
|
||||
- Before \`close_period\` and before the momsdeklaration
|
||||
|
||||
## The model in one paragraph
|
||||
|
||||
Each account is identified by an \`account_key\`: \`bank:<cash_account_id>\` or \`skattekonto\`. For each account the engine compares the outside balance (bank balance / Skatteverket saldo) with the ledger (19xx / 1630) and explains the difference line by line: unmatched outside rows, unmatched ledger lines, ignored rows, and for the skattekonto the opening difference before the fetched history. \`unexplained_difference\` is the number that matters: when it is 0 the account is reconciled. Matched pairs cancel out; a link never writes to the ledger.
|
||||
|
||||
## Workflow
|
||||
|
||||
### Step 1: Read the summary
|
||||
|
||||
Read \`Accounted://reconciliation/summary\` (optionally \`?date_from&date_to\`). Pick the accounts whose \`state\` is \`open\` or \`stale\`. \`stale\` means the outside side is older than 7 days: ask the user to fetch (bank sync / skattekonto sync) before trusting the bridge.
|
||||
|
||||
### Step 2: Read the bridge for one account
|
||||
|
||||
\`gnubok_get_reconciliation_status({ account_key })\` returns the bridge: outside balance, ledger balance, difference, unexplained_difference, the explanatory lines, counts per bucket, and the latest sign-off. Judge on \`unexplained_difference\`, never on \`difference\`.
|
||||
|
||||
### Step 3: Work the buckets, in this order
|
||||
|
||||
\`gnubok_list_reconciliation_items({ account_key, bucket })\`:
|
||||
|
||||
1. **proposed**: outside rows with a proposed verifikat (exact twin on amount/date). Link them in one staged call: \`gnubok_reconcile_match({ account_key, use_proposals: true, dry_run: true })\`, then without dry_run. The response lists \`applied[]\` and \`skipped[{code}]\`: a skip is information, not an error (ALREADY_LINKED, PAIR_NOT_CLOSED, ENTRY_NOT_FOUND).
|
||||
2. **unmatched_external**: outside rows with no counterpart. Bank rows: book them (\`gnubok_categorize_transaction\`, or \`gnubok_link_transaction_to_journal_entry\` when the affärshändelse is already on a verifikat). Skattekonto rows: the user books them from /skattekonto or /reconciliation (the rule-based booking lives there); tell the user which rows and amounts. A row that will never be booked (a duplicate, a noise line) is ignored from the page, not by you.
|
||||
3. **unmatched_ledger**: verifikat lines on the account with nothing outside. Within 5 days of the snapshot they may simply be waiting for the outside side (\`awaiting_external\`). Older ones are usually a wrong account or a missing outside row: show them to the user with voucher numbers; do not reverse anything on your own.
|
||||
4. **matched** and **ignored** explain the bridge and need no work.
|
||||
|
||||
Re-read the status after each round. Stop when \`unexplained_difference\` is 0, or when what remains needs a human decision.
|
||||
|
||||
### Step 4: Sign off
|
||||
|
||||
When the account is reconciled through the month end: \`gnubok_reconcile_signoff({ account_key, through_date: "YYYY-MM-DD", dry_run: true })\`, then without dry_run. It stages; the user approves. Refusals are policy, not failures: NOT_RECONCILED (something is still unexplained), NOT_FETCHED_THROUGH (skattekonto snapshot is older than the date), ALREADY_SIGNED_OFF (reopen first), NOTE_REQUIRED (force needs a note). Signing with \`force: true\` and a note is the user's call, never yours by default.
|
||||
|
||||
### Step 5: Report
|
||||
|
||||
Per account: outside vs ledger, what was linked, what the user still has to book, and the sign-off date. Point at \`/reconciliation?account=<account_key>\` for anything that needs a hand.
|
||||
|
||||
## Rules
|
||||
|
||||
- Links and sign-offs never touch the ledger; booking does, and always stages.
|
||||
- One outside row links to one verifikat in this version; other shapes come back as UNSUPPORTED_PAIR_SHAPE. A fee or rounding difference needs a residual booking by the user first.
|
||||
- Never judge on \`difference\`; the bridge explains it. Judge on \`unexplained_difference\`.
|
||||
- A skattekonto sign-off date cannot pass the saldo snapshot; ask for a fetch.
|
||||
|
||||
## Tools used
|
||||
|
||||
- \`gnubok_get_reconciliation_status\`, \`gnubok_list_reconciliation_items\` (read)
|
||||
- \`gnubok_reconcile_match\`, \`gnubok_reconcile_unmatch\`, \`gnubok_reconcile_signoff\` (staged writes)
|
||||
- \`gnubok_categorize_transaction\`, \`gnubok_link_transaction_to_journal_entry\` (bank-side booking)
|
||||
- \`gnubok_approve_pending_operation\` (when the user approves in chat)
|
||||
- Resource: \`Accounted://reconciliation/summary\`
|
||||
`
|
||||
|
||||
export const reconcileMonthSkill: Skill = {
|
||||
slug: 'reconcile-month',
|
||||
name: 'Reconcile a Month',
|
||||
summary: 'Stäm av månaden: read the per-account bridge, link proposed pairs, get the rest booked, and sign each account off through the month end.',
|
||||
tags: ['monthly', 'reconciliation', 'bank', 'skattekonto', 'sign-off'],
|
||||
body,
|
||||
tier: 'workflow',
|
||||
applicability: { entity_type: 'both' },
|
||||
}
|
||||
@@ -8,6 +8,11 @@ import { getEarliestFiscalPeriodStart } from '@/lib/core/bookkeeping/period-serv
|
||||
import { fetchAllRows } from '@/lib/supabase/fetch-all'
|
||||
import { settleAgiTaxPayments } from './agi-tax-settlement'
|
||||
import { refreshSkattekontoProposals } from './skattekonto-proposals'
|
||||
import { getSkattekontoReconciliationStatus } from '@/lib/reconciliation/skattekonto-reconciliation'
|
||||
import {
|
||||
SKATTEKONTO_RECONCILIATION_LATEST_KEY,
|
||||
type SkattekontoReconciliationLatest,
|
||||
} from '@/lib/reconciliation/skattekonto-latest'
|
||||
import { getSaldo, getTransaktioner } from './skattekonto-client'
|
||||
import { SkatteverketAuthError, type SkvAuth } from './api-client'
|
||||
import type {
|
||||
@@ -385,6 +390,33 @@ export async function syncSkattekonto(
|
||||
await ctx.settings.set(BALANCE_SNAPSHOT_KEY, snapshot)
|
||||
await ctx.settings.set(LAST_SYNCED_AT_KEY, new Date().toISOString())
|
||||
|
||||
// Persist the reconciliation summary so the Hem notice and the attention
|
||||
// resource can read "skattekontot stämmer inte med bokföringen" cheaply
|
||||
// instead of recomputing the bridge on every render. Best effort.
|
||||
try {
|
||||
const status = await getSkattekontoReconciliationStatus(ctx.supabase, ctx.companyId)
|
||||
if (status) {
|
||||
const latest: SkattekontoReconciliationLatest = {
|
||||
as_of: status.as_of,
|
||||
computed_at: new Date().toISOString(),
|
||||
external_balance: status.external_balance,
|
||||
ledger_balance: status.ledger_balance,
|
||||
unexplained_difference: status.unexplained_difference,
|
||||
counts: {
|
||||
proposed: status.counts.proposed,
|
||||
unmatched_external: status.counts.unmatched_external,
|
||||
unmatched_ledger: status.counts.unmatched_ledger,
|
||||
},
|
||||
}
|
||||
await ctx.settings.set(SKATTEKONTO_RECONCILIATION_LATEST_KEY, latest)
|
||||
}
|
||||
} catch (err) {
|
||||
log.warn('reconciliation summary not persisted', {
|
||||
companyId: ctx.companyId,
|
||||
message: err instanceof Error ? err.message : String(err),
|
||||
})
|
||||
}
|
||||
|
||||
// Emit events.
|
||||
await ctx.emit({
|
||||
type: 'skattekonto.synced',
|
||||
|
||||
Reference in New Issue
Block a user