From 15617476297da56a4e36f524e693404915742d2d Mon Sep 17 00:00:00 2001 From: "Nicolas (vin)" Date: Tue, 4 Oct 2022 06:58:59 +0000 Subject: [PATCH] [FIX] account: avoid receivable accounts on bank journals You cannot set a receivable or payable account as a bank account of a bank journal directly, but it is very possible to just set an account of another type, then change the type of this account to receivable. This new constrains will make sure that you cannot change the type of an account.account set as bank account on a bank journal to any of those two types. closes odoo/odoo#102790 X-original-commit: f9500ca4c898cd86681cc2e48ec2c66cf5670917 Related: odoo/enterprise#32579 Signed-off-by: Florian Gilbert (flg) Signed-off-by: Nicolas Viseur --- addons/account/i18n/account.pot | 8 ++++++++ addons/account/models/account_account.py | 15 +++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/addons/account/i18n/account.pot b/addons/account/i18n/account.pot index 274e4b24754..38b2c129f20 100644 --- a/addons/account/i18n/account.pot +++ b/addons/account/i18n/account.pot @@ -15039,6 +15039,14 @@ msgid "" "journal items." msgstr "" +#. module: account +#: code:addons/account/models/account_account.py:0 +#, python-format +msgid "" +"You cannot change the type of an account set as Bank Account on a journal to" +" Receivable or Payable." +msgstr "" + #. module: account #: code:addons/account/models/partner.py:0 #, python-format diff --git a/addons/account/models/account_account.py b/addons/account/models/account_account.py index b88135072c8..24e76802de8 100644 --- a/addons/account/models/account_account.py +++ b/addons/account/models/account_account.py @@ -296,6 +296,21 @@ class AccountAccount(models.Model): "The account code can only contain alphanumeric characters and dots." )) + @api.constrains('account_type') + def _check_account_is_bank_journal_bank_account(self): + self.env['account.account'].flush_model(['account_type']) + self.env['account.journal'].flush_model(['type', 'default_account_id']) + self._cr.execute(''' + SELECT journal.id + FROM account_journal journal + JOIN account_account account ON journal.default_account_id = account.id + WHERE account.account_type IN ('asset_receivable', 'liability_payable') + LIMIT 1; + ''', [tuple(self.ids)]) + + if self._cr.fetchone(): + raise ValidationError(_("You cannot change the type of an account set as Bank Account on a journal to Receivable or Payable.")) + @api.depends('code') def _compute_account_root(self): # this computes the first 2 digits of the account.