From 0dd4a2ccff12ab00eaede0c8c71c81dfd22a98df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Tue, 4 Jan 2022 22:24:20 +0100 Subject: [PATCH] Compute debit/credit and lines on either dates or periods issue11095 review376331002 (grafted from f748b75e32c835ce769580e4fc8dc8e1bde3abca) --HG-- branch : 6.0 --- account.py | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/account.py b/account.py index 4674be0820..a8e8222a4b 100644 --- a/account.py +++ b/account.py @@ -1804,15 +1804,28 @@ class _GeneralLedgerAccount(ActivePeriodMixin, ModelSQL, ModelView): if operator_(getattr(a, fname), operand)] return [('id', 'in', ids)] + @classmethod + def _debit_credit_context(cls): + period_ids, from_date, to_date = None, None, None + context = Transaction().context + if context.get('start_period') or context.get('end_period'): + start_period_ids = set(cls.get_period_ids('start_balance')) + end_period_ids = set(cls.get_period_ids('end_balance')) + period_ids = list(end_period_ids.difference(start_period_ids)) + elif context.get('from_date') or context.get('end_date'): + _, from_date = cls.get_dates('start_balance') + _, to_date = cls.get_dates('end_balance') + return { + 'periods': period_ids, + 'from_date': from_date, + 'to_date': to_date, + } + @classmethod def get_debit_credit(cls, records, name): Account = cls._get_account() - start_period_ids = cls.get_period_ids('start_%s' % name) - end_period_ids = cls.get_period_ids('end_%s' % name) - periods_ids = list( - set(end_period_ids).difference(set(start_period_ids))) - with Transaction().set_context(periods=periods_ids): + with Transaction().set_context(cls._debit_credit_context()): accounts = Account.browse(records) return {a.id: getattr(a, name) for a in accounts} @@ -1820,11 +1833,7 @@ class _GeneralLedgerAccount(ActivePeriodMixin, ModelSQL, ModelView): def search_debit_credit(cls, name, domain): Account = cls._get_account() - start_period_ids = cls.get_period_ids('start_%s' % name) - end_period_ids = cls.get_period_ids('end_%s' % name) - periods_ids = list( - set(end_period_ids).difference(set(start_period_ids))) - with Transaction().set_context(periods=periods_ids): + with Transaction().set_context(cls._debit_credit_context()): accounts = Account.search([], order=[]) _, operator_, operand = domain @@ -2147,10 +2156,7 @@ class GeneralLedgerLine(ModelSQL, ModelView): else: column = Column(line, fname).as_(fname) columns.append(column) - start_period_ids = set(LedgerAccount.get_period_ids('start_balance')) - end_period_ids = set(LedgerAccount.get_period_ids('end_balance')) - period_ids = list(end_period_ids.difference(start_period_ids)) - with Transaction().set_context(periods=period_ids): + with Transaction().set_context(LedgerAccount._debit_credit_context()): line_query, fiscalyear_ids = Line.query_get(line) return line.join(move, condition=line.move == move.id ).join(account, condition=line.account == account.id