From 1818ae139c873a0e8b2bc24c0d38a8aee836e4fa Mon Sep 17 00:00:00 2001
From: Holger Brunn <mail@hunki-enterprises.com>
Date: Thu, 3 Oct 2024 20:38:46 +0200
Subject: [PATCH] [IMP] account: assign tag tags of parent taxes to repartition
 lines

---
 .../migrations/13.0.1.1/post-migration.py     | 22 ++++++++++++++++---
 1 file changed, 19 insertions(+), 3 deletions(-)

diff --git a/addons/account/migrations/13.0.1.1/post-migration.py b/addons/account/migrations/13.0.1.1/post-migration.py
index 38e1561e7ebc..3f2f2e4abb80 100644
--- a/addons/account/migrations/13.0.1.1/post-migration.py
+++ b/addons/account/migrations/13.0.1.1/post-migration.py
@@ -851,13 +851,21 @@ def create_account_tax_repartition_lines(env):
 def move_tags_from_taxes_to_repartition_lines(env):
     openupgrade.logged_query(
         env.cr, """
+        WITH RECURSIVE tax2parent(tax_id, parent_id) AS (
+            SELECT id, id FROM account_tax
+            UNION ALL
+            SELECT rel.child_tax, rel.parent_tax
+            FROM account_tax_filiation_rel rel
+            JOIN tax2parent ON tax2parent.parent_id=rel.child_tax
+        )
         INSERT INTO account_account_tag_account_tax_repartition_line_rel (
             account_tax_repartition_line_id, account_account_tag_id)
         SELECT atrl.id, atat.account_account_tag_id
         FROM account_tax_account_tag atat
+        JOIN tax2parent ON atat.account_tax_id=tax2parent.parent_id
         JOIN account_tax_repartition_line atrl ON
-            (atat.account_tax_id = atrl.invoice_tax_id OR
-             atat.account_tax_id = atrl.refund_tax_id)
+            (tax2parent.tax_id = atrl.invoice_tax_id OR
+             tax2parent.tax_id = atrl.refund_tax_id)
         ON CONFLICT DO NOTHING"""
     )
     openupgrade.logged_query(
@@ -944,14 +952,22 @@ def assign_account_tags_to_move_lines(env):
     # move lines with taxes
     openupgrade.logged_query(
         env.cr, """
+        WITH RECURSIVE tax2child(tax_id, child_id) AS (
+            SELECT id, id FROM account_tax
+            UNION ALL
+            SELECT rel.parent_tax, rel.child_tax
+            FROM account_tax_filiation_rel rel
+            JOIN tax2child ON tax2child.child_id=rel.parent_tax
+        )
         INSERT INTO account_account_tag_account_move_line_rel (
             account_move_line_id, account_account_tag_id)
         SELECT aml.id, aat_atr_rel.account_account_tag_id
         FROM account_move_line aml
         JOIN account_move am ON aml.move_id = am.id
         JOIN account_move_line_account_tax_rel amlatr ON amlatr.account_move_line_id = aml.id
+        JOIN tax2child ON amlatr.account_tax_id=tax2child.child_id
         JOIN account_tax_repartition_line atrl ON (
-            atrl.invoice_tax_id = amlatr.account_tax_id AND atrl.repartition_type = 'base')
+            atrl.invoice_tax_id = tax2child.tax_id AND atrl.repartition_type = 'base')
         JOIN account_account_tag_account_tax_repartition_line_rel aat_atr_rel ON
             aat_atr_rel.account_tax_repartition_line_id = atrl.id
         WHERE aml.old_invoice_line_id IS NOT NULL AND am.type in ('out_invoice', 'in_invoice')