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.

157 lines
5.3 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2013-2017 Therp BV <http://therp.nl>
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. # pylint: disable=api-one-deprecated
  5. """Store relations (connections) between partners."""
  6. from openerp import _, api, fields, models
  7. from openerp.exceptions import ValidationError
  8. class ResPartnerRelation(models.Model):
  9. """Model res.partner.relation is used to describe all links or relations
  10. between partners in the database.
  11. This model is actually only used to store the data. The model
  12. res.partner.relation.all, based on a view that contains each record
  13. two times, once for the normal relation, once for the inverse relation,
  14. will be used to maintain the data.
  15. """
  16. _name = 'res.partner.relation'
  17. _description = 'Partner relation'
  18. left_partner_id = fields.Many2one(
  19. comodel_name='res.partner',
  20. string='Source Partner',
  21. required=True,
  22. auto_join=True,
  23. ondelete='cascade',
  24. )
  25. right_partner_id = fields.Many2one(
  26. comodel_name='res.partner',
  27. string='Destination Partner',
  28. required=True,
  29. auto_join=True,
  30. ondelete='cascade',
  31. )
  32. type_id = fields.Many2one(
  33. comodel_name='res.partner.relation.type',
  34. string='Type',
  35. required=True,
  36. auto_join=True,
  37. )
  38. date_start = fields.Date('Starting date')
  39. date_end = fields.Date('Ending date')
  40. @api.model
  41. def create(self, vals):
  42. """Override create to correct values, before being stored."""
  43. context = self.env.context
  44. if 'left_partner_id' not in vals and context.get('active_id'):
  45. vals['left_partner_id'] = context.get('active_id')
  46. return super(ResPartnerRelation, self).create(vals)
  47. @api.one
  48. @api.constrains('date_start', 'date_end')
  49. def _check_dates(self):
  50. """End date should not be before start date, if not filled
  51. :raises ValidationError: When constraint is violated
  52. """
  53. if (self.date_start and self.date_end and
  54. self.date_start > self.date_end):
  55. raise ValidationError(
  56. _('The starting date cannot be after the ending date.')
  57. )
  58. @api.one
  59. @api.constrains('left_partner_id', 'type_id')
  60. def _check_partner_left(self):
  61. """Check left partner for required company or person
  62. :raises ValidationError: When constraint is violated
  63. """
  64. self._check_partner("left")
  65. @api.one
  66. @api.constrains('right_partner_id', 'type_id')
  67. def _check_partner_right(self):
  68. """Check right partner for required company or person
  69. :raises ValidationError: When constraint is violated
  70. """
  71. self._check_partner("right")
  72. @api.one
  73. def _check_partner(self, side):
  74. """Check partner for required company or person, and for category
  75. :param str side: left or right
  76. :raises ValidationError: When constraint is violated
  77. """
  78. assert side in ['left', 'right']
  79. ptype = getattr(self.type_id, "contact_type_%s" % side)
  80. partner = getattr(self, '%s_partner_id' % side)
  81. if ((ptype == 'c' and not partner.is_company) or
  82. (ptype == 'p' and partner.is_company)):
  83. raise ValidationError(
  84. _('The %s partner is not applicable for this relation type.') %
  85. side
  86. )
  87. category = getattr(self.type_id, "partner_category_%s" % side)
  88. if category and category.id not in partner.category_id.ids:
  89. raise ValidationError(
  90. _('The %s partner does not have category %s.') %
  91. (side, category.name)
  92. )
  93. @api.one
  94. @api.constrains('left_partner_id', 'right_partner_id')
  95. def _check_not_with_self(self):
  96. """Not allowed to link partner to same partner
  97. :raises ValidationError: When constraint is violated
  98. """
  99. if self.left_partner_id == self.right_partner_id:
  100. if not (self.type_id and self.type_id.allow_self):
  101. raise ValidationError(
  102. _('Partners cannot have a relation with themselves.')
  103. )
  104. @api.one
  105. @api.constrains(
  106. 'left_partner_id',
  107. 'type_id',
  108. 'right_partner_id',
  109. 'date_start',
  110. 'date_end',
  111. )
  112. def _check_relation_uniqueness(self):
  113. """Forbid multiple active relations of the same type between the same
  114. partners
  115. :raises ValidationError: When constraint is violated
  116. """
  117. # pylint: disable=no-member
  118. # pylint: disable=no-value-for-parameter
  119. domain = [
  120. ('type_id', '=', self.type_id.id),
  121. ('id', '!=', self.id),
  122. ('left_partner_id', '=', self.left_partner_id.id),
  123. ('right_partner_id', '=', self.right_partner_id.id),
  124. ]
  125. if self.date_start:
  126. domain += [
  127. '|',
  128. ('date_end', '=', False),
  129. ('date_end', '>=', self.date_start),
  130. ]
  131. if self.date_end:
  132. domain += [
  133. '|',
  134. ('date_start', '=', False),
  135. ('date_start', '<=', self.date_end),
  136. ]
  137. if self.search(domain):
  138. raise ValidationError(
  139. _('There is already a similar relation with overlapping dates')
  140. )