From 83efa5993e66b0cfef9f0cbfee20358bb1b7fcde Mon Sep 17 00:00:00 2001 From: "Gauthier Wala (gawa)" Date: Wed, 15 Nov 2023 14:57:45 +0100 Subject: [PATCH] [FIX] analytic: plans without account but with subplan can appear MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit If we have an analytic plan without analytic accounts, but with subplans that do have analytic accounts, we do want them to appear in the widget. Also changed a search in a loop to a read_group closes odoo/odoo#142222 Signed-off-by: William André (wan) --- addons/analytic/models/analytic_plan.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/addons/analytic/models/analytic_plan.py b/addons/analytic/models/analytic_plan.py index f2c05e6139f..58347728e2f 100644 --- a/addons/analytic/models/analytic_plan.py +++ b/addons/analytic/models/analytic_plan.py @@ -147,8 +147,15 @@ class AccountAnalyticPlan(models.Model): @api.depends('account_ids', 'children_ids') def _compute_all_analytic_account_count(self): + plans_count = dict( + self.env['account.analytic.account']._read_group( + domain=[('root_plan_id', 'in', self.ids)], + aggregates=['id:count'], + groupby=['root_plan_id'] + ) + ) for plan in self: - plan.all_account_count = self.env['account.analytic.account'].search_count([('plan_id', "child_of", plan.id)]) + plan.all_account_count = plans_count.get(plan, 0) @api.depends('children_ids') def _compute_children_count(self): @@ -185,7 +192,7 @@ class AccountAnalyticPlan(models.Model): record_account_ids = kwargs.get('existing_account_ids', []) project_plan, other_plans = self.env['account.analytic.plan']._get_all_plans() root_plans = (project_plan + other_plans).filtered(lambda p: ( - bool(p.account_ids) + p.all_account_count > 0 and not p.parent_id and p._get_applicability(**kwargs) != 'unavailable' ))