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.

113 lines
4.2 KiB

  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', domain=[('level', '=', 1)],
  9. string="NUTS L1")
  10. nuts2_id = fields.Many2one(
  11. comodel_name='res.partner.nuts', domain=[('level', '=', 2)],
  12. string="NUTS L2", oldname="region")
  13. nuts3_id = fields.Many2one(
  14. comodel_name='res.partner.nuts', domain=[('level', '=', 3)],
  15. string="NUTS L3", oldname="substate")
  16. nuts4_id = fields.Many2one(
  17. comodel_name='res.partner.nuts', domain=[('level', '=', 4)],
  18. string="NUTS L4")
  19. def _onchange_nuts(self, level):
  20. field = self["nuts%d_id" % level]
  21. country_id = field.country_id
  22. state_id = field.state_id
  23. if country_id and self.country_id != country_id:
  24. self.country_id = country_id
  25. if state_id and self.state_id != state_id:
  26. self.state_id = state_id
  27. if level > 1:
  28. parent_id = field.parent_id
  29. if parent_id:
  30. nuts_parent_level = 'nuts%d_id' % (level - 1)
  31. parent_field = self[nuts_parent_level]
  32. if parent_field != parent_id:
  33. self[nuts_parent_level] = parent_id
  34. result = {}
  35. if country_id and level < 4:
  36. result['domain'] = {}
  37. while level < 4:
  38. parent_field = 'nuts%d_id' % level
  39. domain_field = 'nuts%d_id' % (level + 1)
  40. parent_id = self[parent_field].id
  41. if parent_id:
  42. result['domain'][domain_field] = [
  43. ('parent_id', '=', parent_id),
  44. ]
  45. level += 1
  46. return result
  47. @api.onchange('nuts4_id')
  48. def _onchange_nuts4_id(self):
  49. return self._onchange_nuts(4)
  50. @api.onchange('nuts3_id')
  51. def _onchange_nuts3_id(self):
  52. return self._onchange_nuts(3)
  53. @api.onchange('nuts2_id')
  54. def _onchange_nuts2_id(self):
  55. return self._onchange_nuts(2)
  56. @api.onchange('nuts1_id')
  57. def _onchange_nuts1_id(self):
  58. return self._onchange_nuts(1)
  59. @api.onchange('country_id')
  60. def _onchange_country_id(self):
  61. """Sensible values and domains for related fields."""
  62. fields = ['state_id', 'nuts1_id', 'nuts2_id', 'nuts3_id', 'nuts4_id']
  63. country_domain = ([('country_id', '=', self.country_id.id)]
  64. if self.country_id else [])
  65. domain = dict()
  66. for field in fields:
  67. if self.country_id and self[field].country_id != self.country_id:
  68. self[field] = False
  69. domain[field] = list(country_domain) # Using list() to copy
  70. fields.remove('state_id')
  71. for field in fields:
  72. level = int(field[4])
  73. domain[field].append(('level', '=', level))
  74. if self.country_id:
  75. nuts1 = self.env['res.partner.nuts'].search([
  76. ('level', '=', 1),
  77. ('country_id', '=', self.country_id.id),
  78. ], limit=1)
  79. if self.nuts1_id.id != nuts1.id:
  80. self.nuts1_id = nuts1.id
  81. return {
  82. 'domain': domain,
  83. }
  84. @api.onchange('state_id')
  85. def onchange_state_id_base_location_nuts(self):
  86. if self.state_id:
  87. self.country_id = self.state_id.country_id
  88. if self.state_id.country_id.state_level:
  89. nuts_state = self.env['res.partner.nuts'].search([
  90. ('level', '=', self.state_id.country_id.state_level),
  91. ('state_id', '=', self.state_id.id)
  92. ], limit=1)
  93. if nuts_state:
  94. field = 'nuts%d_id' % self.state_id.country_id.state_level
  95. self[field] = nuts_state
  96. @api.model
  97. def _address_fields(self):
  98. fields = super(ResPartner, self)._address_fields()
  99. if fields:
  100. fields += ['nuts1_id', 'nuts2_id', 'nuts3_id', 'nuts4_id']
  101. return fields