Files
accounted/lib/reports/__tests__/trial-balance.test.ts
T
Jakob WennbergandClaude Opus 4.6 91e2c1705a feat: per-line VAT, invoice document types, ledger-based VAT declaration, bank reconciliation, and pagination
Per-line VAT rates:
- Add generatePerRateLines() to group invoice items by vat_rate with separate
  revenue + VAT lines per rate group (invoice-entries.ts)
- Add getAvailableVatRates() and getVatTreatmentForRate() (vat-rules.ts)
- PDF template shows per-line VAT column and per-rate totals for mixed-rate invoices
- Invoice create/review UI supports per-line rate selection
- Types: add vat_rate/vat_amount to InvoiceItem, vat_rate to CreateInvoiceItemInput

Invoice document types (proforma, delivery note):
- Add InvoiceDocumentType, document_type and converted_from_id to Invoice type
- PDF hides prices for delivery notes, adds proforma notice
- Email templates support all document types
- mark-paid skips journal entries for non-invoice document types
- Migration 031: invoice_document_type

Accounting method support:
- Add AccountingMethod type (accrual/cash)
- Migration 032: add_accounting_method column to company_settings

VAT declaration rewrite:
- Rewrite to read directly from general ledger (26xx/3xxx account lines)
  instead of aggregating invoices/transactions/receipts
- ACCOUNT_RUTA mapping drives momsdeklaration boxes from GL balances

Bank reconciliation:
- Transaction ingest now pre-fetches unlinked GL lines and attempts
  auto-reconciliation during import
- Add transaction.reconciled event type
- Add ReconciliationMethod type and reconciliation_method on Transaction
- Migration 030: bank_reconciliation
- New reconciliation engine, API routes, and BankReconciliationView component

Pagination (fetchAllRows):
- New lib/supabase/fetch-all.ts overcomes PostgREST 1000-row limit
- Adopted in all report generators, SIE/SRU export, account list APIs

Fiscal period validation:
- New validate-period-duration.ts enforces max 18 months per BFL 3 kap.
- Applied in period-service.ts and fiscal-periods API

Account mapper simplification:
- Remove Levenshtein/fuzzy matching, use exact account number match only

Swedbank parser improvements:
- Support abbreviated headers (Clnr, Bokfdag, Radnr)
- Use Referens column as counterparty

Chart of accounts management:
- Add DELETE endpoint with system account and usage protection
- PUT uses partial updates
- New AccountCombobox, AddAccountDialog, EditAccountDialog, ChartOfAccountsManager

Tax deadline corrections:
- Rewrite inkomstdeklaration_ab using Skatteverket lookup table
- Rewrite arsredovisning deadline to 7 months after FY end per ÅRL 8:3

Onboarding first fiscal year:
- Add first fiscal year toggle with date pickers and 18-month validation

UI terminology:
- Change "okategoriserad/kategorisera" to "obokförd/bokföra" throughout

Report column fix:
- Fix start_date/end_date to period_start/period_end in report queries

Supplier invoice input:
- CreateSupplierInvoiceItemInput uses amount field (legacy quantity/unit_price kept)

Misc:
- SIE import uses upsert for idempotent account creation
- account-descriptions.ts falls back to BAS reference data
- Add invoice_default_notes to CompanySettings
- Update CLAUDE.md to reflect current project state

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-21 14:57:15 +01:00

