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.

80 lines
3.0 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.city.zip", "ZIP Location")
  9. @api.onchange("city_id")
  10. def _onchange_city_id(self):
  11. if not self.zip_id:
  12. super()._onchange_city_id()
  13. if self.zip_id and self.city_id != self.zip_id.city_id:
  14. self.update({"zip_id": False, "zip": False, "city": False})
  15. if self.city_id and self.country_enforce_cities:
  16. return {"domain": {"zip_id": [("city_id", "=", self.city_id.id)]}}
  17. return {"domain": {"zip_id": []}}
  18. @api.onchange("country_id")
  19. def _onchange_country_id(self):
  20. res = super()._onchange_country_id()
  21. if self.zip_id and self.zip_id.city_id.country_id != self.country_id:
  22. self.zip_id = False
  23. return res
  24. @api.onchange("zip_id")
  25. def _onchange_zip_id(self):
  26. if self.zip_id:
  27. vals = {
  28. "city_id": self.zip_id.city_id,
  29. "zip": self.zip_id.name,
  30. "city": self.zip_id.city_id.name,
  31. }
  32. if self.zip_id.city_id.country_id:
  33. vals.update({"country_id": self.zip_id.city_id.country_id})
  34. if self.zip_id.city_id.state_id:
  35. vals.update({"state_id": self.zip_id.city_id.state_id})
  36. self.update(vals)
  37. elif not self.country_enforce_cities:
  38. self.city_id = False
  39. @api.constrains("zip_id", "country_id", "city_id", "state_id")
  40. def _check_zip(self):
  41. if self.env.context.get("skip_check_zip"):
  42. return
  43. for rec in self:
  44. if not rec.zip_id:
  45. continue
  46. if rec.zip_id.city_id.state_id != rec.state_id:
  47. raise ValidationError(
  48. _("The state of the partner %s differs from that in " "location %s")
  49. % (rec.name, rec.zip_id.name)
  50. )
  51. if rec.zip_id.city_id.country_id != rec.country_id:
  52. raise ValidationError(
  53. _(
  54. "The country of the partner %s differs from that in "
  55. "location %s"
  56. )
  57. % (rec.name, rec.zip_id.name)
  58. )
  59. if rec.zip_id.city_id != rec.city_id:
  60. raise ValidationError(
  61. _("The city of partner %s differs from that in " "location %s")
  62. % (rec.name, rec.zip_id.name)
  63. )
  64. @api.onchange("state_id")
  65. def _onchange_state_id(self):
  66. vals = {}
  67. if self.state_id.country_id:
  68. vals.update({"country_id": self.state_id.country_id})
  69. if self.zip_id and self.state_id != self.zip_id.city_id.state_id:
  70. vals.update({"zip_id": False, "zip": False, "city": False})
  71. self.update(vals)