* feat(company): ideell förening as a third legal form, behind a flag (#2072 step 1) Why the problem occurred: the legal form was modelled as a binary flag in ~300 files. `EntityType` was a two-member union, but nothing dispatched on it exhaustively: 28 sites defaulted `?? 'enskild_firma'` (invoice, categorize, match, stripe, invoice-inbox) or `?? 'aktiebolag'` (year-end, bokslut, MCP), and every form-dependent choice was an `=== 'aktiebolag' ? A : B` ternary. Widening the union compiled everywhere and changed nothing, so a förening would have booked as an enskild firma in the app and as an aktiebolag in bokslut and MCP, with no error anywhere. The lookup refused föreningar at the door (mapEntityType returned null), which is what the tester hit. What was removed or simplified: the silent defaults. One module, lib/company/entity-type.ts, now holds the list (ENTITY_TYPES), the parser (never defaults), the resolver (settings hint, then companies.entity_type, then throw) and `byEntityType`, whose Record arms make the compiler refuse the next widening until each site has an answer. The form-dependent facts (closing account, owner settlement account, calendar-year lock, default method, K1/K2 label, personnummer vs 16-prefix) live there once instead of in the ternaries. On the SQL side supported_entity_types() replaces four copies of the literal list in the create RPCs. Why this shape and not the proposed one: the tracker asked for the enum widening plus a chart; that alone was the dangerous version (compiles, books wrong). Bundling stiftelse was considered and dropped: identical plumbing but no chart block. Creation sits behind NEXT_PUBLIC_IDEELL_FORENING_ENABLED so the CHECK, RPCs and seed can ship now and the first partner is switched on without a migration; the flag goes when Phase 2 (packs, INK3, årsbokslut, Swish) lands on the tracker. Domain choices (DECISIONS.md 2026-09-08, verify with an accountant before Phase 2): result closes to 2069 with 2068 as prior-year carry; no owner accounts, member settlement on 2890; accrual default; brutet räkenskapsår allowed; K1 label for the 5 000 kr accrual threshold (BFNAR 2010:1); org number gets the 16 prefix. Migration 20260908110835 widens the three CHECK constraints, adds supported_entity_types(), re-creates the three create RPCs with the widened guard and adds the förening block to seed_chart_of_accounts. Applied to staging and covered by ideell-forening-entity-type.pg.test.ts. Part of #2072 Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01PdGafpUA7jVV1oYjkwfQCh * fix(company): close the förening paths the skeptic refuted (#2072) Five refutations from the /skeptic pass on 7a05c54d2, each fixed at the shared definition rather than the reported site: 1. Privately paid supplier invoices and the utlägg dialog resolved the owner account in lib/expenses/payer.ts with its own AB/EF ternary, so a förening member's invoice was built on 2893 and then refused by the expense-claim service (which already said 2890), burning an ankomstnummer. The helper now uses ownerSettlementAccount. 2. Booking templates substitute their `_ab` accounts only for an aktiebolag; the `private_expense` template kept its base 2013 for a förening. Template accounts now resolve through templateAccountForForm: EF base, AB override, förening base with owner accounts translated to 2890 (booking-templates.ts and proposal-lines.ts share it). 3. A VAT-registered förening with helårsmoms got no momsdeklaration deadline: the annual VAT rule bailed on anything but AB/EF. A förening is a juridisk person and follows the räkenskapsår schedule (SFL 26 kap 33 §), so the rule now keys on fiscalYearLockedToCalendar instead of the two literals; same in the MCP VAT report. 4. 2069 would have accumulated across years: the year-open omföring was AB-only with 2099/2098 hard-coded. planResultAppropriation now takes the pair from resultClosingAccounts (AB 2099 -> 2098, förening 2069 -> 2068) and skips forms with no carry (EF). 5. With the flag off, a registry lookup that returned "Ideell förening" was prefilled into the onboarding journey, the form picker was skipped and the create step answered "Ogiltig företagsform" with no way back. The journey, the BankID picker, the onboarding page and the MCP lookup now use mapSetupEntityType, which maps only creatable forms, so a flagged-off form falls through to the picker as before. Also: form picker keeps its AB-first order; tests for each fix. Part of #2072 Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01PdGafpUA7jVV1oYjkwfQCh * chore(migrations): move ideell förening migration after main's latest version (20260908143051) Two migrations landed on main after the branch forked; a lower version would be skipped by the merge-time apply. Staging history row renamed to match. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01PdGafpUA7jVV1oYjkwfQCh * chore(skills): regenerate accounted-api reference for the widened entity_type enum Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01PdGafpUA7jVV1oYjkwfQCh --------- Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
372 lines
16 KiB
TypeScript
372 lines
16 KiB
TypeScript
/**
|
|
* GET /api/transactions/[id]/match-invoice/preview?invoice_id=...
|
|
*
|
|
* Returns the journal entry lines that match-invoice would create for this
|
|
* (transaction, invoice) pair. Read-only: does not stage or write anything.
|
|
*
|
|
* The shape mirrors the routing decision in the POST handler: if the invoice
|
|
* was already booked (invoice.journal_entry_id is set, i.e. 1510 is on the
|
|
* books), we preview the clearing entry (Dr <resolved account> / Cr 1510).
|
|
* Only when the invoice was never booked AND the company is on kontantmetoden
|
|
* AND the receipt fully pays the invoice do we preview the cash entry
|
|
* (Dr <resolved account> / Cr 30xx / Cr 26xx).
|
|
*
|
|
* The bank leg is resolved from THIS transaction's own cash_account_id via
|
|
* resolveSettlementAccount, never hardcoded to 1930, so the preview stays
|
|
* byte-identical to what the POST handler commits (mirrors the fix already
|
|
* applied on the supplier-invoice side).
|
|
*
|
|
* The UI uses this to show the user the exact lines before they confirm:
|
|
* the lack of any preview was part of the reported bug.
|
|
*/
|
|
import { NextResponse } from 'next/server'
|
|
import { resolveCompanyEntityType } from '@/lib/company/entity-type'
|
|
import { withRouteContext } from '@/lib/api/with-route-context'
|
|
import { errorResponseFromCode } from '@/lib/errors/get-structured-error'
|
|
import { cashPartialBlockReason } from '@/lib/bookkeeping/booking-mode'
|
|
import { resolveSekAmount } from '@/lib/bookkeeping/currency-utils'
|
|
import { roundOre, ORE_ROUNDING_SETTLEMENT_MAX } from '@/lib/money'
|
|
import { getRevenueAccount, getOutputVatAccount } from '@/lib/bookkeeping/invoice-entries'
|
|
import { buildInvoicePaymentClearingLines } from '@/lib/bookkeeping/invoice-payment-lines'
|
|
import { resolveSettlementAccount } from '@/lib/bookkeeping/settlement-account'
|
|
import { fetchExchangeRate } from '@/lib/currency/riksbanken'
|
|
import type { Currency, EntityType, Invoice, InvoiceItem } from '@/types'
|
|
import { z } from 'zod'
|
|
|
|
type PreviewLine = {
|
|
account_number: string
|
|
debit_amount: number
|
|
credit_amount: number
|
|
description: string
|
|
}
|
|
|
|
const QuerySchema = z.object({
|
|
invoice_id: z.string().uuid(),
|
|
})
|
|
|
|
export const GET = withRouteContext(
|
|
'transaction.match_invoice_preview',
|
|
async (request, ctx, { params }: { params: Promise<{ id: string }> }) => {
|
|
const { id: transactionId } = await params
|
|
const { supabase, companyId, log, requestId } = ctx
|
|
|
|
const url = new URL(request.url)
|
|
const parsed = QuerySchema.safeParse({ invoice_id: url.searchParams.get('invoice_id') })
|
|
if (!parsed.success) {
|
|
return errorResponseFromCode('VALIDATION_ERROR', log, {
|
|
requestId,
|
|
details: { field: 'invoice_id', message: 'invoice_id must be a UUID' },
|
|
})
|
|
}
|
|
const { invoice_id } = parsed.data
|
|
|
|
// Data minimization (GDPR Art.5(1)(c)): amount_sek + exchange_rate are
|
|
// pulled because buildInvoicePaymentClearingLines needs them for the
|
|
// cross-currency bank-leg math (round-7 FX fix). cash_account_id resolves
|
|
// which BAS account this bank line actually settles into, mirroring the
|
|
// POST handler's settlement-account lookup. All other columns would
|
|
// broaden the projection without serving the preview's purpose.
|
|
const { data: transaction, error: txErr } = await supabase
|
|
.from('transactions')
|
|
.select('id, date, amount, amount_sek, currency, exchange_rate, cash_account_id')
|
|
.eq('id', transactionId)
|
|
.eq('company_id', companyId)
|
|
.single()
|
|
if (txErr || !transaction) {
|
|
return errorResponseFromCode('TX_CATEGORIZE_TX_NOT_FOUND', log, { requestId })
|
|
}
|
|
|
|
const { data: invoice, error: invErr } = await supabase
|
|
.from('invoices')
|
|
.select('*, items:invoice_items(*)')
|
|
.eq('id', invoice_id)
|
|
.eq('company_id', companyId)
|
|
.single()
|
|
if (invErr || !invoice) {
|
|
return errorResponseFromCode('MATCH_INVOICE_NOT_FOUND', log, { requestId })
|
|
}
|
|
|
|
const { data: settings } = await supabase
|
|
.from('company_settings')
|
|
.select('accounting_method, entity_type')
|
|
.eq('company_id', companyId)
|
|
.single()
|
|
|
|
const accountingMethod = settings?.accounting_method || 'accrual'
|
|
const entityType: EntityType = await resolveCompanyEntityType(supabase, companyId, settings?.entity_type)
|
|
|
|
// Same resolution as the POST handler: debit the cash account this
|
|
// transaction is actually linked to, never a hardcoded 1930, so the
|
|
// preview stays byte-identical to what gets committed.
|
|
const paymentAccount = await resolveSettlementAccount(
|
|
supabase,
|
|
companyId!,
|
|
transaction.cash_account_id,
|
|
log,
|
|
)
|
|
|
|
// Cross-currency FX preview. When tx.currency !== invoice.currency we fetch
|
|
// the Riksbanken spot rate for invoice.currency on the tx date and surface
|
|
// the conversion to the dialog (the user sees the rate + invoice-currency-
|
|
// equivalent before approving). The committed verifikat uses the same
|
|
// numbers; the route POST handler re-runs the lookup so the rate at
|
|
// commit time is authoritative.
|
|
//
|
|
// This MUST run BEFORE the paid / remaining / fully-paid math below.
|
|
// invoice.remaining_amount and invoice.total are denominated in INVOICE
|
|
// currency, so a SEK bank tx has to be converted first. Computing the
|
|
// comparison from the raw SEK amount made a 1 000 SEK payment look like
|
|
// it fully cleared a 140 USD invoice (newRemaining went negative →
|
|
// isFullyPaid=true), which for a cash-method unbooked invoice previewed a
|
|
// cash entry (Dr 1930 / Cr 30xx) that the POST: which converts first:
|
|
// would never commit (it posts the clearing entry Dr 1930 / Cr 1510).
|
|
//
|
|
// Per ML 8 kap 21-23§ the rate effective on the payment date is the
|
|
// correct conversion. If the lookup fails (Riksbanken outage, missing
|
|
// rate for that date), the response carries `fx_conversion.error` and
|
|
// the dialog can surface a manual-rate input field instead.
|
|
type FxConversion =
|
|
| {
|
|
required: true
|
|
tx_currency: string
|
|
invoice_currency: string
|
|
rate: number
|
|
rate_date: string
|
|
paid_in_invoice_currency: number
|
|
}
|
|
| { required: true; error: 'rate_unavailable'; tx_currency: string; invoice_currency: string }
|
|
| { required: false }
|
|
|
|
// Mirrors the POST handler's guard. transactions.amount is denominated in
|
|
// transactions.currency; the SEK value lives in amount_sek (pre-computed at
|
|
// ingest) or is derivable from exchange_rate. A foreign row carrying
|
|
// neither (the shape a row gets when the Riksbanken lookup failed at
|
|
// ingest, see lib/transactions/ingest.ts) has no establishable SEK value,
|
|
// and the raw foreign number must never stand in for one: the dialog would
|
|
// preview a 500 USD receipt as a 500 SEK verifikat. Refuse here too so the
|
|
// preview never shows lines the POST would reject.
|
|
const txIsForeign = !!transaction.currency && transaction.currency !== 'SEK'
|
|
if (
|
|
txIsForeign &&
|
|
transaction.amount_sek == null &&
|
|
!(transaction.exchange_rate != null && transaction.exchange_rate > 0)
|
|
) {
|
|
return errorResponseFromCode('MATCH_INVOICE_TX_FX_RATE_MISSING', log, {
|
|
requestId,
|
|
details: {
|
|
transactionCurrency: transaction.currency,
|
|
transactionDate: transaction.date,
|
|
},
|
|
})
|
|
}
|
|
// Actual SEK that hit the bank, resolved through the same helper
|
|
// buildInvoicePaymentClearingLines uses for the bank leg. SEK rows return
|
|
// Math.abs(amount) unchanged.
|
|
const txAbsSek =
|
|
Math.round(
|
|
resolveSekAmount(
|
|
Math.abs(transaction.amount),
|
|
transaction.amount_sek != null ? Math.abs(transaction.amount_sek) : null,
|
|
transaction.currency,
|
|
transaction.exchange_rate,
|
|
) * 100,
|
|
) / 100
|
|
|
|
let fxConversion: FxConversion = { required: false }
|
|
if (transaction.currency !== invoice.currency) {
|
|
const rateInfo = await fetchExchangeRate(
|
|
invoice.currency as Currency,
|
|
new Date(transaction.date),
|
|
)
|
|
if (rateInfo && rateInfo.rate > 0) {
|
|
// bankSek / rate = how many units of invoice.currency this payment
|
|
// satisfies. Round to 4 decimal places to preserve precision through
|
|
// subsequent partial-payment accumulations.
|
|
const paidInInvoiceCurrency =
|
|
Math.round((txAbsSek / rateInfo.rate) * 10000) / 10000
|
|
fxConversion = {
|
|
required: true,
|
|
tx_currency: transaction.currency,
|
|
invoice_currency: invoice.currency,
|
|
rate: rateInfo.rate,
|
|
rate_date: rateInfo.date,
|
|
paid_in_invoice_currency: paidInInvoiceCurrency,
|
|
}
|
|
} else {
|
|
fxConversion = {
|
|
required: true,
|
|
error: 'rate_unavailable',
|
|
tx_currency: transaction.currency,
|
|
invoice_currency: invoice.currency,
|
|
}
|
|
}
|
|
}
|
|
|
|
// paidAmount is denominated in INVOICE currency so the remaining /
|
|
// fully-paid comparison is like-with-like. Same-currency → tx.amount.
|
|
// Cross-currency with a resolved rate → the spot-rate conversion (mirrors
|
|
// the POST handler's paidAmountInInvoiceCurrency).
|
|
const paidAmount =
|
|
fxConversion.required && !('error' in fxConversion)
|
|
? fxConversion.paid_in_invoice_currency
|
|
: transaction.amount
|
|
const currentRemaining =
|
|
invoice.remaining_amount ?? invoice.total - (invoice.paid_amount || 0)
|
|
const newRemaining = Math.max(
|
|
0,
|
|
Math.round((currentRemaining - paidAmount) * 100) / 100,
|
|
)
|
|
// A rate-unavailable cross-currency payment can't be resolved to invoice
|
|
// currency yet, so never report fully-paid (or preview the cash shape) on
|
|
// a guess: the dialog blocks confirm until a manual rate is entered and
|
|
// the POST recomputes the real figure.
|
|
const fxRateUnavailable = fxConversion.required && 'error' in fxConversion
|
|
// Pure-SEK whole-krona settlements absorb a sub-krona remainder as
|
|
// öresavrundning (3740) and settle in full: mirror that here so the
|
|
// preview's fully-paid signal matches the committed verifikat.
|
|
const pureSek = transaction.currency === 'SEK' && invoice.currency === 'SEK'
|
|
const isFullyPaid =
|
|
!fxRateUnavailable &&
|
|
(newRemaining <= 0 || (pureSek && newRemaining < ORE_ROUNDING_SETTLEMENT_MAX))
|
|
|
|
const invoiceAlreadyBooked = !!(invoice as { journal_entry_id?: string | null }).journal_entry_id
|
|
const useCashEntry = !invoiceAlreadyBooked && accountingMethod === 'cash' && isFullyPaid
|
|
|
|
// The POST handler rejects cash-method partials and part-paid completions
|
|
// for never-booked invoices, so refuse to preview lines it will never
|
|
// book. Skipped while the FX rate is unresolved: the dialog must still
|
|
// render to collect a manual rate, and the POST recomputes the real check.
|
|
if (!fxRateUnavailable) {
|
|
const cashBlock = cashPartialBlockReason({
|
|
invoiceAlreadyBooked,
|
|
accountingMethod,
|
|
priorPaidAmount: (invoice as { paid_amount?: number | null }).paid_amount,
|
|
paysRemainingInFull: isFullyPaid,
|
|
})
|
|
if (cashBlock) {
|
|
return errorResponseFromCode('INVOICE_PAID_CASH_PARTIAL_UNSUPPORTED', log, {
|
|
requestId,
|
|
details: { reason: cashBlock },
|
|
})
|
|
}
|
|
}
|
|
|
|
const lines: PreviewLine[] = []
|
|
let entryType: 'clearing' | 'cash' = 'clearing'
|
|
|
|
if (useCashEntry) {
|
|
entryType = 'cash'
|
|
// Mirror createInvoiceCashEntry: per-rate revenue + VAT credits, 1930 debit.
|
|
const inv = invoice as Invoice & { items?: InvoiceItem[] }
|
|
const items = inv.items ?? []
|
|
const isForeign = inv.currency !== 'SEK'
|
|
|
|
// Per-item rate aggregation (matches generatePerRateLines semantics).
|
|
// InvoiceItem.line_total is the NET line amount (EXCLUDES VAT): it sums
|
|
// to invoice.subtotal, and each line's vat_amount = line_total * rate. The
|
|
// commit path (generatePerRateLines) credits revenue with line_total
|
|
// directly; subtracting vat here double-subtracts VAT and unbalances the
|
|
// previewed verifikat against the 1930 debit (inv.total).
|
|
const byRate = new Map<number, { subtotal: number; vat: number }>()
|
|
if (items.length > 0) {
|
|
for (const it of items) {
|
|
const rate = it.vat_rate ?? 25
|
|
const itemVat = resolveSekAmount(it.vat_amount, null, inv.currency, inv.exchange_rate)
|
|
const itemTotal = resolveSekAmount(it.line_total, null, inv.currency, inv.exchange_rate)
|
|
const sub = roundOre(itemTotal)
|
|
const bucket = byRate.get(rate) ?? { subtotal: 0, vat: 0 }
|
|
bucket.subtotal += sub
|
|
bucket.vat += itemVat
|
|
byRate.set(rate, bucket)
|
|
}
|
|
} else {
|
|
// Fallback to invoice-level totals
|
|
const sub = resolveSekAmount(inv.subtotal, inv.subtotal_sek, inv.currency, inv.exchange_rate)
|
|
const vat = resolveSekAmount(inv.vat_amount, inv.vat_amount_sek, inv.currency, inv.exchange_rate)
|
|
byRate.set(inv.vat_rate ?? 25, { subtotal: sub, vat })
|
|
}
|
|
|
|
const creditLines: PreviewLine[] = []
|
|
for (const [rate, totals] of byRate) {
|
|
const vatTreatment = totals.vat > 0
|
|
? (rate === 25 ? 'standard_25' : rate === 12 ? 'reduced_12' : rate === 6 ? 'reduced_6' : inv.vat_treatment)
|
|
: inv.vat_treatment
|
|
const revenueAcct = getRevenueAccount(vatTreatment, entityType)
|
|
creditLines.push({
|
|
account_number: revenueAcct,
|
|
debit_amount: 0,
|
|
credit_amount: Math.round(totals.subtotal * 100) / 100,
|
|
description: `Försäljning ${rate}%`,
|
|
})
|
|
if (totals.vat > 0) {
|
|
creditLines.push({
|
|
account_number: getOutputVatAccount(vatTreatment),
|
|
debit_amount: 0,
|
|
credit_amount: Math.round(totals.vat * 100) / 100,
|
|
description: `Utgående moms ${rate}%`,
|
|
})
|
|
}
|
|
}
|
|
|
|
const totalCredits = creditLines.reduce((s, l) => s + l.credit_amount, 0)
|
|
const cashDebit = isForeign
|
|
? Math.round(totalCredits * 100) / 100
|
|
: resolveSekAmount(inv.total, inv.total_sek, inv.currency, inv.exchange_rate)
|
|
|
|
lines.push({
|
|
account_number: paymentAccount,
|
|
debit_amount: Math.round(cashDebit * 100) / 100,
|
|
credit_amount: 0,
|
|
description: 'Inbetalning från bank',
|
|
})
|
|
lines.push(...creditLines)
|
|
} else {
|
|
// Clearing entry. Delegates to the shared helper so the preview and
|
|
// the committed verifikat are byte-identical: fixing the prior
|
|
// bug where the preview ran `resolveSekAmount(tx.amount, null,
|
|
// INV.currency, INV.rate)`, treating the SEK tx number as if it
|
|
// were in the invoice's currency and multiplying by the invoice's
|
|
// rate. That produced a fictitious bank-leg and silently dropped
|
|
// the FX gain/loss for cross-currency invoices.
|
|
const inv = invoice as Invoice
|
|
const { lines: clearingLines } = buildInvoicePaymentClearingLines(
|
|
{
|
|
amount: transaction.amount,
|
|
amount_sek: transaction.amount_sek ?? null,
|
|
currency: transaction.currency,
|
|
exchange_rate: transaction.exchange_rate ?? null,
|
|
},
|
|
{
|
|
currency: inv.currency,
|
|
exchange_rate: inv.exchange_rate ?? null,
|
|
remaining_amount: inv.remaining_amount ?? null,
|
|
total: inv.total,
|
|
paid_amount: inv.paid_amount ?? null,
|
|
},
|
|
'Inbetalning kundfaktura',
|
|
fxConversion.required && !('error' in fxConversion)
|
|
? fxConversion.paid_in_invoice_currency
|
|
: undefined,
|
|
paymentAccount,
|
|
)
|
|
for (const line of clearingLines) {
|
|
lines.push({
|
|
account_number: line.account_number,
|
|
debit_amount: line.debit_amount,
|
|
credit_amount: line.credit_amount,
|
|
description: line.line_description ?? '',
|
|
})
|
|
}
|
|
}
|
|
|
|
return NextResponse.json({
|
|
entry_type: entryType,
|
|
lines,
|
|
invoice_already_booked: invoiceAlreadyBooked,
|
|
accounting_method: accountingMethod,
|
|
is_fully_paid: isFullyPaid,
|
|
fx_conversion: fxConversion,
|
|
})
|
|
},
|
|
)
|