From 9dc31a29ccd40b2a3e7606b8e6b8463aa74dc281 Mon Sep 17 00:00:00 2001 From: "Habib (ayh)" Date: Wed, 5 Apr 2023 09:42:27 +0000 Subject: [PATCH] [FIX] analytic: handle deleted analytic accounts The analytic distribution json field may contain a deleted analytic account. This causes 2 issues: - When retrieving plans - the analytic account ids are used to force additional plans (maybe applied by a model) - causing a record does not exist error - Opening and closing the popup is required to 'clean' the distribution. this is not ideal as a draft invoice will not display the deleted account, but it is still in the json. With this fix, the analytic accounts existence is checked, and the distribution json is saved without the deleted account. closes odoo/odoo#117835 X-original-commit: ce5045d4ba5633790dfce1ef44c10b5770432829 Signed-off-by: Brice Bartoletti (bib) --- addons/analytic/models/analytic_plan.py | 2 +- .../analytic_distribution/analytic_distribution.js | 8 +++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/addons/analytic/models/analytic_plan.py b/addons/analytic/models/analytic_plan.py index f89637540a0..d04a39fd021 100644 --- a/addons/analytic/models/analytic_plan.py +++ b/addons/analytic/models/analytic_plan.py @@ -145,7 +145,7 @@ class AccountAnalyticPlan(models.Model): # If we have accounts that are already selected (before the applicability rules changed or from a model), # we want the plans that were unavailable to be shown in the list (and in optional, because the previous # percentage could be different from 0) - forced_plans = self.env['account.analytic.account'].browse(record_account_ids).mapped( + forced_plans = self.env['account.analytic.account'].browse(record_account_ids).exists().mapped( 'root_plan_id') - root_plans return sorted([ { diff --git a/addons/analytic/static/src/components/analytic_distribution/analytic_distribution.js b/addons/analytic/static/src/components/analytic_distribution/analytic_distribution.js index 408e38a7882..a1adb8c6afa 100644 --- a/addons/analytic/static/src/components/analytic_distribution/analytic_distribution.js +++ b/addons/analytic/static/src/components/analytic_distribution/analytic_distribution.js @@ -115,9 +115,6 @@ export class AnalyticDistribution extends Component { const data = nextProps.record.data[nextProps.name]; const analytic_account_ids = Object.keys(data).map((id) => parseInt(id)); const records = analytic_account_ids.length ? await this.fetchAnalyticAccounts([["id", "in", analytic_account_ids]]) : []; - if (records.length < data.length) { - console.log('removing tags... value should be updated'); - } let widgetData = Object.assign({}, ...this.allPlans.map((plan) => ({[plan.id]: {...plan, distribution: []}}))); records.map((record) => { if (!widgetData[record.root_plan_id[0]]) { @@ -135,6 +132,11 @@ export class AnalyticDistribution extends Component { }); this.state.list = widgetData; + if (records.length < Object.keys(data).length) { + // analytic accounts were not found for some keys in the json data, they may have been deleted + // save the json without them + this.save(); + } } // ORM