0a8544e0cb
* 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>
39 lines
2.1 KiB
SQL
39 lines
2.1 KiB
SQL
-- Migration: skattekonto_transactions.suggested_journal_entry_id
|
|
--
|
|
-- Reconciliation proposals for the skattekonto (1630). Until now the "Möjlig
|
|
-- dubblett av A 214" hint was recomputed on every page load by
|
|
-- findMatchSuggestionsBulk and lived nowhere, so nothing outside the
|
|
-- /skattekonto request (worklist counts, the reconciliation summary, agents)
|
|
-- could know that a row already has an exact twin in the ledger. Measured on
|
|
-- prod 2026-08-20: 757 of 2 397 "open" rows had an exact-amount 1630 line
|
|
-- within +-7 days, i.e. the work was already done through another door.
|
|
--
|
|
-- The sync writes the single best candidate here (one-to-one across rows,
|
|
-- AGI period first, then nearest date) and clears it when the row is linked
|
|
-- or the candidate stops qualifying. It is a PROPOSAL, never a link:
|
|
-- journal_entry_id is the only link, and only a click (or an approved staged
|
|
-- operation) moves a proposal into it. Propose-only by design; see
|
|
-- DECISIONS.md 2026-08-23.
|
|
--
|
|
-- ON DELETE SET NULL mirrors journal_entry_id: a deleted draft must not leave
|
|
-- a dangling pointer. A proposal is meaningless on a linked row, so the
|
|
-- partial index only covers unlinked rows: that is the worklist predicate
|
|
-- "rows with a twin" and the count the reconciliation summary reads.
|
|
--
|
|
-- RLS: the company-scoped SELECT/UPDATE policies on skattekonto_transactions
|
|
-- already cover these columns; no policy change.
|
|
|
|
ALTER TABLE public.skattekonto_transactions
|
|
ADD COLUMN IF NOT EXISTS suggested_journal_entry_id UUID
|
|
REFERENCES public.journal_entries(id) ON DELETE SET NULL,
|
|
ADD COLUMN IF NOT EXISTS suggested_at TIMESTAMPTZ;
|
|
|
|
COMMENT ON COLUMN public.skattekonto_transactions.suggested_journal_entry_id IS
|
|
'Best exact-twin verifikat proposed by the sync (one-to-one across rows). A proposal, never a link: journal_entry_id is the link.';
|
|
COMMENT ON COLUMN public.skattekonto_transactions.suggested_at IS
|
|
'When suggested_journal_entry_id was last written by the sync.';
|
|
|
|
CREATE INDEX IF NOT EXISTS idx_skattekonto_transactions_suggested_open
|
|
ON public.skattekonto_transactions (company_id)
|
|
WHERE suggested_journal_entry_id IS NOT NULL AND journal_entry_id IS NULL;
|