You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.9 KiB

  1. # Copyright (C) 2015 Forest and Biomass Romania
  2. # Copyright (C) 2020 NextERP Romania
  3. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
  4. import logging
  5. from odoo import api, models
  6. _logger = logging.getLogger(__name__)
  7. try:
  8. from stdnum.eu.vat import check_vies
  9. except ImportError:
  10. _logger.debug("Cannot import check_vies method from python stdnum.")
  11. class ResPartner(models.Model):
  12. _inherit = "res.partner"
  13. @api.model
  14. def _get_vies_data(self, vat):
  15. res = {}
  16. vat_country, vat_number = self._split_vat(vat)
  17. result = check_vies(vat)
  18. if result.name:
  19. res["vat"] = vat
  20. # Update partner name if listed on VIES
  21. if result.name != "---":
  22. res["name"] = result.name.upper()
  23. # Update partner address if listed on VIES
  24. if result.address != "---":
  25. res["street"] = (
  26. result.address.replace("\n", " ").replace("\r", "").title()
  27. )
  28. # Get country by country code
  29. country = self.env["res.country"].search([("code", "ilike", vat_country)])
  30. if country:
  31. res["country_id"] = country[0].id
  32. return res
  33. @api.onchange("vat")
  34. def vies_vat_change(self):
  35. eu_group = self.env.ref("base.europe", raise_if_not_found=False)
  36. if eu_group:
  37. for partner in self:
  38. if not partner.vat or not partner.is_company:
  39. continue
  40. vat = partner.vat.strip().upper()
  41. vat_country, vat_number = self._split_vat(vat)
  42. vat_country = vat_country.upper()
  43. eu_countries = eu_group.country_ids.mapped("code")
  44. if vat_country and vat_country not in eu_countries:
  45. continue
  46. result = self._get_vies_data(vat)
  47. partner.update(result)