c74b19df1b
* 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>
238 lines
8.4 KiB
TypeScript
238 lines
8.4 KiB
TypeScript
'use client'
|
|
|
|
import { useCallback, useEffect, useMemo, useState } from 'react'
|
|
import { useTranslations } from 'next-intl'
|
|
import { useCompany } from '@/contexts/CompanyContext'
|
|
import { Label } from '@/components/ui/label'
|
|
import { Input } from '@/components/ui/input'
|
|
import { cn } from '@/lib/utils'
|
|
|
|
export type DateRangeValue = {
|
|
/** Inclusive lower bound. ISO YYYY-MM-DD. `undefined` = period start. */
|
|
fromDate?: string
|
|
/** Inclusive upper bound. ISO YYYY-MM-DD. `undefined` = period end. */
|
|
toDate?: string
|
|
}
|
|
|
|
type Preset = 'full_year' | 'ytd' | 'this_month' | 'last_month' | 'this_quarter' | 'custom'
|
|
|
|
interface Props {
|
|
/** Selected fiscal period — bounds the range. */
|
|
periodStart: string
|
|
periodEnd: string
|
|
value: DateRangeValue
|
|
onChange: (next: DateRangeValue) => void
|
|
className?: string
|
|
}
|
|
|
|
const STORAGE_KEY_PREFIX = 'Accounted:report-range-preset:'
|
|
|
|
const PRESETS: Preset[] = ['full_year', 'ytd', 'this_month', 'last_month', 'this_quarter', 'custom']
|
|
|
|
function todayIso(): string {
|
|
// Local calendar date — using toISOString() returns UTC, which falls a day
|
|
// behind for Swedish users between midnight and 01:00/02:00 local time and
|
|
// would silently truncate "today" from YTD / this-month / this-quarter.
|
|
const d = new Date()
|
|
const y = d.getFullYear()
|
|
const m = String(d.getMonth() + 1).padStart(2, '0')
|
|
const day = String(d.getDate()).padStart(2, '0')
|
|
return `${y}-${m}-${day}`
|
|
}
|
|
|
|
function clampToPeriod(date: string, periodStart: string, periodEnd: string): string {
|
|
if (date < periodStart) return periodStart
|
|
if (date > periodEnd) return periodEnd
|
|
return date
|
|
}
|
|
|
|
/**
|
|
* Resolve a preset to a concrete range inside the fiscal period.
|
|
*
|
|
* Endpoints are always clamped to the period — e.g. "this month" outside the
|
|
* period collapses to a zero-width range at whichever boundary you're nearest.
|
|
* `full_year` returns `{}` so the API call omits the params entirely and the
|
|
* report falls back to its full-period default (preserves cache parity with
|
|
* the pre-feature behaviour).
|
|
*/
|
|
function resolvePreset(
|
|
preset: Preset,
|
|
periodStart: string,
|
|
periodEnd: string,
|
|
reference: string,
|
|
): DateRangeValue {
|
|
if (preset === 'full_year') return {}
|
|
if (preset === 'custom') return {}
|
|
|
|
if (preset === 'ytd') {
|
|
const to = clampToPeriod(reference, periodStart, periodEnd)
|
|
return { fromDate: periodStart, toDate: to }
|
|
}
|
|
|
|
const ref = new Date(reference)
|
|
// Same UTC pitfall as todayIso(): Date.toISOString() returns UTC, so a
|
|
// local Date constructed via `new Date(y, m, d)` round-trips to the wrong
|
|
// calendar day in any timezone west of UTC. Use the local components.
|
|
const toLocalIso = (d: Date) =>
|
|
`${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, '0')}-${String(d.getDate()).padStart(2, '0')}`
|
|
if (preset === 'this_month') {
|
|
const y = ref.getFullYear()
|
|
const m = ref.getMonth()
|
|
const start = `${y}-${String(m + 1).padStart(2, '0')}-01`
|
|
const end = toLocalIso(new Date(y, m + 1, 0))
|
|
return {
|
|
fromDate: clampToPeriod(start, periodStart, periodEnd),
|
|
toDate: clampToPeriod(end, periodStart, periodEnd),
|
|
}
|
|
}
|
|
if (preset === 'last_month') {
|
|
const y = ref.getFullYear()
|
|
const m = ref.getMonth() - 1
|
|
const start = toLocalIso(new Date(y, m, 1))
|
|
const end = toLocalIso(new Date(y, m + 1, 0))
|
|
return {
|
|
fromDate: clampToPeriod(start, periodStart, periodEnd),
|
|
toDate: clampToPeriod(end, periodStart, periodEnd),
|
|
}
|
|
}
|
|
if (preset === 'this_quarter') {
|
|
const y = ref.getFullYear()
|
|
const q = Math.floor(ref.getMonth() / 3)
|
|
const start = `${y}-${String(q * 3 + 1).padStart(2, '0')}-01`
|
|
const end = toLocalIso(new Date(y, q * 3 + 3, 0))
|
|
return {
|
|
fromDate: clampToPeriod(start, periodStart, periodEnd),
|
|
toDate: clampToPeriod(end, periodStart, periodEnd),
|
|
}
|
|
}
|
|
return {}
|
|
}
|
|
|
|
/**
|
|
* Date-range picker for the resultat-/balansrapport family.
|
|
*
|
|
* Default = "Hittills i år" (YTD) which matches Fortnox/Visma. A "Hela året"
|
|
* preset clears the range entirely so the API falls back to full-period
|
|
* behaviour. Custom range is clamped to the fiscal period — cross-year
|
|
* ranges are out of scope.
|
|
*/
|
|
export function ReportDateRange({
|
|
periodStart,
|
|
periodEnd,
|
|
value,
|
|
onChange,
|
|
className,
|
|
}: Props) {
|
|
const t = useTranslations('reports')
|
|
const { company } = useCompany()
|
|
const [preset, setPreset] = useState<Preset>('ytd')
|
|
|
|
// 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'
|
|
setPreset(initial)
|
|
if (initial !== 'custom') {
|
|
onChange(resolvePreset(initial, periodStart, periodEnd, todayIso()))
|
|
}
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
}, [company?.id, periodStart, periodEnd])
|
|
|
|
const handlePreset = useCallback(
|
|
(next: Preset) => {
|
|
setPreset(next)
|
|
if (company?.id && typeof window !== 'undefined') {
|
|
window.localStorage.setItem(STORAGE_KEY_PREFIX + company.id, next)
|
|
}
|
|
if (next === 'custom') {
|
|
// Seed the custom inputs with whatever is currently active so the
|
|
// user can nudge them rather than start from scratch.
|
|
if (!value.fromDate && !value.toDate) {
|
|
onChange({ fromDate: periodStart, toDate: clampToPeriod(todayIso(), periodStart, periodEnd) })
|
|
}
|
|
return
|
|
}
|
|
onChange(resolvePreset(next, periodStart, periodEnd, todayIso()))
|
|
},
|
|
[company?.id, onChange, periodEnd, periodStart, value.fromDate, value.toDate],
|
|
)
|
|
|
|
const handleFromChange = (raw: string) => {
|
|
const next = raw ? clampToPeriod(raw, periodStart, periodEnd) : undefined
|
|
onChange({ fromDate: next, toDate: value.toDate })
|
|
}
|
|
const handleToChange = (raw: string) => {
|
|
const next = raw ? clampToPeriod(raw, periodStart, periodEnd) : undefined
|
|
onChange({ fromDate: value.fromDate, toDate: next })
|
|
}
|
|
|
|
const presetLabels: Record<Preset, string> = useMemo(
|
|
() => ({
|
|
full_year: t('date_range_preset_full_year'),
|
|
ytd: t('date_range_preset_ytd'),
|
|
this_month: t('date_range_preset_this_month'),
|
|
last_month: t('date_range_preset_last_month'),
|
|
this_quarter: t('date_range_preset_this_quarter'),
|
|
custom: t('date_range_preset_custom'),
|
|
}),
|
|
[t],
|
|
)
|
|
|
|
return (
|
|
<div className={cn('flex flex-col gap-2', className)}>
|
|
<Label className="text-xs font-medium uppercase tracking-wider text-muted-foreground">
|
|
{t('date_range_label')}
|
|
</Label>
|
|
<div className="flex flex-wrap items-center gap-1.5">
|
|
{PRESETS.map((p) => {
|
|
const active = preset === p
|
|
return (
|
|
<button
|
|
key={p}
|
|
type="button"
|
|
onClick={() => handlePreset(p)}
|
|
className={cn(
|
|
'px-3 py-1.5 text-xs rounded-md border transition-colors duration-150',
|
|
active
|
|
? 'bg-secondary border-border text-foreground'
|
|
: 'bg-transparent border-border text-muted-foreground hover:bg-secondary/60 hover:text-foreground',
|
|
)}
|
|
>
|
|
{presetLabels[p]}
|
|
</button>
|
|
)
|
|
})}
|
|
</div>
|
|
{preset === 'custom' && (
|
|
<div className="flex flex-wrap items-end gap-3 mt-1">
|
|
<div className="flex flex-col gap-1">
|
|
<Label className="text-xs text-muted-foreground">{t('date_range_from')}</Label>
|
|
<Input
|
|
type="date"
|
|
min={periodStart}
|
|
max={periodEnd}
|
|
value={value.fromDate ?? ''}
|
|
onChange={(e) => handleFromChange(e.target.value)}
|
|
className="w-[160px] tabular-nums"
|
|
/>
|
|
</div>
|
|
<div className="flex flex-col gap-1">
|
|
<Label className="text-xs text-muted-foreground">{t('date_range_to')}</Label>
|
|
<Input
|
|
type="date"
|
|
min={periodStart}
|
|
max={periodEnd}
|
|
value={value.toDate ?? ''}
|
|
onChange={(e) => handleToChange(e.target.value)}
|
|
className="w-[160px] tabular-nums"
|
|
/>
|
|
</div>
|
|
</div>
|
|
)}
|
|
</div>
|
|
)
|
|
}
|