|
| 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([("report_id", "=", report.id)]) |
| 45 | + |
| 46 | + # Delete related records |
| 47 | + for record_set in [expressions, lines, columns]: |
| 48 | + if record_set: |
| 49 | + try: |
| 50 | + openupgrade.safe_unlink(record_set, do_raise=True) |
| 51 | + except Exception as e: |
| 52 | + openupgrade.logger.error( |
| 53 | + "Failed to delete %s for report %s: %s", |
| 54 | + record_set._name, |
| 55 | + xml_id, |
| 56 | + 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("Failed to delete report %s: %s", xml_id, repr(e)) |
| 64 | + # Switch to noupdate to avoid future errors if unlink failed |
| 65 | + module, name = xml_id.split(".") |
| 66 | + imd = env["ir.model.data"].search( |
| 67 | + [ |
| 68 | + ("module", "=", module), |
| 69 | + ("name", "=", name), |
| 70 | + ] |
| 71 | + ) |
| 72 | + if imd and not imd.noupdate: |
| 73 | + imd.noupdate = True |
0 commit comments