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.

62 lines
2.0 KiB

  1. # -*- coding: utf-8 -*-
  2. # © 2015 Forest and Biomass Services Romania
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. import logging
  5. from openerp import api, models, _
  6. from openerp.exceptions import ValidationError
  7. _logger = logging.getLogger(__name__)
  8. try:
  9. from stdnum.eu.vat import check_vies
  10. except ImportError:
  11. _logger.debug("Cannot import check_vies method from python stdnum.")
  12. class ResPartner(models.Model):
  13. _inherit = "res.partner"
  14. @api.model
  15. def _get_vies_data(self, vat):
  16. res = {}
  17. vat = vat.strip().upper()
  18. vat_country, vat_number = self._split_vat(vat)
  19. result = check_vies(vat)
  20. # Raise error if partner is not listed on Vies
  21. if result.name is None:
  22. raise ValidationError(_("The partner is not listed on Vies "
  23. "Webservice."))
  24. res['vat'] = vat
  25. res['vat_subjected'] = result.valid
  26. # Update partner name if listed on VIES
  27. if result.name != '---':
  28. res['name'] = result.name.upper()
  29. # Update partner address if listed on VIES
  30. if result.address != '---':
  31. res['street'] = \
  32. result.address.replace('\n', ' ').replace('\r', '').title()
  33. # Get country by country code
  34. country = self.env['res.country'].search(
  35. [('code', 'ilike', vat_country)])
  36. if country:
  37. res['country_id'] = country[0].id
  38. return res
  39. @api.multi
  40. def vat_change(self, value):
  41. res = super(ResPartner, self).vat_change(value)
  42. # Update fields with the values available in the upper method
  43. # Skip required name error
  44. with self.env.do_in_onchange():
  45. if value:
  46. result = self._get_vies_data(value)
  47. res['value'].update(result)
  48. return res
  49. @api.one
  50. def get_vies_data_from_vat(self):
  51. if self.vat:
  52. res = self._get_vies_data(self.vat)
  53. self.update(res)