* 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 * feat: enhance employee management with salary type, tax status, and validation improvements * feat: Implement AGI submission flow to Skatteverket - Added AGI submission route to handle the submission process. - Created AGI client for interacting with Skatteverket's API. - Introduced AGI mappers to convert salary run data into the required AGI JSON payload format. - Enhanced API client to support custom base URLs for Skatteverket API requests. - Added types for AGI submission payload and validation results. - Implemented tests for AGI mappers to ensure correct payload structure and data handling. * feat: enhance salary module with Skatteverket integration and update dashboard navigation * Update app/api/salary/runs/[id]/agi/submit/route.ts Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com> * Update app/api/salary/runs/[id]/approve/route.ts Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com> * feat: integrate write permission check and remove Skatteverket extension --------- Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
230 lines
6.1 KiB
TypeScript
230 lines
6.1 KiB
TypeScript
import type { SupabaseClient } from '@supabase/supabase-js'
|
|
import { skvRequest } from './api-client'
|
|
import type { SkatteverketAGIInlamning, SkatteverketAGIKontrollresultat } from '../types'
|
|
|
|
/**
|
|
* Skatteverket AGI (Arbetsgivardeklaration) API client.
|
|
*
|
|
* Follows the same pattern as the Momsdeklaration API:
|
|
* kontrollera → utkast → lås → (BankID signering) → inlämnat
|
|
*
|
|
* Base URL: https://api.skatteverket.se/arbetsgivardeklaration/inlamning/v1
|
|
*
|
|
* Endpoint pattern:
|
|
* /arbetsgivare/{arbetsgivarregistrerad}/redovisningsperioder/{redovisningsperiod}/...
|
|
*/
|
|
|
|
const DEFAULT_AGI_API_BASE_URL =
|
|
'https://api.test.skatteverket.se/arbetsgivardeklaration/inlamning/v1'
|
|
|
|
function getAgiApiBaseUrl(): string {
|
|
return process.env.SKATTEVERKET_AGI_API_BASE_URL || DEFAULT_AGI_API_BASE_URL
|
|
}
|
|
|
|
function basePath(arbetsgivare: string, period: string): string {
|
|
return `/arbetsgivare/${arbetsgivare}/redovisningsperioder/${period}`
|
|
}
|
|
|
|
/**
|
|
* Validate AGI data (dry run) without saving.
|
|
* Returns validation errors/warnings.
|
|
*/
|
|
export async function agiValidate(
|
|
supabase: SupabaseClient,
|
|
userId: string,
|
|
arbetsgivare: string,
|
|
period: string,
|
|
payload: SkatteverketAGIInlamning
|
|
): Promise<{ ok: boolean; status: number; data?: SkatteverketAGIKontrollresultat; error?: string }> {
|
|
const response = await skvRequest(
|
|
supabase,
|
|
userId,
|
|
'POST',
|
|
`${basePath(arbetsgivare, period)}/kontrollera`,
|
|
payload,
|
|
{ baseUrl: getAgiApiBaseUrl() }
|
|
)
|
|
|
|
if (!response.ok) {
|
|
const text = await response.text()
|
|
return { ok: false, status: response.status, error: text }
|
|
}
|
|
|
|
const data = await response.json()
|
|
return { ok: true, status: response.status, data }
|
|
}
|
|
|
|
/**
|
|
* Save AGI as draft to Skatteverket's "Eget utrymme".
|
|
* Returns kontrollresultat and inlämningsId.
|
|
*/
|
|
export async function agiSaveDraft(
|
|
supabase: SupabaseClient,
|
|
userId: string,
|
|
arbetsgivare: string,
|
|
period: string,
|
|
payload: SkatteverketAGIInlamning
|
|
): Promise<{ ok: boolean; status: number; data?: { inlamningId?: string; kontrollresultat?: SkatteverketAGIKontrollresultat }; error?: string }> {
|
|
const response = await skvRequest(
|
|
supabase,
|
|
userId,
|
|
'POST',
|
|
`${basePath(arbetsgivare, period)}/inlamningar`,
|
|
payload,
|
|
{ baseUrl: getAgiApiBaseUrl() }
|
|
)
|
|
|
|
if (!response.ok) {
|
|
const text = await response.text()
|
|
return { ok: false, status: response.status, error: text }
|
|
}
|
|
|
|
const data = await response.json()
|
|
return { ok: true, status: response.status, data }
|
|
}
|
|
|
|
/**
|
|
* Get a specific AGI submission.
|
|
*/
|
|
export async function agiGetSubmission(
|
|
supabase: SupabaseClient,
|
|
userId: string,
|
|
arbetsgivare: string,
|
|
period: string,
|
|
inlamningId: string
|
|
): Promise<{ ok: boolean; status: number; data?: unknown; error?: string }> {
|
|
const response = await skvRequest(
|
|
supabase,
|
|
userId,
|
|
'GET',
|
|
`${basePath(arbetsgivare, period)}/inlamningar/${inlamningId}`,
|
|
undefined,
|
|
{ baseUrl: getAgiApiBaseUrl() }
|
|
)
|
|
|
|
if (response.status === 404) {
|
|
return { ok: true, status: 404, data: null }
|
|
}
|
|
|
|
if (!response.ok) {
|
|
const text = await response.text()
|
|
return { ok: false, status: response.status, error: text }
|
|
}
|
|
|
|
const data = await response.json()
|
|
return { ok: true, status: response.status, data }
|
|
}
|
|
|
|
/**
|
|
* Delete a draft AGI submission.
|
|
*/
|
|
export async function agiDeleteDraft(
|
|
supabase: SupabaseClient,
|
|
userId: string,
|
|
arbetsgivare: string,
|
|
period: string,
|
|
inlamningId: string
|
|
): Promise<{ ok: boolean; status: number; error?: string }> {
|
|
const response = await skvRequest(
|
|
supabase,
|
|
userId,
|
|
'DELETE',
|
|
`${basePath(arbetsgivare, period)}/inlamningar/${inlamningId}`,
|
|
undefined,
|
|
{ baseUrl: getAgiApiBaseUrl() }
|
|
)
|
|
|
|
if (response.status !== 204 && !response.ok) {
|
|
const text = await response.text()
|
|
return { ok: false, status: response.status, error: text }
|
|
}
|
|
|
|
return { ok: true, status: response.status }
|
|
}
|
|
|
|
/**
|
|
* Lock the reporting period for signing.
|
|
* Returns a signeringslänk for BankID signing on Skatteverket's site.
|
|
*/
|
|
export async function agiLockPeriod(
|
|
supabase: SupabaseClient,
|
|
userId: string,
|
|
arbetsgivare: string,
|
|
period: string
|
|
): Promise<{ ok: boolean; status: number; data?: { signeringslank?: string }; error?: string }> {
|
|
const response = await skvRequest(
|
|
supabase,
|
|
userId,
|
|
'PUT',
|
|
`${basePath(arbetsgivare, period)}/las`,
|
|
undefined,
|
|
{ baseUrl: getAgiApiBaseUrl() }
|
|
)
|
|
|
|
if (!response.ok) {
|
|
const text = await response.text()
|
|
return { ok: false, status: response.status, error: text }
|
|
}
|
|
|
|
const data = await response.json()
|
|
return { ok: true, status: response.status, data }
|
|
}
|
|
|
|
/**
|
|
* Unlock a locked reporting period (cancel signing).
|
|
*/
|
|
export async function agiUnlockPeriod(
|
|
supabase: SupabaseClient,
|
|
userId: string,
|
|
arbetsgivare: string,
|
|
period: string
|
|
): Promise<{ ok: boolean; status: number; error?: string }> {
|
|
const response = await skvRequest(
|
|
supabase,
|
|
userId,
|
|
'DELETE',
|
|
`${basePath(arbetsgivare, period)}/las`,
|
|
undefined,
|
|
{ baseUrl: getAgiApiBaseUrl() }
|
|
)
|
|
|
|
if (response.status !== 204 && !response.ok) {
|
|
const text = await response.text()
|
|
return { ok: false, status: response.status, error: text }
|
|
}
|
|
|
|
return { ok: true, status: response.status }
|
|
}
|
|
|
|
/**
|
|
* Fetch submitted AGI (after signing).
|
|
* Returns kvittensnummer and submission timestamp.
|
|
*/
|
|
export async function agiGetSubmitted(
|
|
supabase: SupabaseClient,
|
|
userId: string,
|
|
arbetsgivare: string,
|
|
period: string
|
|
): Promise<{ ok: boolean; status: number; data?: { kvittensnummer?: string; tidpunkt?: string; signerare?: string } | null; error?: string }> {
|
|
const response = await skvRequest(
|
|
supabase,
|
|
userId,
|
|
'GET',
|
|
`${basePath(arbetsgivare, period)}/inlamnat`,
|
|
undefined,
|
|
{ baseUrl: getAgiApiBaseUrl() }
|
|
)
|
|
|
|
if (response.status === 404) {
|
|
return { ok: true, status: 404, data: null }
|
|
}
|
|
|
|
if (!response.ok) {
|
|
const text = await response.text()
|
|
return { ok: false, status: response.status, error: text }
|
|
}
|
|
|
|
const data = await response.json()
|
|
return { ok: true, status: response.status, data }
|
|
}
|