fix
[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 default_get(self, cr, uid, fields, context=None):
39         """
40         This function gets default values
41         @param self: The object pointer
42         @param cr: the current row, from the database cursor,
43         @param uid: the current user’s ID for security checks,
44         @param fields: List of fields for default value
45         @param context: A standard dictionary for contextual values
46
47         @return : default values of fields.
48         """
49         partner_obj = self.pool.get('res.partner')
50         data = context and context.get('active_ids', []) or []
51         res = super(crm_partner2opportunity, self).default_get(cr, uid, fields, context=context)
52
53         for partner in partner_obj.browse(cr, uid, data, []):
54             if 'name' in fields:
55                 res.update({'name': partner.name})
56             if 'partner_id' in fields:
57                 res.update({'partner_id': data and data[0] or False})
58         return res
59
60     def make_opportunity(self, cr, uid, ids, context=None):
61         """
62         @param self: The object pointer
63         @param cr: the current row, from the database cursor,
64         @param uid: the current user’s ID for security checks,
65         @param context: A standard dictionary for contextual values
66         """
67
68         data = context and context.get('active_ids', []) or []
69         make_opportunity = self.pool.get('crm.partner2opportunity')
70         data_obj = self.pool.get('ir.model.data')
71         part_obj = self.pool.get('res.partner')
72         categ_obj = self.pool.get('crm.case.categ')
73         case_obj = self.pool.get('crm.lead')
74         
75         for make_opportunity_obj in make_opportunity.browse(cr, uid, ids, context=context):
76             result = data_obj._get_id(cr, uid, 'crm', 'view_crm_case_opportunities_filter')
77             res = data_obj.read(cr, uid, result, ['res_id'])
78
79             id2 = data_obj._get_id(cr, uid, 'crm', 'crm_case_form_view_oppor')
80             id3 = data_obj._get_id(cr, uid, 'crm', 'crm_case_tree_view_oppor')
81             if id2:
82                 id2 = data_obj.browse(cr, uid, id2, context=context).res_id
83             if id3:
84                 id3 = data_obj.browse(cr, uid, id3, context=context).res_id
85
86             address = part_obj.address_get(cr, uid, data)
87             categ_ids = categ_obj.search(cr, uid, [('object_id.model','=','crm.lead')])
88
89             opp_id = case_obj.create(cr, uid, {
90                 'name' : make_opportunity_obj.name,
91                 'planned_revenue' : make_opportunity_obj.planned_revenue,
92                 'probability' : make_opportunity_obj.probability,
93                 'partner_id' : make_opportunity_obj.partner_id.id,
94                 'partner_address_id' : address['default'],
95                 'categ_id' : categ_ids and categ_ids[0] or '',
96                 'state' :'draft',
97                 'type': 'opportunity'
98             })
99             value = {
100                 'name' : _('Opportunity'),
101                 'view_type' : 'form',
102                 'view_mode' : 'form,tree',
103                 'res_model' : 'crm.lead',
104                 'res_id' : opp_id,
105                 'view_id' : False,
106                 'views' : [(id2, 'form'), (id3, 'tree'), (False, 'calendar'), (False, 'graph')],
107                 'type' : 'ir.actions.act_window',
108                 'search_view_id' : res['res_id']
109             }
110             return value
111
112 crm_partner2opportunity()
113
114 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: