diff --git a/l10n_it_account/migrations/18.0.1.0.0/post-migration.py b/l10n_it_account/migrations/18.0.1.0.0/post-migration.py index 33178c93b43..d71ed47c27f 100644 --- a/l10n_it_account/migrations/18.0.1.0.0/post-migration.py +++ b/l10n_it_account/migrations/18.0.1.0.0/post-migration.py @@ -3,47 +3,125 @@ from odoo import SUPERUSER_ID, api -OLD_MODULE_NAME = "l10n_it_account_tax_kind" -MODEL = "account.tax" -OLD_MODEL = "account.tax.kind" -RENAMED_FIELDS = [ - ( - "law_reference", - "l10n_it_law_reference", - ), +OLD_MODULES = [ + "l10n_it_account_tax_kind", + "l10n_it_fatturapa", + "l10n_it_fiscalcode", + "l10n_it_ipa", + "l10n_it_rea", ] +def rename_fields(env, table, field_updates): + """Generic function to rename fields.""" + set_clauses = ", ".join( + f"{target_field} = {source_field}" + for target_field, source_field in field_updates.items() + ) + query = f""" + UPDATE + {table} + SET + {set_clauses} + """ + openupgrade.logged_query(env.cr, sql.SQL(query)) + + +def update_table(env, target_table, source_table, field_updates, condition): + """Generic function to update fields in a table based on a join.""" + set_clauses = ", ".join( + f"{target_field} = {source_table}.{source_field}" + for target_field, source_field in field_updates.items() + ) + query = f""" + UPDATE + {target_table} + SET + {set_clauses} + FROM + {source_table} + """ + if condition: + query += f""" + WHERE + {condition} + """ + openupgrade.logged_query(env.cr, sql.SQL(query)) + + +def _l10n_it_account_tax_kind_migration(env): + rename_fields( + env, + table="account_tax", + field_updates={"l10n_it_law_reference": "law_reference"}, + ) + + condition = "account_tax.kind_id = account_tax_kind.id" + condition += " AND account_tax.kind_id IS NOT NULL" + update_table( + env, + target_table="account_tax", + source_table="account_tax_kind", + field_updates={"l10n_it_exempt_reason": "code"}, + condition=condition, + ) + + +def _l10n_it_fatturapa_migration(env): + rename_fields( + env, + table="res_partner", + field_updates={ + "l10n_it_pa_index": "codice_destinatario", + "l10n_it_pec_email": "pec_destinatario", + }, + ) + rename_fields( + env, + table="res_company", + field_updates={ + "l10n_it_tax_representative_partner_id": "fatturapa_tax_representative", + }, + ) + + +def _l10n_it_fiscalcode_migration(env): + rename_fields( + env, + table="res_partner", + field_updates={"l10n_it_codice_fiscale": "fiscalcode"}, + ) + + +def _l10n_it_ipa_migration(env): + rename_fields( + env, + table="res_partner", + field_updates={"l10n_it_pa_index": "ipa_code"}, + ) + + +def _l10n_it_rea_migration(env): + condition = "res_company.partner_id = res_partner.id" + condition += " AND res_partner.rea_office IS NOT NULL" + update_table( + env, + target_table="res_company", + source_table="res_partner", + field_updates={ + "l10n_it_eco_index_office": "rea_office", + "l10n_it_eco_index_number": "rea_code", + "l10n_it_eco_index_share_capital": "rea_capital", + "l10n_it_eco_index_sole_shareholder": "rea_member_type", + "l10n_it_eco_index_liquidation_state": "rea_liquidation_state", + }, + condition=condition, + ) + + def migrate(cr, version): env = api.Environment(cr, SUPERUSER_ID, {}) - if openupgrade.is_module_installed(env.cr, OLD_MODULE_NAME): - openupgrade.logged_query( - env.cr, - sql.SQL(f""" - UPDATE - {MODEL.replace(".", "_")} - SET - l10n_it_exempt_reason = kind_id.code - FROM - {OLD_MODEL.replace(".", "_")} AS kind - WHERE - {MODEL.replace(".", "_")}.kind_id = kind.id - AND {MODEL.replace(".", "_")}.kind_id IS NOT NULL - """), - ) - - field_spec = [] - for renamed_field in RENAMED_FIELDS: - old_field, new_field = renamed_field - field_spec.append( - ( - MODEL, - MODEL.replace(".", "_"), - old_field, - new_field, - ) - ) - openupgrade.rename_fields( - env, - field_spec, - ) + for module in OLD_MODULES: + migration_function = globals().get(f"_{module}_migration") + if openupgrade.is_module_installed(env.cr, module) and migration_function: + migration_function(env)