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.

161 lines
6.6 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 _compute_commercial_partner(self):
  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)._compute_commercial_partner()
  90. for partner in self:
  91. if partner.contact_type == 'attached' and not partner.parent_id:
  92. partner.commercial_partner_id = partner.contact_id
  93. return result
  94. def _contact_fields(self):
  95. """ Returns the list of contact fields that are synced from the parent
  96. when a partner is attached to him. """
  97. return ['name', 'title']
  98. def _contact_sync_from_parent(self):
  99. """ Handle sync of contact fields when a new parent contact entity
  100. is set, as if they were related fields
  101. """
  102. self.ensure_one()
  103. if self.contact_id:
  104. contact_fields = self._contact_fields()
  105. sync_vals = self.contact_id._update_fields_values(contact_fields)
  106. self.write(sync_vals)
  107. def update_contact(self, vals):
  108. if self.env.context.get('__update_contact_lock'):
  109. return
  110. contact_fields = self._contact_fields()
  111. contact_vals = dict(
  112. (field, vals[field]) for field in contact_fields if field in vals
  113. )
  114. if contact_vals:
  115. self.with_context(__update_contact_lock=True).write(contact_vals)
  116. @api.multi
  117. def _fields_sync(self, update_values):
  118. """Sync commercial fields and address fields from company and to
  119. children, contact fields from contact and to attached contact
  120. after create/update, just as if those were all modeled as
  121. fields.related to the parent
  122. """
  123. self.ensure_one()
  124. super(ResPartner, self)._fields_sync(update_values)
  125. contact_fields = self._contact_fields()
  126. # 1. From UPSTREAM: sync from parent contact
  127. if update_values.get('contact_id'):
  128. self._contact_sync_from_parent()
  129. # 2. To DOWNSTREAM: sync contact fields to parent or related
  130. elif any(field in contact_fields for field in update_values):
  131. update_ids = self.other_contact_ids.filtered(
  132. lambda p: not p.is_company)
  133. if self.contact_id:
  134. update_ids |= self.contact_id
  135. update_ids.update_contact(update_values)
  136. @api.onchange('contact_id')
  137. def _onchange_contact_id(self):
  138. if self.contact_id:
  139. self.name = self.contact_id.name
  140. @api.onchange('contact_type')
  141. def _onchange_contact_type(self):
  142. if self.contact_type == 'standalone':
  143. self.contact_id = False