2010-03-16 11:03:51 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
##############################################################################
|
|
|
|
#
|
|
|
|
# OpenERP, Open Source Management Solution
|
|
|
|
# Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
##############################################################################
|
|
|
|
|
|
|
|
from osv import osv, fields
|
|
|
|
from tools.translate import _
|
2011-03-29 15:19:09 +00:00
|
|
|
import tools
|
2011-04-06 08:52:52 +00:00
|
|
|
import re
|
2010-03-25 06:19:40 +00:00
|
|
|
|
2011-02-04 13:05:56 +00:00
|
|
|
class crm_lead2opportunity_partner(osv.osv_memory):
|
|
|
|
_name = 'crm.lead2opportunity.partner'
|
|
|
|
_description = 'Lead To Opportunity Partner'
|
|
|
|
_inherit = 'crm.lead2partner'
|
2010-11-08 14:22:51 +00:00
|
|
|
|
2011-02-04 13:05:56 +00:00
|
|
|
_columns = {
|
2012-09-12 17:55:27 +00:00
|
|
|
'action': fields.selection([('exist', 'Link to an existing customer'), \
|
|
|
|
('create', 'Create a new customer'), \
|
|
|
|
('nothing', 'Do not link to a customer')], \
|
|
|
|
'Related Customer', required=True),
|
2012-09-26 15:42:32 +00:00
|
|
|
'name': fields.selection([('convert', 'Convert to opportunity'), \
|
|
|
|
('merge', 'Merge with existing opportunities')], \
|
|
|
|
'Conversion Action', required=True),
|
2011-09-27 17:20:15 +00:00
|
|
|
'opportunity_ids': fields.many2many('crm.lead', string='Opportunities', domain=[('type', '=', 'opportunity')]),
|
2011-02-04 13:05:56 +00:00
|
|
|
}
|
2011-02-17 12:57:31 +00:00
|
|
|
|
2011-02-06 21:28:34 +00:00
|
|
|
def default_get(self, cr, uid, fields, context=None):
|
2011-02-08 13:36:40 +00:00
|
|
|
"""
|
2012-09-26 15:42:32 +00:00
|
|
|
Default get for name, opportunity_ids.
|
|
|
|
If there is an exisitng partner link to the lead, find all existing
|
|
|
|
opportunities links with this partner to merge all information together
|
2011-02-08 13:36:40 +00:00
|
|
|
"""
|
|
|
|
lead_obj = self.pool.get('crm.lead')
|
2011-02-06 21:28:34 +00:00
|
|
|
|
2011-02-08 13:36:40 +00:00
|
|
|
res = super(crm_lead2opportunity_partner, self).default_get(cr, uid, fields, context=context)
|
|
|
|
opportunities = res.get('opportunity_ids') or []
|
2011-02-09 14:15:45 +00:00
|
|
|
partner_id = False
|
2011-04-11 08:51:20 +00:00
|
|
|
email = False
|
2011-02-09 14:15:45 +00:00
|
|
|
for lead in lead_obj.browse(cr, uid, opportunities, context=context):
|
|
|
|
partner_id = lead.partner_id and lead.partner_id.id or False
|
2011-10-26 16:29:01 +00:00
|
|
|
|
|
|
|
#TOFIX: use mail.mail_message.to_mail
|
2011-04-06 08:52:52 +00:00
|
|
|
email = re.findall(r'([^ ,<@]+@[^> ,]+)', lead.email_from or '')
|
|
|
|
email = map(lambda x: "'" + x + "'", email)
|
2011-02-09 14:15:45 +00:00
|
|
|
|
2011-02-17 12:57:31 +00:00
|
|
|
if not partner_id and res.get('partner_id'):
|
2011-02-08 13:36:40 +00:00
|
|
|
partner_id = res.get('partner_id')
|
2011-02-09 14:15:45 +00:00
|
|
|
|
|
|
|
ids = []
|
|
|
|
if partner_id:
|
2012-09-26 15:42:32 +00:00
|
|
|
# Search for opportunities that have the same partner and that arent done or cancelled
|
2011-03-29 13:12:42 +00:00
|
|
|
ids = lead_obj.search(cr, uid, [('partner_id', '=', partner_id), ('type', '=', 'opportunity'), '!', ('state', 'in', ['done', 'cancel'])])
|
2011-03-29 15:19:09 +00:00
|
|
|
if ids:
|
|
|
|
opportunities.append(ids[0])
|
2011-04-06 08:52:52 +00:00
|
|
|
if not partner_id:
|
|
|
|
if email:
|
2012-09-26 15:42:32 +00:00
|
|
|
# Find email of existing opportunity matching the email_from of the lead
|
2011-04-06 08:52:52 +00:00
|
|
|
cr.execute("""select id from crm_lead where type='opportunity' and
|
|
|
|
substring(email_from from '([^ ,<@]+@[^> ,]+)') in (%s)""" % (','.join(email)))
|
|
|
|
ids = map(lambda x:x[0], cr.fetchall())
|
|
|
|
if ids:
|
|
|
|
opportunities.append(ids[0])
|
2011-02-17 12:57:31 +00:00
|
|
|
|
2011-02-09 14:15:45 +00:00
|
|
|
if 'action' in fields:
|
|
|
|
res.update({'action' : partner_id and 'exist' or 'create'})
|
|
|
|
if 'partner_id' in fields:
|
|
|
|
res.update({'partner_id' : partner_id})
|
2011-02-08 13:36:40 +00:00
|
|
|
if 'name' in fields:
|
2011-02-09 14:15:45 +00:00
|
|
|
res.update({'name' : ids and 'merge' or 'convert'})
|
2011-02-08 13:36:40 +00:00
|
|
|
if 'opportunity_ids' in fields:
|
|
|
|
res.update({'opportunity_ids': opportunities})
|
2010-11-08 14:22:51 +00:00
|
|
|
|
2011-02-08 13:36:40 +00:00
|
|
|
return res
|
2011-02-17 12:57:31 +00:00
|
|
|
|
2011-02-04 13:05:56 +00:00
|
|
|
def view_init(self, cr, uid, fields, context=None):
|
2010-03-16 11:03:51 +00:00
|
|
|
"""
|
2012-09-26 15:42:32 +00:00
|
|
|
Check some preconditions before the wizard executes.
|
2010-03-16 11:03:51 +00:00
|
|
|
"""
|
2011-02-04 13:05:56 +00:00
|
|
|
if context is None:
|
|
|
|
context = {}
|
|
|
|
lead_obj = self.pool.get('crm.lead')
|
|
|
|
for lead in lead_obj.browse(cr, uid, context.get('active_ids', []), context=context):
|
|
|
|
if lead.state in ['done', 'cancel']:
|
2012-08-06 17:07:32 +00:00
|
|
|
raise osv.except_osv(_("Warning !"), _("Closed/Cancelled leads cannot be converted into opportunities."))
|
2011-02-04 13:05:56 +00:00
|
|
|
return False
|
2011-02-17 12:57:31 +00:00
|
|
|
|
2011-10-25 07:39:57 +00:00
|
|
|
def _convert_opportunity(self, cr, uid, ids, vals, context=None):
|
2011-10-24 13:30:10 +00:00
|
|
|
if context is None:
|
|
|
|
context = {}
|
|
|
|
lead = self.pool.get('crm.lead')
|
2012-08-23 20:06:29 +00:00
|
|
|
res = False
|
|
|
|
partner_ids_map = self._create_partner(cr, uid, ids, context=context)
|
2011-10-25 07:39:57 +00:00
|
|
|
lead_ids = vals.get('lead_ids', [])
|
|
|
|
team_id = vals.get('section_id', False)
|
2012-08-23 20:06:29 +00:00
|
|
|
for lead_id in lead_ids:
|
|
|
|
partner_id = partner_ids_map.get(lead_id, False)
|
|
|
|
# FIXME: cannot pass user_ids as the salesman allocation only works in batch
|
|
|
|
res = lead.convert_opportunity(cr, uid, [lead_id], partner_id, [], team_id, context=context)
|
2012-09-26 15:42:32 +00:00
|
|
|
# FIXME: must perform salesman allocation in batch separately here
|
2012-08-23 20:06:29 +00:00
|
|
|
user_ids = vals.get('user_ids', False)
|
|
|
|
if user_ids:
|
|
|
|
lead.allocate_salesman(cr, uid, lead_ids, user_ids, team_id=team_id, context=context)
|
|
|
|
return res
|
2011-10-24 13:30:10 +00:00
|
|
|
|
|
|
|
def _merge_opportunity(self, cr, uid, ids, opportunity_ids, action='merge', context=None):
|
|
|
|
if context is None:
|
|
|
|
context = {}
|
|
|
|
res = False
|
2012-08-23 20:06:29 +00:00
|
|
|
# Expected: all newly-converted leads (active_ids) will be merged with the opportunity(ies)
|
|
|
|
# that have been selected in the 'opportunity_ids' m2m, with all these records
|
|
|
|
# merged into the first opportunity (and the rest deleted)
|
|
|
|
opportunity_ids = [o.id for o in opportunity_ids]
|
|
|
|
lead_ids = context.get('active_ids', [])
|
|
|
|
if action == 'merge' and lead_ids and opportunity_ids:
|
|
|
|
# Add the leads in the to-merge list, next to other opps
|
|
|
|
# (the fact that they're passed in context['lead_ids'] means that
|
|
|
|
# they cannot be selected to contain the result of the merge.
|
|
|
|
opportunity_ids.extend(lead_ids)
|
|
|
|
context.update({'lead_ids': lead_ids, "convert" : True})
|
|
|
|
res = self.pool.get('crm.lead').merge_opportunity(cr, uid, opportunity_ids, context=context)
|
2011-10-24 13:30:10 +00:00
|
|
|
return res
|
|
|
|
|
2010-03-16 11:03:51 +00:00
|
|
|
def action_apply(self, cr, uid, ids, context=None):
|
|
|
|
"""
|
2012-09-26 15:42:32 +00:00
|
|
|
Convert lead to opportunity or merge lead and opportunity and open
|
|
|
|
the freshly created opportunity view.
|
2010-03-16 11:03:51 +00:00
|
|
|
"""
|
2012-09-26 15:42:32 +00:00
|
|
|
if context is None:
|
2011-03-16 14:30:25 +00:00
|
|
|
context = {}
|
2011-10-24 13:30:10 +00:00
|
|
|
lead = self.pool.get('crm.lead')
|
2011-10-25 07:39:57 +00:00
|
|
|
lead_ids = context.get('active_ids', [])
|
2011-10-24 13:30:10 +00:00
|
|
|
data = self.browse(cr, uid, ids, context=context)[0]
|
2012-09-24 07:08:49 +00:00
|
|
|
self._convert_opportunity(cr, uid, ids, {'lead_ids': lead_ids}, context=context)
|
|
|
|
self._merge_opportunity(cr, uid, ids, data.opportunity_ids, data.name, context=context)
|
2011-10-24 13:30:10 +00:00
|
|
|
return lead.redirect_opportunity_view(cr, uid, lead_ids[0], context=context)
|
2010-03-16 11:03:51 +00:00
|
|
|
|
|
|
|
|
2011-03-16 14:30:25 +00:00
|
|
|
class crm_lead2opportunity_mass_convert(osv.osv_memory):
|
|
|
|
_name = 'crm.lead2opportunity.partner.mass'
|
|
|
|
_description = 'Mass Lead To Opportunity Partner'
|
|
|
|
_inherit = 'crm.lead2opportunity.partner'
|
2011-03-24 16:09:29 +00:00
|
|
|
|
2011-03-16 14:30:25 +00:00
|
|
|
_columns = {
|
2012-08-23 20:06:29 +00:00
|
|
|
'user_ids': fields.many2many('res.users', string='Salesmen'),
|
2011-03-16 14:30:25 +00:00
|
|
|
'section_id': fields.many2one('crm.case.section', 'Sales Team'),
|
|
|
|
}
|
2012-08-24 16:13:20 +00:00
|
|
|
|
|
|
|
def default_get(self, cr, uid, fields, context=None):
|
|
|
|
res = super(crm_lead2opportunity_mass_convert, self).default_get(cr, uid, fields, context)
|
|
|
|
if 'partner_id' in fields:
|
|
|
|
# avoid forcing the partner of the first lead as default
|
|
|
|
res['partner_id'] = False
|
|
|
|
if 'action' in fields:
|
|
|
|
res['action'] = 'create'
|
|
|
|
if 'name' in fields:
|
|
|
|
res['name'] = 'convert'
|
|
|
|
if 'opportunity_ids' in fields:
|
|
|
|
res['opportunity_ids'] = False
|
|
|
|
return res
|
|
|
|
|
2011-10-25 07:39:57 +00:00
|
|
|
def _convert_opportunity(self, cr, uid, ids, vals, context=None):
|
2011-03-16 14:30:25 +00:00
|
|
|
data = self.browse(cr, uid, ids, context=context)[0]
|
2011-10-21 07:01:11 +00:00
|
|
|
salesteam_id = data.section_id and data.section_id.id or False
|
2011-10-24 13:30:10 +00:00
|
|
|
salesman = []
|
2011-03-16 14:30:25 +00:00
|
|
|
if data.user_ids:
|
2011-10-24 13:30:10 +00:00
|
|
|
salesman = [x.id for x in data.user_ids]
|
2011-10-25 07:39:57 +00:00
|
|
|
vals.update({'user_ids': salesman, 'section_id': salesteam_id})
|
|
|
|
return super(crm_lead2opportunity_mass_convert, self)._convert_opportunity(cr, uid, ids, vals, context=context)
|
|
|
|
|
|
|
|
def mass_convert(self, cr, uid, ids, context=None):
|
2011-10-21 07:01:11 +00:00
|
|
|
return self.action_apply(cr, uid, ids, context=context)
|
2012-08-23 20:06:29 +00:00
|
|
|
|
2010-03-16 11:03:51 +00:00
|
|
|
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
|