diff --git a/addons/account/models/account_move.py b/addons/account/models/account_move.py index 23a52b1e4f6..2a0b2425474 100644 --- a/addons/account/models/account_move.py +++ b/addons/account/models/account_move.py @@ -3581,8 +3581,31 @@ class AccountMove(models.Model): move.to_check = False def button_draft(self): + exchange_move_ids = set() + if self: + self.env['account.full.reconcile'].flush_model(['exchange_move_id']) + self.env['account.partial.reconcile'].flush_model(['exchange_move_id']) + self._cr.execute( + """ + SELECT DISTINCT sub.exchange_move_id + FROM ( + SELECT exchange_move_id + FROM account_full_reconcile + WHERE exchange_move_id IN %s + + UNION ALL + + SELECT exchange_move_id + FROM account_partial_reconcile + WHERE exchange_move_id IN %s + ) AS sub + """, + [tuple(self.ids), tuple(self.ids)], + ) + exchange_move_ids = set([row[0] for row in self._cr.fetchall()]) + for move in self: - if move in move.line_ids.mapped('full_reconcile_id.exchange_move_id'): + if move.id in exchange_move_ids: raise UserError(_('You cannot reset to draft an exchange difference journal entry.')) if move.tax_cash_basis_rec_id or move.tax_cash_basis_origin_move_id: # If the reconciliation was undone, move.tax_cash_basis_rec_id will be empty; diff --git a/addons/account/tests/test_account_move_entry.py b/addons/account/tests/test_account_move_entry.py index 8b974960957..0b9c45f7073 100644 --- a/addons/account/tests/test_account_move_entry.py +++ b/addons/account/tests/test_account_move_entry.py @@ -857,3 +857,53 @@ class TestAccountMove(AccountTestInvoicingCommon): }), ] }) + + def test_reset_draft_exchange_move(self): + """ Ensure you can't reset to draft an exchange journal entry """ + moves = self.env['account.move'].create([ + { + 'date': '2016-01-01', + 'line_ids': [ + Command.create({ + 'name': "line1", + 'account_id': self.company_data['default_account_receivable'].id, + 'currency_id': self.currency_data['currency'].id, + 'balance': 400.0, + 'amount_currency': 1200.0, + }), + Command.create({ + 'name': "line2", + 'account_id': self.company_data['default_account_expense'].id, + 'balance': -400.0, + }), + ] + }, + { + 'date': '2017-01-01', + 'line_ids': [ + Command.create({ + 'name': "line1", + 'account_id': self.company_data['default_account_receivable'].id, + 'currency_id': self.currency_data['currency'].id, + 'balance': -600.0, + 'amount_currency': -1200.0, + }), + Command.create({ + 'name': "line2", + 'account_id': self.company_data['default_account_expense'].id, + 'balance': 600.0, + }), + ] + }, + ]) + moves.action_post() + + res = moves.line_ids\ + .filtered(lambda x: x.account_id == self.company_data['default_account_receivable'])\ + .reconcile() + + self.assertTrue(res.get('partials')) + exchange_diff = res['partials'].exchange_move_id + self.assertTrue(exchange_diff) + with self.assertRaises(UserError), self.cr.savepoint(): + exchange_diff.button_draft()