Skip to content

Commit

Permalink
[REF] base_vat_optional_vies: refactor the code to make it valid for …
Browse files Browse the repository at this point in the history
…the change odoo/odoo@eff3b14
  • Loading branch information
RodrigoBM committed Jan 31, 2023
1 parent 5dec75f commit 215d21e
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 26 deletions.
38 changes: 27 additions & 11 deletions base_vat_optional_vies/models/res_partner.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,50 @@ 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

@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" + _(
Expand Down
50 changes: 36 additions & 14 deletions base_vat_optional_vies/tests/test_res_partner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion base_vat_optional_vies/views/res_partner_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<field name="inherit_id" ref="base.view_partner_form" />
<field name="arch" type="xml">
<field name="vat" position="after">
<field name="vies_passed" />
<field name="vies_passed" force_save="1"/>
</field>
</field>
</record>
Expand Down

0 comments on commit 215d21e

Please sign in to comment.