[IMP] better view
[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 mx.DateTime import now
23 from osv import osv, fields
24 from tools.translate import _
25
26
27 class crm_lead2opportunity(osv.osv_memory):
28     _name = 'crm.lead2opportunity'
29     _description = 'Lead To Opportunity'
30     
31     def action_cancel(self, cr, uid, ids, context=None):
32         """
33         Closes Lead To Opportunity form
34         @param self: The object pointer
35         @param cr: the current row, from the database cursor,
36         @param uid: the current user’s ID for security checks,
37         @param ids: List of Lead to Partner's IDs
38         @param context: A standard dictionary for contextual values
39
40         """
41         return {'type': 'ir.actions.act_window_close'}
42     
43     def action_apply(self, cr, uid, ids, context=None):
44         """
45         This converts lead to opportunity and opens Opportunity view
46         @param self: The object pointer
47         @param cr: the current row, from the database cursor,
48         @param uid: the current user’s ID for security checks,
49         @param ids: List of Lead to Opportunity IDs
50         @param context: A standard dictionary for contextual values
51
52         @return : Dictionary value for created Opportunity form
53         """
54         record_id = context and context.get('active_id', False) or False
55         if not record_id:
56             return {}
57
58         lead_obj = self.pool.get('crm.lead')
59         data_obj = self.pool.get('ir.model.data')
60         model_obj = self.pool.get('ir.model')
61
62         # Get Opportunity views
63         result = data_obj._get_id(cr, uid, 'crm', 'view_crm_case_opportunities_filter')
64         res = data_obj.read(cr, uid, result, ['res_id'])
65         id2 = data_obj._get_id(cr, uid, 'crm', 'crm_case_form_view_oppor')
66         id3 = data_obj._get_id(cr, uid, 'crm', 'crm_case_tree_view_oppor')
67         if id2:
68             id2 = data_obj.browse(cr, uid, id2, context=context).res_id
69         if id3:
70             id3 = data_obj.browse(cr, uid, id3, context=context).res_id
71
72         lead = lead_obj.browse(cr, uid, record_id, context=context)
73         model_ids = model_obj.search(cr, uid, [('model', '=', 'crm.lead')])
74
75
76         for this in self.browse(cr, uid, ids, context=context):
77             vals ={
78                 'planned_revenue': this.planned_revenue, 
79                 'probability': this.probability,
80                 'name': this.name, 
81                 'partner_id': this.partner_id.id, 
82                 'type': 'opportunity'
83             }
84             lead_obj.write(cr, uid, lead.id, vals, context=context)
85             lead_obj._history(cr, uid, [lead], _('Opportunity'), details='Converted to Opportunity', context=context)
86             if lead.partner_id:
87                 msg_ids = [ x.id for x in lead.message_ids]
88                 self.pool.get('mailgate.message').write(cr, uid, msg_ids, {'partner_id': lead.partner_id.id}, context=context)
89
90         value = {
91             'name': _('Opportunity'), 
92             'view_type': 'form', 
93             'view_mode': 'form,tree', 
94             'res_model': 'crm.lead', 
95             'domain': [('type', '=', 'opportunity')], 
96             'res_id': int(lead.id), 
97             'view_id': False, 
98             'views': [(id2, 'form'), (id3, 'tree'), (False, 'calendar'), (False, 'graph')], 
99             'type': 'ir.actions.act_window', 
100             'search_view_id': res['res_id']
101         }
102         return value
103
104     _columns = {
105         'name' : fields.char('Opportunity Summary', size=64, required=True, select=1), 
106         'probability': fields.float('Success Probability'), 
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 != 'open':
128                 raise osv.except_osv(_('Warning !'), _('Lead should be in \
129 \'Open\' 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         rec_ids = context and context.get('active_ids', [])
145         data = context and context.get('active_ids', []) or []
146         res = super(crm_lead2opportunity, self).default_get(cr, uid, fields, context=context)
147         for lead in lead_obj.browse(cr, uid, data, context=context):
148             if 'name' in fields:
149                 res.update({'name': lead.name})
150             if 'partner_id' in fields:
151                 res.update({'partner_id': lead.partner_id.id or False})
152         return res
153
154 crm_lead2opportunity()
155
156
157 class crm_lead2opportunity_partner(osv.osv_memory):
158     _name = 'crm.lead2opportunity.partner'
159     _description = 'Lead To Opportunity Partner'
160     _inherit = 'crm.lead2partner'
161
162     _columns = {
163         'partner_id': fields.many2one('res.partner', 'Partner'), 
164         'action': fields.selection([('exist', 'Link to an existing partner'), ('create', 'Create a new partner')], 'Action'), 
165     }
166     
167     def default_get(self, cr, uid, fields, context=None):
168         """
169         This function gets default values
170         @param self: The object pointer
171         @param cr: the current row, from the database cursor,
172         @param uid: the current user’s ID for security checks,
173         @param fields: List of fields for default value
174         @param context: A standard dictionary for contextual values
175
176         @return : default values of fields.
177         """
178         lead_obj = self.pool.get('crm.lead')
179         partner_obj = self.pool.get('res.partner')
180         contact_obj = self.pool.get('res.partner.address')
181         rec_ids = context and context.get('active_ids', [])
182         partner_id = False
183
184         data = context and context.get('active_ids', []) or []
185         res = super(crm_lead2opportunity_partner, self).default_get(cr, uid, fields, context=context)
186
187         for lead in lead_obj.browse(cr, uid, data, context=context):
188             partner_ids = partner_obj.search(cr, uid, [('name', '=', lead.partner_name or lead.name)])
189             if not partner_ids and lead.email_from:
190                 address_ids = contact_obj.search(cr, uid, [('email', '=', lead.email_from)])
191                 if address_ids:
192                     addresses = contact_obj.browse(cr, uid, address_ids)
193                     partner_ids = addresses and [addresses[0].partner_id.id] or False
194             partner_id = partner_ids and partner_ids[0] or False
195
196             if 'partner_id' in fields:
197                 res.update({'partner_id': partner_id})
198             if 'action' in fields:
199                 res.update({'action': partner_id and 'exist' or 'create'})
200         return res
201     
202     def make_partner(self, cr, uid, ids, context=None):
203         """
204         This function Makes partner based on action.
205         @param self: The object pointer
206         @param cr: the current row, from the database cursor,
207         @param uid: the current user’s ID for security checks,
208         @param ids: List of Lead to Partner's IDs
209         @param context: A standard dictionary for contextual values
210
211         @return : Dictionary value for created Partner form.
212         """
213         if not context:
214             context = {}
215         
216         partner_ids = self._create_partner(cr, uid, ids, context)
217         mod_obj = self.pool.get('ir.model.data')
218         result = mod_obj._get_id(cr, uid, 'base', 'view_res_partner_filter')
219         res = mod_obj.read(cr, uid, result, ['res_id'])
220         value = {}
221         data_obj = self.pool.get('ir.model.data')
222         data_id = data_obj._get_id(cr, uid, 'crm', 'view_crm_lead2opportunity_create')
223         view_id = False
224         if data_id:
225             view_id = data_obj.browse(cr, uid, data_id, context=context).res_id
226         
227         context.update({'partner_id': partner_ids})
228         value = {            
229             'name': _('Create Opportunity'), 
230             'view_type': 'form', 
231             'view_mode': 'form,tree', 
232             'res_model': 'crm.lead2opportunity', 
233             'view_id': False, 
234             'context': context, 
235             'views': [(view_id, 'form')], 
236             'type': 'ir.actions.act_window', 
237             'target': 'new', 
238         }
239         return value
240
241     def action_skip(self, cr, uid, ids, context=None):
242         """
243         This skips partner creation
244         @param self: The object pointer
245         @param cr: the current row, from the database cursor,
246         @param uid: the current user’s ID for security checks,
247         @param ids: List of Lead to Opportunity IDs
248         @param context: A standard dictionary for contextual values
249
250         @return : Dictionary value for Opportunity form
251         """
252         value = {}
253         data_obj = self.pool.get('ir.model.data')
254         data_id = data_obj._get_id(cr, uid, 'crm', 'view_crm_lead2opportunity_create')
255         view_id = False
256         if data_id:
257             view_id = data_obj.browse(cr, uid, data_id, context=context).res_id
258
259         context.update({'partner_id': False})
260         value = {            
261             'name': _('Create Opportunity'), 
262             'view_type': 'form', 
263             'view_mode': 'form,tree', 
264             'res_model': 'crm.lead2opportunity', 
265             'view_id': False, 
266             'context': context, 
267             'views': [(view_id, 'form')], 
268             'type': 'ir.actions.act_window', 
269             'target': 'new', 
270         }
271         return value
272     
273
274     def view_init(self, cr, uid, fields, context=None):
275         """
276         This function checks for precondition before wizard executes
277         @param self: The object pointer
278         @param cr: the current row, from the database cursor,
279         @param uid: the current user’s ID for security checks,
280         @param fields: List of fields for default value
281         @param context: A standard dictionary for contextual values
282
283         """
284         lead_obj = self.pool.get('crm.lead')
285
286         for lead in lead_obj.browse(cr, uid, context.get('active_ids', [])):
287             if lead.state in ['done', 'cancel']:
288                 raise osv.except_osv(_("Warning !"), _("Closed/Cancelled \
289 Leads Could not convert into Opportunity"))
290             if lead.state != 'open':
291                 raise osv.except_osv(_('Warning !'), _('Lead should be in \
292 \'Open\' state before converting to Opportunity.'))
293         return False
294
295 crm_lead2opportunity_partner()
296
297
298 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: