Antony Lesuisse (OpenERP) has proposed merging 
lp:~openerp-dev/openobject-addons/7.0-crm-fixes into lp:openobject-addons/7.0.

Requested reviews:
  OpenERP Core Team (openerp)

For more details, see:
https://code.launchpad.net/~openerp-dev/openobject-addons/7.0-crm-fixes/+merge/146597
-- 
https://code.launchpad.net/~openerp-dev/openobject-addons/7.0-crm-fixes/+merge/146597
Your team OpenERP R&D Team is subscribed to branch 
lp:~openerp-dev/openobject-addons/7.0-crm-fixes.
=== modified file 'crm/crm_lead.py'
--- crm/crm_lead.py	2013-02-01 12:04:24 +0000
+++ crm/crm_lead.py	2013-02-05 10:04:25 +0000
@@ -36,7 +36,6 @@
     'company_id',
     'country_id',
     'section_id',
-    'stage_id',
     'state_id',
     'type_id',
     'user_id',
@@ -472,6 +471,20 @@
 
         return 'lead'
 
+    def _merge_get_result_stage(self, cr, uid, opps, context=None):
+        stage = None
+        for opp in opps:
+            print opp.stage_id.id
+            print opp.stage_id.name
+            if not stage:
+                stage = opp.stage_id.id
+            if opp.type == 'opportunity':
+                print 'Opp found, return:'
+                print opp.stage_id.id
+                print opp.stage_id.name
+                return opp.stage_id.id
+        return stage
+
     def _merge_data(self, cr, uid, ids, oldest, fields, context=None):
         """
         Prepare lead/opp data into a dictionary for merging.  Different types
@@ -520,7 +533,8 @@
 
         # Define the resulting type ('lead' or 'opportunity')
         data['type'] = self._merge_get_result_type(cr, uid, opportunities, context)
-
+        data['stage_id'] = self._merge_get_result_stage(cr, uid, opportunities, context)
+        print data['stage_id']
         return data
 
     def _merge_find_oldest(self, cr, uid, ids, context=None):
@@ -533,9 +547,6 @@
         if context is None:
             context = {}
 
-        if context.get('convert'):
-            ids = list(set(ids) - set(context.get('lead_ids', [])))
-
         # Search opportunities order by create date
         opportunity_ids = self.search(cr, uid, [('id', 'in', ids)], order='create_date', context=context)
         oldest_opp_id = opportunity_ids[0]
@@ -643,19 +654,11 @@
 
         if len(ids) <= 1:
             raise osv.except_osv(_('Warning!'),_('Please select more than one element (lead or opportunity) from the list view.'))
-
-        lead_ids = context.get('lead_ids', [])
-
-        ctx_opportunities = self.browse(cr, uid, lead_ids, context=context)
-        opportunities = self.browse(cr, uid, ids, context=context)
-        opportunities_list = list(set(opportunities) - set(ctx_opportunities))
+        ids.sort()
         oldest = self._merge_find_oldest(cr, uid, ids, context=context)
-        if ctx_opportunities:
-            first_opportunity = ctx_opportunities[0]
-            tail_opportunities = opportunities_list + ctx_opportunities[1:]
-        else:
-            first_opportunity = opportunities_list[0]
-            tail_opportunities = opportunities_list[1:]
+        opportunities_rest = self.browse(cr, uid, list(set(ids) - set([oldest.id])), context=context)
+        first_opportunity = oldest
+        tail_opportunities = opportunities_rest
 
         merged_data = self._merge_data(cr, uid, ids, oldest, CRM_LEAD_FIELDS_TO_MERGE, context=context)
 
@@ -664,14 +667,12 @@
         self._merge_opportunity_attachments(cr, uid, first_opportunity.id, tail_opportunities, context=context)
 
         # Merge notifications about loss of information
-        self._merge_notify(cr, uid, first_opportunity, opportunities, context=context)
+        self._merge_notify(cr, uid, first_opportunity, tail_opportunities, context=context)
         # Write merged data into first opportunity
         self.write(cr, uid, [first_opportunity.id], merged_data, context=context)
         # Delete tail opportunities
         self.unlink(cr, uid, [x.id for x in tail_opportunities], context=context)
 
-        # Open first opportunity
-        self.case_open(cr, uid, [first_opportunity.id])
         return first_opportunity.id
 
     def _convert_opportunity_data(self, cr, uid, lead, customer, section_id=False, context=None):
@@ -769,7 +770,9 @@
         res = False
         res_partner = self.pool.get('res.partner')
         if partner_id:
-            res_partner.write(cr, uid, partner_id, {'section_id': lead.section_id.id or False})
+            if lead.section_id:
+                print '---TESTTRUE---'
+            res_partner.write(cr, uid, partner_id, {'section_id': lead.section_id and lead.section_id.id or False})
             contact_id = res_partner.address_get(cr, uid, [partner_id])['default']
             res = lead.write({'partner_id': partner_id}, context=context)
             message = _("<b>Partner</b> set to <em>%s</em>." % (lead.partner_id.name))
@@ -872,14 +875,10 @@
         return {
             'name': _('Opportunity'),
             'view_type': 'form',
-            'view_mode': 'tree, form',
+            'view_mode': 'form',
             'res_model': 'crm.lead',
             'domain': [('type', '=', 'opportunity')],
             'res_id': int(opportunity_id),
-            'view_id': False,
-            'views': [(form_view or False, 'form'),
-                      (tree_view or False, 'tree'),
-                      (False, 'calendar'), (False, 'graph')],
             'type': 'ir.actions.act_window',
         }
 

=== modified file 'crm/crm_lead_view.xml'
--- crm/crm_lead_view.xml	2013-02-01 18:04:45 +0000
+++ crm/crm_lead_view.xml	2013-02-05 10:04:25 +0000
@@ -219,6 +219,7 @@
                     <field name="date_deadline" invisible="1"/>
                     <field name="create_date" groups="base.group_no_one"/>
                     <field name="name"/>
+                    <field name="type"/>
                     <field name="contact_name"/>
                     <field name="country_id" invisible="context.get('invisible_country', True)"/>
                     <field name="email_from"/>

=== modified file 'crm/wizard/crm_lead_to_opportunity.py'
--- crm/wizard/crm_lead_to_opportunity.py	2012-12-10 11:16:54 +0000
+++ crm/wizard/crm_lead_to_opportunity.py	2013-02-05 10:04:25 +0000
@@ -34,7 +34,8 @@
                 ('convert', 'Convert to opportunity'),
                 ('merge', 'Merge with existing opportunities')
             ], 'Conversion Action', required=True),
-        'opportunity_ids': fields.many2many('crm.lead', string='Opportunities', domain=[('type', '=', 'opportunity')]),
+        'opportunity_ids': fields.many2many('crm.lead', string='Opportunities'),
+        'partner_id': fields.many2one('res.partner', 'Customer'),
     }
 
     def default_get(self, cr, uid, fields, context=None):
@@ -46,42 +47,34 @@
         lead_obj = self.pool.get('crm.lead')
 
         res = super(crm_lead2opportunity_partner, self).default_get(cr, uid, fields, context=context)
-        opportunities = res.get('opportunity_ids') or []
-        partner_id = False
-        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
+        if context.get('active_id'):
+            tomerge = set([int(context['active_id'])])
+
+            email = False
+            partner_id = res.get('partner_id')
+            lead = lead_obj.browse(cr, uid, int(context['active_id']), context=context)
 
             #TOFIX: use mail.mail_message.to_mail
             email = re.findall(r'([^ ,<@]+@[^> ,]+)', lead.email_from or '')
-            email = map(lambda x: "'" + x + "'", email)
-
-        if not partner_id and res.get('partner_id'):
-            partner_id = res.get('partner_id')
-
-        ids = []
-        if partner_id:
-            # Search for opportunities that have the same partner and that arent done or cancelled
-            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:
+
+            if partner_id:
+                # Search for opportunities that have the same partner and that arent done or cancelled
+                ids = lead_obj.search(cr, uid, [('partner_id', '=', partner_id)])
+                for id in ids:
+                    tomerge.add(id)
             if email:
-                # Find email of existing opportunity matching 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)))
-                ids = map(lambda x:x[0], cr.fetchall())
-            if ids:
-                opportunities.append(ids[0])
+                ids = lead_obj.search(cr, uid, [('email_from', 'ilike', email[0])])
+                for id in ids:
+                    tomerge.add(id)
 
-        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' : ids and 'merge' or 'convert'})
-        if 'opportunity_ids' in fields:
-            res.update({'opportunity_ids': opportunities})
+            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' : len(tomerge) >= 2 and 'merge' or 'convert'})
+            if 'opportunity_ids' in fields and len(tomerge) >= 2:
+                res.update({'opportunity_ids': list(tomerge)})
 
         return res
 
@@ -115,24 +108,6 @@
             lead.allocate_salesman(cr, uid, lead_ids, user_ids, team_id=team_id, context=context)
         return res
 
-    def _merge_opportunity(self, cr, uid, ids, opportunity_ids, action='merge', context=None):
-        if context is None:
-            context = {}
-        res = False
-        # Expected: all newly-converted leads (active_ids) will be merged with the opportunity(ies)
-        # that have been selected in the 'opportunity_ids' m2m, with all these records
-        # merged into the first opportunity (and the rest deleted)
-        opportunity_ids = [o.id for o in opportunity_ids]
-        lead_ids = context.get('active_ids', [])
-        if action == 'merge' and lead_ids and opportunity_ids:
-            # Add the leads in the to-merge list, next to other opps
-            # (the fact that they're passed in context['lead_ids'] means that
-            # they cannot be selected to contain the result of the merge.
-            opportunity_ids.extend(lead_ids)
-            context.update({'lead_ids': lead_ids, "convert" : True})
-            res = self.pool.get('crm.lead').merge_opportunity(cr, uid, opportunity_ids, context=context)
-        return res
-
     def action_apply(self, cr, uid, ids, context=None):
         """
         Convert lead to opportunity or merge lead and opportunity and open
