diff --git a/addons/account/models/account.py b/addons/account/models/account.py index d7613478ce6..d0acbc380c0 100644 --- a/addons/account/models/account.py +++ b/addons/account/models/account.py @@ -67,12 +67,11 @@ class AccountAccount(models.Model): @api.constrains('user_type_id') def _check_user_type_id(self): data_unaffected_earnings = self.env.ref('account.data_unaffected_earnings') - for company in self.mapped('company_id'): - account_unaffected_earnings = self.search([ - ('company_id', '=', company.id), - ('user_type_id', '=', data_unaffected_earnings.id), - ]) - if len(account_unaffected_earnings) >= 2: + result = self.read_group([('user_type_id', '=', data_unaffected_earnings.id)], ['company_id'], ['company_id']) + for res in result: + if res.get('company_id_count', 0) >= 2: + account_unaffected_earnings = self.search([('company_id', '=', res['company_id'][0]), + ('user_type_id', '=', data_unaffected_earnings.id)]) raise ValidationError(_('You cannot have more than one account with "Current Year Earnings" as type. (accounts: %s)') % [a.code for a in account_unaffected_earnings]) name = fields.Char(required=True, index=True)