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.

66 lines
2.4 KiB

  1. # Copyright 2016 Nicolas Bessi, Camptocamp SA
  2. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  3. from odoo import api, fields, models, _
  4. from odoo.exceptions import ValidationError
  5. class ResPartner(models.Model):
  6. _inherit = 'res.partner'
  7. zip_id = fields.Many2one('res.better.zip', 'ZIP Location')
  8. @api.onchange('city_id')
  9. def _onchange_city_id(self):
  10. if not self.zip_id:
  11. super(ResPartner, self)._onchange_city_id()
  12. if self.zip_id and self.city_id != self.zip_id.city_id:
  13. self.zip_id = False
  14. self.zip = False
  15. self.city = False
  16. if self.city_id:
  17. return {
  18. 'domain': {
  19. 'zip_id': [('city_id', '=', self.city_id.id)]
  20. },
  21. }
  22. return {'domain': {'zip_id': []}}
  23. @api.onchange('state_id')
  24. def _onchange_state_id(self):
  25. if self.zip_id and self.state_id != self.zip_id.state_id:
  26. self.zip_id = False
  27. self.zip = False
  28. self.city = False
  29. @api.onchange('country_id')
  30. def _onchange_country_id(self):
  31. res = super(ResPartner, self)._onchange_country_id()
  32. if self.zip_id and self.zip_id.country_id != self.country_id:
  33. self.zip_id = False
  34. return res
  35. @api.onchange('zip_id')
  36. def _onchange_zip_id(self):
  37. if self.zip_id:
  38. self.country_id = self.zip_id.country_id
  39. if self.country_id.enforce_cities:
  40. self.city_id = self.zip_id.city_id
  41. self.zip = self.zip_id.name
  42. self.state_id = self.zip_id.state_id
  43. self.city = self.zip_id.city
  44. @api.constrains('zip_id', 'country_id', 'city_id', 'state_id')
  45. def _check_zip(self):
  46. for rec in self.filtered('zip_id'):
  47. if rec.zip_id.state_id != rec.state_id:
  48. raise ValidationError(_(
  49. "The state of the partner %s differs from that in "
  50. "location %s") % (rec.name, rec.zip_id.name))
  51. if rec.zip_id.country_id != rec.country_id:
  52. raise ValidationError(_(
  53. "The country of the partner %s differs from that in "
  54. "location %s") % (rec.name, rec.zip_id.name))
  55. if rec.zip_id.city_id != rec.city_id:
  56. raise ValidationError(_(
  57. "The city of partner %s differs from that in "
  58. "location %s") % (rec.name, rec.zip_id.name))