feat(reconciliation): page-owned window, automatic matching, and a way out for unbookable rows (#1742)

Second half of the reconciliation redesign, on top of the bridge in #1737.

**Toolbar.** The view hosted its own "Datum från / Datum till" inputs behind a
Filtrera button: a second period control competing with the header's
räkenskapsår picker (convention 8), and the source of a "typed but not applied"
state that needed its own attention line to explain. The window is now owned by
the page, narrowed through the shared ReportDateRange like every other report,
and applied on change. The view holds no date state at all, which also removes
the ref-synchronisation dance and the off-by-one it existed to prevent (a year
switch fetching the previous year's window because the refs updated a commit
late).

Reconciliation opens on the FULL year, not the family default of YTD, and keeps
its own preset memory: a reconciliation runs over a whole räkenskapsår, and
inheriting a "Denna månad" last used on Resultatrapport would show an alarming
difference for a window nobody chose here. ReportDateRange gained defaultPreset
and storageKeyPrefix for that; every existing caller keeps its behaviour.

**Automatic matching.** "Förhandsgranska" told the user nothing about what it
did, and the ochre line above it existed only to point at it: people matched a
whole migration row by row next to a button they never found. The matcher now
runs by itself, once per window+account, whenever there is unmatched work. It is
a dry run, so nothing is written and Tillämpa still requires an explicit click.
The button stays as a re-run and is renamed to what it does. ?autorun=1 keeps a
distinct meaning (run even on a clean window) so the transactions-inbox deep
link still produces a result rather than silence.

**A way out for rows that cannot be paired.** An unmatched bank row that no
voucher on the account could settle is not reconciliation work, it is an unbooked
affärshändelse, and the match picker held nothing for it. Those rows now offer
"Bokför" into /transactions?highlight=<id>, with a bulk link in the section
header. The rule (direction-compatible and equal to the öre) is extracted to
lib/reconciliation/voucher-candidate.ts so it is testable and so the component
never imports the server-only reconciliation module. Deliberately strict: a false
negative offers booking on a row that could also have been paired, which is a
legitimate outcome, while a false positive sends the user into an empty picker.

11 new tests for the candidate rule, covering direction, öre equality, float
noise, PostgREST numeric strings and the foreign-account case where the
candidate RPC projects no FX amount and no match may be claimed.

Co-authored-by: Jakob Wennberg <311770904+jakobwennberg-oss@users.noreply.github.com>
Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Jakob Wennberg
2026-08-20 13:14:31 +02:00
committed by GitHub
co-authored by Jakob Wennberg Claude Opus 5
parent b5e908f9ea
commit 5a8dd21931
9 changed files with 385 additions and 175 deletions
+25 -6
View File
@@ -22,6 +22,23 @@ interface Props {
periodEnd: string
value: DateRangeValue
onChange: (next: DateRangeValue) => void
/**
* Preset to open on when this company has no stored choice yet. Defaults to
* YTD, which matches Fortnox/Visma for the resultat-/balansrapport family.
* Bank reconciliation passes 'full_year': a reconciliation is carried out
* over a whole räkenskapsår, and a part-year window makes its difference
* describe a period the user did not ask about.
*/
defaultPreset?: Preset
/**
* localStorage prefix for the remembered preset (companyId is appended).
* Defaults to the range shared by the report family. Pass a page-specific
* prefix where inheriting another report's preset would be wrong rather than
* merely surprising: bank reconciliation opened on a "Denna månad" carried
* over from Resultatrapport would show an alarming difference for a window
* nobody chose.
*/
storageKeyPrefix?: string
className?: string
}
@@ -121,31 +138,33 @@ export function ReportDateRange({
periodEnd,
value,
onChange,
defaultPreset = 'ytd',
storageKeyPrefix = STORAGE_KEY_PREFIX,
className,
}: Props) {
const t = useTranslations('reports')
const { company } = useCompany()
const [preset, setPreset] = useState<Preset>('ytd')
const [preset, setPreset] = useState<Preset>(defaultPreset)
// Restore last-used preset per company, then resolve it against the
// current fiscal period. The period selector lives upstream: when it
// changes, we re-resolve so the dates always sit inside the visible year.
useEffect(() => {
if (!company?.id || typeof window === 'undefined') return
const stored = window.localStorage.getItem(STORAGE_KEY_PREFIX + company.id) as Preset | null
const initial: Preset = stored && PRESETS.includes(stored) ? stored : 'ytd'
const stored = window.localStorage.getItem(storageKeyPrefix + company.id) as Preset | null
const initial: Preset = stored && PRESETS.includes(stored) ? stored : defaultPreset
setPreset(initial)
if (initial !== 'custom') {
onChange(resolvePreset(initial, periodStart, periodEnd, todayIso()))
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [company?.id, periodStart, periodEnd])
}, [company?.id, periodStart, periodEnd, storageKeyPrefix, defaultPreset])
const handlePreset = useCallback(
(next: Preset) => {
setPreset(next)
if (company?.id && typeof window !== 'undefined') {
window.localStorage.setItem(STORAGE_KEY_PREFIX + company.id, next)
window.localStorage.setItem(storageKeyPrefix + company.id, next)
}
if (next === 'custom') {
// Seed the custom inputs with whatever is currently active so the
@@ -157,7 +176,7 @@ export function ReportDateRange({
}
onChange(resolvePreset(next, periodStart, periodEnd, todayIso()))
},
[company?.id, onChange, periodEnd, periodStart, value.fromDate, value.toDate],
[company?.id, onChange, periodEnd, periodStart, storageKeyPrefix, value.fromDate, value.toDate],
)
const handleFromChange = (raw: string) => {