From bc4529327338b5c7c81c549399406b5fb65f8438 Mon Sep 17 00:00:00 2001 From: Alexis de Lattre Date: Tue, 13 Sep 2016 14:44:38 +0200 Subject: [PATCH] Add option to reconcile the reversal move with the original move (#387) [IMP] account_reversal: Several improvements * Default date for reverse move is not start_date of the period following the period of the original move (and not the period following today's period) * Return form view of account.move if only 1 move is reversed * Add option to reconcile with the reversal entry. --- account_reversal/account_reversal.py | 42 ++++++++++++++----- .../wizard/account_move_reverse.py | 38 ++++++++++------- .../wizard/account_move_reverse_view.xml | 4 +- 3 files changed, 55 insertions(+), 29 deletions(-) diff --git a/account_reversal/account_reversal.py b/account_reversal/account_reversal.py index d4ac33d34..f9b10122a 100644 --- a/account_reversal/account_reversal.py +++ b/account_reversal/account_reversal.py @@ -50,7 +50,8 @@ class account_move(models.Model): @api.multi def _move_reversal(self, reversal_date, reversal_period_id=False, reversal_journal_id=False, - move_prefix=False, move_line_prefix=False): + move_prefix=False, move_line_prefix=False, + reconcile=False): """ Create the reversal of a move @@ -67,6 +68,7 @@ class account_move(models.Model): """ self.ensure_one() period_obj = self.env['account.period'] + amlo = self.env['account.move.line'] if reversal_period_id: reversal_period = period_obj.browse([reversal_period_id])[0] @@ -100,27 +102,44 @@ class account_move(models.Model): 'to_be_reversed': False, }) - for reversal_move_line in reversal_move.line_id: - reversal_ml_name = ' '.join( + rec_dict = {} + for rev_move_line in reversal_move.line_id: + rev_ml_name = ' '.join( [x for x - in [move_line_prefix, reversal_move_line.name] + in [move_line_prefix, rev_move_line.name] if x] ) - reversal_move_line.write( - {'debit': reversal_move_line.credit, - 'credit': reversal_move_line.debit, - 'amount_currency': reversal_move_line.amount_currency * -1, - 'name': reversal_ml_name}, + rev_move_line.write( + {'debit': rev_move_line.credit, + 'credit': rev_move_line.debit, + 'amount_currency': rev_move_line.amount_currency * -1, + 'name': rev_ml_name}, check=True, update_check=True) + if reconcile and rev_move_line.account_id.reconcile: + rec_dict.setdefault( + (rev_move_line.account_id, rev_move_line.partner_id), + amlo.browse(False)) + rec_dict[(rev_move_line.account_id, rev_move_line.partner_id)]\ + += rev_move_line + reversal_move.validate() + if reconcile: + for mline in self.line_id: + if mline.account_id.reconcile: + rec_dict[(mline.account_id, mline.partner_id)] += mline + + for to_rec_move_lines in rec_dict.itervalues(): + to_rec_move_lines.reconcile() + return reversal_move.id @api.multi def create_reversals(self, reversal_date, reversal_period_id=False, reversal_journal_id=False, - move_prefix=False, move_line_prefix=False): + move_prefix=False, move_line_prefix=False, + reconcile=False): """ Create the reversal of one or multiple moves @@ -140,7 +159,8 @@ class account_move(models.Model): reversal_period_id=reversal_period_id, reversal_journal_id=reversal_journal_id, move_prefix=move_prefix, - move_line_prefix=move_line_prefix + move_line_prefix=move_line_prefix, + reconcile=reconcile ) for move in self if not move.reversal_id diff --git a/account_reversal/wizard/account_move_reverse.py b/account_reversal/wizard/account_move_reverse.py index 805310ec9..9cccd1ab1 100644 --- a/account_reversal/wizard/account_move_reverse.py +++ b/account_reversal/wizard/account_move_reverse.py @@ -37,7 +37,7 @@ class account_move_reversal(orm.TransientModel): required=True, help="Enter the date of the reversal account entries. " "By default, OpenERP proposes the first day of " - "the next period."), + "the period following the period of the move to reverse."), 'period_id': fields.many2one( 'account.period', 'Reversal Period', @@ -57,6 +57,7 @@ class account_move_reversal(orm.TransientModel): help="Prefix that will be added to the name of the journal " "item to be reversed to create the name of the reversal " "journal item (a space is added after the prefix)."), + 'reconcile': fields.boolean('Reconcile'), } def _next_period_first_date(self, cr, uid, context=None): @@ -66,21 +67,21 @@ class account_move_reversal(orm.TransientModel): period_ctx = context.copy() period_ctx['account_period_prefer_normal'] = True period_obj = self.pool.get('account.period') - today_period_id = period_obj.find(cr, uid, context=period_ctx) - if today_period_id: - today_period = period_obj.browse( - cr, uid, today_period_id[0], context=context) - next_period_id = period_obj.next( - cr, uid, today_period, 1, context=context) - if next_period_id: - next_period = period_obj.browse( - cr, uid, next_period_id, context=context) - res = next_period.date_start + assert context['active_model'] == 'account.move' + to_reverse_move = self.pool['account.move'].browse( + cr, uid, context['active_id'], context=context) + next_period_id = period_obj.next( + cr, uid, to_reverse_move.period_id, 1, context=context) + if next_period_id: + next_period = period_obj.browse( + cr, uid, next_period_id, context=context) + res = next_period.date_start return res _defaults = { 'date': _next_period_first_date, 'move_line_prefix': 'REV -', + 'reconcile': True, } def action_reverse(self, cr, uid, ids, context=None): @@ -90,13 +91,12 @@ class account_move_reversal(orm.TransientModel): form = self.read(cr, uid, ids, context=context)[0] - mod_obj = self.pool.get('ir.model.data') - act_obj = self.pool.get('ir.actions.act_window') move_obj = self.pool.get('account.move') move_ids = context['active_ids'] period_id = form['period_id'][0] if form.get('period_id') else False journal_id = form['journal_id'][0] if form.get('journal_id') else False + reconcile = form['reconcile'] if form.get('reconcile') else False reversed_move_ids = move_obj.create_reversals( cr, uid, move_ids, @@ -105,12 +105,18 @@ class account_move_reversal(orm.TransientModel): reversal_journal_id=journal_id, move_prefix=form['move_prefix'], move_line_prefix=form['move_line_prefix'], + reconcile=reconcile, context=context) - __, action_id = mod_obj.get_object_reference( + action = self.pool['ir.actions.act_window'].for_xml_id( cr, uid, 'account', 'action_move_journal_line') - action = act_obj.read(cr, uid, [action_id], context=context)[0] - action['domain'] = unicode([('id', 'in', reversed_move_ids)]) action['name'] = _('Reversal Entries') action['context'] = unicode({'search_default_to_be_reversed': 0}) + if len(reversed_move_ids) == 1: + action['res_id'] = reversed_move_ids[0] + action['view_mode'] = 'form,tree' + action['views'] = False + action['view_id'] = False + else: + action['domain'] = unicode([('id', 'in', reversed_move_ids)]) return action diff --git a/account_reversal/wizard/account_move_reverse_view.xml b/account_reversal/wizard/account_move_reverse_view.xml index f771ca621..2512b9dc8 100644 --- a/account_reversal/wizard/account_move_reverse_view.xml +++ b/account_reversal/wizard/account_move_reverse_view.xml @@ -6,7 +6,7 @@ account.move.reverse.form account.move.reverse -
+