changeset efb8001e1ac1 in modules/account:default
details: https://hg.tryton.org/modules/account?cmd=changeset;node=efb8001e1ac1
description:
Add ir.message and use custom exceptions
issue3672
diffstat:
account.py | 76 +++++++++----------------
exceptions.py | 77 ++++++++++++++++++++++++++
fiscalyear.py | 67 ++++++++++++----------
journal.py | 36 +++++------
message.xml | 171 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
move.py | 167 ++++++++++++++++++++++++--------------------------------
party.py | 44 ++++----------
period.py | 86 +++++++++++++---------------
tax.py | 39 +++---------
tryton.cfg | 1 +
10 files changed, 465 insertions(+), 299 deletions(-)
diffs (1314 lines):
diff -r 7ebad68e53e0 -r efb8001e1ac1 account.py
--- a/account.py Wed Dec 19 17:08:24 2018 +0100
+++ b/account.py Sat Dec 29 14:20:28 2018 +0100
@@ -10,8 +10,10 @@
from sql.aggregate import Sum, Max
from sql.conditionals import Coalesce, Case
+from trytond.i18n import gettext
from trytond.model import (
ModelView, ModelSQL, fields, Unique, sequence_ordered, tree)
+from trytond.model.exceptions import AccessError
from trytond.wizard import Wizard, StateView, StateAction, StateTransition, \
Button
from trytond.report import Report
@@ -21,6 +23,7 @@
from trytond.pool import Pool
from .common import PeriodMixin, ActivePeriodMixin
+from .exceptions import SecondCurrencyError, ChartWarning
__all__ = ['TypeTemplate', 'Type', 'OpenType',
'AccountTemplate', 'AccountTemplateTaxTemplate',
@@ -698,25 +701,6 @@
'readonly': (Bool(Eval('template', -1))
& ~Eval('template_override', False)),
}
- cls._error_messages.update({
- 'delete_account_containing_move_lines': ('You can not delete '
- 'account "%s" because it has move lines.'),
- 'invalid_second_currency_type': (
- 'The kind of Account "%(account)s" '
- 'does not allow to set a second currency.\n'
- 'Only "Other" type allows.'),
- 'invalid_second_currency_deferral': (
- 'The Account "%(account)s" can not have a second currency '
- 'because it is not deferral.'),
- 'invalid_second_currency_lines': (
- 'The currency "%(currency)s" of '
- 'Account "%(account)s" is not compatible '
- 'with existing lines.'),
- })
- cls._sql_error_messages.update({
- 'parent_fkey': ('You can not delete accounts that have '
- 'children.'),
- })
cls._order.insert(0, ('code', 'ASC'))
cls._order.insert(1, ('name', 'ASC'))
@@ -969,23 +953,25 @@
for account in accounts:
if not account.second_currency:
continue
- if account.kind in {'payable', 'revenue', 'receivable', 'expense'}:
- cls.raise_user_error('invalid_second_currency_type', {
- 'account': account.rec_name,
- })
+ if account.kind != 'other':
+ raise SecondCurrencyError(
+ gettext('account.msg_account_invalid_type_second_currency',
+ account=account.rec_name))
if not account.deferral:
- cls.raise_user_error('invalid_second_currency_deferral', {
- 'account': account.rec_name,
- })
+ raise SecondCurrencyError(
+ gettext('account'
+ '.msg_account_invalid_deferral_second_currency',
+ account=account.rec_name))
lines = Line.search([
('account', '=', account.id),
('second_currency', '!=', account.second_currency.id),
], order=[], limit=1)
if lines:
- cls.raise_user_error('invalid_second_currency_lines', {
- 'currency': account.second_currency.rec_name,
- 'account': account.rec_name,
- })
+ raise SecondCurrencyError(
+ gettext('account'
+ '.msg_account_invalid_lines_second_currency',
+ currency=account.second_currency.rec_name,
+ account=account.rec_name))
@classmethod
def copy(cls, accounts, default=None):
@@ -1009,8 +995,9 @@
('account', 'in', [a.id for a in childs]),
])
if lines:
- cls.raise_user_error('delete_account_containing_move_lines', (
- lines[0].account.rec_name,))
+ raise AccessError(
+ gettext('account.msg_delete_account_with_move_lines',
+ account=lines[0].account.rec_name))
super(Account, cls).delete(accounts)
def update_account(self, template2account=None, template2type=None):
@@ -1157,11 +1144,8 @@
t = cls.__table__()
cls._sql_constraints += [
('deferral_uniq', Unique(t, t.account, t.fiscalyear),
- 'Deferral must be unique by account and fiscal year'),
+ 'account.msg_deferral_unique'),
]
- cls._error_messages.update({
- 'write_deferral': 'You can not modify Account Deferral records',
- })
@classmethod
def default_amount_second_currency(cls):
@@ -1199,7 +1183,7 @@
@classmethod
def write(cls, deferrals, values, *args):
- cls.raise_user_error('write_deferral')
+ raise AccessError(gettext('account.msg_write_deferral'))
class AccountTax(ModelSQL):
@@ -2159,14 +2143,6 @@
])
create_properties = StateTransition()
- @classmethod
- def __setup__(cls):
- super(CreateChart, cls).__setup__()
- cls._error_messages.update({
- 'account_chart_exists': ('A chart of accounts already exists '
- 'for the company "%(company)s".')
- })
-
def transition_create_account(self):
pool = Pool()
TaxCodeTemplate = pool.get('account.tax.code.template')
@@ -2177,6 +2153,7 @@
pool.get('account.tax.rule.line.template')
Config = pool.get('ir.configuration')
Account = pool.get('account.account')
+ Warning = pool.get('res.user.warning')
transaction = Transaction()
company = self.account.company
@@ -2184,10 +2161,11 @@
with transaction.set_user(0):
accounts = Account.search([('company', '=', company.id)], limit=1)
if accounts:
- self.raise_user_warning('duplicated_chart.%d' % company.id,
- 'account_chart_exists', {
- 'company': company.rec_name,
- })
+ key = 'duplicated_chart.%d' % company.id
+ if Warning.check(key):
+ raise ChartWarning(key,
+ gettext('account.msg_account_chart_exists',
+ company=company.rec_name))
with transaction.set_context(language=Config.get_language(),
company=company.id):
diff -r 7ebad68e53e0 -r efb8001e1ac1 exceptions.py
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/exceptions.py Sat Dec 29 14:20:28 2018 +0100
@@ -0,0 +1,77 @@
+# 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.exceptions import UserError, UserWarning
+from trytond.model.exceptions import ValidationError
+
+
+class PeriodNotFoundError(UserError):
+ pass
+
+
+class PeriodValidationError(ValidationError):
+ pass
+
+
+class ClosePeriodError(PeriodValidationError):
+ pass
+
+
+class PeriodDatesError(PeriodValidationError):
+ pass
+
+
+class PeriodSequenceError(PeriodValidationError):
+ pass
+
+
+class FiscalYearNotFoundError(UserError):
+ pass
+
+
+class FiscalYearValidationError(ValidationError):
+ pass
+
+
+class FiscalYearDatesError(FiscalYearValidationError):
+ pass
+
+
+class FiscalYearSequenceError(FiscalYearValidationError):
+ pass
+
+
+class FiscalYearCloseError(UserError):
+ pass
+
+
+class FiscalYearReOpenError(UserError):
+ pass
+
+
+class AccountMissing(UserError):
+ pass
+
+
+class SecondCurrencyError(ValidationError):
+ pass
+
+
+class ChartWarning(UserWarning):
+ pass
+
+
+class PostError(UserError):
+ pass
+
+
+class MoveDatesError(ValidationError):
+ pass
+
+
+class CancelWarning(UserWarning):
+ pass
+
+
+class ReconciliationError(ValidationError):
+ pass
diff -r 7ebad68e53e0 -r efb8001e1ac1 fiscalyear.py
--- a/fiscalyear.py Wed Dec 19 17:08:24 2018 +0100
+++ b/fiscalyear.py Sat Dec 29 14:20:28 2018 +0100
@@ -2,12 +2,18 @@
# this repository contains the full copyright notices and license terms.
from dateutil.relativedelta import relativedelta
+
+from trytond.i18n import gettext
from trytond.model import ModelView, ModelSQL, Workflow, fields
+from trytond.model.exceptions import AccessError
from trytond.wizard import Wizard, StateView, StateAction, Button
from trytond.pyson import Eval, If, PYSONEncoder
from trytond.transaction import Transaction
from trytond.pool import Pool
+from .exceptions import (FiscalYearNotFoundError, FiscalYearDatesError,
+ FiscalYearSequenceError, FiscalYearCloseError, FiscalYearReOpenError)
+
__all__ = ['FiscalYear',
'BalanceNonDeferralStart', 'BalanceNonDeferral',
'RenewFiscalYearStart', 'RenewFiscalYear']
@@ -57,21 +63,6 @@
def __setup__(cls):
super(FiscalYear, cls).__setup__()
cls._order.insert(0, ('start_date', 'ASC'))
- cls._error_messages.update({
- 'change_post_move_sequence': ('You can not change the post '
- 'move sequence in fiscal year "%s".'),
- 'no_fiscalyear_date': 'No fiscal year defined for "%s".',
- 'fiscalyear_overlaps': ('Fiscal year "%(first)s" and '
- '"%(second)s" overlap.'),
- 'different_post_move_sequence': ('Fiscal year "%(first)s" and '
- '"%(second)s" have the same post move sequence.'),
- 'account_balance_not_zero': ('The balance of the account "%s" '
- 'must be zero.'),
- 'close_error': ('You can not close fiscal year "%s" until you '
- 'close all previous fiscal years.'),
- 'reopen_error': ('You can not reopen fiscal year "%s" until '
- 'you reopen all later fiscal years.'),
- })
cls._transitions |= set((
('open', 'close'),
('close', 'locked'),
@@ -142,10 +133,10 @@
second_id = cursor.fetchone()
if second_id:
second = self.__class__(second_id[0])
- self.raise_user_error('fiscalyear_overlaps', {
- 'first': self.rec_name,
- 'second': second.rec_name,
- })
+ raise FiscalYearDatesError(
+ gettext('account.msg_fiscalyear_overlap',
+ first=self.rec_name,
+ second=second.rec_name))
def check_post_move_sequence(self):
years = self.search([
@@ -153,13 +144,15 @@
('id', '!=', self.id),
])
if years:
- self.raise_user_error('different_post_move_sequence', {
- 'first': self.rec_name,
- 'second': years[0].rec_name,
- })
+ raise FiscalYearSequenceError(
+ gettext('account.msg_fiscalyear_different_post_move_sequence',
+ first=self.rec_name,
+ second=years[0].rec_name))
@classmethod
def write(cls, *args):
+ pool = Pool()
+ Move = pool.get('account.move')
actions = iter(args)
for fiscalyears, values in zip(actions, actions):
if values.get('post_move_sequence'):
@@ -167,8 +160,14 @@
if (fiscalyear.post_move_sequence
and fiscalyear.post_move_sequence.id !=
values['post_move_sequence']):
- cls.raise_user_error('change_post_move_sequence', (
- fiscalyear.rec_name,))
+ if Move.search([
+ ('period.fiscalyear', '=', fiscalyear.id),
+ ('state', '=', 'posted'),
+ ]):
+ raise AccessError(
+ gettext('account.'
+ 'msg_change_fiscalyear_post_move_sequence',
+ fiscalyear=fiscalyear.rec_name))
super(FiscalYear, cls).write(*args)
@classmethod
@@ -237,7 +236,9 @@
if not fiscalyears:
if exception:
lang = Lang.get()
- cls.raise_user_error('no_fiscalyear_date', lang.strftime(date))
+ raise FiscalYearNotFoundError(
+ gettext('account.msg_no_fiscalyear_date',
+ date=lang.strftime(date)))
else:
return None
return fiscalyears[0].id
@@ -252,8 +253,10 @@
return
if not account.deferral:
if not Currency.is_zero(self.company.currency, account.balance):
- self.raise_user_error('account_balance_not_zero',
- error_args=(account.rec_name,))
+ raise FiscalYearCloseError(
+ gettext('account'
+ '.msg_close_fiscalyear_account_balance_not_zero',
+ account=account.rec_name))
else:
deferral = Deferral()
deferral.account = account
@@ -288,7 +291,9 @@
('state', '=', 'open'),
('company', '=', fiscalyear.company.id),
]):
- cls.raise_user_error('close_error', (fiscalyear.rec_name,))
+ raise FiscalYearCloseError(
+ gettext('account.msg_close_fiscalyear_earlier',
+ fiscalyear=fiscalyear.rec_name))
periods = Period.search([
('fiscalyear', '=', fiscalyear.id),
@@ -319,7 +324,9 @@
('state', '!=', 'open'),
('company', '=', fiscalyear.company.id),
]):
- cls.raise_user_error('reopen_error')
+ raise FiscalYearReOpenError(
+ gettext('account.msg_reopen_fiscalyear_later',
+ fiscalyear=fiscalyear.rec_name))
deferrals = Deferral.search([
('fiscalyear', '=', fiscalyear.id),
diff -r 7ebad68e53e0 -r efb8001e1ac1 journal.py
--- a/journal.py Wed Dec 19 17:08:24 2018 +0100
+++ b/journal.py Sat Dec 29 14:20:28 2018 +0100
@@ -4,9 +4,11 @@
from sql.aggregate import Sum
+from trytond import backend
+from trytond.i18n import gettext
from trytond.model import (
ModelView, ModelSQL, Workflow, DeactivableMixin, fields, Unique)
-from trytond import backend
+from trytond.model.exceptions import AccessError
from trytond.pyson import Eval, Bool
from trytond.transaction import Transaction
from trytond.pool import Pool
@@ -209,18 +211,8 @@
t = cls.__table__()
cls._sql_constraints += [
('journal_period_uniq', Unique(t, t.journal, t.period),
- 'You can only open one journal per period.'),
+ 'account.msg_journal_period_unique'),
]
-
- cls._error_messages.update({
- 'modify_del_journal_period': ('You can not modify/delete '
- 'journal - period "%s" because it has moves.'),
- 'create_journal_period': ('You can not create a '
- 'journal - period on closed period "%s".'),
- 'open_journal_period': ('You can not open '
- 'journal - period "%(journal_period)s" because period '
- '"%(period)s" is closed.'),
- })
cls._transitions |= set((
('open', 'close'),
('close', 'open'),
@@ -279,8 +271,9 @@
('period', '=', period.period.id),
], limit=1)
if moves:
- cls.raise_user_error('modify_del_journal_period', (
- period.rec_name,))
+ raise AccessError(
+ gettext('account.msg_modify_delete_journal_period_moves',
+ journal_period=period.rec_name))
@classmethod
def create(cls, vlist):
@@ -289,8 +282,10 @@
if vals.get('period'):
period = Period(vals['period'])
if period.state != 'open':
- cls.raise_user_error('create_journal_period', (
- period.rec_name,))
+ raise AccessError(
+ gettext('account'
+ '.msg_create_journal_period_closed_period',
+ period=period.rec_name))
return super(JournalPeriod, cls).create(vlist)
@classmethod
@@ -303,10 +298,11 @@
if values.get('state') == 'open':
for journal_period in journal_periods:
if journal_period.period.state != 'open':
- cls.raise_user_error('open_journal_period', {
- 'journal_period': journal_period.rec_name,
- 'period': journal_period.period.rec_name,
- })
+ raise AccessError(
+ gettext('account'
+ '.msg_open_journal_period_closed_period',
+ journal_period=journal_period.rec_name,
+ period=journal_period.period.rec_name))
super(JournalPeriod, cls).write(*args)
@classmethod
diff -r 7ebad68e53e0 -r efb8001e1ac1 message.xml
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/message.xml Sat Dec 29 14:20:28 2018 +0100
@@ -0,0 +1,171 @@
+<?xml version="1.0"?>
+<!-- This file is part of Tryton. The COPYRIGHT file at the top level of
+this repository contains the full copyright notices and license terms. -->
+<tryton>
+ <data group="1">
+ <record model="ir.message" id="msg_no_period_date">
+ <field name="text">To continue, you must create a period for the
date "%(date)s".</field>
+ </record>
+ <record model="ir.message" id="msg_modify_delete_period_moves">
+ <field name="text">You cannot modify or delete period "%(period)s"
because it contains moves.</field>
+ </record>
+ <record model="ir.message" id="msg_create_period_closed_fiscalyear">
+ <field name="text">You cannot create a period in fiscal year
"%(fiscalyear)s" because it is closed.</field>
+ </record>
+ <record model="ir.message" id="msg_open_period_closed_fiscalyear">
+ <field name="text">You cannot reopen period "%(period)s" because
its fiscal year "%(fiscalyear)s" is closed.</field>
+ </record>
+ <record model="ir.message" id="msg_change_period_post_move_sequence">
+ <field name="text">You cannot change the post move sequence in
period "%(period)s" because it has posted moves.</field>
+ </record>
+ <record model="ir.message" id="msg_close_period_non_posted_moves">
+ <field name="text">To close period "%(period)s" you must post the
moves "%(moves)s".</field>
+ </record>
+ <record model="ir.message" id="msg_period_overlap">
+ <field name="text">The periods "%(first)s" and "%(second)s"
overlap, you must use different dates.</field>
+ </record>
+ <record model="ir.message" id="msg_period_same_sequence">
+ <field name="text">You cannot use the same post move sequence for
the periods "%(first)s" and "%(second)s" because they are from different fiscal
years.</field>
+ </record>
+ <record model="ir.message" id="msg_period_fiscalyear_dates">
+ <field name="text">The dates for period "%(period)s" must be
between the dates of its fiscal year "%(fiscalyear)s".</field>
+ </record>
+
+ <record model="ir.message" id="msg_journal_period_unique">
+ <field name="text">You can create only one journal per
period.</field>
+ </record>
+ <record model="ir.message" id="msg_modify_delete_journal_period_moves">
+ <field name="text">You cannot modify or delete journal-period
"%(journal_period)s" because it contains moves.</field>
+ </record>
+ <record model="ir.message"
id="msg_create_journal_period_closed_period">
+ <field name="text">You cannot create a journal-period in closed
period "%(period)s".</field>
+ </record>
+ <record model="ir.message" id="msg_open_journal_period_closed_period">
+ <field name="text">You cannot reopen journal-period
"%(journal_period)s" in closed period "%(period)s".</field>
+ </record>
+
+ <record model="ir.message" id="msg_tax_update_unit_price_with_parent">
+ <field name="text">You cannot set "Update Unit Price" on child tax
"%(tax)s".</field>
+ </record>
+ <record model="ir.message" id="msg_modify_tax_line_closed_period">
+ <field name="text">You cannot add/modify/delete tax lines in
closed period "%(period)s".</field>
+ </record>
+
+ <record model="ir.message"
id="msg_change_fiscalyear_post_move_sequence">
+ <field name="text">You cannot change the post move sequence on
fiscal year "%(fiscalyear)s" because it contains posted moves.</field>
+ </record>
+ <record model="ir.message" id="msg_no_fiscalyear_date">
+ <field name="text">To continue, you must create a fiscal year for
the date "%(date)s".</field>
+ </record>
+ <record model="ir.message" id="msg_fiscalyear_overlap">
+ <field name="text">The fiscal years "%(first)s" and "%(second)s"
overlap, you must use different dates.</field>
+ </record>
+ <record model="ir.message"
id="msg_fiscalyear_different_post_move_sequence">
+ <field name="text">The fiscal years "%(first)s" and "%(second)s"
cannot have the same post move sequence, you must use different
sequences.</field>
+ </record>
+ <record model="ir.message"
id="msg_close_fiscalyear_account_balance_not_zero">
+ <field name="text">To close the fiscal year, the balance of
account "%(account)s" must be zero.</field>
+ </record>
+ <record model="ir.message" id="msg_close_fiscalyear_earlier">
+ <field name="text">To close fiscal year "%(fiscalyear)s", you must
close all earlier fiscal years.</field>
+ </record>
+ <record model="ir.message" id="msg_reopen_fiscalyear_later">
+ <field name="text">To reopen fiscal year "%(fiscalyear)s", you
must reopen all later fiscal years.</field>
+ </record>
+
+ <record model="ir.message" id="msg_party_missing_receivable_account">
+ <field name="text">To continue, you must define a receivable
account for party "%(party)s".</field>
+ </record>
+ <record model="ir.message" id="msg_party_missing_payable_account">
+ <field name="text">To continue, you must define a payable account
for party "%(party)s".</field>
+ </record>
+ <record model="ir.message" id="msg_erase_party_receivable_payable">
+ <field name="text">You cannot erase party "%(party)s" while they
have pending receivable/payable with company "%(company)s".</field>
+ </record>
+
+ <record model="ir.message" id="msg_delete_account_with_move_lines">
+ <field name="text">You cannot delete account "%(account)s" because
it has move lines.</field>
+ </record>
+ <record model="ir.message"
id="msg_account_invalid_type_second_currency">
+ <field name="text">To set a second currency for account
"%(account)s", it must have the type "Other".</field>
+ </record>
+ <record model="ir.message"
id="msg_account_invalid_deferral_second_currency">
+ <field name="text">To set a second currency for account
"%(account)s", it must be deferral.</field>
+ </record>
+
+ <record model="ir.message" id="msg_deferral_unique">
+ <field name="text">Deferral must be unique by account and fiscal
year.</field>
+ </record>
+ <record model="ir.message" id="msg_write_deferral">
+ <field name="text">You cannot modify any account deferrals.</field>
+ </record>
+
+ <record model="ir.message" id="msg_account_chart_exists">
+ <field name="text">A chart of accounts already exists for company
"%(company)s".</field>
+ </record>
+
+ <record model="ir.message" id="msg_post_empty_move">
+ <field name="text">To post move "%(move)s", you must fill in its
lines.</field>
+ </record>
+ <record model="ir.message" id="msg_post_unbalanced_move">
+ <field name="text">To post move "%(move)s", you must balance all
its lines debits and credits.</field>
+ </record>
+ <record model="ir.message" id="msg_modify_posted_moved">
+ <field name="text">You cannot modify posted move
"%(move)s".</field>
+ </record>
+ <record model="ir.message" id="msg_move_date_outside_period">
+ <field name="text">The date of move "%(move)s" is outside the
period, you must change the date or the period.</field>
+ </record>
+ <record model="ir.message" id="msg_move_cancel_closed_period">
+ <field name="text">The period of move "%(move)s" is closed.
+Use the current period to cancel the move?</field>
+ </record>
+
+ <record model="ir.message" id="msg_write_reconciliation">
+ <field name="text">You cannot modify any reconciliation.</field>
+ </record>
+ <record model="ir.message" id="msg_reconciliation_line_not_valid">
+ <field name="text">You cannot reconcile non-valid line
"%(line)s".</field>
+ </record>
+ <record model="ir.message" id="msg_reconciliation_different_accounts">
+ <field name="text">You cannot reconcile line "%(line)s" with
others because its account "%(account1)s" is different from
"%(account2)s".</field>
+ </record>
+ <record model="ir.message"
id="msg_reconciliation_account_not_reconcile">
+ <field name="text">To reconcile line "%(line)s", you must set its
account "%(account)s" as reconcilable.</field>
+ </record>
+ <record model="ir.message" id="msg_reconciliation_different_parties">
+ <field name="text">You cannot reconcile line "%(line)s" with
others because its party "%(party1)s" is different from "%(party2)s".</field>
+ </record>
+ <record model="ir.message" id="msg_reconciliation_unbalanced">
+ <field name="text">To reconcile lines, they must have the same
debit "%(debit)s" and credit "%(credit)s".</field>
+ </record>
+
+ <record model="ir.message" id="msg_line_debit_credit">
+ <field name="text">You cannot set both debit and credit on the
line.</field>
+ </record>
+ <record model="ir.message" id="msg_line_second_currency_sign">
+ <field name="text">You must set the sign for second currency to
match the sign of debit - credit.</field>
+ </record>
+ <record model="ir.message" id="msg_modify_line_closed_journal_period">
+ <field name="text">You cannot add/modify/delete lines on closed
journal-period "%(journal_period)s".</field>
+ </record>
+ <record model="ir.message" id="msg_modify_line_posted_move">
+ <field name="text">You cannot modify line "%(line)s" from posted
move "%(move)s".</field>
+ </record>
+ <record model="ir.message" id="msg_modify_line_reconciled">
+ <field name="text">You cannot modify reconciled line
"%(line)s".</field>
+ </record>
+ <record model="ir.message" id="msg_line_view_account">
+ <field name="text">To create a line on account "%(account)s", you
must change its "view" kind.</field>
+ </record>
+ <record model="ir.message" id="msg_line_already_reconciled">
+ <field name="text">You cannot reconcile already reconciled line
"%(line)s".</field>
+ </record>
+ <record model="ir.message" id="msg_line_party_required">
+ <field name="text">To create a line on account "%(account)s", you
must set a party on line "%(line)s".</field>
+ </record>
+ <record model="ir.message" id="msg_line_party_set">
+ <field name="text">To create a line on account "%(account)s", you
must remove the party on line "%(line)s".</field>
+ </record>
+ </data>
+</tryton>
diff -r 7ebad68e53e0 -r efb8001e1ac1 move.py
--- a/move.py Wed Dec 19 17:08:24 2018 +0100
+++ b/move.py Sat Dec 29 14:20:28 2018 +0100
@@ -11,7 +11,9 @@
from sql.functions import CharLength
from sql.conditionals import Coalesce, Case
+from trytond.i18n import gettext
from trytond.model import ModelView, ModelSQL, fields, Check, DeactivableMixin
+from trytond.model.exceptions import AccessError
from trytond.wizard import Wizard, StateTransition, StateView, StateAction, \
StateReport, Button
from trytond.report import Report
@@ -23,6 +25,9 @@
from trytond.tools import reduce_ids, grouped_slice
from trytond.config import config
+from .exceptions import (PostError, MoveDatesError, CancelWarning,
+ ReconciliationError)
+
__all__ = ['Move', 'Reconciliation', 'Line', 'WriteOff', 'OpenJournalAsk',
'OpenJournal', 'OpenAccount',
'ReconcileLinesWriteOff', 'ReconcileLines',
@@ -85,19 +90,6 @@
cls._check_modify_exclude = []
cls._order.insert(0, ('date', 'DESC'))
cls._order.insert(1, ('number', 'DESC'))
- cls._error_messages.update({
- 'post_empty_move': ('You can not post move "%s" because it is '
- 'empty.'),
- 'post_unbalanced_move': ('You can not post move "%s" because '
- 'it is an unbalanced.'),
- 'modify_posted_move': ('You can not modify move "%s" because '
- 'it is already posted.'),
- 'date_outside_period': ('You can not create move "%(move)s" '
- 'because its date is outside its period.'),
- 'period_cancel': (
- 'The period of move "%s" is closed.\n'
- 'Use the current period?'),
- })
cls._buttons.update({
'post': {
'invisible': Eval('state') == 'posted',
@@ -211,16 +203,18 @@
def check_date(self):
if (self.date < self.period.start_date
or self.date > self.period.end_date):
- self.raise_user_error('date_outside_period', {
- 'move': self.rec_name,
- })
+ raise MoveDatesError(
+ gettext('account.msg_move_date_outside_period',
+ move=self.rec_name))
@classmethod
def check_modify(cls, moves):
'Check posted moves for modifications.'
for move in moves:
if move.state == 'posted':
- cls.raise_user_error('modify_posted_move', (move.rec_name,))
+ raise AccessError(
+ gettext('account.msg_modify_posted_moved',
+ move=move.rec_name))
@classmethod
def search_rec_name(cls, name, clause):
@@ -351,13 +345,17 @@
pool = Pool()
Date = pool.get('ir.date')
Period = pool.get('account.period')
+ Warning = pool.get('res.user.warning')
default = {
'origin': str(self),
}
if self.period.state == 'close':
- self.raise_user_warning('%s.cancel' % self,
- 'period_cancel', self.rec_name)
+ key = '%s.cancel' % self
+ if Warning.check(key):
+ raise CancelWarning(key,
+ gettext('account.msg_move_date_outside_period',
+ move=self.rec_name))
date = Date.today()
period_id = Period.find(self.company.id, date=date)
default.update({
@@ -394,14 +392,17 @@
for move in moves:
amount = Decimal('0.0')
if not move.lines:
- cls.raise_user_error('post_empty_move', (move.rec_name,))
+ raise PostError(
+ gettext('account.msg_post_empty_move', move=move.rec_name))
company = None
for line in move.lines:
amount += line.debit - line.credit
if not company:
company = line.account.company
if not company.currency.is_zero(amount):
- cls.raise_user_error('post_unbalanced_move', (move.rec_name,))
+ raise PostError(
+ gettext('account.msg_post_unbalanced_move',
+ move=move.rec_name))
for move in moves:
move.state = 'posted'
if not move.post_number:
@@ -429,28 +430,6 @@
help='Highest date of the reconciled lines')
@classmethod
- def __setup__(cls):
- super(Reconciliation, cls).__setup__()
- cls._error_messages.update({
- 'modify': 'You can not modify a reconciliation.',
- 'reconciliation_line_not_valid': ('You can not reconcile line '
- '"%s" because it is not in valid state.'),
- 'reconciliation_different_accounts': ('You can not reconcile '
- 'line "%(line)s" because its account "%(account1)s" is '
- 'different from "%(account2)s".'),
- 'reconciliation_account_no_reconcile': (
- 'You can not reconcile '
- 'line "%(line)s" because its account "%(account)s" is '
- 'configured as not reconcilable.'),
- 'reconciliation_different_parties': ('You can not reconcile '
- 'line "%(line)s" because its party "%(party1)s" is '
- 'different from "%(party2)s".'),
- 'reconciliation_unbalanced': ('You can not create a '
- 'reconciliation where debit "%(debit)s" and credit '
- '"%(credit)s" differ.'),
- })
-
- @classmethod
def __register__(cls, module_name):
TableHandler = backend.get('TableHandler')
cursor = Transaction().connection.cursor()
@@ -491,7 +470,7 @@
@classmethod
def write(cls, moves, values, *args):
- cls.raise_user_error('modify')
+ raise AccessError(gettext('account.msg_write_reconciliation'))
@classmethod
def validate(cls, reconciliations):
@@ -509,38 +488,41 @@
party = reconciliation.lines[0].party
for line in reconciliation.lines:
if line.state != 'valid':
- cls.raise_user_error('reconciliation_line_not_valid',
- (line.rec_name,))
+ raise ReconciliationError(
+ gettext('account.msg_reconciliation_line_not_valid',
+ line=line.rec_name))
debit += line.debit
credit += line.credit
if not account:
account = line.account
elif account.id != line.account.id:
- cls.raise_user_error('reconciliation_different_accounts', {
- 'line': line.rec_name,
- 'account1': line.account.rec_name,
- 'account2': account.rec_name,
- })
+ raise ReconciliationError(
+ gettext('account'
+ '.msg_reconciliation_different_accounts',
+ line=line.rec_name,
+ account1=line.account.rec_name,
+ account2=account.rec_name))
if not account.reconcile:
- cls.raise_user_error('reconciliation_account_no_reconcile',
- {
- 'line': line.rec_name,
- 'account': line.account.rec_name,
- })
+ raise ReconciliationError(
+ gettext('account'
+ '.msg_reconciliation_account_not_reconcile',
+ line=line.rec_name,
+ account=line.account.rec_name))
if line.party != party:
- cls.raise_user_error('reconciliation_different_parties', {
- 'line': line.rec_name,
- 'party1': line.party.rec_name,
- 'party2': party.rec_name,
- })
+ raise ReconciliationError(
+ gettext('account'
+ '.msg_reconciliation_different_parties',
+ line=line.rec_name,
+ party1=line.party.rec_name,
+ party2=party.rec_name))
if not account.company.currency.is_zero(debit - credit):
lang = Lang.get()
debit = lang.currency(debit, account.company.currency)
credit = lang.currency(credit, account.company.currency)
- cls.raise_user_error('reconciliation_unbalanced', {
- 'debit': debit,
- 'credit': credit,
- })
+ raise ReconciliationError(
+ gettext('account.msg_reconciliation_unbalanced',
+ debit=debit,
+ credit=credit))
class Line(ModelSQL, ModelView):
@@ -677,31 +659,16 @@
cls._sql_constraints += [
('credit_debit',
Check(table, table.credit * table.debit == 0),
- 'Wrong credit/debit values.'),
+ 'account.msg_line_debit_credit'),
('second_currency_sign',
Check(table, Coalesce(table.amount_second_currency, 0)
* (table.debit - table.credit) >= 0),
- "Wrong second currency sign."),
+ 'account.msg_line_second_currency_sign'),
]
cls.__rpc__.update({
'on_write': RPC(instantiate=0),
})
cls._order[0] = ('id', 'DESC')
- cls._error_messages.update({
- 'add_modify_closed_journal_period': ('You can not '
- 'add/modify lines in closed journal period "%s".'),
- 'modify_posted_move': ('You can not modify lines of move "%s" '
- 'because it is already posted.'),
- 'modify_reconciled': ('You can not modify line "%s" because '
- 'it is reconciled.'),
- 'no_journal': ('Move line cannot be created because there is '
- 'no journal defined.'),
- 'move_view_account': ('You can not create a move line with '
- 'account "%s" because it is a view account.'),
- 'already_reconciled': 'Line "%s" (%d) already reconciled.',
- 'party_required': 'Party is required on line "%s"',
- 'party_set': 'Party must not be set on line "%s"',
- })
@classmethod
def __register__(cls, module_name):
@@ -1006,11 +973,15 @@
def check_account(self):
if self.account.kind in ('view',):
- self.raise_user_error('move_view_account', (
- self.account.rec_name,))
+ raise AccessError(
+ gettext('account.msg_line_view_account',
+ account=self.account.rec_name))
if bool(self.party) != bool(self.account.party_required):
error = 'party_set' if self.party else 'party_required'
- self.raise_user_error(error, self.rec_name)
+ raise AccessError(
+ gettext('account.msg_line_%s' % error,
+ account=self.account.rec_name,
+ line=self.rec_name))
@classmethod
def check_journal_period_modify(cls, period, journal):
@@ -1026,8 +997,9 @@
if journal_periods:
journal_period, = journal_periods
if journal_period.state == 'close':
- cls.raise_user_error('add_modify_closed_journal_period', (
- journal_period.rec_name,))
+ raise AccessError(
+ gettext('account.msg_modify_line_closed_period',
+ journal_period=journal_period.rec_name))
else:
JournalPeriod.create([{
'journal': journal.id,
@@ -1045,8 +1017,10 @@
journal_period_done = []
for line in lines:
if line.move.state == 'posted':
- cls.raise_user_error('modify_posted_move', (
- line.move.rec_name,))
+ raise AccessError(
+ gettext('account.msg_modify_line_posted_move',
+ line=line.rec_name,
+ move=line.move.rec_name))
journal_period = (line.journal.id, line.period.id)
if journal_period not in journal_period_done:
cls.check_journal_period_modify(line.period,
@@ -1060,7 +1034,9 @@
return
for line in lines:
if line.reconciliation:
- cls.raise_user_error('modify_reconciled', line.rec_name)
+ raise AccessError(
+ gettext('account.msg_modify_line_reconciled',
+ line=line.rec_name))
@classmethod
def delete(cls, lines):
@@ -1099,11 +1075,9 @@
vlist = [x.copy() for x in vlist]
for vals in vlist:
if not vals.get('move'):
- journal_id = (vals.get('journal')
- or Transaction().context.get('journal'))
- if not journal_id:
- cls.raise_user_error('no_journal')
if move is None:
+ journal_id = (vals.get('journal')
+ or Transaction().context.get('journal'))
move = Move()
move.period = vals.get('period',
Transaction().context.get('period'))
@@ -1177,8 +1151,9 @@
for lines in lines_list:
for line in lines:
if line.reconciliation:
- cls.raise_user_error('already_reconciled',
- error_args=(line.move.number, line.id,))
+ raise AccessError(
+ gettext('account.msg_line_already_reconciled',
+ line=line.rec_name))
lines = list(lines)
reconcile_account = None
diff -r 7ebad68e53e0 -r efb8001e1ac1 party.py
--- a/party.py Wed Dec 19 17:08:24 2018 +0100
+++ b/party.py Sat Dec 29 14:20:28 2018 +0100
@@ -7,6 +7,7 @@
from sql.conditionals import Coalesce
from trytond import backend
+from trytond.i18n import gettext
from trytond.model import ModelSQL, fields
from trytond.pyson import Eval
from trytond.transaction import Transaction
@@ -15,6 +16,9 @@
from trytond.tools.multivalue import migrate_property
from trytond.modules.company.model import (
CompanyMultiValueMixin, CompanyValueMixin)
+from trytond.modules.party.exceptions import EraseError
+
+from .exceptions import AccountMissing
__all__ = ['Party', 'PartyAccount', 'PartyReplace', 'PartyErase']
account_names = [
@@ -83,16 +87,6 @@
'get_receivable_payable', searcher='search_receivable_payable')
@classmethod
- def __setup__(cls):
- super(Party, cls).__setup__()
- cls._error_messages.update({
- 'missing_receivable_account': (
- 'There is no receivable account on party "%(name)s".'),
- 'missing_payable_account': (
- 'There is no payable account on party "%(name)s".'),
- })
-
- @classmethod
def multivalue_model(cls, field):
pool = Pool()
if field in account_names:
@@ -224,9 +218,9 @@
account = config.get_multivalue('default_account_payable')
# Allow empty values on on_change
if not account and not Transaction().readonly:
- self.raise_user_error('missing_payable_account', {
- 'name': self.rec_name,
- })
+ raise AccountMissing(
+ gettext('account.msg_party_missing_payable_account',
+ party=self.rec_name))
if account:
return account.current()
@@ -240,9 +234,9 @@
account = config.get_multivalue('default_account_receivable')
# Allow empty values on on_change
if not account and not Transaction().readonly:
- self.raise_user_error('missing_receivable_account', {
- 'name': self.rec_name,
- })
+ raise AccountMissing(
+ gettext('account.msg_party_missing_receivable_account',
+ party=self.rec_name))
if account:
return account.current()
@@ -316,20 +310,10 @@
class PartyErase(metaclass=PoolMeta):
__name__ = 'party.erase'
- @classmethod
- def __setup__(cls):
- super(PartyErase, cls).__setup__()
- cls._error_messages.update({
- 'receivable_payable': (
- 'The party "%(party)s" can not be erased '
- 'because he has pending receivable/payable '
- 'for the company "%(company)s".'),
- })
-
def check_erase_company(self, party, company):
super(PartyErase, self).check_erase_company(party, company)
if party.receivable or party.payable:
- self.raise_user_error('receivable_payable', {
- 'party': party.rec_name,
- 'company': company.rec_name,
- })
+ raise EraseError(
+ gettext('account.msg_erase_party_receivable_payable',
+ party=party.rec_name,
+ company=company.rec_name))
diff -r 7ebad68e53e0 -r efb8001e1ac1 period.py
--- a/period.py Wed Dec 19 17:08:24 2018 +0100
+++ b/period.py Sat Dec 29 14:20:28 2018 +0100
@@ -1,11 +1,17 @@
# 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.i18n import gettext
from trytond.model import ModelView, ModelSQL, Workflow, fields
+from trytond.model.exceptions import AccessError
from trytond.pyson import Eval
from trytond.transaction import Transaction
from trytond.pool import Pool
from trytond.const import OPERATORS
+from .exceptions import (PeriodNotFoundError, ClosePeriodError,
+ PeriodDatesError, PeriodSequenceError)
+
__all__ = ['Period']
_STATES = {
@@ -53,28 +59,6 @@
def __setup__(cls):
super(Period, cls).__setup__()
cls._order.insert(0, ('start_date', 'ASC'))
- cls._error_messages.update({
- 'no_period_date': 'No period defined for date "%s".',
- 'modify_del_period_moves': ('You can not modify/delete '
- 'period "%s" because it has moves.'),
- 'create_period_closed_fiscalyear': ('You can not create '
- 'a period on fiscal year "%s" because it is closed.'),
- 'open_period_closed_fiscalyear': ('You can not open period '
- '"%(period)s" because its fiscal year "%(fiscalyear)s" is '
- 'closed.'),
- 'change_post_move_sequence': ('You can not change the post '
- 'move sequence of period "%s" because there are already '
- 'posted moves in the period.'),
- 'close_period_non_posted_move': ('You can not close period '
- '"%(period)s" because there are non posted moves '
- '"%(move)s" in this period.'),
- 'periods_overlap': ('"%(first)s" and "%(second)s" periods '
- 'overlap.'),
- 'check_move_sequence': ('Period "%(first)s" and "%(second)s" '
- 'have the same sequence.'),
- 'fiscalyear_dates': ('Dates of period "%s" are outside '
- 'are outside it\'s fiscal year dates.'),
- })
cls._transitions |= set((
('open', 'close'),
('close', 'locked'),
@@ -148,15 +132,18 @@
period_id = cursor.fetchone()
if period_id:
overlapping_period = self.__class__(period_id[0])
- self.raise_user_error('periods_overlap', {
- 'first': self.rec_name,
- 'second': overlapping_period.rec_name,
- })
+ raise PeriodDatesError(
+ gettext('account.msg_period_overlap',
+ first=self.rec_name,
+ second=overlapping_period.rec_name))
def check_fiscalyear_dates(self):
if (self.start_date < self.fiscalyear.start_date
or self.end_date > self.fiscalyear.end_date):
- self.raise_user_error('fiscalyear_dates', (self.rec_name,))
+ raise PeriodDatesError(
+ gettext('account.msg_period_fiscalyear_dates',
+ period=self.rec_name,
+ fiscalyear=self.fiscalyear.rec_name))
def check_post_move_sequence(self):
if not self.post_move_sequence:
@@ -166,10 +153,10 @@
('fiscalyear', '!=', self.fiscalyear.id),
])
if periods:
- self.raise_user_error('check_move_sequence', {
- 'first': self.rec_name,
- 'second': periods[0].rec_name,
- })
+ raise PeriodSequenceError(
+ gettext('account.msg_period_same_sequence',
+ first=self.rec_name,
+ second=periods[0].rec_name))
@classmethod
def find(cls, company_id, date=None, exception=True, test_state=True):
@@ -198,7 +185,9 @@
if not periods:
if exception:
lang = Lang.get()
- cls.raise_user_error('no_period_date', lang.strftime(date))
+ raise PeriodNotFoundError(
+ gettext('account.msg_no_period_date',
+ date=lang.strftime(date)))
else:
return None
return periods[0].id
@@ -210,8 +199,9 @@
('period', 'in', [p.id for p in periods]),
], limit=1)
if moves:
- cls.raise_user_error('modify_del_period_moves', (
- moves[0].period.rec_name,))
+ raise AccessError(
+ gettext('account.msg_modify_delete_period_moves',
+ period=moves[0].period.rec_name))
@classmethod
def search(cls, args, offset=0, limit=None, order=None, count=False,
@@ -248,8 +238,9 @@
if vals.get('fiscalyear'):
fiscalyear = FiscalYear(vals['fiscalyear'])
if fiscalyear.state != 'open':
- cls.raise_user_error('create_period_closed_fiscalyear',
- (fiscalyear.rec_name,))
+ raise AccessError(
+ gettext('account.msg_create_period_closed_fiscalyear',
+ fiscalyear=fiscalyear.rec_name))
if not vals.get('post_move_sequence'):
vals['post_move_sequence'] = (
fiscalyear.post_move_sequence.id)
@@ -273,10 +264,11 @@
if values.get('state') == 'open':
for period in periods:
if period.fiscalyear.state != 'open':
- cls.raise_user_error('open_period_closed_fiscalyear', {
- 'period': period.rec_name,
- 'fiscalyear': period.fiscalyear.rec_name,
- })
+ raise AccessError(
+ gettext(
+ 'account.msg_open_period_closed_fiscalyear',
+ period=period.rec_name,
+ fiscalyear=period.fiscalyear.rec_name))
if values.get('post_move_sequence'):
for period in periods:
if (period.post_move_sequence
@@ -286,8 +278,10 @@
('period', '=', period.id),
('state', '=', 'posted'),
]):
- cls.raise_user_error('change_post_move_sequence',
- (period.rec_name,))
+ raise AccessError(
+ gettext('account'
+ '.msg_change_period_post_move_sequence',
+ period=period.rec_name))
args.extend((periods, values))
super(Period, cls).write(*args)
@@ -316,10 +310,10 @@
], limit=1)
if unposted_moves:
unposted_move, = unposted_moves
- cls.raise_user_error('close_period_non_posted_move', {
- 'period': unposted_move.period.rec_name,
- 'move': unposted_move.rec_name,
- })
+ raise ClosePeriodError(
+ gettext('account.msg_close_period_non_posted_moves',
+ period=unposted_move.period.rec_name,
+ move=unposted_move.rec_name))
journal_periods = JournalPeriod.search([
('period', 'in', [p.id for p in periods]),
])
diff -r 7ebad68e53e0 -r efb8001e1ac1 tax.py
--- a/tax.py Wed Dec 19 17:08:24 2018 +0100
+++ b/tax.py Sat Dec 29 14:20:28 2018 +0100
@@ -9,9 +9,11 @@
from sql.aggregate import Sum
from sql.conditionals import Case
+from trytond.i18n import gettext
from trytond.model import (
ModelView, ModelSQL, MatchMixin, DeactivableMixin, fields,
sequence_ordered, tree)
+from trytond.model.exceptions import AccessError
from trytond.wizard import Wizard, StateView, StateAction, Button
from trytond import backend
from trytond.pyson import Eval, If, Bool, PYSONEncoder
@@ -514,10 +516,6 @@
def __setup__(cls):
super(TaxTemplate, cls).__setup__()
cls._order.insert(0, ('account', 'ASC'))
- cls._error_messages.update({
- 'update_unit_price_with_parent': ('"Update Unit Price" can '
- 'not be set on tax "%(template)s" which has a parent.'),
- })
@classmethod
def validate(cls, tax_templates):
@@ -527,9 +525,9 @@
def check_update_unit_price(self):
if self.update_unit_price and self.parent:
- self.raise_user_error('update_unit_price_with_parent', {
- 'template': self.rec_name,
- })
+ raise AccessError(
+ gettext('account.msg_tax_update_unit_price_with_parent',
+ tax=self.rec_name))
@staticmethod
def default_type():
@@ -731,14 +729,6 @@
del _states
@classmethod
- def __setup__(cls):
- super(Tax, cls).__setup__()
- cls._error_messages.update({
- 'update_unit_price_with_parent': ('"Update Unit Price" can '
- 'not be set on tax "%(template)s" which has a parent.'),
- })
-
- @classmethod
def validate(cls, taxes):
super(Tax, cls).validate(taxes)
for tax in taxes:
@@ -746,9 +736,9 @@
def check_update_unit_price(self):
if self.parent and self.update_unit_price:
- self.raise_user_error('update_unit_price_with_parent', {
- 'tax': self.rec_name,
- })
+ raise AccessError(
+ gettext('account.msg_tax_update_unit_price_with_parent',
+ tax=self.rec_name))
@staticmethod
def default_type():
@@ -1157,14 +1147,6 @@
'on_change_with_company')
@classmethod
- def __setup__(cls):
- super(TaxLine, cls).__setup__()
- cls._error_messages.update({
- 'modify_closed_period': (
- 'You can not add/modify tax lines in closed period "%s".'),
- })
-
- @classmethod
def __register__(cls, module_name):
pool = Pool()
Tax = pool.get('account.tax')
@@ -1250,8 +1232,9 @@
for line in lines:
period = line.period_checked
if period and period.state != 'open':
- cls.raise_user_error(
- 'modify_closed_period', (period.rec_name,))
+ raise AccessError(
+ gettext('account.msg_modify_tax_line_closed_period',
+ period=period.rec_name))
class TaxRuleTemplate(ModelSQL, ModelView):
diff -r 7ebad68e53e0 -r efb8001e1ac1 tryton.cfg
--- a/tryton.cfg Wed Dec 19 17:08:24 2018 +0100
+++ b/tryton.cfg Sat Dec 29 14:20:28 2018 +0100
@@ -26,3 +26,4 @@
minimal_chart_pt.xml
minimal_chart_ru.xml
minimal_chart_sl.xml
+ message.xml