diff --git a/backend/src/modules/journal-entries/journal-entries.service.ts b/backend/src/modules/journal-entries/journal-entries.service.ts index 4f67d5c..95098a3 100644 --- a/backend/src/modules/journal-entries/journal-entries.service.ts +++ b/backend/src/modules/journal-entries/journal-entries.service.ts @@ -13,6 +13,16 @@ export class JournalEntriesService { async findAll(filters: { from?: string; to?: string; accountId?: string; type?: string }) { let sql = ` SELECT je.*, + 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,