Files
accounted/lib/import/shared/classify.ts
T
Jakob Wennberg ec27228a8e style: remove em/en dashes repo-wide, add CLAUDE.md rule against them (#890)
Em dashes (—) and en dashes (–) had spread across comments, docs, tests,
and a few UI strings, reading as AI-generated boilerplate rather than
house style. Replaced each with punctuation matching its context: colon
for explanatory clauses, comma for asides, plain hyphen for numeric/legal
ranges (e.g. "21-23§"), "to"/"till" for date ranges, parentheses for
paired-dash asides. messages/en.json and messages/sv.json were fixed by
hand together to keep sv/en in sync.

Left untouched where the dash is the functional subject rather than
decorative punctuation: date-range-parser.ts's separator regex,
charset-repair.ts's CP1252 byte-mapping table (and its test), the SIE
encoding mojibake docs, generic-csv.ts's minus-sign normalizer, the
agent system-prompt files that already instruct against em dashes, and
a golden iXBRL test fixture compared byte-for-byte.

Also fixes two bugs surfaced along the way: an off-by-one in
ApiKeysPanel's scope-label split (a leftover from an earlier partial
pass), and a charset-repair test that had lost the literal en-dash it
exists to verify.

Regenerated the agent atom seed migration (skills:generate) since 27
SKILL.md files changed. Added a CLAUDE.md rule against em/en dashes,
with an explicit carve-out for the functional-dash cases above.

Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
2026-07-04 15:58:06 +02:00

107 lines
3.5 KiB
TypeScript

import type { CustomerType, SupplierType } from '@/types'
/**
* EU VAT number prefixes (excluding SE).
* Source: https://taxation-ec.europa.eu/online-services/check-vat-number-vies_en
*/
export const EU_VAT_PREFIXES = new Set([
'AT', 'BE', 'BG', 'CY', 'CZ', 'DE', 'DK', 'EE', 'EL', 'ES',
'FI', 'FR', 'GR', 'HR', 'HU', 'IE', 'IT', 'LT', 'LU', 'LV',
'MT', 'NL', 'PL', 'PT', 'RO', 'SI', 'SK', 'XI', // XI = Northern Ireland
])
/**
* Strip whitespace, dashes, and dots from an org/personnummer for length checks.
*/
function digits(value: string | null): string {
if (!value) return ''
return value.replace(/\D/g, '')
}
/**
* Check the third digit of a Swedish org/personnummer.
*
* Personnummer: month digit (00-12): third digit ≤ 1.
* Företag: third digit ≥ 2 (per Skatteverket's allocation rules).
*/
function looksLikePersonnummer(orgNumber: string | null): boolean {
const d = digits(orgNumber)
// 12 digits = full personnummer (YYYYMMDDXXXX)
if (d.length === 12) return true
// 10 digits: disambiguate by month (positions 3-4 are month, 01-12)
if (d.length === 10) {
const month = parseInt(d.substring(2, 4), 10)
if (month >= 1 && month <= 12 && d[2] <= '1') return true
}
return false
}
function vatPrefix(vatNumber: string | null): string | null {
if (!vatNumber) return null
const cleaned = vatNumber.trim().toUpperCase()
const match = cleaned.match(/^([A-Z]{2})/)
return match ? match[1] : null
}
/**
* Auto-classify a customer based on org_number + vat_number heuristics.
*
* Precedence:
* 1. Non-SE EU VAT prefix → 'eu_business'
* 2. Non-EU letter prefix → 'non_eu_business'
* 3. Personnummer-shaped org → 'individual'
* 4. Default → 'swedish_business'
*/
export function classifyCustomer(args: {
org_number: string | null
vat_number: string | null
country?: string | null
}): CustomerType {
const prefix = vatPrefix(args.vat_number)
if (prefix && prefix !== 'SE') {
if (EU_VAT_PREFIXES.has(prefix)) return 'eu_business'
return 'non_eu_business'
}
const country = args.country?.trim().toUpperCase()
if (country && country !== 'SE' && country !== 'SVERIGE' && country !== 'SWEDEN') {
// Country-based fallback when VAT is missing.
if (country.length === 2 && EU_VAT_PREFIXES.has(country)) return 'eu_business'
if (country.length >= 3) {
// Common Swedish names for non-EU jurisdictions; heuristic is best-effort
if (/norge|norway|usa|kanada|canada|storbritannien|uk|united kingdom/i.test(country)) {
return 'non_eu_business'
}
}
}
if (looksLikePersonnummer(args.org_number)) return 'individual'
return 'swedish_business'
}
/**
* Auto-classify a supplier. Suppliers cannot be 'individual': Swedish business
* with personnummer is still 'swedish_business' (a sole trader supplier).
*/
export function classifySupplier(args: {
org_number: string | null
vat_number: string | null
country?: string | null
}): SupplierType {
const prefix = vatPrefix(args.vat_number)
if (prefix && prefix !== 'SE') {
if (EU_VAT_PREFIXES.has(prefix)) return 'eu_business'
return 'non_eu_business'
}
const country = args.country?.trim().toUpperCase()
if (country && country !== 'SE' && country !== 'SVERIGE' && country !== 'SWEDEN') {
if (country.length === 2 && EU_VAT_PREFIXES.has(country)) return 'eu_business'
if (/norge|norway|usa|kanada|canada|storbritannien|uk|united kingdom/i.test(country)) {
return 'non_eu_business'
}
}
return 'swedish_business'
}