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.

160 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. comptute='_get_contact_type',
  12. store=True,
  13. required=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.depends('contact_id')
  23. def _get_contact_type(self):
  24. for record in self:
  25. record.contact_type = record.contact_id and 'attached' or 'standalone'
  26. def _basecontact_check_context(self, mode):
  27. """ Remove 'search_show_all_positions' for non-search mode.
  28. Keeping it in context can result in unexpected behaviour (ex: reading
  29. one2many might return wrong result - i.e with "attached contact"
  30. removed even if it's directly linked to a company).
  31. Actually, is easier to override a dictionary value to indicate it
  32. should be ignored...
  33. """
  34. if (mode != 'search' and
  35. 'search_show_all_positions' in self.env.context):
  36. result = self.with_context(
  37. search_show_all_positions={'is_set': False})
  38. else:
  39. result = self
  40. return result
  41. @api.model
  42. def search(self, args, offset=0, limit=None, order=None, count=False):
  43. """ Display only standalone contact matching ``args`` or having
  44. attached contact matching ``args`` """
  45. ctx = self.env.context
  46. if (ctx.get('search_show_all_positions', {}).get('is_set') and
  47. not ctx['search_show_all_positions']['set_value']):
  48. args = expression.normalize_domain(args)
  49. attached_contact_args = expression.AND(
  50. (args, [('contact_type', '=', 'attached')])
  51. )
  52. attached_contacts = super(ResPartner, self).search(
  53. attached_contact_args)
  54. args = expression.OR((
  55. expression.AND(([('contact_type', '=', 'standalone')], args)),
  56. [('other_contact_ids', 'in', attached_contacts.ids)],
  57. ))
  58. return super(ResPartner, self).search(args, offset=offset,
  59. limit=limit, order=order,
  60. count=count)
  61. @api.model
  62. def create(self, vals):
  63. """ When creating, use a modified self to alter the context (see
  64. comment in _basecontact_check_context). Also, we need to ensure
  65. that the name on an attached contact is the same as the name on the
  66. contact it is attached to."""
  67. modified_self = self._basecontact_check_context('create')
  68. if not vals.get('name') and vals.get('contact_id'):
  69. vals['name'] = modified_self.browse(vals['contact_id']).name
  70. return super(ResPartner, modified_self).create(vals)
  71. @api.multi
  72. def read(self, fields=None, load='_classic_read'):
  73. modified_self = self._basecontact_check_context('read')
  74. return super(ResPartner, modified_self).read(fields=fields, load=load)
  75. @api.multi
  76. def write(self, vals):
  77. modified_self = self._basecontact_check_context('write')
  78. return super(ResPartner, modified_self).write(vals)
  79. @api.multi
  80. def unlink(self):
  81. modified_self = self._basecontact_check_context('unlink')
  82. return super(ResPartner, modified_self).unlink()
  83. @api.multi
  84. def _commercial_partner_compute(self, name, args):
  85. """ Returns the partner that is considered the commercial
  86. entity of this partner. The commercial entity holds the master data
  87. for all commercial fields (see :py:meth:`~_commercial_fields`) """
  88. result = super(ResPartner, self)._commercial_partner_compute(name,
  89. args)
  90. for partner in self:
  91. if partner.contact_type == 'attached' and not partner.parent_id:
  92. result[partner.id] = partner.contact_id.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._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. super(ResPartner, self)._fields_sync(update_values)
  124. for record in self:
  125. contact_fields = record._contact_fields()
  126. # 1. From UPSTREAM: sync from parent contact
  127. if update_values.get('contact_id'):
  128. record._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 = record.other_contact_ids.filter(lambda p: not p.is_company)
  132. if record.contact_id:
  133. update_ids |= record.contact_id
  134. update_ids.update_contact(update_values)
  135. @api.onchange('contact_id')
  136. def _onchange_contact_id(self):
  137. if self.contact_id:
  138. self.name = self.contact_id.name
  139. @api.onchange('contact_type')
  140. def _onchange_contact_type(self):
  141. if self.contact_type == 'standalone':
  142. self.contact_id = False