('nothing', 'Do not link to a partner')], \
'Action', required=True),
'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')]),
+ 'opportunity_ids': fields.many2many('crm.lead', string='Opportunities', domain=[('type', '=', 'opportunity')]),
}
def default_get(self, cr, uid, fields, context=None):
email = False
for lead in lead_obj.browse(cr, uid, opportunities, context=context):
partner_id = lead.partner_id and lead.partner_id.id or False
+
+ #TOFIX: use mail.mail_message.to_mail
email = re.findall(r'([^ ,<@]+@[^> ,]+)', lead.email_from or '')
email = map(lambda x: "'" + x + "'", email)
ids = lead_obj.search(cr, uid, [('partner_id', '=', partner_id), ('type', '=', 'opportunity'), '!', ('state', 'in', ['done', 'cancel'])])
if ids:
opportunities.append(ids[0])
-
-
if not partner_id:
label = False
opp_ids = []
def view_init(self, cr, uid, fields, context=None):
"""
This function checks for precondition before wizard executes
- @param self: The object pointer
- @param cr: the current row, from the database cursor,
- @param uid: the current user’s ID for security checks,
- @param fields: List of fields for default value
- @param context: A standard dictionary for contextual values
"""
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']:
raise osv.except_osv(_("Warning !"), _("Closed/Cancelled Leads can not be converted into Opportunity"))
return False
- def _convert(self, cr, uid, ids, lead, partner_id, stage_ids, context=None):
- leads = self.pool.get('crm.lead')
- address_id = False
- if partner_id:
- address_id = self.pool.get('res.partner.address').search(cr, uid,
- [('partner_id', '=', partner_id)],
- order='create_date desc',
- limit=1)
- 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,
- 'date_action': time.strftime('%Y-%m-%d %H:%M:%S'),
- }
- if partner_id and address_id:
- vals['partner_address_id'] = address_id[0]
- else:
- vals['partner_address_id'] = False
-
- lead.write(vals, context=context)
- leads.history(cr, uid, [lead], _('Converted to opportunity'), details='Converted to Opportunity', context=context)
- 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)
-
- def send_mail_to_salesman(self, lead):
- email_to = lead.user_id and lead.user_id.user_email
- if not email_to:
- return
- 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
+ def _convert_opportunity(self, cr, uid, ids, vals, context=None):
+ if context is None:
+ context = {}
+ lead = self.pool.get('crm.lead')
+ partner_id = self._create_partner(cr, uid, ids, context=context)
+ lead_ids = vals.get('lead_ids', [])
+ user_ids = vals.get('user_ids', False)
+ team_id = vals.get('section_id', False)
+ return lead.convert_opportunity(cr, uid, lead_ids, partner_id, user_ids, team_id, context=context)
+
+ def _merge_opportunity(self, cr, uid, ids, opportunity_ids, action='merge', context=None):
+ #TOFIX: is it usefully ?
+ if context is None:
+ context = {}
+ merge_opportunity = self.pool.get('crm.merge.opportunity')
+ res = False
+ #If we convert in mass, don't merge if there is no other opportunity but no warning
+ if action == 'merge' and (len(opportunity_ids) > 1 or not context.get('mass_convert') ):
+ self.write(cr, uid, ids, {'opportunity_ids' : [(6,0, [opportunity_ids[0].id])]}, context=context)
+ context.update({'lead_ids' : record_id, "convert" : True})
+ res = merge_opportunity.merge(cr, uid, data.opportunity_ids, context=context)
+ return res
def action_apply(self, cr, uid, ids, context=None):
"""
This converts lead to opportunity and opens Opportunity view
- @param ids: ids of the leads to convert to opportunities
-
- @return : View dictionary opening the Opportunity form view
"""
if not context:
context = {}
-
- record_id = context and context.get('active_ids') or False
- if not record_id:
- return {'type': 'ir.actions.act_window_close'}
-
- leads = self.pool.get('crm.lead')
- models_data = self.pool.get('ir.model.data')
-
- # Get Opportunity views
- 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
- opportunity_view_form = models_data._get_id(
- cr, uid, 'crm', 'crm_case_form_view_oppor')
- opportunity_view_tree = models_data._get_id(
- cr, uid, 'crm', 'crm_case_tree_view_oppor')
- if opportunity_view_form:
- opportunity_view_form = models_data.browse(
- cr, uid, opportunity_view_form, context=context).res_id
- if opportunity_view_tree:
- opportunity_view_tree = models_data.browse(
- cr, uid, opportunity_view_tree, context=context).res_id
-
- for lead in leads.browse(cr, uid, record_id, context=context):
- if lead.section_id:
- stage_ids = self.pool.get('crm.case.stage').search(cr, uid, [('sequence','>=',1), ('section_ids','=', lead.section_id.id)])
- else:
- stage_ids = self.pool.get('crm.case.stage').search(cr, uid, [('sequence','>=',1)])
-
- data = self.browse(cr, uid, ids[0], context=context)
-
-
- if data.action == 'create':
- partner_ids = []
- partner_ids = self._create_partner(cr, uid, ids, context=context)
- 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
-
- self._convert(cr, uid, ids, lead, partner_id, stage_ids, context=context)
- self.send_mail_to_salesman(lead)
- #If we convert in mass, don't merge if there is no other opportunity but no warning
- if data.name == 'merge' and (len(data.opportunity_ids) > 1 or not context.get('mass_convert') ):
- merge_obj = self.pool.get('crm.merge.opportunity')
- self.write(cr, uid, ids, {'opportunity_ids' : [(6,0, [data.opportunity_ids[0].id])]}, context=context)
- context.update({'lead_ids' : record_id, "convert" : True})
- return merge_obj.merge(cr, uid, data.opportunity_ids, context=context)
-
- return {
- 'name': _('Opportunity'),
- 'view_type': 'form',
- 'view_mode': 'form,tree',
- 'res_model': 'crm.lead',
- 'domain': [('type', '=', 'opportunity')],
- 'res_id': int(lead.id),
- 'view_id': False,
- 'views': [(opportunity_view_form, 'form'),
- (opportunity_view_tree, 'tree'),
- (False, 'calendar'), (False, 'graph')],
- 'type': 'ir.actions.act_window',
- 'search_view_id': opportunity_view_search
- }
+
+ lead = self.pool.get('crm.lead')
+ lead_ids = context.get('active_ids', [])
+ data = self.browse(cr, uid, ids, context=context)[0]
+ self._convert_opportunity(cr, uid, ids, {'lead_ids': lead_ids}, context=context)
+ self._merge_opportunity(cr, uid, ids, data.opportunity_ids, data.action, context=context)
+ return lead.redirect_opportunity_view(cr, uid, lead_ids[0], context=context)
crm_lead2opportunity_partner()
_columns = {
- 'user_ids': fields.many2many('res.users', 'mass_convert_rel', 'user_id', 'wizard_id', 'Salesmans'),
+ 'user_ids': fields.many2many('res.users', string='Salesmans'),
'section_id': fields.many2one('crm.case.section', 'Sales Team'),
}
-
- def mass_convert(self, cr, uid, ids, context=None):
- lead_obj = self.pool.get('crm.lead')
- if not context:
- context = {}
-
- active_ids = context.get('active_ids')
+ def _convert_opportunity(self, cr, uid, ids, vals, context=None):
data = self.browse(cr, uid, ids, context=context)[0]
-
- salesteam = data.section_id and data.section_id.id
+ salesteam_id = data.section_id and data.section_id.id or False
+ salesman = []
if data.user_ids:
- salesmans = map(lambda x : x.id, data.user_ids)
- index = 0
- else:
- salesmans = False
-
- for lead_id in active_ids:
- value = {}
- if salesteam:
- value['section_id'] = salesteam
- 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)
-
- 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)
-
- self.action_apply(cr, uid, ids, context=context)
-
-
-
- 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,
- }
+ salesman = [x.id for x in data.user_ids]
+ 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):
+ 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)
+ return self.action_apply(cr, uid, ids, context=context)
crm_lead2opportunity_mass_convert()
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: