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
|
2010-03-25 06:19:40 +00:00
|
|
|
|
|
2011-02-04 13:05:56 +00:00
|
|
|
|
import time
|
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 = {
|
2011-02-09 14:15:45 +00:00
|
|
|
|
'action': fields.selection([('exist', 'Link to an existing partner'), \
|
|
|
|
|
('create', 'Create a new partner'), \
|
|
|
|
|
('nothing', 'Do not link to a partner')], \
|
|
|
|
|
'Action', required=True),
|
2011-02-04 13:05:56 +00:00
|
|
|
|
'name': fields.selection([('convert', 'Convert to Opportunity'), ('merge', 'Merge with existing Opportunity')],'Select Action', required=True),
|
|
|
|
|
'opportunity_ids': fields.many2many('crm.lead', 'merge_opportunity_rel', 'merge_id', 'opportunity_id', 'Opportunities', domain=[('type', '=', 'opportunity')]),
|
|
|
|
|
}
|
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
|
|
|
|
"""
|
|
|
|
|
Default get for name, opportunity_ids
|
2011-02-17 12:57:31 +00:00
|
|
|
|
if there is an exisitng partner link to the lead, find all existing opportunity link with this partnet to merge
|
2011-02-08 13:36:40 +00:00
|
|
|
|
all information together
|
|
|
|
|
"""
|
|
|
|
|
lead_obj = self.pool.get('crm.lead')
|
2011-02-06 21:28:34 +00:00
|
|
|
|
|
2011-02-17 12:57:31 +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-17 12:57:31 +00:00
|
|
|
|
|
2011-02-09 14:15:45 +00:00
|
|
|
|
partner_id = False
|
|
|
|
|
for lead in lead_obj.browse(cr, uid, opportunities, context=context):
|
|
|
|
|
partner_id = lead.partner_id and lead.partner_id.id or False
|
|
|
|
|
|
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:
|
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-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})
|
2011-02-17 12:57:31 +00:00
|
|
|
|
|
2011-02-06 21:28:34 +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
|
|
|
|
"""
|
2011-02-04 13:05:56 +00:00
|
|
|
|
This function checks for precondition before wizard executes
|
2010-03-16 11:03:51 +00:00
|
|
|
|
@param self: The object pointer
|
|
|
|
|
@param cr: the current row, from the database cursor,
|
|
|
|
|
@param uid: the current user’s ID for security checks,
|
2011-02-04 13:05:56 +00:00
|
|
|
|
@param fields: List of fields for default value
|
2010-03-16 11:03:51 +00:00
|
|
|
|
@param context: A standard dictionary for contextual values
|
|
|
|
|
|
|
|
|
|
"""
|
2011-02-04 13:05:56 +00:00
|
|
|
|
if context is None:
|
|
|
|
|
context = {}
|
|
|
|
|
lead_obj = self.pool.get('crm.lead')
|
2010-11-08 14:22:51 +00:00
|
|
|
|
|
2011-02-04 13:05:56 +00:00
|
|
|
|
for lead in lead_obj.browse(cr, uid, context.get('active_ids', []), context=context):
|
|
|
|
|
if lead.state in ['done', 'cancel']:
|
|
|
|
|
raise osv.except_osv(_("Warning !"), _("Closed/Cancelled \
|
|
|
|
|
Leads Could not convert into Opportunity"))
|
|
|
|
|
return False
|
2011-02-17 12:57:31 +00:00
|
|
|
|
|
2011-02-06 18:54:59 +00:00
|
|
|
|
def _convert(self, cr, uid, ids, lead, partner_id, stage_ids, context=None):
|
|
|
|
|
leads = self.pool.get('crm.lead')
|
2011-03-24 16:09:29 +00:00
|
|
|
|
address_id = False
|
|
|
|
|
if partner_id:
|
|
|
|
|
address_id = self.pool.get('res.partner.address').search(cr, uid,
|
2011-02-22 16:24:03 +00:00
|
|
|
|
[('partner_id', '=', partner_id)],
|
|
|
|
|
order='create_date desc',
|
|
|
|
|
limit=1)
|
2011-02-06 18:54:59 +00:00
|
|
|
|
vals = {
|
|
|
|
|
'planned_revenue': lead.planned_revenue,
|
|
|
|
|
'probability': lead.probability,
|
|
|
|
|
'name': lead.name,
|
|
|
|
|
'partner_id': partner_id,
|
|
|
|
|
'user_id': (lead.user_id and lead.user_id.id),
|
|
|
|
|
'type': 'opportunity',
|
|
|
|
|
'stage_id': stage_ids and stage_ids[0] or False,
|
2011-02-22 16:24:03 +00:00
|
|
|
|
'date_action': time.strftime('%Y-%m-%d %H:%M:%S'),
|
2011-02-06 18:54:59 +00:00
|
|
|
|
}
|
2011-03-30 11:43:24 +00:00
|
|
|
|
if partner_id and address_id:
|
2011-02-22 16:24:03 +00:00
|
|
|
|
vals['partner_address_id'] = address_id[0]
|
2011-03-30 11:43:24 +00:00
|
|
|
|
else:
|
|
|
|
|
vals['partner_address_id'] = False
|
2011-02-22 16:24:03 +00:00
|
|
|
|
|
2011-02-06 18:54:59 +00:00
|
|
|
|
lead.write(vals, context=context)
|
2011-02-08 13:36:40 +00:00
|
|
|
|
leads.history(cr, uid, [lead], _('Converted to opportunity'), details='Converted to Opportunity', context=context)
|
2011-02-06 18:54:59 +00:00
|
|
|
|
if lead.partner_id:
|
|
|
|
|
msg_ids = [ x.id for x in lead.message_ids]
|
|
|
|
|
self.pool.get('mailgate.message').write(cr, uid, msg_ids, {
|
|
|
|
|
'partner_id': lead.partner_id.id
|
|
|
|
|
}, context=context)
|
|
|
|
|
leads.log(cr, uid, lead.id, _("Lead '%s' has been converted to an opportunity.") % lead.name)
|
2011-02-17 12:57:31 +00:00
|
|
|
|
|
2011-03-29 15:19:09 +00:00
|
|
|
|
def send_mail_to_salesman(self, lead):
|
|
|
|
|
email_to = lead.user_id and lead.user_id.user_email
|
2011-03-30 11:43:24 +00:00
|
|
|
|
if not email_to:
|
|
|
|
|
return
|
2011-03-29 15:19:09 +00:00
|
|
|
|
email_from = lead.section_id and lead.section_id.user_id and lead.section_id.user_id.user_email or email_to
|
|
|
|
|
partner = lead.partner_id and lead.partner_id.name or lead.partner_name
|
|
|
|
|
subject = "lead %s converted into opportunity" % lead.name
|
|
|
|
|
body = "Info \n Id : %s \n Subject: %s \n Partner: %s \n Description : %s " % (lead.id, lead.name, lead.partner_id.name, lead.description)
|
|
|
|
|
try :
|
|
|
|
|
tools.email_send(email_from, email_to, subject, body)
|
|
|
|
|
except:
|
|
|
|
|
pass
|
2011-02-17 12:57:31 +00:00
|
|
|
|
|
2010-03-16 11:03:51 +00:00
|
|
|
|
def action_apply(self, cr, uid, ids, context=None):
|
|
|
|
|
"""
|
|
|
|
|
This converts lead to opportunity and opens Opportunity view
|
2010-11-08 14:25:00 +00:00
|
|
|
|
@param ids: ids of the leads to convert to opportunities
|
2010-03-16 11:03:51 +00:00
|
|
|
|
|
2010-11-08 14:25:00 +00:00
|
|
|
|
@return : View dictionary opening the Opportunity form view
|
2010-03-16 11:03:51 +00:00
|
|
|
|
"""
|
2011-03-16 14:30:25 +00:00
|
|
|
|
if not context:
|
|
|
|
|
context = {}
|
2011-03-24 16:09:29 +00:00
|
|
|
|
|
2011-02-04 13:05:56 +00:00
|
|
|
|
record_id = context and context.get('active_ids') or False
|
2010-04-22 11:46:41 +00:00
|
|
|
|
if not record_id:
|
2010-12-28 10:44:45 +00:00
|
|
|
|
return {'type': 'ir.actions.act_window_close'}
|
2010-03-18 11:23:49 +00:00
|
|
|
|
|
2010-11-08 14:41:20 +00:00
|
|
|
|
leads = self.pool.get('crm.lead')
|
|
|
|
|
models_data = self.pool.get('ir.model.data')
|
2010-03-18 11:23:49 +00:00
|
|
|
|
|
2010-04-22 11:46:41 +00:00
|
|
|
|
# Get Opportunity views
|
2010-11-08 14:45:27 +00:00
|
|
|
|
result = models_data._get_id(
|
|
|
|
|
cr, uid, 'crm', 'view_crm_case_opportunities_filter')
|
2010-11-08 14:41:20 +00:00
|
|
|
|
opportunity_view_search = models_data.browse(
|
|
|
|
|
cr, uid, result, context=context).res_id
|
|
|
|
|
opportunity_view_form = models_data._get_id(
|
2010-11-08 14:31:16 +00:00
|
|
|
|
cr, uid, 'crm', 'crm_case_form_view_oppor')
|
2010-11-08 14:41:20 +00:00
|
|
|
|
opportunity_view_tree = models_data._get_id(
|
2010-11-08 14:31:16 +00:00
|
|
|
|
cr, uid, 'crm', 'crm_case_tree_view_oppor')
|
|
|
|
|
if opportunity_view_form:
|
2010-11-08 14:41:20 +00:00
|
|
|
|
opportunity_view_form = models_data.browse(
|
2010-11-08 14:31:16 +00:00
|
|
|
|
cr, uid, opportunity_view_form, context=context).res_id
|
|
|
|
|
if opportunity_view_tree:
|
2010-11-08 14:41:20 +00:00
|
|
|
|
opportunity_view_tree = models_data.browse(
|
2010-11-08 14:31:16 +00:00
|
|
|
|
cr, uid, opportunity_view_tree, context=context).res_id
|
2010-03-23 06:18:25 +00:00
|
|
|
|
|
2011-02-04 13:05:56 +00:00
|
|
|
|
for lead in leads.browse(cr, uid, record_id, context=context):
|
2011-03-16 14:30:25 +00:00
|
|
|
|
if lead.section_id:
|
2011-02-04 13:05:56 +00:00
|
|
|
|
stage_ids = self.pool.get('crm.case.stage').search(cr, uid, [('type','=','opportunity'),('sequence','>=',1), ('section_ids','=', lead.section_id.id)])
|
|
|
|
|
else:
|
|
|
|
|
stage_ids = self.pool.get('crm.case.stage').search(cr, uid, [('type','=','opportunity'),('sequence','>=',1)])
|
2011-02-17 12:57:31 +00:00
|
|
|
|
|
2011-02-04 13:05:56 +00:00
|
|
|
|
data = self.browse(cr, uid, ids[0], context=context)
|
2011-03-29 08:48:22 +00:00
|
|
|
|
|
|
|
|
|
|
2011-02-04 13:05:56 +00:00
|
|
|
|
if data.action == 'create':
|
2011-03-29 08:48:22 +00:00
|
|
|
|
partner_ids = []
|
2011-02-04 13:05:56 +00:00
|
|
|
|
partner_ids = self._create_partner(cr, uid, ids, context=context)
|
2011-03-29 08:48:22 +00:00
|
|
|
|
partner_id = partner_ids and partner_ids[0]
|
|
|
|
|
elif data.action == 'exist':
|
|
|
|
|
partner_id = data.partner_id and data.partner_id.id
|
|
|
|
|
else:
|
|
|
|
|
partner_id = False
|
|
|
|
|
|
2011-02-06 18:54:59 +00:00
|
|
|
|
self._convert(cr, uid, ids, lead, partner_id, stage_ids, context=context)
|
2011-03-29 15:19:09 +00:00
|
|
|
|
self.send_mail_to_salesman(lead)
|
2011-03-16 14:30:25 +00:00
|
|
|
|
#If we convert in mass, don't merge if there is no other opportunity but no warning
|
2011-03-24 16:09:29 +00:00
|
|
|
|
if data.name == 'merge' and (len(data.opportunity_ids) > 1 or not context.get('mass_convert') ):
|
2011-02-04 13:05:56 +00:00
|
|
|
|
merge_obj = self.pool.get('crm.merge.opportunity')
|
2011-02-17 12:57:31 +00:00
|
|
|
|
self.write(cr, uid, ids, {'opportunity_ids' : [(6,0, [data.opportunity_ids[0].id])]}, context=context)
|
2011-02-17 15:35:43 +00:00
|
|
|
|
context.update({'lead_ids' : record_id})
|
2011-02-09 14:15:45 +00:00
|
|
|
|
return merge_obj.merge(cr, uid, data.opportunity_ids, context=context)
|
2010-11-08 14:31:16 +00:00
|
|
|
|
|
2010-11-08 14:25:00 +00:00
|
|
|
|
return {
|
2010-11-08 14:22:51 +00:00
|
|
|
|
'name': _('Opportunity'),
|
|
|
|
|
'view_type': 'form',
|
|
|
|
|
'view_mode': 'form,tree',
|
|
|
|
|
'res_model': 'crm.lead',
|
|
|
|
|
'domain': [('type', '=', 'opportunity')],
|
|
|
|
|
'res_id': int(lead.id),
|
|
|
|
|
'view_id': False,
|
2010-11-08 14:31:16 +00:00
|
|
|
|
'views': [(opportunity_view_form, 'form'),
|
|
|
|
|
(opportunity_view_tree, 'tree'),
|
|
|
|
|
(False, 'calendar'), (False, 'graph')],
|
2010-11-08 14:22:51 +00:00
|
|
|
|
'type': 'ir.actions.act_window',
|
2010-11-08 14:41:20 +00:00
|
|
|
|
'search_view_id': opportunity_view_search
|
2010-04-22 11:46:41 +00:00
|
|
|
|
}
|
2011-02-17 12:57:31 +00:00
|
|
|
|
|
2010-03-25 12:18:35 +00:00
|
|
|
|
crm_lead2opportunity_partner()
|
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 = {
|
|
|
|
|
'user_ids': fields.many2many('res.users', 'mass_convert_rel', 'user_id', 'wizard_id', 'Salesmans'),
|
|
|
|
|
'section_id': fields.many2one('crm.case.section', 'Sales Team'),
|
2011-03-24 16:09:29 +00:00
|
|
|
|
|
2011-03-16 14:30:25 +00:00
|
|
|
|
}
|
2011-03-24 16:09:29 +00:00
|
|
|
|
|
2011-03-16 14:30:25 +00:00
|
|
|
|
def mass_convert(self, cr, uid, ids, context=None):
|
|
|
|
|
lead_obj = self.pool.get('crm.lead')
|
|
|
|
|
if not context:
|
|
|
|
|
context = {}
|
2011-03-24 16:09:29 +00:00
|
|
|
|
|
2011-03-16 14:30:25 +00:00
|
|
|
|
active_ids = context.get('active_ids')
|
|
|
|
|
data = self.browse(cr, uid, ids, context=context)[0]
|
2011-03-24 16:09:29 +00:00
|
|
|
|
|
2011-03-16 14:30:25 +00:00
|
|
|
|
salesteam = data.section_id and data.section_id.id
|
|
|
|
|
if data.user_ids:
|
|
|
|
|
salesmans = map(lambda x : x.id, data.user_ids)
|
|
|
|
|
index = 0
|
|
|
|
|
else:
|
|
|
|
|
salesmans = False
|
2011-03-24 16:09:29 +00:00
|
|
|
|
|
2011-03-16 14:30:25 +00:00
|
|
|
|
for lead_id in active_ids:
|
|
|
|
|
value = {}
|
|
|
|
|
if salesteam:
|
2011-03-24 16:09:29 +00:00
|
|
|
|
value['section_id'] = salesteam
|
2011-03-16 14:30:25 +00:00
|
|
|
|
if salesmans:
|
|
|
|
|
value['user_id'] = salesmans[index]
|
|
|
|
|
index += 1
|
|
|
|
|
index = index < len(salesmans) and index or 0
|
|
|
|
|
if value:
|
|
|
|
|
lead_obj.write(cr, uid, [lead_id], value, context=context)
|
2011-03-24 16:09:29 +00:00
|
|
|
|
|
2011-03-16 14:30:25 +00:00
|
|
|
|
context['active_ids'] = [lead_id]
|
|
|
|
|
value = self.default_get(cr, uid, ['partner_id', 'opportunity_ids'], context=context)
|
|
|
|
|
value['opportunity_ids'] = [(6, 0, value['opportunity_ids'])]
|
|
|
|
|
self.write(cr, uid, ids, value, context=context)
|
2011-03-24 16:09:29 +00:00
|
|
|
|
|
2011-03-16 14:30:25 +00:00
|
|
|
|
self.action_apply(cr, uid, ids, context=context)
|
2011-03-24 16:09:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-03-16 14:30:25 +00:00
|
|
|
|
models_data = self.pool.get('ir.model.data')
|
|
|
|
|
result = models_data._get_id(cr, uid, 'crm', 'view_crm_case_opportunities_filter')
|
|
|
|
|
opportunity_view_search = models_data.browse(cr, uid, result, context=context).res_id
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
'name': _('Opportunity'),
|
|
|
|
|
'view_type': 'form',
|
|
|
|
|
'view_mode': 'tree,form',
|
|
|
|
|
'res_model': 'crm.lead',
|
|
|
|
|
'domain': [('type', '=', 'opportunity'), ('id', 'in', active_ids)],
|
|
|
|
|
'type': 'ir.actions.act_window',
|
|
|
|
|
'search_view_id': opportunity_view_search,
|
|
|
|
|
}
|
2011-03-24 16:09:29 +00:00
|
|
|
|
|
2011-03-16 14:30:25 +00:00
|
|
|
|
crm_lead2opportunity_mass_convert()
|
2010-03-16 11:03:51 +00:00
|
|
|
|
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
|