Files
accounted/lib/bookkeeping/vat-entries.ts
T
Jakob WennbergandClaude Opus 4.6 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

165 lines
4.5 KiB
TypeScript

import type { CreateJournalEntryLineInput, VatTreatment } from '@/types'
/**
* Generate VAT journal entry lines based on VAT treatment
*
* Swedish VAT scenarios:
* - Domestic 25%: Credit 2611 (utgående moms)
* - Domestic 12%: Credit 2621
* - Domestic 6%: Credit 2631
* - Input VAT deduction: Debit 2641 (ingående moms)
* - EU reverse charge (fiktiv moms): Debit 2645, Credit 2614 (offsetting)
* - Export (non-EU): No VAT lines
*/
interface VatEntryConfig {
vatTreatment: VatTreatment
baseAmount: number // Amount before VAT
direction: 'sales' | 'purchase'
}
/**
* Get VAT rate from treatment
*/
export function getVatRate(treatment: VatTreatment): number {
switch (treatment) {
case 'standard_25':
return 0.25
case 'reduced_12':
return 0.12
case 'reduced_6':
return 0.06
case 'reverse_charge':
case 'export':
case 'exempt':
return 0
default:
return 0.25
}
}
/**
* Generate output VAT lines for sales invoices
* Debit 1510 Kundfordringar [total incl VAT]
* Credit 30xx Försäljning [subtotal]
* Credit 26xx Utgående moms [vat_amount]
*/
export function generateSalesVatLines(config: VatEntryConfig): CreateJournalEntryLineInput[] {
const lines: CreateJournalEntryLineInput[] = []
const vatRate = getVatRate(config.vatTreatment)
if (vatRate === 0) return lines
const vatAmount = Math.round(config.baseAmount * vatRate * 100) / 100
// Determine the output VAT account
let vatAccount: string
switch (config.vatTreatment) {
case 'standard_25':
vatAccount = '2611' // Utgående moms försäljning 25%
break
case 'reduced_12':
vatAccount = '2621' // Utgående moms försäljning 12%
break
case 'reduced_6':
vatAccount = '2631' // Utgående moms försäljning 6%
break
default:
return lines
}
lines.push({
account_number: vatAccount,
debit_amount: 0,
credit_amount: vatAmount,
line_description: `Utgående moms ${vatRate * 100}%`,
})
return lines
}
/**
* Generate reverse charge lines (fiktiv moms)
* For EU/non-EU purchases: Debit 2645 + Credit 26x4 (offsetting entries)
* For domestic reverse charge: Debit 2647 + Credit 26x4 (offsetting entries)
*/
export function generateReverseChargeLines(
baseAmount: number,
vatRate: number = 0.25,
isDomestic: boolean = false
): CreateJournalEntryLineInput[] {
const vatAmount = Math.round(baseAmount * vatRate * 100) / 100
// Determine output account based on rate
let outputAccount: string
switch (vatRate) {
case 0.25:
outputAccount = '2614' // Utgående moms omvänd skattskyldighet 25%
break
case 0.12:
outputAccount = '2624' // Utgående moms omvänd skattskyldighet 12%
break
case 0.06:
outputAccount = '2634' // Utgående moms omvänd skattskyldighet 6%
break
default:
outputAccount = '2614'
}
// Input VAT account: 2647 for domestic RC (ML 16 kap), 2645 for EU/non-EU
const inputAccount = isDomestic ? '2647' : '2645'
const context = isDomestic ? 'omvänd skattskyldighet i Sverige' : 'omvänd skattskyldighet'
return [
{
account_number: inputAccount,
debit_amount: vatAmount,
credit_amount: 0,
line_description: `Fiktiv ingående moms ${vatRate * 100}% (${context})`,
},
{
account_number: outputAccount,
debit_amount: 0,
credit_amount: vatAmount,
line_description: `Fiktiv utgående moms ${vatRate * 100}% (${context})`,
},
]
}
/**
* Generate input VAT deduction line for domestic purchases
* Debit 2641 Ingående moms
*/
export function generateInputVatLine(
totalAmount: number,
vatRate: number = 0.25
): CreateJournalEntryLineInput | null {
if (vatRate === 0) return null
// Extract VAT from total amount (VAT-inclusive)
const vatAmount = Math.round((totalAmount * vatRate) / (1 + vatRate) * 100) / 100
return {
account_number: '2641', // Debiterad ingående moms
debit_amount: vatAmount,
credit_amount: 0,
line_description: `Ingående moms ${vatRate * 100}%`,
}
}
/**
* Calculate the net amount (excl VAT) from a total amount
*/
export function extractNetAmount(totalAmount: number, vatRate: number): number {
if (vatRate === 0) return totalAmount
return Math.round((totalAmount / (1 + vatRate)) * 100) / 100
}
/**
* Calculate VAT amount from a total amount (VAT-inclusive)
*/
export function extractVatAmount(totalAmount: number, vatRate: number): number {
if (vatRate === 0) return 0
return Math.round((totalAmount - totalAmount / (1 + vatRate)) * 100) / 100
}