fix(currency): transaction-date FX rate + mall bookings in SEK (#442) (#483)

* fix(currency): use transaction-date FX rate and post mall bookings in SEK (#442)

Three related defects in non-SEK transaction handling:

1. Ingest priced every transaction in a batch at today's rate because
   fetchMultipleRates() was called without a date. Switch to a per-
   (currency, date) fetch via fetchExchangeRate() + Promise.allSettled,
   so each row stores Riksbanken's rate as of its own transaction date.

2. buildMappingResultFromTemplate emitted VAT/reverse-charge/output-VAT
   lines from Math.abs(transaction.amount) — the source-currency amount.
   Mall bookings on USD/EUR transactions therefore posted the journal
   entry in the wrong currency. Resolve to SEK once via resolveSekAmount()
   and reuse for every line, matching what createTransactionJournalEntry
   already does for the settlement line.

3. The Granska bokföring modal and the journal-entry detail page lacked
   a clear audit trail. The verifikation preview now renders in SEK with
   a separate original-currency subtitle in the header; the detail page
   gains a dedicated "Valutaomräkning" card above Kontorader showing the
   rate and original total.

Also: replace truncate with break-all on the modal description so long
UUID-style strings (e.g. BALANCE_CASHBACK-…) wrap instead of forcing
horizontal overscroll.

Tests: 4 new booking-templates cases (USD 25%, EUR fallback via
exchange_rate, EU reverse-charge on USD, output VAT on USD income) and
2 new ingest cases (per-pair fetch dedup; no fetch for SEK rows).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* fix(currency): wire SEK conversion into SwipeCategorizationView (#442 follow-up)

Address Greptile review:

- SwipeCategorizationView rendered JournalEntryPreview without amountSek,
  so the swipe flow had the same defect QuickReviewDialog had — a USD
  transaction showed the verifikation in source currency labeled SEK.
  Now resolves SEK once via resolveSekAmount, passes it to the preview,
  and shows SEK as the headline with the original-currency subtitle
  (parallel to QuickReviewDialog).
- Drop the now-dead `currency` prop from JournalEntryPreviewProps and
  its remaining call site in QuickReviewDialog — the component
  hardcodes 'SEK' on display, so the prop was misleading.
- Correct the misleading inline comment in ingest.ts: network failures
  resolve inside fetchExchangeRate to getFallbackRate() (non-null),
  so the key only stays unset on empty-observation responses or
  outright rejections.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Jakob Wennberg
2026-05-14 15:00:12 +02:00
committed by GitHub
co-authored by Claude Opus 4.7
parent 4a6c473cc4
commit cad83d180d
8 changed files with 276 additions and 58 deletions
@@ -469,6 +469,74 @@ describe('buildMappingResultFromTemplate', () => {
expect(result.description).toBe('Drivmedel & Laddning: OKQ8 tankstation')
})
// Foreign-currency transactions: the mall must always emit SEK amounts
// (issue #442). Previously buildMappingResultFromTemplate used
// Math.abs(transaction.amount) — which is in the source currency — to
// compute VAT lines, producing a verifikation in mixed currencies.
it('emits SEK amounts when transaction currency is USD (issue #442)', () => {
const template = getTemplate('it_saas_subscription') // 25% input VAT
const tx = makeTransaction({
amount: -125, // -125 USD
currency: 'USD',
amount_sek: -1250, // pre-converted to SEK
exchange_rate: 10,
})
const result = buildMappingResultFromTemplate(template, tx, 'enskild_firma')
// VAT line debit must be 250 SEK (1250 * 0.25 / 1.25), not 25 USD
expect(result.vat_lines).toHaveLength(1)
expect(result.vat_lines[0].account_number).toBe('2641')
expect(result.vat_lines[0].debit_amount).toBe(250)
})
it('falls back to amount * exchange_rate when amount_sek is missing', () => {
const template = getTemplate('it_saas_subscription')
const tx = makeTransaction({
amount: -100,
currency: 'EUR',
amount_sek: null,
exchange_rate: 11.5,
})
const result = buildMappingResultFromTemplate(template, tx, 'enskild_firma')
// 100 * 11.5 = 1150 SEK; 1150 * 0.25 / 1.25 = 230 SEK
expect(result.vat_lines[0].debit_amount).toBe(230)
})
it('emits SEK amounts for EU reverse-charge on a non-SEK transaction', () => {
const template = getTemplate('it_saas_eu')
const tx = makeTransaction({
amount: -100,
currency: 'USD',
amount_sek: -1000,
exchange_rate: 10,
})
const result = buildMappingResultFromTemplate(template, tx, 'enskild_firma')
// Fiktiv-moms pair sized at 25% of 1000 SEK = 250, not 25 USD
expect(result.vat_lines).toHaveLength(4)
expect(result.vat_lines[0].debit_amount).toBe(250) // 2645
expect(result.vat_lines[1].credit_amount).toBe(250) // 2614
expect(result.vat_lines[2].debit_amount).toBe(1000) // 4535 basbelopp
expect(result.vat_lines[3].credit_amount).toBe(1000) // 4598
})
it('emits SEK amounts for output VAT on non-SEK income', () => {
const template = getTemplate('revenue_standard_25')
const tx = makeTransaction({
amount: 100,
currency: 'USD',
amount_sek: 1000,
exchange_rate: 10,
})
const result = buildMappingResultFromTemplate(template, tx, 'enskild_firma')
// Output VAT at 25% on 1000 SEK = 200, not 20 USD
expect(result.vat_lines).toHaveLength(1)
expect(result.vat_lines[0].account_number).toBe('2611')
expect(result.vat_lines[0].credit_amount).toBe(200)
})
})
// ============================================================
+13 -4
View File
@@ -13,6 +13,7 @@ import {
generateReverseChargeBasisLines,
generateInputVatLine,
} from './vat-entries'
import { resolveSekAmount } from './currency-utils'
// ============================================================
// Types
@@ -1577,6 +1578,17 @@ export function buildMappingResultFromTemplate(
if (template.credit_account_ab) creditAccount = template.credit_account_ab
}
// Always work in SEK. For non-SEK transactions, resolve the SEK-equivalent
// (via amount_sek or amount * exchange_rate); for SEK rows this is a no-op.
// Without this, VAT and reverse-charge lines would be emitted in the
// original currency and the resulting verifikation would mix currencies.
const absAmount = Math.abs(resolveSekAmount(
transaction.amount,
transaction.amount_sek,
transaction.currency,
transaction.exchange_rate
))
// Generate VAT lines
const vatLines: VatJournalLine[] = []
if (isBusiness && template.vat_treatment && template.deductibility !== 'non_deductible') {
@@ -1588,7 +1600,6 @@ export function buildMappingResultFromTemplate(
// basbelopp pair populates momsdeklaration rutor 20–24; without it
// Skatteverket rejects with FK004 ("ruta 30-32 utan motsvarande
// basbelopp i 20-24" — ML 13 kap kräver båda sidor).
const absAmount = Math.abs(transaction.amount)
const supplierType = template.reverse_charge_supplier_type ?? 'eu_business'
const isDomestic = supplierType === 'swedish_business'
const rcRate = 0.25 // fiktiv moms rate; current templates are 25%
@@ -1618,7 +1629,6 @@ export function buildMappingResultFromTemplate(
}
} else if (vatRate > 0 && isExpense) {
// Input VAT deduction
const absAmount = Math.abs(transaction.amount)
const vatLine = generateInputVatLine(absAmount, vatRate)
if (vatLine) {
vatLines.push({
@@ -1630,8 +1640,7 @@ export function buildMappingResultFromTemplate(
}
} else if (vatRate > 0 && !isExpense) {
// Output VAT (income)
const grossAmount = Math.abs(transaction.amount)
const vatAmount = Math.round((grossAmount * vatRate / (1 + vatRate)) * 100) / 100
const vatAmount = Math.round((absAmount * vatRate / (1 + vatRate)) * 100) / 100
let vatAccount: string
switch (template.vat_treatment) {
case 'standard_25': vatAccount = '2611'; break