From fefa168782ed1d1889e793bb246b24c24f19a921 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Tue, 4 Jan 2022 22:22:17 +0100 Subject: [PATCH] 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 --- account.py | 30 +++++++++++++++++++++++++++++- exceptions.py | 6 +++++- message.xml | 6 ++++++ 3 files changed, 40 insertions(+), 2 deletions(-) diff --git a/account.py b/account.py index f891101562..6b1c1004d1 100644 --- a/account.py +++ b/account.py @@ -26,7 +26,8 @@ from trytond.wizard import ( 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 @@ class Account(AccountMixin(), ActivePeriodMixin, tree(), ModelSQL, ModelView): def validate(cls, accounts): super(Account, cls).validate(accounts) cls.check_second_currency(accounts) + cls.check_move_domain(accounts) @staticmethod def default_left(): @@ -1137,6 +1139,32 @@ class Account(AccountMixin(), ActivePeriodMixin, tree(), ModelSQL, ModelView): currency=account.second_currency.rec_name, 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: diff --git a/exceptions.py b/exceptions.py index e38c1c48d7..cd5ed598b3 100644 --- a/exceptions.py +++ b/exceptions.py @@ -53,7 +53,11 @@ class AccountMissing(UserError): pass -class SecondCurrencyError(ValidationError): +class AccountValidationError(ValidationError): + pass + + +class SecondCurrencyError(AccountValidationError): pass diff --git a/message.xml b/message.xml index ce9e04bda4..1d2507ec3a 100644 --- a/message.xml +++ b/message.xml @@ -95,6 +95,12 @@ this repository contains the full copyright notices and license terms. --> To set a second currency for account "%(account)s", its lines must have the same second currency "%(currency)s". + + You cannot close account "%(account)s" because it has move lines. + + + You cannot remove type of account "%(account)s" because it has move lines. + Deferral must be unique by account and fiscal year.