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 <noreply@anthropic.com>
346 lines
12 KiB
TypeScript
346 lines
12 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
|
|
|
// ============================================================
|
|
// Mock — sequential result queue
|
|
// ============================================================
|
|
|
|
let resultIdx: number
|
|
let results: Array<{ data?: unknown; error?: unknown }>
|
|
|
|
function makeBuilder() {
|
|
const b: Record<string, unknown> = {}
|
|
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 })
|
|
b.then = (resolve: (v: unknown) => void) => resolve(results[resultIdx++] ?? { data: null, error: null })
|
|
return b
|
|
}
|
|
|
|
function makeClient() {
|
|
return {
|
|
from: vi.fn().mockImplementation(() => makeBuilder()),
|
|
}
|
|
}
|
|
|
|
vi.mock('@/lib/supabase/server', () => ({
|
|
createClient: vi.fn(async () => makeClient()),
|
|
}))
|
|
|
|
import { generateSIEExport } from '../sie-export'
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
resultIdx = 0
|
|
results = []
|
|
})
|
|
|
|
const baseOptions = {
|
|
fiscal_period_id: 'period-1',
|
|
company_name: 'Test AB',
|
|
org_number: '556677-8899',
|
|
program_name: 'ERPBase',
|
|
}
|
|
|
|
describe('generateSIEExport', () => {
|
|
it('throws when fiscal period not found', async () => {
|
|
results = [
|
|
// 0: fiscal_periods.single() → null
|
|
{ data: null, error: null },
|
|
]
|
|
|
|
await expect(generateSIEExport('user-1', baseOptions))
|
|
.rejects.toThrow('Fiscal period not found')
|
|
})
|
|
|
|
it('generates correct header format', async () => {
|
|
results = [
|
|
// 0: fiscal_periods
|
|
{ data: { id: 'period-1', period_start: '2024-01-01', period_end: '2024-12-31' }, error: null },
|
|
// 1: chart_of_accounts (empty)
|
|
{ data: [], error: null },
|
|
// 2: journal_entries (empty)
|
|
{ data: [], error: null },
|
|
// 3: cost_centers (empty)
|
|
{ data: [], error: null },
|
|
// 4: projects (empty)
|
|
{ data: [], error: null },
|
|
]
|
|
|
|
const output = await generateSIEExport('user-1', baseOptions)
|
|
const lines = output.split('\r\n')
|
|
|
|
expect(lines[0]).toBe('#FLAGGA 0')
|
|
expect(lines[1]).toBe('#FORMAT PC8')
|
|
expect(lines[2]).toBe('#SIETYP 4')
|
|
expect(lines[3]).toMatch(/^#PROGRAM "ERPBase" "1\.0"$/)
|
|
expect(lines[4]).toMatch(/^#GEN \d{8}$/)
|
|
expect(lines[5]).toBe('#ORGNR 556677-8899')
|
|
expect(lines[6]).toBe('#FNAMN "Test AB"')
|
|
expect(lines[7]).toBe('#RAR 0 20240101 20241231')
|
|
})
|
|
|
|
it('omits #ORGNR when org_number is null', async () => {
|
|
results = [
|
|
{ data: { id: 'period-1', period_start: '2024-01-01', period_end: '2024-12-31' }, error: null },
|
|
{ data: [], error: null },
|
|
{ data: [], error: null },
|
|
{ data: [], error: null },
|
|
{ data: [], error: null },
|
|
]
|
|
|
|
const output = await generateSIEExport('user-1', {
|
|
...baseOptions,
|
|
org_number: undefined,
|
|
})
|
|
|
|
expect(output).not.toContain('#ORGNR')
|
|
})
|
|
|
|
it('generates #KONTO and #SRU for accounts', async () => {
|
|
results = [
|
|
{ data: { id: 'period-1', period_start: '2024-01-01', period_end: '2024-12-31' }, error: null },
|
|
// 1: chart_of_accounts
|
|
{
|
|
data: [
|
|
{ account_number: '1930', account_name: 'Företagskonto', sru_code: '7301', is_active: true },
|
|
{ account_number: '3001', account_name: 'Försäljning', sru_code: null, is_active: true },
|
|
],
|
|
error: null,
|
|
},
|
|
// 2: journal_entries (empty)
|
|
{ data: [], error: null },
|
|
// 3: cost_centers
|
|
{ data: [], error: null },
|
|
// 4: projects
|
|
{ data: [], error: null },
|
|
]
|
|
|
|
const output = await generateSIEExport('user-1', baseOptions)
|
|
|
|
expect(output).toContain('#KONTO 1930 "Företagskonto"')
|
|
expect(output).toContain('#SRU 1930 7301')
|
|
expect(output).toContain('#KONTO 3001 "Försäljning"')
|
|
// No SRU for 3001 since sru_code is null
|
|
expect(output).not.toContain('#SRU 3001')
|
|
})
|
|
|
|
it('generates #VER and #TRANS for journal entries', async () => {
|
|
results = [
|
|
{ data: { id: 'period-1', period_start: '2024-01-01', period_end: '2024-12-31' }, error: null },
|
|
// 1: accounts
|
|
{ data: [], error: null },
|
|
// 2: journal_entries with lines
|
|
{
|
|
data: [
|
|
{
|
|
id: 'e1',
|
|
entry_date: '2024-03-15',
|
|
voucher_number: 1,
|
|
voucher_series: 'A',
|
|
description: 'Sale invoice',
|
|
status: 'posted',
|
|
lines: [
|
|
{ account_number: '1510', debit_amount: 1250, credit_amount: 0, line_description: null, cost_center: null, project: null },
|
|
{ account_number: '3001', debit_amount: 0, credit_amount: 1000, line_description: 'Revenue', cost_center: null, project: null },
|
|
{ account_number: '2611', debit_amount: 0, credit_amount: 250, line_description: null, cost_center: null, project: null },
|
|
],
|
|
},
|
|
],
|
|
error: null,
|
|
},
|
|
// 3: cost_centers
|
|
{ data: [], error: null },
|
|
// 4: projects
|
|
{ data: [], error: null },
|
|
]
|
|
|
|
const output = await generateSIEExport('user-1', baseOptions)
|
|
|
|
expect(output).toContain('#VER "A" 1 20240315 "Sale invoice"')
|
|
expect(output).toContain('{')
|
|
expect(output).toContain('\t#TRANS 1510 {} 1250.00 20240315')
|
|
expect(output).toContain('\t#TRANS 3001 {} -1000.00 20240315 "Revenue"')
|
|
expect(output).toContain('\t#TRANS 2611 {} -250.00 20240315')
|
|
expect(output).toContain('}')
|
|
})
|
|
|
|
it('generates #DIM and #OBJEKT for dimensions', async () => {
|
|
results = [
|
|
{ data: { id: 'period-1', period_start: '2024-01-01', period_end: '2024-12-31' }, error: null },
|
|
{ data: [], error: null },
|
|
{ data: [], error: null },
|
|
// 3: cost_centers
|
|
{
|
|
data: [
|
|
{ code: 'CC1', name: 'Avdelning 1', is_active: true },
|
|
],
|
|
error: null,
|
|
},
|
|
// 4: projects
|
|
{
|
|
data: [
|
|
{ code: 'P001', name: 'Projekt Alpha', is_active: true },
|
|
],
|
|
error: null,
|
|
},
|
|
]
|
|
|
|
const output = await generateSIEExport('user-1', baseOptions)
|
|
|
|
expect(output).toContain('#DIM 1 "Kostnadsställe"')
|
|
expect(output).toContain('#DIM 6 "Projekt"')
|
|
expect(output).toContain('#OBJEKT 1 "CC1" "Avdelning 1"')
|
|
expect(output).toContain('#OBJEKT 6 "P001" "Projekt Alpha"')
|
|
})
|
|
|
|
it('includes dimension objects in #TRANS lines', async () => {
|
|
results = [
|
|
{ data: { id: 'period-1', period_start: '2024-01-01', period_end: '2024-12-31' }, error: null },
|
|
{ data: [], error: null },
|
|
{
|
|
data: [
|
|
{
|
|
id: 'e1',
|
|
entry_date: '2024-03-15',
|
|
voucher_number: 1,
|
|
voucher_series: 'A',
|
|
description: 'With dimensions',
|
|
status: 'posted',
|
|
lines: [
|
|
{ account_number: '5010', debit_amount: 8000, credit_amount: 0, line_description: null, cost_center: 'CC1', project: 'P001' },
|
|
{ account_number: '1930', debit_amount: 0, credit_amount: 8000, line_description: null, cost_center: null, project: null },
|
|
],
|
|
},
|
|
],
|
|
error: null,
|
|
},
|
|
{ data: [{ code: 'CC1', name: 'Avdelning 1', is_active: true }], error: null },
|
|
{ data: [{ code: 'P001', name: 'Projekt Alpha', is_active: true }], error: null },
|
|
]
|
|
|
|
const output = await generateSIEExport('user-1', baseOptions)
|
|
|
|
expect(output).toContain('\t#TRANS 5010 {1 "CC1" 6 "P001"} 8000.00 20240315')
|
|
expect(output).toContain('\t#TRANS 1930 {} -8000.00 20240315')
|
|
})
|
|
|
|
it('generates #UB for class 1-2 and #RES for class 3-8', async () => {
|
|
results = [
|
|
{ data: { id: 'period-1', period_start: '2024-01-01', period_end: '2024-12-31' }, error: null },
|
|
{ data: [], error: null },
|
|
{
|
|
data: [
|
|
{
|
|
id: 'e1',
|
|
entry_date: '2024-01-15',
|
|
voucher_number: 1,
|
|
voucher_series: 'A',
|
|
description: 'Sale',
|
|
status: 'posted',
|
|
lines: [
|
|
{ account_number: '1510', debit_amount: 1250, credit_amount: 0, line_description: null, cost_center: null, project: null },
|
|
{ account_number: '3001', debit_amount: 0, credit_amount: 1000, line_description: null, cost_center: null, project: null },
|
|
{ account_number: '2611', debit_amount: 0, credit_amount: 250, line_description: null, cost_center: null, project: null },
|
|
],
|
|
},
|
|
],
|
|
error: null,
|
|
},
|
|
{ data: [], error: null },
|
|
{ data: [], error: null },
|
|
]
|
|
|
|
const output = await generateSIEExport('user-1', baseOptions)
|
|
|
|
// Account 1510 (class 1) → #UB, balance = 1250 - 0 = 1250
|
|
expect(output).toContain('#UB 0 1510 1250.00')
|
|
// Account 2611 (class 2) → #UB, balance = 0 - 250 = -250
|
|
expect(output).toContain('#UB 0 2611 -250.00')
|
|
// Account 3001 (class 3) → #RES, balance = 0 - 1000 = -1000
|
|
expect(output).toContain('#RES 0 3001 -1000.00')
|
|
})
|
|
|
|
it('escapes quotes in descriptions', async () => {
|
|
results = [
|
|
{ data: { id: 'period-1', period_start: '2024-01-01', period_end: '2024-12-31' }, error: null },
|
|
{ data: [], error: null },
|
|
{
|
|
data: [
|
|
{
|
|
id: 'e1',
|
|
entry_date: '2024-01-15',
|
|
voucher_number: 1,
|
|
voucher_series: 'A',
|
|
description: 'Invoice for "consulting"',
|
|
status: 'posted',
|
|
lines: [
|
|
{ account_number: '1930', debit_amount: 100, credit_amount: 0, line_description: null, cost_center: null, project: null },
|
|
{ account_number: '3001', debit_amount: 0, credit_amount: 100, line_description: null, cost_center: null, project: null },
|
|
],
|
|
},
|
|
],
|
|
error: null,
|
|
},
|
|
{ data: [], error: null },
|
|
{ data: [], error: null },
|
|
]
|
|
|
|
const output = await generateSIEExport('user-1', baseOptions)
|
|
|
|
expect(output).toContain('#VER "A" 1 20240115 "Invoice for \\"consulting\\""')
|
|
})
|
|
|
|
it('uses \\r\\n line endings', async () => {
|
|
results = [
|
|
{ data: { id: 'period-1', period_start: '2024-01-01', period_end: '2024-12-31' }, error: null },
|
|
{ data: [], error: null },
|
|
{ data: [], error: null },
|
|
{ data: [], error: null },
|
|
{ data: [], error: null },
|
|
]
|
|
|
|
const output = await generateSIEExport('user-1', baseOptions)
|
|
|
|
// Every line should end with \r\n
|
|
expect(output).toContain('\r\n')
|
|
// Should not have bare \n (that isn't preceded by \r)
|
|
const lines = output.split('\r\n')
|
|
for (const line of lines.slice(0, -1)) {
|
|
expect(line).not.toContain('\n')
|
|
}
|
|
// File should end with \r\n
|
|
expect(output.endsWith('\r\n')).toBe(true)
|
|
})
|
|
|
|
it('produces no #VER lines when no entries exist', async () => {
|
|
results = [
|
|
{ data: { id: 'period-1', period_start: '2024-01-01', period_end: '2024-12-31' }, error: null },
|
|
{ data: [], error: null },
|
|
{ data: [], error: null },
|
|
{ data: [], error: null },
|
|
{ data: [], error: null },
|
|
]
|
|
|
|
const output = await generateSIEExport('user-1', baseOptions)
|
|
|
|
expect(output).not.toContain('#VER')
|
|
expect(output).not.toContain('#TRANS')
|
|
})
|
|
|
|
it('produces no #DIM lines when no dimensions exist', async () => {
|
|
results = [
|
|
{ data: { id: 'period-1', period_start: '2024-01-01', period_end: '2024-12-31' }, error: null },
|
|
{ data: [], error: null },
|
|
{ data: [], error: null },
|
|
{ data: [], error: null },
|
|
{ data: [], error: null },
|
|
]
|
|
|
|
const output = await generateSIEExport('user-1', baseOptions)
|
|
|
|
expect(output).not.toContain('#DIM')
|
|
expect(output).not.toContain('#OBJEKT')
|
|
})
|
|
})
|