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.

162 lines
6.7 KiB

  1. # -*- coding: utf-8 -*-
  2. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  3. from odoo import api, fields, models, _
  4. from odoo.osv import expression
  5. class ResPartner(models.Model):
  6. _inherit = 'res.partner'
  7. contact_type = fields.Selection(
  8. [('standalone', _('Standalone Contact')),
  9. ('attached', _('Attached to existing Contact')),
  10. ],
  11. compute='_compute_contact_type',
  12. store=True,
  13. index=True,
  14. default='standalone')
  15. contact_id = fields.Many2one('res.partner', string='Main Contact',
  16. domain=[('is_company', '=', False),
  17. ('contact_type', '=', 'standalone'),
  18. ],
  19. )
  20. other_contact_ids = fields.One2many('res.partner', 'contact_id',
  21. string='Others Positions')
  22. @api.multi
  23. @api.depends('contact_id')
  24. def _compute_contact_type(self):
  25. for rec in self:
  26. rec.contact_type = 'attached' if rec.contact_id else 'standalone'
  27. def _basecontact_check_context(self, mode):
  28. """ Remove 'search_show_all_positions' for non-search mode.
  29. Keeping it in context can result in unexpected behaviour (ex: reading
  30. one2many might return wrong result - i.e with "attached contact"
  31. removed even if it's directly linked to a company).
  32. Actually, is easier to override a dictionary value to indicate it
  33. should be ignored...
  34. """
  35. if (mode != 'search' and
  36. 'search_show_all_positions' in self.env.context):
  37. result = self.with_context(
  38. search_show_all_positions={'is_set': False})
  39. else:
  40. result = self
  41. return result
  42. @api.model
  43. def search(self, args, offset=0, limit=None, order=None, count=False):
  44. """ Display only standalone contact matching ``args`` or having
  45. attached contact matching ``args`` """
  46. ctx = self.env.context
  47. if (ctx.get('search_show_all_positions', {}).get('is_set') and
  48. not ctx['search_show_all_positions']['set_value']):
  49. args = expression.normalize_domain(args)
  50. attached_contact_args = expression.AND(
  51. (args, [('contact_type', '=', 'attached')])
  52. )
  53. attached_contacts = super(ResPartner, self).search(
  54. attached_contact_args)
  55. args = expression.OR((
  56. expression.AND(([('contact_type', '=', 'standalone')], args)),
  57. [('other_contact_ids', 'in', attached_contacts.ids)],
  58. ))
  59. return super(ResPartner, self).search(args, offset=offset,
  60. limit=limit, order=order,
  61. count=count)
  62. @api.model
  63. def create(self, vals):
  64. """ When creating, use a modified self to alter the context (see
  65. comment in _basecontact_check_context). Also, we need to ensure
  66. that the name on an attached contact is the same as the name on the
  67. contact it is attached to."""
  68. modified_self = self._basecontact_check_context('create')
  69. if not vals.get('name') and vals.get('contact_id'):
  70. vals['name'] = modified_self.browse(vals['contact_id']).name
  71. return super(ResPartner, modified_self).create(vals)
  72. @api.multi
  73. def read(self, fields=None, load='_classic_read'):
  74. modified_self = self._basecontact_check_context('read')
  75. return super(ResPartner, modified_self).read(fields=fields, load=load)
  76. @api.multi
  77. def write(self, vals):
  78. modified_self = self._basecontact_check_context('write')
  79. return super(ResPartner, modified_self).write(vals)
  80. @api.multi
  81. def unlink(self):
  82. modified_self = self._basecontact_check_context('unlink')
  83. return super(ResPartner, modified_self).unlink()
  84. @api.multi
  85. def _commercial_partner_compute(self, name, args):
  86. """ Returns the partner that is considered the commercial
  87. entity of this partner. The commercial entity holds the master data
  88. for all commercial fields (see :py:meth:`~_commercial_fields`) """
  89. result = super(ResPartner, self)._commercial_partner_compute(name,
  90. args)
  91. for partner in self:
  92. if partner.contact_type == 'attached' and not partner.parent_id:
  93. result[partner.id] = partner.contact_id.id
  94. return result
  95. def _contact_fields(self):
  96. """ Returns the list of contact fields that are synced from the parent
  97. when a partner is attached to him. """
  98. return ['name', 'title']
  99. def _contact_sync_from_parent(self):
  100. """ Handle sync of contact fields when a new parent contact entity
  101. is set, as if they were related fields
  102. """
  103. self.ensure_one()
  104. if self.contact_id:
  105. contact_fields = self._contact_fields()
  106. sync_vals = self.contact_id._update_fields_values(contact_fields)
  107. self.write(sync_vals)
  108. def update_contact(self, vals):
  109. if self.env.context.get('__update_contact_lock'):
  110. return
  111. contact_fields = self._contact_fields()
  112. contact_vals = dict(
  113. (field, vals[field]) for field in contact_fields if field in vals
  114. )
  115. if contact_vals:
  116. self.with_context(__update_contact_lock=True).write(contact_vals)
  117. @api.multi
  118. def _fields_sync(self, update_values):
  119. """Sync commercial fields and address fields from company and to
  120. children, contact fields from contact and to attached contact
  121. after create/update, just as if those were all modeled as
  122. fields.related to the parent
  123. """
  124. self.ensure_one()
  125. super(ResPartner, self)._fields_sync(update_values)
  126. contact_fields = self._contact_fields()
  127. # 1. From UPSTREAM: sync from parent contact
  128. if update_values.get('contact_id'):
  129. self._contact_sync_from_parent()
  130. # 2. To DOWNSTREAM: sync contact fields to parent or related
  131. elif any(field in contact_fields for field in update_values):
  132. update_ids = self.other_contact_ids.filtered(
  133. lambda p: not p.is_company)
  134. if self.contact_id:
  135. update_ids |= self.contact_id
  136. update_ids.update_contact(update_values)
  137. @api.onchange('contact_id')
  138. def _onchange_contact_id(self):
  139. if self.contact_id:
  140. self.name = self.contact_id.name
  141. @api.onchange('contact_type')
  142. def _onchange_contact_type(self):
  143. if self.contact_type == 'standalone':
  144. self.contact_id = False