[MERGE] trunk
[odoo/odoo.git] / addons / crm / wizard / crm_generate_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_generate_partner(osv.osv_memory):
26     """
27     Handle the partner generation from any CRM item (lead, phonecall)
28     either by explicitly converting the element to a partner, either by
29     triggering an action that will create a partner (e.g. convert a lead into
30     an opportunity).
31     """
32     _name = 'crm.generate.partner'
33     _description = 'Generate a partner from a CRM item.'
34     _columns = {
35         'action': fields.selection([
36                 ('exist', 'Link to an existing customer'),
37                 ('create', 'Create a new customer'),
38                 ('nothing', 'Do not link to a customer')
39             ], 'Related Customer', required=True),
40         'partner_id': fields.many2one('res.partner', 'Customer'),
41     }
42
43     def _find_matching_partner(self, cr, uid, context=None):
44         """
45         Try to find a matching partner regarding the active model data, like
46         the customer's name, email, phone number, etc.
47         @return partner_id if any, False otherwise
48         """
49         if context is None:
50             context = {}
51         partner_id = False
52         partner_obj = self.pool.get('res.partner')
53
54         # The active model has to be a lead or a phonecall
55         if (context.get('active_model') == 'crm.lead') and context.get('active_id'):
56             lead_obj = self.pool.get('crm.lead')
57             lead = lead_obj.browse(cr, uid, context.get('active_id'), context=context)
58             # A partner is set already
59             if lead.partner_id:
60                 partner_id = lead.partner_id.id
61             # Search through the existing partners based on the lead's email
62             elif lead.email_from:
63                 partner_ids = partner_obj.search(cr, uid, [('email', '=', lead.email_from)], context=context)
64                 if partner_ids:
65                     partner_id = partner_ids[0]
66             # Search through the existing partners based on the lead's partner or contact name
67             elif lead.partner_name:
68                 partner_ids = partner_obj.search(cr, uid, [('name', 'ilike', '%'+lead.partner_name+'%')], context=context)
69                 if partner_ids:
70                     partner_id = partner_ids[0]
71             elif lead.contact_name:
72                 partner_ids = partner_obj.search(cr, uid, [
73                         ('name', 'ilike', '%'+lead.contact_name+'%')], context=context)
74                 if partner_ids:
75                     partner_id = partner_ids[0]
76         elif (context.get('active_model') == 'crm.phonecall') and context.get('active_id'):
77             phonecall_obj = self.pool.get('crm.phonecall')
78             phonecall = phonecall_obj.browse(cr, uid, context.get('active_id'), context=context)
79             #do stuff
80         return partner_id
81
82     def default_get(self, cr, uid, fields, context=None):
83         res = super(crm_generate_partner, self).default_get(cr, uid, fields, context=context)
84         partner_id = self._find_matching_partner(cr, uid, context=context)
85
86         if 'action' in fields:
87             res.update({'action': partner_id and 'exist' or 'create'})
88         if 'partner_id' in fields:
89             res.update({'partner_id': partner_id})
90
91         return res
92
93     def _create_partner(self, cr, uid, ids, context=None):
94         """
95         Create partner based on action.
96         """
97         if context is None:
98             context = {}
99         lead = self.pool.get('crm.lead')
100         lead_ids = context.get('active_ids', [])
101         data = self.browse(cr, uid, ids, context=context)[0]
102         partner_id = data.partner_id and data.partner_id.id or False
103         return lead.convert_partner(cr, uid, lead_ids, data.action, partner_id, context=context)
104
105     def make_partner(self, cr, uid, ids, context=None):
106         """
107         Make a partner based on action.
108         Only called from form view, so only meant to convert one lead at a time.
109         """
110         if context is None:
111             context = {}
112         lead_id = context.get('active_id', False)
113         partner_ids_map = self._create_partner(cr, uid, ids, context=context)
114         return self.pool.get('res.partner').redirect_partner_form(cr, uid, partner_ids_map.get(lead_id, False), context=context)
115
116 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: