diff --git a/CHANGELOG b/CHANGELOG
index 9d2fc8e839..2a84a30392 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,3 +1,5 @@
+* Prevent posting move from statement that is not posted
+
Version 5.4.0 - 2019-11-04
* Bug fixes (see mercurial logs for details)
* Require fresh session to post a statement
diff --git a/account.py b/account.py
index 92bd879fa7..0331bca8f0 100644
--- a/account.py
+++ b/account.py
@@ -5,10 +5,14 @@ from operator import itemgetter
from sql.operators import Concat
+from trytond.i18n import gettext
+from trytond.model import ModelView
from trytond.pool import PoolMeta, Pool
from trytond.transaction import Transaction
from trytond.tools import grouped_slice, reduce_ids
+from trytond.modules.account.exceptions import PostError
+
__all__ = ['Journal', 'Move']
@@ -55,3 +59,17 @@ class Move(metaclass=PoolMeta):
@classmethod
def _get_origin(cls):
return super(Move, cls)._get_origin() + ['account.statement']
+
+ @classmethod
+ @ModelView.button
+ def post(cls, moves):
+ pool = Pool()
+ Statement = pool.get('account.statement')
+ for move in moves:
+ if (isinstance(move.origin, Statement)
+ and move.origin.state != 'posted'):
+ raise PostError(
+ gettext('account_statement.msg_post_statement_move',
+ move=move.rec_name,
+ statement=move.origin.rec_name))
+ super().post(moves)
diff --git a/message.xml b/message.xml
index 8f52ec9ab2..511027ccbe 100644
--- a/message.xml
+++ b/message.xml
@@ -36,5 +36,8 @@ this repository contains the full copyright notices and license terms. -->
Statement line amount cannot be zero.
+
+ To post the move "%(move)s" you must post the statement "%(statement)s".
+
diff --git a/statement.py b/statement.py
index 9ef08b094b..8636fca85a 100644
--- a/statement.py
+++ b/statement.py
@@ -629,7 +629,8 @@ class Statement(Workflow, ModelSQL, ModelView):
statement=statement.rec_name,
amount=amount,
origin=origin.rec_name))
-
+ # Write state to skip statement test on Move.post
+ cls.write(statements, {'state': 'posted'})
lines = [l for s in statements for l in s.lines]
StatementLine.post_move(lines)
diff --git a/tests/scenario_account_statement.rst b/tests/scenario_account_statement.rst
index adc23c1025..562635bdb5 100644
--- a/tests/scenario_account_statement.rst
+++ b/tests/scenario_account_statement.rst
@@ -209,6 +209,14 @@ Validate statement::
>>> statement.state
'validated'
+Try posting a move::
+
+ >>> statement_line = statement.lines[0]
+ >>> statement_line.move.click('post') # doctest: +IGNORE_EXCEPTION_DETAIL
+ Traceback (most recent call last):
+ ...
+ trytond.modules.account.exceptions.PostError: ...
+
Cancel statement::
>>> statement.click('cancel')