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.

60 lines
2.0 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. class ResPartner(models.Model):
  6. _inherit = 'res.partner'
  7. # called without parameters from the init hook
  8. def find_current_relation(self, left, type_relation, right):
  9. par_rel_mod = self.env['res.partner.relation']
  10. return par_rel_mod.search([
  11. ('left_partner_id', '=', left),
  12. ('type_id', '=', type_relation),
  13. ('right_partner_id', '=', right)
  14. ])
  15. def update_relations(self, old_parent_id=None, parent_id=None):
  16. par_rel_mod = self.env['res.partner.relation']
  17. type_relation = self.env.ref(
  18. 'partner_multi_relation_parent.parent_relation_type'
  19. ).id
  20. for this in self:
  21. if not parent_id:
  22. parent_id = this.parent_id.id
  23. if not old_parent_id:
  24. old_parent_id = this.parent_id.id
  25. # unlink previous relation
  26. if old_parent_id:
  27. previous = self.find_current_relation(
  28. this.id, type_relation, old_parent_id
  29. )
  30. previous.unlink()
  31. # create new relations
  32. par_rel_mod.create(
  33. {'left_partner_id' : this.id,
  34. 'type_id': type_relation,
  35. 'right_partner_id': parent_id,
  36. }
  37. )
  38. @api.model
  39. def create(self, vals):
  40. res = super(ResPartner, self).create(vals=vals)
  41. if "parent_id" in vals:
  42. res.update_relations(None, vals['parent_id'])
  43. return res
  44. @api.multi
  45. def write(self, vals):
  46. if self.env.context.get('relation_create'):
  47. for this in self:
  48. if "parent_id" in vals and vals(['parent_id']):
  49. this.update_relations(self.parent_id.id, vals['parent_id'])
  50. res = super(ResPartner, self).write(vals=vals)
  51. return res