* 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
87 lines
2.8 KiB
TypeScript
87 lines
2.8 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 { CreateSalaryLineItemSchema } from '@/lib/api/schemas'
|
|
import { requireCompanyId } from '@/lib/company/context'
|
|
import { requireWritePermission } from '@/lib/auth/require-write'
|
|
import { getLineItemAccount } from '@/lib/salary/account-mapping'
|
|
|
|
ensureInitialized()
|
|
|
|
export async function POST(
|
|
request: Request,
|
|
{ params }: { params: Promise<{ id: string }> }
|
|
) {
|
|
const { id } = await params
|
|
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, CreateSalaryLineItemSchema)
|
|
if (!validation.success) return validation.response
|
|
const body = validation.data
|
|
|
|
// Verify run is draft
|
|
const { data: run } = await supabase
|
|
.from('salary_runs')
|
|
.select('id, status')
|
|
.eq('id', id)
|
|
.eq('company_id', companyId)
|
|
.single()
|
|
|
|
if (!run) {
|
|
return NextResponse.json({ error: 'Lönekörning hittades inte' }, { status: 404 })
|
|
}
|
|
if (run.status !== 'draft') {
|
|
return NextResponse.json({ error: 'Kan bara redigera utkast' }, { status: 400 })
|
|
}
|
|
|
|
// Verify salary_run_employee belongs to this run
|
|
const { data: sre } = await supabase
|
|
.from('salary_run_employees')
|
|
.select('id, employee_id')
|
|
.eq('id', body.salary_run_employee_id)
|
|
.eq('salary_run_id', id)
|
|
.single()
|
|
|
|
if (!sre) {
|
|
return NextResponse.json({ error: 'Anställd finns inte i denna lönekörning' }, { status: 404 })
|
|
}
|
|
|
|
// Auto-resolve account if not provided
|
|
const accountNumber = body.account_number || getLineItemAccount(body.item_type as never)
|
|
|
|
const { data: lineItem, error } = await supabase
|
|
.from('salary_line_items')
|
|
.insert({
|
|
salary_run_employee_id: body.salary_run_employee_id,
|
|
company_id: companyId,
|
|
item_type: body.item_type,
|
|
description: body.description,
|
|
quantity: body.quantity || null,
|
|
unit_price: body.unit_price || null,
|
|
amount: Math.round(body.amount * 100) / 100,
|
|
is_taxable: body.is_taxable,
|
|
is_avgift_basis: body.is_avgift_basis,
|
|
is_vacation_basis: body.is_vacation_basis,
|
|
is_gross_deduction: body.is_gross_deduction,
|
|
is_net_deduction: body.is_net_deduction,
|
|
account_number: accountNumber,
|
|
sort_order: body.sort_order,
|
|
})
|
|
.select()
|
|
.single()
|
|
|
|
if (error) {
|
|
return NextResponse.json({ error: error.message }, { status: 500 })
|
|
}
|
|
|
|
return NextResponse.json({ data: lineItem }, { status: 201 })
|
|
}
|