* fix: include reversed entries in all reports (general ledger, trial balance, VAT, SIE, NE, INK2) Reversed entries (storno) must appear alongside their original posted entries in reports for a complete audit trail. Previously, filtering by status='posted' excluded them, causing discrepancies when corrections had been made. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * feat: semi-manual invoice payment booking with editable journal lines When marking an invoice as paid, users now see a dialog where they can: - Choose which bank/cash account the payment goes to (1910, 1920, 1930, etc.) - Review and edit the proposed journal entry lines before committing - The happy path remains fast — lines are pre-filled correctly Implementation: - Pure proposePaymentLines() function for line computation (accrual + cash) - PaymentBookingDialog with AccountCombobox, balance validation, date picker - API accepts optional custom lines, falls back to auto-generation without them - 18 tests (8 unit + 10 API) all passing Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: address Greptile review — validation fallback, balance check, error handling - P1: Return 400 on invalid body instead of silently falling back to auto-generated lines (split JSON parse from schema validation) - P1: Add server-side balance check for custom lines before committing (debit must equal credit, totalDebit > 0) - P2: Wrap PaymentBookingDialog init() in try/catch with toast on failure and auto-close instead of silent empty state - Add 2 new tests: unbalanced lines → 400, invalid schema → 400 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: OAuth callback redirect for local dev and timeout resilience - Pass redirectUri dynamically from NEXT_PUBLIC_APP_URL so OAuth callbacks work on localhost (not just production) - Encode consentId/provider in OAuth state (base64url JSON) so the callback doesn't depend on session storage - Add skipAuth flag to extension API routes for OAuth callbacks (external provider redirects have no user session cookie) - Wrap AbortError in descriptive timeout messages in arcim-client - Make preview endpoint resilient to partial failures (company info and SIE fetch are individually non-blocking) - Simplify login page (remove unused magic link auth mode) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: create journal entry before marking invoice as paid Move journal entry creation before the invoice status update so that if accounting fails, the invoice is not permanently marked paid without a corresponding entry. Previously the error was silently swallowed. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: update mark-paid tests for journal-first ordering Reorder mock queue to match new flow (settings before update), update failure test to expect 500 instead of silent success, add try-catch with proper error response in route handler. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * feat: add reverse charge VAT (ruta 20-32) and improve mobile UX across dashboard Add full reverse charge (omvänd skattskyldighet) support to the VAT declaration: - Map accounts 2614/2624/2634 to ruta 30/31/32 for self-assessed output VAT - Calculate purchase bases (ruta 20-24) from supplier invoices by supplier type - Include ruta 30-32 in ruta 49 formula and totalOutputVat summary - Display reverse charge section in reports UI and composition chart - Add comprehensive test coverage for all reverse charge scenarios Improve mobile UX across the app: - Convert nav drawer to bottom sheet with drag handle and safe area padding - Add mobile card layout for PaymentBookingDialog journal lines - Replace settings tab pills with dropdown selector on mobile - Make wizard step indicators responsive (collapsed on mobile) - Ensure all dialog footers stack buttons full-width on mobile - Add 44px minimum touch targets throughout - Make onboarding buttons full-width on mobile Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: address Greptile review — indentation, query efficiency, tab dedup - Fix misleading try-block indentation in mark-paid route - Filter reversed entries at DB level (.eq('status', 'posted')) instead of fetching then discarding in memory - Extract shared settingsTabs array so mobile Select and desktop TabsList stay in sync automatically Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * feat: add resilience fallbacks, Arcim retry logic, and client tests Add FallbackPrompt component and integrate it across banking and migration error states so users always have a manual import escape hatch. Add retry with exponential backoff to Arcim API client for transient failures (429, 502, 503, 504) and timeouts. Expand import page deep-linking with ?mode= parameter. Add persistent error banner on settings page for bank connection failures. Include 18 new tests for the Arcim client covering retry, backoff, pagination, timeout, env validation, and singleton resource unwrapping. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: address Greptile review — setActiveTab, test cleanup, redundant clearTimeout - Add missing setActiveTab('banking') when handling bank_error query param so the error banner is actually visible (P1) - Guard env-var cleanup with try/finally in arcim-client tests to prevent state leakage on assertion failure (P2) - Only mock retry-range setTimeout delays in backoff test, letting AbortController timers pass through real setTimeout (P2) - Remove redundant clearTimeout in catch block — finally handles it (P2) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * feat: add BFL-compliant counterparty names to journal descriptions and cancelled entry status Journal descriptions now include customer/supplier names for traceability (e.g. "Kundfaktura 1001, Foretag AB"). Failed draft entries are marked as 'cancelled' instead of deleted, respecting immutability constraints. Includes DB migration for the new journal_entries status value. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: address Greptile review — Swedish typos, missing source type, trigger and reversal cleanup - Fix Swedish spelling: leverantor → leverantör in all supplier description prefixes - Add supplier_credit_note to supplierSourceTypes in VAT declaration so credit notes correctly reduce reverse-charge bases (ruta 20–24) - Mark orphaned concurrent reversals as cancelled instead of attempting deletion that the immutability trigger blocks - Allow posted → cancelled transition in trigger for orphaned reversal cleanup - Restrict cancelled entry line trigger to DELETE-only (block INSERT/UPDATE) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: use main's Step3TaxRegistration (onboarding restructured in PR #54) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * chore: retrigger Greptile review Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
660 lines
22 KiB
TypeScript
660 lines
22 KiB
TypeScript
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 (_supabase: unknown, _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,
|
|
createInvoicePaymentJournalEntry,
|
|
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', () => {
|
|
expect(getRevenueAccount('standard_25')).toBe('3001')
|
|
})
|
|
|
|
it('reduced_12 returns 3002', () => {
|
|
expect(getRevenueAccount('reduced_12')).toBe('3002')
|
|
})
|
|
|
|
it('reduced_6 returns 3003', () => {
|
|
expect(getRevenueAccount('reduced_6')).toBe('3003')
|
|
})
|
|
|
|
it('reverse_charge returns 3308', () => {
|
|
expect(getRevenueAccount('reverse_charge')).toBe('3308')
|
|
})
|
|
|
|
it('export returns 3305', () => {
|
|
expect(getRevenueAccount('export')).toBe('3305')
|
|
})
|
|
|
|
it('exempt defaults to 3100 for enskild_firma', () => {
|
|
expect(getRevenueAccount('exempt')).toBe('3100')
|
|
expect(getRevenueAccount('exempt', 'enskild_firma')).toBe('3100')
|
|
})
|
|
|
|
it('exempt returns 3004 for aktiebolag', () => {
|
|
expect(getRevenueAccount('exempt', 'aktiebolag')).toBe('3004')
|
|
})
|
|
|
|
it('entityType does not affect non-exempt treatments', () => {
|
|
expect(getRevenueAccount('standard_25', 'aktiebolag')).toBe('3001')
|
|
expect(getRevenueAccount('reduced_12', 'aktiebolag')).toBe('3002')
|
|
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(null as never, 'user-1', invoice)
|
|
|
|
expect(mockedCreateEntry).toHaveBeenCalledOnce()
|
|
const input = mockedCreateEntry.mock.calls[0][2]
|
|
|
|
// 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(null as never, 'user-1', invoice)
|
|
|
|
expect(mockedCreateEntry).toHaveBeenCalledOnce()
|
|
const input = mockedCreateEntry.mock.calls[0][2]
|
|
|
|
// 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(null as never, 'user-1', invoice)
|
|
|
|
expect(mockedCreateEntry).toHaveBeenCalledOnce()
|
|
const input = mockedCreateEntry.mock.calls[0][2]
|
|
|
|
// 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(null as never, 'user-1', invoice)
|
|
|
|
const input = mockedCreateEntry.mock.calls[0][2]
|
|
|
|
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(null as never, 'user-1', creditNote)
|
|
|
|
expect(mockedCreateEntry).toHaveBeenCalledOnce()
|
|
const input = mockedCreateEntry.mock.calls[0][2]
|
|
|
|
// 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(null as never, 'user-1', invoice, '2024-07-01')
|
|
|
|
expect(mockedCreateEntry).toHaveBeenCalledOnce()
|
|
const input = mockedCreateEntry.mock.calls[0][2]
|
|
|
|
// 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)
|
|
})
|
|
})
|
|
|
|
describe('createInvoiceJournalEntry — EUR foreign currency', () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
})
|
|
|
|
it('EUR invoice converts amounts to SEK using exchange rate', async () => {
|
|
// EUR 1,000 + EUR 250 VAT = EUR 1,250 total, rate 11.5
|
|
const invoice = makeInvoice({
|
|
currency: 'EUR',
|
|
exchange_rate: 11.5,
|
|
subtotal: 1000,
|
|
subtotal_sek: 11500,
|
|
vat_amount: 250,
|
|
vat_amount_sek: 2875,
|
|
total: 1250,
|
|
total_sek: 14375,
|
|
vat_treatment: 'standard_25',
|
|
items: [
|
|
makeItem({ line_total: 1000, vat_rate: 25, vat_amount: 250 }),
|
|
],
|
|
})
|
|
|
|
await createInvoiceJournalEntry(null as never, 'user-1', invoice)
|
|
|
|
expect(mockedCreateEntry).toHaveBeenCalledOnce()
|
|
const input = mockedCreateEntry.mock.calls[0][2]
|
|
|
|
// All amounts should be in SEK
|
|
const debit1510 = input.lines.find((l) => l.account_number === '1510')
|
|
expect(debit1510?.debit_amount).toBe(14375) // 1000*11.5 + 250*11.5 = 14375
|
|
|
|
const credit3001 = input.lines.find((l) => l.account_number === '3001')
|
|
expect(credit3001?.credit_amount).toBe(11500) // 1000 * 11.5
|
|
|
|
const credit2611 = input.lines.find((l) => l.account_number === '2611')
|
|
expect(credit2611?.credit_amount).toBe(2875) // 250 * 11.5
|
|
|
|
// 1510 line should have currency metadata
|
|
expect(debit1510?.currency).toBe('EUR')
|
|
expect(debit1510?.amount_in_currency).toBe(1250)
|
|
expect(debit1510?.exchange_rate).toBe(11.5)
|
|
|
|
// 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)
|
|
})
|
|
|
|
it('EUR invoice uses total_sek when available', async () => {
|
|
// Edge case: total_sek differs slightly from computed (e.g. pre-computed at different rate)
|
|
const invoice = makeInvoice({
|
|
currency: 'EUR',
|
|
exchange_rate: 11.5,
|
|
subtotal: 1000,
|
|
subtotal_sek: null,
|
|
vat_amount: 0,
|
|
vat_amount_sek: null,
|
|
total: 1000,
|
|
total_sek: null,
|
|
vat_treatment: 'export',
|
|
items: [
|
|
makeItem({ line_total: 1000, vat_rate: 0, vat_amount: 0 }),
|
|
],
|
|
})
|
|
|
|
await createInvoiceJournalEntry(null as never, 'user-1', invoice)
|
|
|
|
const input = mockedCreateEntry.mock.calls[0][2]
|
|
|
|
// Revenue should be computed via exchange rate
|
|
const credit3305 = input.lines.find((l) => l.account_number === '3305')
|
|
expect(credit3305?.credit_amount).toBe(11500)
|
|
|
|
const debit1510 = input.lines.find((l) => l.account_number === '1510')
|
|
expect(debit1510?.debit_amount).toBe(11500)
|
|
})
|
|
|
|
it('SEK invoice still works unchanged (backward compatibility)', async () => {
|
|
const invoice = makeInvoice({
|
|
subtotal: 800,
|
|
vat_amount: 200,
|
|
total: 1000,
|
|
vat_treatment: 'standard_25',
|
|
items: [
|
|
makeItem({ line_total: 800, vat_rate: 25, vat_amount: 200 }),
|
|
],
|
|
})
|
|
|
|
await createInvoiceJournalEntry(null as never, 'user-1', invoice)
|
|
|
|
const input = mockedCreateEntry.mock.calls[0][2]
|
|
const debit1510 = input.lines.find((l) => l.account_number === '1510')
|
|
expect(debit1510?.debit_amount).toBe(1000)
|
|
|
|
// No currency metadata for SEK
|
|
expect(debit1510?.currency).toBeUndefined()
|
|
expect(debit1510?.amount_in_currency).toBeUndefined()
|
|
})
|
|
})
|
|
|
|
describe('BFL-compliant descriptions with counterparty names', () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
})
|
|
|
|
it('createInvoiceJournalEntry includes customer name in description', async () => {
|
|
const invoice = makeInvoice({
|
|
items: [makeItem()],
|
|
})
|
|
|
|
await createInvoiceJournalEntry(null as never, 'user-1', invoice, 'enskild_firma', 'Foretag AB')
|
|
|
|
const input = mockedCreateEntry.mock.calls[0][2]
|
|
expect(input.description).toBe('Kundfaktura 1001, Foretag AB')
|
|
})
|
|
|
|
it('createInvoiceJournalEntry falls back without customer name', async () => {
|
|
const invoice = makeInvoice({
|
|
items: [makeItem()],
|
|
})
|
|
|
|
await createInvoiceJournalEntry(null as never, 'user-1', invoice, 'enskild_firma')
|
|
|
|
const input = mockedCreateEntry.mock.calls[0][2]
|
|
expect(input.description).toBe('Kundfaktura 1001')
|
|
})
|
|
|
|
it('createInvoicePaymentJournalEntry includes customer name', async () => {
|
|
const invoice = makeInvoice({ total: 1250 })
|
|
|
|
await createInvoicePaymentJournalEntry(null as never, 'user-1', invoice, '2024-07-15', undefined, 'Foretag AB')
|
|
|
|
const input = mockedCreateEntry.mock.calls[0][2]
|
|
expect(input.description).toBe('Inbetalning kundfaktura 1001, Foretag AB')
|
|
})
|
|
|
|
it('createInvoicePaymentJournalEntry falls back without customer name', async () => {
|
|
const invoice = makeInvoice({ total: 1250 })
|
|
|
|
await createInvoicePaymentJournalEntry(null as never, 'user-1', invoice, '2024-07-15')
|
|
|
|
const input = mockedCreateEntry.mock.calls[0][2]
|
|
expect(input.description).toBe('Inbetalning kundfaktura 1001')
|
|
})
|
|
|
|
it('createCreditNoteJournalEntry includes customer name', async () => {
|
|
const creditNote = makeInvoice({
|
|
invoice_number: 'KR-1001',
|
|
subtotal: -1000,
|
|
vat_amount: -250,
|
|
total: -1250,
|
|
items: [makeItem({ quantity: -1, line_total: -1000, vat_amount: -250 })],
|
|
})
|
|
|
|
await createCreditNoteJournalEntry(null as never, 'user-1', creditNote, 'enskild_firma', 'Foretag AB')
|
|
|
|
const input = mockedCreateEntry.mock.calls[0][2]
|
|
expect(input.description).toBe('Kreditfaktura KR-1001, Foretag AB')
|
|
})
|
|
|
|
it('createInvoiceCashEntry includes customer name', async () => {
|
|
const invoice = makeInvoice({
|
|
items: [makeItem()],
|
|
})
|
|
|
|
await createInvoiceCashEntry(null as never, 'user-1', invoice, '2024-07-01', 'enskild_firma', 'Foretag AB')
|
|
|
|
const input = mockedCreateEntry.mock.calls[0][2]
|
|
expect(input.description).toBe('Kontantbetalning kundfaktura 1001, Foretag AB')
|
|
})
|
|
|
|
it('createInvoiceCashEntry falls back without customer name', async () => {
|
|
const invoice = makeInvoice({
|
|
items: [makeItem()],
|
|
})
|
|
|
|
await createInvoiceCashEntry(null as never, 'user-1', invoice, '2024-07-01', 'enskild_firma')
|
|
|
|
const input = mockedCreateEntry.mock.calls[0][2]
|
|
expect(input.description).toBe('Kontantbetalning kundfaktura 1001')
|
|
})
|
|
})
|
|
|
|
describe('createInvoicePaymentJournalEntry — exchange rate difference', () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
})
|
|
|
|
it('SEK payment creates simple 2-line entry', async () => {
|
|
const invoice = makeInvoice({ total: 1250 })
|
|
|
|
await createInvoicePaymentJournalEntry(null as never, 'user-1', invoice, '2024-07-15')
|
|
|
|
const input = mockedCreateEntry.mock.calls[0][2]
|
|
expect(input.lines).toHaveLength(2)
|
|
|
|
const debit1930 = input.lines.find((l) => l.account_number === '1930')
|
|
expect(debit1930?.debit_amount).toBe(1250)
|
|
|
|
const credit1510 = input.lines.find((l) => l.account_number === '1510')
|
|
expect(credit1510?.credit_amount).toBe(1250)
|
|
})
|
|
|
|
it('EUR payment with positive exchange rate difference (gain) creates 3 lines', async () => {
|
|
const invoice = makeInvoice({
|
|
currency: 'EUR',
|
|
exchange_rate: 11.5,
|
|
total: 1000,
|
|
total_sek: 11500,
|
|
})
|
|
|
|
// Gain of 200 SEK (received more than booked)
|
|
await createInvoicePaymentJournalEntry(null as never, 'user-1', invoice, '2024-07-15', 200)
|
|
|
|
const input = mockedCreateEntry.mock.calls[0][2]
|
|
expect(input.lines).toHaveLength(3)
|
|
|
|
// Debit 1930: actual SEK received = 11500 + 200 = 11700
|
|
const debit1930 = input.lines.find((l) => l.account_number === '1930')
|
|
expect(debit1930?.debit_amount).toBe(11700)
|
|
|
|
// Credit 1510: original booked amount
|
|
const credit1510 = input.lines.find((l) => l.account_number === '1510')
|
|
expect(credit1510?.credit_amount).toBe(11500)
|
|
|
|
// Credit 3960: exchange rate gain
|
|
const credit3960 = input.lines.find((l) => l.account_number === '3960')
|
|
expect(credit3960?.credit_amount).toBe(200)
|
|
|
|
// 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)
|
|
})
|
|
|
|
it('EUR payment with negative exchange rate difference (loss) creates 3 lines', async () => {
|
|
const invoice = makeInvoice({
|
|
currency: 'EUR',
|
|
exchange_rate: 11.5,
|
|
total: 1000,
|
|
total_sek: 11500,
|
|
})
|
|
|
|
// Loss of 300 SEK (received less than booked)
|
|
await createInvoicePaymentJournalEntry(null as never, 'user-1', invoice, '2024-07-15', -300)
|
|
|
|
const input = mockedCreateEntry.mock.calls[0][2]
|
|
expect(input.lines).toHaveLength(3)
|
|
|
|
// Debit 1930: actual SEK received = 11500 + (-300) = 11200
|
|
const debit1930 = input.lines.find((l) => l.account_number === '1930')
|
|
expect(debit1930?.debit_amount).toBe(11200)
|
|
|
|
// Credit 1510: original booked amount
|
|
const credit1510 = input.lines.find((l) => l.account_number === '1510')
|
|
expect(credit1510?.credit_amount).toBe(11500)
|
|
|
|
// Debit 7960: exchange rate loss
|
|
const debit7960 = input.lines.find((l) => l.account_number === '7960')
|
|
expect(debit7960?.debit_amount).toBe(300)
|
|
|
|
// 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)
|
|
})
|
|
})
|