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.

125 lines
5.4 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. }
  62. def name_get(self, cr, uid, ids, context=None):
  63. if context is None:
  64. context = {}
  65. if isinstance(ids, (int, long)):
  66. ids = [ids]
  67. res = []
  68. for record in self.browse(cr, uid, ids, context=context):
  69. name = record.name
  70. if record.parent_id and not record.is_company:
  71. name = "%s, %s" % (record.parent_id.name, name)
  72. if context.get('show_address'):
  73. name = name + "\n" + \
  74. self._display_address(cr, uid, record,
  75. without_company=True,
  76. context=context)
  77. name = name.replace('\n\n', '\n')
  78. name = name.replace('\n\n', '\n')
  79. if context.get('show_email') and record.email:
  80. name = "%s <%s>" % (name, record.email)
  81. res.append((record.id, name))
  82. return res
  83. def name_search(self, cr, uid, name, args=None, operator='ilike',
  84. context=None, limit=100):
  85. if not args:
  86. args = []
  87. if name and operator in ('=', 'ilike', '=ilike', 'like', '=like'):
  88. # search on the name of the contacts and of its company
  89. search_name = name
  90. if operator in ('ilike', 'like'):
  91. search_name = '%%%s%%' % name
  92. if operator in ('=ilike', '=like'):
  93. operator = operator[1:]
  94. query_args = {'name': search_name}
  95. limit_str = ''
  96. if limit:
  97. limit_str = ' limit %(limit)s'
  98. query_args['limit'] = limit
  99. cr.execute('''SELECT partner.id FROM res_partner partner
  100. LEFT JOIN res_partner company
  101. ON partner.parent_id = company.id
  102. WHERE partner.email ''' + operator + ''' %(name)s OR
  103. partner.display_name ''' + operator +
  104. ' %(name)s ' + limit_str, query_args)
  105. ids = map(lambda x: x[0], cr.fetchall())
  106. ids = self.search(cr, uid, [('id', 'in', ids)] + args,
  107. limit=limit, context=context)
  108. if ids:
  109. return self.name_get(cr, uid, ids, context)
  110. return super(res_partner, self).name_search(cr, uid, name, args,
  111. operator=operator,
  112. context=context,
  113. limit=limit)