81e9dd224e
* feat(import): add customer and supplier parsing functionality - Implemented customer file parsing in `lib/import/customers/parser.ts` with support for Excel and CSV formats. - Created types for detected customer columns and parsed customer rows in `lib/import/customers/types.ts`. - Added tests for customer classification logic in `lib/import/shared/__tests__/classify.test.ts`. - Developed classification functions for customers and suppliers in `lib/import/shared/classify.ts`. - Introduced shared column utility functions in `lib/import/shared/column-utils.ts`. - Implemented supplier file parsing in `lib/import/suppliers/parser.ts` with validation for various fields. - Created types for detected supplier columns and parsed supplier rows in `lib/import/suppliers/types.ts`. - Added tests for supplier column detection and parsing in `lib/import/suppliers/__tests__/column-detector.test.ts` and `lib/import/suppliers/__tests__/parser.test.ts`. * fix(labels): update 'Svenskt företag' to 'Svenskt företag eller organisation' for clarity * feat(import): refactor encoding handling for Swedish files and add tests for character preservation * feat(recapt): implement clearRecaptIdentity function and integrate into logout flow * feat(bookkeeping): implement copy functionality and next voucher sequence retrieval * feat(import): enhance customer and supplier import functionality with normalization and event handling
107 lines
3.5 KiB
TypeScript
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'
|
|
}
|