details:   https://code.tryton.org/tryton/commit/3bdcec9b622c
branch:    default
user:      Cédric Krier <[email protected]>
date:      Wed Mar 11 12:51:12 2026 +0100
description:
        Use flat balance for general ledger

        Closes #14294
diffstat:

 modules/account/CHANGELOG                  |   1 +
 modules/account/account.py                 |  57 ++++++++++++++++++-----------
 modules/account/tests/scenario_reports.rst |  16 ++++----
 3 files changed, 44 insertions(+), 30 deletions(-)

diffs (174 lines):

diff -r 2e53db33c262 -r 3bdcec9b622c modules/account/CHANGELOG
--- a/modules/account/CHANGELOG Tue Mar 17 16:25:14 2026 +0100
+++ b/modules/account/CHANGELOG Wed Mar 11 12:51:12 2026 +0100
@@ -1,3 +1,4 @@
+* Use flat balance for general ledger
 * Add cron job to reconcile move lines
 
 Version 7.8.0 - 2025-12-15
diff -r 2e53db33c262 -r 3bdcec9b622c modules/account/account.py
--- a/modules/account/account.py        Tue Mar 17 16:25:14 2026 +0100
+++ b/modules/account/account.py        Wed Mar 11 12:51:12 2026 +0100
@@ -1007,6 +1007,7 @@
         FiscalYear = pool.get('account.fiscalyear')
         transaction = Transaction()
         cursor = transaction.connection.cursor()
+        context = transaction.context
 
         table_a = cls.__table__()
         table_c = cls.__table__()
@@ -1019,16 +1020,21 @@
                 line_query, fiscalyear_ids = MoveLine.query_get(line)
             for sub_ids in grouped_slice(ids):
                 red_sql = reduce_ids(table_a.id, sub_ids)
-                query = (table_a.join(table_c,
-                        condition=(table_c.left >= table_a.left)
-                        & (table_c.right <= table_a.right)
-                        ).join(line, condition=line.account == table_c.id
-                        ).select(
-                        table_a.id,
-                        Sum(Coalesce(line.debit, 0) - Coalesce(line.credit, 0)
-                            ).as_('balance'),
-                        where=red_sql & line_query,
-                        group_by=table_a.id))
+                if context.get('flat_balance'):
+                    query = (table_a
+                        .join(line, condition=line.account == table_a.id))
+                else:
+                    query = (table_a
+                        .join(table_c,
+                            condition=(table_c.left >= table_a.left)
+                            & (table_c.right <= table_a.right))
+                        .join(line, condition=line.account == table_c.id))
+                query = query.select(
+                    table_a.id,
+                    Sum(Coalesce(line.debit, 0) - Coalesce(line.credit, 0)
+                        ).as_('balance'),
+                    where=red_sql & line_query,
+                    group_by=table_a.id)
                 if backend.name == 'sqlite':
                     sqlite_apply_types(query, [None, 'NUMERIC'])
                 cursor.execute(*query)
@@ -1495,6 +1501,7 @@
         FiscalYear = pool.get('account.fiscalyear')
         transaction = Transaction()
         cursor = transaction.connection.cursor()
+        context = transaction.context
 
         table_a = Account.__table__()
         table_c = Account.__table__()
@@ -1513,18 +1520,23 @@
                 account_sql = reduce_ids(table_a.id, sub_account_ids)
                 for sub_party_ids in grouped_slice(party_ids):
                     party_sql = reduce_ids(line.party, sub_party_ids)
-                    query = (table_a.join(table_c,
-                            condition=(table_c.left >= table_a.left)
-                            & (table_c.right <= table_a.right)
-                            ).join(line, condition=line.account == table_c.id
-                            ).select(
-                            table_a.id,
-                            line.party,
-                            Sum(
-                                Coalesce(line.debit, 0)
-                                - Coalesce(line.credit, 0)).as_('balance'),
-                            where=account_sql & party_sql & line_query,
-                            group_by=[table_a.id, line.party]))
+                    if context.get('flat_balance'):
+                        query = (table_a
+                            .join(line, condition=line.account == table_a.id))
+                    else:
+                        query = (table_a
+                            .join(table_c,
+                                condition=(table_c.left >= table_a.left)
+                                & (table_c.right <= table_a.right))
+                            .join(line, condition=line.account == table_c.id))
+                    query = query.select(
+                        table_a.id,
+                        line.party,
+                        Sum(
+                            Coalesce(line.debit, 0)
+                            - Coalesce(line.credit, 0)).as_('balance'),
+                        where=account_sql & party_sql & line_query,
+                        group_by=[table_a.id, line.party])
                     if backend.name == 'sqlite':
                         sqlite_apply_types(query, [None, None, 'NUMERIC'])
                     cursor.execute(*query)
@@ -1991,6 +2003,7 @@
             'periods': period_ids,
             'from_date': from_date,
             'to_date': to_date,
+            'flat_balance': True,
             }
 
     @classmethod
diff -r 2e53db33c262 -r 3bdcec9b622c modules/account/tests/scenario_reports.rst
--- a/modules/account/tests/scenario_reports.rst        Tue Mar 17 16:25:14 
2026 +0100
+++ b/modules/account/tests/scenario_reports.rst        Wed Mar 11 12:51:12 
2026 +0100
@@ -131,7 +131,7 @@
     >>> gl_revenue.debit
     Decimal('0.00')
     >>> gl_revenue.end_balance
-    Decimal('-10.00')
+    Decimal('0.00')
     >>> gl_revenue.line_count
     0
     >>> glp_receivable.start_balance
@@ -179,7 +179,7 @@
     >>> gl_revenue.debit
     Decimal('0.00')
     >>> gl_revenue.end_balance
-    Decimal('-10.00')
+    Decimal('0.00')
     >>> gl_revenue.line_count
     0
     >>> glp_receivable.start_balance
@@ -222,7 +222,7 @@
     >>> gl_revenue.debit
     Decimal('0.00')
     >>> gl_revenue.end_balance
-    Decimal('-10.00')
+    Decimal('0.00')
     >>> gl_revenue.line_count
     0
 
@@ -249,13 +249,13 @@
     >>> gl_child_revenue.line_count
     0
     >>> gl_revenue.start_balance
-    Decimal('-10.00')
+    Decimal('0.00')
     >>> gl_revenue.credit
     Decimal('0.00')
     >>> gl_revenue.debit
     Decimal('0.00')
     >>> gl_revenue.end_balance
-    Decimal('-10.00')
+    Decimal('0.00')
     >>> gl_revenue.line_count
     0
 
@@ -288,7 +288,7 @@
     >>> gl_revenue.debit
     Decimal('0.00')
     >>> gl_revenue.end_balance
-    Decimal('-10.00')
+    Decimal('0.00')
     >>> gl_revenue.line_count
     0
 
@@ -315,13 +315,13 @@
     >>> gl_child_revenue.line_count
     0
     >>> gl_revenue.start_balance
-    Decimal('-10.00')
+    Decimal('0.00')
     >>> gl_revenue.credit
     Decimal('0.00')
     >>> gl_revenue.debit
     Decimal('0.00')
     >>> gl_revenue.end_balance
-    Decimal('-10.00')
+    Decimal('0.00')
     >>> gl_revenue.line_count
     0
 

Reply via email to