changeset cb3f2cd90fa6 in modules/analytic_account:default
details: 
https://hg.tryton.org/modules/analytic_account?cmd=changeset&node=cb3f2cd90fa6
description:
        Prevent changing root of account with entries

        issue11018
        review431821003
diffstat:

 account.py                          |  23 ++++++++++++++++++++++-
 message.xml                         |   3 +++
 tests/scenario_analytic_account.rst |  16 ++++++++++++++++
 3 files changed, 41 insertions(+), 1 deletions(-)

diffs (78 lines):

diff -r 367d23523055 -r cb3f2cd90fa6 account.py
--- a/account.py        Mon Oct 31 16:52:03 2022 +0100
+++ b/account.py        Sat Nov 05 09:48:14 2022 +0100
@@ -11,10 +11,11 @@
 from trytond.i18n import gettext
 from trytond.model import (
     DeactivableMixin, Index, ModelSQL, ModelView, Unique, fields, tree)
+from trytond.model.exceptions import AccessError
 from trytond.modules.currency.fields import Monetary
 from trytond.pool import Pool
 from trytond.pyson import Eval, If, PYSONDecoder, PYSONEncoder
-from trytond.tools import is_full_text, lstrip_wildcard
+from trytond.tools import grouped_slice, is_full_text, lstrip_wildcard
 from trytond.transaction import Transaction
 
 from .exceptions import AccountValidationError
@@ -302,6 +303,26 @@
             assert sum(a for _, a in result) == amount
             return result
 
+    @classmethod
+    def write(cls, *args):
+        pool = Pool()
+        Entry = pool.get('analytic.account.entry')
+        actions = iter(args)
+        for records, values in zip(actions, actions):
+            if 'root' in values:
+                for sub_records in grouped_slice(records):
+                    entries = Entry.search([
+                            ('account', 'in', list(map(int, sub_records))),
+                            ],
+                        limit=1, order=[])
+                    if entries:
+                        entry, = entries
+                        raise AccessError(gettext(
+                                'analytic_account'
+                                '.msg_analytic_account_root_change',
+                                account=entry.account.rec_name))
+        super().write(*args)
+
 
 class AccountContext(ModelView):
     'Open Chart of Accounts'
diff -r 367d23523055 -r cb3f2cd90fa6 message.xml
--- a/message.xml       Mon Oct 31 16:52:03 2022 +0100
+++ b/message.xml       Sat Nov 05 09:48:14 2022 +0100
@@ -9,5 +9,8 @@
         <record model="ir.message" id="msg_root_origin_unique">
             <field name="text">Only one account is allowed by analytic root 
and origin.</field>
         </record>
+        <record model="ir.message" id="msg_analytic_account_root_change">
+            <field name="text">You cannot change the root of the analytic 
account "%(account)s" which is associated with analytic account entry.</field>
+        </record>
     </data>
 </tryton>
diff -r 367d23523055 -r cb3f2cd90fa6 tests/scenario_analytic_account.rst
--- a/tests/scenario_analytic_account.rst       Mon Oct 31 16:52:03 2022 +0100
+++ b/tests/scenario_analytic_account.rst       Sat Nov 05 09:48:14 2022 +0100
@@ -183,3 +183,19 @@
     >>> move.click('post')
     >>> [l for l in move.lines if l.analytic_lines]
     []
+
+Prevent changing root of account with entries::
+
+    >>> root2 = AnalyticAccount(type='root', name="Root2")
+    >>> root2.save()
+    >>> analytic_account.root = root2
+    >>> analytic_account.save()  # doctest: +IGNORE_EXCEPTION_DETAIL
+    Traceback (most recent call last):
+        ...
+    AccessError: ...
+
+    >>> analytic_account.reload()
+    >>> analytic_account.code = "1"
+    >>> analytic_account.save()
+    >>> analytic_account.code
+    '1'

Reply via email to