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.

102 lines
3.7 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', index=True)
  9. city_id = fields.Many2one(index=True) # add index for performance
  10. @api.onchange('city_id')
  11. def _onchange_city_id(self):
  12. if not self.zip_id:
  13. super()._onchange_city_id()
  14. if self.zip_id and self.city_id != self.zip_id.city_id:
  15. self.update({
  16. 'zip_id': False,
  17. 'zip': False,
  18. 'city': False,
  19. })
  20. if self.city_id and self.country_enforce_cities:
  21. return {
  22. 'domain': {
  23. 'zip_id': [('city_id', '=', self.city_id.id)]
  24. },
  25. }
  26. if self.country_id:
  27. return {
  28. 'domain': {
  29. 'zip_id': [('city_id.country_id', '=', self.country_id.id)]
  30. }
  31. }
  32. return {'domain': {'zip_id': []}}
  33. @api.onchange('country_id')
  34. def _onchange_country_id(self):
  35. res = super()._onchange_country_id()
  36. if self.zip_id and self.zip_id.city_id.country_id != self.country_id:
  37. self.zip_id = False
  38. if self.country_id:
  39. city_zip_domain = {
  40. "zip_id": [("city_id.country_id", "=", self.country_id.id)]
  41. }
  42. if isinstance(res, dict):
  43. res.setdefault("domain", {})
  44. res["domain"].update(city_zip_domain)
  45. else:
  46. res = {"domain": city_zip_domain}
  47. return res
  48. @api.onchange('zip_id')
  49. def _onchange_zip_id(self):
  50. if self.zip_id:
  51. vals = {
  52. 'city_id': self.zip_id.city_id,
  53. 'zip': self.zip_id.name,
  54. 'city': self.zip_id.city_id.name,
  55. }
  56. if self.zip_id.city_id.country_id:
  57. vals.update({'country_id': self.zip_id.city_id.country_id})
  58. if self.zip_id.city_id.state_id:
  59. vals.update({'state_id': self.zip_id.city_id.state_id})
  60. self.update(vals)
  61. elif not self.country_enforce_cities:
  62. self.city_id = False
  63. @api.constrains('zip_id', 'country_id', 'city_id', 'state_id')
  64. def _check_zip(self):
  65. if self.env.context.get('skip_check_zip'):
  66. return
  67. for rec in self:
  68. if not rec.zip_id:
  69. continue
  70. if rec.zip_id.city_id.state_id != rec.state_id:
  71. raise ValidationError(_(
  72. "The state of the partner %s differs from that in "
  73. "location %s") % (rec.name, rec.zip_id.name))
  74. if rec.zip_id.city_id.country_id != rec.country_id:
  75. raise ValidationError(_(
  76. "The country of the partner %s differs from that in "
  77. "location %s") % (rec.name, rec.zip_id.name))
  78. if rec.city_id and rec.zip_id.city_id != rec.city_id:
  79. raise ValidationError(_(
  80. "The city of partner %s differs from that in "
  81. "location %s") % (rec.name, rec.zip_id.name))
  82. @api.onchange('state_id')
  83. def _onchange_state_id(self):
  84. vals = {}
  85. if self.state_id.country_id:
  86. vals.update({'country_id': self.state_id.country_id})
  87. if self.zip_id and self.state_id != self.zip_id.city_id.state_id:
  88. vals.update({
  89. 'zip_id': False,
  90. 'zip': False,
  91. 'city': False,
  92. })
  93. self.update(vals)