Launchpad automatic translations update.
[odoo/odoo.git] / addons / crm / wizard / crm_phonecall_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_phonecall2partner(osv.osv_memory):
26     """ Converts phonecall to partner """
27
28     _name = 'crm.phonecall2partner'
29     _description = 'Phonecall to Partner'
30
31     _columns = {
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')
36         }
37
38     def view_init(self, cr, uid, fields, context=None):
39         """
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
46
47         """
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.'))
54
55     def _select_partner(self, cr, uid, context=None):
56         """
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
63
64         @return : Partner id if any for selected phonecall.
65         """
66         if context is None:
67             context = {}
68
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', [])
73         value = {}
74
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)])
79                 if address_ids:
80                     addresses = contact_obj.browse(cr, uid, address_ids)
81                     partner_ids = addresses and [addresses[0].partner_id.id] or False
82
83             partner_id = partner_ids and partner_ids[0] or False
84         return partner_id
85
86     _defaults = {
87         'action': lambda *a:'exist',
88         'partner_id': _select_partner
89         }
90
91     def open_create_partner(self, cr, uid, ids, context=None):
92         """
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
99
100         @return : Dictionary value for next form.
101         """
102
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')])
106         return {
107             'view_mode': 'form',
108             'view_type': 'form',
109             'view_id': view_id or False,
110             'res_model': 'crm.phonecall2partner',
111             'context': context,
112             'type': 'ir.actions.act_window',
113             'target': 'new',
114             }
115
116
117     def _create_partner(self, cr, uid, ids, context=None):
118         """
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
125
126         @return : Dictionary {}.
127         """
128         if context is None:
129             context = {}
130
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')
134         partner_ids = []
135         contact_id = False
136
137         rec_ids = context and context.get('active_ids', [])
138
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,
146                     })
147                     contact_id = contact_obj.create(cr, uid, {
148                         'partner_id': partner_id,
149                         'name': phonecall.name,
150                         'phone': phonecall.partner_phone,
151                     })
152
153                 else:
154                     if data.partner_id:
155                         partner_id = data.partner_id.id
156                         contact_id = partner_obj.address_get(cr, uid, [partner_id])['default']
157
158                 partner_ids.append(partner_id)
159
160                 vals = {}
161                 if partner_id:
162                     vals.update({'partner_id': partner_id})
163                 if contact_id:
164                     vals.update({'partner_address_id': contact_id})
165                 phonecall_obj.write(cr, uid, [phonecall.id], vals)
166         return partner_ids
167
168     def make_partner(self, cr, uid, ids, context=None):
169         """
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
176
177         @return : Dictionary value for created Partner form.
178         """
179
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'])
184
185         value = {
186             'domain': "[]",
187             'view_type': 'form',
188             'view_mode': 'form,tree',
189             'res_model': 'res.partner',
190             'res_id': partner_ids and int(partner_ids[0]) or False,
191             'view_id': False,
192             'context': context,
193             'type': 'ir.actions.act_window',
194             'search_view_id': res['res_id']
195         }
196         return value
197
198 crm_phonecall2partner()
199
200 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: