feat: per-line VAT, invoice document types, ledger-based VAT declaration, bank reconciliation, and pagination
Per-line VAT rates: - Add generatePerRateLines() to group invoice items by vat_rate with separate revenue + VAT lines per rate group (invoice-entries.ts) - Add getAvailableVatRates() and getVatTreatmentForRate() (vat-rules.ts) - PDF template shows per-line VAT column and per-rate totals for mixed-rate invoices - Invoice create/review UI supports per-line rate selection - Types: add vat_rate/vat_amount to InvoiceItem, vat_rate to CreateInvoiceItemInput Invoice document types (proforma, delivery note): - Add InvoiceDocumentType, document_type and converted_from_id to Invoice type - PDF hides prices for delivery notes, adds proforma notice - Email templates support all document types - mark-paid skips journal entries for non-invoice document types - Migration 031: invoice_document_type Accounting method support: - Add AccountingMethod type (accrual/cash) - Migration 032: add_accounting_method column to company_settings VAT declaration rewrite: - Rewrite to read directly from general ledger (26xx/3xxx account lines) instead of aggregating invoices/transactions/receipts - ACCOUNT_RUTA mapping drives momsdeklaration boxes from GL balances Bank reconciliation: - Transaction ingest now pre-fetches unlinked GL lines and attempts auto-reconciliation during import - Add transaction.reconciled event type - Add ReconciliationMethod type and reconciliation_method on Transaction - Migration 030: bank_reconciliation - New reconciliation engine, API routes, and BankReconciliationView component Pagination (fetchAllRows): - New lib/supabase/fetch-all.ts overcomes PostgREST 1000-row limit - Adopted in all report generators, SIE/SRU export, account list APIs Fiscal period validation: - New validate-period-duration.ts enforces max 18 months per BFL 3 kap. - Applied in period-service.ts and fiscal-periods API Account mapper simplification: - Remove Levenshtein/fuzzy matching, use exact account number match only Swedbank parser improvements: - Support abbreviated headers (Clnr, Bokfdag, Radnr) - Use Referens column as counterparty Chart of accounts management: - Add DELETE endpoint with system account and usage protection - PUT uses partial updates - New AccountCombobox, AddAccountDialog, EditAccountDialog, ChartOfAccountsManager Tax deadline corrections: - Rewrite inkomstdeklaration_ab using Skatteverket lookup table - Rewrite arsredovisning deadline to 7 months after FY end per ÅRL 8:3 Onboarding first fiscal year: - Add first fiscal year toggle with date pickers and 18-month validation UI terminology: - Change "okategoriserad/kategorisera" to "obokförd/bokföra" throughout Report column fix: - Fix start_date/end_date to period_start/period_end in report queries Supplier invoice input: - CreateSupplierInvoiceItemInput uses amount field (legacy quantity/unit_price kept) Misc: - SIE import uses upsert for idempotent account creation - account-descriptions.ts falls back to BAS reference data - Add invoice_default_notes to CompanySettings - Update CLAUDE.md to reflect current project state Co-Authored-By: Claude Opus 4.6 <[email protected]>
This commit is contained in:
co-authored by
Claude Opus 4.6
parent
f1d187c005
commit
91e2c1705a
@@ -1,5 +1,104 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { getRevenueAccount } from '../invoice-entries'
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
||||
import { getRevenueAccount, getOutputVatAccount } from '../invoice-entries'
|
||||
import type { Invoice, InvoiceItem, CreateJournalEntryInput } from '@/types'
|
||||
|
||||
// Mock the engine so we can capture the input passed to createJournalEntry
|
||||
vi.mock('../engine', () => ({
|
||||
findFiscalPeriod: vi.fn().mockResolvedValue('period-1'),
|
||||
createJournalEntry: vi.fn().mockImplementation(
|
||||
async (_userId: string, input: CreateJournalEntryInput) => ({
|
||||
id: 'entry-1',
|
||||
...input,
|
||||
lines: input.lines,
|
||||
})
|
||||
),
|
||||
}))
|
||||
|
||||
// Mock vat-entries to avoid indirect dependency issues
|
||||
vi.mock('../vat-entries', () => ({
|
||||
generateSalesVatLines: vi.fn().mockImplementation(({ vatTreatment, baseAmount }: { vatTreatment: string; baseAmount: number }) => {
|
||||
const rate = vatTreatment === 'standard_25' ? 0.25
|
||||
: vatTreatment === 'reduced_12' ? 0.12
|
||||
: vatTreatment === 'reduced_6' ? 0.06 : 0
|
||||
if (rate === 0) return []
|
||||
const account = vatTreatment === 'standard_25' ? '2611'
|
||||
: vatTreatment === 'reduced_12' ? '2621' : '2631'
|
||||
return [{
|
||||
account_number: account,
|
||||
debit_amount: 0,
|
||||
credit_amount: Math.round(baseAmount * rate * 100) / 100,
|
||||
line_description: `Utgående moms`,
|
||||
}]
|
||||
}),
|
||||
generateReverseChargeLines: vi.fn().mockReturnValue([]),
|
||||
}))
|
||||
|
||||
const { createJournalEntry } = await import('../engine')
|
||||
const mockedCreateEntry = vi.mocked(createJournalEntry)
|
||||
|
||||
// Import functions under test AFTER mocks are set up
|
||||
const {
|
||||
createInvoiceJournalEntry,
|
||||
createCreditNoteJournalEntry,
|
||||
createInvoiceCashEntry,
|
||||
} = await import('../invoice-entries')
|
||||
|
||||
// Helper to build a minimal Invoice with items
|
||||
function makeInvoice(overrides: Partial<Invoice> & { items?: InvoiceItem[] }): Invoice {
|
||||
return {
|
||||
id: 'inv-1',
|
||||
user_id: 'user-1',
|
||||
customer_id: 'cust-1',
|
||||
invoice_number: '1001',
|
||||
invoice_date: '2024-06-15',
|
||||
due_date: '2024-07-15',
|
||||
currency: 'SEK',
|
||||
exchange_rate: null,
|
||||
exchange_rate_date: null,
|
||||
subtotal: 1000,
|
||||
subtotal_sek: null,
|
||||
vat_amount: 250,
|
||||
vat_amount_sek: null,
|
||||
total: 1250,
|
||||
total_sek: null,
|
||||
vat_treatment: 'standard_25',
|
||||
vat_rate: 25,
|
||||
moms_ruta: '05',
|
||||
reverse_charge_text: null,
|
||||
your_reference: null,
|
||||
our_reference: null,
|
||||
notes: null,
|
||||
status: 'sent',
|
||||
sent_at: null,
|
||||
paid_at: null,
|
||||
payment_date: null,
|
||||
credited_invoice_id: null,
|
||||
journal_entry_id: null,
|
||||
payment_journal_entry_id: null,
|
||||
document_type: 'invoice',
|
||||
created_at: '2024-06-15T00:00:00Z',
|
||||
updated_at: '2024-06-15T00:00:00Z',
|
||||
items: [],
|
||||
...overrides,
|
||||
} as Invoice
|
||||
}
|
||||
|
||||
function makeItem(overrides: Partial<InvoiceItem> = {}): InvoiceItem {
|
||||
return {
|
||||
id: 'item-1',
|
||||
invoice_id: 'inv-1',
|
||||
sort_order: 0,
|
||||
description: 'Service',
|
||||
quantity: 1,
|
||||
unit: 'st',
|
||||
unit_price: 1000,
|
||||
line_total: 1000,
|
||||
vat_rate: 25,
|
||||
vat_amount: 250,
|
||||
created_at: '2024-06-15T00:00:00Z',
|
||||
...overrides,
|
||||
}
|
||||
}
|
||||
|
||||
describe('getRevenueAccount', () => {
|
||||
it('standard_25 returns 3001', () => {
|
||||
@@ -37,3 +136,255 @@ describe('getRevenueAccount', () => {
|
||||
expect(getRevenueAccount('export', 'aktiebolag')).toBe('3305')
|
||||
})
|
||||
})
|
||||
|
||||
describe('getOutputVatAccount', () => {
|
||||
it('standard_25 returns 2611', () => {
|
||||
expect(getOutputVatAccount('standard_25')).toBe('2611')
|
||||
})
|
||||
|
||||
it('reduced_12 returns 2621', () => {
|
||||
expect(getOutputVatAccount('reduced_12')).toBe('2621')
|
||||
})
|
||||
|
||||
it('reduced_6 returns 2631', () => {
|
||||
expect(getOutputVatAccount('reduced_6')).toBe('2631')
|
||||
})
|
||||
})
|
||||
|
||||
describe('createInvoiceJournalEntry — per-line VAT', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
})
|
||||
|
||||
it('single-rate invoice creates one revenue + one VAT line', async () => {
|
||||
const invoice = makeInvoice({
|
||||
subtotal: 1000,
|
||||
vat_amount: 250,
|
||||
total: 1250,
|
||||
vat_treatment: 'standard_25',
|
||||
items: [
|
||||
makeItem({ description: 'A', quantity: 2, unit_price: 300, line_total: 600, vat_rate: 25, vat_amount: 150 }),
|
||||
makeItem({ id: 'item-2', description: 'B', quantity: 1, unit_price: 400, line_total: 400, vat_rate: 25, vat_amount: 100 }),
|
||||
],
|
||||
})
|
||||
|
||||
await createInvoiceJournalEntry('user-1', invoice)
|
||||
|
||||
expect(mockedCreateEntry).toHaveBeenCalledOnce()
|
||||
const input = mockedCreateEntry.mock.calls[0][1]
|
||||
|
||||
// Should have 3 lines: 1510 debit, 3001 credit, 2611 credit
|
||||
expect(input.lines).toHaveLength(3)
|
||||
|
||||
// Debit 1510 = total
|
||||
const debit1510 = input.lines.find((l) => l.account_number === '1510')
|
||||
expect(debit1510?.debit_amount).toBe(1250)
|
||||
expect(debit1510?.credit_amount).toBe(0)
|
||||
|
||||
// Credit 3001 = subtotal
|
||||
const credit3001 = input.lines.find((l) => l.account_number === '3001')
|
||||
expect(credit3001?.debit_amount).toBe(0)
|
||||
expect(credit3001?.credit_amount).toBe(1000)
|
||||
|
||||
// Credit 2611 = VAT
|
||||
const credit2611 = input.lines.find((l) => l.account_number === '2611')
|
||||
expect(credit2611?.debit_amount).toBe(0)
|
||||
expect(credit2611?.credit_amount).toBe(250)
|
||||
})
|
||||
|
||||
it('mixed 25%/12% creates two revenue + two VAT lines', async () => {
|
||||
const invoice = makeInvoice({
|
||||
subtotal: 1000,
|
||||
vat_amount: 184, // 600*0.25 + 400*0.12 = 150 + 48 = 198... let's recalc
|
||||
total: 1198,
|
||||
vat_treatment: 'standard_25',
|
||||
vat_rate: null as unknown as number,
|
||||
items: [
|
||||
makeItem({ description: 'Consulting', quantity: 1, unit_price: 600, line_total: 600, vat_rate: 25, vat_amount: 150 }),
|
||||
makeItem({ id: 'item-2', description: 'Food service', quantity: 1, unit_price: 400, line_total: 400, vat_rate: 12, vat_amount: 48 }),
|
||||
],
|
||||
})
|
||||
invoice.vat_amount = 198
|
||||
invoice.total = 1198
|
||||
|
||||
await createInvoiceJournalEntry('user-1', invoice)
|
||||
|
||||
expect(mockedCreateEntry).toHaveBeenCalledOnce()
|
||||
const input = mockedCreateEntry.mock.calls[0][1]
|
||||
|
||||
// Should have 5 lines: 1510, 3001(25%), 2611(25%), 3002(12%), 2621(12%)
|
||||
expect(input.lines).toHaveLength(5)
|
||||
|
||||
// Debit 1510 = total
|
||||
const debit1510 = input.lines.find((l) => l.account_number === '1510')
|
||||
expect(debit1510?.debit_amount).toBe(1198)
|
||||
|
||||
// Revenue 3001 (25% group)
|
||||
const credit3001 = input.lines.find((l) => l.account_number === '3001')
|
||||
expect(credit3001?.credit_amount).toBe(600)
|
||||
|
||||
// VAT 2611 (25% group)
|
||||
const credit2611 = input.lines.find((l) => l.account_number === '2611')
|
||||
expect(credit2611?.credit_amount).toBe(150)
|
||||
|
||||
// Revenue 3002 (12% group)
|
||||
const credit3002 = input.lines.find((l) => l.account_number === '3002')
|
||||
expect(credit3002?.credit_amount).toBe(400)
|
||||
|
||||
// VAT 2621 (12% group)
|
||||
const credit2621 = input.lines.find((l) => l.account_number === '2621')
|
||||
expect(credit2621?.credit_amount).toBe(48)
|
||||
})
|
||||
|
||||
it('reverse charge creates single 3308, no VAT lines', async () => {
|
||||
const invoice = makeInvoice({
|
||||
subtotal: 5000,
|
||||
vat_amount: 0,
|
||||
total: 5000,
|
||||
vat_treatment: 'reverse_charge',
|
||||
vat_rate: 0,
|
||||
items: [
|
||||
makeItem({ quantity: 1, unit_price: 5000, line_total: 5000, vat_rate: 0, vat_amount: 0 }),
|
||||
],
|
||||
})
|
||||
|
||||
await createInvoiceJournalEntry('user-1', invoice)
|
||||
|
||||
expect(mockedCreateEntry).toHaveBeenCalledOnce()
|
||||
const input = mockedCreateEntry.mock.calls[0][1]
|
||||
|
||||
// Should have 2 lines: 1510 debit, 3308 credit (no VAT)
|
||||
expect(input.lines).toHaveLength(2)
|
||||
|
||||
const debit1510 = input.lines.find((l) => l.account_number === '1510')
|
||||
expect(debit1510?.debit_amount).toBe(5000)
|
||||
|
||||
const credit3308 = input.lines.find((l) => l.account_number === '3308')
|
||||
expect(credit3308?.credit_amount).toBe(5000)
|
||||
|
||||
// No VAT lines
|
||||
const vatLines = input.lines.filter((l) =>
|
||||
l.account_number.startsWith('26')
|
||||
)
|
||||
expect(vatLines).toHaveLength(0)
|
||||
})
|
||||
|
||||
it('balance: debit(1510) = sum(revenue + VAT credits)', async () => {
|
||||
const invoice = makeInvoice({
|
||||
subtotal: 2000,
|
||||
vat_amount: 380, // 1200*0.25 + 500*0.12 + 300*0.06 = 300 + 60 + 18 = 378
|
||||
total: 2378,
|
||||
vat_treatment: 'standard_25',
|
||||
vat_rate: null as unknown as number,
|
||||
items: [
|
||||
makeItem({ description: 'A', quantity: 1, unit_price: 1200, line_total: 1200, vat_rate: 25, vat_amount: 300 }),
|
||||
makeItem({ id: 'item-2', description: 'B', quantity: 1, unit_price: 500, line_total: 500, vat_rate: 12, vat_amount: 60 }),
|
||||
makeItem({ id: 'item-3', description: 'C', quantity: 1, unit_price: 300, line_total: 300, vat_rate: 6, vat_amount: 18 }),
|
||||
],
|
||||
})
|
||||
invoice.vat_amount = 378
|
||||
invoice.total = 2378
|
||||
|
||||
await createInvoiceJournalEntry('user-1', invoice)
|
||||
|
||||
const input = mockedCreateEntry.mock.calls[0][1]
|
||||
|
||||
const totalDebit = input.lines.reduce((sum, l) => sum + l.debit_amount, 0)
|
||||
const totalCredit = input.lines.reduce((sum, l) => sum + l.credit_amount, 0)
|
||||
|
||||
expect(totalDebit).toBe(totalCredit)
|
||||
expect(totalDebit).toBe(2378)
|
||||
})
|
||||
})
|
||||
|
||||
describe('createCreditNoteJournalEntry — per-line VAT', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
})
|
||||
|
||||
it('reverses per-rate lines correctly for mixed rates', async () => {
|
||||
const creditNote = makeInvoice({
|
||||
invoice_number: 'KR-1001',
|
||||
subtotal: -1000,
|
||||
vat_amount: -198,
|
||||
total: -1198,
|
||||
vat_treatment: 'standard_25',
|
||||
items: [
|
||||
makeItem({ quantity: -1, unit_price: 600, line_total: -600, vat_rate: 25, vat_amount: -150 }),
|
||||
makeItem({ id: 'item-2', quantity: -1, unit_price: 400, line_total: -400, vat_rate: 12, vat_amount: -48 }),
|
||||
],
|
||||
})
|
||||
|
||||
await createCreditNoteJournalEntry('user-1', creditNote)
|
||||
|
||||
expect(mockedCreateEntry).toHaveBeenCalledOnce()
|
||||
const input = mockedCreateEntry.mock.calls[0][1]
|
||||
|
||||
// Revenue and VAT lines should be debits (reversed)
|
||||
const debit3001 = input.lines.find((l) => l.account_number === '3001')
|
||||
expect(debit3001?.debit_amount).toBe(600)
|
||||
expect(debit3001?.credit_amount).toBe(0)
|
||||
|
||||
const debit2611 = input.lines.find((l) => l.account_number === '2611')
|
||||
expect(debit2611?.debit_amount).toBe(150)
|
||||
|
||||
const debit3002 = input.lines.find((l) => l.account_number === '3002')
|
||||
expect(debit3002?.debit_amount).toBe(400)
|
||||
|
||||
const debit2621 = input.lines.find((l) => l.account_number === '2621')
|
||||
expect(debit2621?.debit_amount).toBe(48)
|
||||
|
||||
// 1510 should be credit
|
||||
const credit1510 = input.lines.find((l) => l.account_number === '1510')
|
||||
expect(credit1510?.credit_amount).toBe(1198)
|
||||
expect(credit1510?.debit_amount).toBe(0)
|
||||
|
||||
// Balance check
|
||||
const totalDebit = input.lines.reduce((sum, l) => sum + l.debit_amount, 0)
|
||||
const totalCredit = input.lines.reduce((sum, l) => sum + l.credit_amount, 0)
|
||||
expect(totalDebit).toBe(totalCredit)
|
||||
})
|
||||
})
|
||||
|
||||
describe('createInvoiceCashEntry — per-line VAT', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
})
|
||||
|
||||
it('cash method with mixed rates creates per-rate revenue + VAT', async () => {
|
||||
const invoice = makeInvoice({
|
||||
subtotal: 1000,
|
||||
vat_amount: 198,
|
||||
total: 1198,
|
||||
vat_treatment: 'standard_25',
|
||||
items: [
|
||||
makeItem({ quantity: 1, unit_price: 600, line_total: 600, vat_rate: 25, vat_amount: 150 }),
|
||||
makeItem({ id: 'item-2', quantity: 1, unit_price: 400, line_total: 400, vat_rate: 12, vat_amount: 48 }),
|
||||
],
|
||||
})
|
||||
|
||||
await createInvoiceCashEntry('user-1', invoice, '2024-07-01')
|
||||
|
||||
expect(mockedCreateEntry).toHaveBeenCalledOnce()
|
||||
const input = mockedCreateEntry.mock.calls[0][1]
|
||||
|
||||
// Debit 1930 (bank account) instead of 1510
|
||||
const debit1930 = input.lines.find((l) => l.account_number === '1930')
|
||||
expect(debit1930?.debit_amount).toBe(1198)
|
||||
|
||||
// Same per-rate credits as accrual
|
||||
const credit3001 = input.lines.find((l) => l.account_number === '3001')
|
||||
expect(credit3001?.credit_amount).toBe(600)
|
||||
|
||||
const credit2611 = input.lines.find((l) => l.account_number === '2611')
|
||||
expect(credit2611?.credit_amount).toBe(150)
|
||||
|
||||
const credit3002 = input.lines.find((l) => l.account_number === '3002')
|
||||
expect(credit3002?.credit_amount).toBe(400)
|
||||
|
||||
// Balance
|
||||
const totalDebit = input.lines.reduce((sum, l) => sum + l.debit_amount, 0)
|
||||
const totalCredit = input.lines.reduce((sum, l) => sum + l.credit_amount, 0)
|
||||
expect(totalDebit).toBe(totalCredit)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { validatePeriodDuration, monthsBetween } from '../validate-period-duration'
|
||||
|
||||
describe('monthsBetween', () => {
|
||||
it('returns 12 for a standard calendar year', () => {
|
||||
expect(monthsBetween('2025-01-01', '2025-12-31')).toBe(12)
|
||||
})
|
||||
|
||||
it('returns 18 for an 18-month period', () => {
|
||||
expect(monthsBetween('2025-07-01', '2026-12-31')).toBe(18)
|
||||
})
|
||||
|
||||
it('returns 1 for a single month', () => {
|
||||
expect(monthsBetween('2025-03-01', '2025-03-31')).toBe(1)
|
||||
})
|
||||
|
||||
it('returns 3 for a quarter', () => {
|
||||
expect(monthsBetween('2025-10-01', '2025-12-31')).toBe(3)
|
||||
})
|
||||
})
|
||||
|
||||
describe('validatePeriodDuration', () => {
|
||||
it('returns null for a valid 12-month period', () => {
|
||||
expect(validatePeriodDuration('2025-01-01', '2025-12-31')).toBeNull()
|
||||
})
|
||||
|
||||
it('returns null for exactly 18 months (max allowed)', () => {
|
||||
expect(validatePeriodDuration('2025-07-01', '2026-12-31')).toBeNull()
|
||||
})
|
||||
|
||||
it('returns null for a short first year (1 month)', () => {
|
||||
expect(validatePeriodDuration('2025-12-01', '2025-12-31')).toBeNull()
|
||||
})
|
||||
|
||||
it('returns null for a short first year (3 months)', () => {
|
||||
expect(validatePeriodDuration('2025-10-01', '2025-12-31')).toBeNull()
|
||||
})
|
||||
|
||||
it('returns error for 19 months (exceeds max)', () => {
|
||||
const result = validatePeriodDuration('2025-06-01', '2026-12-31')
|
||||
expect(result).toContain('19 months')
|
||||
expect(result).toContain('18 months')
|
||||
})
|
||||
|
||||
it('returns error when end is before start', () => {
|
||||
expect(validatePeriodDuration('2025-06-01', '2025-01-31')).toBe(
|
||||
'Period end must be after period start'
|
||||
)
|
||||
})
|
||||
|
||||
it('returns error when start is not 1st of month', () => {
|
||||
expect(validatePeriodDuration('2025-01-15', '2025-12-31')).toBe(
|
||||
'Period start must be the 1st of a month'
|
||||
)
|
||||
})
|
||||
|
||||
it('returns error when end is not last day of month', () => {
|
||||
expect(validatePeriodDuration('2025-01-01', '2025-12-15')).toBe(
|
||||
'Period end must be the last day of a month'
|
||||
)
|
||||
})
|
||||
|
||||
it('handles February end correctly (non-leap year)', () => {
|
||||
expect(validatePeriodDuration('2025-01-01', '2025-02-28')).toBeNull()
|
||||
})
|
||||
|
||||
it('handles February end correctly (leap year)', () => {
|
||||
expect(validatePeriodDuration('2024-01-01', '2024-02-29')).toBeNull()
|
||||
})
|
||||
|
||||
it('rejects Feb 28 in a leap year (not last day)', () => {
|
||||
expect(validatePeriodDuration('2024-01-01', '2024-02-28')).toBe(
|
||||
'Period end must be the last day of a month'
|
||||
)
|
||||
})
|
||||
|
||||
it('returns null for a broken fiscal year (May-Apr)', () => {
|
||||
expect(validatePeriodDuration('2025-05-01', '2026-04-30')).toBeNull()
|
||||
})
|
||||
})
|
||||
@@ -326,7 +326,55 @@ const ACCOUNT_DESCRIPTIONS: Record<string, AccountDescription> = {
|
||||
}
|
||||
|
||||
export function getAccountDescription(accountNumber: string): AccountDescription | undefined {
|
||||
return ACCOUNT_DESCRIPTIONS[accountNumber]
|
||||
// Check hardcoded descriptions first (most detailed explanations)
|
||||
const hardcoded = ACCOUNT_DESCRIPTIONS[accountNumber]
|
||||
if (hardcoded) return hardcoded
|
||||
|
||||
// Fall back to BAS reference data for accounts not in the hardcoded list
|
||||
try {
|
||||
// Dynamic import avoided — use lazy require pattern
|
||||
const { getBASReference, ACCOUNT_CLASS_LABELS } = require('./bas-reference')
|
||||
const ref = getBASReference(accountNumber)
|
||||
if (ref) {
|
||||
const classLabel = ACCOUNT_CLASS_LABELS[ref.account_class] || ''
|
||||
return {
|
||||
name: ref.account_name,
|
||||
classLabel,
|
||||
type: ref.account_type,
|
||||
explanation: ref.description,
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
// BAS reference not available — that's fine
|
||||
}
|
||||
|
||||
return undefined
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a human-readable account class name for BAS account classes.
|
||||
*/
|
||||
export function getAccountClassName(accountClass: number): string {
|
||||
switch (accountClass) {
|
||||
case 1:
|
||||
return '1xxx - Tillgångar'
|
||||
case 2:
|
||||
return '2xxx - Eget kapital & Skulder'
|
||||
case 3:
|
||||
return '3xxx - Intäkter'
|
||||
case 4:
|
||||
return '4xxx - Varuinköp'
|
||||
case 5:
|
||||
return '5xxx - Externa kostnader'
|
||||
case 6:
|
||||
return '6xxx - Övriga externa kostnader'
|
||||
case 7:
|
||||
return '7xxx - Personal'
|
||||
case 8:
|
||||
return '8xxx - Finansiella poster'
|
||||
default:
|
||||
return `${accountClass}xxx - Övrigt`
|
||||
}
|
||||
}
|
||||
|
||||
export { ACCOUNT_DESCRIPTIONS }
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,21 +1,104 @@
|
||||
import { createJournalEntry, findFiscalPeriod } from './engine'
|
||||
import { generateSalesVatLines, generateReverseChargeLines } from './vat-entries'
|
||||
import { getVatTreatmentForRate } from '@/lib/invoice/vat-rules'
|
||||
import type {
|
||||
CreateJournalEntryInput,
|
||||
CreateJournalEntryLineInput,
|
||||
EntityType,
|
||||
Invoice,
|
||||
InvoiceItem,
|
||||
JournalEntry,
|
||||
VatTreatment,
|
||||
} from '@/types'
|
||||
|
||||
/**
|
||||
* Group invoice items by VAT rate and generate per-rate revenue + VAT lines.
|
||||
* Returns credit lines only (revenue + VAT). The caller adds the debit side.
|
||||
*/
|
||||
function generatePerRateLines(
|
||||
items: InvoiceItem[],
|
||||
invoiceVatTreatment: VatTreatment,
|
||||
entityType: EntityType,
|
||||
invoiceNumber: string
|
||||
): CreateJournalEntryLineInput[] {
|
||||
const lines: CreateJournalEntryLineInput[] = []
|
||||
|
||||
// Check if items have per-line vat_rate set (new invoices)
|
||||
const hasPerLineVat = items.some((item) => item.vat_rate !== undefined && item.vat_rate !== null)
|
||||
|
||||
if (!hasPerLineVat) {
|
||||
// Legacy fallback: single rate from invoice level
|
||||
const revenueAccount = getRevenueAccount(invoiceVatTreatment, entityType)
|
||||
const subtotal = items.reduce((sum, item) => sum + item.line_total, 0)
|
||||
lines.push({
|
||||
account_number: revenueAccount,
|
||||
debit_amount: 0,
|
||||
credit_amount: subtotal,
|
||||
line_description: `Försäljning faktura ${invoiceNumber}`,
|
||||
})
|
||||
|
||||
const totalVat = items.reduce((sum, item) => sum + (item.vat_amount || 0), 0)
|
||||
if (totalVat > 0) {
|
||||
const vatLines = generateSalesVatLines({
|
||||
vatTreatment: invoiceVatTreatment,
|
||||
baseAmount: subtotal,
|
||||
direction: 'sales',
|
||||
})
|
||||
lines.push(...vatLines)
|
||||
}
|
||||
return lines
|
||||
}
|
||||
|
||||
// Group items by vat_rate
|
||||
const rateGroups = new Map<number, { subtotal: number; vatAmount: number }>()
|
||||
for (const item of items) {
|
||||
const rate = item.vat_rate ?? 25
|
||||
const group = rateGroups.get(rate) || { subtotal: 0, vatAmount: 0 }
|
||||
group.subtotal += item.line_total
|
||||
group.vatAmount += item.vat_amount || 0
|
||||
rateGroups.set(rate, group)
|
||||
}
|
||||
|
||||
// Generate revenue + VAT lines per rate group
|
||||
for (const [rate, group] of rateGroups) {
|
||||
const treatment = rate === 0 && (invoiceVatTreatment === 'reverse_charge' || invoiceVatTreatment === 'export')
|
||||
? invoiceVatTreatment
|
||||
: getVatTreatmentForRate(rate)
|
||||
const revenueAccount = getRevenueAccount(treatment, entityType)
|
||||
const roundedSubtotal = Math.round(group.subtotal * 100) / 100
|
||||
|
||||
lines.push({
|
||||
account_number: revenueAccount,
|
||||
debit_amount: 0,
|
||||
credit_amount: roundedSubtotal,
|
||||
line_description: `Försäljning faktura ${invoiceNumber}`,
|
||||
})
|
||||
|
||||
const roundedVat = Math.round(group.vatAmount * 100) / 100
|
||||
if (roundedVat !== 0) {
|
||||
const vatAccount = getOutputVatAccount(treatment)
|
||||
lines.push({
|
||||
account_number: vatAccount,
|
||||
debit_amount: 0,
|
||||
credit_amount: roundedVat,
|
||||
line_description: `Utgående moms ${rate}%`,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return lines
|
||||
}
|
||||
|
||||
/**
|
||||
* Create journal entry when an invoice is created (status != draft)
|
||||
*
|
||||
* Supports mixed VAT rates per line item. Groups items by vat_rate
|
||||
* and creates separate revenue + VAT lines per rate.
|
||||
*
|
||||
* Standard domestic invoice (25% VAT):
|
||||
* Debit 1510 Kundfordringar [total incl VAT]
|
||||
* Credit 30xx Försäljning [subtotal]
|
||||
* Credit 2611 Utgående moms 25% [vat_amount]
|
||||
* Credit 30xx Försäljning [subtotal per rate]
|
||||
* Credit 26xx Utgående moms [vat per rate]
|
||||
*
|
||||
* EU reverse charge:
|
||||
* Debit 1510 Kundfordringar [subtotal]
|
||||
@@ -38,9 +121,6 @@ export async function createInvoiceJournalEntry(
|
||||
|
||||
const lines: CreateJournalEntryLineInput[] = []
|
||||
|
||||
// Determine revenue account based on VAT treatment
|
||||
const revenueAccount = getRevenueAccount(invoice.vat_treatment, entityType)
|
||||
|
||||
// Debit: Kundfordringar (total including VAT)
|
||||
lines.push({
|
||||
account_number: '1510',
|
||||
@@ -52,22 +132,27 @@ export async function createInvoiceJournalEntry(
|
||||
exchange_rate: invoice.exchange_rate || undefined,
|
||||
})
|
||||
|
||||
// Credit: Revenue account (subtotal, excl VAT)
|
||||
lines.push({
|
||||
account_number: revenueAccount,
|
||||
debit_amount: 0,
|
||||
credit_amount: invoice.subtotal,
|
||||
line_description: `Försäljning faktura ${invoice.invoice_number}`,
|
||||
})
|
||||
|
||||
// VAT lines (if applicable)
|
||||
if (invoice.vat_amount > 0) {
|
||||
const vatLines = generateSalesVatLines({
|
||||
vatTreatment: invoice.vat_treatment,
|
||||
baseAmount: invoice.subtotal,
|
||||
direction: 'sales',
|
||||
// Credit lines: revenue + VAT per rate group
|
||||
if (invoice.items && invoice.items.length > 0) {
|
||||
lines.push(...generatePerRateLines(invoice.items, invoice.vat_treatment, entityType, invoice.invoice_number))
|
||||
} else {
|
||||
// Fallback: no items available, use invoice-level amounts
|
||||
const revenueAccount = getRevenueAccount(invoice.vat_treatment, entityType)
|
||||
lines.push({
|
||||
account_number: revenueAccount,
|
||||
debit_amount: 0,
|
||||
credit_amount: invoice.subtotal,
|
||||
line_description: `Försäljning faktura ${invoice.invoice_number}`,
|
||||
})
|
||||
lines.push(...vatLines)
|
||||
|
||||
if (invoice.vat_amount > 0) {
|
||||
const vatLines = generateSalesVatLines({
|
||||
vatTreatment: invoice.vat_treatment,
|
||||
baseAmount: invoice.subtotal,
|
||||
direction: 'sales',
|
||||
})
|
||||
lines.push(...vatLines)
|
||||
}
|
||||
}
|
||||
|
||||
const input: CreateJournalEntryInput = {
|
||||
@@ -128,9 +213,10 @@ export async function createInvoicePaymentJournalEntry(
|
||||
|
||||
/**
|
||||
* Create journal entry for a credit note (reversed version of original invoice entry)
|
||||
* Supports per-item VAT rates with reversed debit/credit sides.
|
||||
*
|
||||
* Debit 30xx Försäljning [subtotal]
|
||||
* Debit 26xx Utgående moms [vat_amount]
|
||||
* Debit 30xx Försäljning [subtotal per rate]
|
||||
* Debit 26xx Utgående moms [vat per rate]
|
||||
* Credit 1510 Kundfordringar [total]
|
||||
*/
|
||||
export async function createCreditNoteJournalEntry(
|
||||
@@ -144,30 +230,43 @@ export async function createCreditNoteJournalEntry(
|
||||
return null
|
||||
}
|
||||
|
||||
const revenueAccount = getRevenueAccount(creditNote.vat_treatment, entityType)
|
||||
const absSubtotal = Math.abs(creditNote.subtotal)
|
||||
const absVat = Math.abs(creditNote.vat_amount)
|
||||
const absTotal = Math.abs(creditNote.total)
|
||||
|
||||
const lines: CreateJournalEntryLineInput[] = []
|
||||
|
||||
// Debit: Revenue account (reverse the credit)
|
||||
lines.push({
|
||||
account_number: revenueAccount,
|
||||
debit_amount: absSubtotal,
|
||||
credit_amount: 0,
|
||||
line_description: `Kreditfaktura ${creditNote.invoice_number}`,
|
||||
})
|
||||
// Generate reversed revenue + VAT lines per rate group
|
||||
if (creditNote.items && creditNote.items.length > 0) {
|
||||
const creditLines = generatePerRateLines(creditNote.items, creditNote.vat_treatment, entityType, creditNote.invoice_number)
|
||||
// Swap debit/credit for credit note reversal (make amounts absolute first)
|
||||
for (const line of creditLines) {
|
||||
lines.push({
|
||||
...line,
|
||||
debit_amount: Math.abs(line.credit_amount),
|
||||
credit_amount: Math.abs(line.debit_amount),
|
||||
line_description: `Kreditfaktura ${creditNote.invoice_number}`,
|
||||
})
|
||||
}
|
||||
} else {
|
||||
// Fallback: invoice-level amounts
|
||||
const revenueAccount = getRevenueAccount(creditNote.vat_treatment, entityType)
|
||||
const absSubtotal = Math.abs(creditNote.subtotal)
|
||||
const absVat = Math.abs(creditNote.vat_amount)
|
||||
|
||||
// Debit: VAT account (reverse the credit, if applicable)
|
||||
if (absVat > 0) {
|
||||
const vatAccount = getOutputVatAccount(creditNote.vat_treatment)
|
||||
lines.push({
|
||||
account_number: vatAccount,
|
||||
debit_amount: absVat,
|
||||
account_number: revenueAccount,
|
||||
debit_amount: absSubtotal,
|
||||
credit_amount: 0,
|
||||
line_description: `Moms kreditfaktura ${creditNote.invoice_number}`,
|
||||
line_description: `Kreditfaktura ${creditNote.invoice_number}`,
|
||||
})
|
||||
|
||||
if (absVat > 0) {
|
||||
const vatAccount = getOutputVatAccount(creditNote.vat_treatment)
|
||||
lines.push({
|
||||
account_number: vatAccount,
|
||||
debit_amount: absVat,
|
||||
credit_amount: 0,
|
||||
line_description: `Moms kreditfaktura ${creditNote.invoice_number}`,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Credit: Kundfordringar (reverse the debit)
|
||||
@@ -192,11 +291,11 @@ export async function createCreditNoteJournalEntry(
|
||||
|
||||
/**
|
||||
* Create journal entry for kontantmetoden (cash method) when payment is received.
|
||||
* Combined entry: revenue + VAT recognised at payment.
|
||||
* Supports per-item VAT rates. Revenue + VAT recognised at payment.
|
||||
*
|
||||
* Debit 1930 Företagskonto [total]
|
||||
* Credit 30xx Försäljning [subtotal]
|
||||
* Credit 26xx Utgående moms [vat_amount] (if applicable)
|
||||
* Credit 30xx Försäljning [subtotal per rate]
|
||||
* Credit 26xx Utgående moms [vat per rate] (if applicable)
|
||||
*/
|
||||
export async function createInvoiceCashEntry(
|
||||
userId: string,
|
||||
@@ -210,7 +309,6 @@ export async function createInvoiceCashEntry(
|
||||
return null
|
||||
}
|
||||
|
||||
const revenueAccount = getRevenueAccount(invoice.vat_treatment, entityType)
|
||||
const lines: CreateJournalEntryLineInput[] = []
|
||||
|
||||
// Debit: Företagskonto (total received)
|
||||
@@ -221,23 +319,28 @@ export async function createInvoiceCashEntry(
|
||||
line_description: `Betalning faktura ${invoice.invoice_number}`,
|
||||
})
|
||||
|
||||
// Credit: Revenue account (subtotal excl VAT)
|
||||
lines.push({
|
||||
account_number: revenueAccount,
|
||||
debit_amount: 0,
|
||||
credit_amount: invoice.subtotal,
|
||||
line_description: `Försäljning faktura ${invoice.invoice_number}`,
|
||||
})
|
||||
|
||||
// Credit: Output VAT (if applicable)
|
||||
if (invoice.vat_amount > 0) {
|
||||
const vatAccount = getOutputVatAccount(invoice.vat_treatment)
|
||||
// Credit lines: revenue + VAT per rate group
|
||||
if (invoice.items && invoice.items.length > 0) {
|
||||
lines.push(...generatePerRateLines(invoice.items, invoice.vat_treatment, entityType, invoice.invoice_number))
|
||||
} else {
|
||||
// Fallback: invoice-level amounts
|
||||
const revenueAccount = getRevenueAccount(invoice.vat_treatment, entityType)
|
||||
lines.push({
|
||||
account_number: vatAccount,
|
||||
account_number: revenueAccount,
|
||||
debit_amount: 0,
|
||||
credit_amount: invoice.vat_amount,
|
||||
line_description: `Utgående moms faktura ${invoice.invoice_number}`,
|
||||
credit_amount: invoice.subtotal,
|
||||
line_description: `Försäljning faktura ${invoice.invoice_number}`,
|
||||
})
|
||||
|
||||
if (invoice.vat_amount > 0) {
|
||||
const vatAccount = getOutputVatAccount(invoice.vat_treatment)
|
||||
lines.push({
|
||||
account_number: vatAccount,
|
||||
debit_amount: 0,
|
||||
credit_amount: invoice.vat_amount,
|
||||
line_description: `Utgående moms faktura ${invoice.invoice_number}`,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
const input: CreateJournalEntryInput = {
|
||||
|
||||
@@ -190,7 +190,7 @@ function getDefaultResult(transaction: Transaction): MappingResult {
|
||||
requires_review: true,
|
||||
default_private: false,
|
||||
vat_lines: [],
|
||||
description: 'Okategoriserad transaktion',
|
||||
description: 'Obokförd transaktion',
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
/**
|
||||
* Validates fiscal period duration per BFL 3 kap.
|
||||
* Maximum 18 months for any fiscal period (first year may be extended).
|
||||
* Normal ongoing periods are 12 months.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Calculate the number of months between two dates (inclusive of partial months).
|
||||
* Assumes start is 1st of month and end is last of month.
|
||||
*/
|
||||
export function monthsBetween(start: string, end: string): number {
|
||||
const s = new Date(start)
|
||||
const e = new Date(end)
|
||||
return (e.getFullYear() - s.getFullYear()) * 12 + (e.getMonth() - s.getMonth()) + 1
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate a fiscal period's duration and date constraints.
|
||||
* Returns null if valid, or an error message string if invalid.
|
||||
*/
|
||||
export function validatePeriodDuration(start: string, end: string): string | null {
|
||||
const startDate = new Date(start)
|
||||
const endDate = new Date(end)
|
||||
|
||||
// end must be after start
|
||||
if (endDate <= startDate) {
|
||||
return 'Period end must be after period start'
|
||||
}
|
||||
|
||||
// start must be 1st of month
|
||||
if (startDate.getDate() !== 1) {
|
||||
return 'Period start must be the 1st of a month'
|
||||
}
|
||||
|
||||
// end must be last day of month
|
||||
const lastDayOfEndMonth = new Date(endDate.getFullYear(), endDate.getMonth() + 1, 0).getDate()
|
||||
if (endDate.getDate() !== lastDayOfEndMonth) {
|
||||
return 'Period end must be the last day of a month'
|
||||
}
|
||||
|
||||
// Max 18 months per BFL 3 kap.
|
||||
const months = monthsBetween(start, end)
|
||||
if (months > 18) {
|
||||
return `Period duration ${months} months exceeds maximum 18 months (BFL 3 kap.)`
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
import { createClient } from '@/lib/supabase/server'
|
||||
import { eventBus } from '@/lib/events'
|
||||
import { validatePeriodDuration } from '@/lib/bookkeeping/validate-period-duration'
|
||||
import type { FiscalPeriod, PeriodStatus } from '@/types'
|
||||
|
||||
/**
|
||||
@@ -161,6 +162,12 @@ export async function createNextPeriod(
|
||||
const nextStartStr = nextStart.toISOString().split('T')[0]
|
||||
const nextEndStr = nextEnd.toISOString().split('T')[0]
|
||||
|
||||
// Validate period duration (max 18 months per BFL 3 kap.)
|
||||
const durationError = validatePeriodDuration(nextStartStr, nextEndStr)
|
||||
if (durationError) {
|
||||
throw new Error(durationError)
|
||||
}
|
||||
|
||||
// Generate name: e.g. "FY 2025" or "FY 2025/2026"
|
||||
const startYear = nextStart.getFullYear()
|
||||
const endYear = nextEnd.getFullYear()
|
||||
|
||||
@@ -1,6 +1,14 @@
|
||||
import type { Invoice, Customer, CompanySettings } from '@/types'
|
||||
import type { Invoice, Customer, CompanySettings, InvoiceDocumentType } from '@/types'
|
||||
import { formatCurrency, formatDate } from '@/lib/utils'
|
||||
|
||||
function getDocumentLabel(invoice: Invoice): string {
|
||||
if (invoice.credited_invoice_id) return 'Kreditfaktura'
|
||||
const docType = (invoice as Invoice & { document_type?: InvoiceDocumentType }).document_type || 'invoice'
|
||||
if (docType === 'proforma') return 'Proformafaktura'
|
||||
if (docType === 'delivery_note') return 'Följesedel'
|
||||
return 'Faktura'
|
||||
}
|
||||
|
||||
export interface InvoiceEmailData {
|
||||
invoice: Invoice
|
||||
customer: Customer
|
||||
@@ -13,8 +21,12 @@ export interface InvoiceEmailData {
|
||||
export function generateInvoiceEmailHtml(data: InvoiceEmailData): string {
|
||||
const { invoice, customer, company } = data
|
||||
|
||||
const documentType = getDocumentLabel(invoice)
|
||||
const isCreditNote = !!invoice.credited_invoice_id
|
||||
const documentType = isCreditNote ? 'Kreditfaktura' : 'Faktura'
|
||||
const docType = (invoice as Invoice & { document_type?: InvoiceDocumentType }).document_type || 'invoice'
|
||||
const isDeliveryNote = docType === 'delivery_note'
|
||||
const isProforma = docType === 'proforma'
|
||||
const hidePayment = isCreditNote || isDeliveryNote || isProforma
|
||||
|
||||
return `
|
||||
<!DOCTYPE html>
|
||||
@@ -79,7 +91,7 @@ export function generateInvoiceEmailHtml(data: InvoiceEmailData): string {
|
||||
</div>
|
||||
|
||||
<!-- Payment Details -->
|
||||
${!isCreditNote ? `
|
||||
${!hidePayment ? `
|
||||
<div style="margin-bottom: 30px;">
|
||||
<h2 style="margin: 0 0 15px 0; font-size: 16px; font-weight: 600; color: #111;">
|
||||
Betalningsinformation
|
||||
@@ -146,8 +158,12 @@ export function generateInvoiceEmailHtml(data: InvoiceEmailData): string {
|
||||
export function generateInvoiceEmailText(data: InvoiceEmailData): string {
|
||||
const { invoice, customer, company } = data
|
||||
|
||||
const documentType = getDocumentLabel(invoice)
|
||||
const isCreditNote = !!invoice.credited_invoice_id
|
||||
const documentType = isCreditNote ? 'Kreditfaktura' : 'Faktura'
|
||||
const docType = (invoice as Invoice & { document_type?: InvoiceDocumentType }).document_type || 'invoice'
|
||||
const isDeliveryNote = docType === 'delivery_note'
|
||||
const isProforma = docType === 'proforma'
|
||||
const hidePayment = isCreditNote || isDeliveryNote || isProforma
|
||||
|
||||
let text = `${documentType} från ${company.company_name}\n`
|
||||
text += `${documentType}nummer: ${invoice.invoice_number}\n\n`
|
||||
@@ -168,7 +184,7 @@ export function generateInvoiceEmailText(data: InvoiceEmailData): string {
|
||||
text += `Att betala: ${formatCurrency(invoice.total, invoice.currency)}\n`
|
||||
text += `---\n\n`
|
||||
|
||||
if (!isCreditNote) {
|
||||
if (!hidePayment) {
|
||||
text += `Betalningsinformation:\n`
|
||||
if (company.bank_name) text += `Bank: ${company.bank_name}\n`
|
||||
if (company.clearing_number && company.account_number) {
|
||||
@@ -198,8 +214,7 @@ export function generateInvoiceEmailText(data: InvoiceEmailData): string {
|
||||
*/
|
||||
export function generateInvoiceEmailSubject(data: InvoiceEmailData): string {
|
||||
const { invoice, company } = data
|
||||
const isCreditNote = !!invoice.credited_invoice_id
|
||||
const documentType = isCreditNote ? 'Kreditfaktura' : 'Faktura'
|
||||
const documentType = getDocumentLabel(invoice)
|
||||
|
||||
return `${documentType} ${invoice.invoice_number} från ${company.company_name}`
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ import type {
|
||||
CAMT053Statement,
|
||||
CAMT054Notification,
|
||||
AuditSecurityEvent,
|
||||
ReconciliationMethod,
|
||||
} from '@/types'
|
||||
|
||||
// ============================================================
|
||||
@@ -32,6 +33,7 @@ export type CoreEvent =
|
||||
// Banking
|
||||
| { type: 'transaction.synced'; payload: { transactions: Transaction[]; userId: string } }
|
||||
| { type: 'transaction.categorized'; payload: { transaction: Transaction; account: string; taxCode: string; userId: string } }
|
||||
| { type: 'transaction.reconciled'; payload: { transaction: Transaction; journalEntryId: string; method: ReconciliationMethod; userId: string } }
|
||||
| { type: 'bank.statement_received'; payload: { statement: CAMT053Statement; userId: string } }
|
||||
| { type: 'bank.payment_notification'; payload: { notification: CAMT054Notification; userId: string } }
|
||||
// Periods
|
||||
|
||||
@@ -0,0 +1,351 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import type { BASAccount } from '@/types'
|
||||
import type { SIEAccount, SIEAccountMappingRecord } from '../types'
|
||||
import {
|
||||
suggestMappings,
|
||||
validateMappings,
|
||||
getMappingStats,
|
||||
applyMappingOverride,
|
||||
mappingsToMap,
|
||||
} from '../account-mapper'
|
||||
|
||||
// --- Helpers ---
|
||||
|
||||
function makeBASAccount(number: string, name: string): BASAccount {
|
||||
const classNum = parseInt(number.charAt(0), 10)
|
||||
const accountType =
|
||||
classNum <= 1
|
||||
? 'asset'
|
||||
: classNum === 2
|
||||
? 'liability'
|
||||
: classNum === 3
|
||||
? 'revenue'
|
||||
: 'expense'
|
||||
return {
|
||||
id: `bas-${number}`,
|
||||
user_id: 'user-1',
|
||||
account_number: number,
|
||||
account_name: name,
|
||||
account_class: classNum,
|
||||
account_group: number.substring(0, 2),
|
||||
account_type: accountType,
|
||||
normal_balance: classNum <= 1 || classNum >= 4 ? 'debit' : 'credit',
|
||||
plan_type: 'k1',
|
||||
is_active: true,
|
||||
is_system_account: false,
|
||||
default_vat_code: null,
|
||||
description: null,
|
||||
sru_code: null,
|
||||
sort_order: parseInt(number, 10),
|
||||
created_at: '2024-01-01',
|
||||
updated_at: '2024-01-01',
|
||||
}
|
||||
}
|
||||
|
||||
function makeSIEAccount(number: string, name: string): SIEAccount {
|
||||
return { number, name }
|
||||
}
|
||||
|
||||
// --- Fixtures ---
|
||||
|
||||
const basAccounts: BASAccount[] = [
|
||||
makeBASAccount('1510', 'Kundfordringar'),
|
||||
makeBASAccount('1930', 'Företagskonto'),
|
||||
makeBASAccount('2440', 'Leverantörsskulder'),
|
||||
makeBASAccount('3001', 'Försäljning varor 25%'),
|
||||
makeBASAccount('3002', 'Försäljning varor 12%'),
|
||||
makeBASAccount('5010', 'Lokalhyra'),
|
||||
makeBASAccount('6211', 'Telekommunikation'),
|
||||
]
|
||||
|
||||
// --- Tests ---
|
||||
|
||||
describe('suggestMappings', () => {
|
||||
it('returns exact match with confidence 1.0', () => {
|
||||
const source = [makeSIEAccount('1510', 'Kundfordringar')]
|
||||
const result = suggestMappings(source, basAccounts)
|
||||
|
||||
expect(result).toHaveLength(1)
|
||||
expect(result[0].targetAccount).toBe('1510')
|
||||
expect(result[0].targetName).toBe('Kundfordringar')
|
||||
expect(result[0].confidence).toBe(1.0)
|
||||
expect(result[0].matchType).toBe('exact')
|
||||
expect(result[0].isOverride).toBe(false)
|
||||
})
|
||||
|
||||
it('returns unmapped entry when no match exists', () => {
|
||||
const source = [makeSIEAccount('9999', 'Okänt konto')]
|
||||
const result = suggestMappings(source, basAccounts)
|
||||
|
||||
expect(result).toHaveLength(1)
|
||||
expect(result[0].targetAccount).toBe('')
|
||||
expect(result[0].targetName).toBe('')
|
||||
expect(result[0].confidence).toBe(0)
|
||||
expect(result[0].matchType).toBe('manual')
|
||||
})
|
||||
|
||||
it('does not fuzzy match accounts with similar names', () => {
|
||||
// 3400 should NOT match 3001 or 3002 despite being in same class
|
||||
const source = [makeSIEAccount('3400', 'Försäljning tjänster')]
|
||||
const result = suggestMappings(source, basAccounts)
|
||||
|
||||
expect(result).toHaveLength(1)
|
||||
expect(result[0].targetAccount).toBe('')
|
||||
expect(result[0].confidence).toBe(0)
|
||||
})
|
||||
|
||||
it('does not fuzzy match accounts with similar numbers', () => {
|
||||
// 2510 should NOT match 2440 despite being in same class
|
||||
const source = [makeSIEAccount('2510', 'Skatteskulder')]
|
||||
const result = suggestMappings(source, basAccounts)
|
||||
|
||||
expect(result).toHaveLength(1)
|
||||
expect(result[0].targetAccount).toBe('')
|
||||
expect(result[0].confidence).toBe(0)
|
||||
})
|
||||
|
||||
it('preserves user overrides from existing mappings', () => {
|
||||
const source = [makeSIEAccount('3400', 'Försäljning tjänster')]
|
||||
const existingMappings: SIEAccountMappingRecord[] = [
|
||||
{
|
||||
id: 'map-1',
|
||||
user_id: 'user-1',
|
||||
source_account: '3400',
|
||||
source_name: 'Försäljning tjänster',
|
||||
target_account: '3001',
|
||||
confidence: 1.0,
|
||||
match_type: 'manual',
|
||||
created_at: '2024-01-01',
|
||||
updated_at: '2024-01-01',
|
||||
},
|
||||
]
|
||||
|
||||
const result = suggestMappings(source, basAccounts, existingMappings)
|
||||
|
||||
expect(result).toHaveLength(1)
|
||||
expect(result[0].targetAccount).toBe('3001')
|
||||
expect(result[0].isOverride).toBe(true)
|
||||
expect(result[0].matchType).toBe('manual')
|
||||
})
|
||||
|
||||
it('sorts unmapped accounts first (lowest confidence)', () => {
|
||||
const source = [
|
||||
makeSIEAccount('1510', 'Kundfordringar'),
|
||||
makeSIEAccount('9999', 'Okänt konto'),
|
||||
makeSIEAccount('1930', 'Företagskonto'),
|
||||
]
|
||||
const result = suggestMappings(source, basAccounts)
|
||||
|
||||
expect(result).toHaveLength(3)
|
||||
// Unmapped (confidence 0) should come first
|
||||
expect(result[0].sourceAccount).toBe('9999')
|
||||
expect(result[0].confidence).toBe(0)
|
||||
// Exact matches (confidence 1.0) come after
|
||||
expect(result[1].confidence).toBe(1.0)
|
||||
expect(result[2].confidence).toBe(1.0)
|
||||
})
|
||||
|
||||
it('handles multiple accounts with mixed results', () => {
|
||||
const source = [
|
||||
makeSIEAccount('1510', 'Kundfordringar'),
|
||||
makeSIEAccount('3400', 'Försäljning tjänster'),
|
||||
makeSIEAccount('5010', 'Lokalhyra'),
|
||||
]
|
||||
const result = suggestMappings(source, basAccounts)
|
||||
|
||||
expect(result).toHaveLength(3)
|
||||
|
||||
const mapped = result.filter((m) => m.targetAccount)
|
||||
const unmapped = result.filter((m) => !m.targetAccount)
|
||||
expect(mapped).toHaveLength(2)
|
||||
expect(unmapped).toHaveLength(1)
|
||||
})
|
||||
|
||||
it('handles empty source accounts', () => {
|
||||
const result = suggestMappings([], basAccounts)
|
||||
expect(result).toHaveLength(0)
|
||||
})
|
||||
|
||||
it('handles empty BAS accounts — everything unmapped', () => {
|
||||
const source = [makeSIEAccount('1510', 'Kundfordringar')]
|
||||
const result = suggestMappings(source, [])
|
||||
|
||||
expect(result).toHaveLength(1)
|
||||
expect(result[0].targetAccount).toBe('')
|
||||
expect(result[0].confidence).toBe(0)
|
||||
})
|
||||
})
|
||||
|
||||
describe('validateMappings', () => {
|
||||
it('returns valid when all accounts are mapped', () => {
|
||||
const mappings = suggestMappings(
|
||||
[makeSIEAccount('1510', 'Kundfordringar'), makeSIEAccount('1930', 'Företagskonto')],
|
||||
basAccounts
|
||||
)
|
||||
const validation = validateMappings(mappings)
|
||||
|
||||
expect(validation.valid).toBe(true)
|
||||
expect(validation.unmappedAccounts).toHaveLength(0)
|
||||
})
|
||||
|
||||
it('returns invalid when accounts are unmapped', () => {
|
||||
const mappings = suggestMappings(
|
||||
[makeSIEAccount('1510', 'Kundfordringar'), makeSIEAccount('9999', 'Okänt konto')],
|
||||
basAccounts
|
||||
)
|
||||
const validation = validateMappings(mappings)
|
||||
|
||||
expect(validation.valid).toBe(false)
|
||||
expect(validation.unmappedAccounts).toContain('9999')
|
||||
expect(validation.unmappedAccounts).toHaveLength(1)
|
||||
})
|
||||
|
||||
it('detects low confidence accounts', () => {
|
||||
// With exact-match-only mapper, low confidence only comes from existing overrides
|
||||
const mappings = [
|
||||
{
|
||||
sourceAccount: '3400',
|
||||
sourceName: 'Försäljning tjänster',
|
||||
targetAccount: '3001',
|
||||
targetName: 'Försäljning varor 25%',
|
||||
confidence: 0.3,
|
||||
matchType: 'class' as const,
|
||||
isOverride: false,
|
||||
},
|
||||
]
|
||||
|
||||
const validation = validateMappings(mappings)
|
||||
expect(validation.lowConfidenceAccounts).toContain('3400')
|
||||
})
|
||||
})
|
||||
|
||||
describe('getMappingStats', () => {
|
||||
it('counts total, mapped, unmapped correctly', () => {
|
||||
const mappings = suggestMappings(
|
||||
[
|
||||
makeSIEAccount('1510', 'Kundfordringar'),
|
||||
makeSIEAccount('9999', 'Okänt konto'),
|
||||
makeSIEAccount('5010', 'Lokalhyra'),
|
||||
],
|
||||
basAccounts
|
||||
)
|
||||
const stats = getMappingStats(mappings)
|
||||
|
||||
expect(stats.total).toBe(3)
|
||||
expect(stats.mapped).toBe(2)
|
||||
expect(stats.unmapped).toBe(1)
|
||||
})
|
||||
|
||||
it('counts match types correctly', () => {
|
||||
const mappings = suggestMappings(
|
||||
[
|
||||
makeSIEAccount('1510', 'Kundfordringar'),
|
||||
makeSIEAccount('9999', 'Okänt konto'),
|
||||
],
|
||||
basAccounts
|
||||
)
|
||||
const stats = getMappingStats(mappings)
|
||||
|
||||
expect(stats.exact).toBe(1)
|
||||
expect(stats.manual).toBe(1) // unmapped gets matchType 'manual'
|
||||
expect(stats.name).toBe(0)
|
||||
expect(stats.class).toBe(0)
|
||||
})
|
||||
|
||||
it('calculates average confidence for mapped accounts only', () => {
|
||||
const mappings = suggestMappings(
|
||||
[
|
||||
makeSIEAccount('1510', 'Kundfordringar'), // exact, confidence 1.0
|
||||
makeSIEAccount('1930', 'Företagskonto'), // exact, confidence 1.0
|
||||
makeSIEAccount('9999', 'Okänt konto'), // unmapped, confidence 0
|
||||
],
|
||||
basAccounts
|
||||
)
|
||||
const stats = getMappingStats(mappings)
|
||||
|
||||
// Average of mapped only: (1.0 + 1.0) / 2 = 1.0
|
||||
expect(stats.averageConfidence).toBe(1.0)
|
||||
})
|
||||
|
||||
it('returns 0 average confidence when nothing is mapped', () => {
|
||||
const mappings = suggestMappings(
|
||||
[makeSIEAccount('9999', 'Okänt konto')],
|
||||
basAccounts
|
||||
)
|
||||
const stats = getMappingStats(mappings)
|
||||
expect(stats.averageConfidence).toBe(0)
|
||||
})
|
||||
})
|
||||
|
||||
describe('applyMappingOverride', () => {
|
||||
it('sets target, confidence 1.0, matchType manual, and isOverride', () => {
|
||||
const mappings = suggestMappings(
|
||||
[makeSIEAccount('3400', 'Försäljning tjänster')],
|
||||
basAccounts
|
||||
)
|
||||
|
||||
const updated = applyMappingOverride(mappings, '3400', '3001', 'Försäljning varor 25%')
|
||||
|
||||
expect(updated).toHaveLength(1)
|
||||
expect(updated[0].targetAccount).toBe('3001')
|
||||
expect(updated[0].targetName).toBe('Försäljning varor 25%')
|
||||
expect(updated[0].confidence).toBe(1.0)
|
||||
expect(updated[0].matchType).toBe('manual')
|
||||
expect(updated[0].isOverride).toBe(true)
|
||||
})
|
||||
|
||||
it('does not mutate the original array', () => {
|
||||
const mappings = suggestMappings(
|
||||
[makeSIEAccount('3400', 'Försäljning tjänster')],
|
||||
basAccounts
|
||||
)
|
||||
const original = [...mappings]
|
||||
|
||||
applyMappingOverride(mappings, '3400', '3001', 'Försäljning varor 25%')
|
||||
|
||||
expect(mappings[0].targetAccount).toBe(original[0].targetAccount)
|
||||
expect(mappings[0].confidence).toBe(original[0].confidence)
|
||||
})
|
||||
|
||||
it('only affects the specified source account', () => {
|
||||
const mappings = suggestMappings(
|
||||
[
|
||||
makeSIEAccount('3400', 'Försäljning tjänster'),
|
||||
makeSIEAccount('9998', 'Annat okänt konto'),
|
||||
],
|
||||
basAccounts
|
||||
)
|
||||
|
||||
const updated = applyMappingOverride(mappings, '3400', '3001', 'Försäljning varor 25%')
|
||||
|
||||
const unchanged = updated.find((m) => m.sourceAccount === '9998')
|
||||
expect(unchanged?.targetAccount).toBe('')
|
||||
expect(unchanged?.confidence).toBe(0)
|
||||
})
|
||||
})
|
||||
|
||||
describe('mappingsToMap', () => {
|
||||
it('creates a Map from source to target account', () => {
|
||||
const mappings = suggestMappings(
|
||||
[makeSIEAccount('1510', 'Kundfordringar'), makeSIEAccount('1930', 'Företagskonto')],
|
||||
basAccounts
|
||||
)
|
||||
const map = mappingsToMap(mappings)
|
||||
|
||||
expect(map.get('1510')).toBe('1510')
|
||||
expect(map.get('1930')).toBe('1930')
|
||||
expect(map.size).toBe(2)
|
||||
})
|
||||
|
||||
it('skips unmapped accounts', () => {
|
||||
const mappings = suggestMappings(
|
||||
[makeSIEAccount('1510', 'Kundfordringar'), makeSIEAccount('9999', 'Okänt konto')],
|
||||
basAccounts
|
||||
)
|
||||
const map = mappingsToMap(mappings)
|
||||
|
||||
expect(map.get('1510')).toBe('1510')
|
||||
expect(map.has('9999')).toBe(false)
|
||||
expect(map.size).toBe(1)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,223 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { generateImportPreview } from '../sie-import'
|
||||
import type { ParsedSIEFile, AccountMapping } from '../types'
|
||||
|
||||
// --- Helpers ---
|
||||
|
||||
function makeParsedFile(overrides?: Partial<ParsedSIEFile>): ParsedSIEFile {
|
||||
return {
|
||||
header: {
|
||||
sieType: 4,
|
||||
program: 'TestProg',
|
||||
programVersion: '1.0',
|
||||
generatedDate: new Date(2024, 0, 1),
|
||||
format: 'PC8',
|
||||
companyName: 'Test AB',
|
||||
orgNumber: '5566778899',
|
||||
address: null,
|
||||
fiscalYears: [{ yearIndex: 0, start: new Date(2024, 0, 1), end: new Date(2024, 11, 31) }],
|
||||
currency: 'SEK',
|
||||
},
|
||||
accounts: [
|
||||
{ number: '1510', name: 'Kundfordringar' },
|
||||
{ number: '1930', name: 'Företagskonto' },
|
||||
{ number: '2440', name: 'Leverantörsskulder' },
|
||||
],
|
||||
openingBalances: [
|
||||
{ yearIndex: 0, account: '1510', amount: 50000 },
|
||||
{ yearIndex: 0, account: '1930', amount: 100000 },
|
||||
{ yearIndex: 0, account: '2440', amount: -150000 },
|
||||
],
|
||||
closingBalances: [],
|
||||
resultBalances: [],
|
||||
vouchers: [
|
||||
{
|
||||
series: 'A',
|
||||
number: 1,
|
||||
date: new Date(2024, 0, 15),
|
||||
description: 'Faktura 1001',
|
||||
lines: [
|
||||
{ account: '1510', amount: 12500 },
|
||||
{ account: '3001', amount: -10000 },
|
||||
{ account: '2611', amount: -2500 },
|
||||
],
|
||||
},
|
||||
],
|
||||
issues: [],
|
||||
stats: {
|
||||
totalAccounts: 3,
|
||||
totalVouchers: 1,
|
||||
totalTransactionLines: 3,
|
||||
fiscalYearStart: new Date(2024, 0, 1),
|
||||
fiscalYearEnd: new Date(2024, 11, 31),
|
||||
},
|
||||
...overrides,
|
||||
}
|
||||
}
|
||||
|
||||
function makeMapping(source: string, target: string, confidence: number = 1.0): AccountMapping {
|
||||
return {
|
||||
sourceAccount: source,
|
||||
sourceName: `Account ${source}`,
|
||||
targetAccount: target,
|
||||
targetName: `Target ${target}`,
|
||||
confidence,
|
||||
matchType: target ? 'exact' : 'manual',
|
||||
isOverride: false,
|
||||
}
|
||||
}
|
||||
|
||||
// --- Tests ---
|
||||
|
||||
describe('generateImportPreview', () => {
|
||||
describe('trial balance from IB', () => {
|
||||
it('calculates debit totals from positive IB amounts', () => {
|
||||
const parsed = makeParsedFile()
|
||||
const mappings = [
|
||||
makeMapping('1510', '1510'),
|
||||
makeMapping('1930', '1930'),
|
||||
makeMapping('2440', '2440'),
|
||||
]
|
||||
const preview = generateImportPreview(parsed, mappings)
|
||||
|
||||
// Positive amounts: 50000 + 100000 = 150000
|
||||
expect(preview.trialBalance.totalDebit).toBe(150000)
|
||||
})
|
||||
|
||||
it('calculates credit totals from negative IB amounts', () => {
|
||||
const parsed = makeParsedFile()
|
||||
const mappings = [
|
||||
makeMapping('1510', '1510'),
|
||||
makeMapping('1930', '1930'),
|
||||
makeMapping('2440', '2440'),
|
||||
]
|
||||
const preview = generateImportPreview(parsed, mappings)
|
||||
|
||||
// Negative amounts: |-150000| = 150000
|
||||
expect(preview.trialBalance.totalCredit).toBe(150000)
|
||||
})
|
||||
|
||||
it('detects balanced trial balance', () => {
|
||||
const parsed = makeParsedFile()
|
||||
const mappings = [makeMapping('1510', '1510')]
|
||||
const preview = generateImportPreview(parsed, mappings)
|
||||
|
||||
// 150000 debit = 150000 credit
|
||||
expect(preview.trialBalance.isBalanced).toBe(true)
|
||||
})
|
||||
|
||||
it('detects unbalanced trial balance', () => {
|
||||
const parsed = makeParsedFile({
|
||||
openingBalances: [
|
||||
{ yearIndex: 0, account: '1510', amount: 50000 },
|
||||
{ yearIndex: 0, account: '1930', amount: 100000 },
|
||||
// Missing credit side — only 150000 debit, 0 credit
|
||||
],
|
||||
})
|
||||
const mappings = [makeMapping('1510', '1510')]
|
||||
const preview = generateImportPreview(parsed, mappings)
|
||||
|
||||
expect(preview.trialBalance.isBalanced).toBe(false)
|
||||
})
|
||||
|
||||
it('handles zero opening balances', () => {
|
||||
const parsed = makeParsedFile({ openingBalances: [] })
|
||||
const mappings: AccountMapping[] = []
|
||||
const preview = generateImportPreview(parsed, mappings)
|
||||
|
||||
expect(preview.trialBalance.totalDebit).toBe(0)
|
||||
expect(preview.trialBalance.totalCredit).toBe(0)
|
||||
expect(preview.trialBalance.isBalanced).toBe(true)
|
||||
})
|
||||
})
|
||||
|
||||
describe('company info passthrough', () => {
|
||||
it('passes company name', () => {
|
||||
const parsed = makeParsedFile()
|
||||
const preview = generateImportPreview(parsed, [])
|
||||
expect(preview.companyName).toBe('Test AB')
|
||||
})
|
||||
|
||||
it('passes org number', () => {
|
||||
const parsed = makeParsedFile()
|
||||
const preview = generateImportPreview(parsed, [])
|
||||
expect(preview.orgNumber).toBe('5566778899')
|
||||
})
|
||||
|
||||
it('handles null company info', () => {
|
||||
const parsed = makeParsedFile({
|
||||
header: {
|
||||
...makeParsedFile().header,
|
||||
companyName: null,
|
||||
orgNumber: null,
|
||||
},
|
||||
})
|
||||
const preview = generateImportPreview(parsed, [])
|
||||
expect(preview.companyName).toBeNull()
|
||||
expect(preview.orgNumber).toBeNull()
|
||||
})
|
||||
})
|
||||
|
||||
describe('mapping status', () => {
|
||||
it('reflects mapper output counts', () => {
|
||||
const parsed = makeParsedFile()
|
||||
const mappings = [
|
||||
makeMapping('1510', '1510'), // mapped
|
||||
makeMapping('1930', '1930'), // mapped
|
||||
makeMapping('2440', '', 0), // unmapped
|
||||
]
|
||||
const preview = generateImportPreview(parsed, mappings)
|
||||
|
||||
expect(preview.mappingStatus.total).toBe(3)
|
||||
expect(preview.mappingStatus.mapped).toBe(2)
|
||||
expect(preview.mappingStatus.unmapped).toBe(1)
|
||||
})
|
||||
|
||||
it('reports low confidence mappings', () => {
|
||||
const mappings = [
|
||||
makeMapping('1510', '1510', 1.0),
|
||||
makeMapping('3400', '3001', 0.3), // low confidence
|
||||
]
|
||||
const parsed = makeParsedFile()
|
||||
const preview = generateImportPreview(parsed, mappings)
|
||||
|
||||
expect(preview.mappingStatus.lowConfidence).toBe(1)
|
||||
})
|
||||
})
|
||||
|
||||
describe('statistics', () => {
|
||||
it('passes account count', () => {
|
||||
const parsed = makeParsedFile()
|
||||
const preview = generateImportPreview(parsed, [])
|
||||
expect(preview.accountCount).toBe(3)
|
||||
})
|
||||
|
||||
it('passes voucher count', () => {
|
||||
const parsed = makeParsedFile()
|
||||
const preview = generateImportPreview(parsed, [])
|
||||
expect(preview.voucherCount).toBe(1)
|
||||
})
|
||||
|
||||
it('passes transaction line count', () => {
|
||||
const parsed = makeParsedFile()
|
||||
const preview = generateImportPreview(parsed, [])
|
||||
expect(preview.transactionLineCount).toBe(3)
|
||||
})
|
||||
})
|
||||
|
||||
describe('issues passthrough', () => {
|
||||
it('passes parse issues to preview', () => {
|
||||
const parsed = makeParsedFile({
|
||||
issues: [
|
||||
{ severity: 'warning', line: 5, message: 'Unknown tag: #FOO', tag: 'FOO' },
|
||||
{ severity: 'error', line: 10, message: 'Invalid voucher', tag: 'VER' },
|
||||
],
|
||||
})
|
||||
const preview = generateImportPreview(parsed, [])
|
||||
|
||||
expect(preview.issues).toHaveLength(2)
|
||||
expect(preview.issues[0].severity).toBe('warning')
|
||||
expect(preview.issues[1].severity).toBe('error')
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,345 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { parseSIEFile, validateSIEFile } from '../sie-parser'
|
||||
|
||||
// --- SIE content fixtures ---
|
||||
|
||||
const MINIMAL_SIE = [
|
||||
'#FLAGGA 0',
|
||||
'#SIETYP 4',
|
||||
'#PROGRAM "TestProg" "1.0"',
|
||||
'#FORMAT PC8',
|
||||
'#GEN 20240101',
|
||||
'#FNAMN "Test AB"',
|
||||
'#ORGNR 5566778899',
|
||||
'#VALUTA SEK',
|
||||
'#RAR 0 20240101 20241231',
|
||||
'#KONTO 1510 "Kundfordringar"',
|
||||
'#KONTO 1930 "Företagskonto"',
|
||||
'#KONTO 3001 "Försäljning varor 25%"',
|
||||
].join('\n')
|
||||
|
||||
const SIE_WITH_BALANCES = [
|
||||
'#FLAGGA 0',
|
||||
'#SIETYP 4',
|
||||
'#FNAMN "Balans AB"',
|
||||
'#ORGNR 1234567890',
|
||||
'#RAR 0 20240101 20241231',
|
||||
'#KONTO 1510 "Kundfordringar"',
|
||||
'#KONTO 1930 "Företagskonto"',
|
||||
'#KONTO 2440 "Leverantörsskulder"',
|
||||
'#IB 0 1510 50000.00',
|
||||
'#IB 0 1930 100000.00',
|
||||
'#IB 0 2440 -150000.00',
|
||||
'#UB 0 1510 75000.00',
|
||||
'#UB 0 1930 125000.00',
|
||||
'#UB 0 2440 -200000.00',
|
||||
].join('\n')
|
||||
|
||||
const SIE_WITH_VOUCHERS = [
|
||||
'#FLAGGA 0',
|
||||
'#SIETYP 4',
|
||||
'#FNAMN "Voucher AB"',
|
||||
'#RAR 0 20240101 20241231',
|
||||
'#KONTO 1510 "Kundfordringar"',
|
||||
'#KONTO 1930 "Företagskonto"',
|
||||
'#KONTO 3001 "Försäljning"',
|
||||
'#KONTO 2611 "Utgående moms 25%"',
|
||||
'#VER A 1 20240115 "Faktura 1001"',
|
||||
'{',
|
||||
'#TRANS 1510 {} 12500.00',
|
||||
'#TRANS 3001 {} -10000.00',
|
||||
'#TRANS 2611 {} -2500.00',
|
||||
'}',
|
||||
'#VER A 2 20240220 "Inbetalning faktura 1001"',
|
||||
'{',
|
||||
'#TRANS 1930 {} 12500.00',
|
||||
'#TRANS 1510 {} -12500.00',
|
||||
'}',
|
||||
].join('\n')
|
||||
|
||||
const SIE_TYPE_1 = [
|
||||
'#FLAGGA 0',
|
||||
'#SIETYP 1',
|
||||
'#FNAMN "SIE1 AB"',
|
||||
'#RAR 0 20240101 20241231',
|
||||
'#KONTO 1510 "Kundfordringar"',
|
||||
'#IB 0 1510 50000.00',
|
||||
'#UB 0 1510 75000.00',
|
||||
].join('\n')
|
||||
|
||||
const SIE_WITH_SRU = [
|
||||
'#FLAGGA 0',
|
||||
'#SIETYP 4',
|
||||
'#FNAMN "SRU AB"',
|
||||
'#RAR 0 20240101 20241231',
|
||||
'#KONTO 1510 "Kundfordringar"',
|
||||
'#SRU 1510 7251',
|
||||
'#KONTO 3001 "Försäljning"',
|
||||
'#SRU 3001 7410',
|
||||
].join('\n')
|
||||
|
||||
const SIE_UNBALANCED_VOUCHER = [
|
||||
'#FLAGGA 0',
|
||||
'#SIETYP 4',
|
||||
'#FNAMN "Obalanserad AB"',
|
||||
'#RAR 0 20240101 20241231',
|
||||
'#KONTO 1510 "Kundfordringar"',
|
||||
'#KONTO 3001 "Försäljning"',
|
||||
'#VER A 1 20240115 "Obalanserad verifikation"',
|
||||
'{',
|
||||
'#TRANS 1510 {} 10000.00',
|
||||
'#TRANS 3001 {} -5000.00',
|
||||
'}',
|
||||
].join('\n')
|
||||
|
||||
const SIE_WITH_OBJECT_LIST = [
|
||||
'#FLAGGA 0',
|
||||
'#SIETYP 4',
|
||||
'#FNAMN "Objects AB"',
|
||||
'#RAR 0 20240101 20241231',
|
||||
'#KONTO 5010 "Lokalhyra"',
|
||||
'#KONTO 1930 "Företagskonto"',
|
||||
'#VER A 1 20240115 "Hyra januari"',
|
||||
'{',
|
||||
'#TRANS 5010 {1 "Kontor"} 15000.00',
|
||||
'#TRANS 1930 {} -15000.00',
|
||||
'}',
|
||||
].join('\n')
|
||||
|
||||
// --- parseSIEFile tests ---
|
||||
|
||||
describe('parseSIEFile', () => {
|
||||
describe('header parsing', () => {
|
||||
it('parses SIE type', () => {
|
||||
const result = parseSIEFile(MINIMAL_SIE)
|
||||
expect(result.header.sieType).toBe(4)
|
||||
})
|
||||
|
||||
it('parses company name from #FNAMN', () => {
|
||||
const result = parseSIEFile(MINIMAL_SIE)
|
||||
expect(result.header.companyName).toBe('Test AB')
|
||||
})
|
||||
|
||||
it('parses org number from #ORGNR', () => {
|
||||
const result = parseSIEFile(MINIMAL_SIE)
|
||||
expect(result.header.orgNumber).toBe('5566778899')
|
||||
})
|
||||
|
||||
it('parses fiscal year from #RAR', () => {
|
||||
const result = parseSIEFile(MINIMAL_SIE)
|
||||
expect(result.header.fiscalYears).toHaveLength(1)
|
||||
expect(result.header.fiscalYears[0].yearIndex).toBe(0)
|
||||
expect(result.header.fiscalYears[0].start).toEqual(new Date(2024, 0, 1))
|
||||
expect(result.header.fiscalYears[0].end).toEqual(new Date(2024, 11, 31))
|
||||
})
|
||||
|
||||
it('parses currency from #VALUTA', () => {
|
||||
const result = parseSIEFile(MINIMAL_SIE)
|
||||
expect(result.header.currency).toBe('SEK')
|
||||
})
|
||||
|
||||
it('defaults currency to SEK when not specified', () => {
|
||||
const content = '#FLAGGA 0\n#SIETYP 4\n#FNAMN "Test"\n#RAR 0 20240101 20241231'
|
||||
const result = parseSIEFile(content)
|
||||
expect(result.header.currency).toBe('SEK')
|
||||
})
|
||||
|
||||
it('parses program info', () => {
|
||||
const result = parseSIEFile(MINIMAL_SIE)
|
||||
expect(result.header.program).toBe('TestProg')
|
||||
expect(result.header.programVersion).toBe('1.0')
|
||||
})
|
||||
|
||||
it('parses generated date', () => {
|
||||
const result = parseSIEFile(MINIMAL_SIE)
|
||||
expect(result.header.generatedDate).toEqual(new Date(2024, 0, 1))
|
||||
})
|
||||
|
||||
it('parses SIE type 1', () => {
|
||||
const result = parseSIEFile(SIE_TYPE_1)
|
||||
expect(result.header.sieType).toBe(1)
|
||||
})
|
||||
})
|
||||
|
||||
describe('account parsing', () => {
|
||||
it('parses #KONTO with number and name', () => {
|
||||
const result = parseSIEFile(MINIMAL_SIE)
|
||||
expect(result.accounts).toHaveLength(3)
|
||||
expect(result.accounts[0]).toEqual({ number: '1510', name: 'Kundfordringar' })
|
||||
expect(result.accounts[1]).toEqual({ number: '1930', name: 'Företagskonto' })
|
||||
})
|
||||
|
||||
it('parses #SRU codes onto accounts', () => {
|
||||
const result = parseSIEFile(SIE_WITH_SRU)
|
||||
const account1510 = result.accounts.find((a) => a.number === '1510')
|
||||
expect(account1510?.sruCode).toBe('7251')
|
||||
const account3001 = result.accounts.find((a) => a.number === '3001')
|
||||
expect(account3001?.sruCode).toBe('7410')
|
||||
})
|
||||
})
|
||||
|
||||
describe('balance parsing', () => {
|
||||
it('parses opening balances (#IB) with positive amounts', () => {
|
||||
const result = parseSIEFile(SIE_WITH_BALANCES)
|
||||
const ib1510 = result.openingBalances.find((b) => b.account === '1510')
|
||||
expect(ib1510?.amount).toBe(50000)
|
||||
expect(ib1510?.yearIndex).toBe(0)
|
||||
})
|
||||
|
||||
it('parses opening balances (#IB) with negative amounts', () => {
|
||||
const result = parseSIEFile(SIE_WITH_BALANCES)
|
||||
const ib2440 = result.openingBalances.find((b) => b.account === '2440')
|
||||
expect(ib2440?.amount).toBe(-150000)
|
||||
})
|
||||
|
||||
it('parses closing balances (#UB)', () => {
|
||||
const result = parseSIEFile(SIE_WITH_BALANCES)
|
||||
expect(result.closingBalances).toHaveLength(3)
|
||||
const ub1930 = result.closingBalances.find((b) => b.account === '1930')
|
||||
expect(ub1930?.amount).toBe(125000)
|
||||
})
|
||||
})
|
||||
|
||||
describe('voucher parsing', () => {
|
||||
it('parses #VER with series, number, date, description', () => {
|
||||
const result = parseSIEFile(SIE_WITH_VOUCHERS)
|
||||
expect(result.vouchers).toHaveLength(2)
|
||||
|
||||
const v1 = result.vouchers[0]
|
||||
expect(v1.series).toBe('A')
|
||||
expect(v1.number).toBe(1)
|
||||
expect(v1.date).toEqual(new Date(2024, 0, 15))
|
||||
expect(v1.description).toBe('Faktura 1001')
|
||||
})
|
||||
|
||||
it('parses #TRANS lines within a voucher', () => {
|
||||
const result = parseSIEFile(SIE_WITH_VOUCHERS)
|
||||
const v1 = result.vouchers[0]
|
||||
|
||||
expect(v1.lines).toHaveLength(3)
|
||||
expect(v1.lines[0]).toMatchObject({ account: '1510', amount: 12500 })
|
||||
expect(v1.lines[1]).toMatchObject({ account: '3001', amount: -10000 })
|
||||
expect(v1.lines[2]).toMatchObject({ account: '2611', amount: -2500 })
|
||||
})
|
||||
|
||||
it('handles object lists in braces', () => {
|
||||
const result = parseSIEFile(SIE_WITH_OBJECT_LIST)
|
||||
expect(result.vouchers).toHaveLength(1)
|
||||
|
||||
const v = result.vouchers[0]
|
||||
expect(v.lines).toHaveLength(2)
|
||||
expect(v.lines[0]).toMatchObject({ account: '5010', amount: 15000 })
|
||||
expect(v.lines[1]).toMatchObject({ account: '1930', amount: -15000 })
|
||||
})
|
||||
|
||||
it('detects unbalanced vouchers as errors', () => {
|
||||
const result = parseSIEFile(SIE_UNBALANCED_VOUCHER)
|
||||
expect(result.vouchers).toHaveLength(1)
|
||||
|
||||
const errors = result.issues.filter((i) => i.severity === 'error')
|
||||
expect(errors.length).toBeGreaterThanOrEqual(1)
|
||||
expect(errors.some((e) => e.message.includes('not balanced'))).toBe(true)
|
||||
})
|
||||
})
|
||||
|
||||
describe('statistics', () => {
|
||||
it('calculates account count', () => {
|
||||
const result = parseSIEFile(MINIMAL_SIE)
|
||||
expect(result.stats.totalAccounts).toBe(3)
|
||||
})
|
||||
|
||||
it('calculates voucher count', () => {
|
||||
const result = parseSIEFile(SIE_WITH_VOUCHERS)
|
||||
expect(result.stats.totalVouchers).toBe(2)
|
||||
})
|
||||
|
||||
it('calculates transaction line count', () => {
|
||||
const result = parseSIEFile(SIE_WITH_VOUCHERS)
|
||||
// Voucher 1: 3 lines, Voucher 2: 2 lines
|
||||
expect(result.stats.totalTransactionLines).toBe(5)
|
||||
})
|
||||
|
||||
it('sets fiscal year start/end from RAR 0', () => {
|
||||
const result = parseSIEFile(MINIMAL_SIE)
|
||||
expect(result.stats.fiscalYearStart).toEqual(new Date(2024, 0, 1))
|
||||
expect(result.stats.fiscalYearEnd).toEqual(new Date(2024, 11, 31))
|
||||
})
|
||||
|
||||
it('returns null fiscal year dates when no RAR', () => {
|
||||
const content = '#FLAGGA 0\n#SIETYP 4\n#FNAMN "Test"'
|
||||
const result = parseSIEFile(content)
|
||||
expect(result.stats.fiscalYearStart).toBeNull()
|
||||
expect(result.stats.fiscalYearEnd).toBeNull()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
// --- validateSIEFile tests ---
|
||||
|
||||
describe('validateSIEFile', () => {
|
||||
it('returns valid for a complete SIE file', () => {
|
||||
const parsed = parseSIEFile(SIE_WITH_VOUCHERS)
|
||||
const validation = validateSIEFile(parsed)
|
||||
|
||||
expect(validation.valid).toBe(true)
|
||||
expect(validation.errors).toHaveLength(0)
|
||||
})
|
||||
|
||||
it('adds error for unbalanced vouchers', () => {
|
||||
const parsed = parseSIEFile(SIE_UNBALANCED_VOUCHER)
|
||||
const validation = validateSIEFile(parsed)
|
||||
|
||||
expect(validation.valid).toBe(false)
|
||||
expect(validation.errors.some((e) => e.includes('not balanced'))).toBe(true)
|
||||
})
|
||||
|
||||
it('adds warning for undefined account references', () => {
|
||||
const content = [
|
||||
'#FLAGGA 0',
|
||||
'#SIETYP 4',
|
||||
'#FNAMN "Test"',
|
||||
'#RAR 0 20240101 20241231',
|
||||
'#KONTO 1510 "Kundfordringar"',
|
||||
'#IB 0 9999 50000.00',
|
||||
].join('\n')
|
||||
|
||||
const parsed = parseSIEFile(content)
|
||||
const validation = validateSIEFile(parsed)
|
||||
|
||||
expect(validation.warnings.some((w) => w.includes('9999') && w.includes('not defined'))).toBe(true)
|
||||
})
|
||||
|
||||
it('adds error for missing #RAR', () => {
|
||||
const content = '#FLAGGA 0\n#SIETYP 4\n#FNAMN "Test"\n#KONTO 1510 "Kund"'
|
||||
const parsed = parseSIEFile(content)
|
||||
const validation = validateSIEFile(parsed)
|
||||
|
||||
expect(validation.valid).toBe(false)
|
||||
expect(validation.errors.some((e) => e.includes('fiscal year') || e.includes('#RAR'))).toBe(true)
|
||||
})
|
||||
|
||||
it('adds warning for unbalanced opening balances', () => {
|
||||
const content = [
|
||||
'#FLAGGA 0',
|
||||
'#SIETYP 4',
|
||||
'#FNAMN "Test"',
|
||||
'#RAR 0 20240101 20241231',
|
||||
'#KONTO 1510 "Kundfordringar"',
|
||||
'#IB 0 1510 50000.00',
|
||||
].join('\n')
|
||||
|
||||
const parsed = parseSIEFile(content)
|
||||
const validation = validateSIEFile(parsed)
|
||||
|
||||
expect(validation.warnings.some((w) => w.includes('Opening balances not balanced'))).toBe(true)
|
||||
})
|
||||
|
||||
it('passes with balanced opening balances', () => {
|
||||
const parsed = parseSIEFile(SIE_WITH_BALANCES)
|
||||
const validation = validateSIEFile(parsed)
|
||||
|
||||
// IB: 50000 + 100000 + (-150000) = 0 → balanced
|
||||
const ibWarning = validation.warnings.find((w) => w.includes('Opening balances not balanced'))
|
||||
expect(ibWarning).toBeUndefined()
|
||||
})
|
||||
})
|
||||
+21
-181
@@ -1,12 +1,10 @@
|
||||
/**
|
||||
* Account Mapping Engine
|
||||
*
|
||||
* Intelligently maps accounts from an imported SIE file to
|
||||
* the user's BAS chart of accounts. Uses multiple strategies:
|
||||
* 1. Exact account number match
|
||||
* 2. Account name similarity (Levenshtein distance)
|
||||
* 3. Account class consistency (5xxx → expense, etc.)
|
||||
* 4. User-defined overrides
|
||||
* Maps accounts from an imported SIE file to the user's BAS chart of accounts.
|
||||
* Uses exact account number matching only — no fuzzy/heuristic matching.
|
||||
* This aligns with Swedish industry standard (e.g. Fortnox): exact match,
|
||||
* create new, or let the user map manually.
|
||||
*/
|
||||
|
||||
import type { BASAccount } from '@/types'
|
||||
@@ -18,113 +16,8 @@ import type {
|
||||
} from './types'
|
||||
|
||||
/**
|
||||
* Calculate Levenshtein distance between two strings
|
||||
* Used for name similarity scoring
|
||||
*/
|
||||
function levenshteinDistance(str1: string, str2: string): number {
|
||||
const s1 = str1.toLowerCase()
|
||||
const s2 = str2.toLowerCase()
|
||||
|
||||
if (s1.length === 0) return s2.length
|
||||
if (s2.length === 0) return s1.length
|
||||
|
||||
const matrix: number[][] = []
|
||||
|
||||
// Initialize first column
|
||||
for (let i = 0; i <= s1.length; i++) {
|
||||
matrix[i] = [i]
|
||||
}
|
||||
|
||||
// Initialize first row
|
||||
for (let j = 0; j <= s2.length; j++) {
|
||||
matrix[0][j] = j
|
||||
}
|
||||
|
||||
// Fill in the rest of the matrix
|
||||
for (let i = 1; i <= s1.length; i++) {
|
||||
for (let j = 1; j <= s2.length; j++) {
|
||||
const cost = s1[i - 1] === s2[j - 1] ? 0 : 1
|
||||
matrix[i][j] = Math.min(
|
||||
matrix[i - 1][j] + 1, // deletion
|
||||
matrix[i][j - 1] + 1, // insertion
|
||||
matrix[i - 1][j - 1] + cost // substitution
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
return matrix[s1.length][s2.length]
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate similarity score between two strings (0-1)
|
||||
*/
|
||||
function nameSimilarity(name1: string, name2: string): number {
|
||||
if (!name1 || !name2) return 0
|
||||
|
||||
const distance = levenshteinDistance(name1, name2)
|
||||
const maxLength = Math.max(name1.length, name2.length)
|
||||
|
||||
if (maxLength === 0) return 1
|
||||
|
||||
return 1 - distance / maxLength
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalize Swedish account names for better matching
|
||||
*/
|
||||
function normalizeAccountName(name: string): string {
|
||||
return name
|
||||
.toLowerCase()
|
||||
.replace(/[^\wåäö\s]/gi, '') // Remove special chars except Swedish
|
||||
.replace(/\s+/g, ' ') // Normalize whitespace
|
||||
.trim()
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the account class (first digit) from an account number
|
||||
*/
|
||||
function getAccountClass(accountNumber: string): number {
|
||||
const firstDigit = accountNumber.charAt(0)
|
||||
return parseInt(firstDigit, 10) || 0
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the account group (first two digits) from an account number
|
||||
*/
|
||||
function getAccountGroup(accountNumber: string): string {
|
||||
return accountNumber.substring(0, 2)
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if two accounts are in compatible classes
|
||||
*/
|
||||
function areClassesCompatible(sourceNumber: string, targetNumber: string): boolean {
|
||||
const sourceClass = getAccountClass(sourceNumber)
|
||||
const targetClass = getAccountClass(targetNumber)
|
||||
|
||||
// Same class is always compatible
|
||||
if (sourceClass === targetClass) return true
|
||||
|
||||
// Allow some flexibility for related classes
|
||||
// 1xxx (assets) can map to 1xxx
|
||||
// 2xxx (equity/liabilities) can map to 2xxx
|
||||
// 3xxx (revenue) can map to 3xxx
|
||||
// 4xxx (cost of goods) can map to 4xxx or 5xxx
|
||||
// 5xxx (external expenses) can map to 5xxx or 6xxx
|
||||
// 6xxx (other external) can map to 6xxx or 5xxx
|
||||
// 7xxx (personnel) can map to 7xxx
|
||||
// 8xxx (financial) can map to 8xxx
|
||||
|
||||
if (sourceClass === 4 && targetClass === 5) return true
|
||||
if (sourceClass === 5 && targetClass === 6) return true
|
||||
if (sourceClass === 6 && targetClass === 5) return true
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the best matching BAS account for a source account
|
||||
* Find the best matching BAS account for a source account.
|
||||
* Only matches on exact account number — no fuzzy matching.
|
||||
*/
|
||||
function findBestMatch(
|
||||
source: SIEAccount,
|
||||
@@ -139,78 +32,25 @@ function findBestMatch(
|
||||
}
|
||||
}
|
||||
|
||||
let bestMatch: BASAccount | null = null
|
||||
let bestScore = 0
|
||||
let matchType: AccountMatchType = 'class'
|
||||
// Exact account number match
|
||||
const exactMatch = basAccounts.find(
|
||||
(target) => source.number === target.account_number
|
||||
)
|
||||
|
||||
for (const target of basAccounts) {
|
||||
// Strategy 1: Exact account number match
|
||||
if (source.number === target.account_number) {
|
||||
return {
|
||||
sourceAccount: source.number,
|
||||
sourceName: source.name,
|
||||
targetAccount: target.account_number,
|
||||
targetName: target.account_name,
|
||||
confidence: 1.0,
|
||||
matchType: 'exact',
|
||||
isOverride: false,
|
||||
}
|
||||
}
|
||||
|
||||
// Strategy 2: Name similarity
|
||||
const nameSim = nameSimilarity(
|
||||
normalizeAccountName(source.name),
|
||||
normalizeAccountName(target.account_name)
|
||||
)
|
||||
|
||||
// Strategy 3: Account class compatibility
|
||||
const classCompatible = areClassesCompatible(source.number, target.account_number)
|
||||
const sameGroup = getAccountGroup(source.number) === getAccountGroup(target.account_number)
|
||||
|
||||
// Calculate combined score
|
||||
let score = 0
|
||||
|
||||
// Name similarity is most important
|
||||
if (nameSim >= 0.9) {
|
||||
score = 0.9 + (nameSim - 0.9) // 0.9 - 1.0
|
||||
} else if (nameSim >= 0.7) {
|
||||
score = 0.7 + (nameSim - 0.7) * 0.5 // 0.7 - 0.85
|
||||
} else if (nameSim >= 0.5) {
|
||||
score = 0.5 + (nameSim - 0.5) * 0.25 // 0.5 - 0.575
|
||||
}
|
||||
|
||||
// Boost for same account group (first 2 digits)
|
||||
if (sameGroup) {
|
||||
score += 0.2
|
||||
} else if (classCompatible) {
|
||||
score += 0.1
|
||||
}
|
||||
|
||||
// Penalize cross-class mappings
|
||||
if (!classCompatible) {
|
||||
score *= 0.5
|
||||
}
|
||||
|
||||
if (score > bestScore) {
|
||||
bestScore = score
|
||||
bestMatch = target
|
||||
matchType = nameSim >= 0.7 ? 'name' : 'class'
|
||||
if (exactMatch) {
|
||||
return {
|
||||
sourceAccount: source.number,
|
||||
sourceName: source.name,
|
||||
targetAccount: exactMatch.account_number,
|
||||
targetName: exactMatch.account_name,
|
||||
confidence: 1.0,
|
||||
matchType: 'exact',
|
||||
isOverride: false,
|
||||
}
|
||||
}
|
||||
|
||||
if (!bestMatch || bestScore < 0.3) {
|
||||
return null
|
||||
}
|
||||
|
||||
return {
|
||||
sourceAccount: source.number,
|
||||
sourceName: source.name,
|
||||
targetAccount: bestMatch.account_number,
|
||||
targetName: bestMatch.account_name,
|
||||
confidence: Math.min(bestScore, 1.0),
|
||||
matchType,
|
||||
isOverride: false,
|
||||
}
|
||||
// No match found
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -2,20 +2,47 @@
|
||||
* Swedbank CSV format parser
|
||||
*
|
||||
* Format: Comma-delimited, PERIOD decimal separator (exception among Swedish banks!)
|
||||
* Columns: Clearingnummer, Kontonummer, Datum, Text, Belopp, Saldo, and more (12 columns)
|
||||
* Columns (real export): Radnr, Clnr, Kontonr, Produkt, Valuta, Bokfdag, Transdag,
|
||||
* Valutadag, Referens, Text, Belopp, Saldo
|
||||
* Date format: YYYY-MM-DD
|
||||
* Encoding: UTF-8 or Windows-1252
|
||||
*
|
||||
* Notes:
|
||||
* - First line is metadata (account info), SKIP it
|
||||
* - First line is metadata (e.g. "* Transaktionsrapport Period ..."), SKIP it
|
||||
* - Second line is the actual header
|
||||
* - Uses period as decimal separator (unlike Nordea/SEB/Handelsbanken)
|
||||
* - Headers may be abbreviated (Clnr vs Clearingnummer, Bokfdag vs Bokföringsdatum)
|
||||
* - Referens column contains counterparty/payee name
|
||||
* - Text column contains transaction type (e.g. "Bg-bet. via internet")
|
||||
*/
|
||||
|
||||
import type { BankFileFormat, BankFileParseResult, ParsedBankTransaction, BankFileParseIssue } from '../types'
|
||||
import { prepareContent } from '../encoding'
|
||||
import { parseCSVLine } from './nordea'
|
||||
|
||||
/**
|
||||
* Check if a header value matches any of the given patterns (case-insensitive)
|
||||
*/
|
||||
function matchesHeader(header: string, patterns: string[]): boolean {
|
||||
const h = header.toLowerCase()
|
||||
return patterns.some((p) => h === p || h.includes(p))
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a line looks like a Swedbank header row
|
||||
*/
|
||||
function isSwedbankHeader(line: string): boolean {
|
||||
const lower = line.toLowerCase()
|
||||
return (
|
||||
// Full names (legacy or alternative exports)
|
||||
lower.includes('clearingnummer') || lower.includes('radnummer') ||
|
||||
// Abbreviated names (current export format)
|
||||
/\bradnr\b/.test(lower) || /\bclnr\b/.test(lower) ||
|
||||
// Combination of typical Swedbank columns
|
||||
(lower.includes('bokfdag') && lower.includes('belopp'))
|
||||
)
|
||||
}
|
||||
|
||||
export const swedbankFormat: BankFileFormat = {
|
||||
id: 'swedbank',
|
||||
name: 'Swedbank',
|
||||
@@ -25,14 +52,10 @@ export const swedbankFormat: BankFileFormat = {
|
||||
detect(content: string, _filename: string): boolean {
|
||||
const prepared = prepareContent(content)
|
||||
const lines = prepared.split('\n')
|
||||
// Check first two lines — Swedbank has metadata line, then header
|
||||
const line1 = lines[0]?.toLowerCase() || ''
|
||||
const line2 = lines[1]?.toLowerCase() || ''
|
||||
const line1 = lines[0] || ''
|
||||
const line2 = lines[1] || ''
|
||||
|
||||
return (
|
||||
(line1.includes('clearingnummer') || line2.includes('clearingnummer') ||
|
||||
line1.includes('radnummer') || line2.includes('radnummer'))
|
||||
)
|
||||
return isSwedbankHeader(line1) || isSwedbankHeader(line2)
|
||||
},
|
||||
|
||||
parse(content: string): BankFileParseResult {
|
||||
@@ -43,16 +66,30 @@ export const swedbankFormat: BankFileFormat = {
|
||||
const issues: BankFileParseIssue[] = []
|
||||
let skippedRows = 0
|
||||
|
||||
// Determine where the header is
|
||||
// Line 0 might be metadata, line 1 might be header
|
||||
let headerLineIdx = 0
|
||||
const line0Lower = lines[0]?.toLowerCase() || ''
|
||||
const line1Lower = lines[1]?.toLowerCase() || ''
|
||||
// Find the header row — may be line 0 or line 1 (if line 0 is metadata)
|
||||
let headerLineIdx = -1
|
||||
for (let i = 0; i < Math.min(lines.length, 3); i++) {
|
||||
if (isSwedbankHeader(lines[i])) {
|
||||
headerLineIdx = i
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if (line1Lower.includes('clearingnummer') || line1Lower.includes('radnummer')) {
|
||||
headerLineIdx = 1
|
||||
} else if (line0Lower.includes('clearingnummer') || line0Lower.includes('radnummer')) {
|
||||
headerLineIdx = 0
|
||||
if (headerLineIdx === -1) {
|
||||
issues.push({
|
||||
row: 1,
|
||||
message: 'Could not find Swedbank header row',
|
||||
severity: 'error',
|
||||
})
|
||||
return {
|
||||
format: 'swedbank',
|
||||
format_name: 'Swedbank',
|
||||
transactions: [],
|
||||
date_from: null,
|
||||
date_to: null,
|
||||
issues,
|
||||
stats: { total_rows: 0, parsed_rows: 0, skipped_rows: 0, total_income: 0, total_expenses: 0 },
|
||||
}
|
||||
}
|
||||
|
||||
const headerLine = lines[headerLineIdx] || ''
|
||||
@@ -60,16 +97,19 @@ export const swedbankFormat: BankFileFormat = {
|
||||
h.trim().toLowerCase().replace(/"/g, '')
|
||||
)
|
||||
|
||||
// Find column indices
|
||||
const dateIdx = headers.findIndex((h) => h === 'datum' || h.includes('bokföringsdatum'))
|
||||
// Find column indices — support both abbreviated and full header names
|
||||
const dateIdx = headers.findIndex((h) =>
|
||||
matchesHeader(h, ['bokfdag', 'bokföringsdatum', 'datum'])
|
||||
)
|
||||
const descIdx = headers.findIndex((h) => h === 'text' || h.includes('beskrivning'))
|
||||
const amountIdx = headers.findIndex((h) => h === 'belopp')
|
||||
const balanceIdx = headers.findIndex((h) => h === 'saldo')
|
||||
const referenceIdx = headers.findIndex((h) => h === 'referens')
|
||||
|
||||
if (dateIdx === -1 || amountIdx === -1) {
|
||||
issues.push({
|
||||
row: 1,
|
||||
message: 'Could not identify required columns (datum, belopp)',
|
||||
row: headerLineIdx + 1,
|
||||
message: `Could not identify required columns (datum, belopp). Found headers: ${headers.join(', ')}`,
|
||||
severity: 'error',
|
||||
})
|
||||
return {
|
||||
@@ -91,7 +131,8 @@ export const swedbankFormat: BankFileFormat = {
|
||||
const fields = parseCSVLine(line, ',').map((f) => f.trim().replace(/^"|"$/g, ''))
|
||||
|
||||
const date = fields[dateIdx]
|
||||
const description = descIdx >= 0 ? fields[descIdx] : 'Unknown'
|
||||
const reference = referenceIdx >= 0 ? fields[referenceIdx]?.trim() : null
|
||||
const textDesc = descIdx >= 0 ? fields[descIdx]?.trim() : null
|
||||
const amountStr = fields[amountIdx]
|
||||
const balanceStr = balanceIdx >= 0 ? fields[balanceIdx] : undefined
|
||||
|
||||
@@ -116,14 +157,19 @@ export const swedbankFormat: BankFileFormat = {
|
||||
|
||||
const balance = balanceStr ? parseFloat(balanceStr.replace(/\s/g, '')) : null
|
||||
|
||||
// Build description: use reference (counterparty) as primary, text as secondary
|
||||
const description = reference && textDesc
|
||||
? `${reference} — ${textDesc}`
|
||||
: reference || textDesc || 'Unknown'
|
||||
|
||||
transactions.push({
|
||||
date,
|
||||
description: (description || 'Unknown').trim(),
|
||||
description,
|
||||
amount,
|
||||
currency: 'SEK',
|
||||
balance: isNaN(balance as number) ? null : balance,
|
||||
reference: null,
|
||||
counterparty: null,
|
||||
counterparty: reference || null,
|
||||
raw_line: line,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -357,6 +357,7 @@ async function importVouchers(
|
||||
description: v.description,
|
||||
source_type: 'import',
|
||||
status: 'posted',
|
||||
committed_at: new Date().toISOString(),
|
||||
}))
|
||||
|
||||
// Insert headers
|
||||
|
||||
+100
-29
@@ -5,7 +5,7 @@ import {
|
||||
View,
|
||||
StyleSheet,
|
||||
} from '@react-pdf/renderer'
|
||||
import type { Invoice, InvoiceItem, Customer, CompanySettings } from '@/types'
|
||||
import type { Invoice, InvoiceItem, Customer, CompanySettings, InvoiceDocumentType } from '@/types'
|
||||
|
||||
// Create styles
|
||||
const styles = StyleSheet.create({
|
||||
@@ -82,7 +82,7 @@ const styles = StyleSheet.create({
|
||||
borderBottomColor: '#eee',
|
||||
},
|
||||
colDescription: {
|
||||
flex: 4,
|
||||
flex: 3.5,
|
||||
},
|
||||
colQty: {
|
||||
flex: 1,
|
||||
@@ -96,6 +96,10 @@ const styles = StyleSheet.create({
|
||||
flex: 1.5,
|
||||
textAlign: 'right',
|
||||
},
|
||||
colVat: {
|
||||
flex: 1,
|
||||
textAlign: 'right',
|
||||
},
|
||||
colTotal: {
|
||||
flex: 1.5,
|
||||
textAlign: 'right',
|
||||
@@ -255,6 +259,14 @@ function formatOrgNumber(orgNumber: string): string {
|
||||
return orgNumber
|
||||
}
|
||||
|
||||
function getDocumentTitle(invoice: Invoice): string {
|
||||
if (invoice.credited_invoice_id) return 'KREDITFAKTURA'
|
||||
const docType = (invoice as Invoice & { document_type?: InvoiceDocumentType }).document_type || 'invoice'
|
||||
if (docType === 'proforma') return 'PROFORMAFAKTURA'
|
||||
if (docType === 'delivery_note') return 'FÖLJESEDEL'
|
||||
return 'FAKTURA'
|
||||
}
|
||||
|
||||
interface InvoicePDFProps {
|
||||
invoice: Invoice
|
||||
customer: Customer
|
||||
@@ -266,6 +278,28 @@ interface InvoicePDFProps {
|
||||
export function InvoicePDF({ invoice, customer, items, company, originalInvoiceNumber }: InvoicePDFProps) {
|
||||
const isCreditNote = !!invoice.credited_invoice_id
|
||||
|
||||
// Check if items have mixed VAT rates
|
||||
const hasPerLineVat = items.some((item) => item.vat_rate !== undefined && item.vat_rate !== null)
|
||||
const uniqueRates = hasPerLineVat
|
||||
? new Set(items.map((item) => item.vat_rate))
|
||||
: new Set<number>()
|
||||
const showVatColumn = hasPerLineVat && uniqueRates.size > 1
|
||||
|
||||
// Calculate per-rate VAT breakdown for totals
|
||||
const vatByRate = new Map<number, { base: number; vat: number }>()
|
||||
if (hasPerLineVat) {
|
||||
for (const item of items) {
|
||||
const rate = item.vat_rate ?? 25
|
||||
const group = vatByRate.get(rate) || { base: 0, vat: 0 }
|
||||
group.base += Math.abs(item.line_total)
|
||||
group.vat += Math.abs(item.vat_amount || 0)
|
||||
vatByRate.set(rate, group)
|
||||
}
|
||||
}
|
||||
const docType = (invoice as Invoice & { document_type?: InvoiceDocumentType }).document_type || 'invoice'
|
||||
const isDeliveryNote = docType === 'delivery_note'
|
||||
const isProforma = docType === 'proforma'
|
||||
|
||||
return (
|
||||
<Document>
|
||||
<Page size="A4" style={styles.page}>
|
||||
@@ -273,7 +307,7 @@ export function InvoicePDF({ invoice, customer, items, company, originalInvoiceN
|
||||
<View style={styles.header}>
|
||||
<View>
|
||||
<Text style={[styles.title, isCreditNote ? styles.creditNoteTitle : {}]}>
|
||||
{isCreditNote ? 'KREDITFAKTURA' : 'FAKTURA'}
|
||||
{getDocumentTitle(invoice)}
|
||||
</Text>
|
||||
<Text style={{ marginTop: 5, color: '#666' }}>{invoice.invoice_number}</Text>
|
||||
</View>
|
||||
@@ -356,8 +390,15 @@ export function InvoicePDF({ invoice, customer, items, company, originalInvoiceN
|
||||
<Text style={[styles.colDescription, styles.tableHeaderText]}>Beskrivning</Text>
|
||||
<Text style={[styles.colQty, styles.tableHeaderText]}>Antal</Text>
|
||||
<Text style={[styles.colUnit, styles.tableHeaderText]}>Enhet</Text>
|
||||
<Text style={[styles.colPrice, styles.tableHeaderText]}>à-pris</Text>
|
||||
<Text style={[styles.colTotal, styles.tableHeaderText]}>Summa</Text>
|
||||
{!isDeliveryNote && (
|
||||
<Text style={[styles.colPrice, styles.tableHeaderText]}>à-pris</Text>
|
||||
)}
|
||||
{!isDeliveryNote && showVatColumn && (
|
||||
<Text style={[styles.colVat, styles.tableHeaderText]}>Moms</Text>
|
||||
)}
|
||||
{!isDeliveryNote && (
|
||||
<Text style={[styles.colTotal, styles.tableHeaderText]}>Summa</Text>
|
||||
)}
|
||||
</View>
|
||||
|
||||
{/* Table rows */}
|
||||
@@ -366,37 +407,67 @@ export function InvoicePDF({ invoice, customer, items, company, originalInvoiceN
|
||||
<Text style={styles.colDescription}>{item.description}</Text>
|
||||
<Text style={styles.colQty}>{item.quantity}</Text>
|
||||
<Text style={styles.colUnit}>{item.unit}</Text>
|
||||
<Text style={styles.colPrice}>{formatCurrency(item.unit_price, invoice.currency)}</Text>
|
||||
<Text style={styles.colTotal}>{formatCurrency(item.line_total, invoice.currency)}</Text>
|
||||
{!isDeliveryNote && (
|
||||
<Text style={styles.colPrice}>{formatCurrency(item.unit_price, invoice.currency)}</Text>
|
||||
)}
|
||||
{!isDeliveryNote && showVatColumn && (
|
||||
<Text style={styles.colVat}>{item.vat_rate ?? 25}%</Text>
|
||||
)}
|
||||
{!isDeliveryNote && (
|
||||
<Text style={styles.colTotal}>{formatCurrency(item.line_total, invoice.currency)}</Text>
|
||||
)}
|
||||
</View>
|
||||
))}
|
||||
</View>
|
||||
</View>
|
||||
|
||||
{/* Totals */}
|
||||
<View style={styles.totalsSection}>
|
||||
<View style={styles.totalRow}>
|
||||
<Text style={styles.totalLabel}>Delsumma:</Text>
|
||||
<Text style={styles.totalValue}>{formatCurrency(invoice.subtotal, invoice.currency)}</Text>
|
||||
</View>
|
||||
<View style={styles.totalRow}>
|
||||
<Text style={styles.totalLabel}>Moms ({invoice.vat_rate}%):</Text>
|
||||
<Text style={styles.totalValue}>{formatCurrency(invoice.vat_amount, invoice.currency)}</Text>
|
||||
</View>
|
||||
<View style={styles.grandTotal}>
|
||||
<Text style={styles.grandTotalLabel}>{isCreditNote ? 'Att kreditera:' : 'Att betala:'}</Text>
|
||||
<Text style={styles.grandTotalValue}>{formatCurrency(invoice.total, invoice.currency)}</Text>
|
||||
</View>
|
||||
{invoice.currency !== 'SEK' && invoice.total_sek && (
|
||||
<View style={[styles.totalRow, { marginTop: 8 }]}>
|
||||
<Text style={[styles.totalLabel, { fontSize: 9 }]}>I SEK (kurs {invoice.exchange_rate}):</Text>
|
||||
<Text style={[styles.totalValue, { fontSize: 9 }]}>{formatCurrency(invoice.total_sek, 'SEK')}</Text>
|
||||
{/* Totals - hidden for delivery notes */}
|
||||
{!isDeliveryNote && (
|
||||
<View style={styles.totalsSection}>
|
||||
<View style={styles.totalRow}>
|
||||
<Text style={styles.totalLabel}>Delsumma:</Text>
|
||||
<Text style={styles.totalValue}>{formatCurrency(invoice.subtotal, invoice.currency)}</Text>
|
||||
</View>
|
||||
)}
|
||||
</View>
|
||||
{vatByRate.size > 1 ? (
|
||||
Array.from(vatByRate.entries())
|
||||
.filter(([, group]) => group.vat > 0)
|
||||
.sort(([a], [b]) => b - a)
|
||||
.map(([rate, group]) => (
|
||||
<View key={rate} style={styles.totalRow}>
|
||||
<Text style={styles.totalLabel}>Moms {rate}%:</Text>
|
||||
<Text style={styles.totalValue}>{formatCurrency(group.vat, invoice.currency)}</Text>
|
||||
</View>
|
||||
))
|
||||
) : (
|
||||
<View style={styles.totalRow}>
|
||||
<Text style={styles.totalLabel}>Moms ({invoice.vat_rate ?? 25}%):</Text>
|
||||
<Text style={styles.totalValue}>{formatCurrency(invoice.vat_amount, invoice.currency)}</Text>
|
||||
</View>
|
||||
)}
|
||||
<View style={styles.grandTotal}>
|
||||
<Text style={styles.grandTotalLabel}>{isCreditNote ? 'Att kreditera:' : 'Att betala:'}</Text>
|
||||
<Text style={styles.grandTotalValue}>{formatCurrency(invoice.total, invoice.currency)}</Text>
|
||||
</View>
|
||||
{invoice.currency !== 'SEK' && invoice.total_sek && (
|
||||
<View style={[styles.totalRow, { marginTop: 8 }]}>
|
||||
<Text style={[styles.totalLabel, { fontSize: 9 }]}>I SEK (kurs {invoice.exchange_rate}):</Text>
|
||||
<Text style={[styles.totalValue, { fontSize: 9 }]}>{formatCurrency(invoice.total_sek, 'SEK')}</Text>
|
||||
</View>
|
||||
)}
|
||||
</View>
|
||||
)}
|
||||
|
||||
{/* Payment information - not shown for credit notes */}
|
||||
{!isCreditNote && (
|
||||
{/* Proforma notice */}
|
||||
{isProforma && (
|
||||
<View style={[styles.reverseChargeBox, { backgroundColor: '#e8f4fd', borderColor: '#90cdf4' }]}>
|
||||
<Text style={[styles.reverseChargeText, { color: '#2b6cb0' }]}>
|
||||
Detta är en proformafaktura och utgör ingen betalningsanmodan.
|
||||
</Text>
|
||||
</View>
|
||||
)}
|
||||
|
||||
{/* Payment information - not shown for credit notes, proformas, or delivery notes */}
|
||||
{!isCreditNote && !isProforma && !isDeliveryNote && (
|
||||
<View style={styles.paymentSection}>
|
||||
<Text style={styles.paymentTitle}>Betalningsinformation</Text>
|
||||
{company.bank_name && (
|
||||
|
||||
@@ -1,5 +1,58 @@
|
||||
import type { CustomerType, VatTreatment } from '@/types'
|
||||
|
||||
export interface VatRateOption {
|
||||
rate: number
|
||||
label: string
|
||||
treatment: VatTreatment
|
||||
}
|
||||
|
||||
/**
|
||||
* Get available VAT rates for invoice line items based on customer type.
|
||||
*
|
||||
* Swedish/EU-unvalidated customers can choose between 25%, 12%, 6%, and 0% (exempt).
|
||||
* Reverse charge and export customers are locked to 0%.
|
||||
*/
|
||||
export function getAvailableVatRates(
|
||||
customerType: CustomerType,
|
||||
vatNumberValidated: boolean = false
|
||||
): VatRateOption[] {
|
||||
// EU business with validated VAT → reverse charge, locked to 0%
|
||||
if (customerType === 'eu_business' && vatNumberValidated) {
|
||||
return [{ rate: 0, label: '0% (omvänd skattskyldighet)', treatment: 'reverse_charge' }]
|
||||
}
|
||||
|
||||
// Non-EU → export, locked to 0%
|
||||
if (customerType === 'non_eu_business') {
|
||||
return [{ rate: 0, label: '0% (export)', treatment: 'export' }]
|
||||
}
|
||||
|
||||
// Swedish customers (or EU without validated VAT) can choose any rate
|
||||
return [
|
||||
{ rate: 25, label: '25%', treatment: 'standard_25' },
|
||||
{ rate: 12, label: '12%', treatment: 'reduced_12' },
|
||||
{ rate: 6, label: '6%', treatment: 'reduced_6' },
|
||||
{ rate: 0, label: '0% (momsfritt)', treatment: 'exempt' },
|
||||
]
|
||||
}
|
||||
|
||||
/**
|
||||
* Map a numeric VAT rate to a VatTreatment.
|
||||
*/
|
||||
export function getVatTreatmentForRate(rate: number): VatTreatment {
|
||||
switch (rate) {
|
||||
case 25:
|
||||
return 'standard_25'
|
||||
case 12:
|
||||
return 'reduced_12'
|
||||
case 6:
|
||||
return 'reduced_6'
|
||||
case 0:
|
||||
return 'exempt'
|
||||
default:
|
||||
return 'standard_25'
|
||||
}
|
||||
}
|
||||
|
||||
export interface VatRule {
|
||||
treatment: VatTreatment
|
||||
rate: number
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -9,7 +9,7 @@ let results: Array<{ data?: unknown; error?: unknown }>
|
||||
|
||||
function makeBuilder() {
|
||||
const b: Record<string, unknown> = {}
|
||||
for (const m of ['select', 'eq', 'in', 'lt', 'order']) {
|
||||
for (const m of ['select', 'eq', 'in', 'lt', 'order', 'range']) {
|
||||
b[m] = vi.fn().mockReturnValue(b)
|
||||
}
|
||||
b.single = vi.fn().mockImplementation(async () => results[resultIdx++] ?? { data: null, error: null })
|
||||
@@ -51,7 +51,7 @@ describe('generateGeneralLedger', () => {
|
||||
it('returns empty report when no entries in period', async () => {
|
||||
results = [
|
||||
// 0: fiscal_periods.single()
|
||||
{ data: { start_date: '2024-01-01', end_date: '2024-12-31' }, error: null },
|
||||
{ data: { period_start: '2024-01-01', period_end: '2024-12-31' }, error: null },
|
||||
// 1: journal_entries (empty)
|
||||
{ data: [], error: null },
|
||||
]
|
||||
@@ -64,7 +64,7 @@ describe('generateGeneralLedger', () => {
|
||||
it('groups lines by account with correct totals and running balance', async () => {
|
||||
results = [
|
||||
// 0: fiscal_periods.single()
|
||||
{ data: { start_date: '2024-01-01', end_date: '2024-12-31' }, error: null },
|
||||
{ data: { period_start: '2024-01-01', period_end: '2024-12-31' }, error: null },
|
||||
// 1: journal_entries for this period
|
||||
{
|
||||
data: [
|
||||
@@ -122,7 +122,7 @@ describe('generateGeneralLedger', () => {
|
||||
it('computes opening balance from prior period entries', async () => {
|
||||
results = [
|
||||
// 0: fiscal_periods.single()
|
||||
{ data: { start_date: '2025-01-01', end_date: '2025-12-31' }, error: null },
|
||||
{ data: { period_start: '2025-01-01', period_end: '2025-12-31' }, error: null },
|
||||
// 1: journal_entries for this period
|
||||
{
|
||||
data: [
|
||||
@@ -168,7 +168,7 @@ describe('generateGeneralLedger', () => {
|
||||
it('filters accounts by account_from and account_to', async () => {
|
||||
results = [
|
||||
// 0: fiscal_periods.single()
|
||||
{ data: { start_date: '2024-01-01', end_date: '2024-12-31' }, error: null },
|
||||
{ data: { period_start: '2024-01-01', period_end: '2024-12-31' }, error: null },
|
||||
// 1: journal_entries
|
||||
{
|
||||
data: [
|
||||
@@ -200,7 +200,7 @@ describe('generateGeneralLedger', () => {
|
||||
it('sorts lines within account by date then voucher number', async () => {
|
||||
results = [
|
||||
// 0: fiscal_periods.single()
|
||||
{ data: { start_date: '2024-01-01', end_date: '2024-12-31' }, error: null },
|
||||
{ data: { period_start: '2024-01-01', period_end: '2024-12-31' }, error: null },
|
||||
// 1: entries out of order
|
||||
{
|
||||
data: [
|
||||
@@ -236,7 +236,7 @@ describe('generateGeneralLedger', () => {
|
||||
|
||||
it('uses Math.round for monetary precision', async () => {
|
||||
results = [
|
||||
{ data: { start_date: '2024-01-01', end_date: '2024-12-31' }, error: null },
|
||||
{ data: { period_start: '2024-01-01', period_end: '2024-12-31' }, error: null },
|
||||
{
|
||||
data: [
|
||||
{ id: 'e1', entry_date: '2024-01-15', voucher_number: 1, voucher_series: 'A', description: 'Precision', source_type: 'manual' },
|
||||
|
||||
@@ -9,7 +9,7 @@ let results: Array<{ data?: unknown; error?: unknown }>
|
||||
|
||||
function makeBuilder() {
|
||||
const b: Record<string, unknown> = {}
|
||||
for (const m of ['select', 'eq', 'in', 'order']) {
|
||||
for (const m of ['select', 'eq', 'in', 'order', 'range']) {
|
||||
b[m] = vi.fn().mockReturnValue(b)
|
||||
}
|
||||
b.single = vi.fn().mockImplementation(async () => results[resultIdx++] ?? { data: null, error: null })
|
||||
@@ -49,7 +49,7 @@ describe('generateJournalRegister', () => {
|
||||
|
||||
it('returns empty report when no entries in period', async () => {
|
||||
results = [
|
||||
{ data: { start_date: '2024-01-01', end_date: '2024-12-31' }, error: null },
|
||||
{ data: { period_start: '2024-01-01', period_end: '2024-12-31' }, error: null },
|
||||
{ data: [], error: null },
|
||||
]
|
||||
|
||||
@@ -62,7 +62,7 @@ describe('generateJournalRegister', () => {
|
||||
it('produces entries in registration order with correct totals', async () => {
|
||||
results = [
|
||||
// 0: fiscal_periods.single()
|
||||
{ data: { start_date: '2024-01-01', end_date: '2024-12-31' }, error: null },
|
||||
{ data: { period_start: '2024-01-01', period_end: '2024-12-31' }, error: null },
|
||||
// 1: journal_entries (already ordered by series/number)
|
||||
{
|
||||
data: [
|
||||
@@ -117,7 +117,7 @@ describe('generateJournalRegister', () => {
|
||||
|
||||
it('includes reversed entries with correct status', async () => {
|
||||
results = [
|
||||
{ data: { start_date: '2024-01-01', end_date: '2024-12-31' }, error: null },
|
||||
{ data: { period_start: '2024-01-01', period_end: '2024-12-31' }, error: null },
|
||||
{
|
||||
data: [
|
||||
{ id: 'e1', entry_date: '2024-01-15', voucher_number: 1, voucher_series: 'A', description: 'Original', source_type: 'manual', status: 'reversed' },
|
||||
@@ -146,7 +146,7 @@ describe('generateJournalRegister', () => {
|
||||
|
||||
it('resolves account names from chart_of_accounts', async () => {
|
||||
results = [
|
||||
{ data: { start_date: '2024-01-01', end_date: '2024-12-31' }, error: null },
|
||||
{ data: { period_start: '2024-01-01', period_end: '2024-12-31' }, error: null },
|
||||
{
|
||||
data: [
|
||||
{ id: 'e1', entry_date: '2024-01-15', voucher_number: 1, voucher_series: 'A', description: 'Test', source_type: 'manual', status: 'posted' },
|
||||
@@ -180,7 +180,7 @@ describe('generateJournalRegister', () => {
|
||||
|
||||
it('defaults voucher_series to A when null', async () => {
|
||||
results = [
|
||||
{ data: { start_date: '2024-01-01', end_date: '2024-12-31' }, error: null },
|
||||
{ data: { period_start: '2024-01-01', period_end: '2024-12-31' }, error: null },
|
||||
{
|
||||
data: [
|
||||
{ id: 'e1', entry_date: '2024-01-15', voucher_number: 1, voucher_series: null, description: 'No series', source_type: 'manual', status: 'posted' },
|
||||
|
||||
@@ -24,7 +24,7 @@ describe('generateMonthlyBreakdown', () => {
|
||||
it('returns empty months when no journal entries exist', async () => {
|
||||
// First call: fiscal period
|
||||
mockResult({
|
||||
data: { start_date: '2024-01-01', end_date: '2024-12-31' },
|
||||
data: { period_start: '2024-01-01', period_end: '2024-12-31' },
|
||||
error: null,
|
||||
})
|
||||
|
||||
@@ -45,7 +45,7 @@ describe('generateMonthlyBreakdown', () => {
|
||||
eq: () => ({
|
||||
single: () =>
|
||||
Promise.resolve({
|
||||
data: { start_date: '2024-01-01', end_date: '2024-12-31' },
|
||||
data: { period_start: '2024-01-01', period_end: '2024-12-31' },
|
||||
error: null,
|
||||
}),
|
||||
}),
|
||||
@@ -88,7 +88,7 @@ describe('generateMonthlyBreakdown', () => {
|
||||
eq: () => ({
|
||||
single: () =>
|
||||
Promise.resolve({
|
||||
data: { start_date: '2024-01-01', end_date: '2024-03-31' },
|
||||
data: { period_start: '2024-01-01', period_end: '2024-03-31' },
|
||||
error: null,
|
||||
}),
|
||||
}),
|
||||
@@ -167,7 +167,7 @@ describe('generateMonthlyBreakdown', () => {
|
||||
eq: () => ({
|
||||
single: () =>
|
||||
Promise.resolve({
|
||||
data: { start_date: '2024-01-01', end_date: '2024-01-31' },
|
||||
data: { period_start: '2024-01-01', period_end: '2024-01-31' },
|
||||
error: null,
|
||||
}),
|
||||
}),
|
||||
|
||||
@@ -9,7 +9,7 @@ let results: Array<{ data?: unknown; error?: unknown }>
|
||||
|
||||
function makeBuilder() {
|
||||
const b: Record<string, unknown> = {}
|
||||
for (const m of ['select', 'eq', 'in', 'order']) {
|
||||
for (const m of ['select', 'eq', 'in', 'order', 'range']) {
|
||||
b[m] = vi.fn().mockReturnValue(b)
|
||||
}
|
||||
b.single = vi.fn().mockImplementation(async () => results[resultIdx++] ?? { data: null, error: null })
|
||||
|
||||
@@ -9,7 +9,7 @@ let results: Array<{ data?: unknown; error?: unknown }>
|
||||
|
||||
function makeBuilder() {
|
||||
const b: Record<string, unknown> = {}
|
||||
for (const m of ['select', 'eq', 'in']) {
|
||||
for (const m of ['select', 'eq', 'in', 'range']) {
|
||||
b[m] = vi.fn().mockReturnValue(b)
|
||||
}
|
||||
b.single = vi.fn().mockImplementation(async () => results[resultIdx++] ?? { data: null, error: null })
|
||||
|
||||
@@ -9,7 +9,7 @@ let results: Array<{ data?: unknown; error?: unknown }>
|
||||
|
||||
function makeBuilder() {
|
||||
const b: Record<string, unknown> = {}
|
||||
for (const m of ['select', 'eq', 'in', 'gte', 'lte', 'lt', 'or', 'not']) {
|
||||
for (const m of ['select', 'eq', 'in', 'gte', 'lte', 'lt', 'or', 'not', 'range']) {
|
||||
b[m] = vi.fn().mockReturnValue(b)
|
||||
}
|
||||
b.single = vi.fn().mockImplementation(async () => results[resultIdx++] ?? { data: null, error: null })
|
||||
@@ -32,7 +32,6 @@ import {
|
||||
formatPeriodLabel,
|
||||
getVatDeclarationSummary,
|
||||
calculateVatDeclaration,
|
||||
calculateVatDeclarationFromTaxCodes,
|
||||
} from '../vat-declaration'
|
||||
import type { VatDeclaration } from '@/types'
|
||||
|
||||
@@ -112,8 +111,8 @@ describe('getVatDeclarationSummary', () => {
|
||||
transactionCount: 10,
|
||||
breakdown: {
|
||||
invoices: { ruta05: 2500, ruta06: 0, ruta07: 0, ruta10: 10000, ruta11: 0, ruta12: 0, ruta39: 0, ruta40: 0 },
|
||||
transactions: { ruta48: 800 },
|
||||
receipts: { ruta48: 200 },
|
||||
transactions: { ruta48: 1000 },
|
||||
receipts: { ruta48: 0 },
|
||||
},
|
||||
}
|
||||
|
||||
@@ -155,17 +154,17 @@ describe('getVatDeclarationSummary', () => {
|
||||
})
|
||||
|
||||
// ============================================================
|
||||
// Async tests — require Supabase mocks
|
||||
// Ledger-based VAT declaration tests
|
||||
//
|
||||
// Mock queue order per call:
|
||||
// [0] fetchAllRows: journal_entry_lines (VAT-relevant accounts)
|
||||
// [1] entry counts: journal_entries source_type
|
||||
// ============================================================
|
||||
|
||||
describe('calculateVatDeclaration', () => {
|
||||
it('returns all zeros when no data exists', async () => {
|
||||
it('returns all zeros when no ledger lines exist', async () => {
|
||||
results = [
|
||||
// 0: invoices
|
||||
{ data: [], error: null },
|
||||
// 1: transactions
|
||||
{ data: [], error: null },
|
||||
// 2: receipts
|
||||
{ data: [], error: null },
|
||||
]
|
||||
|
||||
@@ -180,23 +179,20 @@ describe('calculateVatDeclaration', () => {
|
||||
expect(result.transactionCount).toBe(0)
|
||||
})
|
||||
|
||||
it('maps invoice VAT to correct rutor by moms_ruta', async () => {
|
||||
it('sums output VAT from 2611/2621/2631 credit balances', async () => {
|
||||
results = [
|
||||
// 0: invoices — various moms_ruta values
|
||||
{
|
||||
data: [
|
||||
{ subtotal: 10000, vat_amount: 2500, moms_ruta: '05', subtotal_sek: null, vat_amount_sek: null },
|
||||
{ subtotal: 5000, vat_amount: 600, moms_ruta: '06', subtotal_sek: null, vat_amount_sek: null },
|
||||
{ subtotal: 3000, vat_amount: 180, moms_ruta: '07', subtotal_sek: null, vat_amount_sek: null },
|
||||
{ subtotal: 8000, vat_amount: 0, moms_ruta: '39', subtotal_sek: null, vat_amount_sek: null },
|
||||
{ subtotal: 12000, vat_amount: 0, moms_ruta: '40', subtotal_sek: null, vat_amount_sek: null },
|
||||
{ account_number: '2611', debit_amount: 0, credit_amount: 2500 },
|
||||
{ account_number: '2621', debit_amount: 0, credit_amount: 600 },
|
||||
{ account_number: '2631', debit_amount: 0, credit_amount: 180 },
|
||||
{ account_number: '3001', debit_amount: 0, credit_amount: 10000 },
|
||||
{ account_number: '3002', debit_amount: 0, credit_amount: 5000 },
|
||||
{ account_number: '3003', debit_amount: 0, credit_amount: 3000 },
|
||||
],
|
||||
error: null,
|
||||
},
|
||||
// 1: transactions (none)
|
||||
{ data: [], error: null },
|
||||
// 2: receipts (none)
|
||||
{ data: [], error: null },
|
||||
{ data: [{ source_type: 'invoice_created' }, { source_type: 'invoice_created' }], error: null },
|
||||
]
|
||||
|
||||
const result = await calculateVatDeclaration('user-1', 'monthly', 2024, 1)
|
||||
@@ -207,224 +203,166 @@ describe('calculateVatDeclaration', () => {
|
||||
expect(result.rutor.ruta10).toBe(10000)
|
||||
expect(result.rutor.ruta11).toBe(5000)
|
||||
expect(result.rutor.ruta12).toBe(3000)
|
||||
expect(result.invoiceCount).toBe(2)
|
||||
})
|
||||
|
||||
it('sums input VAT from 2641 debit balance', async () => {
|
||||
results = [
|
||||
{
|
||||
data: [
|
||||
{ account_number: '2641', debit_amount: 250, credit_amount: 0 },
|
||||
{ account_number: '2641', debit_amount: 120, credit_amount: 0 },
|
||||
],
|
||||
error: null,
|
||||
},
|
||||
{ data: [{ source_type: 'bank_transaction' }, { source_type: 'bank_transaction' }], error: null },
|
||||
]
|
||||
|
||||
const result = await calculateVatDeclaration('user-1', 'monthly', 2024, 1)
|
||||
|
||||
expect(result.rutor.ruta48).toBe(370)
|
||||
expect(result.transactionCount).toBe(2)
|
||||
})
|
||||
|
||||
it('includes calculated input VAT (2645) from EU reverse charge in ruta48', async () => {
|
||||
results = [
|
||||
{
|
||||
data: [
|
||||
{ account_number: '2645', debit_amount: 500, credit_amount: 0 },
|
||||
{ account_number: '2641', debit_amount: 200, credit_amount: 0 },
|
||||
],
|
||||
error: null,
|
||||
},
|
||||
{ data: [], error: null },
|
||||
]
|
||||
|
||||
const result = await calculateVatDeclaration('user-1', 'monthly', 2024, 1)
|
||||
|
||||
// Both 2641 and 2645 debit balances sum into ruta48
|
||||
expect(result.rutor.ruta48).toBe(700)
|
||||
})
|
||||
|
||||
it('maps EU/export revenue to ruta39/ruta40', async () => {
|
||||
results = [
|
||||
{
|
||||
data: [
|
||||
{ account_number: '3308', debit_amount: 0, credit_amount: 8000 },
|
||||
{ account_number: '3305', debit_amount: 0, credit_amount: 12000 },
|
||||
],
|
||||
error: null,
|
||||
},
|
||||
{ data: [], error: null },
|
||||
]
|
||||
|
||||
const result = await calculateVatDeclaration('user-1', 'monthly', 2024, 1)
|
||||
|
||||
expect(result.rutor.ruta39).toBe(8000)
|
||||
expect(result.rutor.ruta40).toBe(12000)
|
||||
})
|
||||
|
||||
it('prefers subtotal_sek/vat_amount_sek for foreign currency invoices', async () => {
|
||||
it('handles credit notes as net reduction on revenue/VAT accounts', async () => {
|
||||
results = [
|
||||
// 0: invoices — foreign currency with SEK conversion
|
||||
{
|
||||
data: [
|
||||
{ subtotal: 1000, vat_amount: 250, moms_ruta: '05', subtotal_sek: 11000, vat_amount_sek: 2750 },
|
||||
// Invoice: C2611 2500, C3001 10000
|
||||
{ account_number: '2611', debit_amount: 0, credit_amount: 2500 },
|
||||
{ account_number: '3001', debit_amount: 0, credit_amount: 10000 },
|
||||
// Credit note reversal: D2611 625, D3001 2500
|
||||
{ account_number: '2611', debit_amount: 625, credit_amount: 0 },
|
||||
{ account_number: '3001', debit_amount: 2500, credit_amount: 0 },
|
||||
],
|
||||
error: null,
|
||||
},
|
||||
// 1: transactions
|
||||
{ data: [], error: null },
|
||||
// 2: receipts
|
||||
{ data: [], error: null },
|
||||
{ data: [{ source_type: 'invoice_created' }, { source_type: 'credit_note' }], error: null },
|
||||
]
|
||||
|
||||
const result = await calculateVatDeclaration('user-1', 'monthly', 2024, 1)
|
||||
|
||||
// Should use _sek values
|
||||
expect(result.rutor.ruta05).toBe(2750)
|
||||
expect(result.rutor.ruta10).toBe(11000)
|
||||
})
|
||||
|
||||
it('defaults to ruta05 when moms_ruta is null but VAT > 0', async () => {
|
||||
results = [
|
||||
// 0: invoices — null moms_ruta with VAT
|
||||
{
|
||||
data: [
|
||||
{ subtotal: 4000, vat_amount: 1000, moms_ruta: null, subtotal_sek: null, vat_amount_sek: null },
|
||||
],
|
||||
error: null,
|
||||
},
|
||||
// 1: transactions
|
||||
{ data: [], error: null },
|
||||
// 2: receipts
|
||||
{ data: [], error: null },
|
||||
]
|
||||
|
||||
const result = await calculateVatDeclaration('user-1', 'monthly', 2024, 1)
|
||||
|
||||
expect(result.rutor.ruta05).toBe(1000)
|
||||
expect(result.rutor.ruta10).toBe(4000)
|
||||
})
|
||||
|
||||
it('calculates input VAT from transaction categories', async () => {
|
||||
results = [
|
||||
// 0: invoices
|
||||
{ data: [], error: null },
|
||||
// 1: transactions — business expenses with categories
|
||||
{
|
||||
data: [
|
||||
// 25% category: expense_software, amount -1250 → VAT = 1250 * 0.25/1.25 = 250
|
||||
{ amount: -1250, amount_sek: null, is_business: true, category: 'expense_software' },
|
||||
// 12% category: expense_travel, amount -1120 → VAT = 1120 * 0.12/1.12 = 120
|
||||
{ amount: -1120, amount_sek: null, is_business: true, category: 'expense_travel' },
|
||||
],
|
||||
error: null,
|
||||
},
|
||||
// 2: receipts
|
||||
{ data: [], error: null },
|
||||
]
|
||||
|
||||
const result = await calculateVatDeclaration('user-1', 'monthly', 2024, 1)
|
||||
|
||||
// 250 + 120 = 370
|
||||
expect(result.rutor.ruta48).toBe(370)
|
||||
})
|
||||
|
||||
it('sums VAT from confirmed receipts', async () => {
|
||||
results = [
|
||||
// 0: invoices
|
||||
{ data: [], error: null },
|
||||
// 1: transactions
|
||||
{ data: [], error: null },
|
||||
// 2: receipts — confirmed with vat_amount
|
||||
{
|
||||
data: [
|
||||
{ status: 'confirmed', vat_amount: 59.8 },
|
||||
{ status: 'confirmed', vat_amount: 125 },
|
||||
],
|
||||
error: null,
|
||||
},
|
||||
]
|
||||
|
||||
const result = await calculateVatDeclaration('user-1', 'monthly', 2024, 1)
|
||||
|
||||
expect(result.rutor.ruta48).toBe(184.8)
|
||||
// Net: 2500 - 625 = 1875 output VAT, 10000 - 2500 = 7500 revenue
|
||||
expect(result.rutor.ruta05).toBe(1875)
|
||||
expect(result.rutor.ruta10).toBe(7500)
|
||||
expect(result.invoiceCount).toBe(2)
|
||||
})
|
||||
|
||||
it('calculates ruta49 as output minus input VAT', async () => {
|
||||
results = [
|
||||
// 0: invoices — 25% VAT
|
||||
{
|
||||
data: [
|
||||
{ subtotal: 10000, vat_amount: 2500, moms_ruta: '05', subtotal_sek: null, vat_amount_sek: null },
|
||||
],
|
||||
error: null,
|
||||
},
|
||||
// 1: transactions — 25% expense
|
||||
{
|
||||
data: [
|
||||
{ amount: -1250, amount_sek: null, is_business: true, category: 'expense_software' },
|
||||
],
|
||||
error: null,
|
||||
},
|
||||
// 2: receipts
|
||||
{
|
||||
data: [
|
||||
{ status: 'confirmed', vat_amount: 100 },
|
||||
{ account_number: '2611', debit_amount: 0, credit_amount: 2500 },
|
||||
{ account_number: '3001', debit_amount: 0, credit_amount: 10000 },
|
||||
{ account_number: '2641', debit_amount: 350, credit_amount: 0 },
|
||||
],
|
||||
error: null,
|
||||
},
|
||||
{ data: [], error: null },
|
||||
]
|
||||
|
||||
const result = await calculateVatDeclaration('user-1', 'monthly', 2024, 1)
|
||||
|
||||
// Output: 2500, Input: 250 + 100 = 350
|
||||
expect(result.rutor.ruta49).toBe(2150)
|
||||
})
|
||||
})
|
||||
|
||||
describe('calculateVatDeclarationFromTaxCodes', () => {
|
||||
it('maps journal lines to boxes via tax codes', async () => {
|
||||
results = [
|
||||
// 0: tax_codes
|
||||
{
|
||||
data: [
|
||||
{
|
||||
code: 'MP1',
|
||||
user_id: null,
|
||||
moms_basis_boxes: ['10'],
|
||||
moms_tax_boxes: ['05'],
|
||||
moms_input_boxes: [],
|
||||
},
|
||||
],
|
||||
error: null,
|
||||
},
|
||||
// 1: journal_entry_lines with tax_code
|
||||
{
|
||||
data: [
|
||||
{
|
||||
tax_code: 'MP1',
|
||||
debit_amount: 0,
|
||||
credit_amount: 2500,
|
||||
journal_entry_id: 'e1',
|
||||
journal_entries: { user_id: 'user-1', entry_date: '2024-01-15', status: 'posted' },
|
||||
},
|
||||
],
|
||||
error: null,
|
||||
},
|
||||
]
|
||||
|
||||
const result = await calculateVatDeclarationFromTaxCodes('user-1', 'monthly', 2024, 1)
|
||||
|
||||
expect(result.rutor.ruta05).toBe(2500)
|
||||
expect(result.rutor.ruta10).toBe(2500)
|
||||
expect(result.rutor.ruta48).toBe(350)
|
||||
expect(result.rutor.ruta49).toBe(2150) // 2500 - 350
|
||||
})
|
||||
|
||||
it('user tax codes override system codes', async () => {
|
||||
it('detects refund when input VAT exceeds output VAT', async () => {
|
||||
results = [
|
||||
// 0: tax_codes — system and user with same code
|
||||
{
|
||||
data: [
|
||||
{
|
||||
code: 'MP1',
|
||||
user_id: null,
|
||||
moms_basis_boxes: ['10'],
|
||||
moms_tax_boxes: ['05'],
|
||||
moms_input_boxes: [],
|
||||
},
|
||||
{
|
||||
code: 'MP1',
|
||||
user_id: 'user-1',
|
||||
moms_basis_boxes: ['11'],
|
||||
moms_tax_boxes: ['06'],
|
||||
moms_input_boxes: [],
|
||||
},
|
||||
],
|
||||
error: null,
|
||||
},
|
||||
// 1: journal_entry_lines
|
||||
{
|
||||
data: [
|
||||
{
|
||||
tax_code: 'MP1',
|
||||
debit_amount: 0,
|
||||
credit_amount: 600,
|
||||
journal_entry_id: 'e1',
|
||||
journal_entries: { user_id: 'user-1', entry_date: '2024-01-15', status: 'posted' },
|
||||
},
|
||||
{ account_number: '2611', debit_amount: 0, credit_amount: 500 },
|
||||
{ account_number: '2641', debit_amount: 3000, credit_amount: 0 },
|
||||
],
|
||||
error: null,
|
||||
},
|
||||
{ data: [], error: null },
|
||||
]
|
||||
|
||||
const result = await calculateVatDeclarationFromTaxCodes('user-1', 'monthly', 2024, 1)
|
||||
const result = await calculateVatDeclaration('user-1', 'monthly', 2024, 1)
|
||||
|
||||
// User override maps to ruta06/ruta11 instead of ruta05/ruta10
|
||||
expect(result.rutor.ruta05).toBe(0)
|
||||
expect(result.rutor.ruta06).toBe(600)
|
||||
expect(result.rutor.ruta11).toBe(600)
|
||||
expect(result.rutor.ruta49).toBe(-2500) // 500 - 3000
|
||||
})
|
||||
|
||||
it('returns all zeros when no lines have tax codes', async () => {
|
||||
it('accepts accountingMethod parameter for backward compatibility', async () => {
|
||||
results = [
|
||||
// 0: tax_codes
|
||||
{ data: [], error: null },
|
||||
// 1: journal_entry_lines — empty
|
||||
{ data: [], error: null },
|
||||
]
|
||||
|
||||
const result = await calculateVatDeclarationFromTaxCodes('user-1', 'monthly', 2024, 1)
|
||||
|
||||
expect(result.rutor.ruta05).toBe(0)
|
||||
expect(result.rutor.ruta48).toBe(0)
|
||||
// Should not throw — parameter accepted but not used
|
||||
const result = await calculateVatDeclaration('user-1', 'monthly', 2024, 1, 'cash')
|
||||
expect(result.rutor.ruta49).toBe(0)
|
||||
})
|
||||
|
||||
it('handles all three VAT rates in a single period', async () => {
|
||||
results = [
|
||||
{
|
||||
data: [
|
||||
// 25% rate: 10,000 revenue, 2,500 VAT
|
||||
{ account_number: '3001', debit_amount: 0, credit_amount: 10000 },
|
||||
{ account_number: '2611', debit_amount: 0, credit_amount: 2500 },
|
||||
// 12% rate: 5,000 revenue, 600 VAT
|
||||
{ account_number: '3002', debit_amount: 0, credit_amount: 5000 },
|
||||
{ account_number: '2621', debit_amount: 0, credit_amount: 600 },
|
||||
// 6% rate: 3,000 revenue, 180 VAT
|
||||
{ account_number: '3003', debit_amount: 0, credit_amount: 3000 },
|
||||
{ account_number: '2631', debit_amount: 0, credit_amount: 180 },
|
||||
// Input VAT from purchases
|
||||
{ account_number: '2641', debit_amount: 1000, credit_amount: 0 },
|
||||
],
|
||||
error: null,
|
||||
},
|
||||
{ data: [], error: null },
|
||||
]
|
||||
|
||||
const result = await calculateVatDeclaration('user-1', 'quarterly', 2024, 1)
|
||||
|
||||
expect(result.rutor.ruta05).toBe(2500)
|
||||
expect(result.rutor.ruta06).toBe(600)
|
||||
expect(result.rutor.ruta07).toBe(180)
|
||||
expect(result.rutor.ruta10).toBe(10000)
|
||||
expect(result.rutor.ruta11).toBe(5000)
|
||||
expect(result.rutor.ruta12).toBe(3000)
|
||||
expect(result.rutor.ruta48).toBe(1000)
|
||||
// Output: 2500 + 600 + 180 = 3280, Input: 1000 → Pay: 2280
|
||||
expect(result.rutor.ruta49).toBe(2280)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { createClient } from '@/lib/supabase/server'
|
||||
import { fetchAllRows } from '@/lib/supabase/fetch-all'
|
||||
|
||||
export interface GeneralLedgerLine {
|
||||
date: string
|
||||
@@ -41,7 +42,7 @@ export async function generateGeneralLedger(
|
||||
// Get fiscal period dates
|
||||
const { data: period } = await supabase
|
||||
.from('fiscal_periods')
|
||||
.select('start_date, end_date')
|
||||
.select('period_start, period_end')
|
||||
.eq('id', periodId)
|
||||
.eq('user_id', userId)
|
||||
.single()
|
||||
@@ -59,7 +60,7 @@ export async function generateGeneralLedger(
|
||||
.in('status', ['posted', 'reversed'])
|
||||
|
||||
if (!entries || entries.length === 0) {
|
||||
return { accounts: [], period: { start: period.start_date, end: period.end_date } }
|
||||
return { accounts: [], period: { start: period.period_start, end: period.period_end } }
|
||||
}
|
||||
|
||||
const entryIds = entries.map((e) => e.id)
|
||||
@@ -72,17 +73,20 @@ export async function generateGeneralLedger(
|
||||
.in('journal_entry_id', entryIds)
|
||||
|
||||
if (!lines) {
|
||||
return { accounts: [], period: { start: period.start_date, end: period.end_date } }
|
||||
return { accounts: [], period: { start: period.period_start, end: period.period_end } }
|
||||
}
|
||||
|
||||
// Fetch account names
|
||||
const { data: accounts } = await supabase
|
||||
.from('chart_of_accounts')
|
||||
.select('account_number, account_name')
|
||||
.eq('user_id', userId)
|
||||
const accounts = await fetchAllRows<{ account_number: string; account_name: string }>(({ from, to }) =>
|
||||
supabase
|
||||
.from('chart_of_accounts')
|
||||
.select('account_number, account_name')
|
||||
.eq('user_id', userId)
|
||||
.range(from, to)
|
||||
)
|
||||
|
||||
const accountNameMap = new Map<string, string>()
|
||||
for (const acc of accounts || []) {
|
||||
for (const acc of accounts) {
|
||||
accountNameMap.set(acc.account_number, acc.account_name)
|
||||
}
|
||||
|
||||
@@ -92,7 +96,7 @@ export async function generateGeneralLedger(
|
||||
.select('id')
|
||||
.eq('user_id', userId)
|
||||
.in('status', ['posted', 'reversed'])
|
||||
.lt('entry_date', period.start_date)
|
||||
.lt('entry_date', period.period_start)
|
||||
|
||||
const openingBalances = new Map<string, number>()
|
||||
|
||||
@@ -178,6 +182,6 @@ export async function generateGeneralLedger(
|
||||
|
||||
return {
|
||||
accounts: result,
|
||||
period: { start: period.start_date, end: period.end_date },
|
||||
period: { start: period.period_start, end: period.period_end },
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { createClient } from '@/lib/supabase/server'
|
||||
import { fetchAllRows } from '@/lib/supabase/fetch-all'
|
||||
|
||||
export interface JournalRegisterLine {
|
||||
account_number: string
|
||||
@@ -40,7 +41,7 @@ export async function generateJournalRegister(
|
||||
// Get fiscal period dates
|
||||
const { data: period } = await supabase
|
||||
.from('fiscal_periods')
|
||||
.select('start_date, end_date')
|
||||
.select('period_start, period_end')
|
||||
.eq('id', periodId)
|
||||
.eq('user_id', userId)
|
||||
.single()
|
||||
@@ -60,7 +61,7 @@ export async function generateJournalRegister(
|
||||
.order('voucher_number', { ascending: true })
|
||||
|
||||
if (!entries || entries.length === 0) {
|
||||
return { entries: [], total_entries: 0, total_debit: 0, total_credit: 0, period: { start: period.start_date, end: period.end_date } }
|
||||
return { entries: [], total_entries: 0, total_debit: 0, total_credit: 0, period: { start: period.period_start, end: period.period_end } }
|
||||
}
|
||||
|
||||
const entryIds = entries.map((e) => e.id)
|
||||
@@ -72,13 +73,16 @@ export async function generateJournalRegister(
|
||||
.in('journal_entry_id', entryIds)
|
||||
|
||||
// Fetch account names
|
||||
const { data: accounts } = await supabase
|
||||
.from('chart_of_accounts')
|
||||
.select('account_number, account_name')
|
||||
.eq('user_id', userId)
|
||||
const accounts = await fetchAllRows<{ account_number: string; account_name: string }>(({ from, to }) =>
|
||||
supabase
|
||||
.from('chart_of_accounts')
|
||||
.select('account_number, account_name')
|
||||
.eq('user_id', userId)
|
||||
.range(from, to)
|
||||
)
|
||||
|
||||
const accountNameMap = new Map<string, string>()
|
||||
for (const acc of accounts || []) {
|
||||
for (const acc of accounts) {
|
||||
accountNameMap.set(acc.account_number, acc.account_name)
|
||||
}
|
||||
|
||||
@@ -126,6 +130,6 @@ export async function generateJournalRegister(
|
||||
total_entries: result.length,
|
||||
total_debit: Math.round(grandTotalDebit * 100) / 100,
|
||||
total_credit: Math.round(grandTotalCredit * 100) / 100,
|
||||
period: { start: period.start_date, end: period.end_date },
|
||||
period: { start: period.period_start, end: period.period_end },
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,7 +32,7 @@ export async function generateMonthlyBreakdown(
|
||||
// Get the fiscal period date range
|
||||
const { data: period, error: periodError } = await supabase
|
||||
.from('fiscal_periods')
|
||||
.select('start_date, end_date')
|
||||
.select('period_start, period_end')
|
||||
.eq('id', fiscalPeriodId)
|
||||
.eq('user_id', userId)
|
||||
.single()
|
||||
@@ -67,8 +67,8 @@ export async function generateMonthlyBreakdown(
|
||||
const monthMap = new Map<number, { income: number; expenses: number }>()
|
||||
|
||||
// Initialize all months in the period range
|
||||
const startDate = new Date(period.start_date)
|
||||
const endDate = new Date(period.end_date)
|
||||
const startDate = new Date(period.period_start)
|
||||
const endDate = new Date(period.period_end)
|
||||
const startMonth = startDate.getMonth()
|
||||
const endMonth = endDate.getMonth() + (endDate.getFullYear() - startDate.getFullYear()) * 12
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { createClient } from '@/lib/supabase/server'
|
||||
import { fetchAllRows } from '@/lib/supabase/fetch-all'
|
||||
import type { SIEExportOptions, JournalEntry, JournalEntryLine, BASAccount } from '@/types'
|
||||
|
||||
/**
|
||||
@@ -29,12 +30,15 @@ export async function generateSIEExport(
|
||||
}
|
||||
|
||||
// Fetch all accounts
|
||||
const { data: accounts } = await supabase
|
||||
.from('chart_of_accounts')
|
||||
.select('*')
|
||||
.eq('user_id', userId)
|
||||
.eq('is_active', true)
|
||||
.order('account_number')
|
||||
const accounts = await fetchAllRows(({ from, to }) =>
|
||||
supabase
|
||||
.from('chart_of_accounts')
|
||||
.select('*')
|
||||
.eq('user_id', userId)
|
||||
.eq('is_active', true)
|
||||
.order('account_number')
|
||||
.range(from, to)
|
||||
)
|
||||
|
||||
// Fetch all posted journal entries with lines
|
||||
const { data: entries } = await supabase
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user