[MERGE] upstream
[odoo/odoo.git] / addons / crm / wizard / crm_lead_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_lead2partner(osv.osv_memory):
26     """ Converts lead to partner """
27     _name = 'crm.lead2partner'
28     _description = 'Lead to Partner'
29
30     _columns = {
31         'action': fields.selection([('exist', 'Link to an existing partner'), \
32                                     ('create', 'Create a new partner')], \
33                                     'Action', required=True),
34         'partner_id': fields.many2one('res.partner', 'Partner'),
35     }
36     def view_init(self, cr, uid, fields, context=None):
37         """
38         This function checks for precondition before wizard executes
39         """
40         if context is None:
41             context = {}
42         model = context.get('active_model')
43         model = self.pool.get(model)
44         rec_ids = context and context.get('active_ids', [])
45         for this in model.browse(cr, uid, rec_ids, context=context):
46             if this.partner_id:
47                 raise osv.except_osv(_('Warning !'),
48                         _('A partner is already defined.'))
49
50     def _select_partner(self, cr, uid, context=None):
51         if context is None:
52             context = {}
53         lead = self.pool.get('crm.lead')
54         partner = self.pool.get('res.partner')
55         lead_ids = list(context and context.get('active_ids', []) or [])
56         if not len(lead_ids):
57             return False
58         this = lead.browse(cr, uid, lead_ids[0], context=context)
59         # Find partner address matches the email_from of the lead
60         res = lead.message_partner_by_email(cr, uid, this.email_from, context=context)
61         partner_id = res.get('partner_id', False)      
62         # Find partner name that matches the name of the lead
63         if not partner_id and this.partner_name:
64             partner_ids = partner.search(cr, uid, [('name', '=', this.partner_name)], context=context)
65             if partner_ids and len(partner_ids):
66                partner_id = partner_ids[0]
67         return partner_id
68
69     def default_get(self, cr, uid, fields, context=None):
70         """
71         This function gets default values
72         """
73         res = super(crm_lead2partner, self).default_get(cr, uid, fields, context=context)        
74         partner_id = self._select_partner(cr, uid, context=context)
75
76         if 'partner_id' in fields:
77             res.update({'partner_id': partner_id})
78         if 'action' in fields:
79             res.update({'action': partner_id and 'exist' or 'create'})
80             
81         return res
82
83     def open_create_partner(self, cr, uid, ids, context=None):
84         """
85         This function Opens form of create partner.
86         """
87         view_obj = self.pool.get('ir.ui.view')
88         view_id = view_obj.search(cr, uid, [('model', '=', self._name), \
89                                      ('name', '=', self._name+'.view')])
90         return {
91             'view_mode': 'form',
92             'view_type': 'form',
93             'view_id': view_id or False,
94             'res_model': self._name,
95             'context': context,
96             'type': 'ir.actions.act_window',
97             'target': 'new',
98         }
99
100     def _create_partner(self, cr, uid, ids, context=None):
101         """
102         This function Creates partner based on action.
103         """
104         if context is None:
105             context = {}
106         lead = self.pool.get('crm.lead')
107         lead_ids = context and context.get('active_ids') or []
108         data = self.browse(cr, uid, ids, context=context)[0]
109         partner_id = data.partner_id and data.partner_id.id or False
110         partner_ids = lead.convert_partner(cr, uid, lead_ids, data.action, partner_id, context=context)
111         return partner_ids[lead_ids[0]]
112
113     def make_partner(self, cr, uid, ids, context=None):
114         """
115         This function Makes partner based on action.
116         """
117         partner_id = self._create_partner(cr, uid, ids, context=context)
118         return self.pool.get('res.partner').redirect_partner_form(cr, uid, partner_id, context=context)
119
120 crm_lead2partner()
121
122 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: