Skip to content

Commit 9fc50bf

Browse files
committed
[OU-FIX] delete safely l10n_it tax xml ids, to be created clean at update
1 parent 6b4ed70 commit 9fc50bf

1 file changed

Lines changed: 73 additions & 0 deletions

File tree

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
from openupgradelib import openupgrade
2+
3+
# List of XML IDs of tax reports defined in l10n_it
4+
# These include the generic VAT report and the annual reports
5+
ITALIAN_REPORT_XML_IDS = [
6+
'l10n_it.tax_report_vat',
7+
'l10n_it.tax_annual_report_vat_va',
8+
'l10n_it.tax_annual_report_vat_ve',
9+
'l10n_it.tax_annual_report_vat_vf',
10+
'l10n_it.tax_annual_report_vat_vh',
11+
'l10n_it.tax_annual_report_vat_vj',
12+
'l10n_it.tax_annual_report_vat_vl',
13+
]
14+
15+
16+
@openupgrade.migrate()
17+
def migrate(env, version):
18+
"""
19+
Remove existing reports and related records to avoid duplications.
20+
"""
21+
if not version:
22+
return
23+
24+
# Try standard cleanup first, this usually isn't enough
25+
openupgrade.delete_records_safely_by_xml_id(
26+
env, ITALIAN_REPORT_XML_IDS, delete_childs=True
27+
)
28+
29+
for xml_id in ITALIAN_REPORT_XML_IDS:
30+
report = env.ref(xml_id, raise_if_not_found=False)
31+
if not report or report._name != 'account.report':
32+
continue
33+
34+
# Explicitly search for linked records
35+
# 1. Report lines
36+
lines = env['account.report.line'].search([('report_id', '=', report.id)])
37+
38+
# 2. Expressions linked to lines
39+
expressions = env['account.report.expression'].search([
40+
('report_line_id', 'in', lines.ids)
41+
])
42+
43+
# 3. Report columns
44+
columns = env['account.report.column'].search([
45+
('report_id', '=', report.id)
46+
])
47+
48+
# Delete related records
49+
for record_set in [expressions, lines, columns]:
50+
if record_set:
51+
try:
52+
openupgrade.safe_unlink(record_set, do_raise=True)
53+
except Exception as e:
54+
openupgrade.logger.error(
55+
"Failed to delete %s for report %s: %s",
56+
record_set._name, xml_id, repr(e)
57+
)
58+
59+
# Delete the report itself
60+
try:
61+
openupgrade.safe_unlink(report, do_raise=True)
62+
except Exception as e:
63+
openupgrade.logger.error(
64+
"Failed to delete report %s: %s", xml_id, repr(e)
65+
)
66+
# Switch to noupdate to avoid future errors if unlink failed
67+
module, name = xml_id.split('.')
68+
imd = env['ir.model.data'].search([
69+
('module', '=', module),
70+
('name', '=', name),
71+
])
72+
if imd and not imd.noupdate:
73+
imd.noupdate = True

0 commit comments

Comments
 (0)