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.

163 lines
6.7 KiB

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