[MERGE] Merge with lp:openobject-addons
[odoo/odoo.git] / addons / crm / wizard / crm_partner_to_opportunity.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU Affero General Public License as
9 #    published by the Free Software Foundation, either version 3 of the
10 #    License, or (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU Affero General Public License for more details.
16 #
17 #    You should have received a copy of the GNU Affero General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 #
20 ##############################################################################
21
22 from osv import osv, fields
23 from tools.translate import _
24
25 class crm_partner2opportunity(osv.osv_memory):
26     """Converts Partner To Opportunity"""
27
28     _name = 'crm.partner2opportunity'
29     _description = 'Partner To Opportunity'
30
31     _columns = {
32         'name' : fields.char('Opportunity Name', size=64, required=True),
33         'planned_revenue': fields.float('Expected Revenue', digits=(16,2)),
34         'probability': fields.float('Success Probability', digits=(16,2)),
35         'partner_id': fields.many2one('res.partner', 'Partner'),
36     }
37
38     def action_cancel(self, cr, uid, ids, context=None):
39         """
40         Closes Partner 2 Opportunity
41         """
42         return {'type':'ir.actions.act_window_close'}
43
44     def default_get(self, cr, uid, fields, context=None):
45         """
46         This function gets default values
47         """
48         partner_obj = self.pool.get('res.partner')
49         data = context and context.get('active_ids', []) or []
50         res = super(crm_partner2opportunity, self).default_get(cr, uid, fields, context=context)
51
52         for partner in partner_obj.browse(cr, uid, data, []):
53             if 'name' in fields:
54                 res.update({'name': partner.name})
55             if 'partner_id' in fields:
56                 res.update({'partner_id': data and data[0] or False})
57         return res
58
59     def make_opportunity(self, cr, uid, ids, context=None):
60         partner_ids = context and context.get('active_ids', []) or []
61         partner_id = partner_ids[0] if partner_ids else None
62         partner = self.pool.get('res.partner')
63         lead = self.pool.get('crm.lead')
64         data = self.browse(cr, uid, ids, context=context)[0]
65         opportunity_ids = partner.make_opportunity(cr, uid, partner_ids,
66             data.name,
67             data.planned_revenue,
68             data.probability,
69             partner_id,
70             context=context,
71         )
72         opportunity_id = opportunity_ids[partner_ids[0]]
73         return lead.redirect_opportunity_view(cr, uid, opportunity_id, context=context)
74
75 crm_partner2opportunity()
76
77 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: