Skip to content

Commit

Permalink
[FIX] partner_data_vies_populator: handle address empty trailing line
Browse files Browse the repository at this point in the history
  • Loading branch information
dreispt committed Nov 30, 2024
1 parent a29efba commit 770f1d7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
22 changes: 15 additions & 7 deletions partner_data_vies_populator/models/res_partner.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@
class ResPartner(models.Model):
_inherit = "res.partner"


def _parse_vies_address(self, address):
res = {}
if address != "---":
address_parts = [x for x in address.split("\n") if x]
if len(address_parts) > 1 and " " in address_parts[-1]:
# Last line can be "ZipCode City"
zip_city = address_parts.pop()
res["zip"], res["city"] = zip_city.split(" ", 1)
res["street"] = " ".join(address_parts)
return res

@api.model
def _get_vies_data(self, vat, raise_if_fail=False):
res = {}
Expand All @@ -30,19 +42,15 @@ def _get_vies_data(self, vat, raise_if_fail=False):
_("Failed to query VIES.\nTechnical error: %s.") % e
) from None
return res

Check warning on line 44 in partner_data_vies_populator/models/res_partner.py

View check run for this annotation

Codecov / codecov/patch

partner_data_vies_populator/models/res_partner.py#L44

Added line #L44 was not covered by tests
_logger.debug(result)
# Update partner VAT
if result.valid and result.name:
res["vat"] = vat
# Update partner name if listed on VIES
if result.name != "---":
res["name"] = result.name.upper()
# Update partner address if listed on VIES
# Last line can be "ZipCode City"
if result.address != "---":
address_parts = result.address.split("\n")
if len(address_parts) > 1 and " " in address_parts[-1]:
zip_city = address_parts.pop()
res["zip"], res["city"] = zip_city.split(" ", 1)
res["street"] = " ".join(address_parts)
res.update(self._parse_vies_address(result.address))
# Get country by country code
country = self.env["res.country"].search(
[("code", "ilike", result.countryCode)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,27 @@ def test_create_from_vat1(self):
with Form(self.partner_model) as partner_form:
partner_form.company_type = "company"
partner_form.vat = "be0477472701"

# Check if the datas fetch correspond with the datas from VIES.
# address: 'Chaussée de Namur 40\n1367 Ramillies'
self.assertEqual(partner_form.name, self.sample_1["name"])
self.assertEqual(partner_form.street, self.sample_1["address"])
self.assertEqual(
partner_form.country_id.code, self.sample_1["country_code"]
)
self.assertEqual(partner_form.vat, "BE0477472701")

def test_create_from_vat2nl(self):
# Create an partner from VAT number field
with Form(self.partner_model) as partner_form:
partner_form.company_type = "company"
partner_form.vat = "NL001172359B01"
# Check if the datas fetch correspond with the datas from VIES.
# address: '\nRIJKSWEG 00015\n5462CE VEGHEL\n'
self.assertEqual(partner_form.name, "JUMBO SUPERMARKTEN B.V.")
self.assertEqual(partner_form.country_id.code, "NL")
self.assertEqual(partner_form.zip, "5462CE")
self.assertEqual(partner_form.vat, "NL001172359B01")

def test_company_vat_change(self):
# Change partner VAT number field
partner = self.partner_model.create({"name": "SA ODOO", "is_company": True})
Expand Down

0 comments on commit 770f1d7

Please sign in to comment.