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.

72 lines
2.6 KiB

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