Files
accounted/extensions/general/skatteverket/lib/mappers.ts
T
Jakob WennbergandClaude Opus 4.8 c74b19df1b Accounted rebrand + swarm-skill cleanup + bank-reconciliation fixes (#643)
* feat(reconciliation): close the bank-feed loop on voucher links and re-tag mis-typed opening balances

Two related fixes to bank reconciliation correctness:

1. Auto-reconcile on voucher link. Linking an invoice or supplier invoice to
   an existing voucher previously advanced only the invoice — the bank
   transaction that paid it kept sitting in the Transactions inbox with a null
   journal_entry_id. linkInvoiceToVoucher / linkSupplierInvoiceToVoucher now
   call autoReconcileTransactionForLinkedVoucher (lib/reconciliation), which
   links the bank transaction to the same verifikat when exactly one unbooked
   line matches it. Best-effort and post-commit: a failure here never fails the
   link. The result surfaces reconciledTransactionId; the inbox row leaves the
   list and the UI shows link_success_tx_reconciled.

2. Re-tag mis-typed opening balances. getReconciliationStatus and the GL-line
   matching RPCs identify a cash account's ingående balans solely by
   journal_entries.source_type='opening_balance'. Companies migrated from other
   systems often booked the bank IB as an ordinary voucher (source_type
   'import' or 'manual'), so it was never excluded and surfaced as a phantom
   reconciliation difference equal to the opening balance. Adds:
   - migration mark_entry_as_opening_balance: a GUC-gated carve-out in the
     immutability trigger plus a SECURITY DEFINER RPC that validates the entry
     (balance-sheet lines only, dated on a fiscal-period boundary), flips the
     source_type, and writes an audit row — no blanket data sweep.
   - POST /api/reconciliation/bank/mark-opening-balance + MarkOpeningBalanceSchema.
   - BankReconciliationView action to trigger it from the IB diff.

The gnubok_create_voucher executor now accepts a typed is_opening_balance flag
and derives source_type='opening_balance' only after validating class 1/2 lines
on the period start, so new IBs land correctly typed.

Covered by lib/reconciliation auto-reconcile tests, voucher-executors tests,
and a mark-entry-as-opening-balance pg-real test.

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

* chore: rebrand gnubok → Accounted and prune swarm agent skills

Product rebrand and skills housekeeping. No runtime behaviour change.

Rebrand: replace user-visible "gnubok" with "Accounted" across docs, READMEs,
in-code comments, doc-site content, MCP skill/resource prose, and the
gnubok-mcp package description. The MCP resource URI scheme is moved gnubok://
→ Accounted:// consistently across resource registrations, the event-type
comment, and the resource/skill tests. Deliberately preserved as stable
identifiers (NOT rebranded): the gnubok-company-id cookie, gnubok_sk_ / gnubok_inv_
token prefixes, the gnubok-mcp npm bridge name, and the AGI <gem:Programnamn>
value (kept 'gnubok' per its source comment — it is the software identifier sent
to Skatteverket and must not churn across visual rebrands).

Skills: remove the 27 swarm-* agent SKILL.md atoms (no longer used; already
absent from the agent_atom_registry in prod), refresh the remaining skill docs,
add the .claude/rules/ path-scoped rule set, and regenerate the
seed_agent_atom_bodies migration + .skill-body-manifest.json via
`npm run skills:generate` so the DB-backed skill bodies match the trimmed set.

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

---------

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-03 10:52:01 +02:00

89 lines
3.4 KiB
TypeScript

import type { VatDeclarationRutor } from '@/types'
import type { SkatteverketMomsuppgift } from '../types'
// Re-export shared formatting utilities
export { formatRedovisare, formatRedovisningsperiod } from '@/lib/skatteverket/format'
/**
* Convert Accounted VatDeclarationRutor to Skatteverket's momsuppgift payload.
*
* Fields with value 0 are omitted (Skatteverket treats absent fields as 0).
* This keeps the payload clean and avoids sending unnecessary data.
*
* Rounding: every ruta is rounded to whole kronor (SKV's schema is integers).
* summaMoms is recomputed from the rounded VAT-amount rutor — not from the
* pre-rounding ruta49 — so the payload is internally consistent with how
* Skatteverket recomputes the sum on their side. Rounding ruta49 separately
* from the components causes ±1 SEK drift on fractional-öres inputs and
* triggers SKV's FK009 ("summaMoms stämmer inte överens med övriga
* momsuppgifter") even when the underlying ledger arithmetic is correct.
*/
export function rutorToMomsuppgift(rutor: VatDeclarationRutor): SkatteverketMomsuppgift {
const result: SkatteverketMomsuppgift = {}
// Helper: only set non-zero values, rounded to whole kronor (SKV expects integers)
const set = (key: keyof SkatteverketMomsuppgift, value: number) => {
if (value !== 0) result[key] = Math.round(value)
}
// Taxable sales basis
set('momspliktigForsaljning', rutor.ruta05)
set('momspliktigaUttag', rutor.ruta06)
set('vinstmarginal', rutor.ruta07)
set('hyresInkomst', rutor.ruta08)
// Output VAT on sales
set('momsForsaljningUtgaendeHog', rutor.ruta10)
set('momsForsaljningUtgaendeMedel', rutor.ruta11)
set('momsForsaljningUtgaendeLag', rutor.ruta12)
// Reverse charge purchase bases
set('inkopVarorEU', rutor.ruta20)
set('inkopTjansterEU', rutor.ruta21)
set('inkopTjansterUtanforEU', rutor.ruta22)
set('inkopVarorSE', rutor.ruta23)
set('inkopTjansterSE', rutor.ruta24)
// Output VAT on reverse charge purchases
set('momsInkopUtgaendeHog', rutor.ruta30)
set('momsInkopUtgaendeMedel', rutor.ruta31)
set('momsInkopUtgaendeLag', rutor.ruta32)
// EU/export sales
set('forsaljningVarorEU', rutor.ruta35)
set('forsaljningVarorUtanforEU', rutor.ruta36)
set('inkopVaror3pHandel', rutor.ruta37)
set('forsaljningVaror3pHandel', rutor.ruta38)
set('forsaljningTjansterEU', rutor.ruta39)
set('ovrigForsaljningTjansterUtanforSE', rutor.ruta40)
set('forsaljningBskKopareSE', rutor.ruta41)
set('momsfriForsaljning', rutor.ruta42)
// Input VAT
set('ingaendeMomsAvdrag', rutor.ruta48)
// Import
set('import', rutor.ruta50)
set('momsImportUtgaendeHog', rutor.ruta60)
set('momsImportUtgaendeMedel', rutor.ruta61)
set('momsImportUtgaendeLag', rutor.ruta62)
// Net VAT must always be present, whole kronor. Compute from the already-
// rounded VAT-amount rutor so SKV's reconciliation (sum of integer rutor)
// never disagrees with our summaMoms by ±1 SEK.
result.summaMoms =
(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)
return result
}