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.

185 lines
9.1 KiB

  1. # -*- coding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # OpenERP, Open Source Management Solution
  5. # Copyright (C) 2013-TODAY OpenERP SA (<http://www.openerp.com>).
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU Affero General Public License as
  9. # published by the Free Software Foundation, either version 3 of the
  10. # License, or (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU Affero General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Affero General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. #
  20. ##############################################################################
  21. from openerp.osv import fields, orm, expression
  22. class res_partner(orm.Model):
  23. _inherit = 'res.partner'
  24. _contact_type = [
  25. ('standalone', 'Standalone Contact'),
  26. ('attached', 'Attached to existing Contact'),
  27. ]
  28. def _get_contact_type(self, cr, uid, ids, field_name, args, context=None):
  29. result = dict.fromkeys(ids, 'standalone')
  30. for partner in self.browse(cr, uid, ids, context=context):
  31. if partner.contact_id:
  32. result[partner.id] = 'attached'
  33. return result
  34. _columns = {
  35. 'contact_type': fields.function(_get_contact_type, type='selection', selection=_contact_type,
  36. string='Contact Type', required=True, select=1, store=True),
  37. 'contact_id': fields.many2one('res.partner', 'Main Contact',
  38. domain=[('is_company', '=', False), ('contact_type', '=', 'standalone')]),
  39. 'other_contact_ids': fields.one2many('res.partner', 'contact_id', 'Others Positions'),
  40. # Person specific fields
  41. # add a 'birthdate' as date field, i.e different from char 'birthdate' introduced v6.1!
  42. 'birthdate_date': fields.date('Birthdate'),
  43. 'nationality_id': fields.many2one('res.country', 'Nationality'),
  44. }
  45. _defaults = {
  46. 'contact_type': 'standalone',
  47. }
  48. def _basecontact_check_context(self, cr, user, mode, context=None):
  49. """ Remove 'search_show_all_positions' for non-search mode.
  50. Keeping it in context can result in unexpected behaviour (ex: reading
  51. one2many might return wrong result - i.e with "attached contact" removed
  52. even if it's directly linked to a company). """
  53. context = dict(context or {})
  54. if mode != 'search':
  55. context.pop('search_show_all_positions', None)
  56. return context
  57. def search(self, cr, user, args, offset=0, limit=None, order=None, context=None, count=False):
  58. """ Display only standalone contact matching ``args`` or having
  59. attached contact matching ``args`` """
  60. if context is None:
  61. context = {}
  62. if context.get('search_show_all_positions') is False:
  63. args = expression.normalize_domain(args)
  64. attached_contact_args = expression.AND((args, [('contact_type', '=', 'attached')]))
  65. attached_contact_ids = super(res_partner, self).search(cr, user, attached_contact_args,
  66. context=context)
  67. args = expression.OR((
  68. expression.AND(([('contact_type', '=', 'standalone')], args)),
  69. [('other_contact_ids', 'in', attached_contact_ids)],
  70. ))
  71. return super(res_partner, self).search(cr, user, args, offset=offset, limit=limit,
  72. order=order, context=context, count=count)
  73. def create(self, cr, user, vals, context=None):
  74. context = self._basecontact_check_context(cr, user, 'create', context)
  75. if not vals.get('name') and vals.get('contact_id'):
  76. vals['name'] = self.browse(cr, user, vals['contact_id'], context=context).name
  77. return super(res_partner, self).create(cr, user, vals, context=context)
  78. def read(self, cr, user, ids, fields=None, context=None, load='_classic_read'):
  79. context = self._basecontact_check_context(cr, user, 'read', context)
  80. return super(res_partner, self).read(cr, user, ids, fields=fields, context=context, load=load)
  81. def write(self, cr, user, ids, vals, context=None):
  82. context = self._basecontact_check_context(cr, user, 'write', context)
  83. return super(res_partner, self).write(cr, user, ids, vals, context=context)
  84. def unlink(self, cr, user, ids, context=None):
  85. context = self._basecontact_check_context(cr, user, 'unlink', context)
  86. return super(res_partner, self).unlink(cr, user, ids, context=context)
  87. def _commercial_partner_compute(self, cr, uid, ids, name, args, context=None):
  88. """ Returns the partner that is considered the commercial
  89. entity of this partner. The commercial entity holds the master data
  90. for all commercial fields (see :py:meth:`~_commercial_fields`) """
  91. result = super(res_partner, self)._commercial_partner_compute(cr, uid, ids, name, args, context=context)
  92. for partner in self.browse(cr, uid, ids, context=context):
  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, cr, uid, context=None):
  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, cr, uid, partner, context=None):
  101. """ Handle sync of contact fields when a new parent contact entity is set,
  102. as if they were related fields """
  103. if partner.contact_id:
  104. contact_fields = self._contact_fields(cr, uid, context=context)
  105. sync_vals = self._update_fields_values(cr, uid, partner.contact_id,
  106. contact_fields, context=context)
  107. partner.write(sync_vals)
  108. def update_contact(self, cr, uid, ids, vals, context=None):
  109. if context is None:
  110. context = {}
  111. if context.get('__update_contact_lock'):
  112. return
  113. contact_fields = self._contact_fields(cr, uid, context=context)
  114. contact_vals = dict((field, vals[field]) for field in contact_fields if field in vals)
  115. if contact_vals:
  116. ctx = dict(context, __update_contact_lock=True)
  117. self.write(cr, uid, ids, contact_vals, context=ctx)
  118. def _fields_sync(self, cr, uid, partner, update_values, context=None):
  119. """ Sync commercial fields and address fields from company and to children,
  120. contact fields from contact and to attached contact after create/update,
  121. just as if those were all modeled as fields.related to the parent """
  122. super(res_partner, self)._fields_sync(cr, uid, partner, update_values, context=context)
  123. contact_fields = self._contact_fields(cr, uid, context=context)
  124. # 1. From UPSTREAM: sync from parent contact
  125. if update_values.get('contact_id'):
  126. self._contact_sync_from_parent(cr, uid, partner, context=context)
  127. # 2. To DOWNSTREAM: sync contact fields to parent or related
  128. elif any(field in contact_fields for field in update_values):
  129. update_ids = [c.id for c in partner.other_contact_ids if not c.is_company]
  130. if partner.contact_id:
  131. update_ids.append(partner.contact_id.id)
  132. self.update_contact(cr, uid, update_ids, update_values, context=context)
  133. def onchange_contact_id(self, cr, uid, ids, contact_id, context=None):
  134. values = {}
  135. if contact_id:
  136. values['name'] = self.browse(cr, uid, contact_id, context=context).name
  137. return {'value': values}
  138. def onchange_contact_type(self, cr, uid, ids, contact_type, context=None):
  139. values = {}
  140. if contact_type == 'standalone':
  141. values['contact_id'] = False
  142. return {'value': values}
  143. class ir_actions_window(orm.Model):
  144. _inherit = 'ir.actions.act_window'
  145. def read(self, cr, user, ids, fields=None, context=None, load='_classic_read'):
  146. action_ids = ids
  147. if isinstance(ids, (int, long)):
  148. action_ids = [ids]
  149. actions = super(ir_actions_window, self).read(cr, user, action_ids, fields=fields, context=context, load=load)
  150. for action in actions:
  151. if action.get('res_model', '') == 'res.partner':
  152. # By default, only show standalone contact
  153. action_context = action.get('context', '{}') or '{}'
  154. if 'search_show_all_positions' not in action_context:
  155. action['context'] = action_context.replace('{',
  156. "{'search_show_all_positions': False,", 1)
  157. if isinstance(ids, (int, long)):
  158. if actions:
  159. return actions[0]
  160. return False
  161. return actions