Files
accounted/lib/bokslut/dispositions-proposal-builder.ts
T
MattssonandClaude Fable 5 46b8e2bfea Fix/fable design (#1063)
* fix(bokslut): make dispositions storno-safe and derive fond math from opening balances

A reversed year_end voucher kept its storno in the income statement while
the original was excluded (source_type asymmetry), inflating resultat fore
dispositioner by exactly the reversed amount, and the posted-only fond
balance produced a phantom negative 212X that leaked a bogus aterforing
proposal. Support case: a user double-booked periodiseringsfond, reversed
both correctly, and the dispositions page still showed wrong numbers.

- trial-balance excludeYearEndClosing now also excludes entries chained to
  reversed year_end entries via reverses_id/correction_of_id (grammar
  verified against staging PostgREST)
- listExistingPeriodiseringsfonder counts posted+reversed so storno pairs
  cancel, and returns opening balances per fond
- schablonintakt per IL 30 kap 6a: opening balance base, rate = SLR per
  closing year (1.96% FY2025, 2.55% FY2026), replacing the wrong SLR+1pp
  0.0355 constant
- avsattning 25% cap is year-total: already-provisioned current-cohort
  growth consumes headroom in both preview and commit, so re-running the
  flow can no longer double-book the fond
- SLP posts before avsattning (deductible, shrinks the cap base) and is
  posted-aware: no double proposal or double count on resumed runs
- sumPostedYearEndDispositions counts correction replacements of reversed
  year_end entries and exposes the SLP portion

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

* refactor(bokslut): use roundOre for new fond/disposition rounding

Satisfies the naive-ore-round ratchet that tightened on main; identical
arithmetic, pinned by the existing exact-value tests.

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

* fix(bokslut): address PR #1063 review findings

- computeProposal receives the already-validated period row: a transient
  DB failure can no longer silently skip a requested disposition (and two
  redundant per-item period fetches are gone)
- getSchablonintaktRate fails closed for unmapped years instead of
  falling back to the latest known rate: statutory rates are never
  guessed; POST rate override remains the escape hatch
- listExistingPeriodiseringsfonder is opening-balance-entry aware:
  a fond carried via the OB entry booked by year-end closing was counted
  twice (once from history, once from the OB entry); balances now derive
  from OB + current-period activity when an OB entry exists
- periodStart is validated as a real calendar date, not just a shape
- reversed year_end correction targets resolve company-wide in
  sumPostedYearEndDispositions, matching the trial balance exclusion

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

---------

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-07-17 18:06:41 +02:00

271 lines
11 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import type { SupabaseClient } from '@supabase/supabase-js'
import { generateIncomeStatement } from '@/lib/reports/income-statement'
import { generateTrialBalance } from '@/lib/reports/trial-balance'
import {
calculateBolagsskatt,
sumPostedYearEndDispositions,
} from './tax-provision/bolagsskatt-calculator'
import { calculateSarskildLoneskatt } from './tax-provision/sarskild-loneskatt-calculator'
import {
computeLatentTax,
LATENT_TAX_EXPENSE_ACCOUNT,
LATENT_TAX_LIABILITY_ACCOUNT,
proposeLatentTaxChange,
} from './tax-provision/latent-tax-calculator'
import {
getPeriodiseringsfondCohortAccount,
getSchablonintaktRate,
listExistingPeriodiseringsfonder,
proposeAvsattning,
proposeAteforing,
} from './reserves/periodiseringsfond-service'
import type { DispositionsProposal, ProposedDisposition } from './types'
import type { AccountingFramework } from '@/types'
/**
* Shared core of the GET /bokslutsdispositioner endpoint, lifted out so the
* MCP tool can call the same builder without duplicating the proposal logic.
* The API route and the MCP tool both hand its output to the caller, who
* picks which proposals to commit via the POST endpoint.
*/
export async function buildDispositionsProposal(
supabase: SupabaseClient,
companyId: string,
fiscalPeriodId: string,
): Promise<DispositionsProposal> {
const { data: period, error: periodError } = await supabase
.from('fiscal_periods')
.select('id, name, period_start, period_end, opening_balance_entry_id')
.eq('id', fiscalPeriodId)
.eq('company_id', companyId)
.single()
if (periodError || !period) {
throw new Error('Fiscal period not found')
}
const { data: settings } = await supabase
.from('company_settings')
.select('entity_type')
.eq('company_id', companyId)
.maybeSingle()
const entityType = (settings?.entity_type ?? 'aktiebolag') as DispositionsProposal['entityType']
if (entityType !== 'aktiebolag') {
// Non-AB entities (enskild firma, handelsbolag, etc.) do not produce
// bookable bokslutsdispositioner: bolagsskatt, periodiseringsfond and
// SLP are AB-only mechanisms. EF tax mechanisms (egenavgifter,
// räntefördelning, periodiseringsfond-EF, expansionsfond) are
// declaration-only and surface through the dedicated
// /api/bookkeeping/fiscal-periods/[id]/ef-declaration endpoint and the
// EfDeclarationSection in the wizard: they never produce journal
// entries, so they have no place in this list.
const incomeStatement = await generateIncomeStatement(supabase, companyId, fiscalPeriodId)
return {
entityType,
fiscalPeriod: period,
netResultBefore: incomeStatement.net_result,
proposals: [],
}
}
// Look up the accounting framework: K3 (BFNAR 2012:1) triggers the
// uppskjuten-skatt provision step; K2 skips it.
const { data: companyRow } = await supabase
.from('companies')
.select('accounting_framework')
.eq('id', companyId)
.maybeSingle()
const accountingFramework: AccountingFramework =
(companyRow as { accounting_framework?: AccountingFramework } | null)?.accounting_framework
=== 'k3'
? 'k3'
: 'k2'
const fiscalYear = parseInt(period.period_end.slice(0, 4), 10)
const incomeStatement = await generateIncomeStatement(supabase, companyId, fiscalPeriodId)
const resultBeforeTax = incomeStatement.net_result
const proposals: ProposedDisposition[] = []
// Dispositions already POSTED in this period (a partially completed
// bokslut run) are excluded from resultBeforeTax like all year_end
// entries, but they do affect the taxable base: their signed P&L effect
// is folded into every base below so a re-visit previews the same
// amounts the commit path books.
const postedEffect = await sumPostedYearEndDispositions(
supabase,
companyId,
fiscalPeriodId,
)
const existingFonder = await listExistingPeriodiseringsfonder(
supabase,
companyId,
period.period_end,
period.period_start,
period.opening_balance_entry_id,
)
const ateforing = proposeAteforing(existingFonder, {
schablonintaktRate: getSchablonintaktRate(fiscalYear),
})
proposals.push(...ateforing.proposals)
const ateforingTotal = ateforing.proposals.reduce((sum, p) => sum + p.amount, 0)
// SLP already posted in this period (resumed run): don't re-propose it
// (that would book it twice) and don't subtract it twice below (its
// effect is already inside postedEffect.total).
const slp =
postedEffect.slpPortion !== 0
? null
: await calculateSarskildLoneskatt(supabase, companyId, fiscalPeriodId)
// An avsättning already booked in this bokslut eats into the 25 % cap;
// without this, revisiting the page after committing re-proposes the full
// avsättning and lets the user book it twice. Measured as the current
// cohort ACCOUNT's growth during the period (closing minus opening), so a
// prior-year fond that happens to share the account (shortened brutet
// räkenskapsår, decade wrap) does not consume this year's headroom.
const currentCohort = existingFonder.find(
(f) => f.account_number === getPeriodiseringsfondCohortAccount(fiscalYear),
)
const alreadyProvisioned = currentCohort
? Math.max(0, currentCohort.balance - Math.max(0, currentCohort.opening_balance))
: 0
// Cap base = skattemässigt resultat före avsättning: ledger result plus
// posted dispositions (with any posted avsättning added back: its
// headroom effect is alreadyProvisioned, not a base reduction), plus
// proposed återföringar and schablonintäkt, minus deductible SLP.
const taxableBeforeAvsattning =
resultBeforeTax + postedEffect.total + alreadyProvisioned + ateforingTotal
+ ateforing.schablonintaktAmount - (slp?.amount ?? 0)
const avsattning = proposeAvsattning({
skattemassigtResultatBeforeAvsattning: taxableBeforeAvsattning,
fiscalYear,
alreadyProvisioned,
})
if (avsattning) proposals.push(avsattning)
if (slp) proposals.push(slp)
// Bolagsskatt must be computed on the result AFTER the dispositions above.
// In preview mode nothing is posted yet, so the income statement still shows
// the pre-disposition result: we mirror each proposal's effect on resultat
// före skatt and hand the post-disposition base to the calculator:
// + återföring (8819, intäkt)
// − avsättning (8811, kostnad)
// − SLP (7533, kostnad)
// Without this, the previewed tax ignores the avsättning (tax too high) and
// diverges from what the sequential commit books and from ÅR/INK2.
const resultAfterDispositions =
resultBeforeTax + postedEffect.total + ateforingTotal
- (avsattning?.amount ?? 0) - (slp?.amount ?? 0)
const bolagsskatt = await calculateBolagsskatt(supabase, companyId, fiscalPeriodId, {
resultBeforeTaxOverride: resultAfterDispositions,
manualAdjustments: {
schablonintaktPeriodiseringsfond: ateforing.schablonintaktAmount,
},
})
if (bolagsskatt) proposals.push(bolagsskatt)
// K3 only: split obeskattade reserver into the 79.4 % equity portion and
// the 20.6 % uppskjuten skatteskuld. We sum the projected 21xx balance
// AFTER the dispositions above have been applied so the latent-tax
// amount reflects the closing position: anything else would diverge
// from the BR the user sees in the preview.
if (accountingFramework === 'k3') {
const latentTax = await buildLatentTaxProposal({
supabase,
companyId,
fiscalPeriodId,
proposalsBeforeLatentTax: proposals,
})
if (latentTax) proposals.push(latentTax)
}
return {
entityType,
fiscalPeriod: period,
netResultBefore: resultBeforeTax,
proposals,
}
}
/**
* Compose the K3 uppskjuten-skatt proposal.
*
* The latent tax provision must reflect the *closing* obeskattade-reserver
* balance, so we pull the current 21xx balance from the trial balance and
* adjust it for any 21xx-touching dispositions that haven't yet posted
* (avsättning ↑, återföring ↓). 2240's current balance is the existing
* provision; the delta becomes the new verifikat.
*/
export async function buildLatentTaxProposal(params: {
supabase: SupabaseClient
companyId: string
fiscalPeriodId: string
/** Optional: additional 21xx-touching dispositions that have NOT yet been
* posted but will be in the same batch. The TB already reflects everything
* posted, so leave this empty if the latent-tax run is sequenced after the
* 21xx postings (the API route's case). */
proposalsBeforeLatentTax?: ProposedDisposition[]
}): Promise<ProposedDisposition | null> {
const { supabase, companyId, fiscalPeriodId, proposalsBeforeLatentTax = [] } = params
const tb = await generateTrialBalance(supabase, companyId, fiscalPeriodId)
// 21xx: obeskattade reserver (credit-normal, so we measure credit − debit).
let untaxedReserves = tb.rows
.filter((r) => r.account_number.startsWith('21'))
.reduce((s, r) => s + (r.closing_credit - r.closing_debit), 0)
// Pending 21xx postings from the proposals that will commit alongside
// latent tax. Avsättning adds to the reserves (credit 21xx), återföring
// removes (debit 21xx).
for (const p of proposalsBeforeLatentTax) {
if (
p.kind !== 'periodiseringsfond_avsattning'
&& p.kind !== 'periodiseringsfond_ateforing'
) continue
for (const line of p.lines) {
if (!line.account_number.startsWith('21')) continue
untaxedReserves += (line.credit_amount ?? 0) - (line.debit_amount ?? 0)
}
}
// Current 2240 balance: credit-normal. Equal to existing latent tax.
const current2240 = tb.rows
.filter((r) => r.account_number === LATENT_TAX_LIABILITY_ACCOUNT)
.reduce((s, r) => s + (r.closing_credit - r.closing_debit), 0)
const split = computeLatentTax({ untaxedReserves })
const lines = proposeLatentTaxChange(current2240, split.liabilityPortion)
if (!lines) return null
const delta = Math.round((split.liabilityPortion - current2240) * 100) / 100
const amount = Math.abs(delta)
const direction = delta > 0 ? 'avsättning' : 'återföring'
return {
kind: 'uppskjuten_skatt',
label: 'Uppskjuten skatt (K3)',
description:
delta > 0
? `Avsättning till uppskjuten skatteskuld 20,6 % av obeskattade reserver. Debet ${LATENT_TAX_EXPENSE_ACCOUNT}, kredit ${LATENT_TAX_LIABILITY_ACCOUNT}.`
: `Återföring av uppskjuten skatteskuld när obeskattade reserver minskar. Debet ${LATENT_TAX_LIABILITY_ACCOUNT}, kredit ${LATENT_TAX_EXPENSE_ACCOUNT}.`,
amount,
lines,
warnings: [],
computation: {
untaxedReserves,
taxRate: 0.206,
target2240: split.liabilityPortion,
current2240,
delta,
direction,
equityPortion: split.equityPortion,
},
}
}