Files
accounted/lib/skatteverket/__tests__/bank-counterpart.test.ts
T
MattssonandClaude Opus 4.7 980f29dae8 Bug/momsdeklaration skv (#449)
* fix(salary): show birthdate in masked personnummer, hide the 4-digit suffix

Flip the personnummer display format from XXXXXXXX-NNNN to YYYYMMDD-XXXX so
the sensitive 4-digit suffix is hidden while the (public) birthdate stays
visible. Affects the employees list/detail, salary run, payslip PDF, payslip
email, and the MCP server tools (list_employees, get_salary_run). Each call
site now decrypts the stored personnummer before masking.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* fix(transactions): allow deleting unbooked transactions from "Alla transaktioner"

The history list only let users delete via the inbox card; once a category or
mall was picked but the verifikation hadn't been created, the row showed
"Ej bokförd" with no way to remove it. The API already permits delete while
journal_entry_id is null, so the gap was purely a missing UI affordance.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* fix(vat): populate ruta 20-24 for reverse charge + dishonest "Validera OK"

Three connected issues caused Skatteverket to reject momsdeklarationer
with FK004 even after our local "Validera"-knapp returned OK.

1. supplier-invoice-entries booked fiktiv moms (2614/2624/2634 + 2645/2647)
   on reverse-charge invoices but never the underlying basbelopp on 44xx/45xx.
   Ruta 30-32 filled up at SKV while ruta 20-24 stayed at 0 — SKV's FK004
   ("silent netting prohibited", ML 13 kap kräver båda sidor).

   Fix: generateReverseChargeBasisLines in vat-entries.ts emits parallel
   45xx/44xx debit + 4598 motkonto credit per rate group. Engine calls it
   from registration, cash, and credit-note paths. Skipped when the user
   booked the expense directly on a basis account to avoid double-counting.
   4598 added to BAS reference (no migration needed; account_number is
   plain text on journal_entry_lines).

2. rutorToMomsuppgift rounded each ruta independently but computed
   summaMoms from the unrounded ruta49. SKV recomputes the sum from
   integer rutor on their side, so fractional öres caused ±1 SEK drift
   and SKV rejected with FK009.

   Fix: derive summaMoms from the already-rounded VAT-amount rutor.

3. "Validera"-knappen only confirmed SKV's internal arithmetic — a
   declaration with ruta 30-32 populated and ruta 20-24 empty validated
   fine until /utkast hit FK004. Users got a false green light.

   Fix: vat-declaration-checks.ts runs locally before the SKV call,
   blocks Validera/Spara when ERROR-level findings exist, and surfaces
   them in a separate "Lokala kontroller"-section. Success message
   reworded so SKV's OK is no longer presented as filing-ready.

Tests: 4535/4536/4531/4425 lines + 4598 motkonto on EU/non-EU/byggtjänster
RC, credit-note reversal, fractional-öres summaMoms, all four pre-flight
codes (RC_BASIS_MISSING, RC_OUTPUT_MISSING, RC_INPUT_VAT_MISMATCH,
SUMMA_MOMS_DRIFT).

Backfill for already-posted entries follows in the next commit.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* feat: add skattekonto matching functionality

- Enhance TransactionInboxCard to display a warning for potential 1930↔1630 transfers.
- Implement match suggestions for skattekonto transactions in the backend.
- Create SkattekontoMatchDialog component for linking skattekonto rows to existing journal entries.
- Develop SkattekontoInboxCard component to handle skattekonto transactions in the inbox.
- Introduce skattekonto-match utility functions for candidate matching and linking.
- Update types to include match suggestions and enriched transaction responses.

* refactor: reorganize skattekonto types and implement bank counterpart matching logic

* docs: update CLAUDE.md to streamline integrations and clarify architecture details

* refactor: enhance reverse charge logic to handle non-basis accounts and prevent double-counting

---------

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-12 18:04:48 +02:00

144 lines
5.6 KiB
TypeScript

import { describe, it, expect } from 'vitest'
import {
findBankSkvCounterparts,
BANK_SKV_DATE_WINDOW_DAYS,
} from '../bank-counterpart'
import type { StoredSkattekontoTransaction } from '@/types/skatteverket'
function skv(
partial: Partial<StoredSkattekontoTransaction> &
Pick<StoredSkattekontoTransaction, 'id' | 'transaktionsdatum' | 'belopp_skatteverket'>,
): Pick<StoredSkattekontoTransaction, 'id' | 'transaktionsdatum' | 'belopp_skatteverket'> {
return partial
}
describe('findBankSkvCounterparts', () => {
it('pairs a -5000 bank outflow with a +5000 SKV inflow within window', () => {
const result = findBankSkvCounterparts({
bankRows: [{ id: 'bank-1', date: '2026-03-16', amount: -5000 }],
skvRows: [skv({ id: 'skv-1', transaktionsdatum: '2026-03-17', belopp_skatteverket: 5000 })],
})
expect(result.get('bank-1')).toBe('2026-03-17')
})
it('pairs a +5000 bank inflow (refund) with a -5000 SKV outflow', () => {
const result = findBankSkvCounterparts({
bankRows: [{ id: 'bank-1', date: '2026-03-20', amount: 5000 }],
skvRows: [skv({ id: 'skv-1', transaktionsdatum: '2026-03-19', belopp_skatteverket: -5000 })],
})
expect(result.get('bank-1')).toBe('2026-03-19')
})
it('does NOT pair when signs are equal (not a transfer)', () => {
// Bank -5000 (outgoing) and SKV -5000 (outgoing from skattekonto)
// would mean the user both paid 5000 from bank AND was charged 5000
// by SKV. Not the same event — independent cash flows.
const result = findBankSkvCounterparts({
bankRows: [{ id: 'bank-1', date: '2026-03-16', amount: -5000 }],
skvRows: [skv({ id: 'skv-1', transaktionsdatum: '2026-03-17', belopp_skatteverket: -5000 })],
})
expect(result.has('bank-1')).toBe(false)
})
it('does NOT pair when amounts differ even slightly', () => {
const result = findBankSkvCounterparts({
bankRows: [{ id: 'bank-1', date: '2026-03-16', amount: -5000 }],
skvRows: [skv({ id: 'skv-1', transaktionsdatum: '2026-03-17', belopp_skatteverket: 5000.01 })],
})
expect(result.has('bank-1')).toBe(false)
})
it('rounds to öre — 5000.001 equals 5000', () => {
const result = findBankSkvCounterparts({
bankRows: [{ id: 'bank-1', date: '2026-03-16', amount: -5000 }],
skvRows: [
skv({ id: 'skv-1', transaktionsdatum: '2026-03-17', belopp_skatteverket: 5000.001 }),
],
})
expect(result.get('bank-1')).toBe('2026-03-17')
})
it('does NOT pair when SKV date is outside the ±14 day window', () => {
const result = findBankSkvCounterparts({
bankRows: [{ id: 'bank-1', date: '2026-03-01', amount: -5000 }],
skvRows: [skv({ id: 'skv-1', transaktionsdatum: '2026-03-20', belopp_skatteverket: 5000 })],
})
expect(result.has('bank-1')).toBe(false)
})
it('pairs at the exact 14-day boundary', () => {
const result = findBankSkvCounterparts({
bankRows: [{ id: 'bank-1', date: '2026-03-01', amount: -5000 }],
skvRows: [skv({ id: 'skv-1', transaktionsdatum: '2026-03-15', belopp_skatteverket: 5000 })],
})
expect(result.get('bank-1')).toBe('2026-03-15')
})
it('first plausible match wins when multiple SKV rows would qualify', () => {
// Two SKV inflows of 5000 within window — the first one (in iteration
// order) wins. UI only shows one hint, so we don't need to rank.
const result = findBankSkvCounterparts({
bankRows: [{ id: 'bank-1', date: '2026-03-16', amount: -5000 }],
skvRows: [
skv({ id: 'skv-a', transaktionsdatum: '2026-03-17', belopp_skatteverket: 5000 }),
skv({ id: 'skv-b', transaktionsdatum: '2026-03-15', belopp_skatteverket: 5000 }),
],
})
expect(result.get('bank-1')).toBe('2026-03-17')
})
it('handles multiple bank txs independently', () => {
const result = findBankSkvCounterparts({
bankRows: [
{ id: 'bank-a', date: '2026-03-16', amount: -5000 },
{ id: 'bank-b', date: '2026-04-16', amount: -3000 },
],
skvRows: [
skv({ id: 'skv-1', transaktionsdatum: '2026-03-17', belopp_skatteverket: 5000 }),
skv({ id: 'skv-2', transaktionsdatum: '2026-04-18', belopp_skatteverket: 3000 }),
],
})
expect(result.size).toBe(2)
expect(result.get('bank-a')).toBe('2026-03-17')
expect(result.get('bank-b')).toBe('2026-04-18')
})
it('ignores zero-amount bank tx', () => {
const result = findBankSkvCounterparts({
bankRows: [{ id: 'bank-1', date: '2026-03-16', amount: 0 }],
skvRows: [skv({ id: 'skv-1', transaktionsdatum: '2026-03-17', belopp_skatteverket: 0 })],
})
expect(result.has('bank-1')).toBe(false)
})
it('returns empty map when no SKV rows are provided', () => {
const result = findBankSkvCounterparts({
bankRows: [{ id: 'bank-1', date: '2026-03-16', amount: -5000 }],
skvRows: [],
})
expect(result.size).toBe(0)
})
it('returns empty map when no bank rows are provided', () => {
const result = findBankSkvCounterparts({
bankRows: [],
skvRows: [skv({ id: 'skv-1', transaktionsdatum: '2026-03-17', belopp_skatteverket: 5000 })],
})
expect(result.size).toBe(0)
})
it('respects a custom dateWindowDays override', () => {
// 20 days apart — would fail default window, passes with override.
const result = findBankSkvCounterparts({
bankRows: [{ id: 'bank-1', date: '2026-03-01', amount: -5000 }],
skvRows: [skv({ id: 'skv-1', transaktionsdatum: '2026-03-21', belopp_skatteverket: 5000 })],
dateWindowDays: 30,
})
expect(result.get('bank-1')).toBe('2026-03-21')
})
it('exposes a sensible default window constant', () => {
expect(BANK_SKV_DATE_WINDOW_DAYS).toBe(14)
})
})