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.

111 lines
4.6 KiB

  1. # -*- coding: utf-8 -*-
  2. # © 2017 Therp BV <http://therp.nl>
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. from openerp import _, api, models
  5. from odoo.exceptions import UserError
  6. class ResPartnerRelation(models.Model):
  7. _inherit = 'res.partner.relation'
  8. def relation_exists(self, left, type_rel):
  9. relation = self.search([
  10. ('left_partner_id', '=', left),
  11. ('type_id', '=', type_rel),
  12. ])
  13. return relation
  14. @api.multi
  15. def write(self, vals):
  16. part_mod = self.env['res.partner']
  17. type_relation = self.env.ref(
  18. 'partner_multi_relation_parent.parent_relation_type'
  19. ).id
  20. for this in self:
  21. if this.type_id != type_relation:
  22. continue
  23. # check that whatever relation will come out of this write does
  24. # not exist already , but check only if type_id doesn't change, if
  25. # it does we don't care about uniqueness
  26. if vals.get('type_id', this.type_id) == type_relation:
  27. relation = this.relation_exists(
  28. vals.get('left_partner_id', this.left_partner_id),
  29. type_relation,
  30. vals.get('right_partner_id', this.right_partner_id)
  31. )
  32. if relation:
  33. raise UserError(_(
  34. "The relation you are creating exists and has id %s"
  35. "there can only be one relation of type %s" % (
  36. str(relation.id), relation.type_id.name
  37. )))
  38. if 'type_id' in vals and vals['type_id'] != type_relation:
  39. this.left_partner_id.with_context(
  40. no_relation_update=True
  41. ).write({'parent_id': False})
  42. elif 'right_partner_id' in vals and 'left_partner_id' not in vals:
  43. new_parent = vals.get('right_partner_id')
  44. contact_id = this.left_partner_id
  45. contact_id.with_context(no_relation_update=True).write(
  46. {'parent_id': new_parent}
  47. )
  48. elif 'left_partner_id' in vals and 'right_partner_id' not in vals:
  49. old_contact_id = part_mod.browse(this.left_partner_id)
  50. old_contact_id.with_context(no_relation_update=True).write(
  51. {'parent_id': False}
  52. )
  53. contact_id = part_mod.browse(vals['left_partner_id'])
  54. contact_id.with_context(no_relation_update=True).write(
  55. {'parent_id': this.right_partner_id}
  56. )
  57. elif 'left_partner_id' in vals and 'right_partner_id' in vals:
  58. old_contact_id = this.left_partner_id
  59. old_contact_id.with_context(no_relation_update=True).write(
  60. {'parent_id': False}
  61. )
  62. contact_id = part_mod.browse(vals['left_partner_id'])
  63. contact_id.with_context(no_relation_update=True).write(
  64. {'parent_id': vals['right_partner_id']},
  65. )
  66. res = super(ResPartnerRelation, self).write(vals=vals)
  67. return res
  68. @api.multi
  69. def unlink(self):
  70. type_relation = self.env.ref(
  71. 'partner_multi_relation_parent.parent_relation_type'
  72. ).id
  73. for this in self:
  74. if this.type_id.id != type_relation:
  75. continue
  76. this.left_partner_id.with_context(no_relation_update=True).write(
  77. {'parent_id': False}
  78. )
  79. res = super(ResPartnerRelation, self).unlink()
  80. return res
  81. @api.model
  82. def create(self, vals):
  83. type_relation = self.env.ref(
  84. 'partner_multi_relation_parent.parent_relation_type'
  85. ).id
  86. current = self.relation_exists(
  87. vals['left_partner_id'],
  88. type_relation,
  89. )
  90. if current:
  91. # we are creating a relation but one already exists, raise an
  92. # exception to warn the user relations of type_relation must be
  93. # unique.
  94. raise UserError(_(
  95. "The relation you are creating exists and has id %s"
  96. "there can only be one relation of type %s" % (
  97. str(current.id), current.type_id.name
  98. )))
  99. # there is no relation, so we can create it, but we must update
  100. # the parent_id of the left contact of this new relation
  101. res = super(ResPartnerRelation, self).create(vals=vals)
  102. res.left_partner_id.with_context(no_relation_update=True).write(
  103. {'parent_id': vals['right_partner_id']}
  104. )
  105. return res