Files
accounted/extensions/general/skatteverket/__tests__/mappers.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

195 lines
6.4 KiB
TypeScript

import { describe, it, expect } from 'vitest'
import { rutorToMomsuppgift, formatRedovisare, formatRedovisningsperiod } from '../lib/mappers'
import type { VatDeclarationRutor } from '@/types'
const emptyRutor: VatDeclarationRutor = {
ruta05: 0, ruta06: 0, ruta07: 0, ruta08: 0,
ruta10: 0, ruta11: 0, ruta12: 0,
ruta20: 0, ruta21: 0, ruta22: 0, ruta23: 0, ruta24: 0,
ruta30: 0, ruta31: 0, ruta32: 0,
ruta35: 0, ruta36: 0, ruta37: 0, ruta38: 0,
ruta39: 0, ruta40: 0, ruta41: 0, ruta42: 0,
ruta48: 0, ruta49: 0,
ruta50: 0, ruta60: 0, ruta61: 0, ruta62: 0,
}
describe('rutorToMomsuppgift', () => {
it('converts typical EF VAT declaration', () => {
const rutor: VatDeclarationRutor = {
...emptyRutor,
ruta05: 10000,
ruta10: 2500,
ruta48: 350,
ruta49: 2150,
}
const result = rutorToMomsuppgift(rutor)
expect(result.momspliktigForsaljning).toBe(10000)
expect(result.momsForsaljningUtgaendeHog).toBe(2500)
expect(result.ingaendeMomsAvdrag).toBe(350)
expect(result.summaMoms).toBe(2150)
})
it('omits zero-value fields except summaMoms', () => {
const rutor: VatDeclarationRutor = {
...emptyRutor,
ruta05: 5000,
ruta10: 1250,
ruta49: 1250,
}
const result = rutorToMomsuppgift(rutor)
expect(result.momspliktigForsaljning).toBe(5000)
expect(result.momsForsaljningUtgaendeHog).toBe(1250)
expect(result.summaMoms).toBe(1250)
// Zero fields should not be present
expect(result.momspliktigaUttag).toBeUndefined()
expect(result.momsForsaljningUtgaendeMedel).toBeUndefined()
expect(result.momsForsaljningUtgaendeLag).toBeUndefined()
expect(result.ingaendeMomsAvdrag).toBeUndefined()
})
it('always includes summaMoms even when zero', () => {
const result = rutorToMomsuppgift(emptyRutor)
expect(result.summaMoms).toBe(0)
})
it('maps reverse charge fields correctly', () => {
const rutor: VatDeclarationRutor = {
...emptyRutor,
ruta21: 5000,
ruta30: 1250,
ruta48: 1250,
ruta49: 0,
}
const result = rutorToMomsuppgift(rutor)
expect(result.inkopTjansterEU).toBe(5000)
expect(result.momsInkopUtgaendeHog).toBe(1250)
expect(result.ingaendeMomsAvdrag).toBe(1250)
expect(result.summaMoms).toBe(0)
})
it('maps EU/export and import fields', () => {
const rutor: VatDeclarationRutor = {
...emptyRutor,
ruta35: 8000,
ruta36: 12000,
ruta39: 3000,
ruta40: 5000,
ruta50: 2000,
ruta60: 500,
ruta49: 500,
}
const result = rutorToMomsuppgift(rutor)
expect(result.forsaljningVarorEU).toBe(8000)
expect(result.forsaljningVarorUtanforEU).toBe(12000)
expect(result.forsaljningTjansterEU).toBe(3000)
expect(result.ovrigForsaljningTjansterUtanforSE).toBe(5000)
expect(result.import).toBe(2000)
expect(result.momsImportUtgaendeHog).toBe(500)
})
// FK009 regression: SKV recomputes summaMoms from rounded rutor and
// compares against ours. If we round ruta49 from unrounded inputs while
// rounding each ruta independently we drift by ±1 SEK per fractional ruta.
it('summaMoms equals Σ(rounded output VAT rutor) - rounded ruta48', () => {
const rutor: VatDeclarationRutor = {
...emptyRutor,
// Fractional öres on every VAT-amount ruta so banker's rounding can
// disagree between the orundad ruta49 and the rundade individual fält.
ruta10: 100.49,
ruta11: 50.51,
ruta12: 25.49,
ruta30: 10.51,
ruta31: 5.49,
ruta32: 2.51,
ruta60: 7.49,
ruta61: 3.51,
ruta62: 1.49,
ruta48: 80.51,
ruta49: 100.49 + 50.51 + 25.49 + 10.51 + 5.49 + 2.51 + 7.49 + 3.51 + 1.49 - 80.51,
}
const result = rutorToMomsuppgift(rutor)
const expectedSumma =
(result.momsForsaljningUtgaendeHog ?? 0) +
(result.momsForsaljningUtgaendeMedel ?? 0) +
(result.momsForsaljningUtgaendeLag ?? 0) +
(result.momsInkopUtgaendeHog ?? 0) +
(result.momsInkopUtgaendeMedel ?? 0) +
(result.momsInkopUtgaendeLag ?? 0) +
(result.momsImportUtgaendeHog ?? 0) +
(result.momsImportUtgaendeMedel ?? 0) +
(result.momsImportUtgaendeLag ?? 0) -
(result.ingaendeMomsAvdrag ?? 0)
expect(result.summaMoms).toBe(expectedSumma)
// Sanity-check: result is an integer (SKV requires whole kronor)
expect(Number.isInteger(result.summaMoms)).toBe(true)
})
it('summaMoms is negative when input VAT exceeds output VAT', () => {
const rutor: VatDeclarationRutor = {
...emptyRutor,
ruta48: 5000,
ruta49: -5000,
}
const result = rutorToMomsuppgift(rutor)
expect(result.summaMoms).toBe(-5000)
})
})
describe('formatRedovisare', () => {
it('formats aktiebolag org number with 16 prefix', () => {
expect(formatRedovisare('5020000013', 'aktiebolag')).toBe('165020000013')
})
it('formats aktiebolag org number with hyphen', () => {
expect(formatRedovisare('502000-0013', 'aktiebolag')).toBe('165020000013')
})
it('formats enskild firma personnummer with 19 prefix for older', () => {
// Person born in 1985 — 85 > 26 (current year), so prefix 19
expect(formatRedovisare('8501011234', 'enskild_firma')).toBe('198501011234')
})
it('formats enskild firma personnummer with 20 prefix for younger', () => {
// Person born in 2005 — 05 < 26 (current year), so prefix 20
expect(formatRedovisare('0501011234', 'enskild_firma')).toBe('200501011234')
})
it('passes through 12-digit numbers unchanged', () => {
expect(formatRedovisare('165020000013', 'aktiebolag')).toBe('165020000013')
})
it('throws for invalid length', () => {
expect(() => formatRedovisare('12345', 'aktiebolag')).toThrow('Ogiltigt organisationsnummer')
})
})
describe('formatRedovisningsperiod', () => {
it('formats monthly period as YYYYMM', () => {
expect(formatRedovisningsperiod('monthly', 2025, 1)).toBe('202501')
expect(formatRedovisningsperiod('monthly', 2025, 12)).toBe('202512')
})
it('formats quarterly period as last month of quarter', () => {
expect(formatRedovisningsperiod('quarterly', 2025, 1)).toBe('202503')
expect(formatRedovisningsperiod('quarterly', 2025, 2)).toBe('202506')
expect(formatRedovisningsperiod('quarterly', 2025, 3)).toBe('202509')
expect(formatRedovisningsperiod('quarterly', 2025, 4)).toBe('202512')
})
it('formats yearly period as December', () => {
expect(formatRedovisningsperiod('yearly', 2025, 1)).toBe('202512')
})
})