From 972171968d9420cdacd3a087a119b473c702d102 Mon Sep 17 00:00:00 2001 From: gawa-odoo Date: Fri, 21 Oct 2022 14:35:55 +0000 Subject: [PATCH] [FIX] account: wrong computation smart button on analytic account The smart buttons were computing the number of aml instead of the number of invoices/bills with the account. We now compute back the number of moves. We also changed it so it is consistent with the view, so it computes the number of moves with move lines that have the distribution instead of the analytic lines with the account. X-original-commit: 84f3ee28156721cea0b530b92ede36fad58c5db8 Part-of: odoo/odoo#104933 --- .../models/account_analytic_account.py | 54 ++++++++++++++----- 1 file changed, 40 insertions(+), 14 deletions(-) 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()