fix(salary): declare, book and pay AGI in whole kronor (SKV per-sats computation) (#1611)
* fix(salary): declare, book and pay AGI in whole kronor (SKV per-sats computation) A user's first lönekörning surfaced öre amounts in the AGI payable while Skatteverket deals in whole kronor. Three connected defects: - the AGI XML rounded amounts (Math.round); öretal bortfaller (SFF 2011:1261 22 kap. 1 §) requires truncation, and FK487 must be Skatteverket's own per-sats computation on the whole-krona underlag sums (IK587, kontroll B_006), not a truncation of the öre-exact engine sum - the salary booking credited 2731 with exact öre, leaving a residual after the whole-krona skattekonto draw; 2731 now carries the declared amount with the remainder on 3740 (Öres- och kronutjämning) - the LB payment file and TaxPaymentPanel paid/showed öre; they now use the declared whole-krona totals stored on agi_declarations (which also lets skattekonto auto-settlement match the draw); legacy öre rows keep paying öre-exact so pre-deploy bookings still clear 2731 New lib/salary/declared-avgifter.ts implements the SKV computation (per-IU whole-krona underlag, per-sats sums, youth/växa cap splits, exact integer math) shared by the AGI generator, the booking split and the preview. Review overrides route all legs through the same per-category truncation; basis overrides are inert on money totals (they never reach the filed IUs); the v1 book route gains override parity with book-run; F-skatt rows ignore avgifter overrides on every surface. Booked runs show their posted verifikat instead of a recomputed projection. tax_withheld_override requires whole kronor. Adversarially verified over three /skeptic rounds. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * chore: merge origin/main and re-ratchet the öre-round baseline The merge brought #1609 (net-pay öresavrundning) whose two new Math.round(x*100)/100 occurrences are counted against the baseline this branch had tightened from 637 to 629; 631 keeps the net -6 improvement without policing already-merged code. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(salary): address PR review (hybrid override computation, legacy youth cap, robustness) CodeRabbit round on #1611, all findings in one pass: - computeDeclaredAvgifterWithOverrides: one shared hybrid for the AGI generator AND the booking split. Overridden rows contribute their manual amounts per category; colleagues keep the SKV-exact per-sats underlag computation (a FoU override on one employee no longer costs the rest of the roster kronor of declared accuracy) - youth cap keys on the RESOLVED category so legacy null-category rows classified as youth by the rate heuristic still get the 25k split - F-skatt rows zero their avgifter_basis on both booking surfaces and in the preview, matching the AGI's isFSkattRow invariant - preview route: posted-voucher lookup errors return 500 instead of masquerading as a booked run with no vouchers; 400/500 tests added - run page clears stale AGI totals when the tax-payment fetch fails - SalaryOverridePanel truncates the tax override to whole kronor so the schema's .int() cannot bounce a decimal input with a 400 - v1 book route override parity pinned by a lifecycle test - DECISIONS.md format fixes + superseded entry marked; exempt category mapped explicitly; unified truncation-drift band with rationale Declined (recorded): dating the decision entries 2026-08-13 (bot assumed UTC; the decisions were made after midnight local time). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(salary): round-2 review nits (shared F-skatt helper, test hygiene) - isFSkattStatus in declared-avgifter.ts: single source for the F-skatt exclusion, consumed by book-run, the v1 book route, the preview route and the AGI generator, per the Swedish review's drift-risk finding - declared-avgifter test suite gets the standard beforeEach cleanup Declined (recorded for the summary): auto-generated correction voucher for regenerated legacy periods (data-repair follow-up needing Emil's go); SFF 22 kap. 1 par. citation doubt (verified against lagen.nu and already shipped in tax-tables.ts); 3740 scope doubt (BAS generic utjamning account, Visma praxis, matches the user's reference voucher). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> --------- Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Fable 5
parent
fbe4e18730
commit
4e14182a00
@@ -0,0 +1,181 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
||||
import { NextResponse } from 'next/server'
|
||||
import {
|
||||
createMockRequest,
|
||||
createMockRouteParams,
|
||||
createQueuedMockSupabase,
|
||||
} from '@/tests/helpers'
|
||||
|
||||
const { supabase: mockSupabase, enqueue, reset } = createQueuedMockSupabase()
|
||||
|
||||
const requireAuthMock = vi.fn()
|
||||
vi.mock('@/lib/auth/require-auth', () => ({
|
||||
requireAuth: (...args: unknown[]) => requireAuthMock(...args),
|
||||
}))
|
||||
|
||||
vi.mock('@/lib/init', () => ({ ensureInitialized: vi.fn() }))
|
||||
|
||||
vi.mock('@/lib/company/context', () => ({
|
||||
getActiveCompanyId: vi.fn().mockResolvedValue('company-1'),
|
||||
requireCompanyId: vi.fn().mockResolvedValue('company-1'),
|
||||
}))
|
||||
|
||||
import { GET } from '../route'
|
||||
|
||||
const CALCULATED_RUN = {
|
||||
id: 'run-1',
|
||||
company_id: 'company-1',
|
||||
status: 'review',
|
||||
period_year: 2026,
|
||||
period_month: 7,
|
||||
calculation_params: { slpRate: 0.2426, avgifterTotal: 0.3142 },
|
||||
}
|
||||
|
||||
const EMPLOYEE_ROW = {
|
||||
employee_id: 'emp-1',
|
||||
employee: { employment_type: 'employee' },
|
||||
gross_salary: 51158,
|
||||
tax_withheld: 12268,
|
||||
net_salary: 38890,
|
||||
avgifter_amount: 16073.84,
|
||||
avgifter_amount_override: null,
|
||||
avgifter_basis: 51158,
|
||||
avgifter_rate: 0.3142,
|
||||
avgifter_category: 'standard',
|
||||
vacation_accrual: 0,
|
||||
vacation_accrual_avgifter: 0,
|
||||
line_items: [],
|
||||
}
|
||||
|
||||
describe('GET /api/salary/runs/[id]/preview', () => {
|
||||
const mockUser = { id: 'user-1', email: 'test@test.se' }
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
reset()
|
||||
requireAuthMock.mockResolvedValue({ user: mockUser, supabase: mockSupabase, error: null })
|
||||
})
|
||||
|
||||
it('returns 401 when not authenticated', async () => {
|
||||
requireAuthMock.mockResolvedValue({
|
||||
user: null,
|
||||
supabase: mockSupabase,
|
||||
error: NextResponse.json({ error: 'Unauthorized' }, { status: 401 }),
|
||||
})
|
||||
const response = await GET(
|
||||
createMockRequest('/api/salary/runs/run-1/preview'),
|
||||
createMockRouteParams({ id: 'run-1' }),
|
||||
)
|
||||
expect(response.status).toBe(401)
|
||||
})
|
||||
|
||||
it('returns 404 for an unknown run', async () => {
|
||||
enqueue({ data: null, error: { message: 'not found' } })
|
||||
const response = await GET(
|
||||
createMockRequest('/api/salary/runs/run-1/preview'),
|
||||
createMockRouteParams({ id: 'run-1' }),
|
||||
)
|
||||
expect(response.status).toBe(404)
|
||||
})
|
||||
|
||||
it('returns 400 when the run has no calculated employees', async () => {
|
||||
enqueue({ data: CALCULATED_RUN }) // salary_runs
|
||||
enqueue({ data: [] }) // salary_run_employees: nothing calculated yet
|
||||
const response = await GET(
|
||||
createMockRequest('/api/salary/runs/run-1/preview'),
|
||||
createMockRouteParams({ id: 'run-1' }),
|
||||
)
|
||||
expect(response.status).toBe(400)
|
||||
})
|
||||
|
||||
it('returns 500 when the posted-voucher lookup fails for a booked run', async () => {
|
||||
enqueue({
|
||||
data: { ...CALCULATED_RUN, status: 'booked', salary_entry_id: 'je-1' },
|
||||
}) // salary_runs
|
||||
enqueue({ data: null, error: { message: 'rls denied' } }) // journal_entries
|
||||
const response = await GET(
|
||||
createMockRequest('/api/salary/runs/run-1/preview'),
|
||||
createMockRouteParams({ id: 'run-1' }),
|
||||
)
|
||||
// A failed lookup must not masquerade as "booked run with no vouchers".
|
||||
expect(response.status).toBe(500)
|
||||
})
|
||||
|
||||
it('previews the whole-krona 2731/3740 split for a calculated run', async () => {
|
||||
enqueue({ data: CALCULATED_RUN }) // salary_runs
|
||||
enqueue({ data: [EMPLOYEE_ROW] }) // salary_run_employees
|
||||
|
||||
const response = await GET(
|
||||
createMockRequest('/api/salary/runs/run-1/preview'),
|
||||
createMockRouteParams({ id: 'run-1' }),
|
||||
)
|
||||
expect(response.status).toBe(200)
|
||||
const { data } = await response.json()
|
||||
|
||||
expect(data.booked).toBeUndefined()
|
||||
const lines = data.avgifterEntry.lines as Array<{
|
||||
account_number: string
|
||||
debit_amount: number
|
||||
credit_amount: number
|
||||
}>
|
||||
expect(lines.find((l) => l.account_number === '7510')?.debit_amount).toBe(16073.84)
|
||||
expect(lines.find((l) => l.account_number === '2731')?.credit_amount).toBe(16073)
|
||||
expect(lines.find((l) => l.account_number === '3740')?.credit_amount).toBe(0.84)
|
||||
})
|
||||
|
||||
it('returns the ACTUAL posted verifikat for a booked run, voucher labels included', async () => {
|
||||
enqueue({
|
||||
data: {
|
||||
...CALCULATED_RUN,
|
||||
status: 'booked',
|
||||
salary_entry_id: 'je-1',
|
||||
avgifter_entry_id: 'je-2',
|
||||
vacation_entry_id: null,
|
||||
pension_entry_id: null,
|
||||
},
|
||||
}) // salary_runs
|
||||
enqueue({
|
||||
data: [
|
||||
{
|
||||
id: 'je-1',
|
||||
description: 'Lön 2026-07',
|
||||
voucher_series: 'A',
|
||||
voucher_number: 214,
|
||||
lines: [
|
||||
{ account_number: '7210', line_description: 'Lön', debit_amount: 51158, credit_amount: 0 },
|
||||
{ account_number: '2710', line_description: 'Personalskatt', debit_amount: 0, credit_amount: 12268 },
|
||||
{ account_number: '1930', line_description: 'Nettolön', debit_amount: 0, credit_amount: 38890 },
|
||||
],
|
||||
},
|
||||
{
|
||||
id: 'je-2',
|
||||
description: 'Lön 2026-07: Arbetsgivaravgifter',
|
||||
voucher_series: 'A',
|
||||
voucher_number: 215,
|
||||
// A pre-whole-krona legacy voucher: the view must show the posted
|
||||
// öre-exact lines, never a recomputed projection with 3740.
|
||||
lines: [
|
||||
{ account_number: '7510', line_description: 'Arbetsgivaravgifter', debit_amount: 16073.84, credit_amount: 0 },
|
||||
{ account_number: '2731', line_description: 'Arbetsgivaravgifter', debit_amount: 0, credit_amount: 16073.84 },
|
||||
],
|
||||
},
|
||||
],
|
||||
}) // journal_entries
|
||||
|
||||
const response = await GET(
|
||||
createMockRequest('/api/salary/runs/run-1/preview'),
|
||||
createMockRouteParams({ id: 'run-1' }),
|
||||
)
|
||||
expect(response.status).toBe(200)
|
||||
const { data } = await response.json()
|
||||
|
||||
expect(data.booked).toBe(true)
|
||||
expect(data.salaryEntry.description).toBe('Lön 2026-07 (A-214)')
|
||||
expect(data.avgifterEntry.description).toBe('Lön 2026-07: Arbetsgivaravgifter (A-215)')
|
||||
const avgifterLines = data.avgifterEntry.lines as Array<{ account_number: string; credit_amount: number }>
|
||||
expect(avgifterLines.find((l) => l.account_number === '2731')?.credit_amount).toBe(16073.84)
|
||||
expect(avgifterLines.some((l) => l.account_number === '3740')).toBe(false)
|
||||
expect(data.vacationEntry).toBeNull()
|
||||
expect(data.pensionEntry).toBeNull()
|
||||
})
|
||||
})
|
||||
@@ -2,6 +2,8 @@ import { NextResponse } from 'next/server'
|
||||
import { ensureInitialized } from '@/lib/init'
|
||||
import { withRouteContext } from '@/lib/api/with-route-context'
|
||||
import { SALARY_ACCOUNTS, getLineItemAccount } from '@/lib/salary/account-mapping'
|
||||
import { splitAvgifterLiability } from '@/lib/salary/salary-entries'
|
||||
import { isFSkattStatus } from '@/lib/salary/declared-avgifter'
|
||||
import { roundOre } from '@/lib/money'
|
||||
import type { CreateJournalEntryLineInput } from '@/types'
|
||||
|
||||
@@ -28,10 +30,78 @@ export const GET = withRouteContext<{ params: Promise<{ id: string }> }>(
|
||||
return NextResponse.json({ error: 'Lönekörning hittades inte' }, { status: 404 })
|
||||
}
|
||||
|
||||
// Booked/corrected runs return the ACTUAL posted verifikat instead of a
|
||||
// recomputed preview: a preview built by today's booking rules would
|
||||
// contradict an immutable voucher booked under earlier rules (e.g. the
|
||||
// 2731/3740 whole-krona split) exactly where users reconcile. Same
|
||||
// response shape, entries keyed by the run's entry ids, voucher labels
|
||||
// folded into the description.
|
||||
if (run.status === 'booked' || run.status === 'corrected') {
|
||||
const { data: posted, error: postedError } = await supabase
|
||||
.from('journal_entries')
|
||||
.select(
|
||||
'id, description, voucher_series, voucher_number, lines:journal_entry_lines(account_number, line_description, debit_amount, credit_amount)',
|
||||
)
|
||||
.eq('company_id', companyId)
|
||||
.eq('source_type', 'salary_payment')
|
||||
.eq('source_id', id)
|
||||
|
||||
// A failed lookup must not masquerade as "booked run with no vouchers".
|
||||
if (postedError) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Kunde inte läsa lönekörningens bokförda verifikat' },
|
||||
{ status: 500 },
|
||||
)
|
||||
}
|
||||
|
||||
const byId = new Map(
|
||||
((posted ?? []) as Array<{ id: string }>).map((e) => [e.id, e] as const),
|
||||
)
|
||||
const toEntry = (entryId: unknown) => {
|
||||
const entry = entryId ? (byId.get(entryId as string) as
|
||||
| {
|
||||
description: string
|
||||
voucher_series: string | null
|
||||
voucher_number: number | null
|
||||
lines: Array<{
|
||||
account_number: string
|
||||
line_description: string | null
|
||||
debit_amount: number | null
|
||||
credit_amount: number | null
|
||||
}>
|
||||
}
|
||||
| undefined) : undefined
|
||||
if (!entry) return null
|
||||
const voucher =
|
||||
entry.voucher_number != null
|
||||
? ` (${entry.voucher_series ?? ''}${entry.voucher_series ? '-' : ''}${entry.voucher_number})`
|
||||
: ''
|
||||
return {
|
||||
description: `${entry.description}${voucher}`,
|
||||
lines: entry.lines.map((l) => ({
|
||||
account_number: l.account_number,
|
||||
line_description: l.line_description ?? '',
|
||||
debit_amount: l.debit_amount,
|
||||
credit_amount: l.credit_amount,
|
||||
})),
|
||||
}
|
||||
}
|
||||
|
||||
return NextResponse.json({
|
||||
data: {
|
||||
booked: true,
|
||||
salaryEntry: toEntry(run.salary_entry_id),
|
||||
avgifterEntry: toEntry(run.avgifter_entry_id),
|
||||
vacationEntry: toEntry(run.vacation_entry_id),
|
||||
pensionEntry: toEntry(run.pension_entry_id),
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// Load employees with line items
|
||||
const { data: employees } = await supabase
|
||||
.from('salary_run_employees')
|
||||
.select('*, employee:employees(employment_type), line_items:salary_line_items(*)')
|
||||
.select('*, employee:employees(employment_type, f_skatt_status), line_items:salary_line_items(*)')
|
||||
.eq('salary_run_id', id)
|
||||
|
||||
if (!employees || employees.length === 0) {
|
||||
@@ -107,8 +177,44 @@ export const GET = withRouteContext<{ params: Promise<{ id: string }> }>(
|
||||
// mirroring the vacation/pension guards below. The bookkeeping engine never
|
||||
// posts an all-zero 7510/2731 voucher (see book/route.ts nollkörning path),
|
||||
// so previewing one would falsely imply a verifikat that is never created.
|
||||
const totalAvgifter = employees.reduce((sum, e) => sum + e.avgifter_amount, 0)
|
||||
const roundedAvgifter = Math.round(totalAvgifter * 100) / 100
|
||||
// Override-coalesced, like the booking (book-run.ts): the preview must
|
||||
// project the voucher that would actually post. F-skatt rows ignore
|
||||
// avgifter overrides and carry no underlag, matching book-run and the
|
||||
// AGI's isFSkattRow invariant.
|
||||
const isFSkattRow = (e: { employee?: { f_skatt_status?: string | null } | null }) =>
|
||||
isFSkattStatus(e.employee?.f_skatt_status)
|
||||
const totalAvgifter = employees.reduce(
|
||||
(sum, e) =>
|
||||
sum +
|
||||
((isFSkattRow(e) ? e.avgifter_amount : e.avgifter_amount_override ?? e.avgifter_amount) ||
|
||||
0),
|
||||
0,
|
||||
)
|
||||
const roundedAvgifter = roundOre(totalAvgifter)
|
||||
// Identical split to createAvgifterEntry (shared function): 2731 gets the
|
||||
// whole-krona amount Skatteverket computes from the underlag, the
|
||||
// remainder goes to 3740; the 7510 cost side stays exact.
|
||||
const { liabilityAvgifter, oresutjamning } = splitAvgifterLiability(
|
||||
{
|
||||
employees: (employees as Array<Record<string, unknown>>).map((sre) => {
|
||||
const fSkatt = isFSkattRow(sre as never)
|
||||
return {
|
||||
avgifter_amount:
|
||||
((fSkatt
|
||||
? (sre.avgifter_amount as number)
|
||||
: (sre.avgifter_amount_override as number | null) ??
|
||||
(sre.avgifter_amount as number)) || 0),
|
||||
avgifter_basis: fSkatt ? 0 : (sre.avgifter_basis as number | undefined),
|
||||
avgifter_rate: sre.avgifter_rate as number,
|
||||
avgifter_category: (sre.avgifter_category as string | null) ?? null,
|
||||
avgifter_amount_overridden:
|
||||
!fSkatt && (sre.avgifter_amount_override as number | null) != null,
|
||||
}
|
||||
}),
|
||||
calculation_params: run.calculation_params as Record<string, unknown> | null,
|
||||
},
|
||||
roundedAvgifter,
|
||||
)
|
||||
const avgifterLines: CreateJournalEntryLineInput[] = roundedAvgifter !== 0
|
||||
? [
|
||||
{
|
||||
@@ -117,12 +223,26 @@ export const GET = withRouteContext<{ params: Promise<{ id: string }> }>(
|
||||
credit_amount: 0,
|
||||
line_description: `${desc}: Arbetsgivaravgifter`,
|
||||
},
|
||||
{
|
||||
account_number: SALARY_ACCOUNTS.AVGIFTER_LIABILITY,
|
||||
debit_amount: 0,
|
||||
credit_amount: roundedAvgifter,
|
||||
line_description: `${desc}: Arbetsgivaravgifter`,
|
||||
},
|
||||
...(liabilityAvgifter !== 0 || oresutjamning === 0
|
||||
? [
|
||||
{
|
||||
account_number: SALARY_ACCOUNTS.AVGIFTER_LIABILITY,
|
||||
debit_amount: 0,
|
||||
credit_amount: liabilityAvgifter,
|
||||
line_description: `${desc}: Arbetsgivaravgifter`,
|
||||
} satisfies CreateJournalEntryLineInput,
|
||||
]
|
||||
: []),
|
||||
...(oresutjamning > 0
|
||||
? [
|
||||
{
|
||||
account_number: SALARY_ACCOUNTS.ORESUTJAMNING,
|
||||
debit_amount: 0,
|
||||
credit_amount: oresutjamning,
|
||||
line_description: `${desc}: Öres- och kronutjämning`,
|
||||
} satisfies CreateJournalEntryLineInput,
|
||||
]
|
||||
: []),
|
||||
]
|
||||
: []
|
||||
|
||||
|
||||
@@ -94,5 +94,44 @@ describe('GET /api/skatteverket/tax-payments/[period]/payment-file', () => {
|
||||
expect(response.headers.get('Content-Type')).toBe('text/plain; charset=iso-8859-1')
|
||||
expect(response.headers.get('Content-Disposition')).toContain('skatt-2026-04.txt')
|
||||
expect(mockGenerateBgLb).toHaveBeenCalledTimes(1)
|
||||
expect(mockGenerateBgLb.mock.calls[0][1]).toMatchObject({ amount: 1500 })
|
||||
})
|
||||
|
||||
it('pays the declared whole-krona totals as-is for new-era declarations', async () => {
|
||||
// Declarations generated since the whole-krona change store the declared
|
||||
// integers (what Skatteverket computes from the underlag and draws), and
|
||||
// the matching salary booking credited 2731 with the same number: the
|
||||
// payment must be exactly their sum.
|
||||
enqueue({ data: { id: 'agi-1', total_tax: 12268, total_avgifter: 16073 } }) // agi
|
||||
enqueue({ data: { name: 'Test AB', org_number: '5566778899' } }) // companies
|
||||
enqueue({ data: { bankgiro: '123-4567' } }) // company_settings
|
||||
enqueue({ data: null, error: null }) // update tax_payment_file_generated_at
|
||||
|
||||
const response = await GET(
|
||||
createMockRequest('/api/skatteverket/tax-payments/2026-04/payment-file'),
|
||||
createMockRouteParams({ period: '2026-04' }),
|
||||
)
|
||||
|
||||
expect(response.status).toBe(200)
|
||||
expect(mockGenerateBgLb.mock.calls[0][1]).toMatchObject({ amount: 28341 })
|
||||
})
|
||||
|
||||
it('keeps paying öre-exact for legacy öre-bearing declarations', async () => {
|
||||
// Legacy rows predate the whole-krona storage: their salary bookings
|
||||
// credited 2731 with the öre, so the payment keeps clearing 2731 in full
|
||||
// (the öre parks as a small skattekonto överskott, the pre-existing
|
||||
// equilibrium). Truncating here would strand the öre on 2731 instead.
|
||||
enqueue({ data: { id: 'agi-1', total_tax: 12268, total_avgifter: 16073.84 } }) // agi
|
||||
enqueue({ data: { name: 'Test AB', org_number: '5566778899' } }) // companies
|
||||
enqueue({ data: { bankgiro: '123-4567' } }) // company_settings
|
||||
enqueue({ data: null, error: null }) // update tax_payment_file_generated_at
|
||||
|
||||
const response = await GET(
|
||||
createMockRequest('/api/skatteverket/tax-payments/2026-04/payment-file'),
|
||||
createMockRouteParams({ period: '2026-04' }),
|
||||
)
|
||||
|
||||
expect(response.status).toBe(200)
|
||||
expect(mockGenerateBgLb.mock.calls[0][1]).toMatchObject({ amount: 28341.84 })
|
||||
})
|
||||
})
|
||||
|
||||
@@ -5,6 +5,7 @@ import { getErrorMessage } from '@/lib/errors/get-error-message'
|
||||
import { generateBankgiroPaymentBgLb } from '@/lib/salary/payment/bg-lb-generator'
|
||||
import { generateSkattekontoOcr, SKATTEKONTO_BANKGIRO } from '@/lib/skatteverket/skattekonto-ocr'
|
||||
import { validateBankgiroNumber } from '@/lib/bankgiro/luhn'
|
||||
import { roundOre } from '@/lib/money'
|
||||
|
||||
ensureInitialized()
|
||||
|
||||
@@ -51,7 +52,17 @@ export const GET = withRouteContext<{ params: Promise<{ period: string }> }>(
|
||||
)
|
||||
}
|
||||
|
||||
const totalAmount = Math.round((agi.total_tax + agi.total_avgifter) * 100) / 100
|
||||
// Declarations generated since the whole-krona change store the declared
|
||||
// amounts (what Skatteverket computes from the underlag and draws): pay
|
||||
// exactly those. Legacy öre-bearing rows predate that storage; their
|
||||
// salary bookings credited 2731 with the öre, so keep paying öre-exact as
|
||||
// before: the öre lands as a small skattekonto överskott (the pre-existing
|
||||
// equilibrium) instead of stranding on 2731 with no counterpart.
|
||||
const declaredWholeKronor =
|
||||
Number.isInteger(agi.total_tax) && Number.isInteger(agi.total_avgifter)
|
||||
const totalAmount = declaredWholeKronor
|
||||
? agi.total_tax + agi.total_avgifter
|
||||
: roundOre(agi.total_tax + agi.total_avgifter)
|
||||
if (totalAmount <= 0) {
|
||||
return NextResponse.json(
|
||||
{ error: `Inget belopp att betala för perioden ${period}.` },
|
||||
|
||||
@@ -487,6 +487,85 @@ describe('POST /salary-runs/:id/book', () => {
|
||||
)
|
||||
})
|
||||
|
||||
it('applies review overrides with book-run parity (tax/net reconciled, F-skatt avgifter override ignored)', async () => {
|
||||
// Overrides set during dashboard review must reach the ledger the same
|
||||
// way no matter which surface books the run: v1 previously ignored them,
|
||||
// so the booked 2710/2731 diverged from the AGI by the override delta.
|
||||
const overriddenRow = {
|
||||
...employeeRow,
|
||||
tax_withheld_override: 9000,
|
||||
avgifter_amount_override: 10000,
|
||||
avgifter_basis: 35000,
|
||||
avgifter_category: 'standard',
|
||||
}
|
||||
const fSkattRow = {
|
||||
...employeeRow,
|
||||
employee_id: 'emp_2',
|
||||
employee: { employment_type: 'employee', f_skatt_status: 'f_skatt' },
|
||||
gross_salary: 15000,
|
||||
tax_withheld: 0,
|
||||
net_salary: 15000,
|
||||
avgifter_amount: 0,
|
||||
// An avgifter override on an F-skatt row must be ignored (the AGI's
|
||||
// isFSkattRow invariant), and the underlag zeroed.
|
||||
avgifter_amount_override: 500,
|
||||
avgifter_basis: 15000,
|
||||
avgifter_category: 'standard',
|
||||
}
|
||||
mockServiceClient.mockReturnValue(
|
||||
makeFlexibleSupabase({
|
||||
company_members: { data: { company_id: COMPANY_ID, role: 'owner' }, error: null },
|
||||
salary_runs: [
|
||||
{ data: paidRun, error: null },
|
||||
{
|
||||
data: {
|
||||
id: RUN_ID, status: 'booked',
|
||||
booked_at: '2026-05-26T09:15:00Z', booked_by: USER_ID,
|
||||
salary_entry_id: 'je_salary', avgifter_entry_id: 'je_avg',
|
||||
vacation_entry_id: null, pension_entry_id: null,
|
||||
},
|
||||
error: null,
|
||||
},
|
||||
],
|
||||
salary_run_employees: { data: [overriddenRow, fSkattRow], error: null },
|
||||
idempotency_keys: { data: null, error: null },
|
||||
}),
|
||||
)
|
||||
mocks.checkPeriodLock.mockResolvedValue({ locked: false })
|
||||
mocks.createSalaryRunEntries.mockResolvedValue({
|
||||
salaryEntry: { id: 'je_salary', voucher_number: 'L2026-0024' },
|
||||
avgifterEntry: { id: 'je_avg' },
|
||||
vacationEntry: null,
|
||||
pensionEntry: null,
|
||||
})
|
||||
|
||||
const res = await book(
|
||||
makeRequest(`https://x.test/api/v1/companies/${COMPANY_ID}/salary-runs/${RUN_ID}/book`, {
|
||||
method: 'POST',
|
||||
}),
|
||||
detailParams(COMPANY_ID, RUN_ID),
|
||||
)
|
||||
|
||||
expect(res.status).toBe(200)
|
||||
const payload = mocks.createSalaryRunEntries.mock.calls[0][3] as {
|
||||
employees: Array<Record<string, unknown>>
|
||||
}
|
||||
const [regular, fSkatt] = payload.employees
|
||||
expect(regular).toMatchObject({
|
||||
tax_withheld: 9000,
|
||||
// net reconciles by the withheld difference: 25 500 + (9 500 - 9 000).
|
||||
net_salary: 26000,
|
||||
avgifter_amount: 10000,
|
||||
avgifter_amount_overridden: true,
|
||||
avgifter_basis: 35000,
|
||||
})
|
||||
expect(fSkatt).toMatchObject({
|
||||
avgifter_amount: 0,
|
||||
avgifter_amount_overridden: false,
|
||||
avgifter_basis: 0,
|
||||
})
|
||||
})
|
||||
|
||||
it('returns PERIOD_LOCKED before invoking the engine when payment_date is locked', async () => {
|
||||
mockServiceClient.mockReturnValue(
|
||||
makeFlexibleSupabase({
|
||||
|
||||
@@ -34,6 +34,7 @@ import { withApiV1 } from '@/lib/api/v1/with-api-v1'
|
||||
import { v1ErrorResponse, v1ErrorResponseFromCode } from '@/lib/api/v1/errors'
|
||||
import { checkPeriodLock } from '@/lib/api/v1/check-period-lock'
|
||||
import { createSalaryRunEntries } from '@/lib/salary/salary-entries'
|
||||
import { isFSkattStatus } from '@/lib/salary/declared-avgifter'
|
||||
import { syncVacationLedgerForEmployees } from '@/lib/salary/vacation-ledger'
|
||||
import { isBookkeepingError } from '@/lib/bookkeeping/errors'
|
||||
import { eventBus } from '@/lib/events'
|
||||
@@ -156,7 +157,7 @@ export const POST = withApiV1<{ params: Promise<{ companyId: string; id: string
|
||||
// 3. Load run + employees + line items for the engine.
|
||||
const { data: employees, error: empErr } = await ctx.supabase
|
||||
.from('salary_run_employees')
|
||||
.select('*, employee:employees(employment_type, default_dimensions), line_items:salary_line_items(*)')
|
||||
.select('*, employee:employees(employment_type, default_dimensions, f_skatt_status), line_items:salary_line_items(*)')
|
||||
.eq('salary_run_id', salaryRunId)
|
||||
if (empErr) {
|
||||
return v1ErrorResponse(empErr, ctx.log, { requestId: ctx.requestId })
|
||||
@@ -210,12 +211,20 @@ export const POST = withApiV1<{ params: Promise<{ companyId: string; id: string
|
||||
// 4. Engine call. Strict-mode: any throw aborts before status flip.
|
||||
type EmpRow = {
|
||||
employee_id: string
|
||||
employee: { employment_type: string; default_dimensions?: Record<string, string> } | null
|
||||
employee: {
|
||||
employment_type: string
|
||||
default_dimensions?: Record<string, string>
|
||||
f_skatt_status?: string | null
|
||||
} | null
|
||||
gross_salary: number
|
||||
tax_withheld: number
|
||||
tax_withheld_override: number | null
|
||||
net_salary: number
|
||||
avgifter_amount: number
|
||||
avgifter_amount_override: number | null
|
||||
avgifter_rate: number
|
||||
avgifter_basis: number
|
||||
avgifter_category: string | null
|
||||
vacation_accrual: number
|
||||
vacation_accrual_avgifter: number
|
||||
line_items: Array<{
|
||||
@@ -247,10 +256,28 @@ export const POST = withApiV1<{ params: Promise<{ companyId: string; id: string
|
||||
employee_id: sre.employee_id,
|
||||
employment_type: sre.employee?.employment_type || 'employee',
|
||||
gross_salary: sre.gross_salary,
|
||||
tax_withheld: sre.tax_withheld,
|
||||
net_salary: sre.net_salary,
|
||||
avgifter_amount: sre.avgifter_amount,
|
||||
// Override parity with book-run.ts: review overrides must reach
|
||||
// the ledger identically no matter which surface books the run,
|
||||
// or the booked 2731/2710 diverge from the AGI totals by the full
|
||||
// override delta. F-skatt rows ignore avgifter overrides (the AGI
|
||||
// hard-excludes them via isFSkattRow).
|
||||
tax_withheld: sre.tax_withheld_override ?? sre.tax_withheld,
|
||||
net_salary:
|
||||
sre.net_salary + (sre.tax_withheld - (sre.tax_withheld_override ?? sre.tax_withheld)),
|
||||
avgifter_amount:
|
||||
isFSkattStatus(sre.employee?.f_skatt_status)
|
||||
? sre.avgifter_amount
|
||||
: sre.avgifter_amount_override ?? sre.avgifter_amount,
|
||||
avgifter_rate: sre.avgifter_rate,
|
||||
// Declared-avgifter inputs: 2731 books the whole-krona amount
|
||||
// Skatteverket computes from the underlag (declared-avgifter.ts).
|
||||
// Zeroed for F-skatt rows, matching book-run and the AGI's
|
||||
// isFSkattRow invariant.
|
||||
avgifter_basis:
|
||||
isFSkattStatus(sre.employee?.f_skatt_status) ? 0 : sre.avgifter_basis,
|
||||
avgifter_category: sre.avgifter_category ?? null,
|
||||
avgifter_amount_overridden:
|
||||
!isFSkattStatus(sre.employee?.f_skatt_status) && sre.avgifter_amount_override != null,
|
||||
vacation_accrual: sre.vacation_accrual,
|
||||
vacation_accrual_avgifter: sre.vacation_accrual_avgifter,
|
||||
// Dimensions PR8: read-at-book from the employee row.
|
||||
|
||||
Reference in New Issue
Block a user