Files
MattssonandClaude Fable 5.1 9a8291f454 feat(reports): list a booked 8999 in Resultatrapport instead of hiding it (#2457)
* feat(reports): list a booked 8999 in Resultatrapport instead of hiding it

Resultatrapport and dimension-pnl filtered account 8999 out and printed a
computed result row, so a user who books or imports the omföring of årets
resultat by hand saw huvudboken and the account-level report disagree.

Why it occurred: the filter was copied from the formal Resultaträkning,
where it is right (ÅRL's uppställningsform has no 8999 line). In the
operational report it hid a real balance. Our own bokslut verifikat never
posts 8999 (it zeroes each P&L account straight against 2099), so the only
8999 balances that exist are manual or SIE-imported ones, exactly the case
the report suppressed.

What was removed: the exclusion itself, in both operational reports, so
they keep reconciling. The XLSX bottom row is renamed to "Beräknat resultat"
to match the UI and PDF. Beräknat resultat now reads zero after such an
omföring, the Fortnox/Visma resultatrapport convention.

Why this and not the proposed shape: the user asked about Resultaträkning,
which stays as is on purpose. The bigger version (Stage 2 of #1051, showing
the bokslut verifikat via exclude-final) would zero every row of a closed
year given our closing-entry shape and is a separate decision; recorded in
DECISIONS.md.

Fixes #2455

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0131jmfXGzSdyjaQoGiCoo1t

* test(reports): pin the deliberate 8999 gap between Resultatrapport and Resultaträkning

The cross-surface agreement test claimed the two operational reports are
identical; after #2455 they differ by exactly a booked 8999 omföring, and
the fixture had no such row so the invariant went silently false. Pin the
gap explicitly, and note in DECISIONS.md that this supersedes the
2026-07-29 same-profit line.

Refs #2455

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0131jmfXGzSdyjaQoGiCoo1t

---------

Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
2026-09-09 13:57:38 +02:00

291 lines
11 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { describe, it, expect, vi, beforeEach } from 'vitest'
import type { TrialBalanceRow } from '@/types'
// ============================================================
// Resultat per projekt/kostnadsställe (dimensions PR4).
//
// generateTrialBalance is mocked (post-processor pattern, like
// resultatrapport.test.ts); the registry + tagged-line queries use a
// table-keyed FIFO mock.
// ============================================================
vi.mock('../trial-balance', () => ({
generateTrialBalance: vi.fn(),
}))
type MockResult = { data?: unknown; error?: unknown }
let mockResults: Record<string, MockResult[]>
function makeBuilder(tableName: string) {
const b: Record<string, unknown> = {}
for (const m of ['select', 'eq', 'in', 'lt', 'lte', 'gte', 'neq', 'not', 'contains', 'order', 'range']) {
b[m] = vi.fn().mockReturnValue(b)
}
const consume = (): MockResult => {
const queue = mockResults[tableName]
if (!queue || queue.length === 0) {
// The two-step entry-lines fetch (lib/bookkeeping/entry-lines.ts) reads
// journal_entries before journal_entry_lines. Tests queue line rows
// directly, so default the entries step to one generic entry.
if (tableName === 'journal_entries') {
return { data: [{ id: 'entry-1' }], error: null }
}
return { data: null, error: null }
}
return queue.shift()!
}
b.single = vi.fn().mockImplementation(async () => consume())
b.maybeSingle = vi.fn().mockImplementation(async () => consume())
b.then = (resolve: (v: unknown) => void) => resolve(consume())
return b
}
function makeClient() {
return {
from: vi.fn().mockImplementation((table: string) => makeBuilder(table)),
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any
}
import { generateDimensionPnl } from '../dimension-pnl'
import { generateTrialBalance } from '../trial-balance'
const mockTrialBalance = vi.mocked(generateTrialBalance)
function tbRow(partial: Partial<TrialBalanceRow>): TrialBalanceRow {
return {
account_number: '3001',
account_name: 'Försäljning',
account_class: 3,
opening_debit: 0,
opening_credit: 0,
period_debit: 0,
period_credit: 0,
closing_debit: 0,
closing_credit: 0,
...partial,
}
}
function tb(rows: TrialBalanceRow[]) {
return { rows, totalDebit: 0, totalCredit: 0, isBalanced: true }
}
let supabase: ReturnType<typeof makeClient>
beforeEach(() => {
vi.clearAllMocks()
mockResults = {}
supabase = makeClient()
})
const PERIOD = { period_start: '2026-01-01', period_end: '2026-12-31' }
describe('generateDimensionPnl', () => {
it('builds the value-as-column matrix with an untagged residual that reconciles to the trial balance', async () => {
mockResults = {
fiscal_periods: [{ data: PERIOD, error: null }],
dimensions: [
{ data: { id: 'dim-6', sie_dim_no: 6, name: 'Projekt' }, error: null },
],
dimension_values: [
{
data: [
{ code: 'P001', name: 'Villa Almgren' },
{ code: 'P002', name: 'Kontorsbygget' },
],
error: null,
},
],
journal_entry_lines: [
{
data: [
{ id: 'l1', account_number: '3001', debit_amount: 0, credit_amount: 600, dimensions: { '6': 'P001' } },
{ id: 'l2', account_number: '3001', debit_amount: 0, credit_amount: 300, dimensions: { '6': 'P002' } },
{ id: 'l3', account_number: '4010', debit_amount: 400, credit_amount: 0, dimensions: { '6': 'P001' } },
// Balance-account line: outside the P&L scope, must be ignored.
{ id: 'l4', account_number: '1930', debit_amount: 0, credit_amount: 900, dimensions: { '6': 'P001' } },
],
error: null,
},
],
}
mockTrialBalance.mockResolvedValue(
tb([
// 3001: 1000 total credit: only 900 of it is tagged → 100 untagged.
tbRow({ account_number: '3001', account_class: 3, closing_credit: 1000 }),
tbRow({ account_number: '4010', account_name: 'Inköp', account_class: 4, closing_debit: 400 }),
tbRow({ account_number: '1930', account_name: 'Bank', account_class: 1, closing_debit: 900 }),
]),
)
const report = await generateDimensionPnl(supabase, 'company-1', 'period-1', '6')
expect(report.dimension).toEqual({ sie_dim_no: '6', name: 'Projekt' })
expect(report.columns).toEqual([
{ code: 'P001', name: 'Villa Almgren' },
{ code: 'P002', name: 'Kontorsbygget' },
{ code: null, name: null }, // (Utan dimension)
])
const revenue = report.groups.find((g) => g.class === 3)!
expect(revenue.rows).toEqual([
{ account_number: '3001', account_name: 'Försäljning', values: [600, 300, 100], total: 1000 },
])
const costs = report.groups.find((g) => g.class === 4)!
expect(costs.rows).toEqual([
{ account_number: '4010', account_name: 'Inköp', values: [-400, 0, 0], total: -400 },
])
// Every row sums exactly to its Totalt (reconciliation by construction).
for (const g of report.groups) {
for (const r of g.rows) {
expect(r.values.reduce((s, v) => s + v, 0)).toBeCloseTo(r.total, 10)
}
}
expect(report.net_per_column).toEqual([200, 300, 100])
// net_total = resultatrapport semantics over classes 3-8:
// +1000 (3001) − 400 (4010) = 600. 1930 (class 1) excluded.
expect(report.net_total).toBe(600)
expect(report.period).toEqual({ start: '2026-01-01', end: '2026-12-31' })
})
it('lists a booked 8999 in the untagged bucket and nets it into net_total (#2455)', async () => {
mockResults = {
fiscal_periods: [{ data: PERIOD, error: null }],
dimensions: [{ data: { id: 'dim-6', sie_dim_no: 6, name: 'Projekt' }, error: null }],
dimension_values: [{ data: [{ code: 'P001', name: 'Villa Almgren' }], error: null }],
journal_entry_lines: [
{
data: [
{ id: 'l1', account_number: '3001', debit_amount: 0, credit_amount: 1000, dimensions: { '6': 'P001' } },
],
error: null,
},
],
}
mockTrialBalance.mockResolvedValue(
tb([
tbRow({ account_number: '3001', account_class: 3, closing_credit: 1000 }),
// Manual omföring of årets resultat: 8999 debit, never dimension-tagged.
tbRow({ account_number: '8999', account_name: 'Årets resultat', account_class: 8, closing_debit: 1000 }),
]),
)
const report = await generateDimensionPnl(supabase, 'company-1', 'period-1', '6')
const row8999 = report.groups.flatMap((g) => g.rows).find((r) => r.account_number === '8999')
expect(row8999?.total).toBe(-1000)
// Same scope as resultatrapport: the omföring zeroes the result.
expect(report.net_total).toBe(0)
expect(report.net_per_column).toEqual([1000, -1000])
})
it('drops the untagged column when every krona is tagged', async () => {
mockResults = {
fiscal_periods: [{ data: PERIOD, error: null }],
dimensions: [{ data: { id: 'dim-6', sie_dim_no: 6, name: 'Projekt' }, error: null }],
dimension_values: [{ data: [{ code: 'P001', name: 'Villa Almgren' }], error: null }],
journal_entry_lines: [
{
data: [
{ id: 'l1', account_number: '3001', debit_amount: 0, credit_amount: 1000, dimensions: { '6': 'P001' } },
],
error: null,
},
],
}
mockTrialBalance.mockResolvedValue(
tb([tbRow({ account_number: '3001', account_class: 3, closing_credit: 1000 })]),
)
const report = await generateDimensionPnl(supabase, 'company-1', 'period-1', '6')
expect(report.columns).toEqual([{ code: 'P001', name: 'Villa Almgren' }])
expect(report.groups[0].rows[0].values).toEqual([1000])
expect(report.net_per_column).toEqual([1000])
expect(report.net_total).toBe(1000)
})
it('falls back to seeded dimension names when the registry has no row', async () => {
mockResults = {
fiscal_periods: [{ data: PERIOD, error: null }],
dimensions: [{ data: null, error: null }],
journal_entry_lines: [
{
data: [
{ id: 'l1', account_number: '3001', debit_amount: 0, credit_amount: 100, dimensions: { '1': 'KS01' } },
],
error: null,
},
],
}
mockTrialBalance.mockResolvedValue(
tb([tbRow({ account_number: '3001', account_class: 3, closing_credit: 100 })]),
)
const report = await generateDimensionPnl(supabase, 'company-1', 'period-1', '1')
expect(report.dimension.name).toBe('Kostnadsställe')
// Code column without a registry name.
expect(report.columns[0]).toEqual({ code: 'KS01', name: null })
})
it('passes the caller date range to the trial balance (Totalt parity with resultatrapport)', async () => {
mockResults = {
fiscal_periods: [{ data: PERIOD, error: null }],
dimensions: [{ data: null, error: null }],
journal_entry_lines: [{ data: [], error: null }],
}
mockTrialBalance.mockResolvedValue(tb([]))
const report = await generateDimensionPnl(supabase, 'company-1', 'period-1', '6', {
toDate: '2026-06-30',
})
expect(mockTrialBalance).toHaveBeenCalledWith(supabase, 'company-1', 'period-1', {
closingEntry: 'exclude-all-year-end',
toDate: '2026-06-30',
})
// The label reflects actual coverage: cumulative from period_start.
expect(report.period).toEqual({ start: '2026-01-01', end: '2026-06-30' })
})
it('handles fully untagged periods: one residual column carrying the whole result', async () => {
mockResults = {
fiscal_periods: [{ data: PERIOD, error: null }],
dimensions: [{ data: { id: 'dim-6', sie_dim_no: 6, name: 'Projekt' }, error: null }],
dimension_values: [{ data: [], error: null }],
journal_entry_lines: [{ data: [], error: null }],
}
mockTrialBalance.mockResolvedValue(
tb([
tbRow({ account_number: '3001', account_class: 3, closing_credit: 1000 }),
tbRow({ account_number: '4010', account_name: 'Inköp', account_class: 4, closing_debit: 250 }),
]),
)
const report = await generateDimensionPnl(supabase, 'company-1', 'period-1', '6')
expect(report.columns).toEqual([{ code: null, name: null }])
expect(report.groups.find((g) => g.class === 3)?.rows[0].values).toEqual([1000])
expect(report.groups.find((g) => g.class === 4)?.rows[0].values).toEqual([-250])
expect(report.net_per_column).toEqual([750])
expect(report.net_total).toBe(750)
})
it('rejects a non-numeric dimension number (PostgREST path guard)', async () => {
await expect(
generateDimensionPnl(supabase, 'company-1', 'period-1', '6,is.null'),
).rejects.toThrow('positive SIE dimension number')
})
it('throws when the fiscal period does not exist', async () => {
mockResults = { fiscal_periods: [{ data: null, error: null }] }
await expect(generateDimensionPnl(supabase, 'company-1', 'missing', '6')).rejects.toThrow(
'Fiscal period not found',
)
})
})