Files
accounted/lib/skatteverket/skattekonto-ocr.ts
T
Mattsson 7e81f661b2 Add/skv salary agi (#423)
* feat: add Bankgirot LB-fil support for salary payments and tax payments

- Implemented `generateBgLb` for salary batch payments, producing opening, payment, and closing records.
- Added tests for `generateBgLb` to ensure correct record generation and validation.
- Created `generateBankgiroPaymentBgLb` for single tax payments to Skatteverket, including validation and formatting.
- Added tests for `generateBankgiroPaymentBgLb` to verify record structure and data integrity.
- Introduced `generateSkattekontoOcr` for generating valid OCR references for Skattekonto payments, with tests for various input formats.
- Updated database schema to track payment file formats and timestamps for salary runs and AGI declarations.
- Created a new table for logging salary payslip deliveries to ensure compliance with audit requirements.

* feat: add write permission check and company ID validation for payment file generation

* feat: add write permission check for salary payment file generation
2026-05-09 12:41:16 +02:00

54 lines
1.9 KiB
TypeScript

/**
* OCR-nummer for Skatteverket Skattekonto payments.
*
* Companies pay tax (skatt + arbetsgivaravgifter + F-skatt + slutlig skatt etc.)
* to Bankgiro 5050-1055 with an OCR reference. The reference identifies which
* Skattekonto receives the credit; Skatteverket applies it to the most recent
* declared liability.
*
* Format (per Skatteverket "OCR-nummer för inbetalning till skattekontot"):
* - 10-digit organisationsnummer (AB) or 10-digit personnummer (EF)
* stripped of dashes/spaces
* - Followed by a single Luhn check digit
* - Total: 11 digits
*
* Examples:
* 556012-3456 → "5560123456" + check digit "6" = "55601234566"
* 880225-1234 → "8802251234" + check digit → 11 digits
*
* Reference: https://www.skatteverket.se/foretag/skatterochavdrag/skattekonto/betalainochavskattekonto/sabetalardupaskattekontot.4.18e1b10334ebe8bc80004499.html
*/
import { luhnCheckDigit } from '@/lib/bankgiro/luhn'
/** Bankgiro number for all payments to Skattekontot. */
export const SKATTEKONTO_BANKGIRO = '5050-1055'
/**
* Generate the standard Skattekontot OCR reference for a company.
*
* Accepts org_number/personnummer in any common Swedish format
* ("556012-3456", "5560123456", "19880225-1234", "198802251234").
*
* For 12-digit personnummer (with century prefix), the leading century digits
* are stripped — Skatteverket's Skattekonto-OCR uses the 10-digit form.
*/
export function generateSkattekontoOcr(orgOrPersonnummer: string): string {
const digits = orgOrPersonnummer.replace(/\D/g, '')
let base: string
if (digits.length === 10) {
base = digits
} else if (digits.length === 12) {
// Strip century prefix (1900s = "19", 2000s = "20")
base = digits.slice(2)
} else {
throw new Error(
`Ogiltigt org/personnummer för Skattekonto-OCR: "${orgOrPersonnummer}" (förväntat 10 eller 12 siffror)`
)
}
const checkDigit = luhnCheckDigit(base)
return base + checkDigit.toString()
}