Files
accounted/lib/reports/__tests__/rc-basis-gaps.test.ts
T
Jakob Wennberg 98886e68d8 fix(vat): keep the RC-basis worklist visible until every voucher is fixed (#1164)
Correcting a single voucher cleared the momsdeklaration's RC_BASIS_MISSING
error and the whole per-voucher worklist with it: the check tested mere
presence of ruta 20-24 basis, the stepper re-derived its landing step and
yanked the user to Granska mid-work, and the remounted checks card never
refetched gaps once the aggregate check stopped firing. The declaration
then claimed "klart" while the remaining vouchers still under-reported
rutor 20-24 (FK004).

- Make RC_BASIS_MISSING/RC_OUTPUT_MISSING proportional: compare reported
  basis against the basis the per-rate output boxes imply (moms/sats),
  with a 0.5% + 1 kr tolerance for per-voucher ore rounding.
- Fetch the rc-basis-gaps worklist once per period, ungated from the
  aggregate check, so remaining rows survive remounts.
- Latch the automatic stepper landing once per period so a refetch after
  a korrigering cannot navigate the user off Kontrollera.
- Resolve rc-basis-gaps against the rakenskapsar (fiscal_period_id) for
  helarsmoms, matching the declaration totals; a calendar span hid gap
  vouchers in the tail of an extended first year.

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-07-24 22:10:20 +02:00

128 lines
4.0 KiB
TypeScript

/**
* Tests for findRcBasisGaps: per-voucher FK004 detection.
*
* Mocks the entry-lines fetch layer and resolvePeriodDates. The period must
* resolve through resolvePeriodDates (not the calendar arithmetic) so yearly
* (helårsmoms) worklists cover extended/broken räkenskapsår: a calendar span
* hid gap vouchers that the declaration totals still included.
*/
import { describe, it, expect, vi, beforeEach } from 'vitest'
import type { SupabaseClient } from '@supabase/supabase-js'
const resolvePeriodDatesMock = vi.fn()
vi.mock('../vat-declaration', () => ({
resolvePeriodDates: (...args: unknown[]) => resolvePeriodDatesMock(...args),
}))
const fetchEntryLinesMock = vi.fn()
const fetchLinesByEntryIdsMock = vi.fn()
vi.mock('@/lib/bookkeeping/entry-lines', () => ({
fetchEntryLines: (...args: unknown[]) => fetchEntryLinesMock(...args),
fetchLinesByEntryIds: (...args: unknown[]) => fetchLinesByEntryIdsMock(...args),
}))
import { findRcBasisGaps } from '../rc-basis-gaps'
const supabase = {} as SupabaseClient
function rcLine(entryId: string, voucherNumber: number, credit: number) {
return {
journal_entry_id: entryId,
account_number: '2614',
debit_amount: 0,
credit_amount: credit,
journal_entries: {
id: entryId,
voucher_number: voucherNumber,
voucher_series: 'A',
entry_date: '2026-05-20',
description: `Voucher ${voucherNumber}`,
},
}
}
describe('findRcBasisGaps', () => {
beforeEach(() => {
vi.clearAllMocks()
resolvePeriodDatesMock.mockResolvedValue({ start: '2025-07-17', end: '2026-12-31' })
fetchEntryLinesMock.mockResolvedValue([])
fetchLinesByEntryIdsMock.mockResolvedValue([])
})
it('resolves the period via resolvePeriodDates with the fiscal period id', async () => {
await findRcBasisGaps(supabase, 'company-1', 'yearly', 2026, 1, { fiscalPeriodId: 'fp-1' })
expect(resolvePeriodDatesMock).toHaveBeenCalledWith(
supabase, 'company-1', 'yearly', 2026, 1, 'fp-1',
)
})
it('filters entries on the resolved bounds, not the calendar year', async () => {
await findRcBasisGaps(supabase, 'company-1', 'yearly', 2026, 1, { fiscalPeriodId: 'fp-1' })
const { filterEntries } = fetchEntryLinesMock.mock.calls[0][0]
const calls: Array<[string, ...unknown[]]> = []
const q = new Proxy(
{},
{
get:
(_t, method: string) =>
(...args: unknown[]) => {
calls.push([method, ...args])
return q
},
},
)
filterEntries(q)
expect(calls).toContainEqual(['gte', 'entry_date', '2025-07-17'])
expect(calls).toContainEqual(['lte', 'entry_date', '2026-12-31'])
})
it('flags vouchers whose RC output VAT lacks a matching basis pair', async () => {
fetchEntryLinesMock.mockResolvedValue([
rcLine('entry-1', 8, 527.29), // no basis lines at all
rcLine('entry-2', 9, 250), // fully booked basis
])
fetchLinesByEntryIdsMock.mockResolvedValue([
{
id: 'l-1',
journal_entry_id: 'entry-2',
account_number: '4535',
debit_amount: 1000,
credit_amount: 0,
},
])
const gaps = await findRcBasisGaps(supabase, 'company-1', 'monthly', 2026, 5)
expect(gaps).toHaveLength(1)
expect(gaps[0]).toMatchObject({
entryId: 'entry-1',
voucherNumber: 8,
rcOutputAccount: '2614',
rcOutputAmount: 527.29,
expectedBasisAmount: 2109.16,
suggestedBasisAccount: '4535',
rate: 0.25,
})
})
it('flags a voucher whose basis is materially short of the expected amount', async () => {
fetchEntryLinesMock.mockResolvedValue([rcLine('entry-1', 8, 2500)])
fetchLinesByEntryIdsMock.mockResolvedValue([
{
id: 'l-1',
journal_entry_id: 'entry-1',
account_number: '4535',
debit_amount: 4000, // expected 10000
credit_amount: 0,
},
])
const gaps = await findRcBasisGaps(supabase, 'company-1', 'monthly', 2026, 5)
expect(gaps).toHaveLength(1)
expect(gaps[0].expectedBasisAmount).toBe(10000)
})
})