Files
accounted/lib/salary/lonevaxling.ts
T
Mattsson 04dbb31d7e Salary module (#245)
* feat: implement salary module with personnummer encryption, salary entries, tax tables, and AGI tracking

- Added personnummer encryption and decryption functions for secure storage.
- Created salary entries handling for journal entries including gross salary, tax withholding, and employer contributions.
- Implemented tax table lookup functionality for calculating tax amounts based on monthly income.
- Developed SQL migration for salary module including tables for payroll configuration, tax rates, employees, salary runs, salary run employees, salary line items, and AGI declarations.
- Established row-level security policies for all new tables to ensure company-scoped access.

* feat: add salary calculation modules for 2026

- Implemented engångsskatt calculation for one-time payments with tax brackets.
- Added löneväxling functionality for salary sacrifice to pension, including employer savings and warnings.
- Created pain.001 generator for salary batch payments in compliance with Swedish banking standards.
- Developed PDF template for payslips, including detailed breakdowns and employer costs.
- Generated seed data for Swedish tax tables for 2026, including SQL insert statements.
- Implemented traktamente calculations for per diem and mileage allowances, adhering to Skatteverket regulations.
- Added seed script for populating tax tables in the database.

* feat: Update meal reduction percentages in traktamente calculation

fix: Remove obsolete seed script for 2026 tax tables

feat: Extend SalaryRunStatus type to include 'corrected' status

feat: Implement KU10 XML generation endpoint for annual employee income statements

feat: Add endpoint for creating corrections to booked salary runs

feat: Implement endpoint for sending payslip PDFs to employees

feat: Create KU10 XML generator for annual reporting

feat: Add salary transaction matcher for auto-linking bank transactions to salary entries

chore: Add database migration for salary correction support

* feat: replace select elements with custom Select component for employment and salary types

* feat: enhance salary calculations with pension entry and avgifter category support
2026-04-15 11:17:39 +02:00

126 lines
3.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import type { PayrollConfig } from './payroll-config'
/**
* Löneväxling till pension — salary sacrifice to occupational pension.
*
* The 1.058 factor: For every 1 SEK salary reduction, pension contribution
* should be 1.058 SEK to make the employee roughly cost-neutral
* (because reduced salary → reduced avgifter for employer).
*
* Warnings:
* - Post-reduction salary < 8.07 × IBB / 12 reduces PGI and SGI
* - Employer pension cap: 35% of pensionsmedförande lön or 10 × PBB/year
*/
export interface LoneVaxlingResult {
salaryReduction: number
pensionContribution: number
preReductionSalary: number
postReductionSalary: number
savedAvgifter: number
slpOnPension: number
netEmployerSaving: number
warnings: string[]
steps: LoneVaxlingStep[]
}
export interface LoneVaxlingStep {
label: string
formula: string
input: Record<string, number | string>
output: number
}
/**
* Calculate löneväxling impact.
*
* @param reductionAmount - Monthly bruttolöneavdrag amount
* @param currentMonthlySalary - Current gross monthly salary before reduction
* @param config - Payroll config for the year
* @param avgifterRate - Employee's applicable avgifter rate (may differ from standard)
*/
export function calculateLoneVaxling(
reductionAmount: number,
currentMonthlySalary: number,
config: PayrollConfig,
avgifterRate: number = 0.3142
): LoneVaxlingResult {
const r = (x: number) => Math.round(x * 100) / 100
const steps: LoneVaxlingStep[] = []
const warnings: string[] = []
const factor = 1.058
const pensionContribution = r(reductionAmount * factor)
steps.push({
label: 'Pensionsavsättning',
formula: 'reduction × 1.058',
input: { reduction: reductionAmount, factor },
output: pensionContribution,
})
const postReductionSalary = r(currentMonthlySalary - reductionAmount)
steps.push({
label: 'Ny bruttolön',
formula: 'current - reduction',
input: { current: currentMonthlySalary, reduction: reductionAmount },
output: postReductionSalary,
})
// Avgifter savings
const savedAvgifter = r(reductionAmount * avgifterRate)
steps.push({
label: 'Sparade arbetsgivaravgifter',
formula: 'reduction × avgifter_rate',
input: { reduction: reductionAmount, avgifter_rate: avgifterRate },
output: savedAvgifter,
})
// SLP (Särskild löneskatt) on pension: 24.26%
const slpOnPension = r(pensionContribution * config.slpRate)
steps.push({
label: 'Särskild löneskatt på pension',
formula: 'pension × SLP_rate',
input: { pension: pensionContribution, slp_rate: config.slpRate },
output: slpOnPension,
})
// Net employer cost difference
const netEmployerSaving = r(savedAvgifter - pensionContribution - slpOnPension + reductionAmount)
steps.push({
label: 'Nettoresultat arbetsgivare',
formula: 'saved_avgifter - pension - slp + reduction',
input: { saved_avgifter: savedAvgifter, pension: pensionContribution, slp: slpOnPension, reduction: reductionAmount },
output: netEmployerSaving,
})
// Warning: PGI/SGI floor
const pgiFloor = r(8.07 * config.inkomstbasbelopp / 12)
if (postReductionSalary < pgiFloor) {
warnings.push(
`Varning: Ny lön ${r(postReductionSalary)} SEK < PGI-golv ${r(pgiFloor)} SEK/mån. ` +
`Reducerad allmän pension och sjukpenninggrundande inkomst (SGI).`
)
}
// Warning: Pension cap
const annualPensionCap = r(10 * config.prisbasbelopp)
const annualContribution = r(pensionContribution * 12)
if (annualContribution > annualPensionCap) {
warnings.push(
`Varning: Årsvis pensionsavsättning ${r(annualContribution)} SEK överstiger taket ${r(annualPensionCap)} SEK (10 × PBB).`
)
}
return {
salaryReduction: reductionAmount,
pensionContribution,
preReductionSalary: currentMonthlySalary,
postReductionSalary,
savedAvgifter,
slpOnPension,
netEmployerSaving,
warnings,
steps,
}
}