res_partner.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. from openerp.osv import fields,osv
  22. class res_partner(osv.osv):
  23. """ Inherits partner and adds CRM information in the partner form """
  24. _inherit = 'res.partner'
  25. def _opportunity_meeting_phonecall_count(self, cr, uid, ids, field_name, arg, context=None):
  26. res = dict(map(lambda x: (x,{'opportunity_count': 0, 'meeting_count': 0}), ids))
  27. # the user may not have access rights for opportunities or meetings
  28. try:
  29. for partner in self.browse(cr, uid, ids, context):
  30. if partner.is_company:
  31. operator = 'child_of'
  32. else:
  33. operator = '='
  34. opp_ids = self.pool['crm.lead'].search(cr, uid, [('partner_id', operator, partner.id), ('type', '=', 'opportunity'), ('probability', '<', '100')], context=context)
  35. res[partner.id] = {
  36. 'opportunity_count': len(opp_ids),
  37. 'meeting_count': len(partner.meeting_ids),
  38. }
  39. except:
  40. pass
  41. return res
  42. def _phonecall_count(self, cr, uid, ids, field_name, arg, context=None):
  43. res = {}
  44. for partner in self.browse(cr, uid, ids, context):
  45. res[partner.id] = len(partner.phonecall_ids)
  46. return res
  47. _columns = {
  48. 'section_id': fields.many2one('crm.case.section', 'Sales Team'),
  49. 'opportunity_ids': fields.one2many('crm.lead', 'partner_id',\
  50. 'Leads and Opportunities', domain=[('probability', 'not in', ['0', '100'])]),
  51. 'meeting_ids': fields.many2many('calendar.event', 'calendar_event_res_partner_rel','res_partner_id', 'calendar_event_id',
  52. 'Meetings'),
  53. 'phonecall_ids': fields.one2many('crm.phonecall', 'partner_id',\
  54. 'Phonecalls'),
  55. 'opportunity_count': fields.function(_opportunity_meeting_phonecall_count, string="Opportunity", type='integer', multi='opp_meet'),
  56. 'meeting_count': fields.function(_opportunity_meeting_phonecall_count, string="# Meetings", type='integer', multi='opp_meet'),
  57. 'phonecall_count': fields.function(_phonecall_count, string="Phonecalls", type="integer"),
  58. }
  59. def redirect_partner_form(self, cr, uid, partner_id, context=None):
  60. search_view = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'base', 'view_res_partner_filter')
  61. value = {
  62. 'domain': "[]",
  63. 'view_type': 'form',
  64. 'view_mode': 'form,tree',
  65. 'res_model': 'res.partner',
  66. 'res_id': int(partner_id),
  67. 'view_id': False,
  68. 'context': context,
  69. 'type': 'ir.actions.act_window',
  70. 'search_view_id': search_view and search_view[1] or False
  71. }
  72. return value
  73. def make_opportunity(self, cr, uid, ids, opportunity_summary, planned_revenue=0.0, probability=0.0, partner_id=None, context=None):
  74. categ_obj = self.pool.get('crm.case.categ')
  75. categ_ids = categ_obj.search(cr, uid, [('object_id.model','=','crm.lead')])
  76. lead_obj = self.pool.get('crm.lead')
  77. opportunity_ids = {}
  78. for partner in self.browse(cr, uid, ids, context=context):
  79. if not partner_id:
  80. partner_id = partner.id
  81. opportunity_id = lead_obj.create(cr, uid, {
  82. 'name' : opportunity_summary,
  83. 'planned_revenue' : planned_revenue,
  84. 'probability' : probability,
  85. 'partner_id' : partner_id,
  86. 'categ_ids' : categ_ids and categ_ids[0:1] or [],
  87. 'type': 'opportunity'
  88. }, context=context)
  89. opportunity_ids[partner_id] = opportunity_id
  90. return opportunity_ids
  91. def schedule_meeting(self, cr, uid, ids, context=None):
  92. partner_ids = list(ids)
  93. partner_ids.append(self.pool.get('res.users').browse(cr, uid, uid).partner_id.id)
  94. res = self.pool.get('ir.actions.act_window').for_xml_id(cr, uid, 'calendar', 'action_calendar_event', context)
  95. res['context'] = {
  96. 'search_default_partner_ids': list(ids),
  97. 'default_partner_ids': partner_ids,
  98. }
  99. return res