From 822dd8a5f217aea31fecd1a0526bb4a300e5e008 Mon Sep 17 00:00:00 2001 From: Sanket Brahmbhatt Date: Fri, 20 Oct 2023 15:57:28 +0530 Subject: [PATCH] [FIX] account: don't allow to user remove the company When a user creates a journal entry in accounting and the company is empty or not selected in that journal entry, an error is generated. step to reproduce - - install the `Invoicing` & `Inventory`. - open the `Invoicing`. - create New. - open `other info` note page > company (company_id) must be empty. - the error will be generated. Error-ValueError: Expected singleton: res. company() If the user attempts to remove the company after this commit, they get a validation error. sentry-4558397052 closes odoo/odoo#144982 X-original-commit: baa6153bd25a794f432e1a8665098479b33b6e8e Signed-off-by: Brice Bartoletti (bib) --- addons/account/i18n/account.pot | 9 +++++++++ addons/account/models/account_move.py | 5 +++++ 2 files changed, 14 insertions(+) diff --git a/addons/account/i18n/account.pot b/addons/account/i18n/account.pot index 845e6b290a1..369d789f3bb 100644 --- a/addons/account/i18n/account.pot +++ b/addons/account/i18n/account.pot @@ -11947,6 +11947,15 @@ msgstr "" msgid "Select specific invoice and delivery addresses" msgstr "" +#. module: account +#. odoo-python +#: code:addons/account/models/account_move.py:0 +#, python-format +msgid "" +"We can't leave this document without any company. Please select a company " +"for this document." +msgstr "" + #. module: account #: model_terms:ir.ui.view,arch_db:account.res_config_settings_view_form msgid "" diff --git a/addons/account/models/account_move.py b/addons/account/models/account_move.py index c0cb12b897a..e6957559ff3 100644 --- a/addons/account/models/account_move.py +++ b/addons/account/models/account_move.py @@ -1668,6 +1668,11 @@ class AccountMove(models.Model): @api.onchange('company_id') def _inverse_company_id(self): + for move in self: + # This can't be caught by a python constraint as it is only triggered at save and the compute method that + # needs this data to be set correctly before saving + if not move.company_id: + raise ValidationError(_("We can't leave this document without any company. Please select a company for this document.")) self._conditional_add_to_compute('journal_id', lambda m: ( not m.journal_id.filtered_domain(self.env['account.journal']._check_company_domain(m.company_id)) ))