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 _
26 class crm_lead2opportunity(osv.osv_memory):
27 _name = 'crm.lead2opportunity'
28 _description = 'Lead To Opportunity'
30 def action_cancel(self, cr, uid, ids, context=None):
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
40 return {'type': 'ir.actions.act_window_close'}
42 def action_apply(self, cr, uid, ids, context=None):
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
51 @return : Dictionary value for created Opportunity form
53 record_id = context and context.get('active_id', False) or False
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')
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')
67 id2 = data_obj.browse(cr, uid, id2, context=context).res_id
69 id3 = data_obj.browse(cr, uid, id3, context=context).res_id
71 lead = lead_obj.browse(cr, uid, record_id, context=context)
73 for this in self.browse(cr, uid, ids, context=context):
75 'planned_revenue': this.planned_revenue,
76 'probability': this.probability,
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),
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)
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)
90 'name': _('Opportunity'),
92 'view_mode': 'form,tree',
93 'res_model': 'crm.lead',
94 'domain': [('type', '=', 'opportunity')],
95 'res_id': int(lead.id),
97 'views': [(id2, 'form'), (id3, 'tree'), (False, 'calendar'), (False, 'graph')],
98 'type': 'ir.actions.act_window',
99 'search_view_id': res['res_id']
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'),
111 def view_init(self, cr, uid, fields, context=None):
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
121 lead_obj = self.pool.get('crm.lead')
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.'))
132 def default_get(self, cr, uid, fields, context=None):
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
141 @return : default values of fields.
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):
148 res.update({'name': lead.name})
149 if 'partner_id' in fields:
150 res.update({'partner_id': lead.partner_id.id or False})
153 crm_lead2opportunity()
156 class crm_lead2opportunity_partner(osv.osv_memory):
157 _name = 'crm.lead2opportunity.partner'
158 _description = 'Lead To Opportunity Partner'
159 _inherit = 'crm.lead2partner'
162 'partner_id': fields.many2one('res.partner', 'Partner'),
163 'action': fields.selection([('exist', 'Link to an existing partner'), ('create', 'Create a new partner')], 'Action'),
166 def make_partner(self, cr, uid, ids, context=None):
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
175 @return : Dictionary value for created Partner form.
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')
184 data_obj = self.pool.get('ir.model.data')
185 data_id = data_obj._get_id(cr, uid, 'crm', 'view_crm_lead2opportunity_action')
188 view_id = data_obj.browse(cr, uid, data_id, context=context).res_id
190 context.update({'partner_id': partner_ids})
192 'name': _('Create Opportunity'),
194 'view_mode': 'form,tree',
195 'res_model': 'crm.lead2opportunity.action',
198 'views': [(view_id, 'form')],
199 'type': 'ir.actions.act_window',
204 def action_skip(self, cr, uid, ids, context=None):
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
213 @return : Dictionary value for Opportunity form
216 data_obj = self.pool.get('ir.model.data')
217 data_id = data_obj._get_id(cr, uid, 'crm', 'view_crm_lead2opportunity_create')
220 view_id = data_obj.browse(cr, uid, data_id, context=context).res_id
222 context.update({'partner_id': False})
224 'name': _('Create Opportunity'),
226 'view_mode': 'form,tree',
227 'res_model': 'crm.lead2opportunity',
230 'views': [(view_id, 'form')],
231 'type': 'ir.actions.act_window',
237 def view_init(self, cr, uid, fields, context=None):
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
247 lead_obj = self.pool.get('crm.lead')
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.'))
258 crm_lead2opportunity_partner()
260 class crm_lead2opportunity_action(osv.osv_memory):
262 Merge with Existing Opportunity or Convert to Opportunity
264 _name = 'crm.lead2opportunity.action'
265 _description = 'Convert/Merge Opportunity'
268 'name': fields.selection([('convert', 'Convert to Opportunity'), ('merge', 'Merge with existing Opportunity')],'Select Action', required=True),
276 def do_action(self, cr, uid, ids, context=None):
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
287 data_obj = self.pool.get('ir.model.data')
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')
293 view_id = data_obj.browse(cr, uid, data_id, context=context).res_id
295 'name': _('Create Opportunity'),
297 'view_mode': 'form,tree',
298 'res_model': 'crm.lead2opportunity',
301 'views': [(view_id, 'form')],
302 'type': 'ir.actions.act_window',
305 elif this.name == 'merge':
306 data_id = data_obj._get_id(cr, uid, 'crm', 'merge_opportunity_form')
308 view_id = data_obj.browse(cr, uid, data_id, context=context).res_id
310 'name': _('Merge with Existing Opportunity'),
312 'view_mode': 'form,tree',
313 'res_model': 'crm.merge.opportunity',
316 'views': [(view_id, 'form')],
317 'type': 'ir.actions.act_window',
322 crm_lead2opportunity_action()
324 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: