[FIX] delete unlink opportunity in field opportunity_ids in wizard

bzr revid: tfr@openerp.com-20110217125731-9qp6uw5srjl7ibzo
This commit is contained in:
Thibault Francois 2011-02-17 13:57:31 +01:00
parent f708bfe135
commit d972df33da
2 changed files with 65 additions and 64 deletions

View File

@ -37,31 +37,31 @@ class crm_lead2opportunity_partner(osv.osv_memory):
'name': fields.selection([('convert', 'Convert to Opportunity'), ('merge', 'Merge with existing Opportunity')],'Select 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', 'merge_opportunity_rel', 'merge_id', 'opportunity_id', 'Opportunities', domain=[('type', '=', 'opportunity')]),
} }
def default_get(self, cr, uid, fields, context=None): def default_get(self, cr, uid, fields, context=None):
""" """
Default get for name, opportunity_ids Default get for name, opportunity_ids
if there is an exisitng partner link to the lead, find all existing opportunity link with this partnet to merge if there is an exisitng partner link to the lead, find all existing opportunity link with this partnet to merge
all information together all information together
""" """
lead_obj = self.pool.get('crm.lead') lead_obj = self.pool.get('crm.lead')
res = super(crm_lead2opportunity_partner, self).default_get(cr, uid, fields, context=context) res = super(crm_lead2opportunity_partner, self).default_get(cr, uid, fields, context=context)
opportunities = res.get('opportunity_ids') or [] opportunities = res.get('opportunity_ids') or []
partner_id = False partner_id = False
for lead in lead_obj.browse(cr, uid, opportunities, context=context): for lead in lead_obj.browse(cr, uid, opportunities, context=context):
partner_id = lead.partner_id and lead.partner_id.id or False partner_id = lead.partner_id and lead.partner_id.id or False
if not partner_id and res.get('partner_id'): if not partner_id and res.get('partner_id'):
partner_id = res.get('partner_id') partner_id = res.get('partner_id')
ids = [] ids = []
if partner_id: if partner_id:
ids = lead_obj.search(cr, uid, [('partner_id', '=', partner_id), ('type', '=', 'opportunity')]) ids = lead_obj.search(cr, uid, [('partner_id', '=', partner_id), ('type', '=', 'opportunity')])
opportunities += ids opportunities += ids
if 'action' in fields: if 'action' in fields:
res.update({'action' : partner_id and 'exist' or 'create'}) res.update({'action' : partner_id and 'exist' or 'create'})
if 'partner_id' in fields: if 'partner_id' in fields:
@ -70,10 +70,10 @@ class crm_lead2opportunity_partner(osv.osv_memory):
res.update({'name' : ids and 'merge' or 'convert'}) res.update({'name' : ids and 'merge' or 'convert'})
if 'opportunity_ids' in fields: if 'opportunity_ids' in fields:
res.update({'opportunity_ids': opportunities}) res.update({'opportunity_ids': opportunities})
return res return res
def view_init(self, cr, uid, fields, context=None): def view_init(self, cr, uid, fields, context=None):
""" """
This function checks for precondition before wizard executes This function checks for precondition before wizard executes
@ -93,7 +93,7 @@ class crm_lead2opportunity_partner(osv.osv_memory):
raise osv.except_osv(_("Warning !"), _("Closed/Cancelled \ raise osv.except_osv(_("Warning !"), _("Closed/Cancelled \
Leads Could not convert into Opportunity")) Leads Could not convert into Opportunity"))
return False return False
def _convert(self, cr, uid, ids, lead, partner_id, stage_ids, context=None): def _convert(self, cr, uid, ids, lead, partner_id, stage_ids, context=None):
leads = self.pool.get('crm.lead') leads = self.pool.get('crm.lead')
vals = { vals = {
@ -114,8 +114,8 @@ Leads Could not convert into Opportunity"))
'partner_id': lead.partner_id.id 'partner_id': lead.partner_id.id
}, context=context) }, context=context)
leads.log(cr, uid, lead.id, _("Lead '%s' has been converted to an opportunity.") % lead.name) leads.log(cr, uid, lead.id, _("Lead '%s' has been converted to an opportunity.") % lead.name)
def action_apply(self, cr, uid, ids, context=None): def action_apply(self, cr, uid, ids, context=None):
""" """
This converts lead to opportunity and opens Opportunity view This converts lead to opportunity and opens Opportunity view
@ -151,16 +151,17 @@ Leads Could not convert into Opportunity"))
stage_ids = self.pool.get('crm.case.stage').search(cr, uid, [('type','=','opportunity'),('sequence','>=',1), ('section_ids','=', lead.section_id.id)]) stage_ids = self.pool.get('crm.case.stage').search(cr, uid, [('type','=','opportunity'),('sequence','>=',1), ('section_ids','=', lead.section_id.id)])
else: else:
stage_ids = self.pool.get('crm.case.stage').search(cr, uid, [('type','=','opportunity'),('sequence','>=',1)]) stage_ids = self.pool.get('crm.case.stage').search(cr, uid, [('type','=','opportunity'),('sequence','>=',1)])
data = self.browse(cr, uid, ids[0], context=context) data = self.browse(cr, uid, ids[0], context=context)
partner_ids = [] partner_ids = []
if data.action == 'create': if data.action == 'create':
partner_ids = self._create_partner(cr, uid, ids, context=context) partner_ids = self._create_partner(cr, uid, ids, context=context)
partner_id = partner_ids and partner_ids[0] or data.partner_id.id partner_id = partner_ids and partner_ids[0] or data.partner_id.id
self._convert(cr, uid, ids, lead, partner_id, stage_ids, context=context) self._convert(cr, uid, ids, lead, partner_id, stage_ids, context=context)
if data.name == 'merge': if data.name == 'merge':
merge_obj = self.pool.get('crm.merge.opportunity') merge_obj = self.pool.get('crm.merge.opportunity')
self.write(cr, uid, ids, {'opportunity_ids' : [(6,0, [data.opportunity_ids[0].id])]}, context=context)
return merge_obj.merge(cr, uid, data.opportunity_ids, context=context) return merge_obj.merge(cr, uid, data.opportunity_ids, context=context)
return { return {
@ -177,7 +178,7 @@ Leads Could not convert into Opportunity"))
'type': 'ir.actions.act_window', 'type': 'ir.actions.act_window',
'search_view_id': opportunity_view_search 'search_view_id': opportunity_view_search
} }
crm_lead2opportunity_partner() crm_lead2opportunity_partner()
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

