[IMP/FIX] res_partner_addres model clean up for crm, lead, phonecall, meeting
[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.email_from:
47                 address_ids = partner_obj.search(cr, uid, ['|', ('phone', '=', phonecall.partner_phone), ('mobile','=',phonecall.partner_mobile)])
48                 if address_ids:
49                     addresses = partner_ids.browse(cr, uid, address_ids)
50                     partner_ids = addresses and [addresses[0].parent_id.id] or False
51
52             partner_id = partner_ids and partner_ids[0] or False
53         return partner_id
54
55
56     def _create_partner(self, cr, uid, ids, context=None):
57         """
58         This function Creates partner based on action.
59         """
60         if context is None:
61             context = {}
62         phonecall = self.pool.get('crm.phonecall')
63
64         data = self.browse(cr, uid, ids, context=context)[0]
65         call_ids = context and context.get('active_ids') or []
66         partner_id = data.partner_id and data.partner_id.id or False
67         partner_ids = phonecall.convert_partner(cr, uid, call_ids, data.action, partner_id, context=context)
68         return partner_ids[call_ids[0]]
69
70 crm_phonecall2partner()
71
72 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: