Files
accounted/lib/reports/journal-register.ts
T
Jakob WennbergandClaude Fable 5 a8801430f4 fix(reports): stop driving report queries from the unfiltered journal_entry_lines side (#971)
* fix(reports): drive report line queries from journal_entries, not the unfiltered lines side

Every report generator fetched journal_entry_lines with a
journal_entries!inner(...) embed and put the tenant filter on the
embedded side (.eq('journal_entries.company_id', ...)). PostgREST
compiles that to a correlated INNER JOIN LATERAL with a parameterized
LIMIT inside, which blocks join reordering: Postgres walked the ENTIRE
journal_entry_lines table (603k rows, all tenants) per report query.
Measured in production: 13.6 s vs 2.7 ms for the equivalent plain join,
against Supabase's 8 s statement_timeout; nightly cloud backups failed
for 5 of 11 companies on 2026-07-09 and a GL report 500'd.

Introduce lib/bookkeeping/entry-lines.ts with a shared two-step fetch:

1. fetch matching journal_entries (id + caller-selected columns)
   filtered by company_id / fiscal_period_id / status / entry_date /
   source_type, paginated via fetchAllRows;
2. fetch journal_entry_lines with .in('journal_entry_id', chunk) in
   chunks of 100 ids (URL-length safety), paginated per chunk;
3. reattach the parent entry to each line under the embed's key shape
   (line.journal_entries = {...}, aliasable) and sort lines by id
   ascending to preserve the old .order('id') semantics.

Converted call sites (selected columns and filters preserved):
trial-balance (x2), general-ledger, journal-register, sie-export
(reuses its existing entry list via fetchLinesByEntryIds),
vat-declaration, dimension-pnl, opening-balances, monthly-breakdown,
periodisk-sammanstallning, rc-basis-gaps (sibling-line fetch now also
chunked), ar-reconciliation, supplier-reconciliation,
bank-reconciliation, asset-service (x2), bolagsskatt-calculator,
sarskild-loneskatt-calculator.

Tests: unit tests for the helper (chunk size, reattachment shape,
forced id/journal_entry_id columns, empty result, cross-chunk sort,
error propagation); existing report/reconciliation/bokslut test mocks
updated to the two-step query shape, preserving every assertion about
report output.

From the 2026-07-09 production log triage.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(reports): stop echoing raw error messages from the general-ledger route

The catch handler returned err.message to the client in
details.reason; internal error strings (SQL fragments, table names,
timeout messages) must not reach the browser. The error is already
logged server-side with the request id, so the client envelope keeps
only the REPORT_GENERATION_FAILED code.

From the 2026-07-09 production log triage.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-07-10 11:03:28 +02:00

175 lines
5.8 KiB
TypeScript

import type { SupabaseClient } from '@supabase/supabase-js'
import { fetchAllRows } from '@/lib/supabase/fetch-all'
import { fetchEntryLines, type EntryLinesQuery } from '@/lib/bookkeeping/entry-lines'
export interface JournalRegisterLine {
account_number: string
account_name: string
debit: number
credit: number
}
export interface JournalRegisterEntry {
voucher_series: string
voucher_number: number
date: string
description: string
source_type: string
status: string
lines: JournalRegisterLine[]
total_debit: number
total_credit: number
}
export interface JournalRegisterReport {
entries: JournalRegisterEntry[]
total_entries: number
total_debit: number
total_credit: number
period: { start: string; end: string }
}
/**
* Generate journal register (grundbok) for a fiscal period.
* BFL 5 kap. 1 §: registreringsordning: all vouchers in chronological registration order.
*
* Uses the shared two-step entry-lines fetch (lib/bookkeeping/entry-lines.ts):
* entries first, then lines chunked by entry id, both paginated, so any
* number of entries is handled without the pathological journal_entries!inner
* embed plan.
*
* Unlike the general ledger and trial balance, the grundbok includes ALL
* entries: the opening_balance_entry is NOT excluded, because it is a
* real voucher that should appear in registration order.
*/
export async function generateJournalRegister(
supabase: SupabaseClient,
companyId: string,
periodId: string
): Promise<JournalRegisterReport> {
// Get fiscal period dates
const { data: period } = await supabase
.from('fiscal_periods')
.select('period_start, period_end')
.eq('id', periodId)
.eq('company_id', companyId)
.single()
if (!period) {
return { entries: [], total_entries: 0, total_debit: 0, total_credit: 0, period: { start: '', end: '' } }
}
// Fetch entries and their lines via the two-step entry-lines fetch: both
// sides paginated, lines chunked by entry id, no truncation at 1000 rows.
const rawLines = await fetchEntryLines<{
id: string
account_number: string
debit_amount: number
credit_amount: number
journal_entry_id: string
journal_entries: {
id: string
entry_date: string
voucher_number: number
voucher_series: string
description: string
source_type: string
status: string
}
}>({
supabase,
entryColumns:
'id, entry_date, voucher_number, voucher_series, description, source_type, status, company_id, fiscal_period_id',
lineColumns: 'id, account_number, debit_amount, credit_amount, journal_entry_id',
filterEntries: (q: EntryLinesQuery) =>
q
.eq('company_id', companyId)
.eq('fiscal_period_id', periodId)
.in('status', ['posted', 'reversed']),
})
if (rawLines.length === 0) {
return { entries: [], total_entries: 0, total_debit: 0, total_credit: 0, period: { start: period.period_start, end: period.period_end } }
}
// Fetch account names
const accounts = await fetchAllRows<{ account_number: string; account_name: string }>(({ from, to }) =>
supabase
.from('chart_of_accounts')
.select('account_number, account_name')
.eq('company_id', companyId)
.order('account_number', { ascending: true })
.range(from, to)
)
const accountNameMap = new Map<string, string>()
for (const acc of accounts) {
accountNameMap.set(acc.account_number, acc.account_name)
}
// Extract unique entries and group lines by entry
const entryMap = new Map<string, typeof rawLines[0]['journal_entries']>()
const linesByEntry = new Map<string, JournalRegisterLine[]>()
for (const line of rawLines) {
const entryId = line.journal_entry_id
const entry = line.journal_entries
if (!entryMap.has(entryId)) {
entryMap.set(entryId, entry)
}
if (!linesByEntry.has(entryId)) {
linesByEntry.set(entryId, [])
}
linesByEntry.get(entryId)!.push({
account_number: line.account_number,
account_name: accountNameMap.get(line.account_number) || `Konto ${line.account_number}`,
debit: Math.round((Number(line.debit_amount) || 0) * 100) / 100,
credit: Math.round((Number(line.credit_amount) || 0) * 100) / 100,
})
}
// Build entries sorted by voucher_series, then voucher_number (registration order)
const sortedEntries = Array.from(entryMap.entries())
.sort(([, a], [, b]) => {
const seriesCompare = (a.voucher_series || 'A').localeCompare(b.voucher_series || 'A')
if (seriesCompare !== 0) return seriesCompare
return a.voucher_number - b.voucher_number
})
const result: JournalRegisterEntry[] = sortedEntries.map(([entryId, entry]) => {
const entryLines = linesByEntry.get(entryId) || []
// Sort lines by account number within each entry
entryLines.sort((a, b) => a.account_number.localeCompare(b.account_number))
const totalDebit = entryLines.reduce((sum, l) => sum + l.debit, 0)
const totalCredit = entryLines.reduce((sum, l) => sum + l.credit, 0)
return {
voucher_series: entry.voucher_series || 'A',
voucher_number: entry.voucher_number,
date: entry.entry_date,
description: entry.description || '',
source_type: entry.source_type || '',
status: entry.status,
lines: entryLines,
total_debit: Math.round(totalDebit * 100) / 100,
total_credit: Math.round(totalCredit * 100) / 100,
}
})
const grandTotalDebit = result.reduce((sum, e) => sum + e.total_debit, 0)
const grandTotalCredit = result.reduce((sum, e) => sum + e.total_credit, 0)
return {
entries: result,
total_entries: result.length,
total_debit: Math.round(grandTotalDebit * 100) / 100,
total_credit: Math.round(grandTotalCredit * 100) / 100,
period: { start: period.period_start, end: period.period_end },
}
}