From f66c6e2fd694e2b5416afc91713d9a51450aaf38 Mon Sep 17 00:00:00 2001 From: Marius Mutu Date: Tue, 9 Dec 2025 15:49:39 +0200 Subject: [PATCH] fix: Correct SQL syntax in treasury totals query when filters are applied MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The totals PL/SQL block was generating invalid SQL with duplicate WHERE clauses when bank/cash filters were applied (e.g., "WHERE bank_account=X WHERE dataact IS NULL"). Now properly uses AND when where_clause exists, WHERE otherwise. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- .../backend/app/services/treasury_service.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/reports-app/backend/app/services/treasury_service.py b/reports-app/backend/app/services/treasury_service.py index 9840f1b..9f46cc9 100644 --- a/reports-app/backend/app/services/treasury_service.py +++ b/reports-app/backend/app/services/treasury_service.py @@ -257,6 +257,11 @@ class TreasuryService: # sold_precedent = suma sold pentru rânduri cu dataact IS NULL # total_incasari = suma incasari pentru rânduri cu dataact IS NOT NULL # total_plati = suma plati pentru rânduri cu dataact IS NOT NULL + # Notă: where_clause poate fi gol sau poate conține "WHERE ..." + # Dacă e gol, adăugăm WHERE; dacă nu, adăugăm AND + dataact_null_cond = " AND dataact IS NULL" if where_clause else " WHERE dataact IS NULL" + dataact_not_null_cond = " AND dataact IS NOT NULL" if where_clause else " WHERE dataact IS NOT NULL" + totals_plsql = f""" DECLARE v_an NUMBER; @@ -270,18 +275,15 @@ class TreasuryService: -- Sold precedent: suma sold pentru rânduri fără dată (opening balance) SELECT NVL(SUM(sold), 0) INTO :sold_precedent_all - FROM ({base_select}) sub{where_clause} - WHERE dataact IS NULL; + FROM ({base_select}) sub{where_clause}{dataact_null_cond}; -- Total încasări: suma incasari pentru rânduri cu dată (transactions) SELECT NVL(SUM(incasari), 0) INTO :total_incasari_all - FROM ({base_select}) sub{where_clause} - WHERE dataact IS NOT NULL; + FROM ({base_select}) sub{where_clause}{dataact_not_null_cond}; -- Total plăți: suma plati pentru rânduri cu dată (transactions) SELECT NVL(SUM(plati), 0) INTO :total_plati_all - FROM ({base_select}) sub{where_clause} - WHERE dataact IS NOT NULL; + FROM ({base_select}) sub{where_clause}{dataact_not_null_cond}; END; """