diff --git a/account.py b/account.py index c082ccd405..a33023fae4 100644 --- a/account.py +++ b/account.py @@ -8,6 +8,7 @@ from sql.aggregate import Sum from sql.conditionals import Coalesce from trytond import backend +from trytond.i18n import gettext from trytond.model import (ModelView, ModelSQL, DeactivableMixin, fields, Unique, tree) from trytond.wizard import Wizard, StateView, StateAction, Button @@ -15,6 +16,8 @@ from trytond.pyson import Eval, If, PYSONEncoder, PYSONDecoder from trytond.transaction import Transaction from trytond.pool import Pool +from .exceptions import AccountValidationError, RootValidationError + __all__ = ['Account', 'AccountDistribution', 'OpenChartAccountStart', 'OpenChartAccount', 'AnalyticAccountEntry', 'AnalyticMixin'] @@ -109,11 +112,6 @@ class Account( super(Account, cls).__setup__() cls._order.insert(0, ('code', 'ASC')) cls._order.insert(1, ('name', 'ASC')) - cls._error_messages.update({ - 'invalid_distribution': ( - 'The distribution sum of account "%(account)s" ' - 'is not 100%%.'), - }) @classmethod def __register__(cls, module_name): @@ -153,9 +151,9 @@ class Account( if self.type != 'distribution': return if sum((d.ratio for d in self.distributions)) != 1: - self.raise_user_error('invalid_distribution', { - 'account': self.rec_name, - }) + raise AccountValidationError( + gettext('analytic_account.msg_invalid_distribution', + account=self.rec_name)) @fields.depends('company') def on_change_with_currency(self, name=None): @@ -447,7 +445,7 @@ class AnalyticAccountEntry(ModelView, ModelSQL): t = cls.__table__() cls._sql_constraints += [ ('root_origin_uniq', Unique(t, t.origin, t.root), - 'Only one account is allowed per analytic root and origin.'), + 'analytic_account.msg_root_origin_unique'), ] @classmethod @@ -502,14 +500,6 @@ class AnalyticMixin(object): analytic_accounts_size = fields.Function(fields.Integer( 'Analytic Accounts Size'), 'get_analytic_accounts_size') - @classmethod - def __setup__(cls): - super(AnalyticMixin, cls).__setup__() - cls._error_messages.update({ - 'root_account': ('Some mandatory root account are missing ' - 'on "%(name)s"'), - }) - @classmethod def __register__(cls, module_name): pool = Pool() @@ -591,7 +581,10 @@ class AnalyticMixin(object): for mandatory in all_mandatory_roots: if mandatory.company in companies: mandatory_roots.add(mandatory) - if not mandatory_roots <= analytic_roots: - cls.raise_user_error('root_account', { - 'name': analytic.rec_name, - }) + mandatory_roots.difference_update(analytic_roots) + if mandatory_roots: + raise RootValidationError( + gettext('analytic_account.msg_missing_root_account', + name=analytic.rec_name, + analytics=', '.join( + (r.rec_name for r in mandatory_roots)))) diff --git a/exceptions.py b/exceptions.py new file mode 100644 index 0000000000..671304ce46 --- /dev/null +++ b/exceptions.py @@ -0,0 +1,12 @@ +# This file is part of Tryton. The COPYRIGHT file at the top level of +# this repository contains the full copyright notices and license terms. + +from trytond.model.exceptions import ValidationError + + +class AccountValidationError(ValidationError): + pass + + +class RootValidationError(ValidationError): + pass diff --git a/line.py b/line.py index 9a1c710ba0..3721f6c295 100644 --- a/line.py +++ b/line.py @@ -46,15 +46,8 @@ class Line(ModelSQL, ModelView): ('credit_debit', Check(t, (t.credit * t.debit == 0) & (t.credit + t.debit >= 0)), - 'Wrong credit/debit values.'), + 'account.msg_line_debit_credit'), ] - cls._error_messages.update({ - 'line_on_view_account': ( - 'You can not create a move line using ' - 'view account "%s".'), - 'line_on_inactive_account': ('You can not create a move line ' - 'using inactive account "%s".'), - }) cls._order.insert(0, ('date', 'ASC')) @classmethod @@ -144,20 +137,6 @@ class Line(ModelSQL, ModelView): MoveLine.set_analytic_state(move_lines) MoveLine.save(move_lines) - @classmethod - def validate(cls, lines): - super(Line, cls).validate(lines) - for line in lines: - line.check_account() - - def check_account(self): - if self.account.type == 'view': - self.raise_user_error('line_on_view_account', - (self.account.rec_name,)) - if not self.account.active: - self.raise_user_error('line_on_inactive_account', - (self.account.rec_name,)) - class Move(metaclass=PoolMeta): __name__ = 'account.move' diff --git a/message.xml b/message.xml new file mode 100644 index 0000000000..8afd274e7d --- /dev/null +++ b/message.xml @@ -0,0 +1,16 @@ + + + + + + To save account "%(account)s" the sum of their distributions must be 100%%. + + + To save "%(name)s" you must set a value for analytics "%(analytics)s". + + + Only one account is allowed by analytic root and origin. + + + diff --git a/tryton.cfg b/tryton.cfg index da1f4280fd..9f4a2b899c 100644 --- a/tryton.cfg +++ b/tryton.cfg @@ -12,3 +12,4 @@ xml: account.xml line.xml rule.xml + message.xml