From 7406ec13d34bb9f292b96f9edb72bfdbc97acc84 Mon Sep 17 00:00:00 2001 From: aliya Date: Wed, 27 Jul 2022 08:38:42 +0000 Subject: [PATCH] [FIX] account: fix account type computation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit There can be many accounts in self and the "search_read" is currently breaking because of self.company_id in the domain. This commit addresses two problems: - Creating accounts in a multi-company environment - Setting a default account_type when there is no parent found. Account codes should however be still limited to the current company even though for performance reason we batch the searching part. closes odoo/odoo#96832 Signed-off-by: William André (wan) --- addons/account/models/account_account.py | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/addons/account/models/account_account.py b/addons/account/models/account_account.py index 2dfdce49c0c..0c884506ab7 100644 --- a/addons/account/models/account_account.py +++ b/addons/account/models/account_account.py @@ -3,6 +3,7 @@ from odoo import api, fields, models, _, tools from odoo.osv import expression from odoo.exceptions import UserError, ValidationError from bisect import bisect_left +from collections import defaultdict import re ACCOUNT_REGEX = re.compile(r'(?:(\S*\d+\S*)\s)?(.*)') @@ -374,14 +375,23 @@ class AccountAccount(models.Model): def _compute_account_type(self): """ Compute the account type based on the account code. Search for the closest parent account code and sets the account type according to the parent. + If there is no parent (e.g. the account code is lower than any other existing account code), + the account type will be set to 'asset_current'. """ accounts_to_process = self.filtered(lambda r: r.code and not r.account_type) - accounts_with_codes = {r['code']: r['account_type'] for r in self.search_read(domain=[( - 'code', '!=', False), ('company_id', '=', self.company_id.id)], fields=['code', 'account_type'])} - codes_list = list(accounts_with_codes.keys()) + all_accounts = self.search_read( + domain=[('company_id', 'in', accounts_to_process.company_id.ids)], + fields=['code', 'account_type', 'company_id'], + order='code', + ) + accounts_with_codes = defaultdict(dict) + # We want to group accounts by company to only search for account codes of the current company + for account in all_accounts: + accounts_with_codes[account['company_id'][0]][account['code']] = account['account_type'] for account in accounts_to_process: + codes_list = list(accounts_with_codes[account.company_id.id].keys()) closest_index = bisect_left(codes_list, account.code) - 1 - account.account_type = accounts_with_codes[codes_list[closest_index]] + account.account_type = accounts_with_codes[account.company_id.id][codes_list[closest_index]] if closest_index != -1 else 'asset_current' @api.depends('internal_group') def _compute_is_off_balance(self):