Skip to content

Instantly share code, notes, and snippets.

@blaggacao
Last active May 5, 2019 21:03
Show Gist options
  • Save blaggacao/abc0b1ea343914239e908e14c400c8d0 to your computer and use it in GitHub Desktop.
Save blaggacao/abc0b1ea343914239e908e14c400c8d0 to your computer and use it in GitHub Desktop.
Some odoo.migration backed migration
from odoo import migration
CITY_XMLID_MAP = {} # TODO: Rename external Ids (Yan's work)
REGIME_OBLIGATION_XMLID_MAP = {
"1": "obligation_type_0",
"2": "obligation_type_1",
"3": "obligation_type_2",
"4": "obligation_type_3",
"5": "obligation_type_4",
"6": "obligation_type_5",
"7": "obligation_type_6",
"8": "obligation_type_7",
"9": "obligation_type_8",
"10": "obligation_type_9",
"11": "obligation_type_10",
"12": "obligation_type_11",
"13": "obligation_type_12",
"14": "obligation_type_13",
"15": "obligation_type_14",
"16": "obligation_type_15",
"17": "obligation_type_16",
"18": "obligation_type_17",
"19": "obligation_type_18",
"20": "obligation_type_19",
"21": "obligation_type_20",
"22": "obligation_type_21",
"23": "obligation_type_22",
"24": "obligation_type_23",
"26": "obligation_type_24",
"32": "obligation_type_25",
"33": "obligation_type_26",
"34": "obligation_type_27",
"35": "obligation_type_28",
"36": "obligation_type_29",
"37": "obligation_type_30",
"38": "obligation_type_31",
"39": "obligation_type_32",
"40": "obligation_type_33",
"41": "obligation_type_34",
"42": "obligation_type_35",
}
REGIME_OTHER_XMLID_MAP = {
"AGENCIADIPLO": "other_type_1",
"EmisorasDeTarjetas": "other_type_2",
"NGOInternacional": "other_type_3",
"SCI": "other_type_4",
"EE": "other_type_5",
}
################################
# Handle former l10n_co_terceros
################################
migration.rename_field(
cr, "res.partner", "CO_tin_no", "vat_formatted", update_references=True
)
migration.rename_model(
cr, "actividad.economica", "l10n_co.economic_activity", rename_table=True
)
migration.move_model(
cr,
"l10n_co.economic_activity",
"l10n_co_terceros",
"l10n_co",
move_data=True,
delete=False,
)
migration.rename_model(
cr, "res.partner.regime", "l10n_co.partner_type", rename_table=True
)
migration.move_model(
cr,
"l10n_co.partner_type",
"l10n_co_terceros",
"l10n_co",
move_data=True,
delete=False,
)
# Update external ids
module_str = "l10n_co."
for old, new in REGIME_OBLIGATION_XMLID_MAP.items():
migration.rename_xmlid(cr, module_str + old, module_str + new, noupdate=None)
for old, new in REGIME_OTHER_XMLID_MAP.items():
migration.rename_xmlid(cr, module_str + old, module_str + new, noupdate=None)
# Map former relational values to selection (l10n_co_document_type)
migration.create_column(cr, "res_partner", "l10n_co_document_type", "VARCHAR")
query = """
UPDATE res_partner AS rp
SET l10n_co_document_type=%s
WHERE "rp"."CO_tin_type_id" IN (
SELECT res_id
FROM ir_model_data
WHERE model='res.partner.idtype'
AND name IN %s
)
"""
argslist = [
("nit", ("NIT", "NITE")),
("national_citizen_id", ("CC",)),
("foreign_id_card", ("CE",)),
("passport", ("PP",)),
# ('id_special_permit_ve', ('',)),
("id_card", ("TI",)),
("external_id", ("TE", "TDE", "TDEJ")),
("diplomatic_card", ("CD",)),
# ('residence_document', ('',)),
("civil_registration", ("RC",)),
]
for args in argslist:
cr.execute(query, args)
# Map former relational values to selection (l10n_co_regimen)
migration.create_column(cr, "res_partner", "l10n_co_regimen", "VARCHAR")
query = """
UPDATE res_partner AS rp
SET l10n_co_regimen=%s
WHERE "rp"."CO_regime_id" IN (
SELECT res_id
FROM ir_model_data
WHERE model='l10n_co.partner_type'
AND name=%s
)
"""
argslist = [
("common", "999"),
("common", "400"),
("common", "100"),
("simple", "200"),
("big", "300"),
]
for args in argslist:
cr.execute(query, args)
# Split the regime details to to l10n_co_obligation_type & l10n_co_other_type
migration.create_m2m(
cr,
"partner_l10n_co_obligation_type",
"res_partner",
"l10n_co_partner_type",
col1="partner_id",
col2="type_id",
)
migration.create_m2m(
cr,
"partner_l10n_co_other_type",
"res_partner",
"l10n_co_partner_type",
col1="partner_id",
col2="type_id",
)
regime_obligation_ids = []
for xmlid in REGIME_OBLIGATION_XMLID_MAP.values():
regime_obligation_ids.append(migration.ref(cr, module_str + xmlid))
regime_other_ids = []
for xmlid in REGIME_OTHER_XMLID_MAP.values():
regime_other_ids.append(migration.ref(cr, module_str + xmlid))
query = """
SELECT res_partner_id, res_partner_regime_id
FROM res_partner_res_partner_regime_rel
WHERE res_partner_regime_id IN %s
"""
cr.execute(query, (tuple(regime_obligation_ids),))
insert_query = """
INSERT INTO partner_l10n_co_obligation_type (partner_id, type_id)
VALUES %s
"""
for vals in cr.fetchall():
cr.execute(insert_query, (vals, ))
cr.execute(query, (tuple(regime_other_ids),))
insert_query = """
INSERT INTO partner_l10n_co_other_type (partner_id, type_id)
VALUES %s
"""
for vals in cr.fetchall():
cr.execute(insert_query, (vals, ))
#########################################
# Handle former l10n_co_geo
#########################################
migration.rename_model(cr, "res.country.state.city", "res.city", rename_table=True)
migration.move_model(
cr, "res.city", "l10n_co_geo", "base_address_city", move_data=False, delete=False
)
migration.rename_module(cr, "l10n_co_geo", "l10n_co_data_cities")
module_str = "l10n_co_data_cities."
for old, new in CITY_XMLID_MAP.items():
migration.rename_xmlid(cr, module_str + old, module_str + new, noupdate=None)
#########################################
# Handle former l10n_co_partner_bank_data
#########################################
# No xml changes detected
migration.rename_module(
cr, "l10n_co_partner_bank_data", "l10n_co_data_partners"
)
#########################################
# Handle former l10n_co_terceros_data
#########################################
# No xml changes detected
migration.merge_module(
cr, "l10n_co_terceros_data", "l10n_co_data_partners", tolerant=True
)
########################################
# Handle former tax_min_amount
########################################
migration.rename_field(
cr, "account.tax", "min_inv_amount", "minimum_base", update_references=True
)
########################################
# Handle former l10n_co_product_data
########################################
# Just release the module ownership from the data
# SELECT DISTINCT model FROM ir_model_data WHERE module='l10n_co_product_data';
# model
# ------------------
# product.category
# product.template
# (2 rows)
cr.execute("""DELETE FROM ir_model_data WHERE module='l10n_co_product_data';""")
########################################
# Handle former l10n_co_taxes
########################################
migration.merge_module(cr, "l10n_co_taxes", "l10n_co", tolerant=True)
xml_map = {"fp_imp_colombia": "fp_standard"}
cr.execute("SELECT id FROM res_company;")
module_str = "l10n_co."
for old, new in xml_map.items():
migration.rename_xmlid(cr, module_str + old, module_str + new, noupdate=None)
for c in cr.fetchall():
migration.rename_xmlid(
cr,
module_str + c[0] + "_" + old,
module_str + c[0] + "_" + new,
noupdate=None,
)
# TODO: Cleanup old taxes ?? Are there any?
########################################
# Handle former l10n_co_taxes_bogota
########################################
migration.rename_module(cr, "l10n_co_taxes_bogota", "l10n_co_data_bogota")
########################################
# Handle former l10n_co_niif
########################################
migration.merge_module(cr, "l10n_co_niif", "l10n_co", tolerant=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment