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.

158 lines
6.4 KiB

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