2
0

[REF] base_vat_optional_vies: refactor the code to make it valid for the change eff3b140cc

This commit is contained in:
Rodrigo 2023-01-31 02:05:31 +01:00
parent 46303c4677
commit 604871bb9f
3 changed files with 64 additions and 26 deletions

View File

@ -17,22 +17,23 @@ class ResPartner(models.Model):
def simple_vat_check(self, country_code, vat_number): def simple_vat_check(self, country_code, vat_number):
res = super().simple_vat_check(country_code, vat_number) res = super().simple_vat_check(country_code, vat_number)
partner = self._context.get("vat_partner") partner = self._context.get("vat_partner")
if partner: if res is False and partner:
partner.update({"vies_passed": False}) 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 return res
@api.model @api.model
def vies_vat_check(self, country_code, vat_number): def vies_vat_check(self, country_code, vat_number):
partner = self._context.get("vat_partner") 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) res = super().vies_vat_check(country_code, vat_number)
if res is False: if res is False and partner:
if partner: partner.update({"vies_passed": False})
partner.update({"vies_passed": False})
return self.simple_vat_check(country_code, vat_number)
elif partner: elif partner:
partner.update({"vies_passed": True}) partner.update({"vies_passed": True})
return res return res
@ -40,11 +41,26 @@ class ResPartner(models.Model):
@api.constrains("vat", "country_id") @api.constrains("vat", "country_id")
def check_vat(self): def check_vat(self):
self.update({"vies_passed": False}) 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) 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 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 @api.model
def _build_vat_error_message(self, country_code, wrong_vat, record_label): def _build_vat_error_message(self, country_code, wrong_vat, record_label):
return "\n" + _( return "\n" + _(

View File

@ -11,32 +11,54 @@ from odoo.tests import common
class TestResPartner(common.TransactionCase): class TestResPartner(common.TransactionCase):
def setUp(self): @classmethod
super().setUp() def setUpClass(cls):
self.company = self.env.user.company_id def check_vies(vat_number):
self.company.vat_check_vies = True return {"valid": vat_number == "ESB87530432"}
self.partner = self.env["res.partner"].create(
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} {"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): def test_validate_vat_vies(self):
with patch(self.vatnumber_path) as mock_vatnumber: with patch(self.vatnumber_path, type(self)._vies_check_func):
mock_vatnumber.check_vies.return_value = True values = {"vat": "ESB87530432", "country_id": self.env.ref("base.be").id}
self.partner.vat = "ESB87530432" field_onchange = self.partner._onchange_spec()
self.partner.country_id = self.env.ref("base.be") 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) self.assertEqual(self.partner.vies_passed, True)
def test_exception_vat_vies(self): def test_exception_vat_vies(self):
with patch(self.vatnumber_path) as mock_vatnumber: with patch(self.vatnumber_path, side_effect=Exception()):
mock_vatnumber.check_vies.side_effect = Exception() values = {"vat": "87530432", "country_id": self.env.ref("base.es").id}
self.partner.vat = "ESB87530432" 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) self.assertEqual(self.partner.vies_passed, False)
def test_no_validate_vat(self): def test_no_validate_vat(self):
with patch(self.vatnumber_path) as mock_vatnumber: with patch(self.vatnumber_path) as mock_vatnumber:
mock_vatnumber.check_vies.return_value = False 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) self.assertEqual(self.partner.vies_passed, False)
def test_validate_simple_vat_vies(self): def test_validate_simple_vat_vies(self):

View File

@ -6,7 +6,7 @@
<field name="inherit_id" ref="base.view_partner_form" /> <field name="inherit_id" ref="base.view_partner_form" />
<field name="arch" type="xml"> <field name="arch" type="xml">
<field name="vat" position="after"> <field name="vat" position="after">
<field name="vies_passed" /> <field name="vies_passed" force_save="1" />
</field> </field>
</field> </field>
</record> </record>