ee7ed1fc31f71f600247074cecdc4d466a2d2de9
[odoo/odoo.git] / addons / crm / wizard / crm_opportunity_to_phonecall.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 openerp.osv import fields, osv
23 from openerp.tools.translate import _
24
25 import time
26
27 class crm_opportunity2phonecall(osv.osv_memory):
28     """Converts Opportunity to Phonecall"""
29     _inherit = 'crm.phonecall2phonecall'
30     _name = 'crm.opportunity2phonecall'
31     _description = 'Opportunity to Phonecall'
32
33     def default_get(self, cr, uid, fields, context=None):
34         opp_obj = self.pool.get('crm.lead')
35         categ_id = False
36         data_obj = self.pool.get('ir.model.data')
37         res_id = data_obj._get_id(cr, uid, 'crm', 'categ_phone2')
38         if res_id:
39             categ_id = data_obj.browse(cr, uid, res_id, context=context).res_id
40
41         record_ids = context and context.get('active_ids', []) or []
42         res = {}
43         res.update({'action': 'log', 'date': time.strftime('%Y-%m-%d %H:%M:%S')})
44         for opp in opp_obj.browse(cr, uid, record_ids, context=context):
45             if 'name' in fields:
46                 res.update({'name': opp.name})
47             if 'user_id' in fields:
48                 res.update({'user_id': opp.user_id and opp.user_id.id or False})
49             if 'section_id' in fields:
50                 res.update({'section_id': opp.section_id and opp.section_id.id or False})
51             if 'categ_id' in fields:
52                 res.update({'categ_id': categ_id})
53             if 'partner_id' in fields:
54                 res.update({'partner_id': opp.partner_id and opp.partner_id.id or False})
55             if 'contact_name' in fields:
56                 res.update({'contact_name': opp.partner_id and opp.partner_id.name or False})
57             if 'phone' in fields:
58                 res.update({'phone': opp.phone or (opp.partner_id and opp.partner_id.phone or False)})
59         return res
60
61     def action_schedule(self, cr, uid, ids, context=None):
62         value = {}
63         if context is None:
64             context = {}
65         phonecall = self.pool.get('crm.phonecall')
66         opportunity_ids = context and context.get('active_ids') or []
67         opportunity = self.pool.get('crm.lead')
68         data = self.browse(cr, uid, ids, context=context)[0]
69         call_ids = opportunity.schedule_phonecall(cr, uid, opportunity_ids, data.date, data.name, \
70                 data.note, data.phone, data.contact_name, data.user_id and data.user_id.id or False, \
71                 data.section_id and data.section_id.id or False, \
72                 data.categ_id and data.categ_id.id or False, \
73                 action=data.action, context=context)
74         return {'type': 'ir.actions.act_window_close'}
75
76 crm_opportunity2phonecall()
77
78 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: