1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
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.
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.
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/>.
20 ##############################################################################
22 from osv import osv, fields
23 from tools.translate import _
25 class crm_phonecall2opportunity(osv.osv_memory):
26 """ Converts Phonecall to Opportunity"""
28 _name = 'crm.phonecall2opportunity'
29 _description = 'Phonecall To Opportunity'
31 def action_cancel(self, cr, uid, ids, context=None):
33 Closes Phonecall to Opportunity form
36 return {'type':'ir.actions.act_window_close'}
39 def action_apply(self, cr, uid, ids, context=None):
41 This converts Phonecall to Opportunity and opens Phonecall view
43 record_id = context and context.get('active_id', False) or False
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')
54 id2 = data_obj.browse(cr, uid, id2, context=context).res_id
56 id3 = data_obj.browse(cr, uid, id3, context=context).res_id
58 for this in self.browse(cr, uid, ids, context=context):
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, {
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,
80 'partner_id': this.partner_id.id,
81 'opportunity_id' : new_opportunity_id,
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])
88 'name': _('Opportunity'),
90 'view_mode': 'form,tree',
91 'res_model': 'crm.lead',
92 'res_id': int(new_opportunity_id),
94 'views': [(id2, 'form'), (id3, 'tree'), (False, 'calendar'), (False, 'graph')],
95 'type': 'ir.actions.act_window',
96 'search_view_id': res['res_id']
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'),
107 def default_get(self, cr, uid, fields, context=None):
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
116 @return : default values of fields.
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)
122 phonecall = self.pool.get('crm.phonecall').browse(cr, uid, record_id, context=context)
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})
129 crm_phonecall2opportunity()