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.

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