From 327ecd4ff2c1f1c6bc2ff164131fe265dc6d29d7 Mon Sep 17 00:00:00 2001 From: Jakob Wennberg Date: Sat, 21 Feb 2026 15:22:42 +0100 Subject: [PATCH] fix: include period result in balance sheet to ensure it balances MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The balance sheet only showed class 1-2 accounts but did not include the period result (årets resultat) from class 3-8 income/expense accounts. Before year-end closing, this caused a balance discrepancy equal to the period's profit/loss. Now calculates the net result and adds it as a synthetic "Årets resultat" section under equity. Co-Authored-By: Claude Opus 4.6 --- lib/reports/balance-sheet.ts | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/lib/reports/balance-sheet.ts b/lib/reports/balance-sheet.ts index eebb86bd..44d2104c 100644 --- a/lib/reports/balance-sheet.ts +++ b/lib/reports/balance-sheet.ts @@ -55,6 +55,34 @@ export async function generateBalanceSheet( 'credit' // Equity/liabilities have credit normal balance ) + // Calculate period result from income/expense accounts (class 3-8) + // Before year-end closing, this result lives on class 3-8 accounts and must + // be included in equity for the balance sheet to balance. + const incomeExpenseRows = rows.filter( + (r) => r.account_class >= 3 && r.account_class <= 8 + ) + const periodResult = Math.round( + incomeExpenseRows.reduce( + (sum, r) => sum + (r.closing_credit - r.closing_debit), + 0 + ) * 100 + ) / 100 + + // Add period result as a synthetic section under equity if non-zero + if (Math.abs(periodResult) > 0.005) { + equityLiabilitySections.push({ + title: 'Årets resultat', + rows: [ + { + account_number: '', + account_name: 'Beräknat resultat', + amount: periodResult, + }, + ], + subtotal: periodResult, + }) + } + const totalAssets = assetSections.reduce((sum, s) => sum + s.subtotal, 0) const totalEquityLiabilities = equityLiabilitySections.reduce((sum, s) => sum + s.subtotal, 0)