[IMP]: crm: Improvement in lead to opportunity wizard for partner selection
[odoo/odoo.git] / addons / crm / wizard / crm_lead_to_opportunity.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
26 class crm_lead2opportunity(osv.osv_memory):
27     _name = 'crm.lead2opportunity'
28     _description = 'Lead To Opportunity'
29     
30     def action_cancel(self, cr, uid, ids, context=None):
31         """
32         Closes Lead To Opportunity form
33         @param self: The object pointer
34         @param cr: the current row, from the database cursor,
35         @param uid: the current user’s ID for security checks,
36         @param ids: List of Lead to Partner's IDs
37         @param context: A standard dictionary for contextual values
38
39         """
40         return {'type': 'ir.actions.act_window_close'}
41     
42     def action_apply(self, cr, uid, ids, context=None):
43         """
44         This converts lead to opportunity and opens Opportunity view
45         @param self: The object pointer
46         @param cr: the current row, from the database cursor,
47         @param uid: the current user’s ID for security checks,
48         @param ids: List of Lead to Opportunity IDs
49         @param context: A standard dictionary for contextual values
50
51         @return : Dictionary value for created Opportunity form
52         """
53         record_id = context and context.get('active_id', False) or False
54         if not record_id:
55             return {}
56
57         lead_obj = self.pool.get('crm.lead')
58         data_obj = self.pool.get('ir.model.data')
59         model_obj = self.pool.get('ir.model')
60
61         # Get Opportunity views
62         result = data_obj._get_id(cr, uid, 'crm', 'view_crm_case_opportunities_filter')
63         res = data_obj.read(cr, uid, result, ['res_id'])
64         id2 = data_obj._get_id(cr, uid, 'crm', 'crm_case_form_view_oppor')
65         id3 = data_obj._get_id(cr, uid, 'crm', 'crm_case_tree_view_oppor')
66         if id2:
67             id2 = data_obj.browse(cr, uid, id2, context=context).res_id
68         if id3:
69             id3 = data_obj.browse(cr, uid, id3, context=context).res_id
70
71         lead = lead_obj.browse(cr, uid, record_id, context=context)
72
73         for this in self.browse(cr, uid, ids, context=context):
74             vals ={
75                 'planned_revenue': this.planned_revenue, 
76                 'probability': this.probability,
77                 'name': this.name, 
78                 'partner_id': this.partner_id.id, 
79                 'user_id': (this.partner_id.user_id and this.partner_id.user_id.id) or (lead.user_id and lead.user_id.id), 
80                 'type': 'opportunity'
81             }
82             lead_obj.write(cr, uid, lead.id, vals, context=context)
83             lead_obj.history(cr, uid, [lead], _('Opportunity'), details='Converted to Opportunity', context=context)
84             if lead.partner_id:
85                 msg_ids = [ x.id for x in lead.message_ids]
86                 self.pool.get('mailgate.message').write(cr, uid, msg_ids, {'partner_id': lead.partner_id.id}, context=context)
87             message = _('Lead ') + " '" + lead.name + "' "+ _("is converted to Opportunity.")
88             self.log(cr, uid, lead.id, message)
89         value = {
90             'name': _('Opportunity'), 
91             'view_type': 'form', 
92             'view_mode': 'form,tree', 
93             'res_model': 'crm.lead', 
94             'domain': [('type', '=', 'opportunity')], 
95             'res_id': int(lead.id), 
96             'view_id': False, 
97             'views': [(id2, 'form'), (id3, 'tree'), (False, 'calendar'), (False, 'graph')], 
98             'type': 'ir.actions.act_window', 
99             'search_view_id': res['res_id']
100         }
101         
102         return value
103
104     _columns = {
105         'name' : fields.char('Opportunity', size=64, required=True, select=1), 
106         'probability': fields.float('Success Rate (%)'), 
107         'planned_revenue': fields.float('Expected Revenue'), 
108         'partner_id': fields.many2one('res.partner', 'Partner'), 
109     }
110     
111     def view_init(self, cr, uid, fields, context=None):
112         """
113         This function checks for precondition before wizard executes
114         @param self: The object pointer
115         @param cr: the current row, from the database cursor,
116         @param uid: the current user’s ID for security checks,
117         @param fields: List of fields for default value
118         @param context: A standard dictionary for contextual values
119
120         """
121         lead_obj = self.pool.get('crm.lead')
122
123         for lead in lead_obj.browse(cr, uid, context.get('active_ids', [])):
124             if lead.state in ['done', 'cancel']:
125                 raise osv.except_osv(_("Warning !"), _("Closed/Cancelled \
126 Leads Could not convert into Opportunity"))
127             if lead.state not in ('open', 'pending'):
128                 raise osv.except_osv(_('Warning !'), _('Lead should be in \
129 \'Open\' or \'Pending\' state before converting to Opportunity.'))
130         return False
131
132     def default_get(self, cr, uid, fields, context=None):
133         """
134         This function gets default values
135         @param self: The object pointer
136         @param cr: the current row, from the database cursor,
137         @param uid: the current user’s ID for security checks,
138         @param fields: List of fields for default value
139         @param context: A standard dictionary for contextual values
140
141         @return : default values of fields.
142         """
143         lead_obj = self.pool.get('crm.lead')
144         data = context and context.get('active_ids', []) or []
145         res = super(crm_lead2opportunity, self).default_get(cr, uid, fields, context=context)
146         for lead in lead_obj.browse(cr, uid, data, context=context):
147             if 'name' in fields:
148                 res.update({'name': lead.name})
149             if 'partner_id' in fields:
150                 res.update({'partner_id': lead.partner_id.id or False})
151         return res
152
153 crm_lead2opportunity()
154
155
156 class crm_lead2opportunity_partner(osv.osv_memory):
157     _name = 'crm.lead2opportunity.partner'
158     _description = 'Lead To Opportunity Partner'
159     _inherit = 'crm.lead2partner'
160
161     _columns = {
162         'partner_id': fields.many2one('res.partner', 'Partner'), 
163         'action': fields.selection([('exist', 'Link to an existing partner'), ('create', 'Create a new partner')], 'Action'), 
164     }
165
166     def make_partner(self, cr, uid, ids, context=None):
167         """
168         This function Makes partner based on action.
169         @param self: The object pointer
170         @param cr: the current row, from the database cursor,
171         @param uid: the current user’s ID for security checks,
172         @param ids: List of Lead to Partner's IDs
173         @param context: A standard dictionary for contextual values
174
175         @return : Dictionary value for created Partner form.
176         """
177         if not context:
178             context = {}
179         
180         partner_ids = self._create_partner(cr, uid, ids, context)
181         mod_obj = self.pool.get('ir.model.data')
182         result = mod_obj._get_id(cr, uid, 'base', 'view_res_partner_filter')
183         value = {}
184         data_obj = self.pool.get('ir.model.data')
185         data_id = data_obj._get_id(cr, uid, 'crm', 'view_crm_lead2opportunity_action')
186         view_id = False
187         if data_id:
188             view_id = data_obj.browse(cr, uid, data_id, context=context).res_id
189         
190         context.update({'partner_id': partner_ids})
191         value = {            
192             'name': _('Create Opportunity'), 
193             'view_type': 'form', 
194             'view_mode': 'form,tree', 
195             'res_model': 'crm.lead2opportunity.action', 
196             'view_id': False, 
197             'context': context, 
198             'views': [(view_id, 'form')], 
199             'type': 'ir.actions.act_window', 
200             'target': 'new', 
201         }
202         return value
203
204     def action_skip(self, cr, uid, ids, context=None):
205         """
206         This skips partner creation
207         @param self: The object pointer
208         @param cr: the current row, from the database cursor,
209         @param uid: the current user’s ID for security checks,
210         @param ids: List of Lead to Opportunity IDs
211         @param context: A standard dictionary for contextual values
212
213         @return : Dictionary value for Opportunity form
214         """
215         value = {}
216         data_obj = self.pool.get('ir.model.data')
217         data_id = data_obj._get_id(cr, uid, 'crm', 'view_crm_lead2opportunity_create')
218         view_id = False
219         if data_id:
220             view_id = data_obj.browse(cr, uid, data_id, context=context).res_id
221
222         context.update({'partner_id': False})
223         value = {            
224             'name': _('Create Opportunity'), 
225             'view_type': 'form', 
226             'view_mode': 'form,tree', 
227             'res_model': 'crm.lead2opportunity', 
228             'view_id': False, 
229             'context': context, 
230             'views': [(view_id, 'form')], 
231             'type': 'ir.actions.act_window', 
232             'target': 'new', 
233         }
234         return value
235     
236
237     def view_init(self, cr, uid, fields, context=None):
238         """
239         This function checks for precondition before wizard executes
240         @param self: The object pointer
241         @param cr: the current row, from the database cursor,
242         @param uid: the current user’s ID for security checks,
243         @param fields: List of fields for default value
244         @param context: A standard dictionary for contextual values
245
246         """
247         lead_obj = self.pool.get('crm.lead')
248
249         for lead in lead_obj.browse(cr, uid, context.get('active_ids', [])):
250             if lead.state in ['done', 'cancel']:
251                 raise osv.except_osv(_("Warning !"), _("Closed/Cancelled \
252 Leads Could not convert into Opportunity"))
253             if lead.state not in ('open', 'pending'):
254                 raise osv.except_osv(_('Warning !'), _('Lead should be in \
255 \'Open\' or \'Pending\' state before converting to Opportunity.'))
256         return False
257
258 crm_lead2opportunity_partner()
259
260 class crm_lead2opportunity_action(osv.osv_memory):
261     '''
262     Merge with Existing Opportunity or Convert to Opportunity
263     '''
264     _name = 'crm.lead2opportunity.action'
265     _description = 'Convert/Merge Opportunity'
266     
267     _columns = {
268         'name': fields.selection([('convert', 'Convert to Opportunity'), ('merge', 'Merge with existing Opportunity')],'Select Action', required=True),
269         
270     }
271
272     _defaults = {
273         'name': 'convert',
274         }
275
276     def do_action(self, cr, uid, ids, context=None):
277         """
278         This function opens form according to selected Action
279         @param self: The object pointer
280         @param cr: the current row, from the database cursor,
281         @param uid: the current user’s ID for security checks,
282         @param ids: List of Lead to Opportunity IDs
283         @param context: A standard dictionary for contextual values
284         @return : Dictionary value for Opportunity form
285         """
286         value = {}
287         data_obj = self.pool.get('ir.model.data')
288         view_id = False
289         for this in self.browse(cr, uid, ids, context=context):
290             if this.name == 'convert':
291                 data_id = data_obj._get_id(cr, uid, 'crm', 'view_crm_lead2opportunity_create')
292                 if data_id:
293                     view_id = data_obj.browse(cr, uid, data_id, context=context).res_id
294                 value = {
295                         'name': _('Create Opportunity'), 
296                         'view_type': 'form', 
297                         'view_mode': 'form,tree', 
298                         'res_model': 'crm.lead2opportunity', 
299                         'view_id': False, 
300                         'context': context, 
301                         'views': [(view_id, 'form')], 
302                         'type': 'ir.actions.act_window', 
303                         'target': 'new', 
304                     }
305             elif this.name == 'merge':
306                 data_id = data_obj._get_id(cr, uid, 'crm', 'merge_opportunity_form')
307                 if data_id:
308                     view_id = data_obj.browse(cr, uid, data_id, context=context).res_id
309                 value = {
310                         'name': _('Merge with Existing Opportunity'), 
311                         'view_type': 'form', 
312                         'view_mode': 'form,tree', 
313                         'res_model': 'crm.merge.opportunity', 
314                         'view_id': False, 
315                         'context': context, 
316                         'views': [(view_id, 'form')], 
317                         'type': 'ir.actions.act_window', 
318                         'target': 'new', 
319                     }
320         return value
321
322 crm_lead2opportunity_action()
323
324 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: