diff --git a/CHANGELOG b/CHANGELOG
index e0b75316ec..db1fc19936 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,3 +1,5 @@
+* Add cron task to post clearing move after journal delay
+
Version 4.6.0 - 2017-10-30
* Bug fixes (see mercurial logs for details)
* Allow to use account instead of line for clearing
diff --git a/doc/index.rst b/doc/index.rst
index 3e7bf61e26..d1514b9566 100644
--- a/doc/index.rst
+++ b/doc/index.rst
@@ -7,3 +7,6 @@ account defined on the payment journal.
The clearing account will be used for statement lines which are linked to such
payment.
+
+If a delay is configured on journal for posting, a scheduled task will post
+automatically clearing moves after this delay.
diff --git a/payment.py b/payment.py
index e3ecfd1822..785d4a13a2 100644
--- a/payment.py
+++ b/payment.py
@@ -25,6 +25,31 @@ class Journal:
'required': Bool(Eval('clearing_account')),
},
depends=['clearing_account'])
+ clearing_posting_delay = fields.TimeDelta(
+ "Clearing Posting Delay",
+ help="Post automatically the clearing moves after the delay.\n"
+ "Leave empty for no posting.")
+
+ @classmethod
+ def cron_post_clearing_moves(cls, date=None):
+ pool = Pool()
+ Date = pool.get('ir.date')
+ Move = pool.get('account.move')
+ if date is None:
+ date = Date.today()
+ moves = []
+ journals = cls.search([
+ ('clearing_posting_delay', '!=', None),
+ ])
+ for journal in journals:
+ move_date = date - journal.clearing_posting_delay
+ moves.extend(Move.search([
+ ('date', '<=', move_date),
+ ('origin.journal.id', '=', journal.id,
+ 'account.payment'),
+ ('state', '=', 'draft'),
+ ]))
+ Move.post(moves)
class Payment:
diff --git a/payment.xml b/payment.xml
index 8eacefd255..c7f6eb947c 100644
--- a/payment.xml
+++ b/payment.xml
@@ -33,5 +33,33 @@ this repository contains the full copyright notices and license terms. -->
form
succeed_start_form
+
+
+ user_cron_post_clearing_moves
+ Cron Post Clearing Moves
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Post Clearing Moves
+
+
+
+ days
+
+ account.payment.journal
+ cron_post_clearing_moves
+
diff --git a/statement.py b/statement.py
index c123d43ddc..0738b74924 100644
--- a/statement.py
+++ b/statement.py
@@ -158,4 +158,6 @@ class StatementLine:
Move = pool.get('account.move')
super(StatementLine, cls).post_move(lines)
Move.post([l.payment.clearing_move for l in lines
- if l.payment and l.payment.clearing_move])
+ if l.payment
+ and l.payment.clearing_move
+ and l.payment.clearing_move.state == 'draft'])
diff --git a/tests/scenario_account_payment_clearing.rst b/tests/scenario_account_payment_clearing.rst
index 0198ca751a..5b283b2a20 100644
--- a/tests/scenario_account_payment_clearing.rst
+++ b/tests/scenario_account_payment_clearing.rst
@@ -16,6 +16,7 @@ Imports::
>>> from trytond.modules.account_invoice.tests.tools import \
... set_fiscalyear_invoice_sequences
>>> today = datetime.date.today()
+ >>> yesterday = today - relativedelta(days=1)
>>> first = today + relativedelta(day=1)
Install account_payment_clearing and account_statement::
@@ -60,7 +61,8 @@ Create payment journal::
>>> PaymentJournal = Model.get('account.payment.journal')
>>> payment_journal = PaymentJournal(name='Manual',
... process_method='manual', clearing_journal=expense,
- ... clearing_account=bank_clearing)
+ ... clearing_account=bank_clearing,
+ ... clearing_posting_delay=datetime.timedelta(1))
>>> payment_journal.save()
Create parties::
@@ -183,11 +185,22 @@ Fail payment::
Succeed payment and post clearing::
>>> succeed = Wizard('account.payment.succeed', [payment])
+ >>> succeed.form.date = yesterday
>>> succeed.execute('succeed')
>>> payment.state
u'succeeded'
+
+ >>> Cron = Model.get('ir.cron')
+ >>> Company = Model.get('company.company')
+ >>> cron_post_clearing_moves, = Cron.find([
+ ... ('model', '=', 'account.payment.journal'),
+ ... ('function', '=', 'cron_post_clearing_moves'),
+ ... ])
+ >>> cron_post_clearing_moves.companies.append(Company(company.id))
+ >>> cron_post_clearing_moves.click('run_once')
+
+ >>> payment.reload()
>>> clearing_move = payment.clearing_move
- >>> clearing_move.click('post')
>>> clearing_move.state
u'posted'
diff --git a/view/payment_journal_form.xml b/view/payment_journal_form.xml
index 17c2d36687..22f3792465 100644
--- a/view/payment_journal_form.xml
+++ b/view/payment_journal_form.xml
@@ -8,5 +8,7 @@ this repository contains the full copyright notices and license terms. -->
+
+