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.

109 lines
5.6 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, context=None):
  31. """ Returns the partner that is considered the commercial
  32. entity of this partner. The commercial entity holds the master data
  33. for all commercial fields (see :py:meth:`~_commercial_fields`) """
  34. result = dict.fromkeys(ids, False)
  35. for partner in self.browse(cr, uid, ids, context=context):
  36. current_partner = partner
  37. while not current_partner.is_company and current_partner.parent_id:
  38. current_partner = current_partner.parent_id
  39. result[partner.id] = current_partner.id
  40. return result
  41. def _display_name_compute(self, cr, uid, ids, name, args, context=None):
  42. return dict(self.name_get(cr, uid, ids, context=context))
  43. # indirections to avoid passing a copy of the overridable method when declaring the function field
  44. _display_name = lambda self, *args, **kwargs: self._display_name_compute(*args, **kwargs)
  45. _display_name_store_triggers = {
  46. 'res.partner': (lambda self,cr,uid,ids,context=None: self.search(cr, uid, [('id','child_of',ids)]),
  47. ['parent_id', 'is_company', 'name'], 10)
  48. }
  49. _order = "display_name"
  50. _columns = {
  51. 'display_name': fields.function(_display_name, type='char', string='Name', store=_display_name_store_triggers),
  52. 'id': fields.integer('Id', readonly=True),
  53. 'create_date': fields.datetime('Create Date', readonly=True),
  54. }
  55. def name_get(self, cr, uid, ids, context=None):
  56. if context is None:
  57. context = {}
  58. if isinstance(ids, (int, long)):
  59. ids = [ids]
  60. res = []
  61. for record in self.browse(cr, uid, ids, context=context):
  62. name = record.name
  63. if record.parent_id and not record.is_company:
  64. name = "%s, %s" % (record.parent_id.name, name)
  65. if context.get('show_address'):
  66. name = name + "\n" + self._display_address(cr, uid, record, without_company=True, context=context)
  67. name = name.replace('\n\n','\n')
  68. name = name.replace('\n\n','\n')
  69. if context.get('show_email') and record.email:
  70. name = "%s <%s>" % (name, record.email)
  71. res.append((record.id, name))
  72. return res
  73. def name_search(self, cr, uid, name, args=None, operator='ilike', context=None, limit=100):
  74. if not args:
  75. args = []
  76. if name and operator in ('=', 'ilike', '=ilike', 'like', '=like'):
  77. # search on the name of the contacts and of its company
  78. search_name = name
  79. if operator in ('ilike', 'like'):
  80. search_name = '%%%s%%' % name
  81. if operator in ('=ilike', '=like'):
  82. operator = operator[1:]
  83. query_args = {'name': search_name}
  84. limit_str = ''
  85. if limit:
  86. limit_str = ' limit %(limit)s'
  87. query_args['limit'] = limit
  88. cr.execute('''SELECT partner.id FROM res_partner partner
  89. LEFT JOIN res_partner company ON partner.parent_id = company.id
  90. WHERE partner.email ''' + operator +''' %(name)s OR
  91. partner.display_name ''' + operator + ' %(name)s ' + limit_str, query_args)
  92. ids = map(lambda x: x[0], cr.fetchall())
  93. ids = self.search(cr, uid, [('id', 'in', ids)] + args, limit=limit, context=context)
  94. if ids:
  95. return self.name_get(cr, uid, ids, context)
  96. return super(res_partner,self).name_search(cr, uid, name, args, operator=operator, context=context, limit=limit)