1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
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.
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.
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/>.
20 ##############################################################################
22 from osv import osv, fields
23 from tools.translate import _
25 class crm_lead2partner(osv.osv_memory):
26 """ Converts lead to partner """
27 _name = 'crm.lead2partner'
28 _description = 'Lead to Partner'
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'),
36 def view_init(self, cr, uid, fields, context=None):
38 This function checks for precondition before wizard executes
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):
47 raise osv.except_osv(_('Warning !'),
48 _('A partner is already defined.'))
50 def _select_partner(self, cr, uid, context=None):
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 [])
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]
69 def default_get(self, cr, uid, fields, context=None):
71 This function gets default values
73 res = super(crm_lead2partner, self).default_get(cr, uid, fields, context=context)
74 partner_id = self._select_partner(cr, uid, context=context)
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'})
83 def open_create_partner(self, cr, uid, ids, context=None):
85 This function Opens form of create partner.
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')])
93 'view_id': view_id or False,
94 'res_model': self._name,
96 'type': 'ir.actions.act_window',
100 def _create_partner(self, cr, uid, ids, context=None):
102 This function Creates partner based on action.
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]]
113 def make_partner(self, cr, uid, ids, context=None):
115 This function Makes partner based on action.
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)
122 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: