[IMP] crm: clear apis

bzr revid: hmo@tinyerp.com-20111021070111-tzgqm0jo3gor9jvm
This commit is contained in:
Harry (OpenERP) 2011-10-21 12:31:11 +05:30
parent ae326f6909
commit c6a71b8dbb
7 changed files with 392 additions and 495 deletions

View File

@ -334,7 +334,331 @@ class crm_lead(crm_case, osv.osv):
"""
return self.set_priority(cr, uid, ids, '3')
def convert_opportunity(self, cr, uid, ids, context=None):
def _merge_data(self, cr, uid, ids, oldest, context=None):
# prepare opportunity data into dictionary for merging
opportunities = self.browse(cr, uid, ids, context=context)
def _get_first_not_null(attr):
if hasattr(oldest, attr):
return getattr(oldest, attr)
for opportunity in opportunities:
if hasattr(opportunity, attr):
return getattr(opportunity, attr)
return False
def _get_first_not_null_id(attr):
res = _get_first_not_null(attr)
return res and res.id or False
def _concat_all(attr):
return ', '.join([getattr(opportunity, attr) for opportunity in opportunities if hasattr(opportunity, attr)])
data = {
'partner_id': _get_first_not_null_id('partner_id'), # !!
'title': _get_first_not_null_id('title'),
'name' : _get_first_not_null('name'), #not lost
'categ_id' : _get_first_not_null_id('categ_id'), # !!
'channel_id' : _get_first_not_null_id('channel_id'), # !!
'city' : _get_first_not_null('city'), # !!
'company_id' : _get_first_not_null_id('company_id'), #!!
'contact_name' : _get_first_not_null('contact_name'), #not lost
'country_id' : _get_first_not_null_id('country_id'), #!!
'partner_address_id' : _get_first_not_null_id('partner_address_id'), #!!
'type_id' : _get_first_not_null_id('type_id'), #!!
'user_id' : _get_first_not_null_id('user_id'), #!!
'section_id' : _get_first_not_null_id('section_id'), #!!
'state_id' : _get_first_not_null_id('state_id'),
'description' : _concat_all('description'), #not lost
'email' : _get_first_not_null('email'), # !!
'fax' : _get_first_not_null('fax'),
'mobile' : _get_first_not_null('mobile'),
'partner_name' : _get_first_not_null('partner_name'),
'phone' : _get_first_not_null('phone'),
'probability' : _get_first_not_null('probability'),
'planned_revenue' : _get_first_not_null('planned_revenue'),
'street' : _get_first_not_null('street'),
'street2' : _get_first_not_null('street2'),
'zip' : _get_first_not_null('zip'),
'state' : 'open',
'create_date' : _get_first_not_null('create_date'),
'date_action_last': _get_first_not_null('date_action_last'),
'date_action_next': _get_first_not_null('date_action_next'),
'email_from' : _get_first_not_null('email_from'),
'email_cc' : _get_first_not_null('email_cc'),
'partner_name' : _get_first_not_null('partner_name'),
}
return data
def _merge_find_oldest(self, cr, uid, ids, context=None):
if context is None:
context = {}
if context.get('convert'):
ids = list(set(ids) - set(context.get('lead_ids', False)) )
#search opportunities order by create date
opportunity_ids = lead_obj.search(cr, uid, [('id', 'in', ids)], order='create_date' , context=context)
oldest_id = opportunity_ids[0]
return self.browse(cr, uid, oldest_id, context=context)
def _merge_notification(self, cr, uid, opportunity_id, opportunities, context=None):
#TOFIX: mail template should be used instead of fix body, subject text
details = []
merge_message = _('Merged opportunities')
subject = [merge_message]
for opportunity in opportunities:
subject.append(opportunity.name)
details.append(_('%s : %s\n Partner: %s\n Stage: %s\n Section: %s\n Salesman: %s\n Category: %s\n Channel: %s\n Company: %s\n Contact name: %s\n Email: %s\n Phone number: %s\n Fax: %s\n Mobile: %s\n State: %s\n Description: %s\n Probability: %s\n Planned revennue: %s\n Country: %s\n City: %s\n Street: %s\n Street 2: %s\n Zip 2: %s') % (merge_message, opportunity.name, opportunity.partner_id.name or '',
opportunity.stage_id.name or '',
opportunity.section_id.name or '',
opportunity.user_id.name or '',
opportunity.categ_id.name or '',
opportunity.channel_id.name or '',
opportunity.company_id.name or '',
opportunity.contact_name or '',
opportunity.email_from or '',
opportunity.phone or '',
opportunity.fax or '',
opportunity.mobile or '',
opportunity.state_id.name or '',
opportunity.description or '',
opportunity.probability or '',
opportunity.planned_revenue or '',
opportunity.country_id.name or '',
opportunity.city or '',
opportunity.street or '',
opportunity.street2 or '',
opportunity.zip or '',
))
subject = subject[0] + ", ".join(subject[1:])
details = "\n\n".join(details)
return self.message_append(cr, uid, [opportunity_id], subject, body_text=details, context=context)
def _merge_opportunity_history(self, cr, uid, opportunity_id, opportunities, context=None):
message = self.pool.get('mail.message')
for opportunity in opportunities:
for history in opportunity.message_ids:
message.write(cr, uid, history.id, {
'res_id': opportunity_id,
'subject' : _("From %s : %s") % (opportunity.name, history.subject)
}, context=context)
return True
def _merge_opportunity_attachments(self, cr, uid, opportunity_id, opportunities, context=None):
attachment = self.pool.get('ir.attachment')
# return attachments of opportunity
def _get_attachments(opportunity_id):
attachment_ids = attachment.search(cr, uid, [('res_model', '=', self._name), ('res_id', '=', opportunity_id)], context=context)
return attachment.browse(cr, uid, attachment_ids, context=context)
count = 1
first_attachments = _get_attachments(opportunity_id)
for opportunity in opportunities:
attachments = _get_attachments(opportunity.id)
for first in first_attachments:
for attachment in attachments:
if attachment.name == first.name:
values = dict(
name = "%s (%s)" % (attachment.name, count,),
res_id = opportunity_id,
)
attachment.write(values)
count+=1
return True
def merge_opportunity(self, cr, uid, ids, context=None):
"""
To merge opportunities
:param ids: list of opportunities ids to merge
"""
if context is None: context = {}
#TOCHECK: where pass lead_ids in context?
lead_ids = context and context.get('lead_ids', []) or []
if len(ids) <= 1:
raise osv.except_osv(_('Warning !'),_('Please select more than one opportunities.'))
opportunities = self.browse(cr, uid, lead_ids, context=context)
opportunities_list = list(set(ids) - set(opportunities))
oldest = self._find_oldest_opportunity(cr, uid, ids, context=context)
if opportunities :
first_opportunity = opportunities[0]
tail_opportunities = opportunities_list
else:
first_opportunity = opportunities_list[0]
tail_opportunities = opportunities_list[1:]
data = self._merge_data(cr, uid, ids, oldest, context=context)
# merge data into first opportunity
self.write(cr, uid, [first_opportunity.id], data, context=context)
#copy message and attachements into the first opportunity
self._merge_opportunity_history(cr, uid, first_opportunity.id, tail_opportunities, context=context)
self._merge_opportunity_attachments(cr, uid, first_opportunity.id, tail_opportunities, context=context)
#Notification about loss of information
self._merge_notification(cr, uid, first_opportunity, opportunities, context=context)
#delete tail opportunities
self.unlink(cr, uid, [x.id for x in tail_opportunities], context=context)
return first_opportunity.id
def _convert_opportunity_data(self, lead):
crm_stage = self.pool.get('crm.case.stage')
if lead.section_id:
stage_ids = crm_stage.search(cr, uid, [('sequence','>=',1), ('section_ids','=', lead.section_id.id)])
else:
stage_ids = crm_stage.search(cr, uid, [('sequence','>=',1)])
return {
'planned_revenue': lead.planned_revenue,
'probability': lead.probability,
'name': lead.name,
'partner_id': customer.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'),
'partner_address_id': len(customer.address_id) and customer.address_id[0] or False
}
def _convert_opportunity_notification(self, cr, uid, lead, context=context):
success_message = _("Lead '%s' has been converted to an opportunity.") % lead.name
self.message_append(cr, uid, [lead.id], success_message, body_text=success_message, context=context)
self.log(cr, uid, [lead.id], success_message)
self._send_mail_to_salesman(cr, uid, lead, context=context)
return True
def convert_opportunity(self, cr, uid, ids, partner_id, mass_convert=False, merge=False, context=None):
partner = self.pool.get('res.partner')
mail_message = self.pool.get('mail.message')
customer = partner.browse(cr, uid, partner_id, context=context)
for lead in self.browse(cr, uid, ids, context=context):
vals = self._convert_opportunity_data(lead)
self.write(cr, uid, [lead.id], vals, context=context)
self._convert_opportunity_notification(cr, uid, lead, context=context)
#TOCHECK: why need to change partner details in all messages of lead ?
if lead.partner_id:
msg_ids = [ x.id for x in lead.message_ids]
mail_message.write(cr, uid, msg_ids, {
'partner_id': lead.partner_id.id
}, context=context)
return True
def _lead_create_partner(self, cr, uid, lead, context=None):
partner = self.pool.get('res.partner')
partner_id = partner.create(cr, uid, {
'name': lead.partner_name or lead.contact_name or lead.name,
'user_id': lead.user_id.id,
'comment': lead.description,
'address': []
})
return partner_id
def _lead_assign_partner(self, cr, uid, ids, partner_id, context=None):
return self.write(cr, uid, ids, {'partner_id' : partner_id}, context=context)
def _lead_create_partner_address(self, cr, uid, lead, partner_id, context=None):
address = self.pool.get('res.partner.address')
return address.create(cr, uid, {
'partner_id': partner_id,
'name': lead.contact_name,
'phone': lead.phone,
'mobile': lead.mobile,
'email': lead.email_from,
'fax': lead.fax,
'title': lead.title and lead.title.id or False,
'function': lead.function,
'street': lead.street,
'street2': lead.street2,
'zip': lead.zip,
'city': lead.city,
'country_id': lead.country_id and lead.country_id.id or False,
'state_id': lead.state_id and lead.state_id.id or False,
})
def convert_partner(self, cr, uid, ids, action='new', partner_id=False, context=None):
"""
This function convert partner based on action.
if action is 'new', create new partner with contact and assign lead to new partner_id.
otherwise assign lead to specified partner_id
"""
if context is None:
context = {}
partner_ids = []
for lead in self.browse(cr, uid, ids, context=context):
if action == 'create':
partner_id = self._lead_create_partner(cr, uid, lead, context=context)
self._lead_assign_partner(cr, uid, [lead.id], partner_id, context=context)
partner_ids.append(partner_id)
return partner_ids
def _send_mail_to_salesman(self, cr, uid, lead, context=None):
"""
Send mail to salesman with updated Lead details.
@ lead: browse record of 'crm.lead' object.
"""
#TOFIX: mail template should be used here instead of fix subject, body text.
message = self.pool.get('mail.message')
email_to = lead.user_id and lead.user_id.user_email
if not email_to:
return False
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)
return message.schedule_with_attach(cr, uid, email_from, [email_to], subject, body)
def allocate_salesman(self, cr, uid, ids, user_ids, team_id=False, context=None):
index = 0
for lead_id in ids:
value = {}
if team_id:
value['section_id'] = team_id
if index < len(user_ids):
value['user_id'] = user_ids[index]
index += 1
if value:
self.write(cr, uid, [lead_id], value, context=context)
return True
def redirect_opportunity_view(self, cr, uid, opportunity_id, context=None):
models_data = self.pool.get('ir.model.data')
# Get Opportunity views
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
return {
'name': _('Opportunity'),
'view_type': 'form',
'view_mode': 'tree, form',
'res_model': 'crm.lead',
'domain': [('type', '=', 'opportunity')],
'res_id': int(opportunity_id),
'view_id': False,
'views': [(opportunity_view_form, 'form'),
(opportunity_view_tree, 'tree'),
(False, 'calendar'), (False, 'graph')],
'type': 'ir.actions.act_window',
}
def button_convert_opportunity(self, cr, uid, ids, context=None):
""" Precomputation for converting lead to opportunity
"""
if context is None:

View File

@ -59,7 +59,7 @@
widget="selection"
domain="[('object_id.model','=','crm.lead')]"/>
<button
name="convert_opportunity"
name="button_convert_opportunity"
string="Convert to Opportunity"
help="Convert to Opportunity" icon="gtk-go-forward"
type="object"/>

View File

@ -33,6 +33,29 @@ class res_partner(osv.osv):
'phonecall_ids': fields.one2many('crm.phonecall', 'partner_id',\
'Phonecalls'),
}
def make_opportunity(self, cr, uid, ids, opportunity, planned_revenue=0.0, probability=0.0, partner_id=None. context=None):
categ = self.pool.get('crm.case.categ')
address = self.address_get(cr, uid, ids)
categ_ids = categ.search(cr, uid, [('object_id.model','=','crm.lead')])
lead = self.pool.get('crm.lead')
opportunity_ids = []
for partner in self.browse(cr, uid, ids, context=context):
address = self.address_get(cr, uid, partner.id)
if not partner_id:
partner_id = partner.id
opportunity_id = lead.create(cr, uid, {
'name' : opportunity,
'planned_revenue' : planned_revenue,
'probability' : probability,
'partner_id' : partner_id,
'partner_address_id' : address['default'],
'categ_id' : categ_ids and categ_ids[0] or '',
'state' :'draft',
'type': 'opportunity'
})
opportunity_id.add(opportunity_id)
return opportunity_ids
res_partner()

View File

@ -93,65 +93,16 @@ class crm_lead2opportunity_partner(osv.osv_memory):
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 users 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)
text = _('Converted to opportunity')
leads.message_append(cr, uid, [lead], text, body_text=text, context=context)
if lead.partner_id:
msg_ids = [ x.id for x in lead.message_ids]
self.pool.get('mail.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, cr, uid, lead):
email_to = lead.user_id and lead.user_id.user_email
if not email_to:
return False
message_pool = self.pool.get('mail.message')
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)
return message_pool.schedule_with_attach(cr, uid, email_from, [email_to], subject, body)
def action_apply(self, cr, uid, ids, context=None):
"""
This converts lead to opportunity and opens Opportunity view
@ -161,71 +112,20 @@ class crm_lead2opportunity_partner(osv.osv_memory):
"""
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')
partner_ids = self._create_partner(cr, uid, ids, context=context)
partner_id = partner_ids and partner_ids[0] or False
leads.convert_opportunity(cr, uid, lead_ids, partner_id, context=context)
#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)
# 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(cr, uid, 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
}
return leads.redirect_opportunity_view(cr, uid, lead_ids[0], context=context)
crm_lead2opportunity_partner()
@ -242,53 +142,21 @@ class crm_lead2opportunity_mass_convert(osv.osv_memory):
}
def mass_convert(self, cr, uid, ids, context=None):
lead_obj = self.pool.get('crm.lead')
lead = self.pool.get('crm.lead')
if not context:
context = {}
active_ids = context.get('active_ids')
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
salesmans = []
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,
}
salesmans = [x.id for x in data.user_ids]
lead.allocate_salesman(cr, uid, active_ids, salesman, salesteam_id, context=context)
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:

View File

@ -25,7 +25,6 @@ import re
class crm_lead2partner(osv.osv_memory):
""" Converts lead to partner """
_name = 'crm.lead2partner'
_description = 'Lead to Partner'
@ -35,18 +34,10 @@ class crm_lead2partner(osv.osv_memory):
'Action', required=True),
'partner_id': fields.many2one('res.partner', 'Partner'),
}
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 users ID for security checks,
@param fields: List of fields for default value
@param context: A standard dictionary for contextual values
"""
lead_obj = self.pool.get('crm.lead')
rec_ids = context and context.get('active_ids', [])
for lead in lead_obj.browse(cr, uid, rec_ids, context=context):
@ -57,15 +48,7 @@ class crm_lead2partner(osv.osv_memory):
def default_get(self, cr, uid, fields, context=None):
"""
This function gets default values
@param self: The object pointer
@param cr: the current row, from the database cursor,
@param uid: the current users ID for security checks,
@param fields: List of fields for default value
@param context: A standard dictionary for contextual values
@return : default values of fields.
"""
lead_obj = self.pool.get('crm.lead')
partner_obj = self.pool.get('res.partner')
partner_id = False
@ -90,8 +73,6 @@ class crm_lead2partner(osv.osv_memory):
partner_ids = partner_obj.search(cr, uid, [('name', '=', lead.partner_name)], context=context)
partner_id = partner_ids and partner_ids[0] or False
if 'partner_id' in fields:
res.update({'partner_id': partner_id})
@ -105,17 +86,7 @@ class crm_lead2partner(osv.osv_memory):
def open_create_partner(self, cr, uid, ids, context=None):
"""
This function Opens form of create partner.
@param self: The object pointer
@param cr: the current row, from the database cursor,
@param uid: the current users ID for security checks,
@param ids: List of Lead to Partner's IDs
@param context: A standard dictionary for contextual values
@return : Dictionary value for next form.
"""
if context is None:
context = {}
view_obj = self.pool.get('ir.ui.view')
view_id = view_obj.search(cr, uid, [('model', '=', 'crm.lead2partner'), \
('name', '=', 'crm.lead2partner.view')])
@ -129,80 +100,23 @@ class crm_lead2partner(osv.osv_memory):
'target': 'new',
}
def _create_partner(self, cr, uid, ids, context=None):
"""
This function Creates partner based on action.
@param self: The object pointer
@param cr: the current row, from the database cursor,
@param uid: the current users ID for security checks,
@param ids: List of Lead to Partner's IDs
@param context: A standard dictionary for contextual values
@return : Dictionary {}.
"""
if context is None:
context = {}
lead_obj = self.pool.get('crm.lead')
partner_obj = self.pool.get('res.partner')
contact_obj = self.pool.get('res.partner.address')
partner_ids = []
partner_id = False
rec_ids = context and context.get('active_ids', [])
lead = self.pool.get('crm.lead')
lead_ids = context and context.get('active_ids') or []
for data in self.browse(cr, uid, ids, context=context):
for lead in lead_obj.browse(cr, uid, rec_ids, context=context):
if data.action == 'create':
partner_id = partner_obj.create(cr, uid, {
'name': lead.partner_name or lead.contact_name or lead.name,
'user_id': lead.user_id.id,
'comment': lead.description,
'address': []
})
contact_obj.create(cr, uid, {
'partner_id': partner_id,
'name': lead.contact_name,
'phone': lead.phone,
'mobile': lead.mobile,
'email': lead.email_from,
'fax': lead.fax,
'title': lead.title and lead.title.id or False,
'function': lead.function,
'street': lead.street,
'street2': lead.street2,
'zip': lead.zip,
'city': lead.city,
'country_id': lead.country_id and lead.country_id.id or False,
'state_id': lead.state_id and lead.state_id.id or False,
})
else:
if data.partner_id:
partner_id = data.partner_id.id
self.assign_partner(cr, uid, lead.id, partner_id)
partner_ids.append(partner_id)
partner_id = data.partner_id and data.partner_id.id or False
partner_ids += lead.convert_partner(cr, uid, lead_ids, data.action, partner_id, context=context)
return partner_ids
def assign_partner(self, cr, uid, lead_id, partner_id):
self.pool.get("crm.lead").write(cr, uid, [lead_id], {'partner_id' : partner_id})
def make_partner(self, cr, uid, ids, context=None):
"""
This function Makes partner based on action.
@param self: The object pointer
@param cr: the current row, from the database cursor,
@param uid: the current users ID for security checks,
@param ids: List of Lead to Partner's IDs
@param context: A standard dictionary for contextual values
@return : Dictionary value for created Partner form.
"""
if context is None:
context = {}
partner_ids = self._create_partner(cr, uid, ids, context=context)
return {'type': 'ir.actions.act_window_close'}

View File

@ -26,195 +26,16 @@ class crm_merge_opportunity(osv.osv_memory):
_name = 'crm.merge.opportunity'
_description = 'Merge two Opportunities'
def _get_first_not_null_id(self, attr, ops, oldest):
if hasattr(oldest, attr) and getattr(oldest, attr):
return getattr(oldest, attr).id
for op in ops:
if hasattr(op, attr) and getattr(op, attr):
return getattr(op, attr).id
return False
def _get_first_not_null(self, attr, ops, oldest):
if hasattr(oldest, attr) and getattr(oldest, attr):
return getattr(oldest, attr)
for op in ops:
if hasattr(op, attr) and getattr(op, attr):
return getattr(op, attr)
return False
def _concat_all(self, attr, ops):
return ', '.join([getattr(op, attr) for op in ops if hasattr(op, attr) and getattr(op, attr)])
def get_attachments(self, cr, uid, id, context=None):
proxy = self.pool.get('ir.attachment')
ids = proxy.search(cr, uid, [('res_model', '=', 'crm.lead'), ('res_id', '=', id)], context=context)
return proxy.browse(cr, uid, ids, context=context)
def find_oldest(self, cr, uid, op_ids, context=None):
if not context:
context = {}
ids = [op_id.id for op_id in op_ids]
if context.get('convert'):
ids = list(set(ids) - set(context.get('lead_ids', False)) )
lead_obj = self.pool.get('crm.lead')
op_id = lead_obj.search(cr, uid, [('id', 'in', ids)], order='create_date' , context=context)
if not op_id:
return False
opps = lead_obj.browse(cr, uid, [op_id[0]], context=context)
return opps[0]
def _update_data(self, op_ids, oldest_opp):
data = {
'partner_id': self._get_first_not_null_id('partner_id', op_ids, oldest_opp), # !!
'title': self._get_first_not_null_id('title', op_ids, oldest_opp),
'name' : self._get_first_not_null('name', op_ids, oldest_opp), #not lost
'categ_id' : self._get_first_not_null_id('categ_id', op_ids, oldest_opp), # !!
'channel_id' : self._get_first_not_null_id('channel_id', op_ids, oldest_opp), # !!
'city' : self._get_first_not_null('city', op_ids, oldest_opp), # !!
'company_id' : self._get_first_not_null_id('company_id', op_ids, oldest_opp), #!!
'contact_name' : self._get_first_not_null('contact_name', op_ids, oldest_opp), #not lost
'country_id' : self._get_first_not_null_id('country_id', op_ids, oldest_opp), #!!
'partner_address_id' : self._get_first_not_null_id('partner_address_id', op_ids, oldest_opp), #!!
'type_id' : self._get_first_not_null_id('type_id', op_ids, oldest_opp), #!!
'user_id' : self._get_first_not_null_id('user_id', op_ids, oldest_opp), #!!
'section_id' : self._get_first_not_null_id('section_id', op_ids, oldest_opp), #!!
'state_id' : self._get_first_not_null_id('state_id', op_ids, oldest_opp),
'description' : self._concat_all('description', op_ids), #not lost
'email' : self._get_first_not_null('email', op_ids, oldest_opp), # !!
'fax' : self._get_first_not_null('fax', op_ids, oldest_opp),
'mobile' : self._get_first_not_null('mobile', op_ids, oldest_opp),
'partner_name' : self._get_first_not_null('partner_name', op_ids, oldest_opp),
'phone' : self._get_first_not_null('phone', op_ids, oldest_opp),
'probability' : self._get_first_not_null('probability', op_ids, oldest_opp),
'planned_revenue' : self._get_first_not_null('planned_revenue', op_ids, oldest_opp),
'street' : self._get_first_not_null('street', op_ids, oldest_opp),
'street2' : self._get_first_not_null('street2', op_ids, oldest_opp),
'zip' : self._get_first_not_null('zip', op_ids, oldest_opp),
'state' : 'open',
'create_date' : self._get_first_not_null('create_date', op_ids, oldest_opp),
'date_action_last': self._get_first_not_null('date_action_last', op_ids, oldest_opp),
'date_action_next': self._get_first_not_null('date_action_next', op_ids, oldest_opp),
'email_from' : self._get_first_not_null('email_from', op_ids, oldest_opp),
'email_cc' : self._get_first_not_null('email_cc', op_ids, oldest_opp),
'partner_name' : self._get_first_not_null('partner_name', op_ids, oldest_opp),
}
return data
def merge(self, cr, uid, op_ids, context=None):
"""
:param opp_ids: list of opportunities ids to merge
"""
opp_obj = self.pool.get('crm.lead')
message_obj = self.pool.get('mail.message')
lead_ids = context and context.get('lead_ids', []) or []
if len(op_ids) <= 1:
raise osv.except_osv(_('Warning !'),_('Please select more than one opportunities.'))
opportunities = opp_obj.browse(cr, uid, lead_ids, context=context)
opportunities_list = list(set(op_ids) - set(opportunities))
oldest_opp = self.find_oldest(cr, uid, op_ids, context=context)
if opportunities :
first_opportunity = opportunities[0]
tail_opportunities = opportunities_list
else:
first_opportunity = opportunities_list[0]
tail_opportunities = opportunities_list[1:]
data = self._update_data(op_ids, oldest_opp)
#copy message into the first opportunity + merge attachement
count = 1
first_attachments = self.get_attachments(cr, uid, first_opportunity, context=context)
for opp in tail_opportunities:
attachments = self.get_attachments(cr, uid, opp, context=context)
for first in first_attachments:
for attachment in attachments:
if attachment.name == first.name:
values = dict(
name = "%s (%s)" % (attachment.name, count,),
res_id = first_opportunity.id,
)
attachment.write(values)
count+=1
for history in opp.message_ids:
message_obj.write(cr, uid, history.id, {'res_id': first_opportunity.id, 'subject' : _("From %s : %s") % (opp.name, history.subject) }, context=context)
#Notification about loss of information
details = []
subject = ['Merged opportunities :']
for opp in op_ids:
subject.append(opp.name)
details.append(_('Merged Opportunity: %s\n Partner: %s\n Stage: %s\n Section: %s\n Salesman: %s\n Category: %s\n Channel: %s\n Company: %s\n Contact name: %s\n Email: %s\n Phone number: %s\n Fax: %s\n Mobile: %s\n State: %s\n Description: %s\n Probability: %s\n Planned revennue: %s\n Country: %s\n City: %s\n Street: %s\n Street 2: %s\n Zip 2: %s') % ( opp.name, opp.partner_id.name or '',
opp.stage_id.name or '',
opp.section_id.name or '',
opp.user_id.name or '',
opp.categ_id.name or '',
opp.channel_id.name or '',
opp.company_id.name or '',
opp.contact_name or '',
opp.email_from or '',
opp.phone or '',
opp.fax or '',
opp.mobile or '',
opp.state_id.name or '',
opp.description or '',
opp.probability or '',
opp.planned_revenue or '',
opp.country_id.name or '',
opp.city or '',
opp.street or '',
opp.street2 or '',
opp.zip or '',
))
subject = subject[0] + ", ".join(subject[1:])
details = "\n\n".join(details)
opp_obj.message_append(cr, uid, [first_opportunity], subject, body_text=details)
#data.update({'message_ids' : [(6, 0 ,self._concat_o2m('message_ids', op_ids))]})
opp_obj.write(cr, uid, [first_opportunity.id], data, context=context)
unlink_ids = map(lambda x: x.id, tail_opportunities)
opp_obj.unlink(cr, uid, unlink_ids, context=context)
models_data = self.pool.get('ir.model.data')
# Get Opportunity views
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
return {
'name': _('Opportunity'),
'view_type': 'form',
'view_mode': 'tree, form',
'res_model': 'crm.lead',
'domain': [('type', '=', 'opportunity')],
'res_id': int(first_opportunity.id),
'view_id': False,
'views': [(opportunity_view_form, 'form'),
(opportunity_view_tree, 'tree'),
(False, 'calendar'), (False, 'graph')],
'type': 'ir.actions.act_window',
}
def action_merge(self, cr, uid, ids, context=None):
obj_opportunity = self.browse(cr, uid, ids[0], context=context)
op_ids = obj_opportunity.opportunity_ids
self.write(cr, uid, ids, {'opportunity_ids' : [(6,0, [op_ids[0].id])]}, context=context)
context['lead_ids'] = [op_ids[0].id]
return self.merge(cr, uid, op_ids, context)
lead = self.pool.get('crm.lead')
record = self.browse(cr, uid, ids[0], context=context)
opportunities = record.opportunity_ids
#TOFIX: why need to check lead_ids here
lead_ids = [opportunities[0].id]
self.write(cr, uid, ids, {'opportunity_ids' : [(6,0, lead_ids)]}, context=context)
context['lead_ids'] = lead_ids
merge_id = lead.merge_opportunity(cr, uid, [x.id for x in opportunities], context=context)
return lead.redirect_opportunity_view(cr, uid, merge_id, context=context)
_columns = {
'opportunity_ids' : fields.many2many('crm.lead', 'merge_opportunity_rel', 'merge_id', 'opportunity_id', 'Opportunities', domain=[('type', '=', 'opportunity')]),
@ -223,13 +44,6 @@ class crm_merge_opportunity(osv.osv_memory):
def default_get(self, cr, uid, fields, context=None):
"""
This function gets default values
@param self: The object pointer
@param cr: the current row, from the database cursor,
@param uid: the current users ID for security checks,
@param fields: List of fields for default value
@param context: A standard dictionary for contextual values
@return : default values of fields.
"""
record_ids = context and context.get('active_ids', False) or False
res = super(crm_merge_opportunity, self).default_get(cr, uid, fields, context=context)

View File

@ -38,13 +38,6 @@ class crm_partner2opportunity(osv.osv_memory):
def default_get(self, cr, uid, fields, context=None):
"""
This function gets default values
@param self: The object pointer
@param cr: the current row, from the database cursor,
@param uid: the current users ID for security checks,
@param fields: List of fields for default value
@param context: A standard dictionary for contextual values
@return : default values of fields.
"""
partner_obj = self.pool.get('res.partner')
data = context and context.get('active_ids', []) or []
@ -58,56 +51,17 @@ class crm_partner2opportunity(osv.osv_memory):
return res
def make_opportunity(self, cr, uid, ids, context=None):
"""
@param self: The object pointer
@param cr: the current row, from the database cursor,
@param uid: the current users ID for security checks,
@param context: A standard dictionary for contextual values
"""
data = context and context.get('active_ids', []) or []
make_opportunity = self.pool.get('crm.partner2opportunity')
data_obj = self.pool.get('ir.model.data')
part_obj = self.pool.get('res.partner')
categ_obj = self.pool.get('crm.case.categ')
case_obj = self.pool.get('crm.lead')
for make_opportunity_obj in make_opportunity.browse(cr, uid, ids, context=context):
result = data_obj._get_id(cr, uid, 'crm', 'view_crm_case_opportunities_filter')
res = data_obj.read(cr, uid, result, ['res_id'])
id2 = data_obj._get_id(cr, uid, 'crm', 'crm_case_form_view_oppor')
id3 = data_obj._get_id(cr, uid, 'crm', 'crm_case_tree_view_oppor')
if id2:
id2 = data_obj.browse(cr, uid, id2, context=context).res_id
if id3:
id3 = data_obj.browse(cr, uid, id3, context=context).res_id
address = part_obj.address_get(cr, uid, data)
categ_ids = categ_obj.search(cr, uid, [('object_id.model','=','crm.lead')])
opp_id = case_obj.create(cr, uid, {
'name' : make_opportunity_obj.name,
'planned_revenue' : make_opportunity_obj.planned_revenue,
'probability' : make_opportunity_obj.probability,
'partner_id' : make_opportunity_obj.partner_id.id,
'partner_address_id' : address['default'],
'categ_id' : categ_ids and categ_ids[0] or '',
'state' :'draft',
'type': 'opportunity'
partner_ids = context and context.get('active_ids', []) or []
partner = self.pool.get('res.partner')
lead = self.pool.get('crm.lead')
for data in self.browse(cr, uid, ids, context=context):
opportunity_ids = partner.make_opportunity(cr, uid, partner_ids,
data.name,
data.planned_revenue,
data.probability,
})
value = {
'name' : _('Opportunity'),
'view_type' : 'form',
'view_mode' : 'form,tree',
'res_model' : 'crm.lead',
'res_id' : opp_id,
'view_id' : False,
'views' : [(id2, 'form'), (id3, 'tree'), (False, 'calendar'), (False, 'graph')],
'type' : 'ir.actions.act_window',
'search_view_id' : res['res_id']
}
return value
opportunity_id = len(opportunity_ids) and opportunity_ids[0] or False
return lead.redirect_opportunity_view(cr, uid, opportunity_id, context=context)
crm_partner2opportunity()