Files
accounted/lib/reports/vat-declaration.ts
T
Jakob Wennberg 6ccd4f429c fix: Swedish VAT compliance — representation, domestic RC, full 26xx mapping, SIE (#206)
* feat: add INK2 declaration improvements, invoice delivery date, and Swedish compliance skills

Expand INK2 engine with full INK2S/INK2R support and improved SRU generation.
Add delivery_date field to invoices and corresponding PDF/migration support.
Add Claude skills for Swedish asset accounting, invoice compliance, SIE import/export, SRU filing, and tax planning.

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

* fix: address PR review — map BAS 4500–4899, strip CRLF in SRU, document P3

- Map BAS accounts 4500–4599 (legoarbeten), 4700–4899 (diverse
  varuinköpskostnader) to SRU 7512 so they are not silently dropped
  from INK2R declarations
- Strip \r\n in sanitizeString to prevent CRLF injection in SRU fields
- Document P3 period suffix limitation for brutet räkenskapsår

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

* fix: correct BAS 4500-4599, 4700-4899 mapping from 7512 to 7511

Per the official BAS-to-SRU mapping, these account ranges are cost of
goods (legoarbeten, inkurans, svinn) and belong under 7511 (Råvaror
och förnödenheter), not 7512 (Handelsvaror). 7512 remains 4600-4699.

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

* fix: Swedish VAT compliance — representation VAT, domestic RC, full BAS 26xx mapping, SIE encoding

- Representation expenses now default to reduced_12 VAT (ML 13 kap 24-25 §§);
  income tax deduction was abolished 2017 but VAT deduction at 12% remains
- Domestic reverse charge (byggtjänster etc.) uses 2647 instead of 2645,
  with distinct line descriptions for Swedish vs EU/non-EU RC
- VAT declaration maps all BAS 26xx variant accounts (egna uttag 2612/2622/2632,
  uthyrning 2613/2623/2633, VMB 2616/2626/2636, import 2615/2625/2635,
  domestic RC 2647, frivillig skattskyldighet 2642) and revenue variants
  (3108/3105/3004/3100) to correct momsdeklaration rutor
- SIE parser: remove unreliable #FORMAT PC8 encoding detection (most software
  exports UTF-8 with PC8 header), parse #FLAGGA for import-already-done warning,
  default SIE type to 1 when absent, fix RTRANS/BTRANS documentation
- SIE export: add #RAR -1 (previous fiscal year), fix UB = IB + movements
- Error messages: add pattern matching for locked period trigger errors

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

* fix: address Greptile review — update ruta49 JSDoc, use null sentinel in error map

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

---------

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-09 14:13:15 +02:00

444 lines
14 KiB
TypeScript

