|
| 1 | +from openupgradelib import openupgrade |
| 2 | + |
| 3 | +# Lista degli XML ID dei report principali definiti in l10n_it |
| 4 | +# Questi includono il report IVA generico e i singoli quadri annuali |
| 5 | +ITALIAN_REPORT_XML_IDS = [ |
| 6 | + 'l10n_it.tax_report_vat', # Report IVA Periodico (account_tax_report_data.xml) |
| 7 | + 'l10n_it.tax_annual_report_vat_va', # Quadro VA |
| 8 | + 'l10n_it.tax_annual_report_vat_ve', # Quadro VE |
| 9 | + 'l10n_it.tax_annual_report_vat_vf', # Quadro VF |
| 10 | + 'l10n_it.tax_annual_report_vat_vh', # Quadro VH |
| 11 | + 'l10n_it.tax_annual_report_vat_vj', # Quadro VJ |
| 12 | + 'l10n_it.tax_annual_report_vat_vl', # Quadro 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 |
| 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