From 83b051ba58bb8634066102fbeff436ded9e4a849 Mon Sep 17 00:00:00 2001 From: GuillemCForgeFlow Date: Thu, 28 Sep 2023 15:50:21 +0200 Subject: [PATCH 1/2] [FIX]hs_code_link: properly declare OCA hs_code field on product.template Before the change, the field was not being updated after the value of the H.S. Code was changed on a Product Template. The related stored field was not working fine with the company dependent field. As the `hs_code_id` field is company dependent, we can have the `hs_code` on the product.template as a computed non-stored to be computed based on the `force_company` value of the context, just as in the company_dependent fields. --- hs_code_link/__manifest__.py | 1 + hs_code_link/models/product.py | 11 +++++++++-- hs_code_link/readme/CONTRIBUTORS.rst | 1 + 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/hs_code_link/__manifest__.py b/hs_code_link/__manifest__.py index 6824b08e3..c4b53a5bc 100644 --- a/hs_code_link/__manifest__.py +++ b/hs_code_link/__manifest__.py @@ -1,4 +1,5 @@ # Copyright 2017 Camptocamp SA +# Copyright 2023 ForgeFlow # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). { "name": "HS Code Link", diff --git a/hs_code_link/models/product.py b/hs_code_link/models/product.py index d00049580..52128f8f2 100644 --- a/hs_code_link/models/product.py +++ b/hs_code_link/models/product.py @@ -1,10 +1,17 @@ # 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 +from odoo import api, fields, models class ProductTemplate(models.Model): _inherit = "product.template" - hs_code = fields.Char(related="hs_code_id.hs_code", readonly=True, store=True) + # Make it compute non-stored as we will get the value from the bypassed company + 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 From e650afe3b63e6ee19f51ed993e46798339b6287f Mon Sep 17 00:00:00 2001 From: GuillemCForgeFlow Date: Tue, 3 Oct 2023 13:44:22 +0200 Subject: [PATCH 2/2] [IMP]hs_code_link: add tests to cover new changes --- hs_code_link/tests/__init__.py | 1 + hs_code_link/tests/test_hs_code_link.py | 62 +++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 hs_code_link/tests/__init__.py create mode 100644 hs_code_link/tests/test_hs_code_link.py diff --git a/hs_code_link/tests/__init__.py b/hs_code_link/tests/__init__.py new file mode 100644 index 000000000..e28dd936a --- /dev/null +++ b/hs_code_link/tests/__init__.py @@ -0,0 +1 @@ +from . import test_hs_code_link diff --git a/hs_code_link/tests/test_hs_code_link.py b/hs_code_link/tests/test_hs_code_link.py new file mode 100644 index 000000000..219677c67 --- /dev/null +++ b/hs_code_link/tests/test_hs_code_link.py @@ -0,0 +1,62 @@ +# Copyright 2023 ForgeFlow S.L. (https://www.forgeflow.com) +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). +from odoo.tests.common import SavepointCase + + +class TestHSCodeLink(SavepointCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + # Models + cls.company_model = cls.env["res.company"] + cls.hs_code_model = cls.env["hs.code"] + cls.product_template_model = cls.env["product.template"] + cls.property_model = cls.env["ir.property"] + + # Instances + cls.company_1 = cls._create_company("Company 1") + cls.company_2 = cls._create_company("Company 2") + + cls.hs_code_1 = cls._create_hs_code() + cls.hs_code_2 = cls._create_hs_code("12345678910") + + cls.product_template = cls._create_product_template("Product Template 1") + + @classmethod + def _create_company(cls, name): + return cls.company_model.create({"name": name}) + + @classmethod + def _create_hs_code(cls, local_code="123456789", description=False): + return cls.hs_code_model.create( + {"local_code": local_code, "description": description} + ) + + @classmethod + def _create_product_template(cls, name): + return cls.product_template_model.create({"name": name}) + + def test_01_check_correctly_sync_values_in_different_companies(self): + """ + Check that the values are correctly synced in different companies. + """ + # First Company + template_company_1 = self.product_template.with_context( + force_company=self.company_1.id + ) + template_company_1.hs_code_id = self.hs_code_1 + self.assertEqual( + template_company_1.hs_code, + self.hs_code_1.hs_code, + "The H.S. Codes should be the same.", + ) + # Second Company + template_company_2 = self.product_template.with_context( + force_company=self.company_2.id + ) + template_company_2.hs_code_id = self.hs_code_2 + self.assertEqual( + template_company_2.hs_code, + self.hs_code_2.hs_code, + "The H.S. Codes should be the same.", + )