View File

@ -26,46 +26,46 @@ class crm_merge_opportunity(osv.osv_memory):
_name = 'crm.merge.opportunity' _name = 'crm.merge.opportunity'
_description = 'Merge two Opportunities' _description = 'Merge two Opportunities'
def _get_first_not_null_id(self, attr, ops): def _get_first_not_null_id(self, attr, ops):
for op in ops: for op in ops:
if hasattr(op, attr) and getattr(op, attr): if hasattr(op, attr) and getattr(op, attr):
return getattr(op, attr).id return getattr(op, attr).id
return False return False
def _get_first_not_null(self, attr, ops): def _get_first_not_null(self, attr, ops):
for op in ops: for op in ops:
if hasattr(op, attr) and getattr(op, attr): if hasattr(op, attr) and getattr(op, attr):
return getattr(op, attr) return getattr(op, attr)
return False return False
def _concat_all(self, attr, ops): def _concat_all(self, attr, ops):
return ', '.join([getattr(op, attr) for op in ops if hasattr(op, attr) and getattr(op, attr)]) 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): def get_attachments(self, cr, uid, id, context=None):
attach_obj = self.pool.get('ir.attachment') attach_obj = self.pool.get('ir.attachment')
result = [] result = []
attach_ids = attach_obj.search(cr, uid, [('res_model' , '=', 'crm.lead'), ('res_id', '=', id)]) attach_ids = attach_obj.search(cr, uid, [('res_model' , '=', 'crm.lead'), ('res_id', '=', id)])
return attach_ids return attach_ids
def set_attachements_res_id(self, cr, uid, op_id, attach_ids, context=None): def set_attachements_res_id(self, cr, uid, op_id, attach_ids, context=None):
attach_obj = self.pool.get('ir.attachment') attach_obj = self.pool.get('ir.attachment')
attach_obj.write(cr, uid, attach_ids, {'res_id' : op_id}) attach_obj.write(cr, uid, attach_ids, {'res_id' : op_id})
def merge(self, cr, uid, op_ids, context=None): def merge(self, cr, uid, op_ids, context=None):
""" """
@param opp_ids : list of opportunities ids to merge @param opp_ids : list of opportunities ids to merge
""" """
opp_obj = self.pool.get('crm.lead') opp_obj = self.pool.get('crm.lead')
message_obj = self.pool.get('mailgate.message') message_obj = self.pool.get('mailgate.message')
if len(op_ids) <= 1: if len(op_ids) <= 1:
raise osv.except_osv(_('Warning !'),_('Please select more than one opportunities.')) raise osv.except_osv(_('Warning !'),_('Please select more than one opportunities.'))
first_opp = op_ids[0] first_opp = op_ids[0]
data = { data = {
'partner_id': self._get_first_not_null_id('partner_id', op_ids), # !! 'partner_id': self._get_first_not_null_id('partner_id', op_ids), # !!
@ -82,70 +82,70 @@ class crm_merge_opportunity(osv.osv_memory):
'type_id' : self._get_first_not_null_id('type_id', op_ids), #!! 'type_id' : self._get_first_not_null_id('type_id', op_ids), #!!
'user_id' : self._get_first_not_null_id('user_id', op_ids), #!! 'user_id' : self._get_first_not_null_id('user_id', op_ids), #!!
'section_id' : self._get_first_not_null_id('section_id', op_ids), #!! 'section_id' : self._get_first_not_null_id('section_id', op_ids), #!!
'state_id' : self._get_first_not_null_id('state_id', op_ids), 'state_id' : self._get_first_not_null_id('state_id', op_ids),
'description' : self._concat_all('description', op_ids), #not lost 'description' : self._concat_all('description', op_ids), #not lost
'email' : self._get_first_not_null('email', op_ids), # !! 'email' : self._get_first_not_null('email', op_ids), # !!
'fax' : self._get_first_not_null('fax', op_ids), 'fax' : self._get_first_not_null('fax', op_ids),
'mobile' : self._get_first_not_null('mobile', op_ids), 'mobile' : self._get_first_not_null('mobile', op_ids),
'partner_latitude' : hasattr(opp_obj,'partner_latitude') and self._get_first_not_null('partner_latitude', op_ids), 'partner_latitude' : hasattr(opp_obj,'partner_latitude') and self._get_first_not_null('partner_latitude', op_ids),
'partner_longitude' : hasattr(opp_obj,'partner_longitude') and self._get_first_not_null('partner_longitude', op_ids), 'partner_longitude' : hasattr(opp_obj,'partner_longitude') and self._get_first_not_null('partner_longitude', op_ids),
'partner_name' : self._get_first_not_null('partner_name', op_ids), 'partner_name' : self._get_first_not_null('partner_name', op_ids),
'phone' : self._get_first_not_null('phone', op_ids), 'phone' : self._get_first_not_null('phone', op_ids),
'probability' : self._get_first_not_null('probability', op_ids), 'probability' : self._get_first_not_null('probability', op_ids),
'planned_revenue' : self._get_first_not_null('planned_revenue', op_ids), 'planned_revenue' : self._get_first_not_null('planned_revenue', op_ids),
'street' : self._get_first_not_null('street', op_ids), 'street' : self._get_first_not_null('street', op_ids),
'street2' : self._get_first_not_null('street2', op_ids), 'street2' : self._get_first_not_null('street2', op_ids),
'zip' : self._get_first_not_null('zip', op_ids), 'zip' : self._get_first_not_null('zip', op_ids),
} }
#copy message into the first opportunity + merge attachement #copy message into the first opportunity + merge attachement
for opp in op_ids[1:]: for opp in op_ids[1:]:
attach_ids = self.get_attachments(cr, uid, opp, context=context) attach_ids = self.get_attachments(cr, uid, opp, context=context)
self.set_attachements_res_id(cr, uid, first_opp.id, attach_ids) self.set_attachements_res_id(cr, uid, first_opp.id, attach_ids)
for history in opp.message_ids: for history in opp.message_ids:
new_history = message_obj.copy(cr, uid, history.id, default={'res_id': opp.id}) new_history = message_obj.copy(cr, uid, history.id, default={'res_id': opp.id})
#Notification about loss of information #Notification about loss of information
details = [] details = []
subject = ['Merged opportunities :'] subject = ['Merged opportunities :']
for opp in op_ids: for opp in op_ids:
subject.append(opp.name) 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 '', 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.stage_id.name or '',
opp.section_id.name or '', opp.section_id.name or '',
opp.user_id.name or '', opp.user_id.name or '',
opp.categ_id.name or '', opp.categ_id.name or '',
opp.channel_id.name or '', opp.channel_id.name or '',
opp.company_id.name or '', opp.company_id.name or '',
opp.contact_name or '', opp.contact_name or '',
opp.email_from or '', opp.email_from or '',
opp.phone or '', opp.phone or '',
opp.fax or '', opp.fax or '',
opp.mobile or '', opp.mobile or '',
opp.state_id.name or '', opp.state_id.name or '',
opp.description or '', opp.description or '',
opp.probability or '', opp.probability or '',
opp.planned_revenue or '', opp.planned_revenue or '',
opp.country_id.name or '', opp.country_id.name or '',
opp.city or '', opp.city or '',
opp.street or '', opp.street or '',
opp.street2 or '', opp.street2 or '',
opp.zip or '', opp.zip or '',
)) ))
subject = subject[0] + ", ".join(subject[1:]) subject = subject[0] + ", ".join(subject[1:])
details = "\n\n".join(details) details = "\n\n".join(details)
opp_obj._history(cr, uid, [first_opp], subject, details=details) opp_obj._history(cr, uid, [first_opp], subject, details=details)
#data.update({'message_ids' : [(6, 0 ,self._concat_o2m('message_ids', op_ids))]}) #data.update({'message_ids' : [(6, 0 ,self._concat_o2m('message_ids', op_ids))]})
opp_obj.write(cr, uid, [first_opp.id], data) opp_obj.write(cr, uid, [first_opp.id], data)
unlink_ids = map(lambda x: x.id, op_ids[1:]) unlink_ids = map(lambda x: x.id, op_ids[1:])
opp_obj.unlink(cr, uid, unlink_ids) opp_obj.unlink(cr, uid, unlink_ids)
models_data = self.pool.get('ir.model.data') models_data = self.pool.get('ir.model.data')
@ -163,7 +163,7 @@ class crm_merge_opportunity(osv.osv_memory):
if opportunity_view_tree: if opportunity_view_tree:
opportunity_view_tree = models_data.browse( opportunity_view_tree = models_data.browse(
cr, uid, opportunity_view_tree, context=context).res_id cr, uid, opportunity_view_tree, context=context).res_id
return { return {
'name': _('Opportunity'), 'name': _('Opportunity'),
'view_type': 'form', 'view_type': 'form',
@ -177,16 +177,16 @@ class crm_merge_opportunity(osv.osv_memory):
(False, 'calendar'), (False, 'graph')], (False, 'calendar'), (False, 'graph')],
'type': 'ir.actions.act_window', 'type': 'ir.actions.act_window',
} }
def action_merge(self, cr, uid, ids, context=None): def action_merge(self, cr, uid, ids, context=None):
obj_opportunity = self.browse(cr, uid, ids[0], context=context) obj_opportunity = self.browse(cr, uid, ids[0], context=context)
if hasattr(obj_opportunity, 'opportunity_ids'): if hasattr(obj_opportunity, 'opportunity_ids'):
op_ids = obj_opportunity.opportunity_ids op_ids = obj_opportunity.opportunity_ids
self.write(cr, uid, ids, {'opportunity_ids' : [(6,0, [op_ids[0].id])]}, context=context)
return self.merge(cr, uid, op_ids, context) return self.merge(cr, uid, op_ids, context)
_columns = { _columns = {
'opportunity_ids' : fields.many2many('crm.lead', 'merge_opportunity_rel', 'merge_id', 'opportunity_id', 'Opportunities', domain=[('type', '=', 'opportunity')]), 'opportunity_ids' : fields.many2many('crm.lead', 'merge_opportunity_rel', 'merge_id', 'opportunity_id', 'Opportunities', domain=[('type', '=', 'opportunity')]),