12c59399ee
* fix(bokslut): remove uppskjuten skatt on obeskattade reserver in juridisk person (K3 29.37) and confirm K3 to K2 reversion In juridisk person K3 29.37 keeps obeskattade reserver at gross; the 79.4/20.6 split belongs to koncernredovisning. The old disposition double-counted the tax portion (result charged twice, 2240 overstated on top of gross 21xx). Removes the proposal step, POST kind, UI case, K2-to-K3 account seeding and the interim framework gate; keeps LATENT_TAX_DEFAULT_RATE for analytical soliditet presentation. Also adds the K3-to-K2 consequence confirmation dialog in settings. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * docs(decisions): scope the batch log to shipped code and record the 29.37 election nuance Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(settings): stop promising deferred-tax accounting the engine no longer does The framework help text and the K2-to-K3 confirmation both told the user that switching to K3 means uppskjuten skatt is recognised separately on 2240/8940 with a 79.4/20.6 split. This PR removes exactly that behaviour, so the copy would have promised something the product does not do, which is the defect class this batch exists to remove. Both now describe what actually happens: kassaflodesanalys, komponentavskrivning and a wider note set, with obeskattade reserver carried gross per K3 29.37. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> --------- Co-authored-by: Jakob Wennberg <311770904+jakobwennberg-oss@users.noreply.github.com> Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
124 lines
3.9 KiB
TypeScript
124 lines
3.9 KiB
TypeScript
import { getActiveCompanyId } from '@/lib/company/context'
|
|
import { requireAuth } from '@/lib/auth/require-auth'
|
|
import { withRouteContext } from '@/lib/api/with-route-context'
|
|
import { validateBody } from '@/lib/api/validate'
|
|
import { AccountingFrameworkSchema } from '@/lib/api/schemas'
|
|
import { NextResponse } from 'next/server'
|
|
import { z } from 'zod'
|
|
import { getErrorMessage as getUserErrorMessage } from '@/lib/errors/get-error-message'
|
|
|
|
/**
|
|
* GET /api/company/current
|
|
*
|
|
* Returns the active company id for the authenticated user. Used by the
|
|
* client-side CompanyTabSync listener to detect cross-tab divergence (e.g.
|
|
* when a tab was hidden/backgrounded during a switch in another tab) and
|
|
* force a hard reload on mismatch.
|
|
*
|
|
* Never cached: the whole point is that the response reflects the current
|
|
* authoritative value in user_preferences.
|
|
*
|
|
* Uses requireAuth() directly (not withRouteContext): a null companyId is a
|
|
* valid answer here — the wrapper would short-circuit it into an error.
|
|
*/
|
|
export async function GET() {
|
|
const auth = await requireAuth()
|
|
if (auth.error) {
|
|
auth.error.headers.set('Cache-Control', 'private, no-store')
|
|
return auth.error
|
|
}
|
|
const { user, supabase } = auth
|
|
|
|
const companyId = await getActiveCompanyId(supabase, user.id)
|
|
|
|
return NextResponse.json(
|
|
{ companyId },
|
|
{ headers: { 'Cache-Control': 'private, no-store' } },
|
|
)
|
|
}
|
|
|
|
/**
|
|
* Body shape for PATCH /api/company/current.
|
|
*
|
|
* Currently only carries `accounting_framework` (K2 / K3). Adding more
|
|
* companies-level fields here is fine but anything that belongs on
|
|
* company_settings should go to /api/settings instead.
|
|
*/
|
|
const PatchBodySchema = z.object({
|
|
accounting_framework: AccountingFrameworkSchema.optional(),
|
|
})
|
|
|
|
/**
|
|
* PATCH /api/company/current
|
|
*
|
|
* Updates company-level fields (in the `companies` table) for the active
|
|
* company. Separate from /api/settings (which writes to `company_settings`)
|
|
* because the columns live on different tables.
|
|
*
|
|
* Currently scoped to `accounting_framework` (K2 / K3), only meaningful for
|
|
* entity_type='aktiebolag'. The handler rejects K3 for non-AB to prevent
|
|
* impossible chart-of-accounts states downstream.
|
|
*/
|
|
export const PATCH = withRouteContext(
|
|
'company.update_current',
|
|
async (request, ctx) => {
|
|
const { supabase, companyId } = ctx
|
|
|
|
const validation = await validateBody(request, PatchBodySchema)
|
|
if (!validation.success) return validation.response
|
|
|
|
const updates: Record<string, unknown> = {}
|
|
|
|
if (validation.data.accounting_framework !== undefined) {
|
|
// Only AB can opt in to K3; EF stays on the simpler EF rules and never
|
|
// touches K2/K3. Fetch the entity_type before applying.
|
|
const { data: company } = await supabase
|
|
.from('companies')
|
|
.select('entity_type')
|
|
.eq('id', companyId)
|
|
.single()
|
|
if (!company) {
|
|
return NextResponse.json(
|
|
{ error: 'Företaget kunde inte hittas' },
|
|
{ status: 404 },
|
|
)
|
|
}
|
|
if (
|
|
validation.data.accounting_framework === 'k3'
|
|
&& company.entity_type !== 'aktiebolag'
|
|
) {
|
|
return NextResponse.json(
|
|
{ error: 'K3 (BFNAR 2012:1) gäller endast aktiebolag.' },
|
|
{ status: 400 },
|
|
)
|
|
}
|
|
updates.accounting_framework = validation.data.accounting_framework
|
|
}
|
|
|
|
if (Object.keys(updates).length === 0) {
|
|
// Nothing to write: surface the current row so the client can refresh
|
|
// its local state without a no-op write.
|
|
const { data } = await supabase
|
|
.from('companies')
|
|
.select('id, accounting_framework, entity_type')
|
|
.eq('id', companyId)
|
|
.single()
|
|
return NextResponse.json({ data })
|
|
}
|
|
|
|
const { data, error } = await supabase
|
|
.from('companies')
|
|
.update(updates)
|
|
.eq('id', companyId)
|
|
.select('id, accounting_framework, entity_type')
|
|
.single()
|
|
|
|
if (error) {
|
|
return NextResponse.json({ error: getUserErrorMessage(error) }, { status: 500 })
|
|
}
|
|
|
|
return NextResponse.json({ data })
|
|
},
|
|
{ requireWrite: true },
|
|
)
|