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.

120 lines
4.2 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. # Copyright 2015 Antonio Espinosa <antonio.espinosa@tecnativa.com>
  2. # Copyright 2016 Jairo Llopis <jairo.llopis@tecnativa.com>
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. from odoo import api, fields, models
  5. class ResPartner(models.Model):
  6. _inherit = "res.partner"
  7. nuts1_id = fields.Many2one(
  8. comodel_name="res.partner.nuts",
  9. domain=[("level", "=", 1)],
  10. string="NUTS L1",
  11. )
  12. nuts2_id = fields.Many2one(
  13. comodel_name="res.partner.nuts",
  14. domain=[("level", "=", 2)],
  15. string="NUTS L2",
  16. )
  17. nuts3_id = fields.Many2one(
  18. comodel_name="res.partner.nuts",
  19. domain=[("level", "=", 3)],
  20. string="NUTS L3",
  21. )
  22. nuts4_id = fields.Many2one(
  23. comodel_name="res.partner.nuts",
  24. domain=[("level", "=", 4)],
  25. string="NUTS L4",
  26. )
  27. def _onchange_nuts(self, level):
  28. field = self["nuts%d_id" % level]
  29. country_id = field.country_id
  30. state_id = field.state_id
  31. if country_id and self.country_id != country_id:
  32. self.country_id = country_id
  33. if state_id and self.state_id != state_id:
  34. self.state_id = state_id
  35. if level > 1:
  36. parent_id = field.parent_id
  37. if parent_id:
  38. nuts_parent_level = "nuts%d_id" % (level - 1)
  39. parent_field = self[nuts_parent_level]
  40. if parent_field != parent_id:
  41. self[nuts_parent_level] = parent_id
  42. result = {}
  43. if country_id and level < 4:
  44. result["domain"] = {}
  45. while level < 4:
  46. parent_field = "nuts%d_id" % level
  47. domain_field = "nuts%d_id" % (level + 1)
  48. parent_id = self[parent_field].id
  49. if parent_id:
  50. result["domain"][domain_field] = [("parent_id", "=", parent_id)]
  51. level += 1
  52. return result
  53. @api.onchange("nuts4_id")
  54. def _onchange_nuts4_id(self):
  55. return self._onchange_nuts(4)
  56. @api.onchange("nuts3_id")
  57. def _onchange_nuts3_id(self):
  58. return self._onchange_nuts(3)
  59. @api.onchange("nuts2_id")
  60. def _onchange_nuts2_id(self):
  61. return self._onchange_nuts(2)
  62. @api.onchange("nuts1_id")
  63. def _onchange_nuts1_id(self):
  64. return self._onchange_nuts(1)
  65. @api.onchange("country_id")
  66. def _onchange_country_id_base_location_nuts(self):
  67. """Sensible values and domains for related fields."""
  68. fields = ["state_id", "nuts1_id", "nuts2_id", "nuts3_id", "nuts4_id"]
  69. country_domain = (
  70. [("country_id", "=", self.country_id.id)] if self.country_id else []
  71. )
  72. domain = dict()
  73. for field in fields:
  74. if self.country_id and self[field].country_id != self.country_id:
  75. self[field] = False
  76. domain[field] = list(country_domain) # Using list() to copy
  77. fields.remove("state_id")
  78. for field in fields:
  79. level = int(field[4])
  80. domain[field].append(("level", "=", level))
  81. if self.country_id:
  82. nuts1 = self.env["res.partner.nuts"].search(
  83. [("level", "=", 1), ("country_id", "=", self.country_id.id)], limit=1
  84. )
  85. if self.nuts1_id.id != nuts1.id:
  86. self.nuts1_id = nuts1.id
  87. return {"domain": domain}
  88. @api.onchange("state_id")
  89. def onchange_state_id_base_location_nuts(self):
  90. if self.state_id:
  91. self.country_id = self.state_id.country_id
  92. if self.state_id.country_id.state_level:
  93. nuts_state = self.env["res.partner.nuts"].search(
  94. [
  95. ("level", "=", self.state_id.country_id.state_level),
  96. ("state_id", "=", self.state_id.id),
  97. ],
  98. limit=1,
  99. )
  100. if nuts_state:
  101. field = "nuts%d_id" % self.state_id.country_id.state_level
  102. self[field] = nuts_state
  103. @api.model
  104. def _address_fields(self):
  105. fields = super(ResPartner, self)._address_fields()
  106. if fields:
  107. fields += ["nuts1_id", "nuts2_id", "nuts3_id", "nuts4_id"]
  108. return fields