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.

96 lines
3.6 KiB

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