[MERGE] 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 osv import osv, fields
21 from tools.translate import _
22
23 class crm_merge_opportunity(osv.osv_memory):
24     """Merge two Opportunities"""
25
26     _name = 'crm.merge.opportunity'
27     _description = 'Merge two Opportunities'
28
29     def action_merge(self, cr, uid, ids, context=None):
30         if context is None:
31             context = {}
32         lead = self.pool.get('crm.lead')
33         record = self.browse(cr, uid, ids[0], context=context)
34         opportunities = record.opportunity_ids
35         #TOFIX: why need to check lead_ids here
36         lead_ids = [opportunities[0].id]
37         self.write(cr, uid, ids, {'opportunity_ids' : [(6,0, lead_ids)]}, context=context)
38         context['lead_ids'] = lead_ids
39         merge_id = lead.merge_opportunity(cr, uid, [x.id for x in opportunities], context=context)
40         return lead.redirect_opportunity_view(cr, uid, merge_id, context=context)
41
42     _columns = {
43         'opportunity_ids': fields.many2many('crm.lead', rel='merge_opportunity_rel', id1='merge_id', id2='opportunity_id', string='Leads/Opportunities'),
44     }
45
46     def default_get(self, cr, uid, fields, context=None):
47         """
48         This function gets default values
49         """
50         record_ids = context and context.get('active_ids', False) or False
51         res = super(crm_merge_opportunity, self).default_get(cr, uid, fields, context=context)
52
53         if record_ids:
54             opp_ids = []
55             opps = self.pool.get('crm.lead').browse(cr, uid, record_ids, context=context)
56             for opp in opps:
57                 if opp.state not in ('done', 'cancel'):
58                     opp_ids.append(opp.id)
59             if 'opportunity_ids' in fields:
60                 res.update({'opportunity_ids': opp_ids})
61
62         return res
63
64 crm_merge_opportunity()
65
66 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: