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>
This commit is contained in:
Jakob Wennberg
2026-04-09 14:13:15 +02:00
committed by GitHub
co-authored by Claude Opus 4.6
parent 7a18d89c70
commit 6ccd4f429c
15 changed files with 445 additions and 128 deletions
@@ -182,7 +182,6 @@ describe('getDefaultVatTreatmentForCategory', () => {
expect(getDefaultVatTreatmentForCategory('expense_bank_fees')).toBeNull()
expect(getDefaultVatTreatmentForCategory('expense_card_fees')).toBeNull()
expect(getDefaultVatTreatmentForCategory('expense_currency_exchange')).toBeNull()
expect(getDefaultVatTreatmentForCategory('expense_representation')).toBeNull()
})
it('returns null for private transactions', () => {
@@ -194,21 +193,22 @@ describe('getDefaultVatTreatmentForCategory', () => {
})
})
describe('representation VAT (ML 8:9 — illegal since 2017)', () => {
it('getDefaultVatTreatmentForCategory returns null for representation', () => {
expect(getDefaultVatTreatmentForCategory('expense_representation')).toBeNull()
describe('representation VAT (reduced 12%, ML 13 kap 24-25 §§)', () => {
it('getDefaultVatTreatmentForCategory returns reduced_12 for representation', () => {
expect(getDefaultVatTreatmentForCategory('expense_representation')).toBe('reduced_12')
})
it('getCategoryAccountMapping has vatTreatment: null for representation', () => {
it('getCategoryAccountMapping has vatTreatment: reduced_12 for representation', () => {
const result = getCategoryAccountMapping('expense_representation', -500, true)
expect(result.vatTreatment).toBeNull()
expect(result.vatDebitAccount).toBeNull()
expect(result.vatTreatment).toBe('reduced_12')
expect(result.vatDebitAccount).toBe('2641')
})
it('buildMappingResultFromCategory generates no VAT lines for representation', () => {
it('buildMappingResultFromCategory generates 12% VAT line for representation', () => {
const tx = makeTransaction({ amount: -500 })
const result = buildMappingResultFromCategory('expense_representation', tx, true)
expect(result.vat_lines).toHaveLength(0)
expect(result.vat_lines).toHaveLength(1)
expect(result.vat_lines[0].account_number).toBe('2641')
})
})
@@ -39,17 +39,19 @@ vi.mock('../currency-utils', () => ({
// Mock vat-entries with real reverse charge logic
vi.mock('../vat-entries', () => ({
generateReverseChargeLines: vi.fn().mockImplementation(
(baseAmount: number, vatRate: number = 0.25) => {
(baseAmount: number, vatRate: number = 0.25, isDomestic: boolean = false) => {
const vatAmount = Math.round(baseAmount * vatRate * 100) / 100
const inputAccount = isDomestic ? '2647' : '2645'
let outputAccount: string
switch (vatRate) {
case 0.12: outputAccount = '2624'; break
case 0.06: outputAccount = '2634'; break
default: outputAccount = '2614'; break
}
const context = isDomestic ? 'omvänd skattskyldighet i Sverige' : 'omvänd skattskyldighet'
return [
{ account_number: '2645', debit_amount: vatAmount, credit_amount: 0, line_description: `Fiktiv ingående moms ${vatRate * 100}% (omvänd skattskyldighet)` },
{ account_number: outputAccount, debit_amount: 0, credit_amount: vatAmount, line_description: `Fiktiv utgående moms ${vatRate * 100}% (omvänd skattskyldighet)` },
{ 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})` },
]
}
),
@@ -420,6 +422,66 @@ describe('createSupplierInvoiceRegistrationEntry', () => {
assertBalanced(input)
})
it('creates domestic reverse charge entry using 2647 (byggtjänster etc.)', async () => {
const invoice = makeSupplierInvoice({
subtotal: 20000,
vat_amount: 0,
total: 20000,
reverse_charge: true,
})
const items = [makeItem({ line_total: 20000, vat_rate: 0.25, account_number: '4425' })]
await createSupplierInvoiceRegistrationEntry(
null as never, 'company-1', 'user-1', invoice, items, 'swedish_business'
)
const input = mockedCreateEntry.mock.calls[0][3]
// Domestic RC uses 2647 (not 2645) for input VAT
const debit2647 = findByAccount(input.lines, '2647')
expect(debit2647).toHaveLength(1)
expect(debit2647[0].debit_amount).toBe(5000) // 20000 * 0.25
const credit2614 = findByAccount(input.lines, '2614')
expect(credit2614).toHaveLength(1)
expect(credit2614[0].credit_amount).toBe(5000)
// No EU reverse charge account used
expect(findByAccount(input.lines, '2645')).toHaveLength(0)
// No regular input VAT
expect(findByAccount(input.lines, '2641')).toHaveLength(0)
// 2440 = expense only (RC is offsetting)
const credit2440 = findByAccount(input.lines, '2440')
expect(credit2440[0].credit_amount).toBe(20000)
assertBalanced(input)
})
it('does NOT create RC entry for swedish_business when reverse_charge is false', async () => {
const invoice = makeSupplierInvoice({
subtotal: 8000,
vat_amount: 2000,
total: 10000,
reverse_charge: false,
})
const items = [makeItem({ line_total: 8000, vat_rate: 0.25, account_number: '4010' })]
await createSupplierInvoiceRegistrationEntry(
null as never, 'company-1', 'user-1', invoice, items, 'swedish_business'
)
const input = mockedCreateEntry.mock.calls[0][3]
// Should use standard domestic path with 2641
expect(findByAccount(input.lines, '2641')).toHaveLength(1)
expect(findByAccount(input.lines, '2647')).toHaveLength(0)
expect(findByAccount(input.lines, '2645')).toHaveLength(0)
expect(findByAccount(input.lines, '2614')).toHaveLength(0)
assertBalanced(input)
})
it('creates per-rate 2645/26x4 pairs for mixed-rate reverse charge', async () => {
const invoice = makeSupplierInvoice({
subtotal: 15000,
+12 -7
View File
@@ -132,13 +132,13 @@ export function getCategoryAccountMapping(
if (category.startsWith('expense_')) {
const expenseAccount = getExpenseAccount(category, entityType)
// Bank fees, card fees, currency exchange, and representation are VAT-exempt in Sweden
// Representation has zero input VAT deduction since 2017-01-01 (ML 8:9)
const vatExemptCategories = ['expense_bank_fees', 'expense_card_fees', 'expense_currency_exchange', 'expense_representation']
// Bank fees, card fees, and currency exchange are VAT-exempt in Sweden
const vatExemptCategories = ['expense_bank_fees', 'expense_card_fees', 'expense_currency_exchange']
const isVatExempt = vatExemptCategories.includes(category)
// Use provided vatTreatment, or default based on category
const resolvedVat = vatTreatment ?? (isVatExempt ? null : 'standard_25')
// Representation defaults to reduced_12 (ML 13 kap 24-25 §§, max 300 SEK/person).
// Note: income tax deduction was abolished 2017 (IL 16 kap 2 §), but VAT deduction remains.
const resolvedVat = vatTreatment ?? (isVatExempt ? null : category === 'expense_representation' ? 'reduced_12' : 'standard_25')
return {
debitAccount: expenseAccount,
@@ -345,11 +345,16 @@ export function getDefaultVatTreatmentForCategory(
return null
}
// Representation has zero input VAT deduction since 2017-01-01 (ML 8:9)
const vatExemptCategories = ['expense_bank_fees', 'expense_card_fees', 'expense_currency_exchange', 'expense_representation']
const vatExemptCategories = ['expense_bank_fees', 'expense_card_fees', 'expense_currency_exchange']
if (vatExemptCategories.includes(category)) {
return null
}
// Representation defaults to reduced_12 (ML 13 kap 24-25 §§, max 300 SEK/person).
// Note: income tax deduction was abolished 2017 (IL 16 kap 2 §), but VAT deduction remains.
if (category === 'expense_representation') {
return 'reduced_12'
}
return 'standard_25'
}
+17 -10
View File
@@ -82,19 +82,21 @@ export async function createSupplierInvoiceRegistrationEntry(
}
lines.push(...debitLines)
const isReverseCharge = (supplierType === 'eu_business' || supplierType === 'non_eu_business') && invoice.reverse_charge
const isReverseCharge = (supplierType === 'eu_business' || supplierType === 'non_eu_business' || supplierType === 'swedish_business') && invoice.reverse_charge
const isDomesticRC = supplierType === 'swedish_business' && invoice.reverse_charge
if (isReverseCharge) {
// EU/non-EU reverse charge: fiktiv moms entries per rate group
// Reverse charge: fiktiv moms entries per rate group
// Domestic (byggtjänster etc.): 2647/26x4, EU/non-EU: 2645/26x4
const vatByRate = groupVatByRate(items, invoice.currency, invoice.exchange_rate)
for (const [rate, amount] of vatByRate) {
if (rate > 0 && amount > 0) {
const rcLines = generateReverseChargeLines(amount / rate, rate)
const rcLines = generateReverseChargeLines(amount / rate, rate, isDomesticRC)
lines.push(...rcLines)
}
}
} else if (invoice.vat_amount > 0) {
// Domestic: Debit ingående moms per rate group
// Domestic standard: Debit ingående moms per rate group
const vatByRate = groupVatByRate(items, invoice.currency, invoice.exchange_rate)
for (const [rate, amount] of vatByRate) {
if (amount > 0) {
@@ -275,19 +277,21 @@ export async function createSupplierInvoiceCashEntry(
})
}
const isReverseCharge = (supplierType === 'eu_business' || supplierType === 'non_eu_business') && invoice.reverse_charge
const isReverseCharge = (supplierType === 'eu_business' || supplierType === 'non_eu_business' || supplierType === 'swedish_business') && invoice.reverse_charge
const isDomesticRC = supplierType === 'swedish_business' && invoice.reverse_charge
if (isReverseCharge) {
// EU/non-EU reverse charge: fiktiv moms entries per rate group
// Reverse charge: fiktiv moms entries per rate group
// Domestic (byggtjänster etc.): 2647/26x4, EU/non-EU: 2645/26x4
const vatByRate = groupVatByRate(items, invoice.currency, invoice.exchange_rate)
for (const [rate, amount] of vatByRate) {
if (rate > 0 && amount > 0) {
const rcLines = generateReverseChargeLines(amount / rate, rate)
const rcLines = generateReverseChargeLines(amount / rate, rate, isDomesticRC)
lines.push(...rcLines)
}
}
} else if (invoice.vat_amount > 0) {
// Domestic: Debit ingående moms per rate group
// Domestic standard: Debit ingående moms per rate group
const vatByRate = groupVatByRate(items, invoice.currency, invoice.exchange_rate)
for (const [rate, amount] of vatByRate) {
if (amount > 0) {
@@ -367,10 +371,13 @@ export async function createSupplierCreditNoteEntry(
})
}
const isReverseCharge = (supplierType === 'eu_business' || supplierType === 'non_eu_business') && creditNote.reverse_charge
const isReverseCharge = (supplierType === 'eu_business' || supplierType === 'non_eu_business' || supplierType === 'swedish_business') && creditNote.reverse_charge
const isDomesticRC = supplierType === 'swedish_business' && creditNote.reverse_charge
if (isReverseCharge) {
// Reverse the fiktiv moms per rate group (swap debit/credit from registration)
// Input VAT account: 2647 for domestic RC, 2645 for EU/non-EU
const inputAccount = isDomesticRC ? '2647' : '2645'
const vatByRate = groupVatByRate(items, creditNote.currency, creditNote.exchange_rate, true)
for (const [rate, amount] of vatByRate) {
if (rate > 0 && amount > 0) {
@@ -382,7 +389,7 @@ export async function createSupplierCreditNoteEntry(
default: outputAccount = '2614'; break
}
creditLines.push({
account_number: '2645',
account_number: inputAccount,
debit_amount: 0,
credit_amount: amount,
line_description: `Omvänd fiktiv ingående moms ${Math.round(rate * 100)}% ${desc}`,
+13 -7
View File
@@ -79,16 +79,18 @@ export function generateSalesVatLines(config: VatEntryConfig): CreateJournalEntr
}
/**
* Generate EU reverse charge lines (fiktiv moms)
* For purchases from EU: Debit 2645 + Credit 2614 (offsetting entries)
* 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
vatRate: number = 0.25,
isDomestic: boolean = false
): CreateJournalEntryLineInput[] {
const vatAmount = Math.round(baseAmount * vatRate * 100) / 100
// Determine accounts based on rate
// Determine output account based on rate
let outputAccount: string
switch (vatRate) {
case 0.25:
@@ -104,18 +106,22 @@ export function generateReverseChargeLines(
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: '2645', // Beräknad ingående moms förvärv utlandet
account_number: inputAccount,
debit_amount: vatAmount,
credit_amount: 0,
line_description: `Fiktiv ingående moms ${vatRate * 100}% (omvänd skattskyldighet)`,
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}% (omvänd skattskyldighet)`,
line_description: `Fiktiv utgående moms ${vatRate * 100}% (${context})`,
},
]
}