diff --git a/addons/account/models/account_analytic_account.py b/addons/account/models/account_analytic_account.py index a80a2f5e9ef..8ef84cd0adc 100644 --- a/addons/account/models/account_analytic_account.py +++ b/addons/account/models/account_analytic_account.py @@ -18,26 +18,52 @@ class AccountAnalyticAccount(models.Model): @api.depends('line_ids') def _compute_invoice_count(self): sale_types = self.env['account.move'].get_sale_types(include_receipts=True) - domain = [ - ('move_line_id.move_id.move_type', 'in', sale_types), - ('account_id', 'in', self.ids) - ] - groups = self.env['account.analytic.line']._read_group(domain, ['move_line_id.move_id:count_distinct'], ['account_id']) - moves_count_mapping = dict((g['account_id'][0], g['account_id_count']) for g in groups) + + query = self.env['account.move.line']._search([ + ('move_id.state', '=', 'posted'), + ('move_id.move_type', 'in', sale_types), + ]) + query.add_where( + 'account_move_line.analytic_distribution ?| array[%s]', + [str(account_id) for account_id in self.ids], + ) + + query.order = None + query_string, query_param = query.select( + 'jsonb_object_keys(account_move_line.analytic_distribution) as account_id', + 'COUNT(DISTINCT(move_id)) as move_count', + ) + query_string = f"{query_string} GROUP BY jsonb_object_keys(account_move_line.analytic_distribution)" + + self._cr.execute(query_string, query_param) + data = {int(record.get('account_id')): record.get('move_count') for record in self._cr.dictfetchall()} for account in self: - account.invoice_count = moves_count_mapping.get(account.id, 0) + account.invoice_count = data.get(account.id, 0) @api.depends('line_ids') def _compute_vendor_bill_count(self): purchase_types = self.env['account.move'].get_purchase_types(include_receipts=True) - domain = [ - ('move_line_id.move_id.move_type', 'in', purchase_types), - ('account_id', 'in', self.ids) - ] - groups = self.env['account.analytic.line']._read_group(domain, ['move_line_id.move_id:count_distinct'], ['account_id']) - moves_count_mapping = dict((g['account_id'][0], g['account_id_count']) for g in groups) + + query = self.env['account.move.line']._search([ + ('move_id.state', '=', 'posted'), + ('move_id.move_type', 'in', purchase_types), + ]) + query.add_where( + 'account_move_line.analytic_distribution ?| array[%s]', + [str(account_id) for account_id in self.ids], + ) + + query.order = None + query_string, query_param = query.select( + 'jsonb_object_keys(account_move_line.analytic_distribution) as account_id', + 'COUNT(DISTINCT(move_id)) as move_count', + ) + query_string = f"{query_string} GROUP BY jsonb_object_keys(account_move_line.analytic_distribution)" + + self._cr.execute(query_string, query_param) + data = {int(record.get('account_id')): record.get('move_count') for record in self._cr.dictfetchall()} for account in self: - account.vendor_bill_count = moves_count_mapping.get(account.id, 0) + account.vendor_bill_count = data.get(account.id, 0) def action_view_invoice(self): self.ensure_one()