@@ -140,13 +115,38 @@
         """
         if context is None:
             context = {}
+
+        w = self.browse(cr, uid, ids, context=context)[0]
+        opp_ids = [o.id for o in w.opportunity_ids]
+        if w.name == 'merge':
+            lead_id = self.pool.get('crm.lead').merge_opportunity(cr, uid, opp_ids, context=context)
+            lead_ids = [lead_id]
+            lead = self.pool.get('crm.lead').read(cr, uid, lead_id, ['type'], context=context)
+            if lead['type'] == "lead":
+                context.update({'active_ids': lead_ids})
+                self._convert_opportunity(cr, uid, ids, {'lead_ids': lead_ids}, context=context)
+        else:
+            lead_ids = context.get('active_ids', [])
+            self._convert_opportunity(cr, uid, ids, {'lead_ids': lead_ids}, context=context)
+
+        return self.pool.get('crm.lead').redirect_opportunity_view(cr, uid, lead_ids[0], context=context)
+
+    def _create_partner(self, cr, uid, ids, context=None):
+        """
+        Create partner based on action.
+        :return dict: dictionary organized as followed: {lead_id: partner_assigned_id}
+        """
+        #TODO this method in only called by crm_lead2opportunity_partner
+        #wizard and would probably diserve to be refactored or at least
+        #moved to a better place
+        if context is None:
+            context = {}
         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.name, context=context)
-        return lead.redirect_opportunity_view(cr, uid, lead_ids[0], context=context)
-
+        print repr(data._table._columns)
+        partner_id = data.partner_id and data.partner_id.id or False
+        return lead.handle_partner_assignation(cr, uid, lead_ids, data.action, partner_id, context=context)
 
 class crm_lead2opportunity_mass_convert(osv.osv_memory):
     _name = 'crm.lead2opportunity.partner.mass'

=== modified file 'crm/wizard/crm_lead_to_opportunity_view.xml'
--- crm/wizard/crm_lead_to_opportunity_view.xml	2012-12-08 08:28:14 +0000
+++ crm/wizard/crm_lead_to_opportunity_view.xml	2013-02-05 10:04:25 +0000
@@ -9,12 +9,20 @@
                 <form string="Convert to Opportunity" version="7.0">
                     <group name="name">
                         <field name="name" class="oe_inline"/>
-                        <field name="opportunity_ids" attrs="{'invisible': [('name', '!=', 'merge')]}">
+                    </group>
+                    <group string="Opportunities">
+                        <field name="opportunity_ids" attrs="{'invisible': [('name', '!=', 'merge')]}" nolabel="1">
                             <tree>
+                                <field name="create_date"/>
                                 <field name="name"/>
-                                <field name="partner_id"/>
-                                <field name="user_id"/>
-                                <field name="section_id"/>
+                                <field name="type"/>
+                                <field name="contact_name"/>
+                                <field name="country_id" invisible="context.get('invisible_country', True)"/>
+                                <field name="email_from"/>
+                                <field name="phone"/>
+                                <field name="stage_id"/>
+                                <field name="user_id" invisible="1"/>
+                                <field name="section_id" invisible="context.get('invisible_section', True)"/>
                             </tree>
                         </field>
                     </group>
@@ -51,10 +59,16 @@
                     <group string="Select Opportunities" attrs="{'invisible': [('name', '!=', 'merge')]}">
                         <field name="opportunity_ids" colspan="4" nolabel="1" attrs="{'invisible': [('name', '=', 'convert')]}">
                             <tree>
+                                <field name="create_date"/>
                                 <field name="name"/>
-                                <field name="partner_id"/>
-                                <field name="user_id"/>
-                                <field name="section_id"/>
+                                <field name="type"/>
+                                <field name="contact_name"/>
+                                <field name="country_id" invisible="context.get('invisible_country', True)"/>
+                                <field name="email_from"/>
+                                <field name="phone"/>
+                                <field name="stage_id"/>
+                                <field name="user_id" invisible="1"/>
+                                <field name="section_id" invisible="context.get('invisible_section', True)"/>
                             </tree>
                         </field>
                     </group>

=== modified file 'crm/wizard/crm_merge_opportunities_view.xml'
--- crm/wizard/crm_merge_opportunities_view.xml	2012-11-29 22:26:45 +0000
+++ crm/wizard/crm_merge_opportunities_view.xml	2013-02-05 10:04:25 +0000
@@ -11,10 +11,14 @@
                     <separator string="Select Leads/Opportunities"/>
                     <field name="opportunity_ids">
                         <tree>
+                            <field name="create_date"/>
                             <field name="name"/>
-                            <field name="partner_id"/>
-                            <field name="user_id"/>
-                            <field name="section_id"/>
+                            <field name="type"/>
+                            <field name="contact_name"/>
+                            <field name="email_from"/>
+                            <field name="phone"/>
+                            <field name="stage_id"/>
+                            <field name="user_id" invisible="1"/>
                         </tree>
                     </field>
                     <footer>

=== modified file 'crm/wizard/crm_partner_binding.py'
--- crm/wizard/crm_partner_binding.py	2012-12-10 11:16:54 +0000
+++ crm/wizard/crm_partner_binding.py	2013-02-05 10:04:25 +0000
@@ -96,20 +96,4 @@
 
         return res
 
-    def _create_partner(self, cr, uid, ids, context=None):
-        """
-        Create partner based on action.
-        :return dict: dictionary organized as followed: {lead_id: partner_assigned_id}
-        """
-        #TODO this method in only called by crm_lead2opportunity_partner
-        #wizard and would probably diserve to be refactored or at least
-        #moved to a better place
-        if context is None:
-            context = {}
-        lead = self.pool.get('crm.lead')
-        lead_ids = context.get('active_ids', [])
-        data = self.browse(cr, uid, ids, context=context)[0]
-        partner_id = data.partner_id and data.partner_id.id or False
-        return lead.handle_partner_assignation(cr, uid, lead_ids, data.action, partner_id, context=context)
-
 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

_______________________________________________
Mailing list: https://launchpad.net/~openerp-dev-gtk
Post to     : [email protected]
Unsubscribe : https://launchpad.net/~openerp-dev-gtk
More help   : https://help.launchpad.net/ListHelp

Reply via email to