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.

114 lines
4.9 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2017 Therp BV <https://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 ValidationError
  6. class ResPartnerRelation(models.Model):
  7. _inherit = 'res.partner.relation'
  8. @api.multi
  9. def write(self, vals):
  10. """Synchronize parent_id in left partner with connection.
  11. - If changed to non contact type, clear parent_id in partner;
  12. - If changed to contact type, set parent_id and contact type
  13. in partner.
  14. """
  15. type_model = self.env['res.partner.relation.type']
  16. partner_model = self.env['res.partner']
  17. vals_type = 'type_id' in vals and \
  18. type_model.browse(vals['type_id']) or False
  19. vals_left_partner = 'left_partner_id' in vals and \
  20. partner_model.browse(vals['left_partner_id']) or False
  21. vals_right_partner = 'right_partner_id' in vals and \
  22. partner_model.browse(vals['right_partner_id']) or False
  23. for this in self:
  24. # Determine old and new type
  25. old_type = this.type_id
  26. new_type = vals_type or old_type
  27. # First handle simple case: no address type involved
  28. if not old_type.partner_type and not new_type.partner_type:
  29. super(ResPartnerRelation, this).write(vals)
  30. continue
  31. # Store existing values
  32. existing_left_partner = this.left_partner_id
  33. existing_right_partner = this.right_partner_id
  34. left_partner = vals_left_partner or existing_left_partner
  35. right_partner = vals_right_partner or existing_right_partner
  36. # Second relatively simple case is where non address
  37. # connection is replaced by address connection
  38. if not old_type.partner_type:
  39. # Unlink existing connection
  40. super(ResPartnerRelation, this).unlink()
  41. # Create new connection
  42. left_partner.write({
  43. 'type': new_type.partner_type,
  44. 'parent_id': right_partner.id,
  45. })
  46. continue
  47. # Third handle case where address connection is changed into
  48. # regular connection:
  49. if not new_type.partner_type:
  50. # Clear existing parent:
  51. existing_left_partner.write({
  52. 'parent_id': False,
  53. })
  54. # Now create new connection:
  55. vals['left_partner_id'] = left_partner.id
  56. vals['type_id'] = new_type.id
  57. vals['right_partner_id'] = right_partner.id
  58. super(ResPartnerRelation, this).create(vals)
  59. continue
  60. # If we get here, both old and new connection are for address:
  61. # Check wether new type is already allowed:
  62. if not new_type.partner_synchronization_active:
  63. raise ValidationError(
  64. _("Creating a relation for address with type %s is"
  65. " not allowed at this time.") %
  66. (new_type.partner_type, ))
  67. # If left partner changed, clear parent on left partner:
  68. if left_partner != existing_left_partner:
  69. existing_left_partner.write({
  70. 'parent_id': False,
  71. })
  72. left_partner.write({
  73. 'type': new_type.partner_type,
  74. 'parent_id': right_partner.id,
  75. })
  76. return True
  77. @api.multi
  78. def unlink(self):
  79. """Unlinking relations for address, means clearing parent_id."""
  80. for this in self:
  81. if this.type_id.partner_type:
  82. this.left_partner_id.write({
  83. 'parent_id': False,
  84. })
  85. continue
  86. super(ResPartnerRelation, this).unlink()
  87. return True
  88. @api.model
  89. def create(self, vals):
  90. """Creating a relation for an address means updating parent."""
  91. type_model = self.env['res.partner.relation.type']
  92. partner_model = self.env['res.partner']
  93. relation_type = type_model.browse(vals['type_id'])
  94. if relation_type.partner_type:
  95. if not relation_type.partner_synchronization_active:
  96. raise ValidationError(
  97. _("Creating a relation for address with type %s is"
  98. " not allowed at this time.") %
  99. (relation_type.partner_type, ))
  100. left_partner = partner_model.browse(vals['left_partner_id'])
  101. left_partner.write({
  102. 'type': relation_type.partner_type,
  103. 'parent_id': vals['right_partner_id'],
  104. })
  105. # Return the left partner.
  106. # Create in res_partner_relation_all will know what to do.
  107. return left_partner
  108. return super(ResPartnerRelation, self).create(vals)