Add ir.message and use custom exceptions

issue3672
This commit is contained in:
Cédric Krier committed 2018-12-29 14:20:29 +01:00
1 parent f301ebdf26
commit 432a6cdbd5
7 files changed
+121 -34

No files matched your search

+55 -28
View File
@@ -9,14 +9,19 @@ from sql.conditionals import Coalesce
from sql.aggregate import Max, Sum
from trytond.config import config
from trytond.i18n import gettext
from trytond.model import Workflow, ModelView, ModelSQL, fields, Check, \
sequence_ordered, DictSchemaMixin
from trytond.model.exceptions import AccessError
from trytond.pyson import Eval, If, Bool
from trytond.transaction import Transaction
from trytond.pool import Pool
from trytond.wizard import Wizard, StateView, StateAction, Button
from trytond.modules.company import CompanyReport
from .exceptions import (
StatementValidateError, StatementValidateWarning, StatementPostError)
__all__ = ['Statement', 'Line', 'LineGroup',
'Origin', 'OriginInformation',
'ImportStatementStart', 'ImportStatement',
@@ -139,18 +144,6 @@ class Statement(Workflow, ModelSQL, ModelView):
def __setup__(cls):
super(Statement, cls).__setup__()
cls._order[0] = ('id', 'DESC')
cls._error_messages.update({
'wrong_end_balance': 'End Balance must be "%s".',
'wrong_total_amount': 'Total Amount must be "%s".',
'wrong_number_of_lines': 'Number of Lines must be "%s".',
'delete_cancel': ('Statement "%s" must be cancelled before '
'deletion.'),
'paid_invoice_draft_statement': ('There are paid invoices on '
'draft statements.'),
'post_with_pending_amount': ('Origin line "%(origin)s" '
'of statement "%(statement)s" still has a pending amount '
'of "%(amount)s".'),
})
cls._transitions |= set((
('draft', 'validated'),
('draft', 'cancel'),
@@ -377,7 +370,9 @@ class Statement(Workflow, ModelSQL, ModelView):
cls.cancel(statements)
for statement in statements:
if statement.state != 'cancel':
cls.raise_user_error('delete_cancel', (statement.rec_name,))
raise AccessError(
gettext('account_statement.msg_statement_delete_cancel',
statement=statement.rec_name))
super(Statement, cls).delete(statements)
@classmethod
@@ -390,12 +385,18 @@ class Statement(Workflow, ModelSQL, ModelView):
pool = Pool()
Lang = pool.get('ir.lang')
end_balance = (self.start_balance
amount = (self.start_balance
+ sum(l.amount for l in self.lines))
if end_balance != self.end_balance:
if amount != self.end_balance:
lang = Lang.get()
amount = lang.currency(end_balance, self.journal.currency)
self.raise_user_error('wrong_end_balance', amount)
end_balance = lang.currency(
self.end_balance, self.journal.currency)
amount = lang.currency(amount, self.journal.currency)
raise StatementValidateError(
gettext('account_statement.msg_statement_wrong_end_balance',
statement=self.rec_name,
end_balance=end_balance,
amount=amount))
def validate_amount(self):
pool = Pool()
@@ -404,13 +405,29 @@ class Statement(Workflow, ModelSQL, ModelView):
amount = sum(l.amount for l in self.lines)
if amount != self.total_amount:
lang = Lang.get()
total_amount = lang.currency(
self.total_amount, self.journal.currency)
amount = lang.currency(amount, self.journal.currency)
self.raise_user_error('wrong_total_amount', amount)
raise StatementValidateError(
gettext('account_statement.msg_statement_wrong_total_amount',
statement=self.rec_name,
total_amount=total_amount,
amount=amount))
def validate_number_of_lines(self):
number = len(list(self.grouped_lines))
if number != self.number_of_lines:
self.raise_user_error('wrong_number_of_lines', number)
if number > self.number_of_lines:
raise StatementValidateError(
gettext('account_statement'
'.msg_statement_wrong_number_of_lines_remove',
statement=self.rec_name,
n=number - self.number_of_lines))
elif number < self.number_of_lines:
raise StatementValidateError(
gettext('account_statement'
'.msg_statement_wrong_number_of_lines_remove',
statement=self.rec_name,
n=self.number_of_lines - number))
@classmethod
@ModelView.button
@@ -418,6 +435,7 @@ class Statement(Workflow, ModelSQL, ModelView):
def validate_statement(cls, statements):
pool = Pool()
Line = pool.get('account.statement.line')
Warning = pool.get('res.user.warning')
for statement in statements:
getattr(statement, 'validate_%s' % statement.validation)()
@@ -433,7 +451,10 @@ class Statement(Workflow, ModelSQL, ModelView):
])
if common_lines:
warning_key = '_'.join(str(l.id) for l in common_lines)
cls.raise_user_warning(warning_key, 'paid_invoice_draft_statement')
if Warning.check(warning_key):
raise StatementValidateWarning(warning_key,
gettext('account_statement'
'.msg_statement_paid_invoice_draft'))
Line.write(common_lines, {
'invoice': None,
})
@@ -537,15 +558,21 @@ class Statement(Workflow, ModelSQL, ModelView):
@ModelView.button
@Workflow.transition('posted')
def post(cls, statements):
StatementLine = Pool().get('account.statement.line')
pool = Pool()
Lang = pool.get('ir.lang')
StatementLine = pool.get('account.statement.line')
for statement in statements:
for origin in statement.origins:
if origin.pending_amount:
cls.raise_user_error('post_with_pending_amount', {
'origin': origin.rec_name,
'amount': origin.pending_amount,
'statement': statement.rec_name,
})
lang = Lang.get()
amount = lang.currency(
origin.pending_amount, statement.journal.currency)
raise StatementPostError(
gettext('account_statement'
'.msg_statement_post_pending_amount',
statement=statement.rec_name,
amount=amount,
origin=origin.rec_name))
lines = [l for s in statements for l in s.lines]
StatementLine.post_move(lines)
@@ -674,7 +701,7 @@ class Line(
t = cls.__table__()
cls._sql_constraints += [
('check_statement_line_amount', Check(t, t.amount != 0),
'Amount should be a positive or negative value.'),
'account_statement.msg_line_amount_non_zero'),
]
@staticmethod