fix(ui): stop mislabeling unconverted FX amounts as kr in aggregates and toasts (#1182)

Fixes #1173. invoices.total_sek stays NULL when the Riksbanken rate
fetch fails at creation, and every `total_sek || total` fallback then
treated a raw foreign amount as kronor:

- lib/calendar/utils: new invoiceSekAmount() returns null for
  unconverted non-SEK invoices; period summaries and day totals skip
  them and PeriodSummary exposes unconvertedCount. PaymentSummaryCard
  shows a one-line note when invoices were excluded; CalendarDayView
  renders each invoice in its own currency instead.
- Deadlines page: the overdue attn sum now skips unconverted FX
  invoices and appends "(+N i utlandsk valuta)" instead of adding EUR
  into a kr total.
- Supplier-invoice payment toast formats the amount with the invoice's
  currency (key drops its hardcoded " kr" in both locales).
- AR aging drill-down row labels Betalt with the invoice currency,
  mirroring the outstanding cell.
- BankFileColumnMappingStep: comment pinning why SEK is safe there
  (generic-csv hardcodes it).

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Jakob Wennberg
2026-07-25 12:59:14 +02:00
committed by GitHub
co-authored by Claude Fable 5
parent 6d5a435ed9
commit aead2bc1d1
10 changed files with 133 additions and 21 deletions
+57
View File
@@ -0,0 +1,57 @@
import { describe, it, expect } from 'vitest'
import { invoiceSekAmount, calculatePeriodSummary, createPaymentCalendarDay } from '../utils'
import { makeInvoice } from '@/tests/helpers'
describe('invoiceSekAmount', () => {
it('prefers the stored SEK conversion', () => {
const inv = makeInvoice({ total: 100, total_sek: 1150, currency: 'EUR' })
expect(invoiceSekAmount(inv)).toBe(1150)
})
it('uses total directly for SEK invoices without a conversion', () => {
const inv = makeInvoice({ total: 100, total_sek: null, currency: 'SEK' })
expect(invoiceSekAmount(inv)).toBe(100)
})
it('returns null for a non-SEK invoice without a stored conversion', () => {
// total_sek stays NULL when the rate fetch failed at creation: the raw
// EUR total must never be treated as kronor.
const inv = makeInvoice({ total: 100, total_sek: null, currency: 'EUR' })
expect(invoiceSekAmount(inv)).toBeNull()
})
})
describe('calculatePeriodSummary', () => {
it('excludes unconverted foreign invoices from totals and counts them', () => {
const past = '2000-01-01'
const invoices = [
makeInvoice({ status: 'sent', due_date: past, total: 1000, total_sek: null, currency: 'SEK' }),
// Unconverted EUR invoice: counted, never summed as kr.
makeInvoice({ status: 'sent', due_date: past, total: 500, total_sek: null, currency: 'EUR' }),
makeInvoice({ status: 'paid', total: 200, total_sek: 2300, currency: 'EUR' }),
]
const summary = calculatePeriodSummary(invoices)
expect(summary.totalExpected).toBe(1000)
expect(summary.totalOverdue).toBe(1000)
expect(summary.totalPaid).toBe(2300)
expect(summary.pendingCount).toBe(2)
expect(summary.unconvertedCount).toBe(1)
})
})
describe('createPaymentCalendarDay', () => {
it('skips unconverted foreign invoices in the day total', () => {
const date = '2026-07-25'
const invoices = [
makeInvoice({ status: 'sent', due_date: date, total: 1000, total_sek: null, currency: 'SEK' }),
makeInvoice({ status: 'sent', due_date: date, total: 500, total_sek: null, currency: 'EUR' }),
]
const day = createPaymentCalendarDay(date, invoices)
expect(day.totalExpected).toBe(1000)
expect(day.invoices).toHaveLength(2)
})
})
+22 -6
View File
@@ -131,13 +131,24 @@ export function groupDeadlinesByDate(deadlines: Deadline[]): Map<string, Deadlin
return grouped
}
/**
* SEK value of an invoice for aggregation, or null when it cannot be known:
* total_sek stays NULL when the Riksbanken rate fetch failed at creation, and
* falling back to the raw foreign total would add EUR into SEK sums. Callers
* skip null and surface the count instead of silently mixing currencies.
*/
export function invoiceSekAmount(invoice: Invoice): number | null {
if (invoice.total_sek != null) return invoice.total_sek
return !invoice.currency || invoice.currency === 'SEK' ? invoice.total : null
}
// Create PaymentCalendarDay from invoices for a specific date
export function createPaymentCalendarDay(date: string, invoices: Invoice[]): PaymentCalendarDay {
const dayInvoices = invoices.filter(inv => inv.due_date === date)
const overdueCount = dayInvoices.filter(isInvoiceOverdue).length
const totalExpected = dayInvoices
.filter(inv => inv.status !== 'paid' && inv.status !== 'cancelled' && inv.status !== 'credited')
.reduce((sum, inv) => sum + (inv.total_sek || inv.total), 0)
.reduce((sum, inv) => sum + (invoiceSekAmount(inv) ?? 0), 0)
return {
date,
@@ -171,6 +182,8 @@ export interface PeriodSummary {
overdueCount: number
pendingCount: number
paidCount: number
/** Non-SEK invoices without a stored SEK conversion, excluded from the totals. */
unconvertedCount: number
}
export function calculatePeriodSummary(invoices: Invoice[]): PeriodSummary {
@@ -180,19 +193,21 @@ export function calculatePeriodSummary(invoices: Invoice[]): PeriodSummary {
let overdueCount = 0
let pendingCount = 0
let paidCount = 0
let unconvertedCount = 0
for (const invoice of invoices) {
const amount = invoice.total_sek || invoice.total
const amount = invoiceSekAmount(invoice)
if (amount == null) unconvertedCount++
if (invoice.status === 'paid') {
totalPaid += amount
totalPaid += amount ?? 0
paidCount++
} else if (invoice.status !== 'cancelled' && invoice.status !== 'credited') {
totalExpected += amount
totalExpected += amount ?? 0
pendingCount++
if (isInvoiceOverdue(invoice)) {
totalOverdue += amount
totalOverdue += amount ?? 0
overdueCount++
}
}
@@ -204,7 +219,8 @@ export function calculatePeriodSummary(invoices: Invoice[]): PeriodSummary {
totalPaid,
overdueCount,
pendingCount,
paidCount
paidCount,
unconvertedCount
}
}