316 lines
9.4 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from 'vitest'
// ============================================================
// Mock — sequential result queue with rpc support
// ============================================================
let resultIdx: number
let results: Array<{ data?: unknown; error?: unknown }>
function makeBuilder() {
const b: Record<string, unknown> = {}
for (const m of ['select', 'eq', 'in', 'range']) {
b[m] = vi.fn().mockReturnValue(b)
}
b.single = vi.fn().mockImplementation(async () => results[resultIdx++] ?? { data: null, error: null })
b.then = (resolve: (v: unknown) => void) => resolve(results[resultIdx++] ?? { data: null, error: null })
return b
}
function makeClient() {
return {
from: vi.fn().mockImplementation(() => makeBuilder()),
rpc: vi.fn().mockImplementation(async () => results[resultIdx++] ?? { data: null, error: null }),
}
}
vi.mock('@/lib/supabase/server', () => ({
createClient: vi.fn(async () => makeClient()),
}))
import { generateTrialBalance } from '../trial-balance'
beforeEach(() => {
vi.clearAllMocks()
resultIdx = 0
results = []
})
describe('generateTrialBalance', () => {
it('returns rows directly from RPC when successful', async () => {
results = [
// 0: rpc('generate_trial_balance')
{
data: [
{
account_number: '1930',
account_name: 'Företagskonto',
account_class: 1,
opening_debit: 0,
opening_credit: 0,
period_debit: 5000,
period_credit: 0,
closing_debit: 5000,
closing_credit: 0,
},
{
account_number: '3001',
account_name: 'Försäljning 25%',
account_class: 3,
opening_debit: 0,
opening_credit: 0,
period_debit: 0,
period_credit: 5000,
closing_debit: 0,
closing_credit: 5000,
},
],
error: null,
},
]
const result = await generateTrialBalance('user-1', 'period-1')
expect(result.rows).toHaveLength(2)
expect(result.totalDebit).toBe(5000)
expect(result.totalCredit).toBe(5000)
expect(result.isBalanced).toBe(true)
})
it('falls back to manual aggregation when RPC returns error', async () => {
results = [
// 0: rpc fails
{ data: null, error: { message: 'function not found' } },
// 1: manual — journal_entries
{
data: [{ id: 'e1' }],
error: null,
},
// 2: manual — journal_entry_lines
{
data: [
{ account_number: '1930', debit_amount: 1000, credit_amount: 0 },
{ account_number: '3001', debit_amount: 0, credit_amount: 1000 },
],
error: null,
},
// 3: manual — chart_of_accounts
{
data: [
{ account_number: '1930', account_name: 'Företagskonto', account_class: 1 },
{ account_number: '3001', account_name: 'Försäljning 25%', account_class: 3 },
],
error: null,
},
]
const result = await generateTrialBalance('user-1', 'period-1')
expect(result.rows).toHaveLength(2)
expect(result.totalDebit).toBe(1000)
expect(result.totalCredit).toBe(1000)
expect(result.isBalanced).toBe(true)
})
it('falls back to manual aggregation when RPC returns null data', async () => {
results = [
// 0: rpc succeeds but data is null
{ data: null, error: null },
// 1: manual — journal_entries (empty)
{ data: [], error: null },
]
const result = await generateTrialBalance('user-1', 'period-1')
expect(result.rows).toEqual([])
expect(result.isBalanced).toBe(true)
})
it('returns empty report when no entries exist (manual path)', async () => {
results = [
// 0: rpc fails
{ data: null, error: { message: 'error' } },
// 1: manual — journal_entries empty
{ data: [], error: null },
]
const result = await generateTrialBalance('user-1', 'period-1')
expect(result.rows).toEqual([])
expect(result.totalDebit).toBe(0)
expect(result.totalCredit).toBe(0)
expect(result.isBalanced).toBe(true)
})
it('aggregates lines by account and sorts by account_number', async () => {
results = [
// 0: rpc fails
{ data: null, error: { message: 'error' } },
// 1: journal_entries
{ data: [{ id: 'e1' }, { id: 'e2' }], error: null },
// 2: journal_entry_lines — multiple lines per account
{
data: [
{ account_number: '3001', debit_amount: 0, credit_amount: 500 },
{ account_number: '1930', debit_amount: 300, credit_amount: 0 },
{ account_number: '3001', debit_amount: 0, credit_amount: 200 },
{ account_number: '1930', debit_amount: 450, credit_amount: 0 },
],
error: null,
},
// 3: chart_of_accounts
{
data: [
{ account_number: '1930', account_name: 'Företagskonto', account_class: 1 },
{ account_number: '3001', account_name: 'Försäljning', account_class: 3 },
],
error: null,
},
]
const result = await generateTrialBalance('user-1', 'period-1')
expect(result.rows).toHaveLength(2)
// Sorted by account number
expect(result.rows[0].account_number).toBe('1930')
expect(result.rows[1].account_number).toBe('3001')
// Aggregated correctly
expect(result.rows[0].closing_debit).toBe(750)
expect(result.rows[0].closing_credit).toBe(0)
expect(result.rows[1].closing_debit).toBe(0)
expect(result.rows[1].closing_credit).toBe(700)
})
it('falls back to "Konto {number}" when account not in chart_of_accounts', async () => {
results = [
// 0: rpc fails
{ data: null, error: { message: 'error' } },
// 1: journal_entries
{ data: [{ id: 'e1' }], error: null },
// 2: journal_entry_lines
{
data: [
{ account_number: '9999', debit_amount: 100, credit_amount: 0 },
],
error: null,
},
// 3: chart_of_accounts — empty
{ data: [], error: null },
]
const result = await generateTrialBalance('user-1', 'period-1')
expect(result.rows[0].account_name).toBe('Konto 9999')
})
it('derives account_class from first digit when account not in chart', async () => {
results = [
// 0: rpc fails
{ data: null, error: { message: 'error' } },
// 1: journal_entries
{ data: [{ id: 'e1' }], error: null },
// 2: journal_entry_lines
{
data: [
{ account_number: '5410', debit_amount: 200, credit_amount: 0 },
],
error: null,
},
// 3: chart_of_accounts — empty
{ data: [], error: null },
]
const result = await generateTrialBalance('user-1', 'period-1')
expect(result.rows[0].account_class).toBe(5)
})
it('uses Math.round for monetary precision', async () => {
results = [
// 0: rpc fails
{ data: null, error: { message: 'error' } },
// 1: journal_entries
{ data: [{ id: 'e1' }], error: null },
// 2: journal_entry_lines — values that cause floating point issues
{
data: [
{ account_number: '1930', debit_amount: 33.33, credit_amount: 0 },
{ account_number: '1930', debit_amount: 33.33, credit_amount: 0 },
{ account_number: '1930', debit_amount: 33.34, credit_amount: 0 },
{ account_number: '3001', debit_amount: 0, credit_amount: 100 },
],
error: null,
},
// 3: chart_of_accounts
{
data: [
{ account_number: '1930', account_name: 'Bank', account_class: 1 },
{ account_number: '3001', account_name: 'Revenue', account_class: 3 },
],
error: null,
},
]
const result = await generateTrialBalance('user-1', 'period-1')
expect(result.rows[0].closing_debit).toBe(100)
expect(result.totalDebit).toBe(100)
expect(result.totalCredit).toBe(100)
expect(result.isBalanced).toBe(true)
})
it('detects unbalanced entries (isBalanced=false)', async () => {
results = [
// 0: rpc succeeds with unbalanced data
{
data: [
{
account_number: '1930',
account_name: 'Bank',
account_class: 1,
opening_debit: 0,
opening_credit: 0,
period_debit: 1000,
period_credit: 0,
closing_debit: 1000,
closing_credit: 0,
},
{
account_number: '3001',
account_name: 'Revenue',
account_class: 3,
opening_debit: 0,
opening_credit: 0,
period_debit: 0,
period_credit: 999,
closing_debit: 0,
closing_credit: 999,
},
],
error: null,
},
]
const result = await generateTrialBalance('user-1', 'period-1')
expect(result.totalDebit).toBe(1000)
expect(result.totalCredit).toBe(999)
expect(result.isBalanced).toBe(false)
})
it('returns empty when entries query errors (manual path)', async () => {
results = [
// 0: rpc fails
{ data: null, error: { message: 'error' } },
// 1: journal_entries query errors
{ data: null, error: { message: 'DB error' } },
]
const result = await generateTrialBalance('user-1', 'period-1')
expect(result.rows).toEqual([])
expect(result.isBalanced).toBe(true)
})
})