import type { SupabaseClient } from '@supabase/supabase-js'
import { fetchAllRows } from '@/lib/supabase/fetch-all'
import type {
VatDeclaration,
VatDeclarationRutor,
VatPeriodType,
AccountingMethod,
} from '@/types'
/**
* Calculate VAT declaration (Momsdeklaration) for a given period.
*
* Reads directly from the general ledger — sums posted journal entry lines
* on 26xx (VAT) and 3xxx (revenue) accounts for the period. This makes the
* momsdeklaration a pure projection from the double-entry bookkeeping ledger.
*
* The accounting method (accrual vs cash) is already reflected in when
* journal entries were created by the entry generators, so no separate
* filtering logic is needed here.
*/
/**
* Account-to-ruta mapping for the Swedish momsdeklaration (SKV 4700).
*
* Covers all BAS 26xx VAT accounts and 3xxx revenue accounts that feed the
* momsdeklaration. Includes variant accounts (egna uttag, uthyrning, VMB,
* import, domestic reverse charge) that may appear from manual entries or
* SIE imports, not just accounts generated by the system.
*
* Output VAT (261x/262x/263x) → ruta 10/11/12 per rate
* Reverse charge output (2614/2624/2634) → ruta 30/31/32
* Import VAT (2615/2625/2635) → ruta 60/61/62
* Input VAT (2641-2649) → ruta 48
* Revenue (3001-3003) → ruta 05; EU (3108/3308) → ruta 35/39;
* Export (3105/3305) → ruta 36/40; Exempt (3004/3100) → ruta 42
*/
const ACCOUNT_RUTA: Record<string, { box: keyof VatDeclarationRutor; side: 'credit' | 'debit' }> = {
// Output VAT 25% → ruta 10
'2611': { box: 'ruta10', side: 'credit' }, // Försäljning inom Sverige
'2612': { box: 'ruta10', side: 'credit' }, // Egna uttag
'2613': { box: 'ruta10', side: 'credit' }, // Uthyrning (frivillig skattskyldighet)
'2616': { box: 'ruta10', side: 'credit' }, // Vinstmarginalbeskattning
// Output VAT 12% → ruta 11
'2621': { box: 'ruta11', side: 'credit' },
'2622': { box: 'ruta11', side: 'credit' }, // Egna uttag
'2623': { box: 'ruta11', side: 'credit' }, // Uthyrning
'2626': { box: 'ruta11', side: 'credit' }, // VMB
// Output VAT 6% → ruta 12
'2631': { box: 'ruta12', side: 'credit' },
'2632': { box: 'ruta12', side: 'credit' }, // Egna uttag
'2633': { box: 'ruta12', side: 'credit' }, // Uthyrning
'2636': { box: 'ruta12', side: 'credit' }, // VMB
// Reverse charge output VAT → ruta 30/31/32
'2614': { box: 'ruta30', side: 'credit' },
'2624': { box: 'ruta31', side: 'credit' },
'2634': { box: 'ruta32', side: 'credit' },
// Input VAT → ruta 48
'2641': { box: 'ruta48', side: 'debit' }, // Debiterad ingående moms
'2642': { box: 'ruta48', side: 'debit' }, // Frivillig skattskyldighet
'2645': { box: 'ruta48', side: 'debit' }, // Förvärv utlandet (EU/non-EU RC)
'2646': { box: 'ruta48', side: 'debit' }, // Uthyrning
'2647': { box: 'ruta48', side: 'debit' }, // Omvänd skattskyldighet i Sverige
'2649': { box: 'ruta48', side: 'debit' }, // Blandad verksamhet
// Import VAT (since 2015, via momsdeklaration) → ruta 60/61/62
'2615': { box: 'ruta60', side: 'credit' }, // Import 25%
'2625': { box: 'ruta61', side: 'credit' }, // Import 12%
'2635': { box: 'ruta62', side: 'credit' }, // Import 6%
// Revenue: domestic taxable sales → ruta 05
'3001': { box: 'ruta05', side: 'credit' },
'3002': { box: 'ruta05', side: 'credit' },
'3003': { box: 'ruta05', side: 'credit' },
// Revenue: EU goods/services → ruta 35/39
'3108': { box: 'ruta35', side: 'credit' }, // Varuförsäljning till EU
'3308': { box: 'ruta39', side: 'credit' }, // Tjänsteförsäljning till EU
// Revenue: export/other → ruta 36/40/42
'3105': { box: 'ruta36', side: 'credit' }, // Varuförsäljning export
'3305': { box: 'ruta40', side: 'credit' }, // Tjänsteförsäljning export
'3004': { box: 'ruta42', side: 'credit' }, // Momsfri försäljning (AB)
'3100': { box: 'ruta42', side: 'credit' }, // Momsfria intäkter (EF)
}
const VAT_ACCOUNTS = Object.keys(ACCOUNT_RUTA)
/**
* Calculate period start and end dates
*/
export function calculatePeriodDates(
periodType: VatPeriodType,
year: number,
period: number
): { start: string; end: string } {
let startMonth: number
let endMonth: number
switch (periodType) {
case 'monthly':
// period is 1-12
startMonth = period
endMonth = period
break
case 'quarterly':
// period is 1-4
startMonth = (period - 1) * 3 + 1
endMonth = period * 3
break
case 'yearly':
// period is 1
startMonth = 1
endMonth = 12
break
default:
startMonth = 1
endMonth = 12
}
const startDate = new Date(year, startMonth - 1, 1)
const endDate = new Date(year, endMonth, 0) // Last day of end month
return {
start: formatDate(startDate),
end: formatDate(endDate),
}
}
/**
* Format date as YYYY-MM-DD
*/
function formatDate(date: Date): string {
const y = date.getFullYear()
const m = String(date.getMonth() + 1).padStart(2, '0')
const d = String(date.getDate()).padStart(2, '0')
return `${y}-${m}-${d}`
}
/**
* Round to 2 decimal places
*/
function round(value: number): number {
return Math.round(value * 100) / 100
}
/**
* Calculate VAT declaration from the general ledger.
*
* Sums posted journal entry lines on 26xx and 3xxx accounts per ACCOUNT_RUTA mapping.
* - ruta 49 = (10 + 11 + 12 + 30 + 31 + 32 + 60 + 61 + 62) - 48
*
* The accounting method parameter is accepted for backward compatibility
* but not used — the method is already baked into journal entry timing.
*/
export async function calculateVatDeclaration(
supabase: SupabaseClient,
companyId: string,
periodType: VatPeriodType,
year: number,
period: number,
_accountingMethod: AccountingMethod = 'accrual'
): Promise<VatDeclaration> {
const { start, end } = calculatePeriodDates(periodType, year, period)
// Fetch all posted journal entry lines on VAT-relevant accounts for the period
const lines = await fetchAllRows<{
account_number: string
debit_amount: number
credit_amount: number
}>(({ from, to }) =>
supabase
.from('journal_entry_lines')
.select(`
account_number,
debit_amount,
credit_amount,
journal_entries!inner (company_id, entry_date, status)
`)
.in('account_number', VAT_ACCOUNTS)
.eq('journal_entries.company_id', companyId)
.in('journal_entries.status', ['posted', 'reversed'])
.gte('journal_entries.entry_date', start)
.lte('journal_entries.entry_date', end)
.range(from, to)
)
// Aggregate debit/credit totals per account
const totals = new Map<string, { debit: number; credit: number }>()
for (const line of lines) {
const t = totals.get(line.account_number) || { debit: 0, credit: 0 }
t.debit += Number(line.debit_amount) || 0
t.credit += Number(line.credit_amount) || 0
totals.set(line.account_number, t)
}
// Map account balances to momsdeklaration boxes
const rutor: 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,
}
for (const [account, mapping] of Object.entries(ACCOUNT_RUTA)) {
const t = totals.get(account)
if (!t) continue
const balance = mapping.side === 'credit'
? t.credit - t.debit
: t.debit - t.credit
rutor[mapping.box] = round(rutor[mapping.box] + balance)
}
// FK009: summaMoms = (10 + 11 + 12 + 30 + 31 + 32 + 60 + 61 + 62) - 48
rutor.ruta49 = round(
rutor.ruta10 + rutor.ruta11 + rutor.ruta12 +
rutor.ruta30 + rutor.ruta31 + rutor.ruta32 +
rutor.ruta60 + rutor.ruta61 + rutor.ruta62 -
rutor.ruta48
)
// Compute per-rate base amounts from individual revenue accounts
const revenueByRate = {
base25: 0, // 3001
base12: 0, // 3002
base6: 0, // 3003
}
for (const [account, rate] of [['3001', 'base25'], ['3002', 'base12'], ['3003', 'base6']] as const) {
const t = totals.get(account)
if (t) revenueByRate[rate] = round(t.credit - t.debit)
}
// Calculate reverse charge purchase bases (ruta 20-24) from supplier invoices
const rcBases = await calculateReverseChargeBases(supabase, companyId, start, end)
rutor.ruta20 = rcBases.ruta20
rutor.ruta21 = rcBases.ruta21
rutor.ruta22 = rcBases.ruta22
rutor.ruta23 = rcBases.ruta23
rutor.ruta24 = rcBases.ruta24
// Count journal entries by source type for metadata
const { data: entryCounts } = await supabase
.from('journal_entries')
.select('source_type')
.eq('company_id', companyId)
.in('status', ['posted', 'reversed'])
.gte('entry_date', start)
.lte('entry_date', end)
const invoiceSources = new Set([
'invoice_created', 'invoice_paid', 'invoice_cash_payment', 'credit_note',
])
let invoiceCount = 0
let transactionCount = 0
for (const e of entryCounts || []) {
if (invoiceSources.has(e.source_type)) invoiceCount++
else if (e.source_type === 'bank_transaction') transactionCount++
}
return {
period: { type: periodType, year, period, start, end },
rutor,
invoiceCount,
transactionCount,
breakdown: {
invoices: {
ruta05: rutor.ruta05,
ruta06: rutor.ruta06,
ruta07: rutor.ruta07,
ruta10: rutor.ruta10,
ruta11: rutor.ruta11,
ruta12: rutor.ruta12,
ruta39: rutor.ruta39,
ruta40: rutor.ruta40,
base25: revenueByRate.base25,
base12: revenueByRate.base12,
base6: revenueByRate.base6,
},
transactions: { ruta48: rutor.ruta48 },
receipts: { ruta48: 0 },
reverseCharge: {
ruta20: rutor.ruta20,
ruta21: rutor.ruta21,
ruta22: rutor.ruta22,
ruta23: rutor.ruta23,
ruta24: rutor.ruta24,
ruta30: rutor.ruta30,
ruta31: rutor.ruta31,
ruta32: rutor.ruta32,
},
},
}
}
/**
* Calculate reverse charge purchase bases (ruta 20-24) from supplier invoices.
*
* Queries journal entries with supplier invoice source types, then looks up
* the linked supplier invoices + suppliers to determine supplier_type and
* sum item line_total (the net tax base in SEK).
*
* Classification (all reverse charge is currently services):
* eu_business → ruta21 (services from EU)
* non_eu_business → ruta22 (services from outside EU)
* swedish_business → ruta24 (domestic reverse charge, e.g. construction)
* ruta20/23 (goods) → 0 for now (Tullverket path not implemented)
*/
async function calculateReverseChargeBases(
supabase: SupabaseClient,
companyId: string,
start: string,
end: string,
): Promise<{ ruta20: number; ruta21: number; ruta22: number; ruta23: number; ruta24: number }> {
const result = { ruta20: 0, ruta21: 0, ruta22: 0, ruta23: 0, ruta24: 0 }
// Step 1: Find journal entries from supplier invoices in this period
const supplierSourceTypes = [
'supplier_invoice_registered',
'supplier_invoice_cash_payment',
'supplier_credit_note',
]
const entries = await fetchAllRows<{
id: string
source_id: string
}>(({ from, to }) =>
supabase
.from('journal_entries')
.select('id, source_id')
.eq('company_id', companyId)
.in('source_type', supplierSourceTypes)
.eq('status', 'posted')
.gte('entry_date', start)
.lte('entry_date', end)
.range(from, to)
)
if (entries.length === 0) return result
const sourceIds = [...new Set(entries.map(e => e.source_id).filter(Boolean))]
if (sourceIds.length === 0) return result
// Step 2: Fetch supplier invoices that are reverse charge, with supplier type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const invoices = await fetchAllRows<Record<string, any>>(({ from, to }) =>
supabase
.from('supplier_invoices')
.select('id, supplier_id, reverse_charge, is_credit_note, subtotal_sek, subtotal, currency, exchange_rate, suppliers!inner(supplier_type)')
.in('id', sourceIds)
.eq('reverse_charge', true)
.eq('company_id', companyId)
.range(from, to)
)
if (invoices.length === 0) return result
// Step 3: Sum tax bases by supplier type
for (const inv of invoices) {
// Use subtotal_sek if available, otherwise convert via exchange_rate
let baseSek: number
if (inv.subtotal_sek != null) {
baseSek = Number(inv.subtotal_sek)
} else if (inv.currency !== 'SEK' && inv.exchange_rate) {
baseSek = Math.round(Number(inv.subtotal) * Number(inv.exchange_rate) * 100) / 100
} else {
baseSek = Number(inv.subtotal)
}
// Credit notes reduce the base
if (inv.is_credit_note) baseSek = -baseSek
// !inner join: Supabase returns the related row as an object (1-to-1 FK)
const supplier = Array.isArray(inv.suppliers) ? inv.suppliers[0] : inv.suppliers
const supplierType = supplier?.supplier_type as string
switch (supplierType) {
case 'eu_business':
result.ruta21 = round(result.ruta21 + baseSek)
break
case 'non_eu_business':
result.ruta22 = round(result.ruta22 + baseSek)
break
case 'swedish_business':
result.ruta24 = round(result.ruta24 + baseSek)
break
}
}
return result
}
/**
* Get a summary of the VAT declaration for display
*/
export function getVatDeclarationSummary(declaration: VatDeclaration): {
totalOutputVat: number
totalInputVat: number
vatToPay: number
isRefund: boolean
} {
const totalOutputVat = round(
declaration.rutor.ruta10 +
declaration.rutor.ruta11 +
declaration.rutor.ruta12 +
declaration.rutor.ruta30 +
declaration.rutor.ruta31 +
declaration.rutor.ruta32 +
declaration.rutor.ruta60 +
declaration.rutor.ruta61 +
declaration.rutor.ruta62
)
const totalInputVat = declaration.rutor.ruta48
const vatToPay = declaration.rutor.ruta49
return {
totalOutputVat,
totalInputVat,
vatToPay,
isRefund: vatToPay < 0,
}
}
/**
* Format period label for display
*/
export function formatPeriodLabel(
periodType: VatPeriodType,
year: number,
period: number
): string {
switch (periodType) {
case 'monthly':
const monthNames = [
'Januari', 'Februari', 'Mars', 'April', 'Maj', 'Juni',
'Juli', 'Augusti', 'September', 'Oktober', 'November', 'December'
]
return `${monthNames[period - 1]} ${year}`
case 'quarterly':
return `Kvartal ${period} ${year}`
case 'yearly':
return `Helår ${year}`
default:
return `${year}`
}
}