From a1b2ed5e794f2b382c3a4c9347a290ad924bb37e Mon Sep 17 00:00:00 2001 From: Leonardo Pistone Date: Tue, 14 Jan 2014 12:21:47 +0100 Subject: [PATCH] [add] move batch validate: now you can cancel all jobs that are not done yet --- account_move_batch_validate/account.py | 40 ++++++++++++++++++- account_move_batch_validate/account_view.xml | 2 + .../wizard/move_marker.py | 39 +++++++++--------- 3 files changed, 59 insertions(+), 22 deletions(-) diff --git a/account_move_batch_validate/account.py b/account_move_batch_validate/account.py index da7353f4f..9030330a8 100644 --- a/account_move_batch_validate/account.py +++ b/account_move_batch_validate/account.py @@ -21,9 +21,11 @@ """Accounting customisation for delayed posting.""" from openerp.osv import fields, orm +from openerp.tools.translate import _ from openerp.addons.connector.queue.job import job from openerp.addons.connector.session import ConnectorSession +from openerp.addons.connector.queue.job import OpenERPJobStorage class account_move(orm.Model): @@ -43,7 +45,7 @@ class account_move(orm.Model): ), } - def _delay_post_marked(self, cr, uid, context=None): + def _delay_post_marked(self, cr, uid, eta=None, context=None): """Create a job for every move marked for posting. If some moves already have a job, they are skipped. @@ -62,11 +64,38 @@ class account_move(orm.Model): ], context=context) for move_id in move_ids: - job_uuid = validate_one_move.delay(session, self._name, move_id) + job_uuid = validate_one_move.delay(session, self._name, move_id, + eta=eta) self.write(cr, uid, [move_id], { 'post_job_uuid': job_uuid }) + def _cancel_jobs(self, cr, uid, context=None): + """Find moves where the mark has been removed and cancel the jobs. + + For the moves that are posted already it's too late: we skip them. + + """ + + if context is None: + context = {} + + session = ConnectorSession(cr, uid, context=context) + storage = OpenERPJobStorage(session) + + move_ids = self.search(cr, uid, [ + ('to_post', '=', False), + ('post_job_uuid', '!=', False), + ('state', '=', 'draft'), + ], context=context) + + for move in self.browse(cr, uid, move_ids, context=context): + job = storage.load(move.post_job_uuid) + if job.state in (u'pending', u'enqueued'): + job.set_done(result=_( + u'Task set to Done because the user unmarked the move' + )) + def mark_for_posting(self, cr, uid, move_ids, context=None): """Mark a list of moves for delayed posting, and enqueue the jobs.""" if context is None: @@ -74,6 +103,13 @@ class account_move(orm.Model): self.write(cr, uid, move_ids, {'to_post': True}, context=context) self._delay_post_marked(cr, uid, context=context) + def unmark_for_posting(self, cr, uid, move_ids, context=None): + """Unmark moves for delayed posting, and cancel the jobs.""" + if context is None: + context = {} + self.write(cr, uid, move_ids, {'to_post': False}, context=context) + self._cancel_jobs(cr, uid, context=context) + @job def validate_one_move(session, model_name, move_id): diff --git a/account_move_batch_validate/account_view.xml b/account_move_batch_validate/account_view.xml index 1f5a60af9..ea73c28e2 100644 --- a/account_move_batch_validate/account_view.xml +++ b/account_move_batch_validate/account_view.xml @@ -9,6 +9,7 @@ + @@ -20,6 +21,7 @@ + diff --git a/account_move_batch_validate/wizard/move_marker.py b/account_move_batch_validate/wizard/move_marker.py index 5fa250215..12a6e7d98 100644 --- a/account_move_batch_validate/wizard/move_marker.py +++ b/account_move_batch_validate/wizard/move_marker.py @@ -50,28 +50,27 @@ class AccountMoveMarker(orm.TransientModel): move_obj = self.pool['account.move'] - if wiz.action == 'mark': - domain = [] - if wiz.journal_ids: - domain.append(( - 'journal_id', - 'in', - [journal.id for journal in wiz.journal_ids] - )) + domain = [('state', '=', 'draft')] - move_ids = move_obj.search(cr, uid, domain, context=context) - - move_obj.mark_for_posting(cr, uid, move_ids, context=context) - - return {'type': 'ir.actions.act_window_close'} - - elif wiz.action == 'unmark': - # TODO - raise NotImplementedError( - 'The Unmark action is not implemented yet' - ) if wiz.filter != 'filter_no': # TODO raise NotImplementedError( - 'Date and period filter are not implemented yet' + 'Date and period filters are not implemented yet' ) + + if wiz.journal_ids: + domain.append(( + 'journal_id', + 'in', + [journal.id for journal in wiz.journal_ids] + )) + + move_ids = move_obj.search(cr, uid, domain, context=context) + + if wiz.action == 'mark': + move_obj.mark_for_posting(cr, uid, move_ids, context=context) + + elif wiz.action == 'unmark': + move_obj.unmark_for_posting(cr, uid, move_ids, context=context) + + return {'type': 'ir.actions.act_window_close'}