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.

95 lines
3.6 KiB

  1. # Copyright 2017-2018 Therp BV <https://therp.nl>.
  2. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
  3. from odoo import _, api, fields, models
  4. from odoo.exceptions import ValidationError
  5. from ..tablib import Tab
  6. class ResPartnerTab(models.Model):
  7. """Model that defines relation types that might exist between partners"""
  8. _name = 'res.partner.tab'
  9. _description = 'Tabs to add to partner'
  10. _order = 'name'
  11. @api.model
  12. def get_partner_types(self):
  13. """Partner types are defined by model res.partner.relation.type."""
  14. # pylint: disable=no-self-use
  15. rprt_model = self.env['res.partner.relation.type']
  16. return rprt_model.get_partner_types()
  17. code = fields.Char(
  18. string='Code',
  19. required=True,
  20. help="Language independent code for tab")
  21. name = fields.Char(
  22. string='Name',
  23. required=True,
  24. translate=True,
  25. help="Will provide title for tab in user language")
  26. contact_type = fields.Selection(
  27. selection='get_partner_types',
  28. string='Valid for partner type')
  29. partner_category_id = fields.Many2one(
  30. comodel_name='res.partner.category',
  31. string='Valid for partner category')
  32. partner_ids = fields.Many2many(
  33. comodel_name='res.partner',
  34. string="Partners with this tab",
  35. help="This tab will only show for certain partners.\n"
  36. "Do not combine this with selection for contact type or"
  37. " category.")
  38. @api.constrains('contact_type', 'partner_category_id', 'partner_ids')
  39. def _check_partner_ids(self):
  40. """If partner_ids filled, other domain fields should be empty."""
  41. if self.partner_ids and \
  42. (self.contact_type or self.partner_category_id):
  43. raise ValidationError(_(
  44. "You can not both specify partner_ids and other criteria."))
  45. @api.multi
  46. def update_types(self, vals=None):
  47. """Update types on write or unlink.
  48. If we have no vals, assume unlink.
  49. """
  50. if vals:
  51. contact_type = vals.get('contact_type', False)
  52. partner_category_id = vals.get('partner_category_id', False)
  53. type_model = self.env['res.partner.relation.type']
  54. for this in self:
  55. for tab_side in ('left', 'right'):
  56. side_tab = 'tab_%s_id' % tab_side
  57. tab_using = type_model.search([(side_tab, '=', this.id)])
  58. for relation_type in tab_using:
  59. type_value = relation_type['contact_type_%s' % tab_side]
  60. category_value = \
  61. relation_type['partner_category_%s' % tab_side]
  62. if (not vals or
  63. (contact_type and contact_type != type_value) or
  64. (partner_category_id and
  65. partner_category_id != category_value.id)):
  66. relation_type.write({side_tab: False})
  67. @api.multi
  68. def write(self, vals):
  69. """Remove tab from types no longer satifying criteria."""
  70. if vals.get('contact_type', False) or \
  71. vals.get('partner_category_id', False):
  72. self.update_types(vals)
  73. result = super(ResPartnerTab, self).write(vals)
  74. return result
  75. @api.multi
  76. def unlink(self):
  77. """Unlink should first remove references."""
  78. self.update_types()
  79. return super(ResPartnerTab, self).unlink()
  80. @api.model
  81. def get_tabs(self):
  82. """Convert information on tabs in database to array of objects."""
  83. tabs = [Tab(tab_record) for tab_record in self.search([])]
  84. return tabs