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.

117 lines
4.3 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(
  9. comodel_name="res.city.zip",
  10. string="ZIP Location",
  11. index=True,
  12. compute="_compute_zip_id",
  13. readonly=False,
  14. store=True,
  15. domain="[('city_id', '=?', city_id), "
  16. "('city_id.country_id', '=?', country_id), "
  17. "('city_id.state_id', '=?', state_id)]",
  18. )
  19. city_id = fields.Many2one(
  20. index=True, # add index for performance
  21. compute="_compute_city_id",
  22. readonly=False,
  23. store=True,
  24. )
  25. city = fields.Char(compute="_compute_city", readonly=False, store=True)
  26. zip = fields.Char(compute="_compute_zip", readonly=False, store=True)
  27. country_id = fields.Many2one(
  28. compute="_compute_country_id", readonly=False, store=True
  29. )
  30. state_id = fields.Many2one(compute="_compute_state_id", readonly=False, store=True)
  31. @api.depends("state_id", "country_id")
  32. def _compute_zip_id(self):
  33. """Empty the zip auto-completion field if data mismatch when on UI."""
  34. for record in self.filtered("zip_id"):
  35. for field in ["state_id", "country_id"]:
  36. if (
  37. record[field]
  38. and record[field] != record._origin[field]
  39. and record[field] != record.zip_id.city_id[field]
  40. ):
  41. record.zip_id = False
  42. @api.depends("zip_id")
  43. def _compute_city_id(self):
  44. if hasattr(super(), "_compute_city_id"):
  45. super()._compute_city_id() # pragma: no cover
  46. for record in self:
  47. if record.zip_id:
  48. record.city_id = record.zip_id.city_id
  49. elif not record.country_enforce_cities:
  50. record.city_id = False
  51. @api.depends("zip_id")
  52. def _compute_city(self):
  53. if hasattr(super(), "_compute_city"):
  54. super()._compute_city() # pragma: no cover
  55. for record in self:
  56. if record.zip_id:
  57. record.city = record.zip_id.city_id.name
  58. @api.depends("zip_id")
  59. def _compute_zip(self):
  60. if hasattr(super(), "_compute_zip"):
  61. super()._compute_zip() # pragma: no cover
  62. for record in self:
  63. if record.zip_id:
  64. record.zip = record.zip_id.name
  65. @api.depends("zip_id", "state_id")
  66. def _compute_country_id(self):
  67. if hasattr(super(), "_compute_country_id"):
  68. super()._compute_country_id() # pragma: no cover
  69. for record in self:
  70. if record.zip_id.city_id.country_id:
  71. record.country_id = record.zip_id.city_id.country_id
  72. elif record.state_id:
  73. record.country_id = record.state_id.country_id
  74. @api.depends("zip_id")
  75. def _compute_state_id(self):
  76. if hasattr(super(), "_compute_state_id"):
  77. super()._compute_state_id() # pragma: no cover
  78. for record in self:
  79. state = record.zip_id.city_id.state_id
  80. if state and record.state_id != state:
  81. record.state_id = record.zip_id.city_id.state_id
  82. @api.constrains("zip_id", "country_id", "city_id", "state_id")
  83. def _check_zip(self):
  84. if self.env.context.get("skip_check_zip"):
  85. return
  86. for rec in self:
  87. if not rec.zip_id:
  88. continue
  89. if rec.zip_id.city_id.state_id != rec.state_id:
  90. raise ValidationError(
  91. _("The state of the partner %s differs from that in " "location %s")
  92. % (rec.name, rec.zip_id.name)
  93. )
  94. if rec.zip_id.city_id.country_id != rec.country_id:
  95. raise ValidationError(
  96. _(
  97. "The country of the partner %s differs from that in "
  98. "location %s"
  99. )
  100. % (rec.name, rec.zip_id.name)
  101. )
  102. if rec.zip_id.city_id != rec.city_id:
  103. raise ValidationError(
  104. _("The city of partner %s differs from that in " "location %s")
  105. % (rec.name, rec.zip_id.name)
  106. )