[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) <bib@odoo.com>
This commit is contained in:
Habib (ayh)
2023-04-05 20:03:09 +02:00
parent 6ebe32e106
commit 9dc31a29cc
2 changed files with 6 additions and 4 deletions
+1 -1
View File
@@ -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([
{
@@ -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