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.

121 lines
5.1 KiB

7 years ago
  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 this.left_partner_id.type != 'contact':
  27. raise UserError(_(
  28. "Only contact types are allowed in relation of type %s"
  29. "you are trying to make a ' %s ' relation with a partner"
  30. "of type %s" % (
  31. relation.type_id.name,
  32. relation.type_id.name,
  33. this.left_partner_id.type
  34. )))
  35. if vals.get('type_id', this.type_id) == type_relation:
  36. relation = this.relation_exists(
  37. vals.get('left_partner_id', this.left_partner_id),
  38. type_relation,
  39. vals.get('right_partner_id', this.right_partner_id)
  40. )
  41. if relation:
  42. raise UserError(_(
  43. "The relation you are creating exists and has id %s"
  44. "there can only be one relation of type %s" % (
  45. str(relation.id), relation.type_id.name
  46. )))
  47. if 'type_id' in vals and vals['type_id'] != type_relation:
  48. this.left_partner_id.with_context(
  49. no_relation_update=True
  50. ).write({'parent_id': False})
  51. elif 'right_partner_id' in vals and 'left_partner_id' not in vals:
  52. new_parent = vals.get('right_partner_id')
  53. contact_id = this.left_partner_id
  54. contact_id.with_context(no_relation_update=True).write(
  55. {'parent_id': new_parent}
  56. )
  57. elif 'left_partner_id' in vals and 'right_partner_id' not in vals:
  58. old_contact_id = part_mod.browse(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': this.right_partner_id}
  65. )
  66. elif 'left_partner_id' in vals and 'right_partner_id' in vals:
  67. old_contact_id = this.left_partner_id
  68. old_contact_id.with_context(no_relation_update=True).write(
  69. {'parent_id': False}
  70. )
  71. contact_id = part_mod.browse(vals['left_partner_id'])
  72. contact_id.with_context(no_relation_update=True).write(
  73. {'parent_id': vals['right_partner_id']},
  74. )
  75. res = super(ResPartnerRelation, self).write(vals=vals)
  76. return res
  77. @api.multi
  78. def unlink(self):
  79. type_relation = self.env.ref(
  80. 'partner_multi_relation_parent.parent_relation_type'
  81. ).id
  82. for this in self:
  83. if this.type_id.id != type_relation:
  84. continue
  85. this.left_partner_id.with_context(no_relation_update=True).write(
  86. {'parent_id': False}
  87. )
  88. res = super(ResPartnerRelation, self).unlink()
  89. return res
  90. @api.model
  91. def create(self, vals):
  92. type_relation = self.env.ref(
  93. 'partner_multi_relation_parent.parent_relation_type'
  94. ).id
  95. current = self.relation_exists(
  96. vals['left_partner_id'],
  97. type_relation,
  98. )
  99. if current:
  100. # we are creating a relation but one already exists, raise an
  101. # exception to warn the user relations of type_relation must be
  102. # unique.
  103. raise UserError(_(
  104. "The relation you are creating exists and has id %s"
  105. "there can only be one relation of type %s" % (
  106. str(current.id), current.type_id.name
  107. )))
  108. # there is no relation, so we can create it, but we must update
  109. # the parent_id of the left contact of this new relation
  110. res = super(ResPartnerRelation, self).create(vals=vals)
  111. if res.type_id.id == type_relation:
  112. res.left_partner_id.with_context(no_relation_update=True).write(
  113. {'parent_id': vals['right_partner_id']}
  114. )
  115. return res