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.

73 lines
2.3 KiB

  1. # -*- coding: utf-8 -*-
  2. ##############################################################################
  3. # For copyright and license notices, see __openerp__.py file in root directory
  4. ##############################################################################
  5. from openerp import models, fields, api
  6. from openerp.tools.translate import _
  7. class ResPartner(models.Model):
  8. _inherit = 'res.partner'
  9. region_id = fields.Many2one(
  10. 'res.partner.nuts',
  11. "Region",
  12. oldname="region")
  13. substate_id = fields.Many2one(
  14. 'res.partner.nuts',
  15. "Substate",
  16. oldname="substate")
  17. lbl_region = fields.Char(
  18. default=_("Region"),
  19. compute='_labels_get')
  20. lbl_substate = fields.Char(
  21. default=_("Substate"),
  22. compute='_labels_get')
  23. @api.multi
  24. @api.depends('country_id')
  25. def _labels_get(self):
  26. for s in self:
  27. s.lbl_region = s.country_id.region_label or _('Region')
  28. s.lbl_substate = s.country_id.substate_label or _('Substate')
  29. @api.multi
  30. @api.onchange("substate_id")
  31. def _onchange_substate_id(self):
  32. if self.substate_id.country_id:
  33. self.country_id = self.substate_id.country_id
  34. return dict()
  35. @api.multi
  36. @api.onchange("region_id")
  37. def _onchange_region_id(self):
  38. if self.region_id.country_id:
  39. self.country_id = self.region_id.country_id
  40. return dict()
  41. @api.multi
  42. @api.onchange("country_id")
  43. def _onchange_country_id(self):
  44. """Sensible values and domains for related fields."""
  45. fields = {"state", "region", "substate"}
  46. country_domain = ([("country_id", "=", self.country_id.id)]
  47. if self.country_id else [])
  48. domain = dict()
  49. for field in fields:
  50. field += "_id"
  51. if self.country_id and self[field].country_id != self.country_id:
  52. self[field] = False
  53. domain[field] = list(country_domain) # Using list() to copy
  54. fields.remove("state")
  55. for field in fields:
  56. level = self.country_id["%s_level" % field]
  57. field += "_id"
  58. if level:
  59. domain[field].append(("level", "=", level))
  60. return {
  61. "domain": domain,
  62. }