diff --git a/base_vat_optional_vies/models/res_partner.py b/base_vat_optional_vies/models/res_partner.py index 2124c5daae0c..a629c1994538 100644 --- a/base_vat_optional_vies/models/res_partner.py +++ b/base_vat_optional_vies/models/res_partner.py @@ -17,22 +17,23 @@ class ResPartner(models.Model): def simple_vat_check(self, country_code, vat_number): res = super().simple_vat_check(country_code, vat_number) partner = self._context.get("vat_partner") - if partner: + if res is False and partner: partner.update({"vies_passed": False}) + elif partner: + if self.env.context.get("company_id"): + company = self.env["res.company"].browse(self.env.context["company_id"]) + else: + company = self.env.company + if company.vat_check_vies: + self.vies_vat_check(country_code, vat_number) return res @api.model def vies_vat_check(self, country_code, vat_number): partner = self._context.get("vat_partner") - if partner: - # If there's an exception checking VIES, the upstream method will - # call simple_vat_check and thus the flag will be removed - partner.update({"vies_passed": True}) res = super().vies_vat_check(country_code, vat_number) - if res is False: - if partner: - partner.update({"vies_passed": False}) - return self.simple_vat_check(country_code, vat_number) + if res is False and partner: + partner.update({"vies_passed": False}) elif partner: partner.update({"vies_passed": True}) return res @@ -40,11 +41,26 @@ def vies_vat_check(self, country_code, vat_number): @api.constrains("vat", "country_id") def check_vat(self): self.update({"vies_passed": False}) - for partner in self: + for partner in self.sorted(lambda p: bool(p.commercial_partner_id)): partner = partner.with_context(vat_partner=partner) - super(ResPartner, partner).check_vat() + if ( + partner.commercial_partner_id + and partner.commercial_partner_id != partner + ): + partner.update( + {"vies_passed": partner.commercial_partner_id.vies_passed} + ) + else: + super(ResPartner, partner).check_vat() return True + @api.onchange("vat", "country_id") + def _onchange_check_vies(self): + self.update({"vies_passed": False}) + return super( + ResPartner, self.with_context(vat_partner=self) + )._onchange_check_vies() + @api.model def _build_vat_error_message(self, country_code, wrong_vat, record_label): return "\n" + _( diff --git a/base_vat_optional_vies/tests/test_res_partner.py b/base_vat_optional_vies/tests/test_res_partner.py index fce0979a22b7..7705256866ab 100644 --- a/base_vat_optional_vies/tests/test_res_partner.py +++ b/base_vat_optional_vies/tests/test_res_partner.py @@ -11,32 +11,54 @@ class TestResPartner(common.TransactionCase): - def setUp(self): - super().setUp() - self.company = self.env.user.company_id - self.company.vat_check_vies = True - self.partner = self.env["res.partner"].create( + @classmethod + def setUpClass(cls): + def check_vies(vat_number): + return {"valid": vat_number == "ESB87530432"} + + super().setUpClass() + cls.company = cls.env.user.company_id + cls.company.vat_check_vies = True + cls.partner = cls.env["res.partner"].create( {"name": "Test partner", "is_company": True} ) - self.vatnumber_path = "odoo.addons.base_vat.models.res_partner.check_vies" + cls.vatnumber_path = "odoo.addons.base_vat.models.res_partner.check_vies" + + cls._vies_check_func = check_vies def test_validate_vat_vies(self): - with patch(self.vatnumber_path) as mock_vatnumber: - mock_vatnumber.check_vies.return_value = True - self.partner.vat = "ESB87530432" - self.partner.country_id = self.env.ref("base.be") + with patch(self.vatnumber_path, type(self)._vies_check_func): + values = {"vat": "ESB87530432", "country_id": self.env.ref("base.be").id} + field_onchange = self.partner._onchange_spec() + result = self.partner.onchange( + values=values, + field_name=["vat", "country_id"], + field_onchange=field_onchange, + ) + self.assertEqual(result.get("value", {}).get("vies_passed"), True) + self.assertEqual(self.partner.vies_passed, False) + self.partner.write(values) self.assertEqual(self.partner.vies_passed, True) def test_exception_vat_vies(self): - with patch(self.vatnumber_path) as mock_vatnumber: - mock_vatnumber.check_vies.side_effect = Exception() - self.partner.vat = "ESB87530432" + with patch(self.vatnumber_path, side_effect=Exception()): + values = {"vat": "87530432", "country_id": self.env.ref("base.es").id} + field_onchange = self.partner._onchange_spec() + result = self.partner.onchange( + values=values, + field_name=["vat", "country_id"], + field_onchange=field_onchange, + ) + self.assertEqual(result.get("value", {}).get("vies_passed"), True) + with self.assertRaises(ValidationError): + self.partner.write(values) self.assertEqual(self.partner.vies_passed, False) def test_no_validate_vat(self): with patch(self.vatnumber_path) as mock_vatnumber: mock_vatnumber.check_vies.return_value = False - self.partner.vat = "ESB87530432" + with self.assertRaises(ValidationError): + self.partner.vat = "ESB11" self.assertEqual(self.partner.vies_passed, False) def test_validate_simple_vat_vies(self): diff --git a/base_vat_optional_vies/views/res_partner_view.xml b/base_vat_optional_vies/views/res_partner_view.xml index 8edae786e36d..c15b89410e6f 100644 --- a/base_vat_optional_vies/views/res_partner_view.xml +++ b/base_vat_optional_vies/views/res_partner_view.xml @@ -6,7 +6,7 @@ - +