diff --git a/hs_code_link/__manifest__.py b/hs_code_link/__manifest__.py index 6824b08e3..ca7101bf0 100644 --- a/hs_code_link/__manifest__.py +++ b/hs_code_link/__manifest__.py @@ -1,8 +1,9 @@ # Copyright 2017 Camptocamp SA +# Copyright 2023 ForgeFlow # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). { "name": "HS Code Link", - "version": "13.0.1.0.1", + "version": "13.0.1.1.1", "depends": ["product_harmonized_system", "delivery"], "author": "Camptocamp SA, Odoo Community Association (OCA)", "website": "https://github.com/OCA/intrastat-extrastat", diff --git a/hs_code_link/migrations/13.0.1.1.1/post-migration.py b/hs_code_link/migrations/13.0.1.1.1/post-migration.py new file mode 100644 index 000000000..0ae7a2037 --- /dev/null +++ b/hs_code_link/migrations/13.0.1.1.1/post-migration.py @@ -0,0 +1,71 @@ +# Copyright 2023 ForgeFlow +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). +import logging + +from openupgradelib import openupgrade + +from odoo import SUPERUSER_ID + +_logger = logging.getLogger(__name__) + + +def _fill_columns(env): + _logger.info("Filling hs_code Company Property") + field_name = "hs_code" + template_model = env["ir.model"].search( + [("model", "=", "product.template")], limit=1 + ) + field = env["ir.model.fields"].search( + [("name", "=", field_name), ("model_id", "=", template_model.id)], limit=1 + ) + if not field: + _logger.info("Field %s not found." % (field_name)) + return False + insert_query = """ + WITH query AS ( + SELECT + ip.company_id AS company_id, + pt.id AS product_tmpl_id, + SUBSTRING(hc.local_code, 0, 7) AS value_text + FROM ir_property ip + JOIN product_template pt ON + CAST(SUBSTRING(ip.res_id, 18, 30) AS INTEGER) = pt.id + JOIN hs_code hc ON + CAST(SUBSTRING(ip.value_reference, 9, 20) AS INTEGER) = hc.id + WHERE + ip.name = 'hs_code_id' + AND ip.res_id ILIKE 'product.template,%' + ) + INSERT INTO ir_property ( + name, + res_id, + company_id, + fields_id, + value_text, + type, + create_date, + write_date, + create_uid, + write_uid + ) + SELECT + 'hs_code', + 'product.template,' || CAST(product_tmpl_id AS VARCHAR), + company_id, + {}, + value_text, + 'char', + now(), + now(), + {}, + {} + FROM query; + """.format( + field.id, SUPERUSER_ID, SUPERUSER_ID + ) + env.cr.execute(insert_query) + + +@openupgrade.migrate() +def migrate(env, version): + _fill_columns(env) diff --git a/hs_code_link/models/__init__.py b/hs_code_link/models/__init__.py index 9649db77a..923698b5d 100644 --- a/hs_code_link/models/__init__.py +++ b/hs_code_link/models/__init__.py @@ -1 +1,2 @@ from . import product +from . import ir_property diff --git a/hs_code_link/models/ir_property.py b/hs_code_link/models/ir_property.py new file mode 100644 index 000000000..0682c0eab --- /dev/null +++ b/hs_code_link/models/ir_property.py @@ -0,0 +1,36 @@ +# Copyright 2023 ForgeFlow +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +from odoo import api, models + + +class IrProperty(models.Model): + _inherit = "ir.property" + + @api.model + def set_multi(self, name, model, values, default_value=None): + """ + When the H.S. Code instance is updated on the Product Template, we also update + the H.S. Code (6 digits code) value + """ + result = super().set_multi(name, model, values, default_value=default_value) + if name == "hs_code_id" and model == "product.template": + Template = self.env[model] + hs_code_values = { + rec_id: Template.browse(rec_id).hs_code + for rec_id, _rec_val in values.items() + } + self.set_multi("hs_code", model, hs_code_values) + return result + + def unlink(self): + to_delete_ids = self.ids + for p_id in self.ids: + p = self.browse(p_id) + if p.exists() and p.name == "hs_code_id" and "product.template" in p.res_id: + r_p = self.search( + [("res_id", "=", p.res_id), ("name", "=", "hs_code")], limit=1 + ) + to_delete_ids.append(r_p.id) + self = self.browse(to_delete_ids) + return super(IrProperty, self).unlink() diff --git a/hs_code_link/models/product.py b/hs_code_link/models/product.py index d00049580..81f1ce267 100644 --- a/hs_code_link/models/product.py +++ b/hs_code_link/models/product.py @@ -1,4 +1,5 @@ # Copyright 2017 Camptocamp SA +# Copyright 2023 ForgeFlow # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). from odoo import fields, models @@ -7,4 +8,14 @@ class ProductTemplate(models.Model): _inherit = "product.template" - hs_code = fields.Char(related="hs_code_id.hs_code", readonly=True, store=True) + # Company Dependent field, as we want to have as a related field to the + # hs_code_id.hs_code value + hs_code = fields.Char(company_dependent=True) + + # 1. FIRST SOLUTION: + # hs_code = fields.Char(compute="_compute_hs_code") + + # @api.depends_context("force_company") + # def _compute_hs_code(self): + # for template in self: + # template.hs_code = template.hs_code_id.hs_code diff --git a/hs_code_link/readme/CONTRIBUTORS.rst b/hs_code_link/readme/CONTRIBUTORS.rst index fcb7aaa08..316ea709a 100644 --- a/hs_code_link/readme/CONTRIBUTORS.rst +++ b/hs_code_link/readme/CONTRIBUTORS.rst @@ -1 +1,2 @@ * Denis Leemann +* Guillem Casassas