From 44a92bde2dbdfaa99432cbf4f6c688e84a6a268d Mon Sep 17 00:00:00 2001 From: Alexis de Lattre Date: Tue, 27 Sep 2022 09:04:39 +0200 Subject: [PATCH] account_usability: reversal wizard: check the move hasn't already been reversed --- .../wizard/account_move_reversal.py | 29 ++++++++++++------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/account_usability/wizard/account_move_reversal.py b/account_usability/wizard/account_move_reversal.py index 3b8830a0..9516db06 100644 --- a/account_usability/wizard/account_move_reversal.py +++ b/account_usability/wizard/account_move_reversal.py @@ -2,21 +2,28 @@ # @author: Alexis de Lattre # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -from odoo import api, fields, models +from odoo import api, models, _ from dateutil.relativedelta import relativedelta +from odoo.exceptions import UserError class AccountMoveReversal(models.TransientModel): _inherit = 'account.move.reversal' + # Set default reversal date to original move + 1 day + # and raise error if original move has already been reversed @api.model - def _default_date(self): - date_dt = None - if ( - self._context.get('active_model') == 'account.move' and - self._context.get('active_id')): - move = self.env['account.move'].browse(self._context['active_id']) - date_dt = move.date + relativedelta(days=1) - return date_dt - - date = fields.Date(default=_default_date) + def default_get(self, fields_list): + res = super().default_get(fields_list) + assert self._context.get('active_model') == 'account.move' + amo = self.env['account.move'] + moves = amo.browse(self._context['active_ids']) + if len(moves) == 1: + res['date'] = moves.date + relativedelta(days=1) + reversed_move = amo.search([('reversed_entry_id', 'in', moves.ids)], limit=1) + if reversed_move: + raise UserError(_( + "Move '%s' has already been reversed by move '%s'.") % ( + reversed_move.reversed_entry_id.display_name, + reversed_move.display_name)) + return res