changeset 226b2fb53672 in modules/account:5.0
details: https://hg.tryton.org/modules/account?cmd=changeset&node=226b2fb53672
description:
        Compute deferral balance by cumulating children account

        issue11057
        review376391002
        (grafted from 5457ef491963844c12b1459550260718356bfffd)
diffstat:

 account.py |  35 +++++++++++++++++++++++++++++++++--
 1 files changed, 33 insertions(+), 2 deletions(-)

diffs (45 lines):

diff -r 3a6589264869 -r 226b2fb53672 account.py
--- a/account.py        Tue Feb 01 18:32:49 2022 +0100
+++ b/account.py        Thu Feb 03 22:20:43 2022 +0100
@@ -1183,8 +1183,39 @@
     def default_amount_second_currency(cls):
         return Decimal(0)
 
-    def get_balance(self, name):
-        return self.debit - self.credit
+    @classmethod
+    def get_balance(cls, deferrals, name):
+        pool = Pool()
+        Account = pool.get('account.account')
+        cursor = Transaction().connection.cursor()
+
+        table = cls.__table__()
+        table_child = cls.__table__()
+        account = Account.__table__()
+        account_child = Account.__table__()
+        balances = defaultdict(Decimal)
+
+        for sub_deferrals in grouped_slice(deferrals):
+            red_sql = reduce_ids(table.id, [d.id for d in sub_deferrals])
+            cursor.execute(*table
+                .join(account, condition=table.account == account.id)
+                .join(account_child,
+                    condition=(account_child.left >= account.left)
+                    & (account_child.right <= account.right))
+                .join(table_child,
+                    condition=(table_child.account == account_child.id)
+                    & (table_child.fiscalyear == table.fiscalyear))
+                .select(
+                    table.id,
+                    Sum(table_child.debit - table_child.credit),
+                    where=red_sql,
+                    group_by=table.id))
+            balances.update(dict(cursor))
+
+        for id_, balance in balances.items():
+            if not isinstance(balance, Decimal):
+                balances[id_] = Decimal(str(balance))
+        return balances
 
     def get_currency(self, name):
         return self.account.currency.id

Reply via email to