d840257c0c
* fix(mcp-oauth): allow ChatGPT connector callbacks and resume OAuth after login Add chatgpt.com/connector/oauth/* (per-instance) and the legacy chatgpt.com/connector_platform_oauth_redirect to the built-in OAuth redirect allowlist so ChatGPT MCP connectors can register and authorize. Fix the login page dropping the ?next= destination: an OAuth-initiated visit that required login previously ended on the dashboard and the connection flow silently died. Login now resumes to the sanitized next path (hard navigation, since the consent page is route-handler HTML), carries it through the MFA step-up as returnTo, and /mfa/verify hard-navigates for /api/ destinations. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(transactions): dedup incoming feed rows against booked hand-entered twins Users who bookkeep via MCP/chat first and connect their bank afterwards got the same movement twice: the synced row's external_id lives in a different namespace, the free-form manual title never text-bridges the bank's raw string, and the cross-channel mirror deliberately excluded manual/mcp rows. Extend the mirror with a booked-hand-entered track: an incoming feed row is skipped when a BOOKED manual/mcp row shares its (date, ore) bucket count- symmetrically. Gates beyond the feed-vs-feed mirror: stored row must be booked (staged rows never consume an import), currencies must not contradict (bucket key is date+ore only), the cash-account guard applies to the count exactly as to consumption, and symmetry uses the Layer-1-unmatched incoming count so an already-stored row cannot inflate it. Consumption stamps the batch cash_account_id onto an account-unbound hand row, so one hand row can never consume feed rows on other accounts in later syncs. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat(bookkeeping): inline verifikat rattelse (strike lines + text/date edit) Second sanctioned correction track under BFL 5 kap 5/9 pp, Fortnox-style: strike lines inside a posted verifikat with replacements in the same voucher, and correct description/entry_date without an andringsverifikat. Envelope: posted entries, open unlocked periods, company lock date, same-period date moves, structural/FX/doc-linked lines excluded, and a reconciliation guard preserving per-account net on bank/reskontra sides of externally linked entries. Every rattelse writes an immutable who/when row (journal_entry_rattelse_log, WORM, archived as rakenskapsinformation) and struck originals render struck-through in the verifikat; list rows and the detail header carry a Rattad marker. CLAUDE.md hard rule 1 and the swedish-accounting-compliance skill are amended to state the two-track rule. Staging carries the DDL; prod gets it on merge. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat: live saldo in booking form, prior-year window comparison, hideable assistant FAB - Manual journal entry: saldo column now shows before -> after computed from the typed debit/credit amounts (direction feedback while booking) - Resultatrapport: a narrowed date range now compares against the same window shifted one year back (#862), merged across fiscal periods for brutet rakenskapsar; P&L rows report window activity instead of rolled-forward YTD closing - Assistant FAB: per-user hide toggle (user_preferences.hide_assistant_fab, settings > assistant), sidebar entry unaffected; collapsed sessions keep their reopen handle Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat(stripe): sync balance transactions as a bank feed on 1686 Import the connected Stripe balance into the transactions inbox, opt-in per connection (transaction_sync_enabled on stripe_connections): - Balance transactions map to feed rows with the two-row gross+fee split and frozen external_id formats (stripe_{acct}_{txn} / _fee), dated on created, bound to a provisioned "Stripe-saldo" cash account on 1686 so booking settles against the clearing account by construction. - Double-booking protection: settled payment-link charges import pre-linked to their settlement entry; payout rows import pre-linked to the payout entry; processPayoutPaidEvent claims the payout's fee rows at booking time (linkPayoutFeedRows, idempotent from both directions). - Cursor last_balance_txn_synced_at with 24h overlap; first run backfills 90 days floored at the day after the company lock date. - Nightly cron /api/extensions/stripe/transactions/cron (03:30), transaction-sync toggle route, "Synka nu" covers both feeds, settings panel toggle with last-synced/backfill note, sv+en strings. - Migration 20260723200000 (applied to staging). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(transactions): offer match-to-voucher on unbooked history rows Unbooked transactions with is_business already set (e.g. left behind when a voucher was removed without a full uncategorize) land in the history list instead of the inbox, where the match-against-existing-voucher action did not exist, leaving them with no path back to voucher matching. Add the same menu item to the history list for unbooked rows. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat(transactions): enhance ownership checks and error handling in journal entry routes --------- Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
271 lines
10 KiB
TypeScript
271 lines
10 KiB
TypeScript
import type { SupabaseClient } from '@supabase/supabase-js'
|
|
import { generateTrialBalance } from './trial-balance'
|
|
import type {
|
|
ResultatrapportReport,
|
|
ResultatrapportRow,
|
|
ResultatrapportGroup,
|
|
TrialBalanceRow,
|
|
} from '@/types'
|
|
|
|
const CLASS_LABELS: Record<number, string> = {
|
|
3: '3 Rörelsens inkomster/intäkter',
|
|
4: '4 Material- och varukostnader',
|
|
5: '5 Övriga externa kostnader',
|
|
6: '6 Övriga externa kostnader',
|
|
7: '7 Personalkostnader',
|
|
8: '8 Finansiella poster och bokslutsdispositioner',
|
|
}
|
|
|
|
/**
|
|
* Resultatrapport: operational P&L report.
|
|
*
|
|
* Lists every account in classes 3-8 with current-period and prior-period
|
|
* values side by side. Unlike Resultaträkning (formal, ÅRL Bilaga 2), this
|
|
* keeps account numbers and is meant for ongoing reconciliation, not for
|
|
* årsbokslut/årsredovisning.
|
|
*
|
|
* Account 8999 is excluded: it's the year-end closing account that moves
|
|
* årets resultat into equity (2099). Including its balance would double-count
|
|
* the result. Same exclusion as generateIncomeStatement.
|
|
*/
|
|
export async function generateResultatrapport(
|
|
supabase: SupabaseClient,
|
|
companyId: string,
|
|
fiscalPeriodId: string,
|
|
options?: {
|
|
fromDate?: string
|
|
toDate?: string
|
|
/** SIE dim → code filter ({"6":"P001"}). P&L-safe: see trial-balance.ts. */
|
|
dimensions?: Record<string, string>
|
|
}
|
|
): Promise<ResultatrapportReport> {
|
|
const { data: period } = await supabase
|
|
.from('fiscal_periods')
|
|
.select('period_start, period_end, previous_period_id')
|
|
.eq('id', fiscalPeriodId)
|
|
.eq('company_id', companyId)
|
|
.single()
|
|
|
|
if (!period) {
|
|
throw new Error('Fiscal period not found')
|
|
}
|
|
|
|
const effectiveFromDate = options?.fromDate ?? period.period_start
|
|
const effectiveToDate = options?.toDate ?? period.period_end
|
|
|
|
const currentTb = await generateTrialBalance(supabase, companyId, fiscalPeriodId, {
|
|
fromDate: options?.fromDate,
|
|
toDate: options?.toDate,
|
|
dimensions: options?.dimensions,
|
|
})
|
|
const currentRows = filterPnl(currentTb.rows)
|
|
|
|
// Prior-period comparison. Full period: the previous fiscal period.
|
|
// Narrowed range: the same window shifted one year back (#862), so
|
|
// "Hittills i år" compares against the same dates last year.
|
|
// Dimension filter: no comparison at all; project codes are time-limited
|
|
// under K2/K3 (registry start/end dates), so "this code last year" may be
|
|
// a different project entirely: drop the column rather than compare
|
|
// unrelated activity (#862 review).
|
|
let priorRows: TrialBalanceRow[] = []
|
|
let priorPeriodInfo: { start: string; end: string } | null = null
|
|
const hasRange = Boolean(options?.fromDate || options?.toDate)
|
|
const isFullPeriod = !hasRange && !options?.dimensions
|
|
if (isFullPeriod) {
|
|
// Prefer the explicit continuity chain; fall back to the period that ends
|
|
// immediately before this one. The fallback keeps the comparison working
|
|
// for companies whose chain was never linked: e.g. multi-year SIE imports
|
|
// created before the importer started setting previous_period_id.
|
|
let priorPeriodId: string | null = period.previous_period_id ?? null
|
|
if (!priorPeriodId) {
|
|
const { data: priorByDate } = await supabase
|
|
.from('fiscal_periods')
|
|
.select('id')
|
|
.eq('company_id', companyId)
|
|
.lt('period_end', period.period_start)
|
|
.order('period_end', { ascending: false })
|
|
.limit(1)
|
|
priorPeriodId = priorByDate && priorByDate.length > 0 ? priorByDate[0].id : null
|
|
}
|
|
|
|
if (priorPeriodId) {
|
|
const { data: prior } = await supabase
|
|
.from('fiscal_periods')
|
|
.select('period_start, period_end')
|
|
.eq('id', priorPeriodId)
|
|
.eq('company_id', companyId)
|
|
.single()
|
|
|
|
if (prior) {
|
|
const priorTb = await generateTrialBalance(supabase, companyId, priorPeriodId)
|
|
priorRows = filterPnl(priorTb.rows)
|
|
priorPeriodInfo = { start: prior.period_start, end: prior.period_end }
|
|
}
|
|
}
|
|
} else if (hasRange && !options?.dimensions) {
|
|
// Same window, prior year. Shift the window back one year (leap-day
|
|
// clamped) and read activity from whichever fiscal period(s) cover the
|
|
// shifted dates: brutet räkenskapsår can split a calendar window across
|
|
// two periods, so merge per account. The current period itself is a
|
|
// valid source (a long first fiscal year can contain both windows).
|
|
const shiftedFrom = shiftDateOneYearBack(effectiveFromDate)
|
|
const shiftedTo = shiftDateOneYearBack(effectiveToDate)
|
|
// A window longer than a year would overlap itself when shifted back;
|
|
// that comparison is meaningless, so leave the column empty.
|
|
if (shiftedTo < effectiveFromDate) {
|
|
const { data: candidatePeriods } = await supabase
|
|
.from('fiscal_periods')
|
|
.select('id, period_start, period_end')
|
|
.eq('company_id', companyId)
|
|
.lte('period_start', shiftedTo)
|
|
.gte('period_end', shiftedFrom)
|
|
.order('period_start', { ascending: true })
|
|
|
|
const merged = new Map<string, TrialBalanceRow>()
|
|
let coveredAny = false
|
|
for (const p of candidatePeriods ?? []) {
|
|
const from = shiftedFrom > p.period_start ? shiftedFrom : p.period_start
|
|
const to = shiftedTo < p.period_end ? shiftedTo : p.period_end
|
|
if (from > to) continue
|
|
const tbPart = await generateTrialBalance(supabase, companyId, p.id, {
|
|
fromDate: from,
|
|
toDate: to,
|
|
})
|
|
coveredAny = true
|
|
for (const row of filterPnl(tbPart.rows)) {
|
|
const existing = merged.get(row.account_number)
|
|
if (!existing) {
|
|
merged.set(row.account_number, { ...row })
|
|
} else {
|
|
existing.period_debit = round2(existing.period_debit + row.period_debit)
|
|
existing.period_credit = round2(existing.period_credit + row.period_credit)
|
|
}
|
|
}
|
|
}
|
|
if (coveredAny) {
|
|
priorRows = [...merged.values()]
|
|
priorPeriodInfo = { start: shiftedFrom, end: shiftedTo }
|
|
}
|
|
}
|
|
}
|
|
|
|
const priorByAccount = new Map<string, TrialBalanceRow>()
|
|
for (const r of priorRows) priorByAccount.set(r.account_number, r)
|
|
|
|
const groups = buildGroups(currentRows, priorByAccount)
|
|
|
|
const netResultCurrent = sumNet(currentRows)
|
|
const netResultPrior = sumNet(priorRows)
|
|
|
|
return {
|
|
groups,
|
|
net_result_current: round2(netResultCurrent),
|
|
net_result_prior: round2(netResultPrior),
|
|
period: { start: effectiveFromDate, end: effectiveToDate },
|
|
prior_period: priorPeriodInfo,
|
|
}
|
|
}
|
|
|
|
function filterPnl(rows: TrialBalanceRow[]): TrialBalanceRow[] {
|
|
return rows.filter(
|
|
(r) =>
|
|
r.account_class >= 3 &&
|
|
r.account_class <= 8 &&
|
|
r.account_number !== '8999'
|
|
)
|
|
}
|
|
|
|
/**
|
|
* Sign convention: revenue (class 3) has credit normal balance, expenses
|
|
* (class 4-7) have debit. We render every line as `credit - debit` so that
|
|
* revenue is positive, expenses are negative, and a positive net result
|
|
* means profit. This matches how Fortnox and Visma present a Resultatrapport.
|
|
*
|
|
* Window activity (`period_*`), not `closing_*`: when fromDate > period_start
|
|
* the trial balance rolls pre-window activity into the opening columns, so
|
|
* `closing_*` on a P&L account would silently report year-to-date amounts in
|
|
* a month/quarter window. In the full-period case P&L accounts carry no
|
|
* opening balance, so period_* equals closing_* and nothing changes there.
|
|
*/
|
|
function signedAmount(row: TrialBalanceRow): number {
|
|
return row.period_credit - row.period_debit
|
|
}
|
|
|
|
/**
|
|
* `2026-03-15` -> `2025-03-15`; leap day clamps to the target month's last
|
|
* day (`2028-02-29` -> `2027-02-28`). String math on the ISO parts: no Date
|
|
* object, no timezone edge.
|
|
*/
|
|
export function shiftDateOneYearBack(isoDate: string): string {
|
|
const [y, m, d] = isoDate.split('-').map(Number)
|
|
const year = y - 1
|
|
const daysInMonth = [31, year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0) ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
|
|
const day = Math.min(d, daysInMonth[m - 1])
|
|
return `${year}-${String(m).padStart(2, '0')}-${String(day).padStart(2, '0')}`
|
|
}
|
|
|
|
function sumNet(rows: TrialBalanceRow[]): number {
|
|
return rows.reduce((sum, r) => sum + signedAmount(r), 0)
|
|
}
|
|
|
|
function buildGroups(
|
|
currentRows: TrialBalanceRow[],
|
|
priorByAccount: Map<string, TrialBalanceRow>
|
|
): ResultatrapportGroup[] {
|
|
const accountIndex = new Map<string, { name: string; class: number }>()
|
|
for (const r of currentRows) {
|
|
accountIndex.set(r.account_number, { name: r.account_name, class: r.account_class })
|
|
}
|
|
for (const r of priorByAccount.values()) {
|
|
if (!accountIndex.has(r.account_number)) {
|
|
accountIndex.set(r.account_number, { name: r.account_name, class: r.account_class })
|
|
}
|
|
}
|
|
|
|
const currentByAccount = new Map<string, TrialBalanceRow>()
|
|
for (const r of currentRows) currentByAccount.set(r.account_number, r)
|
|
|
|
const groups: ResultatrapportGroup[] = []
|
|
for (const klass of [3, 4, 5, 6, 7, 8] as const) {
|
|
const accountsInClass = [...accountIndex.entries()]
|
|
.filter(([, info]) => info.class === klass)
|
|
.map(([account_number, info]) => ({ account_number, name: info.name }))
|
|
.sort((a, b) => a.account_number.localeCompare(b.account_number))
|
|
|
|
const rows: ResultatrapportRow[] = []
|
|
let subtotalCurrent = 0
|
|
let subtotalPrior = 0
|
|
for (const { account_number, name } of accountsInClass) {
|
|
const cur = currentByAccount.get(account_number)
|
|
const pr = priorByAccount.get(account_number)
|
|
const currentAmount = cur ? signedAmount(cur) : 0
|
|
const priorAmount = pr ? signedAmount(pr) : 0
|
|
if (Math.abs(currentAmount) < 0.005 && Math.abs(priorAmount) < 0.005) continue
|
|
rows.push({
|
|
account_number,
|
|
account_name: name,
|
|
current_period: round2(currentAmount),
|
|
prior_period: round2(priorAmount),
|
|
})
|
|
subtotalCurrent += currentAmount
|
|
subtotalPrior += priorAmount
|
|
}
|
|
|
|
if (rows.length === 0) continue
|
|
|
|
groups.push({
|
|
class: klass,
|
|
class_label: CLASS_LABELS[klass],
|
|
rows,
|
|
subtotal_current: round2(subtotalCurrent),
|
|
subtotal_prior: round2(subtotalPrior),
|
|
})
|
|
}
|
|
|
|
return groups
|
|
}
|
|
|
|
function round2(n: number): number {
|
|
return Math.round(n * 100) / 100
|
|
}
|