* 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>
378 lines
14 KiB
TypeScript
378 lines
14 KiB
TypeScript
/**
|
|
* Received self-billing invoice (mottagen självfaktura, ML 17 kap 15§).
|
|
*
|
|
* A self-billing invoice we RECEIVE is a SALE for us: the customer issued the
|
|
* document on our behalf, so for our books it is an ordinary customer invoice
|
|
* (Debit 1510, Credit 30xx + 26xx) and the output VAT lands in our
|
|
* momsdeklaration. It differs from a normal customer invoice in two ways:
|
|
* - We never assign a number from our own series (BFL 5 kap 6§): the
|
|
* counterparty's number lives in external_invoice_number and our own
|
|
* invoice_number stays null (enforced by invoices_self_billed_numbering).
|
|
* - There is no send step: under faktureringsmetoden it is booked on
|
|
* registration; under kontantmetoden it stays unbooked until payment.
|
|
*
|
|
* This is the single implementation behind both the internal dashboard route
|
|
* (/api/invoices/self-billed) and the public v1 invoice create endpoint (which
|
|
* accepts an optional is_self_billed flag), so the two can never drift.
|
|
*/
|
|
import type { SupabaseClient } from '@supabase/supabase-js'
|
|
import { resolveCompanyEntityType } from '@/lib/company/entity-type'
|
|
import { getVatRules, getPermittedVatRates } from '@/lib/invoices/vat-rules'
|
|
import { fetchExchangeRate, convertToSEK } from '@/lib/currency/riksbanken'
|
|
import { createInvoiceJournalEntry } from '@/lib/bookkeeping/invoice-entries'
|
|
import { eventBus } from '@/lib/events'
|
|
import { roundOre } from '@/lib/money'
|
|
import { createLogger } from '@/lib/logger'
|
|
import type { EntityType, Invoice } from '@/types'
|
|
|
|
const log = createLogger('self-billed-sale')
|
|
|
|
export interface SelfBilledSaleItemInput {
|
|
description: string
|
|
quantity: number
|
|
unit: string
|
|
unit_price: number
|
|
vat_rate?: number
|
|
}
|
|
|
|
export interface SelfBilledSaleInput {
|
|
customer_id: string
|
|
external_invoice_number: string
|
|
self_billing_agreement_ref?: string | null
|
|
invoice_date: string
|
|
received_date: string
|
|
due_date: string
|
|
currency: string
|
|
notes?: string | null
|
|
items: SelfBilledSaleItemInput[]
|
|
}
|
|
|
|
export type SelfBilledSaleFailure =
|
|
| { code: 'customer_not_found'; customerId: string }
|
|
| { code: 'vat_rule_violation'; attemptedRate: number; allowedRates: number[]; customerType: string }
|
|
| { code: 'fx_rate_unavailable'; currency: string; invoiceDate: string }
|
|
| { code: 'insert_failed'; stage?: string; pgCode?: string; pgMessage?: string }
|
|
| { code: 'items_failed'; pgCode?: string; pgMessage?: string }
|
|
| { code: 'no_fiscal_period' }
|
|
|
|
interface SelfBilledCustomer {
|
|
id: string
|
|
name: string
|
|
customer_type: string
|
|
vat_number_validated: boolean | null
|
|
/** ISO 3166-1 alpha-2; gates reverse charge together with the two above. */
|
|
country?: string | null
|
|
}
|
|
|
|
export interface SelfBilledSaleComputedItem {
|
|
sort_order: number
|
|
description: string
|
|
quantity: number
|
|
unit: string
|
|
unit_price: number
|
|
line_total: number
|
|
vat_rate: number
|
|
vat_amount: number
|
|
}
|
|
|
|
export interface SelfBilledSaleDraft {
|
|
customer: SelfBilledCustomer
|
|
items: SelfBilledSaleComputedItem[]
|
|
subtotal: number
|
|
vatAmount: number
|
|
total: number
|
|
subtotalSek: number | null
|
|
vatAmountSek: number | null
|
|
totalSek: number | null
|
|
exchangeRate: number | null
|
|
exchangeRateDate: string | null
|
|
currency: string
|
|
vatTreatment: string
|
|
momsRuta: string | null
|
|
reverseChargeText: string | null
|
|
vatRate: number | null
|
|
}
|
|
|
|
/**
|
|
* Validate + cost a received self-billing invoice. No writes, no number
|
|
* consumption. Used for the dry-run preview and by createSelfBilledSaleInvoice.
|
|
*/
|
|
export async function resolveSelfBilledSaleDraft(
|
|
supabase: SupabaseClient,
|
|
companyId: string,
|
|
input: SelfBilledSaleInput,
|
|
): Promise<{ ok: true; draft: SelfBilledSaleDraft } | { ok: false; failure: SelfBilledSaleFailure }> {
|
|
// The issuer of a self-billing invoice is, in our books, the customer we sold
|
|
// to. Require an existing customer so VAT rules + reporting work. Project only
|
|
// the fields used (data minimisation).
|
|
const { data: customer, error: customerError } = await supabase
|
|
.from('customers')
|
|
.select('id, name, customer_type, vat_number_validated, country')
|
|
.eq('id', input.customer_id)
|
|
.eq('company_id', companyId)
|
|
.maybeSingle()
|
|
|
|
if (customerError || !customer) {
|
|
return { ok: false, failure: { code: 'customer_not_found', customerId: input.customer_id } }
|
|
}
|
|
const c = customer as SelfBilledCustomer
|
|
|
|
// VAT treatment is driven by who the customer is (domestic / EU reverse charge
|
|
// / export), exactly like an own-issued invoice.
|
|
const vatRules = getVatRules(
|
|
c.customer_type as Parameters<typeof getVatRules>[0],
|
|
c.vat_number_validated ?? undefined,
|
|
c.country,
|
|
)
|
|
// Gate on the PERMITTED set, not the picker default, exactly like
|
|
// buildInvoiceWriteData: the ML 6 kap. supplies taxed where they are performed
|
|
// (hotel/restaurang 12%, persontransport and event admission 6%,
|
|
// fastighetstjänst and korttidsuthyrning 25%) carry Swedish VAT even to a
|
|
// foreign business customer, and a self-bill transcribes what the customer
|
|
// actually invoiced in our name. The default is still 0% (vatRules.rate is the
|
|
// fallback below), so a Swedish rate only lands here when the line carried it.
|
|
const permittedRates = getPermittedVatRates(
|
|
c.customer_type as Parameters<typeof getPermittedVatRates>[0],
|
|
c.vat_number_validated ?? undefined,
|
|
c.country,
|
|
)
|
|
const allowedRates = new Set(permittedRates.map((r) => r.rate))
|
|
|
|
const items: SelfBilledSaleComputedItem[] = []
|
|
let vatAmount = 0
|
|
for (let i = 0; i < input.items.length; i++) {
|
|
const item = input.items[i]
|
|
const itemRate = item.vat_rate !== undefined ? item.vat_rate : vatRules.rate
|
|
if (!allowedRates.has(itemRate)) {
|
|
return {
|
|
ok: false,
|
|
failure: {
|
|
code: 'vat_rule_violation',
|
|
attemptedRate: itemRate,
|
|
allowedRates: Array.from(allowedRates),
|
|
customerType: c.customer_type,
|
|
},
|
|
}
|
|
}
|
|
const lineTotal = item.quantity * item.unit_price
|
|
const lineVat = roundOre((lineTotal * itemRate) / 100)
|
|
vatAmount += lineVat
|
|
items.push({
|
|
sort_order: i,
|
|
description: item.description,
|
|
quantity: item.quantity,
|
|
unit: item.unit,
|
|
unit_price: item.unit_price,
|
|
line_total: lineTotal,
|
|
vat_rate: itemRate,
|
|
vat_amount: lineVat,
|
|
})
|
|
}
|
|
|
|
const subtotal = input.items.reduce((sum, item) => sum + item.quantity * item.unit_price, 0)
|
|
const total = roundOre(subtotal + vatAmount)
|
|
|
|
const uniqueRates = new Set(input.items.map((item) => item.vat_rate ?? vatRules.rate))
|
|
const isMixedRate = uniqueRates.size > 1
|
|
const vatRate = isMixedRate ? null : (uniqueRates.values().next().value ?? vatRules.rate)
|
|
|
|
// Foreign currency: convert using the rate on the INVOICE date (ML 7 kap 7§),
|
|
// not today's rate. Refuse rather than fall through to a silent 1:1 booking.
|
|
let exchangeRate: number | null = null
|
|
let exchangeRateDate: string | null = null
|
|
let subtotalSek: number | null = null
|
|
let vatAmountSek: number | null = null
|
|
let totalSek: number | null = null
|
|
if (input.currency !== 'SEK') {
|
|
const rateData = await fetchExchangeRate(
|
|
input.currency as Parameters<typeof fetchExchangeRate>[0],
|
|
new Date(input.invoice_date),
|
|
)
|
|
if (!rateData) {
|
|
return { ok: false, failure: { code: 'fx_rate_unavailable', currency: input.currency, invoiceDate: input.invoice_date } }
|
|
}
|
|
exchangeRate = rateData.rate
|
|
exchangeRateDate = rateData.date
|
|
subtotalSek = convertToSEK(subtotal, exchangeRate)
|
|
vatAmountSek = convertToSEK(vatAmount, exchangeRate)
|
|
totalSek = convertToSEK(total, exchangeRate)
|
|
}
|
|
|
|
return {
|
|
ok: true,
|
|
draft: {
|
|
customer: c,
|
|
items,
|
|
subtotal,
|
|
vatAmount,
|
|
total,
|
|
subtotalSek,
|
|
vatAmountSek,
|
|
totalSek,
|
|
exchangeRate,
|
|
exchangeRateDate,
|
|
currency: input.currency,
|
|
vatTreatment: vatRules.treatment,
|
|
momsRuta: vatRules.momsRuta,
|
|
reverseChargeText: vatRules.reverseChargeText || null,
|
|
vatRate,
|
|
},
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Register a received self-billing invoice: insert the row, its items, and
|
|
* (under faktureringsmetoden) book the registration entry. Rolls back on any
|
|
* failure. Returns the created invoice or a structured failure. Re-throws an
|
|
* unexpected booking error (e.g. a period-lock trigger) after rolling back so
|
|
* the caller can map it to its structured envelope.
|
|
*/
|
|
export async function createSelfBilledSaleInvoice(
|
|
supabase: SupabaseClient,
|
|
companyId: string,
|
|
userId: string,
|
|
input: SelfBilledSaleInput,
|
|
): Promise<{ ok: true; invoice: Invoice } | { ok: false; failure: SelfBilledSaleFailure }> {
|
|
const resolved = await resolveSelfBilledSaleDraft(supabase, companyId, input)
|
|
if (!resolved.ok) return resolved
|
|
const { draft } = resolved
|
|
|
|
const { data: invoice, error: invoiceError } = await supabase
|
|
.from('invoices')
|
|
.insert({
|
|
user_id: userId,
|
|
company_id: companyId,
|
|
customer_id: input.customer_id,
|
|
// No own number: the counterparty's number lives in external_invoice_number.
|
|
invoice_number: null,
|
|
is_self_billed: true,
|
|
external_invoice_number: input.external_invoice_number,
|
|
self_billing_agreement_ref: input.self_billing_agreement_ref ?? null,
|
|
received_date: input.received_date,
|
|
invoice_date: input.invoice_date,
|
|
due_date: input.due_date,
|
|
// Booked + awaiting/with payment: never a draft.
|
|
status: 'sent',
|
|
currency: input.currency,
|
|
exchange_rate: draft.exchangeRate,
|
|
exchange_rate_date: draft.exchangeRateDate,
|
|
subtotal: draft.subtotal,
|
|
subtotal_sek: draft.subtotalSek,
|
|
vat_amount: draft.vatAmount,
|
|
vat_amount_sek: draft.vatAmountSek,
|
|
total: draft.total,
|
|
total_sek: draft.totalSek,
|
|
remaining_amount: draft.total,
|
|
vat_treatment: draft.vatTreatment,
|
|
vat_rate: draft.vatRate,
|
|
moms_ruta: draft.momsRuta,
|
|
reverse_charge_text: draft.reverseChargeText,
|
|
notes: input.notes,
|
|
document_type: 'invoice',
|
|
})
|
|
.select()
|
|
.single()
|
|
|
|
if (invoiceError || !invoice) {
|
|
log.error('self-billed invoice insert failed', invoiceError as Error)
|
|
return {
|
|
ok: false,
|
|
failure: { code: 'insert_failed', pgCode: invoiceError?.code, pgMessage: invoiceError?.message },
|
|
}
|
|
}
|
|
|
|
const itemRows = draft.items.map((item) => ({
|
|
invoice_id: invoice.id,
|
|
sort_order: item.sort_order,
|
|
description: item.description,
|
|
quantity: item.quantity,
|
|
unit: item.unit,
|
|
unit_price: item.unit_price,
|
|
line_total: item.line_total,
|
|
vat_rate: item.vat_rate,
|
|
vat_amount: item.vat_amount,
|
|
}))
|
|
|
|
const { error: itemsError } = await supabase.from('invoice_items').insert(itemRows)
|
|
if (itemsError) {
|
|
// Items insert failed: remove the orphaned invoice header.
|
|
await supabase.from('invoices').delete().eq('id', invoice.id)
|
|
log.error('self-billed invoice items insert failed; rolled back', itemsError, { invoiceId: invoice.id })
|
|
return { ok: false, failure: { code: 'items_failed', pgCode: itemsError.code, pgMessage: itemsError.message } }
|
|
}
|
|
|
|
const { data: settings } = await supabase
|
|
.from('company_settings')
|
|
.select('accounting_method, entity_type')
|
|
.eq('company_id', companyId)
|
|
.maybeSingle()
|
|
const accountingMethod = settings?.accounting_method || 'accrual'
|
|
const entityType = await resolveCompanyEntityType(supabase, companyId, settings?.entity_type)
|
|
|
|
const { data: completeInvoice } = await supabase
|
|
.from('invoices')
|
|
.select('*, customer:customers(*), items:invoice_items(*)')
|
|
.eq('id', invoice.id)
|
|
.maybeSingle()
|
|
|
|
// Faktureringsmetoden: book the registration entry now (Debit 1510, Credit
|
|
// 30xx + 26xx). Kontantmetoden: leave unbooked until payment.
|
|
if (accountingMethod === 'accrual') {
|
|
if (!completeInvoice) {
|
|
await supabase.from('invoices').delete().eq('id', invoice.id)
|
|
log.error('self-billed invoice re-fetch returned no row before booking; rolled back', undefined, {
|
|
invoiceId: invoice.id,
|
|
})
|
|
return { ok: false, failure: { code: 'insert_failed', stage: 'refetch_before_booking' } }
|
|
}
|
|
try {
|
|
const journalEntry = await createInvoiceJournalEntry(
|
|
supabase,
|
|
companyId,
|
|
userId,
|
|
completeInvoice as Invoice,
|
|
entityType,
|
|
draft.customer.name,
|
|
{ descriptionPrefix: 'Självfaktura', numberOverride: input.external_invoice_number },
|
|
)
|
|
if (!journalEntry) {
|
|
// No open fiscal period: roll back so we never leave an unbooked sale.
|
|
await supabase.from('invoices').delete().eq('id', invoice.id)
|
|
return { ok: false, failure: { code: 'no_fiscal_period' } }
|
|
}
|
|
const { error: linkError } = await supabase
|
|
.from('invoices')
|
|
.update({ journal_entry_id: journalEntry.id })
|
|
.eq('id', invoice.id)
|
|
.eq('company_id', companyId)
|
|
if (linkError) {
|
|
// The verifikat is committed (immutable): don't roll it back over a
|
|
// failed convenience link. Log loudly.
|
|
log.error('self-billed invoice booked but journal_entry_id link failed', linkError, {
|
|
invoiceId: invoice.id,
|
|
journalEntryId: journalEntry.id,
|
|
})
|
|
}
|
|
} catch (err) {
|
|
await supabase.from('invoices').delete().eq('id', invoice.id)
|
|
log.error('failed to book self-billed invoice; rolled back', err as Error, { invoiceId: invoice.id })
|
|
throw err
|
|
}
|
|
}
|
|
|
|
const { data: finalInvoice } = await supabase
|
|
.from('invoices')
|
|
.select('*, customer:customers(*), items:invoice_items(*)')
|
|
.eq('id', invoice.id)
|
|
.maybeSingle()
|
|
|
|
const responseInvoice = (finalInvoice ?? completeInvoice ?? invoice) as Invoice
|
|
|
|
await eventBus.emit({
|
|
type: 'invoice.created',
|
|
payload: { invoice: responseInvoice, companyId, userId },
|
|
})
|
|
|
|
return { ok: true, invoice: responseInvoice }
|
|
}
|