[FIX] merge opportunity add in m2m ok

bzr revid: tfr@openerp.com-20110209141545-l01rbb9rt4ls24h2
This commit is contained in:
Thibault Francois 2011-02-09 15:15:45 +01:00
parent 08d2c920ad
commit a0e3a22dda
5 changed files with 42 additions and 64 deletions

View File

@ -30,8 +30,10 @@ class crm_lead2opportunity_partner(osv.osv_memory):
_inherit = 'crm.lead2partner'
_columns = {
#'partner_id': fields.many2one('res.partner', 'Partner'),
#'action': fields.selection([('exist', 'Link to an existing partner'), ('create', 'Create a new partner')], 'Action'),
'action': fields.selection([('exist', 'Link to an existing partner'), \
('create', 'Create a new partner'), \
('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')]),
}
@ -43,22 +45,29 @@ class crm_lead2opportunity_partner(osv.osv_memory):
all information together
"""
lead_obj = self.pool.get('crm.lead')
partner_id = False
res = super(crm_lead2opportunity_partner, self).default_get(cr, uid, fields, context=context)
opportunities = res.get('opportunity_ids') or []
name = 'convert'
if res.get('partner_id'):
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
if not partner_id and res.get('partner_id'):
partner_id = res.get('partner_id')
ids = []
if partner_id:
ids = lead_obj.search(cr, uid, [('partner_id', '=', partner_id), ('type', '=', 'opportunity')])
if ids:
name = 'merge'
opportunities += ids
if 'action' in fields:
res.update({'action' : partner_id and 'exist' or 'create'})
if 'partner_id' in fields:
res.update({'partner_id' : partner_id})
if 'name' in fields:
res.update({'name' : name})
res.update({'name' : ids and 'merge' or 'convert'})
if 'opportunity_ids' in fields:
res.update({'opportunity_ids': opportunities})
@ -152,8 +161,7 @@ Leads Could not convert into Opportunity"))
self._convert(cr, uid, ids, lead, partner_id, stage_ids, context=context)
if data.name == 'merge':
merge_obj = self.pool.get('crm.merge.opportunity')
context.update({'opportunity_ids': data.opportunity_ids})
return merge_obj.action_merge(cr, uid, ids, context=context)
return merge_obj.merge(cr, uid, data.opportunity_ids, context=context)
return {
'name': _('Opportunity'),

View File

@ -8,10 +8,6 @@
<field name="arch" type="xml">
<form string="Convert to Opportunity">
<group colspan="4" col="1" attrs="{'invisible':[('msg','=',False)]}">
<field name="msg" colspan="4" nolabel="1" height="50"/>
</group>
<newline />
<field name="action"/>
<group attrs="{'invisible':[('action','!=','exist')]}">
<field name="partner_id" attrs="{'required': [('action', '=', 'exist')]}"/>

View File

@ -31,12 +31,10 @@ class crm_lead2partner(osv.osv_memory):
_columns = {
'action': fields.selection([('exist', 'Link to an existing partner'), \
('create', 'Create a new partner'), \
('nothing', 'Do not link to a partner')], \
('create', 'Create a new partner')], \
'Action', required=True),
'partner_id': fields.many2one('res.partner', 'Partner'),
'msg': fields.text('Message', readonly=True)
}
}
def view_init(self, cr, uid, fields, context=None):
"""
@ -67,6 +65,7 @@ class crm_lead2partner(osv.osv_memory):
@return : default values of fields.
"""
lead_obj = self.pool.get('crm.lead')
partner_obj = self.pool.get('res.partner')
contact_obj = self.pool.get('res.partner.address')
@ -98,32 +97,10 @@ class crm_lead2partner(osv.osv_memory):
partner_ids = map(lambda x: x[0], cr.fetchall())
partner_id = partner_ids and partner_ids[0] or False
if not partner_id:
label = False
opp_ids = []
if email:
# Find email of existing opportunity matches the email_from of the lead
cr.execute("""select id from crm_lead
where type='opportunity' and
substring(email_from from '([^ ,<@]+@[^> ,]+)') in (%s)""" % (','.join(email)))
opp_ids = map(lambda x:x[0], cr.fetchall())
label = opp_ids and 'email' or False
if not opp_ids:
# Find name of existing opportunity matches the name of the lead
cr.execute("""SELECT l.id from crm_lead l
where type = 'opportunity' and
regexp_replace(lower(l.name), '[^a-z]*', '', 'g') = regexp_replace(%s, '[^a-z]*', '', 'g')""", (lead.name.lower(), ))
opp_ids = map(lambda x: x[0], cr.fetchall())
label = opp_ids and 'name' or False
if label:
res.update({'msg': "An existing opportunity seems to match the %s of this lead, you should double-check before converting it." % (label)})
if 'partner_id' in fields:
res.update({'partner_id': partner_id})
if 'action' in fields:
res.update({'action': partner_id and 'exist' or 'create'})
if 'name' in fields:
res.update({'name': 'convert'})
if 'opportunity_ids' in fields:
res.update({'opportunity_ids': data})

View File

@ -25,9 +25,9 @@
</field>
</record>
<!-- Lead to Partner wizard -->
<record id="action_crm_lead2partner" model="ir.actions.act_window">
<!-- Lead to Partner wizard -->
<record id="action_crm_lead2partner" model="ir.actions.act_window">
<field name="name">Create a Partner</field>
<field name="type">ir.actions.act_window</field>
<field name="res_model">crm.lead2partner</field>

View File

@ -41,28 +41,15 @@ class crm_merge_opportunity(osv.osv_memory):
def _concat_all(self, attr, ops):
return ', '.join([getattr(op, attr) for op in ops if hasattr(op, attr) and getattr(op, attr)])
def action_merge(self, cr, uid, ids, context=None):
def merge(self, cr, uid, op_ids, context=None):
"""
This function merges opportunities
@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 Phonecall to Opportunity IDs
@param context: A standard dictionary for contextual valuesn
@return : Dictionary value for created Opportunity form
@param opp_ids : list of opportunities ids to merge
"""
record_id = context and context.get('active_id') or False
opp_obj = self.pool.get('crm.lead')
message_obj = self.pool.get('mailgate.message')
if self.datas:
obj_opportunity = self.browse(cr, uid, ids[0], context=context)
if hasattr(obj_opportunity, 'opportunity_ids'):
op_ids = obj_opportunity.opportunity_ids
else:
op_ids = context.get('opportunity_ids')
if len(op_ids) <= 1:
raise osv.except_osv(_('Warning !'),_('Please select more than one opportunities.'))
@ -119,7 +106,7 @@ class crm_merge_opportunity(osv.osv_memory):
opp.city or '',
opp.company_id.name or '',
opp.country_id.name or '',
opp.email or '',
opp.email_from or '',
opp.phone or '',
opp.contact_name or ''))
subject = subject[0] + ", ".join(subject[1:])
@ -164,6 +151,16 @@ class crm_merge_opportunity(osv.osv_memory):
(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)
if hasattr(obj_opportunity, 'opportunity_ids'):
op_ids = obj_opportunity.opportunity_ids
return self.merge(cr, uid, op_ids, context)
_columns = {
'opportunity_ids' : fields.many2many('crm.lead', 'merge_opportunity_rel', 'merge_id', 'opportunity_id', 'Opportunities', domain=[('type', '=', 'opportunity')]),