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.

186 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. if context is None:
  54. context = {}
  55. if mode != 'search':
  56. context.pop('search_show_all_positions', None)
  57. return context
  58. def search(self, cr, user, args, offset=0, limit=None, order=None, context=None, count=False):
  59. """ Display only standalone contact matching ``args`` or having
  60. attached contact matching ``args`` """
  61. if context is None:
  62. context = {}
  63. if context.get('search_show_all_positions') is False:
  64. args = expression.normalize_domain(args)
  65. attached_contact_args = expression.AND((args, [('contact_type', '=', 'attached')]))
  66. attached_contact_ids = super(res_partner, self).search(cr, user, attached_contact_args,
  67. context=context)
  68. args = expression.OR((
  69. expression.AND(([('contact_type', '=', 'standalone')], args)),
  70. [('other_contact_ids', 'in', attached_contact_ids)],
  71. ))
  72. return super(res_partner, self).search(cr, user, args, offset=offset, limit=limit,
  73. order=order, context=context, count=count)
  74. def create(self, cr, user, vals, context=None):
  75. context = self._basecontact_check_context(cr, user, 'create', context)
  76. if not vals.get('name') and vals.get('contact_id'):
  77. vals['name'] = self.browse(cr, user, vals['contact_id'], context=context).name
  78. return super(res_partner, self).create(cr, user, vals, context=context)
  79. def read(self, cr, user, ids, fields=None, context=None, load='_classic_read'):
  80. context = self._basecontact_check_context(cr, user, 'read', context)
  81. return super(res_partner, self).read(cr, user, ids, fields=fields, context=context, load=load)
  82. def write(self, cr, user, ids, vals, context=None):
  83. context = self._basecontact_check_context(cr, user, 'write', context)
  84. return super(res_partner, self).write(cr, user, ids, vals, context=context)
  85. def unlink(self, cr, user, ids, context=None):
  86. context = self._basecontact_check_context(cr, user, 'unlink', context)
  87. return super(res_partner, self).unlink(cr, user, ids, context=context)
  88. def _commercial_partner_compute(self, cr, uid, ids, name, args, context=None):
  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(res_partner, self)._commercial_partner_compute(cr, uid, ids, name, args, context=context)
  93. for partner in self.browse(cr, uid, ids, context=context):
  94. if partner.contact_type == 'attached' and not partner.parent_id:
  95. result[partner.id] = partner.contact_id.id
  96. return result
  97. def _contact_fields(self, cr, uid, context=None):
  98. """ Returns the list of contact fields that are synced from the parent
  99. when a partner is attached to him. """
  100. return ['name', 'title']
  101. def _contact_sync_from_parent(self, cr, uid, partner, context=None):
  102. """ Handle sync of contact fields when a new parent contact entity is set,
  103. as if they were related fields """
  104. if partner.contact_id:
  105. contact_fields = self._contact_fields(cr, uid, context=context)
  106. sync_vals = self._update_fields_values(cr, uid, partner.contact_id,
  107. contact_fields, context=context)
  108. partner.write(sync_vals)
  109. def update_contact(self, cr, uid, ids, vals, context=None):
  110. if context is None:
  111. context = {}
  112. if context.get('__update_contact_lock'):
  113. return
  114. contact_fields = self._contact_fields(cr, uid, context=context)
  115. contact_vals = dict((field, vals[field]) for field in contact_fields if field in vals)
  116. if contact_vals:
  117. ctx = dict(context, __update_contact_lock=True)
  118. self.write(cr, uid, ids, contact_vals, context=ctx)
  119. def _fields_sync(self, cr, uid, partner, update_values, context=None):
  120. """ Sync commercial fields and address fields from company and to children,
  121. contact fields from contact and to attached contact after create/update,
  122. just as if those were all modeled as fields.related to the parent """
  123. super(res_partner, self)._fields_sync(cr, uid, partner, update_values, context=context)
  124. contact_fields = self._contact_fields(cr, uid, context=context)
  125. # 1. From UPSTREAM: sync from parent contact
  126. if update_values.get('contact_id'):
  127. self._contact_sync_from_parent(cr, uid, partner, context=context)
  128. # 2. To DOWNSTREAM: sync contact fields to parent or related
  129. elif any(field in contact_fields for field in update_values):
  130. update_ids = [c.id for c in partner.other_contact_ids if not c.is_company]
  131. if partner.contact_id:
  132. update_ids.append(partner.contact_id.id)
  133. self.update_contact(cr, uid, update_ids, update_values, context=context)
  134. def onchange_contact_id(self, cr, uid, ids, contact_id, context=None):
  135. values = {}
  136. if contact_id:
  137. values['name'] = self.browse(cr, uid, contact_id, context=context).name
  138. return {'value': values}
  139. def onchange_contact_type(self, cr, uid, ids, contact_type, context=None):
  140. values = {}
  141. if contact_type == 'standalone':
  142. values['contact_id'] = False
  143. return {'value': values}
  144. class ir_actions_window(orm.Model):
  145. _inherit = 'ir.actions.act_window'
  146. def read(self, cr, user, ids, fields=None, context=None, load='_classic_read'):
  147. action_ids = ids
  148. if isinstance(ids, (int, long)):
  149. action_ids = [ids]
  150. actions = super(ir_actions_window, self).read(cr, user, action_ids, fields=fields, context=context, load=load)
  151. for action in actions:
  152. if action.get('res_model', '') == 'res.partner':
  153. # By default, only show standalone contact
  154. action_context = action.get('context', '{}') or '{}'
  155. if 'search_show_all_positions' not in action_context:
  156. action['context'] = action_context.replace('{',
  157. "{'search_show_all_positions': False,", 1)
  158. if isinstance(ids, (int, long)):
  159. if actions:
  160. return actions[0]
  161. return False
  162. return actions