[MERGE] merged 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 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         lead = self.pool.get('crm.lead')
31         record = self.browse(cr, uid, ids[0], context=context)
32         opportunities = record.opportunity_ids
33         #TOFIX: why need to check lead_ids here
34         lead_ids = [opportunities[0].id]
35         self.write(cr, uid, ids, {'opportunity_ids' : [(6,0, lead_ids)]}, context=context)
36         context['lead_ids'] = lead_ids
37         merge_id = lead.merge_opportunity(cr, uid, [x.id for x in opportunities], context=context)
38         return lead.redirect_opportunity_view(cr, uid, merge_id, context=context)
39
40     _columns = {
41         'opportunity_ids' : fields.many2many('crm.lead',  'merge_opportunity_rel', 'merge_id', 'opportunity_id', 'Opportunities', domain=[('type', '=', 'opportunity')]),
42     }
43
44     def default_get(self, cr, uid, fields, context=None):
45         """
46         This function gets default values
47         """
48         record_ids = context and context.get('active_ids', False) or False
49         res = super(crm_merge_opportunity, self).default_get(cr, uid, fields, context=context)
50
51         if record_ids:
52             opp_ids = []
53             opps = self.pool.get('crm.lead').browse(cr, uid, record_ids, context=context)
54             for opp in opps:
55                 if opp.state not in ('done', 'cancel'):
56                     opp_ids.append(opp.id)
57             if 'opportunity_ids' in fields:
58                 res.update({'opportunity_ids': opp_ids})
59
60         return res
61
62 crm_merge_opportunity()
63
64 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: