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_phonecall2partner(osv.osv_memory):
26 """ Converts phonecall to partner """
28 _name = 'crm.phonecall2partner'
29 _description = 'Phonecall to Partner'
32 'action': fields.selection([('exist', 'Link to an existing partner'), \
33 ('create', 'Create a new partner')], \
34 'Action', required=True),
35 'partner_id': fields.many2one('res.partner', 'Partner')
38 def view_init(self, cr, uid, fields, context=None):
40 This function checks for precondition before wizard executes
41 @param self: The object pointer
42 @param cr: the current row, from the database cursor,
43 @param uid: the current user’s ID for security checks,
44 @param fields: List of fields for default value
45 @param context: A standard dictionary for contextual values
48 phonecall_obj = self.pool.get('crm.phonecall')
49 rec_ids = context and context.get('active_ids', [])
50 for phonecall in phonecall_obj.browse(cr, uid, rec_ids, context=context):
51 if phonecall.partner_id:
52 raise osv.except_osv(_('Warning !'),
53 _('A partner is already defined on this phonecall.'))
55 def _select_partner(self, cr, uid, context=None):
57 This function Searches for Partner from selected phonecall.
58 @param self: The object pointer
59 @param cr: the current row, from the database cursor,
60 @param uid: the current user’s ID for security checks,
61 @param fields: List of fields for default value
62 @param context: A standard dictionary for contextual values
64 @return : Partner id if any for selected phonecall.
69 phonecall_obj = self.pool.get('crm.phonecall')
70 partner_obj = self.pool.get('res.partner')
71 contact_obj = self.pool.get('res.partner.address')
72 rec_ids = context and context.get('active_ids', [])
75 for phonecall in phonecall_obj.browse(cr, uid, rec_ids, context=context):
76 partner_ids = partner_obj.search(cr, uid, [('name', '=', phonecall.name or phonecall.name)])
77 if not partner_ids and phonecall.email_from:
78 address_ids = contact_obj.search(cr, uid, [('email', '=', phonecall.email_from)])
80 addresses = contact_obj.browse(cr, uid, address_ids)
81 partner_ids = addresses and [addresses[0].partner_id.id] or False
83 partner_id = partner_ids and partner_ids[0] or False
87 'action': lambda *a:'exist',
88 'partner_id': _select_partner
91 def open_create_partner(self, cr, uid, ids, context=None):
93 This function Opens form of create partner.
94 @param self: The object pointer
95 @param cr: the current row, from the database cursor,
96 @param uid: the current user’s ID for security checks,
97 @param ids: List of Phonecall to Partner's IDs
98 @param context: A standard dictionary for contextual values
100 @return : Dictionary value for next form.
103 view_obj = self.pool.get('ir.ui.view')
104 view_id = view_obj.search(cr, uid, [('model', '=', 'crm.phonecall2partner'), \
105 ('name', '=', 'crm.phonecall2partner.view')])
109 'view_id': view_id or False,
110 'res_model': 'crm.phonecall2partner',
112 'type': 'ir.actions.act_window',
117 def _create_partner(self, cr, uid, ids, context=None):
119 This function Creates partner based on action.
120 @param self: The object pointer
121 @param cr: the current row, from the database cursor,
122 @param uid: the current user’s ID for security checks,
123 @param ids: List of Phonecall to Partner's IDs
124 @param context: A standard dictionary for contextual values
126 @return : Dictionary {}.
131 phonecall_obj = self.pool.get('crm.phonecall')
132 partner_obj = self.pool.get('res.partner')
133 contact_obj = self.pool.get('res.partner.address')
137 rec_ids = context and context.get('active_ids', [])
139 for data in self.browse(cr, uid, ids, context=context):
140 for phonecall in phonecall_obj.browse(cr, uid, rec_ids, context=context):
141 if data.action == 'create':
142 partner_id = partner_obj.create(cr, uid, {
143 'name': phonecall.name or phonecall.name,
144 'user_id': phonecall.user_id.id,
145 'comment': phonecall.description,
147 contact_id = contact_obj.create(cr, uid, {
148 'partner_id': partner_id,
149 'name': phonecall.name,
150 'phone': phonecall.partner_phone,
155 partner_id = data.partner_id.id
156 contact_id = partner_obj.address_get(cr, uid, [partner_id])['default']
158 partner_ids.append(partner_id)
162 vals.update({'partner_id': partner_id})
164 vals.update({'partner_address_id': contact_id})
165 phonecall_obj.write(cr, uid, [phonecall.id], vals)
168 def make_partner(self, cr, uid, ids, context=None):
170 This function Makes partner based on action.
171 @param self: The object pointer
172 @param cr: the current row, from the database cursor,
173 @param uid: the current user’s ID for security checks,
174 @param ids: List of Phonecall to Partner's IDs
175 @param context: A standard dictionary for contextual values
177 @return : Dictionary value for created Partner form.
180 partner_ids = self._create_partner(cr, uid, ids, context=context)
181 mod_obj = self.pool.get('ir.model.data')
182 result = mod_obj._get_id(cr, uid, 'base', 'view_res_partner_filter')
183 res = mod_obj.read(cr, uid, result, ['res_id'])
188 'view_mode': 'form,tree',
189 'res_model': 'res.partner',
190 'res_id': partner_ids and int(partner_ids[0]) or False,
193 'type': 'ir.actions.act_window',
194 'search_view_id': res['res_id']
198 crm_phonecall2partner()
200 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: