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.

125 lines
4.4 KiB

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