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.

64 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 odoo import api, fields, models, _
  6. from odoo.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. vat_subjected = fields.Boolean('VAT Legal Statement')
  15. @api.model
  16. def _get_vies_data(self):
  17. res = {}
  18. vat = self.vat.strip().upper()
  19. vat_country, vat_number = self._split_vat(vat)
  20. result = check_vies(vat)
  21. # Raise error if partner is not listed on Vies
  22. if result.name is None:
  23. raise ValidationError(_("The partner is not listed on Vies "
  24. "Webservice."))
  25. res['vat'] = vat
  26. res['vat_subjected'] = result.valid
  27. # Update partner name if listed on VIES
  28. if result.name != '---':
  29. res['name'] = result.name.upper()
  30. # Update partner address if listed on VIES
  31. if result.address != '---':
  32. res['street'] = \
  33. result.address.replace('\n', ' ').replace('\r', '').title()
  34. # Get country by country code
  35. country = self.env['res.country'].search(
  36. [('code', 'ilike', vat_country)])
  37. if country:
  38. res['country_id'] = country[0].id
  39. return res
  40. @api.onchange('vat')
  41. def vat_change(self):
  42. res = {'value': {}}
  43. # Update fields with the values available in the upper method
  44. # Skip required name error
  45. with self.env.do_in_onchange():
  46. if self.vat:
  47. result = self._get_vies_data()
  48. res['value'].update(result)
  49. return res
  50. @api.one
  51. def get_vies_data_from_vat(self):
  52. if self.vat:
  53. res = self._get_vies_data()
  54. self.update(res)