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.

44 lines
1.9 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2017-2018 Therp BV <https://therp.nl>.
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. from odoo import _, api, models
  5. from odoo.exceptions import ValidationError
  6. class ResPartnerRelation(models.Model):
  7. _inherit = 'res.partner.relation'
  8. @api.model
  9. def create(self, vals):
  10. """Prevent contradictory links in hierarchy.
  11. We should not do this in a constraint, as those are only checked
  12. after creation has already been done, and other modules might
  13. fail when they process in invalid hierarchy when triggered on
  14. create.
  15. TODO: Prevent this also on write.
  16. """
  17. context = self.env.context
  18. if 'left_partner_id' not in vals and context.get('active_id'):
  19. vals['left_partner_id'] = context.get('active_id')
  20. # Check if we have needed left partner, type and right
  21. # partner. If not leave error handling to super
  22. if not {'left_partner_id', 'type_id', 'right_partner_id'} <= set(vals):
  23. return super(ResPartnerRelation, self).create(vals)
  24. type_model = self.env['res.partner.relation.type']
  25. type_id = type_model.browse(vals['type_id'])
  26. hierarchy = type_id.hierarchy
  27. # If relation is not for a hierarchy, just return super
  28. if hierarchy == 'equal':
  29. return super(ResPartnerRelation, self).create(vals)
  30. partner_model = self.env['res.partner']
  31. left_partner = partner_model.browse(vals['left_partner_id'])
  32. right_partner = partner_model.browse(vals['right_partner_id'])
  33. if ((hierarchy == 'left' and right_partner.is_above(left_partner)) or
  34. (hierarchy == 'right' and
  35. left_partner.is_above(right_partner))):
  36. raise ValidationError(
  37. _("Not allowed to create an inconsistent hierarchy"))
  38. # Everything is OK, call super
  39. return super(ResPartnerRelation, self).create(vals)