From 6f16302a2e556feac8219395af1d9ec84b94b6a4 Mon Sep 17 00:00:00 2001 From: Alexis de Lattre Date: Mon, 13 Dec 2021 01:01:01 +0100 Subject: [PATCH] [MIG] hs_code_id company_dependent=True to False Migration script provided for product.template and product.category Fixes bug #126 --- product_harmonized_system/__manifest__.py | 2 +- .../migrations/14.0.2.0.0/post-migration.py | 45 +++++++++++++++++++ .../models/product_category.py | 4 +- .../models/product_template.py | 4 +- 4 files changed, 52 insertions(+), 3 deletions(-) create mode 100644 product_harmonized_system/migrations/14.0.2.0.0/post-migration.py diff --git a/product_harmonized_system/__manifest__.py b/product_harmonized_system/__manifest__.py index 93d9b843e..466492272 100644 --- a/product_harmonized_system/__manifest__.py +++ b/product_harmonized_system/__manifest__.py @@ -9,7 +9,7 @@ { "name": "Product Harmonized System Codes", - "version": "14.0.1.0.1", + "version": "14.0.2.0.0", "category": "Reporting", "license": "AGPL-3", "summary": "Base module for Product Import/Export reports", diff --git a/product_harmonized_system/migrations/14.0.2.0.0/post-migration.py b/product_harmonized_system/migrations/14.0.2.0.0/post-migration.py new file mode 100644 index 000000000..2319cf261 --- /dev/null +++ b/product_harmonized_system/migrations/14.0.2.0.0/post-migration.py @@ -0,0 +1,45 @@ +# Copyright 2021 Akretion France (http://www.akretion.com/) +# @author: Alexis de Lattre +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from odoo import SUPERUSER_ID, api + + +def migrate(cr, version): + if not version: + return + + with api.Environment.manage(): + env = api.Environment(cr, SUPERUSER_ID, {}) + pc_field_id = env.ref( + "product_harmonized_system.field_product_category__hs_code_id" + ).id + cr.execute( + """ + UPDATE product_category pc + SET hs_code_id=SUBSTRING(ip.value_reference, 9, 99)::int + FROM ir_property ip + WHERE ip.res_id like 'product.category,%%' AND + SUBSTRING(ip.res_id, 18, 99)::int=pc.id AND + ip.name='hs_code_id' AND + ip.value_reference IS NOT null AND + ip.fields_id=%s + """, + (pc_field_id,), + ) + pt_field_id = env.ref( + "product_harmonized_system.field_product_template__hs_code_id" + ).id + cr.execute( + """ + UPDATE product_template pt + SET hs_code_id=SUBSTRING(ip.value_reference, 9, 99)::int + FROM ir_property ip + WHERE ip.res_id like 'product.template,%%' AND + SUBSTRING(ip.res_id, 18, 99)::int=pt.id AND + ip.name='hs_code_id' AND + ip.value_reference IS NOT null AND + ip.fields_id=%s + """, + (pt_field_id,), + ) diff --git a/product_harmonized_system/models/product_category.py b/product_harmonized_system/models/product_category.py index d4400c986..89d10a311 100644 --- a/product_harmonized_system/models/product_category.py +++ b/product_harmonized_system/models/product_category.py @@ -13,7 +13,9 @@ class ProductCategory(models.Model): hs_code_id = fields.Many2one( "hs.code", string="H.S. Code", - company_dependent=True, + # company_dependent updated from True to False in 14.0.2.0.0 + # migration scripts provided + company_dependent=False, ondelete="restrict", help="Harmonised System Code. If this code is not " "set on the product itself, it will be read here, on the " diff --git a/product_harmonized_system/models/product_template.py b/product_harmonized_system/models/product_template.py index b37ee0447..4670cfc74 100644 --- a/product_harmonized_system/models/product_template.py +++ b/product_harmonized_system/models/product_template.py @@ -13,7 +13,9 @@ class ProductTemplate(models.Model): hs_code_id = fields.Many2one( "hs.code", string="H.S. Code", - company_dependent=True, + # company_dependent updated from True to False in 14.0.2.0.0 + # migration scripts provided + company_dependent=False, ondelete="restrict", help="Harmonised System Code. Nomenclature is " "available from the World Customs Organisation, see "