* 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
122 lines
4.1 KiB
TypeScript
122 lines
4.1 KiB
TypeScript
import { createClient } from '@/lib/supabase/server'
|
|
import { NextResponse } from 'next/server'
|
|
import { ensureInitialized } from '@/lib/init'
|
|
import { validateBody } from '@/lib/api/validate'
|
|
import { CreateEmployeeSchema } from '@/lib/api/schemas'
|
|
import { requireCompanyId } from '@/lib/company/context'
|
|
import { requireWritePermission } from '@/lib/auth/require-write'
|
|
import { encryptPersonnummer, extractLast4, validatePersonnummer } from '@/lib/salary/personnummer'
|
|
|
|
ensureInitialized()
|
|
|
|
export async function GET(request: Request) {
|
|
const supabase = await createClient()
|
|
const { data: { user } } = await supabase.auth.getUser()
|
|
if (!user) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
|
|
|
const companyId = await requireCompanyId(supabase, user.id)
|
|
|
|
const { searchParams } = new URL(request.url)
|
|
const activeOnly = searchParams.get('active') !== 'false'
|
|
|
|
let query = supabase
|
|
.from('employees')
|
|
.select('*')
|
|
.eq('company_id', companyId)
|
|
|
|
if (activeOnly) {
|
|
query = query.eq('is_active', true)
|
|
}
|
|
|
|
const { data, error } = await query.order('last_name')
|
|
|
|
if (error) {
|
|
return NextResponse.json({ error: error.message }, { status: 500 })
|
|
}
|
|
|
|
// Mask personnummer — only show last 4
|
|
const masked = (data || []).map(emp => ({
|
|
...emp,
|
|
personnummer: `XXXXXXXX-${emp.personnummer_last4}`,
|
|
}))
|
|
|
|
return NextResponse.json({ data: masked })
|
|
}
|
|
|
|
export async function POST(request: Request) {
|
|
const supabase = await createClient()
|
|
const { data: { user } } = await supabase.auth.getUser()
|
|
if (!user) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
|
|
|
const writeCheck = await requireWritePermission(supabase, user.id)
|
|
if (!writeCheck.ok) return writeCheck.response
|
|
|
|
const companyId = await requireCompanyId(supabase, user.id)
|
|
|
|
const validation = await validateBody(request, CreateEmployeeSchema)
|
|
if (!validation.success) return validation.response
|
|
const body = validation.data
|
|
|
|
// Validate personnummer format + Luhn
|
|
const pnrValidation = validatePersonnummer(body.personnummer)
|
|
if (!pnrValidation.valid) {
|
|
return NextResponse.json({ error: pnrValidation.error }, { status: 400 })
|
|
}
|
|
|
|
// Encrypt personnummer
|
|
const encryptedPnr = encryptPersonnummer(body.personnummer)
|
|
const last4 = extractLast4(body.personnummer)
|
|
|
|
const { data: employee, error } = await supabase
|
|
.from('employees')
|
|
.insert({
|
|
company_id: companyId,
|
|
user_id: user.id,
|
|
first_name: body.first_name,
|
|
last_name: body.last_name,
|
|
personnummer: encryptedPnr,
|
|
personnummer_last4: last4,
|
|
employment_type: body.employment_type,
|
|
employment_start: body.employment_start,
|
|
employment_end: body.employment_end || null,
|
|
employment_degree: body.employment_degree,
|
|
salary_type: body.salary_type,
|
|
monthly_salary: body.monthly_salary || null,
|
|
hourly_rate: body.hourly_rate || null,
|
|
tax_table_number: body.tax_table_number || null,
|
|
tax_column: body.tax_column,
|
|
tax_municipality: body.tax_municipality || null,
|
|
is_sidoinkomst: body.is_sidoinkomst,
|
|
f_skatt_status: body.f_skatt_status,
|
|
clearing_number: body.clearing_number || null,
|
|
bank_account_number: body.bank_account_number || null,
|
|
vacation_rule: body.vacation_rule,
|
|
vacation_days_per_year: body.vacation_days_per_year,
|
|
semestertillagg_rate: body.semestertillagg_rate,
|
|
email: body.email || null,
|
|
phone: body.phone || null,
|
|
address_line1: body.address_line1 || null,
|
|
postal_code: body.postal_code || null,
|
|
city: body.city || null,
|
|
vaxa_stod_eligible: body.vaxa_stod_eligible,
|
|
vaxa_stod_start: body.vaxa_stod_start || null,
|
|
vaxa_stod_end: body.vaxa_stod_end || null,
|
|
})
|
|
.select()
|
|
.single()
|
|
|
|
if (error) {
|
|
if (error.code === '23505') {
|
|
return NextResponse.json({ error: 'En anställd med detta personnummer finns redan' }, { status: 409 })
|
|
}
|
|
return NextResponse.json({ error: error.message }, { status: 500 })
|
|
}
|
|
|
|
return NextResponse.json({
|
|
data: {
|
|
...employee,
|
|
personnummer: `XXXXXXXX-${last4}`,
|
|
},
|
|
}, { status: 201 })
|
|
}
|