[MERGE] Sync with trunk
[odoo/odoo.git] / addons / crm / wizard / crm_merge_opportunities.py
1 ##############################################################################
2 #
3 #    OpenERP, Open Source Management Solution
4 #    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
5 #
6 #    This program is free software: you can redistribute it and/or modify
7 #    it under the terms of the GNU Affero General Public License as
8 #    published by the Free Software Foundation, either version 3 of the
9 #    License, or (at your option) any later version.
10 #
11 #    This program is distributed in the hope that it will be useful,
12 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
13 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 #    GNU Affero General Public License for more details.
15 #
16 #    You should have received a copy of the GNU Affero General Public License
17 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 #
19 ##############################################################################
20 from openerp.osv import fields, osv
21 from openerp.tools.translate import _
22
23 class crm_merge_opportunity(osv.osv_memory):
24     """
25     Merge opportunities together.
26     If we're talking about opportunities, it's just because it makes more sense
27     to merge opps than leads, because the leads are more ephemeral objects.
28     But since opportunities are leads, it's also possible to merge leads
29     together (resulting in a new lead), or leads and opps together (resulting
30     in a new opp).
31     """
32
33     _name = 'crm.merge.opportunity'
34     _description = 'Merge opportunities'
35     _columns = {
36         'opportunity_ids': fields.many2many('crm.lead', rel='merge_opportunity_rel', id1='merge_id', id2='opportunity_id', string='Leads/Opportunities'),
37         'user_id': fields.many2one('res.users', 'Salesperson', select=True),
38         'section_id': fields.many2one('crm.case.section', 'Sales Team', select=True),
39     }
40
41     def action_merge(self, cr, uid, ids, context=None):
42         if context is None:
43             context = {}
44
45         lead_obj = self.pool.get('crm.lead')
46         wizard = self.browse(cr, uid, ids[0], context=context)
47         opportunity2merge_ids = wizard.opportunity_ids
48
49         #TODO: why is this passed through the context ?
50         context['lead_ids'] = [opportunity2merge_ids[0].id]
51
52         merge_id = lead_obj.merge_opportunity(cr, uid, [x.id for x in opportunity2merge_ids], wizard.user_id.id, wizard.section_id.id, context=context)
53
54         # The newly created lead might be a lead or an opp: redirect toward the right view
55         merge_result = lead_obj.browse(cr, uid, merge_id, context=context)
56
57         if merge_result.type == 'opportunity':
58             return lead_obj.redirect_opportunity_view(cr, uid, merge_id, context=context)
59         else:
60             return lead_obj.redirect_lead_view(cr, uid, merge_id, context=context)
61
62     def default_get(self, cr, uid, fields, context=None):
63         """
64         Use active_ids from the context to fetch the leads/opps to merge.
65         In order to get merged, these leads/opps can't be in 'Dead' or 'Closed'
66         """
67         if context is None:
68             context = {}
69         record_ids = context.get('active_ids', False)
70         res = super(crm_merge_opportunity, self).default_get(cr, uid, fields, context=context)
71
72         if record_ids:
73             opp_ids = []
74             opps = self.pool.get('crm.lead').browse(cr, uid, record_ids, context=context)
75             for opp in opps:
76                 if opp.probability < 100:
77                     opp_ids.append(opp.id)
78             if 'opportunity_ids' in fields:
79                 res.update({'opportunity_ids': opp_ids})
80
81         return res
82
83     def on_change_user(self, cr, uid, ids, user_id, section_id, context=None):
84         """ When changing the user, also set a section_id or restrict section id
85             to the ones user_id is member of. """
86         if user_id:
87             if section_id:
88                 user_in_section = self.pool.get('crm.case.section').search(cr, uid, [('id', '=', section_id), '|', ('user_id', '=', user_id), ('member_ids', '=', user_id)], context=context, count=True)
89             else:
90                 user_in_section = False
91             if not user_in_section:
92                 section_id = False
93                 section_ids = self.pool.get('crm.case.section').search(cr, uid, ['|', ('user_id', '=', user_id), ('member_ids', '=', user_id)], context=context)
94                 if section_ids:
95                     section_id = section_ids[0]
96         return {'value': {'section_id': section_id}}
97
98 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: