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.

114 lines
4.2 KiB

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