From b0282b7f8b0887dce2c578923e17a5119070b5ac Mon Sep 17 00:00:00 2001 From: olsch01 Date: Tue, 10 Mar 2026 09:41:26 -0400 Subject: [PATCH] fix: show P&L debit/credit totals on journal entries list The previous aggregation used simple SUM(debit)/SUM(credit) which always produced equal values for balanced entries. This was misleading for entries with income/expense lines (e.g., monthly actuals). Now, when an entry has income/expense lines, the totals reflect only P&L account activity (expenses as debits, income as credits), excluding the cash offset. For balance-sheet-only entries (opening balances, adjustments), the full entry totals are shown. Co-Authored-By: Claude Opus 4.6 --- .../journal-entries/journal-entries.service.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/backend/src/modules/journal-entries/journal-entries.service.ts b/backend/src/modules/journal-entries/journal-entries.service.ts index dc768b0..95098a3 100644 --- a/backend/src/modules/journal-entries/journal-entries.service.ts +++ b/backend/src/modules/journal-entries/journal-entries.service.ts @@ -13,8 +13,16 @@ export class JournalEntriesService { async findAll(filters: { from?: string; to?: string; accountId?: string; type?: string }) { let sql = ` SELECT je.*, - COALESCE(SUM(jel.debit), 0) as total_debit, - COALESCE(SUM(jel.credit), 0) as total_credit, + CASE + WHEN SUM(CASE WHEN a.account_type IN ('income','expense') THEN 1 ELSE 0 END) > 0 + THEN COALESCE(SUM(CASE WHEN a.account_type IN ('income','expense') THEN jel.debit ELSE 0 END), 0) + ELSE COALESCE(SUM(jel.debit), 0) + END as total_debit, + CASE + WHEN SUM(CASE WHEN a.account_type IN ('income','expense') THEN 1 ELSE 0 END) > 0 + THEN COALESCE(SUM(CASE WHEN a.account_type IN ('income','expense') THEN jel.credit ELSE 0 END), 0) + ELSE COALESCE(SUM(jel.credit), 0) + END as total_credit, json_agg(json_build_object( 'id', jel.id, 'account_id', jel.account_id, 'debit', jel.debit, 'credit', jel.credit, 'memo', jel.memo,