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.

129 lines
5.7 KiB

  1. # -*- coding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # OpenERP, Open Source Management Solution
  5. # Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
  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. import openerp
  22. from openerp import SUPERUSER_ID
  23. from openerp import tools
  24. from openerp.osv import osv, fields
  25. from openerp.tools.translate import _
  26. from openerp.tools.yaml_import import is_comment
  27. class res_partner(osv.Model):
  28. _description = 'Partner'
  29. _inherit = "res.partner"
  30. def _commercial_partner_compute(self, cr, uid, ids, name, args,
  31. context=None):
  32. """ Returns the partner that is considered the commercial
  33. entity of this partner. The commercial entity holds the master data
  34. for all commercial fields (see :py:meth:`~_commercial_fields`) """
  35. result = dict.fromkeys(ids, False)
  36. for partner in self.browse(cr, uid, ids, context=context):
  37. current_partner = partner
  38. while not current_partner.is_company and current_partner.parent_id:
  39. current_partner = current_partner.parent_id
  40. result[partner.id] = current_partner.id
  41. return result
  42. def _display_name_compute(self, cr, uid, ids, name, args, context=None):
  43. return dict(self.name_get(cr, uid, ids, context=context))
  44. # indirections to avoid passing a copy of the overridable method when
  45. # declaring the function field
  46. _display_name = lambda self, *args, **kwargs: \
  47. self._display_name_compute(*args, **kwargs)
  48. _display_name_store_triggers = {
  49. 'res.partner': (lambda self, cr, uid, ids, context=None:
  50. self.search(cr, uid, [(
  51. 'id', 'child_of', ids)]),
  52. ['parent_id', 'is_company', 'name'], 10)
  53. }
  54. _order = "display_name"
  55. _columns = {
  56. 'display_name': fields.function(_display_name, type='char',
  57. string='Name',
  58. store=_display_name_store_triggers),
  59. 'id': fields.integer('Id', readonly=True),
  60. 'create_date': fields.datetime('Create Date', readonly=True),
  61. 'partner_merged_ids' : fields.many2many('res.partner',\
  62. 'partners_mergeds', 'partner_active', 'partner_id', 'Relation '\
  63. 'with partner merged', domain=['|', ('active','=',True), (\
  64. 'active','=',False)], readonly=True)
  65. }
  66. def name_get(self, cr, uid, ids, context=None):
  67. if context is None:
  68. context = {}
  69. if isinstance(ids, (int, long)):
  70. ids = [ids]
  71. res = []
  72. for record in self.browse(cr, uid, ids, context=context):
  73. name = record.name
  74. if record.parent_id and not record.is_company:
  75. name = "%s, %s" % (record.parent_id.name, name)
  76. if context.get('show_address'):
  77. name = name + "\n" + \
  78. self._display_address(cr, uid, record,
  79. without_company=True,
  80. context=context)
  81. name = name.replace('\n\n', '\n')
  82. name = name.replace('\n\n', '\n')
  83. if context.get('show_email') and record.email:
  84. name = "%s <%s>" % (name, record.email)
  85. res.append((record.id, name))
  86. return res
  87. def name_search(self, cr, uid, name, args=None, operator='ilike',
  88. context=None, limit=100):
  89. if not args:
  90. args = []
  91. if name and operator in ('=', 'ilike', '=ilike', 'like', '=like'):
  92. # search on the name of the contacts and of its company
  93. search_name = name
  94. if operator in ('ilike', 'like'):
  95. search_name = '%%%s%%' % name
  96. if operator in ('=ilike', '=like'):
  97. operator = operator[1:]
  98. query_args = {'name': search_name}
  99. limit_str = ''
  100. if limit:
  101. limit_str = ' limit %(limit)s'
  102. query_args['limit'] = limit
  103. cr.execute('''SELECT partner.id FROM res_partner partner
  104. LEFT JOIN res_partner company
  105. ON partner.parent_id = company.id
  106. WHERE partner.email ''' + operator + ''' %(name)s OR
  107. partner.display_name ''' + operator +
  108. ' %(name)s ' + limit_str, query_args)
  109. ids = map(lambda x: x[0], cr.fetchall())
  110. ids = self.search(cr, uid, [('id', 'in', ids)] + args,
  111. limit=limit, context=context)
  112. if ids:
  113. return self.name_get(cr, uid, ids, context)
  114. return super(res_partner, self).name_search(cr, uid, name, args,
  115. operator=operator,
  116. context=context,
  117. limit=limit)