|
3 | 3 |
|
4 | 4 | from odoo import SUPERUSER_ID, api |
5 | 5 |
|
6 | | -OLD_MODULE_NAME = "l10n_it_account_tax_kind" |
7 | | -MODEL = "account.tax" |
8 | | -OLD_MODEL = "account.tax.kind" |
9 | | -RENAMED_FIELDS = [ |
10 | | - ( |
11 | | - "law_reference", |
12 | | - "l10n_it_law_reference", |
13 | | - ), |
| 6 | +OLD_MODULES = [ |
| 7 | + "l10n_it_account_tax_kind", |
| 8 | + "l10n_it_fatturapa", |
| 9 | + "l10n_it_fiscalcode", |
| 10 | + "l10n_it_ipa", |
| 11 | + "l10n_it_rea", |
14 | 12 | ] |
15 | 13 |
|
16 | 14 |
|
| 15 | +def rename_fields(env, table, field_updates): |
| 16 | + """Generic function to rename fields.""" |
| 17 | + set_clauses = ", ".join( |
| 18 | + f"{target_field} = {source_field}" |
| 19 | + for target_field, source_field in field_updates.items() |
| 20 | + ) |
| 21 | + query = f""" |
| 22 | + UPDATE |
| 23 | + {table} |
| 24 | + SET |
| 25 | + {set_clauses} |
| 26 | + """ |
| 27 | + openupgrade.logged_query(env.cr, sql.SQL(query)) |
| 28 | + |
| 29 | + |
| 30 | +def update_table(env, target_table, source_table, field_updates, condition): |
| 31 | + """Generic function to update fields in a table based on a join.""" |
| 32 | + set_clauses = ", ".join( |
| 33 | + f"{target_field} = {source_table}.{source_field}" |
| 34 | + for target_field, source_field in field_updates.items() |
| 35 | + ) |
| 36 | + query = f""" |
| 37 | + UPDATE |
| 38 | + {target_table} |
| 39 | + SET |
| 40 | + {set_clauses} |
| 41 | + FROM |
| 42 | + {source_table} |
| 43 | + """ |
| 44 | + if condition: |
| 45 | + query += f""" |
| 46 | + WHERE |
| 47 | + {condition} |
| 48 | + """ |
| 49 | + openupgrade.logged_query(env.cr, sql.SQL(query)) |
| 50 | + |
| 51 | + |
| 52 | +def _l10n_it_account_tax_kind_migration(env): |
| 53 | + rename_fields( |
| 54 | + env, |
| 55 | + table="account_tax", |
| 56 | + field_updates={"l10n_it_law_reference": "law_reference"}, |
| 57 | + ) |
| 58 | + |
| 59 | + condition = "account_tax.kind_id = account_tax_kind.id" |
| 60 | + condition += " AND account_tax.kind_id IS NOT NULL" |
| 61 | + update_table( |
| 62 | + env, |
| 63 | + target_table="account_tax", |
| 64 | + source_table="account_tax_kind", |
| 65 | + field_updates={"l10n_it_exempt_reason": "code"}, |
| 66 | + condition=condition, |
| 67 | + ) |
| 68 | + |
| 69 | + |
| 70 | +def _l10n_it_fatturapa_migration(env): |
| 71 | + rename_fields( |
| 72 | + env, |
| 73 | + table="res_partner", |
| 74 | + field_updates={ |
| 75 | + "l10n_it_pa_index": "codice_destinatario", |
| 76 | + "l10n_it_pec_email": "pec_destinatario", |
| 77 | + }, |
| 78 | + ) |
| 79 | + rename_fields( |
| 80 | + env, |
| 81 | + table="res_company", |
| 82 | + field_updates={ |
| 83 | + "l10n_it_tax_representative_partner_id": "fatturapa_tax_representative", |
| 84 | + }, |
| 85 | + ) |
| 86 | + |
| 87 | + |
| 88 | +def _l10n_it_fiscalcode_migration(env): |
| 89 | + rename_fields( |
| 90 | + env, |
| 91 | + table="res_partner", |
| 92 | + field_updates={"l10n_it_codice_fiscale": "fiscalcode"}, |
| 93 | + ) |
| 94 | + |
| 95 | + |
| 96 | +def _l10n_it_ipa_migration(env): |
| 97 | + rename_fields( |
| 98 | + env, |
| 99 | + table="res_partner", |
| 100 | + field_updates={"l10n_it_pa_index": "ipa_code"}, |
| 101 | + ) |
| 102 | + |
| 103 | + |
| 104 | +def _l10n_it_rea_migration(env): |
| 105 | + condition = "res_company.partner_id = res_partner.id" |
| 106 | + condition += " AND res_partner.rea_office IS NOT NULL" |
| 107 | + update_table( |
| 108 | + env, |
| 109 | + target_table="res_company", |
| 110 | + source_table="res_partner", |
| 111 | + field_updates={ |
| 112 | + "l10n_it_eco_index_office": "rea_office", |
| 113 | + "l10n_it_eco_index_number": "rea_code", |
| 114 | + "l10n_it_eco_index_share_capital": "rea_capital", |
| 115 | + "l10n_it_eco_index_sole_shareholder": "rea_member_type", |
| 116 | + "l10n_it_eco_index_liquidation_state": "rea_liquidation_state", |
| 117 | + }, |
| 118 | + condition=condition, |
| 119 | + ) |
| 120 | + |
| 121 | + |
17 | 122 | def migrate(cr, version): |
18 | 123 | env = api.Environment(cr, SUPERUSER_ID, {}) |
19 | | - if openupgrade.is_module_installed(env.cr, OLD_MODULE_NAME): |
20 | | - openupgrade.logged_query( |
21 | | - env.cr, |
22 | | - sql.SQL(f""" |
23 | | - UPDATE |
24 | | - {MODEL.replace(".", "_")} |
25 | | - SET |
26 | | - l10n_it_exempt_reason = kind_id.code |
27 | | - FROM |
28 | | - {OLD_MODEL.replace(".", "_")} AS kind |
29 | | - WHERE |
30 | | - {MODEL.replace(".", "_")}.kind_id = kind.id |
31 | | - AND {MODEL.replace(".", "_")}.kind_id IS NOT NULL |
32 | | - """), |
33 | | - ) |
34 | | - |
35 | | - field_spec = [] |
36 | | - for renamed_field in RENAMED_FIELDS: |
37 | | - old_field, new_field = renamed_field |
38 | | - field_spec.append( |
39 | | - ( |
40 | | - MODEL, |
41 | | - MODEL.replace(".", "_"), |
42 | | - old_field, |
43 | | - new_field, |
44 | | - ) |
45 | | - ) |
46 | | - openupgrade.rename_fields( |
47 | | - env, |
48 | | - field_spec, |
49 | | - ) |
| 124 | + for module in OLD_MODULES: |
| 125 | + migration_function = globals().get(f"_{module}_migration") |
| 126 | + if openupgrade.is_module_installed(env.cr, module) and migration_function: |
| 127 | + migration_function(env) |
0 commit comments