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>
204 lines
7.4 KiB
TypeScript
204 lines
7.4 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 { generateJournalRegister } from '../journal-register'
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
resultIdx = 0
|
|
results = []
|
|
})
|
|
|
|
describe('generateJournalRegister', () => {
|
|
it('returns empty report when no fiscal period found', async () => {
|
|
results = [
|
|
{ data: null, error: null },
|
|
]
|
|
|
|
const report = await generateJournalRegister('user-1', 'period-1')
|
|
expect(report.entries).toEqual([])
|
|
expect(report.total_entries).toBe(0)
|
|
expect(report.period).toEqual({ start: '', end: '' })
|
|
})
|
|
|
|
it('returns empty report when no entries in period', async () => {
|
|
results = [
|
|
{ data: { period_start: '2024-01-01', period_end: '2024-12-31' }, error: null },
|
|
{ data: [], error: null },
|
|
]
|
|
|
|
const report = await generateJournalRegister('user-1', 'period-1')
|
|
expect(report.entries).toEqual([])
|
|
expect(report.total_entries).toBe(0)
|
|
expect(report.period).toEqual({ start: '2024-01-01', end: '2024-12-31' })
|
|
})
|
|
|
|
it('produces entries in registration order with correct totals', async () => {
|
|
results = [
|
|
// 0: fiscal_periods.single()
|
|
{ data: { period_start: '2024-01-01', period_end: '2024-12-31' }, error: null },
|
|
// 1: journal_entries (already ordered by series/number)
|
|
{
|
|
data: [
|
|
{ id: 'e1', entry_date: '2024-01-15', voucher_number: 1, voucher_series: 'A', description: 'Sale invoice', source_type: 'invoice', status: 'posted' },
|
|
{ id: 'e2', entry_date: '2024-02-01', voucher_number: 2, voucher_series: 'A', description: 'Payment', source_type: 'transaction', status: 'posted' },
|
|
],
|
|
error: null,
|
|
},
|
|
// 2: journal_entry_lines
|
|
{
|
|
data: [
|
|
{ account_number: '1510', debit_amount: 1250, credit_amount: 0, journal_entry_id: 'e1' },
|
|
{ account_number: '3001', debit_amount: 0, credit_amount: 1000, journal_entry_id: 'e1' },
|
|
{ account_number: '2611', debit_amount: 0, credit_amount: 250, journal_entry_id: 'e1' },
|
|
{ account_number: '1930', debit_amount: 1250, credit_amount: 0, journal_entry_id: 'e2' },
|
|
{ account_number: '1510', debit_amount: 0, credit_amount: 1250, journal_entry_id: 'e2' },
|
|
],
|
|
error: null,
|
|
},
|
|
// 3: chart_of_accounts
|
|
{
|
|
data: [
|
|
{ account_number: '1510', account_name: 'Kundfordringar' },
|
|
{ account_number: '1930', account_name: 'Företagskonto' },
|
|
{ account_number: '2611', account_name: 'Utgående moms 25%' },
|
|
{ account_number: '3001', account_name: 'Försäljning 25%' },
|
|
],
|
|
error: null,
|
|
},
|
|
]
|
|
|
|
const report = await generateJournalRegister('user-1', 'period-1')
|
|
|
|
expect(report.total_entries).toBe(2)
|
|
expect(report.entries[0].voucher_number).toBe(1)
|
|
expect(report.entries[1].voucher_number).toBe(2)
|
|
|
|
// Entry 1: sale invoice
|
|
expect(report.entries[0].total_debit).toBe(1250)
|
|
expect(report.entries[0].total_credit).toBe(1250)
|
|
expect(report.entries[0].lines).toHaveLength(3)
|
|
|
|
// Lines sorted by account number
|
|
expect(report.entries[0].lines[0].account_number).toBe('1510')
|
|
expect(report.entries[0].lines[1].account_number).toBe('2611')
|
|
expect(report.entries[0].lines[2].account_number).toBe('3001')
|
|
|
|
// Grand totals
|
|
expect(report.total_debit).toBe(2500)
|
|
expect(report.total_credit).toBe(2500)
|
|
})
|
|
|
|
it('includes reversed entries with correct status', async () => {
|
|
results = [
|
|
{ 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' },
|
|
{ id: 'e2', entry_date: '2024-01-16', voucher_number: 2, voucher_series: 'A', description: 'Reversal', source_type: 'manual', status: 'posted' },
|
|
],
|
|
error: null,
|
|
},
|
|
{
|
|
data: [
|
|
{ account_number: '1930', debit_amount: 500, credit_amount: 0, journal_entry_id: 'e1' },
|
|
{ account_number: '5410', debit_amount: 0, credit_amount: 500, journal_entry_id: 'e1' },
|
|
{ account_number: '5410', debit_amount: 500, credit_amount: 0, journal_entry_id: 'e2' },
|
|
{ account_number: '1930', debit_amount: 0, credit_amount: 500, journal_entry_id: 'e2' },
|
|
],
|
|
error: null,
|
|
},
|
|
{ data: [], error: null },
|
|
]
|
|
|
|
const report = await generateJournalRegister('user-1', 'period-1')
|
|
|
|
expect(report.entries[0].status).toBe('reversed')
|
|
expect(report.entries[1].status).toBe('posted')
|
|
expect(report.total_entries).toBe(2)
|
|
})
|
|
|
|
it('resolves account names from chart_of_accounts', async () => {
|
|
results = [
|
|
{ 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' },
|
|
],
|
|
error: null,
|
|
},
|
|
{
|
|
data: [
|
|
{ account_number: '1930', debit_amount: 100, credit_amount: 0, journal_entry_id: 'e1' },
|
|
{ account_number: '9999', debit_amount: 0, credit_amount: 100, journal_entry_id: 'e1' },
|
|
],
|
|
error: null,
|
|
},
|
|
{
|
|
data: [
|
|
{ account_number: '1930', account_name: 'Företagskonto' },
|
|
],
|
|
error: null,
|
|
},
|
|
]
|
|
|
|
const report = await generateJournalRegister('user-1', 'period-1')
|
|
|
|
const line1930 = report.entries[0].lines.find((l) => l.account_number === '1930')!
|
|
expect(line1930.account_name).toBe('Företagskonto')
|
|
|
|
// Unknown account gets fallback name
|
|
const line9999 = report.entries[0].lines.find((l) => l.account_number === '9999')!
|
|
expect(line9999.account_name).toBe('Konto 9999')
|
|
})
|
|
|
|
it('defaults voucher_series to A when null', async () => {
|
|
results = [
|
|
{ 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' },
|
|
],
|
|
error: null,
|
|
},
|
|
{
|
|
data: [
|
|
{ account_number: '1930', debit_amount: 100, credit_amount: 0, journal_entry_id: 'e1' },
|
|
{ account_number: '3001', debit_amount: 0, credit_amount: 100, journal_entry_id: 'e1' },
|
|
],
|
|
error: null,
|
|
},
|
|
{ data: [], error: null },
|
|
]
|
|
|
|
const report = await generateJournalRegister('user-1', 'period-1')
|
|
expect(report.entries[0].voucher_series).toBe('A')
|
|
})
|
|
})
|