changeset 60b961dddc4b in modules/account:default
details: https://hg.tryton.org/modules/account?cmd=changeset&node=60b961dddc4b
description:
        Ensure move line domain is still valid after account modification

        We can not close or remove the type of an account if it has move lines.

        issue10300
        review376321002
diffstat:

 account.py    |  30 +++++++++++++++++++++++++++++-
 exceptions.py |   6 +++++-
 message.xml   |   6 ++++++
 3 files changed, 40 insertions(+), 2 deletions(-)

diffs (86 lines):

diff -r 76966ba40b58 -r 60b961dddc4b account.py
--- a/account.py        Tue Jan 04 22:19:48 2022 +0100
+++ b/account.py        Tue Jan 04 22:22:17 2022 +0100
@@ -26,7 +26,8 @@
     Button, StateAction, StateTransition, StateView, Wizard)
 
 from .common import ActivePeriodMixin, PeriodMixin
-from .exceptions import ChartWarning, SecondCurrencyError
+from .exceptions import (
+    AccountValidationError, ChartWarning, SecondCurrencyError)
 
 
 def inactive_records(func):
@@ -912,6 +913,7 @@
     def validate(cls, accounts):
         super(Account, cls).validate(accounts)
         cls.check_second_currency(accounts)
+        cls.check_move_domain(accounts)
 
     @staticmethod
     def default_left():
@@ -1138,6 +1140,32 @@
                         account=account.rec_name))
 
     @classmethod
+    def check_move_domain(cls, accounts):
+        pool = Pool()
+        Line = pool.get('account.move.line')
+        accounts = [a for a in accounts if a.closed or not a.type]
+        for sub_accounts in grouped_slice(accounts):
+            sub_accounts = list(sub_accounts)
+            if Line.search([
+                        ('account', 'in', [a.id for a in sub_accounts]),
+                        ], order=[], limit=1):
+                for account in sub_accounts:
+                    if not account.closed:
+                        continue
+                    lines = Line.search([
+                            ('account', '=', account.id),
+                            ], order=[], limit=1)
+                    if lines:
+                        if account.closed:
+                            raise AccountValidationError(gettext(
+                                    'account.msg_account_closed_lines',
+                                    account=account.rec_name))
+                        elif account.type:
+                            raise AccountValidationError(gettext(
+                                    'account.msg_account_no_type_lines',
+                                    account=account.rec_name))
+
+    @classmethod
     def copy(cls, accounts, default=None):
         if default is None:
             default = {}
diff -r 76966ba40b58 -r 60b961dddc4b exceptions.py
--- a/exceptions.py     Tue Jan 04 22:19:48 2022 +0100
+++ b/exceptions.py     Tue Jan 04 22:22:17 2022 +0100
@@ -53,7 +53,11 @@
     pass
 
 
-class SecondCurrencyError(ValidationError):
+class AccountValidationError(ValidationError):
+    pass
+
+
+class SecondCurrencyError(AccountValidationError):
     pass
 
 
diff -r 76966ba40b58 -r 60b961dddc4b message.xml
--- a/message.xml       Tue Jan 04 22:19:48 2022 +0100
+++ b/message.xml       Tue Jan 04 22:22:17 2022 +0100
@@ -95,6 +95,12 @@
         <record model="ir.message" 
id="msg_account_invalid_lines_second_currency">
             <field name="text">To set a second currency for account 
"%(account)s", its lines must have the same second currency 
"%(currency)s".</field>
         </record>
+        <record model="ir.message" id="msg_account_closed_lines">
+            <field name="text">You cannot close account "%(account)s" because 
it has move lines.</field>
+        </record>
+        <record model="ir.message" id="msg_account_no_type_lines">
+            <field name="text">You cannot remove type of account "%(account)s" 
because it has move lines.</field>
+        </record>
 
         <record model="ir.message" id="msg_deferral_unique">
             <field name="text">Deferral must be unique by account and fiscal 
year.</field>

Reply via email to