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.

138 lines
6.6 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, osv, expression
  22. class res_partner(osv.osv):
  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. 'birthdate_date': fields.date('Birthdate'), # add a 'birthdate' as date field, i.e different from char 'birthdate' introduced v6.1!
  42. 'nationality_id': fields.many2one('res.country', 'Nationality'),
  43. }
  44. _defaults = {
  45. 'contact_type': 'standalone',
  46. }
  47. def _basecontact_check_context(self, cr, user, mode, context=None):
  48. if context is None:
  49. context = {}
  50. # Remove 'search_show_all_positions' for non-search mode.
  51. # Keeping it in context can result in unexpected behaviour (ex: reading
  52. # one2many might return wrong result - i.e with "attached contact" removed
  53. # even if it's directly linked to a company).
  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. if context is None:
  59. context = {}
  60. if context.get('search_show_all_positions') is False:
  61. # display only standalone contact matching ``args`` or having
  62. # attached contact matching ``args``
  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)
  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 onchange_contact_id(self, cr, uid, ids, contact_id, context=None):
  97. if contact_id:
  98. name = self.browse(cr, uid, contact_id, context=context).name
  99. return {'value': {'name': name}}
  100. return {}
  101. class ir_actions_window(osv.osv):
  102. _inherit = 'ir.actions.act_window'
  103. def read(self, cr, user, ids, fields=None, context=None, load='_classic_read'):
  104. action_ids = ids
  105. if isinstance(ids, (int, long)):
  106. action_ids = [ids]
  107. actions = super(ir_actions_window, self).read(cr, user, action_ids, fields=fields, context=context, load=load)
  108. for action in actions:
  109. if action.get('res_model', '') == 'res.partner':
  110. # By default, only show standalone contact
  111. action_context = action.get('context', '{}') or '{}'
  112. if 'search_show_all_positions' not in action_context:
  113. action['context'] = action_context.replace('{',
  114. "{'search_show_all_positions': False,", 1)
  115. if isinstance(ids, (int, long)):
  116. if actions:
  117. return actions[0]
  118. return False
  119. return actions