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
+18 -9
View File
@@ -30,7 +30,7 @@ export default function DeadlinesPage() {
const { canWrite } = useCanWrite()
const [deadlines, setDeadlines] = useState<Deadline[]>([])
const [customers, setCustomers] = useState<{ id: string; name: string }[]>([])
const [overdueInvoices, setOverdueInvoices] = useState<{ count: number; total: number }>({ count: 0, total: 0 })
const [overdueInvoices, setOverdueInvoices] = useState<{ count: number; total: number; unconverted: number }>({ count: 0, total: 0, unconverted: 0 })
const [isLoading, setIsLoading] = useState(true)
const [isGenerating, setIsGenerating] = useState(false)
const [showForm, setShowForm] = useState(false)
@@ -68,10 +68,10 @@ export default function DeadlinesPage() {
.order('id', { ascending: true })
.range(from, to),
),
fetchAllRows<{ total_sek: number | null; total: number | null }>(({ from, to }) =>
fetchAllRows<{ total_sek: number | null; total: number | null; currency: string | null }>(({ from, to }) =>
supabase
.from('invoices')
.select('total_sek, total')
.select('total_sek, total, currency')
.eq('company_id', companyId)
.in('status', ['sent', 'unpaid'])
.lt('due_date', today)
@@ -80,14 +80,20 @@ export default function DeadlinesPage() {
),
])
const overdueTotal = overdueRows.reduce(
(sum, inv) => sum + (inv.total_sek || inv.total || 0),
0
)
// Non-SEK invoices without a stored SEK conversion (rate fetch failed at
// creation) are excluded from the SEK sum rather than mixed in raw, and
// surfaced as a count in the attn line instead.
let overdueTotal = 0
let unconvertedCount = 0
for (const inv of overdueRows) {
if (inv.total_sek != null) overdueTotal += inv.total_sek
else if (!inv.currency || inv.currency === 'SEK') overdueTotal += inv.total || 0
else unconvertedCount++
}
setDeadlines(deadlineRows)
setCustomers(customerRows)
setOverdueInvoices({ count: overdueRows.length, total: overdueTotal })
setOverdueInvoices({ count: overdueRows.length, total: overdueTotal, unconverted: unconvertedCount })
} catch {
toast({
title: t('load_failed_title'),
@@ -364,7 +370,10 @@ export default function DeadlinesPage() {
action={{ label: t('overdue_invoices_action'), href: '/invoices?status=unpaid' }}
>
{t('overdue_invoices', { count: overdueInvoices.count })} ·{' '}
{formatCurrency(overdueInvoices.total)}.
{formatCurrency(overdueInvoices.total)}
{overdueInvoices.unconverted > 0
? ` ${t('overdue_invoices_fx', { count: overdueInvoices.unconverted })}`
: ''}.
</AttnLine>
) : null}
@@ -340,7 +340,11 @@ export default function SupplierInvoiceDetailPage() {
} else {
toast({
title: result.status === 'paid' ? t('paid_title') : t('partial_payment_title'),
description: t('amount_registered_description', { amount: formatAmount(parseFloat(payAmount)) }),
// The paid amount is in the invoice's currency (the dialog's helper
// text says so): the toast must not relabel it as kr.
description: t('amount_registered_description', {
amount: formatCurrency(parseFloat(payAmount), invoice?.currency || 'SEK'),
}),
})
setIsPayDialogOpen(false)
setDuplicateCandidates(null)