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.

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