From b79863a62e1596214616e8b174085cb54e82a2bc Mon Sep 17 00:00:00 2001 From: Laurent Smet Date: Wed, 8 Feb 2023 14:33:57 +0000 Subject: [PATCH] [FIX] account: Prevent reset draft an exchange journal entry An exchange journal entry must not be reset to draft manually. It's done by odoo itself automatically when breaking an existing reconciliation. closes odoo/odoo#112472 X-original-commit: ad9d53c8976ed78f3f6fdd65795c582d533de436 Related: odoo/enterprise#36999 Signed-off-by: Brice Bartoletti (bib) --- addons/account/models/account_move.py | 25 +++++++++- .../account/tests/test_account_move_entry.py | 50 +++++++++++++++++++ 2 files changed, 74 insertions(+), 1 deletion(-) 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()