Files
accounted/lib/reconciliation/schemas.ts
T
0a8544e0cb feat(reconciliation): account-keyed engine: one bridge for bank and skattekonto (#1813)
* 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>

---------

Co-authored-by: Jakob Wennberg <311770904+jakobwennberg-oss@users.noreply.github.com>
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-08-24 13:55:08 +02:00

201 lines
7.7 KiB
TypeScript

import { z } from 'zod'
/**
* Shared shapes for the account-keyed reconciliation surface.
*
* One engine, three doors: the dashboard routes, the public v1 API and the
* MCP tools all speak these shapes, so the Zod schemas here are the single
* source for OpenAPI (v1 registry), MCP input/output schemas and the UI
* types. Kind-specific detail lives in a `bank` / `skattekonto` block; the
* common block is what every caller can reason about without knowing the
* account kind.
*
* Account keys:
* bank:<cash_account_id> one cash_accounts row (PSD2 or file-fed)
* skattekonto the company's Skatteverket tax account (BAS 1630)
* manual:<account_number> later: accounts with a typed external balance
*/
export const ACCOUNT_KEY_REGEX =
/^(bank:[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}|skattekonto|manual:\d{4})$/
export const AccountKeySchema = z.string().regex(ACCOUNT_KEY_REGEX, 'Ogiltig account_key')
export type AccountKey = z.infer<typeof AccountKeySchema>
export const ReconciliationKindSchema = z.enum(['bank', 'skattekonto', 'manual'])
export type ReconciliationKind = z.infer<typeof ReconciliationKindSchema>
export type ParsedAccountKey =
| { kind: 'bank'; cashAccountId: string }
| { kind: 'skattekonto' }
| { kind: 'manual'; accountNumber: string }
export function parseAccountKey(key: string): ParsedAccountKey | null {
if (!ACCOUNT_KEY_REGEX.test(key)) return null
if (key === 'skattekonto') return { kind: 'skattekonto' }
if (key.startsWith('bank:')) return { kind: 'bank', cashAccountId: key.slice('bank:'.length) }
return { kind: 'manual', accountNumber: key.slice('manual:'.length) }
}
export function bankAccountKey(cashAccountId: string): string {
return `bank:${cashAccountId}`
}
export const SKATTEKONTO_ACCOUNT_KEY = 'skattekonto' as const
export const ReconciliationSourceSchema = z.object({
type: z.enum(['psd2', 'bank_file', 'skatteverket_api', 'skatteverket_file', 'manual']),
/** ISO timestamp of the last successful sync / import; null when never. */
synced_at: z.string().nullable(),
/** True when the outside truth is older than STALE_AFTER_DAYS. */
stale: z.boolean(),
})
export const ReconciliationStateSchema = z.enum([
'reconciled',
'open',
'stale',
'not_configured',
])
export const ReconciliationAccountSchema = z.object({
account_key: AccountKeySchema,
kind: ReconciliationKindSchema,
account_number: z.string(),
name: z.string(),
currency: z.string(),
logo_url: z.string().nullable(),
source: ReconciliationSourceSchema,
status: z
.object({
state: ReconciliationStateSchema,
/** ISO timestamp the status was computed for (snapshot time or now). */
as_of: z.string(),
unexplained_difference: z.number().nullable(),
open_counts: z.object({
proposed: z.number().int(),
unmatched_external: z.number().int(),
unmatched_ledger: z.number().int(),
}),
})
.nullable(),
/** Another enabled cash account shares this IBAN and currency (reconnect duplicate). */
superseded_by: AccountKeySchema.nullable(),
})
export type ReconciliationAccount = z.infer<typeof ReconciliationAccountSchema>
/** One explanatory line of the bridge: label + amount + the filter that lists its rows. */
export const BridgeLineSchema = z.object({
key: z.string(),
label_sv: z.string(),
label_en: z.string(),
amount: z.number(),
count: z.number().int().nullable(),
/** Bucket to request from the items endpoint to see these rows, when any. */
items_bucket: z.string().nullable(),
})
export type BridgeLine = z.infer<typeof BridgeLineSchema>
export const ReconciliationItemBucketSchema = z.enum([
'proposed',
'unmatched_external',
'unmatched_ledger',
'matched',
'ignored',
'upcoming',
])
export type ReconciliationItemBucket = z.infer<typeof ReconciliationItemBucketSchema>
export const ReconciliationItemActionSchema = z.enum([
'match',
'unmatch',
'book',
'ignore',
'unignore',
'review',
])
export const ReconciliationProposalSchema = z.object({
journal_entry_id: z.string(),
voucher_number: z.number().int().nullable(),
voucher_series: z.string().nullable(),
entry_date: z.string(),
description: z.string(),
entry_status: z.enum(['draft', 'posted', 'reversed']),
confidence: z.number().min(0).max(1),
reasons: z.array(z.string()),
})
export type ReconciliationProposal = z.infer<typeof ReconciliationProposalSchema>
export const ReconciliationItemSchema = z.object({
/** Qualified id of the row on its side: skattekonto_transaction_id, transaction_id or journal_entry_id. */
item_id: z.string(),
item_type: z.enum(['skattekonto_transaction', 'transaction', 'journal_entry']),
side: z.enum(['external', 'ledger']),
bucket: ReconciliationItemBucketSchema,
date: z.string(),
description: z.string(),
/** Signed amount in the account's natural direction (debit positive on the ledger side). */
amount: z.number(),
currency: z.string(),
voucher_number: z.number().int().nullable().optional(),
voucher_series: z.string().nullable().optional(),
entry_status: z.enum(['draft', 'posted', 'reversed']).optional(),
/** Linked counterpart on the other side, when any. */
linked_journal_entry_id: z.string().nullable().optional(),
/** Why a linked row is not counted as settled (the entry was reversed or is still a draft). */
link_problem: z.enum(['entry_reversed', 'entry_draft', 'entry_missing']).nullable().optional(),
proposal: ReconciliationProposalSchema.nullable().optional(),
/** Ledger line dated within AWAITING_EXTERNAL_DAYS of the snapshot: the outside side may simply not have posted it yet. */
awaiting_external: z.boolean().optional(),
actions: z.array(ReconciliationItemActionSchema),
})
export type ReconciliationItem = z.infer<typeof ReconciliationItemSchema>
export const SkattekontoStatusBlockSchema = z.object({
saldo_skatteverket: z.number().nullable(),
/** ISO timestamp of the saldo snapshot; null when never synced. */
fetched_at: z.string().nullable(),
/** Earliest SKV-posted row we hold: the start of the comparable history. */
history_start: z.string().nullable(),
/** saldo_at_start - ledger balance before history_start. Null without a snapshot. */
opening_difference: z.number().nullable(),
upcoming_count: z.number().int(),
upcoming_total: z.number(),
ledger_balance_before_start: z.number().nullable(),
})
export const ReconciliationStatusSchema = z.object({
account_key: AccountKeySchema,
kind: ReconciliationKindSchema,
account_number: z.string(),
currency: z.string(),
window: z.object({ from: z.string().nullable(), to: z.string().nullable() }),
as_of: z.string(),
stale: z.boolean(),
/** What the outside says (bank balance or Skatteverket saldo); null when unknown. */
external_balance: z.number().nullable(),
/** What the ledger says on the account (balance for skattekonto, period movement for bank). */
ledger_balance: z.number().nullable(),
difference: z.number().nullable(),
unexplained_difference: z.number().nullable(),
is_reconciled: z.boolean(),
bridge: z.array(BridgeLineSchema),
counts: z.object({
proposed: z.number().int(),
unmatched_external: z.number().int(),
unmatched_ledger: z.number().int(),
matched: z.number().int(),
ignored: z.number().int(),
}),
skattekonto: SkattekontoStatusBlockSchema.nullable(),
/** Today's bank status fields, unchanged, for the bank kind (see bank-reconciliation.ts). */
bank: z.record(z.string(), z.unknown()).nullable(),
})
export type ReconciliationStatus = z.infer<typeof ReconciliationStatusSchema>
/** Outside truth older than this is flagged stale on every read. */
export const STALE_AFTER_DAYS = 7
/** A ledger line this close to the snapshot may simply be waiting for Skatteverket to post the same event. */
export const AWAITING_EXTERNAL_DAYS = 5