Files
accounted/app/api/reports/vat-declaration/rc-basis-gaps/fix/route.ts
T
MattssonandClaude Opus 4.8 abe9ac9d8c Fix/attributes config (#926)
* fix(git): pin LF on generated extension registry and vitest snapshots

setup:extensions and vitest write these files with LF; with
core.autocrlf=true git expects CRLF and flags them as phantom
modifications on every dev/build run.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(security): enforce MFA on mcp-oauth consent and gate viewer storno route

mcp-oauth/authorize renders an HTML consent page and issues 303 redirects that withRouteContext cannot express, so it kept raw getUser() and thereby skipped the AAL2 gate: a password-only (AAL1) session could approve consent that mints a long-lived, MFA-bypassing API key. Add a route-local requireAal2() step-up on GET and POST; AAL1 sessions redirect to /mfa/verify, BankID users are exempt.

Separately, POST /api/reports/vat-declaration/rc-basis-gaps/fix calls correctEntry() (storno of a posted entry) but lacked requireWrite, so viewer-role members could trigger it. Add { requireWrite: true }.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(api): route transactions endpoints through withRouteContext

Migrate the transactions routes off hand-rolled supabase.auth.getUser() onto the MFA-enforcing withRouteContext wrapper; add requireWrite on mutating handlers (book, uncategorize, attach-document, ignore, batch-match, create-from-document). Behavior and response shapes preserved; tests updated to the wrapper mock pattern.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(api): route SIE import and bank reconciliation through withRouteContext

Migrate import/sie and reconciliation/bank routes onto the MFA-enforcing wrapper; requireWrite on mutations (import execute, create-accounts, mappings write verbs, link/unlink/run/mark-opening-balance). Reads (status, unmatched-entries) stay ungated. Response shapes preserved; tests added/updated to the wrapper mock pattern.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(api): route salary endpoints through withRouteContext

Migrate salary employees and runs routes (plus ku, payroll-config, tax-tables) onto the MFA-enforcing wrapper; requireWrite on mutations. Personnummer masking/encryption untouched; file downloads (AGI XML, payslip PDF, payment files) keep their headers. Two payment-file GETs retain requireWrite because they stamp *_file_generated_at and previously gated viewers. Tests added/updated to the wrapper mock pattern.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(api): route report endpoints through withRouteContext

Migrate the read-only report routes (trial balance, balansrapport, resultatrapport, income statement, ledgers, KPI, VAT declaration, salary journal, monthly breakdown, journal register, continuity check, full archive, etc.) onto the MFA-enforcing wrapper. All read-only, no requireWrite. JSON/XLSX/PDF/ZIP response bodies and headers preserved byte-for-byte; tests updated to the wrapper mock pattern.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(api): route invoices, skatteverket, agent and extension endpoints through withRouteContext

Migrate invoices, supplier-invoices, skatteverket tax-payments, and dynamic extension routes onto the MFA-enforcing wrapper with requireWrite on mutations. The two NDJSON streaming agent routes (invoke, onboarding/stream) use requireAuth() directly (the wrapper can't wrap a streaming response) so MFA is still enforced. skatteverket payment-file GET keeps requireWrite (stamps a generated-at field). Response shapes and file headers preserved; tests added/updated.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(api): route documents, events, team and account endpoints through withRouteContext

Migrate documents, events, kpi/preferences, vat/validate, support/contact onto the MFA-enforcing wrapper with requireWrite on mutations. account/password, team/accept and team/members use requireAuth() directly (user-level or pre-membership flows with no active company context) so MFA is still enforced. events keeps its dual API-key-or-session auth. Document retention guard untouched; tests added/updated.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(api): route settings and pending-operations endpoints through withRouteContext

Migrate settings (api-keys, oauth-clients, booking-templates, counterparty-templates, logo, company settings) and pending-operations (commit, bulk-commit, reject, edit-before-approve) onto the MFA-enforcing wrapper with requireWrite on mutations. Credential-guarding routes keep their per-user ownership filters. Response shapes preserved; tests added/updated to the wrapper mock pattern.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* chore(guards): ratchet raw-route-auth baseline 119->1 after A1 migration

Lock in the withRouteContext migration so the count cannot regress. The single remaining entry, mcp-oauth/authorize, is a documented exception (HTML consent + redirects, MFA enforced via route-local step-up). Record the campaign and requireWrite decisions in DECISIONS.md.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* feat(vat): add eSKD momsdeklaration file export for "Deklarera via fil"

Generate the Skatteverket eSKDUpload v6.0 XML file so users can file VAT by
upload instead of typing every ruta into the form. Extract buildFiledAmounts()
as the shared whole-krona source of truth (öre truncated per SFL 22 kap 1 §) so
the XML file and the manual-filing PDF can never disagree. Adds the /eskd API
route, an XML option in the report export menu, and the upload button on the
manual-filing card. Strings in sv + en.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* feat(vat): add 'vat_settlement' source type and update related components

* fix(booking): adjust search input layout and enable autofocus

* fix(vat): support 12-digit org numbers and adjust emission order for eSKD file

* fix(migration): add 'vat_settlement' to journal_entries.source_type CHECK

---------

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-07-08 09:54:46 +02:00

184 lines
7.3 KiB
TypeScript

import { NextResponse } from 'next/server'
import { z } from 'zod'
import { withRouteContext } from '@/lib/api/with-route-context'
import { validateBody } from '@/lib/api/validate'
import { errorResponseFromCode } from '@/lib/errors/get-structured-error'
import { correctEntry } from '@/lib/core/bookkeeping/storno-service'
import type { CreateJournalEntryLineInput, JournalEntryLine } from '@/types'
/**
* POST /api/reports/vat-declaration/rc-basis-gaps/fix
*
* Adds the missing basbelopp pair (44xx/45xx debit + 4598 credit) to a
* posted journal entry that has reverse-charge output VAT (2614/2624/2634)
* but no corresponding basis lines. Uses correctEntry() so the original
* voucher is preserved in compliance with BFL (storno + corrected entry).
*/
const SUPPLIER_TYPE = z.enum(['eu_business', 'non_eu_business', 'swedish_business'])
const SERVICE_OR_GOODS = z.enum(['service', 'goods'])
const FixGapSchema = z.object({
entryId: z.string().uuid(),
supplierType: SUPPLIER_TYPE,
supplyType: SERVICE_OR_GOODS,
})
const RC_OUTPUT_ACCOUNTS = new Set(['2614', '2624', '2634'])
const RATE_BY_OUTPUT: Record<string, number> = {
'2614': 0.25,
'2624': 0.12,
'2634': 0.06,
}
function pickBasisAccount(
outputAccount: string,
supplierType: 'eu_business' | 'non_eu_business' | 'swedish_business',
supplyType: 'service' | 'goods',
): { account: string; error?: undefined } | { account?: undefined; error: string } {
const rateIdx = outputAccount === '2614' ? 0 : outputAccount === '2624' ? 1 : outputAccount === '2634' ? 2 : -1
if (rateIdx < 0) return { error: 'Okänt RC-utgående konto.' }
// EU services 4535/4536/4537, EU goods 4515/4516/4517,
// non-EU services 4531/4532/4533, domestic services 4425/4426/4427,
// domestic goods 4415/4416/4417.
// Non-EU goods is NOT reverse charge: it's import VAT (ruta 50/60-62 via
// 4545-4547), a separate flow that doesn't belong on this correction path.
if (supplierType === 'eu_business' && supplyType === 'service') return { account: ['4535', '4536', '4537'][rateIdx] }
if (supplierType === 'eu_business' && supplyType === 'goods') return { account: ['4515', '4516', '4517'][rateIdx] }
if (supplierType === 'non_eu_business' && supplyType === 'service') return { account: ['4531', '4532', '4533'][rateIdx] }
if (supplierType === 'non_eu_business' && supplyType === 'goods') {
return {
error:
'Varor från leverantörer utanför EU hanteras som import (ruta 50/60-62), inte omvänd skattskyldighet. ' +
'Korrigera verifikationen manuellt med importmoms på 2615/4545.',
}
}
if (supplierType === 'swedish_business' && supplyType === 'service') return { account: ['4425', '4426', '4427'][rateIdx] }
if (supplierType === 'swedish_business' && supplyType === 'goods') return { account: ['4415', '4416', '4417'][rateIdx] }
return { error: 'Kunde inte välja basbeloppskonto för angiven leverantörstyp.' }
}
export const POST = withRouteContext(
'report.vat_declaration.rc_basis_gaps.fix',
async (request, ctx) => {
const { user, supabase, companyId, log, requestId } = ctx
const result = await validateBody(request, FixGapSchema)
if (!result.success) return result.response
const { entryId, supplierType, supplyType } = result.data
// Fetch the entry + its lines (RLS + explicit company filter)
const { data: entry, error: fetchErr } = await supabase
.from('journal_entries')
.select('id, status, lines:journal_entry_lines(*)')
.eq('id', entryId)
.eq('company_id', companyId)
.single()
if (fetchErr || !entry) {
return errorResponseFromCode('JOURNAL_ENTRY_NOT_FOUND', log, { requestId, details: { entryId } })
}
if (entry.status !== 'posted') {
return errorResponseFromCode('JOURNAL_ENTRY_NOT_FOUND', log, {
requestId,
details: { entryId, reason: `entry is ${entry.status}, expected posted` },
})
}
const originalLines = (entry.lines as JournalEntryLine[]) || []
// Identify the RC output account and amount (sum across multiple lines if any)
let outputAccount: string | null = null
let outputAmount = 0
for (const line of originalLines) {
if (RC_OUTPUT_ACCOUNTS.has(line.account_number)) {
const net = (Number(line.credit_amount) || 0) - (Number(line.debit_amount) || 0)
if (net > 0) {
if (outputAccount && outputAccount !== line.account_number) {
return errorResponseFromCode('VAT_REPORT_GENERATION_FAILED', log, {
requestId,
details: {
reason: 'Verifikationen har RC-moms på flera räntesatser. Korrigera manuellt.',
},
})
}
outputAccount = line.account_number
outputAmount += net
}
}
}
if (!outputAccount || outputAmount <= 0) {
return errorResponseFromCode('VAT_REPORT_GENERATION_FAILED', log, {
requestId,
details: { reason: 'Ingen RC-utgående moms hittades i verifikationen.' },
})
}
const rate = RATE_BY_OUTPUT[outputAccount]
const pick = pickBasisAccount(outputAccount, supplierType, supplyType)
if (!pick.account) {
return errorResponseFromCode('VAT_REPORT_GENERATION_FAILED', log, {
requestId,
details: { reason: pick.error ?? 'Kunde inte välja basbeloppskonto.' },
})
}
const basisAccount: string = pick.account
const basisAmount = Math.round((outputAmount / rate) * 100) / 100
const rateLabel = `${Math.round(rate * 100)}%`
// Build corrected lines = original lines + basis pair (44xx debit + 4598 credit)
const correctedLines: CreateJournalEntryLineInput[] = [
...originalLines.map((l) => {
const line: CreateJournalEntryLineInput = {
account_number: l.account_number,
debit_amount: Number(l.debit_amount) || 0,
credit_amount: Number(l.credit_amount) || 0,
}
if (l.currency) line.currency = l.currency
if (l.amount_in_currency != null) line.amount_in_currency = Number(l.amount_in_currency)
if (l.exchange_rate != null) line.exchange_rate = Number(l.exchange_rate)
if (l.line_description) line.line_description = l.line_description
if (l.tax_code) line.tax_code = l.tax_code
if (l.cost_center) line.cost_center = l.cost_center
if (l.project) line.project = l.project
return line
}),
{
account_number: basisAccount,
debit_amount: basisAmount,
credit_amount: 0,
line_description: `Basbelopp omvänd skattskyldighet ${rateLabel}`,
},
{
account_number: '4598',
debit_amount: 0,
credit_amount: basisAmount,
line_description: `Motkonto beräknad omvänd moms ${rateLabel}`,
},
]
try {
const correction = await correctEntry(supabase, companyId, user.id, entryId, correctedLines)
return NextResponse.json({
data: {
reversalId: correction.reversal.id,
correctedId: correction.corrected.id,
basisAccount,
basisAmount,
},
})
} catch (err) {
log.error('rc-basis-gap fix failed', err as Error, { entryId })
return errorResponseFromCode('VAT_REPORT_GENERATION_FAILED', log, {
requestId,
details: { reason: err instanceof Error ? err.message : 'unknown' },
})
}
},
{ requireWrite: true },
)