From a449e2ccf61e469b4503b8cdff07b2c89c445c3a Mon Sep 17 00:00:00 2001 From: oco-odoo Date: Fri, 15 Nov 2019 13:18:29 +0000 Subject: [PATCH] [FIX] account: tax report: remove tags from aml when needing to delete them because of tag_name removal Before that, when trying setting to None the tag_name of a tax report line whose tags were referenced by some move lines, a exception was raised, telling that some account.move.line objects still referenced an account.tag we were trying to delete. Removing the tags from db was legit (since we're removing the tag_name, we need to remove the tags that it generated), but first needed to remove all these tags from the account move lines linked to them. closes odoo/odoo#40375 X-original-commit: 3eb5a3e947bb371f90c9f2357ff410816419bbb0 Signed-off-by: Laurent Smet --- addons/account/models/account.py | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/addons/account/models/account.py b/addons/account/models/account.py index be5ad351a43..ef333b1de3f 100644 --- a/addons/account/models/account.py +++ b/addons/account/models/account.py @@ -141,11 +141,23 @@ class AccountTaxReportLine(models.Model): return super(AccountTaxReportLine, self).unlink() def _delete_tags_from_taxes(self, tag_ids_to_delete): - """ Based on a list of tag ids, delete them all from database, removing them - first from the repartition lines they are linked to. + """ Based on a list of tag ids, removes them first from the + repartition lines they are linked to, then deletes them + from the account move lines. """ - repartition_lines = self.env['account.tax.repartition.line'].search([('tag_ids', 'in', tag_ids_to_delete)]) - repartition_lines.write({'tag_ids': [(3, tag_id, 0) for tag_id in tag_ids_to_delete]}) + if not tag_ids_to_delete: + # Nothing to do, then! + return + + self.env.cr.execute(""" + delete from account_account_tag_account_tax_repartition_line_rel + where account_account_tag_id in %(tag_ids_to_delete)s; + delete from account_account_tag_account_move_line_rel + where account_account_tag_id in %(tag_ids_to_delete)s; + """, {'tag_ids_to_delete': tuple(tag_ids_to_delete)}) + + self.env['account.move.line'].invalidate_cache(fnames=['tag_ids']) + self.env['account.tax.repartition.line'].invalidate_cache(fnames=['tag_ids']) @api.constrains('formula', 'tag_name') def _validate_formula(self):