'Merge opportunities in one. Different cases of merge:
- merge leads together = 1 new lead
- merge at least 1 opp with anything else (lead or opp) = 1 new opp
The resulting lead/opportunity will be the most important one (based on its confidence level)
updated with values from other opportunities to merge.
:param user_id : the id of the saleperson. If not given, will be determined by `_merge_data`.
:param team : the id of the sales team. If not given, will be determined by `_merge_data`.
:return crm.lead record resulting of th merge'

@api.multi
def merge_opportunity(self, user_id=False, team_id=False):

    if (len(self.ids) <= 1):
        raise UserError(_('Please    select    more    than    one    element    (lead    or    opportunity)    from    the    list    view.'))
    def opps_key(opportunity):
        sequence = (-1)
        if opportunity.stage_id.on_change:
            sequence = opportunity.stage_id.sequence
        return (((sequence != (-1)) and (opportunity.type == 'opportunity')), sequence, (- opportunity.id))
    opportunities = self.sorted(key=opps_key, reverse=True)
    opportunities_head = opportunities[0]
    opportunities_tail = opportunities[1:]
    merged_data = opportunities._merge_data(list(CRM_LEAD_FIELDS_TO_MERGE))
    if user_id:
        merged_data['user_id'] = user_id
    if team_id:
        merged_data['team_id'] = team_id
    opportunities_head.merge_dependences(opportunities_tail)
    if merged_data.get('team_id'):
        team_stage_ids = self.env['crm.stage'].search(['|', ('team_id', '=', merged_data['team_id']), ('team_id', '=', False)], order='sequence')
        if (merged_data.get('stage_id') not in team_stage_ids.ids):
            merged_data['stage_id'] = (team_stage_ids[0].id if team_stage_ids else False)
    opportunities_head.write(merged_data)
    opportunities_tail.sudo().unlink()
    return opportunities_head
