# -*- coding: utf-8 -*- from odoo import api, models, _ from odoo.exceptions import ValidationError, UserError class AccountAccount(models.Model): _inherit = 'account.account' @api.constrains('company_ids', 'account_type') def _check_company_consistency(self): if accounts_without_company := self.filtered(lambda a: not a.sudo().company_ids): raise ValidationError( self.env._( "The following accounts must be assigned to at least one company:\n%(accounts)s", accounts="\n".join(f"- {account.display_name}" for account in accounts_without_company), ), ) # We explicitly REMOVE the check preventing 'asset_cash' accounts from having len(company_ids) > 1 # The standard Odoo validation below was removed: # if self.filtered(lambda a: a.account_type == 'asset_cash' and len(a.company_ids) > 1): # raise ValidationError(_("Bank & Cash accounts cannot be shared between companies.")) # Need to invalidate the sudo cache as we might have just written on `company_ids` self.invalidate_recordset(fnames=['company_ids']) for companies, accounts in self.grouped(lambda a: a.company_ids).items(): if self.env['account.move.line'].sudo().search_count([ ('account_id', 'in', accounts.ids), '!', ('company_id', 'child_of', companies.ids) ], limit=1): raise UserError(_("You can't unlink this company from this account since there are some journal items linked to it."))