* 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
128 lines
4.6 KiB
TypeScript
128 lines
4.6 KiB
TypeScript
import type { SupabaseClient } from '@supabase/supabase-js'
|
|
|
|
/**
|
|
* Lönejournal — Monthly/annual per-employee salary register.
|
|
*
|
|
* Required per BFL as underlag for AGI reconciliation.
|
|
* Lists gross, tax, net, avgifter, and vacation accrual per employee per period.
|
|
*
|
|
* Per BFNAR 2013:2: Must be part of systemdokumentation and producible
|
|
* on demand for audit. Retained 7 years per BFL 7 kap.
|
|
*/
|
|
|
|
export interface SalaryJournalRow {
|
|
employeeId: string
|
|
employeeName: string
|
|
personnummerLast4: string
|
|
employmentType: string
|
|
periodYear: number
|
|
periodMonth: number
|
|
paymentDate: string
|
|
grossSalary: number
|
|
taxWithheld: number
|
|
netSalary: number
|
|
avgifterAmount: number
|
|
avgifterRate: number
|
|
vacationAccrual: number
|
|
vacationAccrualAvgifter: number
|
|
totalEmployerCost: number
|
|
sickDays: number
|
|
vabDays: number
|
|
parentalDays: number
|
|
vacationDaysTaken: number
|
|
salaryRunStatus: string
|
|
}
|
|
|
|
export interface SalaryJournalReport {
|
|
rows: SalaryJournalRow[]
|
|
totals: {
|
|
grossSalary: number
|
|
taxWithheld: number
|
|
netSalary: number
|
|
avgifterAmount: number
|
|
vacationAccrual: number
|
|
vacationAccrualAvgifter: number
|
|
totalEmployerCost: number
|
|
}
|
|
period: { year: number; monthFrom?: number; monthTo?: number }
|
|
}
|
|
|
|
/**
|
|
* Generate lönejournal for a year or specific month range.
|
|
*/
|
|
export async function generateSalaryJournal(
|
|
supabase: SupabaseClient,
|
|
companyId: string,
|
|
year: number,
|
|
monthFrom?: number,
|
|
monthTo?: number
|
|
): Promise<SalaryJournalReport> {
|
|
const query = supabase
|
|
.from('salary_run_employees')
|
|
.select(`
|
|
*,
|
|
employee:employees(id, first_name, last_name, personnummer_last4, employment_type),
|
|
salary_run:salary_runs(period_year, period_month, payment_date, status)
|
|
`)
|
|
.eq('company_id', companyId)
|
|
|
|
// We need to filter by the salary_run's period_year, which requires a join filter
|
|
// Supabase doesn't support filtering on joined columns directly in .eq(),
|
|
// so we fetch all and filter client-side for the year
|
|
const { data, error } = await query.order('created_at')
|
|
|
|
if (error) {
|
|
throw new Error(`Failed to generate salary journal: ${error.message}`)
|
|
}
|
|
|
|
const rows: SalaryJournalRow[] = (data || [])
|
|
.filter(sre => {
|
|
const run = sre.salary_run as { period_year: number; period_month: number; status: string } | null
|
|
if (!run || run.period_year !== year) return false
|
|
if (run.status !== 'booked') return false // Only booked runs for BFL-compliant lönejournal
|
|
if (monthFrom && run.period_month < monthFrom) return false
|
|
if (monthTo && run.period_month > monthTo) return false
|
|
return true
|
|
})
|
|
.map(sre => {
|
|
const emp = sre.employee as { first_name: string; last_name: string; personnummer_last4: string; employment_type: string } | null
|
|
const run = sre.salary_run as { period_year: number; period_month: number; payment_date: string; status: string }
|
|
return {
|
|
employeeId: sre.employee_id,
|
|
employeeName: emp ? `${emp.first_name} ${emp.last_name}` : 'Okänd',
|
|
personnummerLast4: emp?.personnummer_last4 || '????',
|
|
employmentType: emp?.employment_type || 'employee',
|
|
periodYear: run.period_year,
|
|
periodMonth: run.period_month,
|
|
paymentDate: run.payment_date,
|
|
grossSalary: sre.gross_salary,
|
|
taxWithheld: sre.tax_withheld,
|
|
netSalary: sre.net_salary,
|
|
avgifterAmount: sre.avgifter_amount,
|
|
avgifterRate: sre.avgifter_rate,
|
|
vacationAccrual: sre.vacation_accrual,
|
|
vacationAccrualAvgifter: sre.vacation_accrual_avgifter,
|
|
totalEmployerCost: sre.gross_salary + sre.avgifter_amount + sre.vacation_accrual + sre.vacation_accrual_avgifter,
|
|
sickDays: sre.sick_days,
|
|
vabDays: sre.vab_days,
|
|
parentalDays: sre.parental_days,
|
|
vacationDaysTaken: sre.vacation_days_taken,
|
|
salaryRunStatus: run.status,
|
|
}
|
|
})
|
|
.sort((a, b) => a.periodMonth - b.periodMonth || a.employeeName.localeCompare(b.employeeName))
|
|
|
|
const r = (x: number) => Math.round(x * 100) / 100
|
|
const totals = {
|
|
grossSalary: r(rows.reduce((s, r) => s + r.grossSalary, 0)),
|
|
taxWithheld: r(rows.reduce((s, r) => s + r.taxWithheld, 0)),
|
|
netSalary: r(rows.reduce((s, r) => s + r.netSalary, 0)),
|
|
avgifterAmount: r(rows.reduce((s, r) => s + r.avgifterAmount, 0)),
|
|
vacationAccrual: r(rows.reduce((s, r) => s + r.vacationAccrual, 0)),
|
|
vacationAccrualAvgifter: r(rows.reduce((s, r) => s + r.vacationAccrualAvgifter, 0)),
|
|
totalEmployerCost: r(rows.reduce((s, r) => s + r.totalEmployerCost, 0)),
|
|
}
|
|
|
|
return { rows, totals, period: { year, monthFrom, monthTo } }
|
|
}
|