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.

86 lines
3.1 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. return {'domain': {'zip_id': []}}
  26. @api.onchange('country_id')
  27. def _onchange_country_id(self):
  28. res = super()._onchange_country_id()
  29. if self.zip_id and self.zip_id.city_id.country_id != self.country_id:
  30. self.zip_id = False
  31. return res
  32. @api.onchange('zip_id')
  33. def _onchange_zip_id(self):
  34. if self.zip_id:
  35. vals = {
  36. 'city_id': self.zip_id.city_id,
  37. 'zip': self.zip_id.name,
  38. 'city': self.zip_id.city_id.name,
  39. }
  40. if self.zip_id.city_id.country_id:
  41. vals.update({'country_id': self.zip_id.city_id.country_id})
  42. if self.zip_id.city_id.state_id:
  43. vals.update({'state_id': self.zip_id.city_id.state_id})
  44. self.update(vals)
  45. elif not self.country_enforce_cities:
  46. self.city_id = False
  47. @api.constrains('zip_id', 'country_id', 'city_id', 'state_id')
  48. def _check_zip(self):
  49. if self.env.context.get('skip_check_zip'):
  50. return
  51. for rec in self:
  52. if not rec.zip_id:
  53. continue
  54. if rec.zip_id.city_id.state_id != rec.state_id:
  55. raise ValidationError(_(
  56. "The state of the partner %s differs from that in "
  57. "location %s") % (rec.name, rec.zip_id.name))
  58. if rec.zip_id.city_id.country_id != rec.country_id:
  59. raise ValidationError(_(
  60. "The country of the partner %s differs from that in "
  61. "location %s") % (rec.name, rec.zip_id.name))
  62. if rec.zip_id.city_id != rec.city_id:
  63. raise ValidationError(_(
  64. "The city of partner %s differs from that in "
  65. "location %s") % (rec.name, rec.zip_id.name))
  66. @api.onchange('state_id')
  67. def _onchange_state_id(self):
  68. vals = {}
  69. if self.state_id.country_id:
  70. vals.update({'country_id': self.state_id.country_id})
  71. if self.zip_id and self.state_id != self.zip_id.city_id.state_id:
  72. vals.update({
  73. 'zip_id': False,
  74. 'zip': False,
  75. 'city': False,
  76. })
  77. self.update(vals)