fix(import): show "created on import" as the mapping target for source accounts the chart lacks (#2342)

The guided Fortnox import self-mapped a source account that exists in
neither the company chart nor BAS (4599) and then rendered its Malkonto
select blank, because the dropdown only knew chart + BAS accounts. The
row looked unmapped and unmappable while the import created the account
correctly. A nameless account (referenced by #TRANS without #KONTO) was
worse: the mapper refused the self-map, so it stayed unmapped with no
self-target to pick.

- account-mapper: the bas_range self-map no longer requires a #KONTO
  name; unmapped now means exactly "outside 1000-8999". isValidBASRange
  exported as the auto-create boundary.
- AccountMappingStep (shared by both wizards): a target the list cannot
  name is an explicit "<nr> <name> (skapas vid importen)" option, a
  "nya konton skapas" badge/filter lists them, out-of-range accounts
  that block Continue are named, nameless sources say so.
- sie-import: skippedVouchers.unmappedAccounts (per account, voucher
  count) via summarizeUnmappedSkips; warning names the accounts.
- Migration result step: names created accounts and the accounts behind
  "med ej kopplade konton".

Closes #2212


Claude-Session: https://claude.ai/code/session_019SaJfqNi4VmsG8FMKq99G6

Co-authored-by: Jakob Wennberg <311770904+jakobwennberg-oss@users.noreply.github.com>
Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
Jakob Wennberg
2026-09-06 18:39:36 +02:00
committed by GitHub
co-authored by Jakob Wennberg Claude Fable 5.1
parent ea4da0eb07
commit 162de2128a
10 changed files with 318 additions and 14 deletions
@@ -1490,6 +1490,7 @@ function openingBalanceSentence(ob: NonNullable<NonNullable<ImportResult['detail
* errors keep strong color. Nothing folds away, nothing gets a box.
*/
function FiscalYearLine({ result, index }: { result: ImportResult; index: number }) {
const t = useTranslations('extensions')
const status = getFYStatus(result)
const d = result.details
const fyLabel = d?.fiscalYear
@@ -1503,7 +1504,16 @@ function FiscalYearLine({ result, index }: { result: ImportResult; index: number
if (d.skippedVouchers.empty > 0) parts.push(`${d.skippedVouchers.empty} tomma`)
if (d.skippedVouchers.unbalanced > 0) parts.push(`${d.skippedVouchers.unbalanced} obalanserade`)
if (d.skippedVouchers.singleLine > 0) parts.push(`${d.skippedVouchers.singleLine} enradiga`)
if (d.skippedVouchers.unmapped > 0) parts.push(`${d.skippedVouchers.unmapped} med ej kopplade konton`)
if (d.skippedVouchers.unmapped > 0) {
// Name the accounts (issue #2212): a count alone sends the user to diff
// the general ledger against the source system by hand.
const perAccount = (d.skippedVouchers.unmappedAccounts ?? [])
.map((a) => `konto ${a.account}: ${a.vouchers}`)
.join(', ')
parts.push(
`${d.skippedVouchers.unmapped} med ej kopplade konton${perAccount ? ` (${perAccount})` : ''}`
)
}
warningSentences.push(
`${d.skippedVouchers.total} verifikationer hoppades över (${parts.join(', ')}): saldon har justerats automatiskt via omföringsverifikation.`
)
@@ -1526,6 +1536,12 @@ function FiscalYearLine({ result, index }: { result: ImportResult; index: number
)
const infoLines: string[] = []
// Accounts the import inserted into the chart (self-mapped source accounts
// the company did not have). Said out loud so "did the import go right?"
// has an answer on screen instead of in a chart-of-accounts diff.
if (result.accountsCreated && result.accountsCreated > 0) {
infoLines.push(t('ext_arcim_accounts_created', { count: result.accountsCreated }))
}
if (d?.openingBalance) infoLines.push(openingBalanceSentence(d.openingBalance))
if (d?.migrationAdjustment?.created) {
infoLines.push(
+98 -7
View File
@@ -31,6 +31,7 @@ import {
Filter,
} from 'lucide-react'
import type { AccountMapping } from '@/lib/import/types'
import { isValidBASRange } from '@/lib/import/account-mapper'
import type { BASAccount } from '@/types'
import { getAccountClassName } from '@/lib/bookkeeping/account-descriptions'
import {
@@ -61,7 +62,7 @@ interface AccountMappingStepProps {
onBack: () => void
}
type FilterType = 'all' | 'unmapped' | 'vat_review' | 'low_confidence' | 'manual'
type FilterType = 'all' | 'unmapped' | 'new_account' | 'vat_review' | 'low_confidence' | 'manual'
const PAGE_SIZE = 50
@@ -84,6 +85,19 @@ export default function AccountMappingStep({
})
const [currentPage, setCurrentPage] = useState(1)
// Targets the dropdown can name: the caller's list (the company chart, or
// chart + BAS). A mapped target outside it is an account the import will
// CREATE (syncMappedAccounts inserts every missing target, class and type
// derived from the number). Such a row is mapped and valid, but a Select
// whose value matches no option renders blank, which is how a self-mapped
// Fortnox account outside BAS looked unmapped and unmappable (issue #2212).
// Every target outside this set is therefore rendered as an explicit
// "created on import" option.
const knownTargets = useMemo(
() => new Set(basAccounts.map((a) => a.account_number)),
[basAccounts],
)
// Filter and search mappings
const filteredMappings = useMemo(() => {
let result = mappings
@@ -93,6 +107,9 @@ export default function AccountMappingStep({
case 'unmapped':
result = result.filter((m) => !m.targetAccount)
break
case 'new_account':
result = result.filter((m) => m.targetAccount && !knownTargets.has(m.targetAccount))
break
case 'low_confidence':
result = result.filter((m) => m.targetAccount && m.confidence < 0.7)
break
@@ -117,7 +134,7 @@ export default function AccountMappingStep({
}
return result
}, [mappings, filter, searchTerm])
}, [mappings, filter, searchTerm, knownTargets])
// Pagination
const totalPages = Math.ceil(filteredMappings.length / PAGE_SIZE)
@@ -140,11 +157,20 @@ export default function AccountMappingStep({
// Calculate stats
const stats = useMemo(() => {
const unmapped = mappings.filter((m) => !m.targetAccount).length
const newAccounts = mappings.filter((m) => m.targetAccount && !knownTargets.has(m.targetAccount)).length
const lowConfidence = mappings.filter((m) => m.targetAccount && m.confidence < 0.7).length
const manual = mappings.filter((m) => m.isOverride).length
const vatReview = mappings.filter((m) => m.requiresVatTreatmentReview && !m.vatTreatmentReviewed).length
return { unmapped, lowConfidence, manual, vatReview }
}, [mappings])
return { unmapped, newAccounts, lowConfidence, manual, vatReview }
}, [mappings, knownTargets])
// After the mapper's self-map rule, an unmapped row is always a number
// outside 1000-8999: nothing can be created for it, so the user must pick a
// target. Name them so the disabled Continue button is not the only signal.
const unmappedAccounts = useMemo(
() => mappings.filter((m) => !m.targetAccount).map((m) => m.sourceAccount),
[mappings],
)
const canContinue = stats.unmapped === 0 && stats.vatReview === 0
@@ -168,7 +194,8 @@ export default function AccountMappingStep({
<CardTitle>Kontomappning</CardTitle>
<CardDescription>
Varje konto i SIE-filen kopplas till ett konto i din kontoplan.
De flesta matchas automatiskt: granska de osäkra nedan.
De flesta matchas automatiskt: granska de osäkra nedan.{' '}
{t('mapping_new_accounts_note')}
</CardDescription>
</CardHeader>
<CardContent className="space-y-4">
@@ -190,6 +217,14 @@ export default function AccountMappingStep({
<XCircle className="h-3 w-3 mr-1" />
{stats.unmapped} ej mappade
</Badge>
<Badge
variant={filter === 'new_account' ? 'default' : stats.newAccounts > 0 ? 'secondary' : 'outline'}
className="cursor-pointer"
onClick={() => handleFilterChange('new_account')}
>
<CheckCircle className="h-3 w-3 mr-1" />
{t('new_account_filter', { count: stats.newAccounts })}
</Badge>
<Badge
variant={filter === 'low_confidence' ? 'default' : stats.lowConfidence > 0 ? 'secondary' : 'outline'}
className="cursor-pointer"
@@ -215,6 +250,12 @@ export default function AccountMappingStep({
</Badge>
</div>
{unmappedAccounts.length > 0 && (
<p className="text-sm text-muted-foreground">
{t('unmapped_out_of_range_help', { accounts: unmappedAccounts.join(', ') })}
</p>
)}
{/* Search and filter */}
<div className="flex gap-4">
<div className="relative flex-1">
@@ -234,6 +275,7 @@ export default function AccountMappingStep({
<SelectContent>
<SelectItem value="all">Visa alla</SelectItem>
<SelectItem value="unmapped">Ej mappade</SelectItem>
<SelectItem value="new_account">{t('new_account_filter', { count: stats.newAccounts })}</SelectItem>
<SelectItem value="vat_review">{t('vat_review_filter', { count: stats.vatReview })}</SelectItem>
<SelectItem value="low_confidence">Osäkra</SelectItem>
<SelectItem value="manual">Manuellt satta</SelectItem>
@@ -274,7 +316,11 @@ export default function AccountMappingStep({
>
<TableCell className="font-mono">{mapping.sourceAccount}</TableCell>
<TableCell className="text-muted-foreground">
<TruncatedSourceName sourceName={mapping.sourceName} />
{mapping.sourceName ? (
<TruncatedSourceName sourceName={mapping.sourceName} />
) : (
<span className="italic">{t('source_name_missing')}</span>
)}
</TableCell>
<TableCell className="!px-0">
<ArrowRight className="mx-auto h-4 w-4 text-muted-foreground" />
@@ -284,10 +330,15 @@ export default function AccountMappingStep({
value={mapping.targetAccount || 'none'}
onValueChange={(value) => {
const account = basAccounts.find((a) => a.account_number === value)
// A target outside the list is created on import
// under the file's name (the identity option) or
// the name the row already carries.
const createdName =
value === mapping.sourceAccount ? mapping.sourceName : mapping.targetName
onMappingChange(
mapping.sourceAccount,
value === 'none' ? '' : value,
account?.account_name || ''
account?.account_name ?? createdName ?? ''
)
}}
>
@@ -296,6 +347,15 @@ export default function AccountMappingStep({
</SelectTrigger>
<SelectContent className="max-h-80">
<SelectItem value="none">-- Välj konto --</SelectItem>
{createdOptionsFor(mapping, knownTargets).map((option) => (
<SelectItem key={option.value} value={option.value}>
<span className="font-mono mr-2">{option.value}</span>
{option.name}
<span className="ml-2 text-muted-foreground">
({t('new_account_option')})
</span>
</SelectItem>
))}
{Object.entries(accountsByClass).map(([className, accounts]) => (
<div key={className}>
<div className="px-2 py-1.5 text-xs font-semibold text-muted-foreground bg-muted">
@@ -495,6 +555,37 @@ export default function AccountMappingStep({
)
}
/**
* The "created on import" options a row can select: its own number (when
* that is in the auto-create range and not already in the chart) and, if the
* row currently points at some other target the list cannot name, that
* target too, so the current value always renders. Ordered so the identity
* option comes first.
*/
function createdOptionsFor(
mapping: AccountMapping,
knownTargets: ReadonlySet<string>,
): Array<{ value: string; name: string }> {
const options: Array<{ value: string; name: string }> = []
if (!knownTargets.has(mapping.sourceAccount) && isValidBASRange(mapping.sourceAccount)) {
options.push({
value: mapping.sourceAccount,
name: mapping.sourceName || `Konto ${mapping.sourceAccount}`,
})
}
if (
mapping.targetAccount &&
mapping.targetAccount !== mapping.sourceAccount &&
!knownTargets.has(mapping.targetAccount)
) {
options.push({
value: mapping.targetAccount,
name: mapping.targetName || `Konto ${mapping.targetAccount}`,
})
}
return options
}
function TruncatedSourceName({ sourceName }: { sourceName: string }) {
const [open, setOpen] = useState(false)