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.

165 lines
6.8 KiB

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