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 openerp.osv import fields, osv
23 from openerp.tools.translate import _
25 class crm_partner_binding(osv.osv_memory):
27 Handle the partner binding or generation in any CRM wizard that requires
28 such feature, like the lead2opportunity wizard, or the
29 phonecall2opportunity wizard. Try to find a matching partner from the
30 CRM model's information (name, email, phone number, etc) or create a new
32 Use it like a mixin with the wizard of your choice.
34 _name = 'crm.partner.binding'
35 _description = 'Handle partner binding or generation in CRM wizards.'
37 'action': fields.selection([
38 ('exist', 'Link to an existing customer'),
39 ('create', 'Create a new customer'),
40 ('nothing', 'Do not link to a customer')
41 ], 'Related Customer', required=True),
42 'partner_id': fields.many2one('res.partner', 'Customer'),
45 def _find_matching_partner(self, cr, uid, context=None):
47 Try to find a matching partner regarding the active model data, like
48 the customer's name, email, phone number, etc.
50 :return int partner_id if any, False otherwise
55 partner_obj = self.pool.get('res.partner')
57 # The active model has to be a lead or a phonecall
58 if (context.get('active_model') == 'crm.lead') and context.get('active_id'):
59 active_model = self.pool.get('crm.lead').browse(cr, uid, context.get('active_id'), context=context)
60 elif (context.get('active_model') == 'crm.phonecall') and context.get('active_id'):
61 active_model = self.pool.get('crm.phonecall').browse(cr, uid, context.get('active_id'), context=context)
63 # Find the best matching partner for the active model
65 partner_obj = self.pool.get('res.partner')
67 # A partner is set already
68 if active_model.partner_id:
69 partner_id = active_model.partner_id.id
70 # Search through the existing partners based on the lead's email
71 elif active_model.email_from:
72 partner_ids = partner_obj.search(cr, uid, [('email', '=', active_model.email_from)], context=context)
74 partner_id = partner_ids[0]
75 # Search through the existing partners based on the lead's partner or contact name
76 elif active_model.partner_name:
77 partner_ids = partner_obj.search(cr, uid, [('name', 'ilike', '%'+active_model.partner_name+'%')], context=context)
79 partner_id = partner_ids[0]
80 elif active_model.contact_name:
81 partner_ids = partner_obj.search(cr, uid, [
82 ('name', 'ilike', '%'+active_model.contact_name+'%')], context=context)
84 partner_id = partner_ids[0]
88 def default_get(self, cr, uid, fields, context=None):
89 res = super(crm_partner_binding, self).default_get(cr, uid, fields, context=context)
90 partner_id = self._find_matching_partner(cr, uid, context=context)
92 if 'action' in fields:
93 res['action'] = partner_id and 'exist' or 'create'
94 if 'partner_id' in fields:
95 res['partner_id'] = partner_id
99 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: