[MERGE] upstream
[odoo/odoo.git] / addons / crm / wizard / crm_phonecall_to_partner.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_phonecall2partner(osv.osv_memory):
26     """ Converts phonecall to partner """
27
28     _name = 'crm.phonecall2partner'
29     _inherit = 'crm.lead2partner'
30     _description = 'Phonecall to Partner'
31     
32     def _select_partner(self, cr, uid, context=None):
33         """
34         This function Searches for Partner from selected phonecall.
35         """
36         if context is None:
37             context = {}
38
39         phonecall_obj = self.pool.get('crm.phonecall')
40         partner_obj = self.pool.get('res.partner')
41         rec_ids = context and context.get('active_ids', [])
42         value = {}
43         partner_id = False
44         for phonecall in phonecall_obj.browse(cr, uid, rec_ids, context=context):
45             partner_ids = partner_obj.search(cr, uid, [('name', '=', phonecall.name or phonecall.name)])
46             if not partner_ids and (phonecall.partner_phone or phonecall.partner_mobile):
47                 partner_ids = partner_obj.search(cr, uid, ['|', ('phone', '=', phonecall.partner_phone), ('mobile','=',phonecall.partner_mobile)])
48
49             partner_id = partner_ids and partner_ids[0] or False
50         return partner_id
51
52
53     def _create_partner(self, cr, uid, ids, context=None):
54         """
55         This function Creates partner based on action.
56         """
57         if context is None:
58             context = {}
59         phonecall = self.pool.get('crm.phonecall')
60
61         data = self.browse(cr, uid, ids, context=context)[0]
62         call_ids = context and context.get('active_ids') or []
63         partner_id = data.partner_id and data.partner_id.id or False
64         partner_ids = phonecall.convert_partner(cr, uid, call_ids, data.action, partner_id, context=context)
65         return partner_ids[call_ids[0]]
66
67 crm_phonecall2partner()
68
69 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: