[MERGE] Fixes related to server removal of osv_memory
[odoo/odoo.git] / addons / crm / wizard / crm_phonecall_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_phonecall2opportunity(osv.osv_memory):
26     """ Converts Phonecall to Opportunity"""
27
28     _name = 'crm.phonecall2opportunity'
29     _description = 'Phonecall To Opportunity'
30
31     def action_cancel(self, cr, uid, ids, context=None):
32         """
33         Closes Phonecall to Opportunity form
34         """
35
36         return {'type':'ir.actions.act_window_close'}
37
38
39     def action_apply(self, cr, uid, ids, context=None):
40         """
41         This converts Phonecall to Opportunity and opens Phonecall view
42         """
43         record_id = context and context.get('active_id', False) or False
44         if record_id:
45             opp_obj = self.pool.get('crm.lead')
46             phonecall_obj = self.pool.get('crm.phonecall')
47             case = phonecall_obj.browse(cr, uid, record_id, context=context)
48             data_obj = self.pool.get('ir.model.data')
49             result = data_obj._get_id(cr, uid, 'crm', 'view_crm_case_opportunities_filter')
50             res = data_obj.read(cr, uid, result, ['res_id'])
51             id2 = data_obj._get_id(cr, uid, 'crm', 'crm_case_form_view_oppor')
52             id3 = data_obj._get_id(cr, uid, 'crm', 'crm_case_tree_view_oppor')
53             if id2:
54                 id2 = data_obj.browse(cr, uid, id2, context=context).res_id
55             if id3:
56                 id3 = data_obj.browse(cr, uid, id3, context=context).res_id
57
58             for this in self.browse(cr, uid, ids, context=context):
59                 address = None
60                 if this.partner_id:
61                     address_id = self.pool.get('res.partner').address_get(cr, uid, [this.partner_id.id])
62                     if address_id['default']:
63                         address = self.pool.get('res.partner.address').browse(cr, uid, address_id['default'], context=context)
64                 new_opportunity_id = opp_obj.create(cr, uid, {
65                                 'name': this.name,
66                                 'planned_revenue': this.planned_revenue,
67                                 'probability': this.probability,
68                                 'partner_id': this.partner_id and this.partner_id.id or False,
69                                 'partner_address_id': address and address.id, 
70                                 'phone': address and address.phone,
71                                 'mobile': address and address.mobile,
72                                 'section_id': case.section_id and case.section_id.id or False,
73                                 'description': case.description or False,
74                                 'phonecall_id': case.id,
75                                 'priority': case.priority,
76                                 'type': 'opportunity', 
77                                 'phone': case.partner_phone or False,
78                             })
79                 vals = {
80                             'partner_id': this.partner_id.id,
81                             'opportunity_id' : new_opportunity_id,
82                             }
83                 phonecall_obj.write(cr, uid, [case.id], vals)
84                 phonecall_obj.case_close(cr, uid, [case.id])
85                 opp_obj.case_open(cr, uid, [new_opportunity_id])
86
87         value = {
88             'name': _('Opportunity'),
89             'view_type': 'form',
90             'view_mode': 'form,tree',
91             'res_model': 'crm.lead',
92             'res_id': int(new_opportunity_id),
93             'view_id': False,
94             'views': [(id2, 'form'), (id3, 'tree'), (False, 'calendar'), (False, 'graph')],
95             'type': 'ir.actions.act_window',
96             'search_view_id': res['res_id']
97         }
98         return value
99
100     _columns = {
101         'name' : fields.char('Opportunity Summary', size=64, required=True, select=1),
102         'probability': fields.float('Success Probability'),
103         'planned_revenue': fields.float('Expected Revenue'),
104         'partner_id': fields.many2one('res.partner', 'Partner'),
105     }
106
107     def default_get(self, cr, uid, fields, context=None):
108         """
109         This function gets default values
110         @param self: The object pointer
111         @param cr: the current row, from the database cursor,
112         @param uid: the current user’s ID for security checks,
113         @param fields: List of fields for default value
114         @param context: A standard dictionary for contextual values
115
116         @return : default values of fields.
117         """
118         record_id = context and context.get('active_id', False) or False
119         res = super(crm_phonecall2opportunity, self).default_get(cr, uid, fields, context=context)
120
121         if record_id:
122             phonecall = self.pool.get('crm.phonecall').browse(cr, uid, record_id, context=context)
123             if 'name' in fields:
124                 res.update({'name': phonecall.name})
125             if 'partner_id' in fields:
126                 res.update({'partner_id': phonecall.partner_id and phonecall.partner_id.id or False})
127         return res
128
129 crm_phonecall2opportunity()