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 mx.DateTime import now
23 from osv import osv, fields
24 from tools.translate import _
27 class crm_lead2opportunity(osv.osv_memory):
28 _name = 'crm.lead2opportunity'
29 _description = 'Lead To Opportunity'
31 def action_cancel(self, cr, uid, ids, context=None):
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
41 return {'type': 'ir.actions.act_window_close'}
43 def action_apply(self, cr, uid, ids, context=None):
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
52 @return : Dictionary value for created Opportunity form
54 record_id = context and context.get('active_id', False) or False
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')
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')
68 id2 = data_obj.browse(cr, uid, id2, context=context).res_id
70 id3 = data_obj.browse(cr, uid, id3, context=context).res_id
72 lead = lead_obj.browse(cr, uid, record_id, context=context)
73 model_ids = model_obj.search(cr, uid, [('model', '=', 'crm.lead')])
76 for this in self.browse(cr, uid, ids, context=context):
78 'planned_revenue': this.planned_revenue,
79 'probability': this.probability,
81 'partner_id': this.partner_id.id,
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)
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)
91 'name': _('Opportunity'),
93 'view_mode': 'form,tree',
94 'res_model': 'crm.lead',
95 'domain': [('type', '=', 'opportunity')],
96 'res_id': int(lead.id),
98 'views': [(id2, 'form'), (id3, 'tree'), (False, 'calendar'), (False, 'graph')],
99 'type': 'ir.actions.act_window',
100 'search_view_id': res['res_id']
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'),
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 != 'open':
128 raise osv.except_osv(_('Warning !'), _('Lead should be in \
129 \'Open\' 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 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):
149 res.update({'name': lead.name})
150 if 'partner_id' in fields:
151 res.update({'partner_id': lead.partner_id.id or False})
154 crm_lead2opportunity()
157 class crm_lead2opportunity_partner(osv.osv_memory):
158 _name = 'crm.lead2opportunity.partner'
159 _description = 'Lead To Opportunity Partner'
160 _inherit = 'crm.lead2partner'
163 'partner_id': fields.many2one('res.partner', 'Partner'),
164 'action': fields.selection([('exist', 'Link to an existing partner'), ('create', 'Create a new partner')], 'Action'),
167 def default_get(self, cr, uid, fields, context=None):
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
176 @return : default values of fields.
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', [])
184 data = context and context.get('active_ids', []) or []
185 res = super(crm_lead2opportunity_partner, self).default_get(cr, uid, fields, context=context)
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)])
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
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'})
202 def make_partner(self, cr, uid, ids, context=None):
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
211 @return : Dictionary value for created Partner form.
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'])
221 data_obj = self.pool.get('ir.model.data')
222 data_id = data_obj._get_id(cr, uid, 'crm', 'view_crm_lead2opportunity_create')
225 view_id = data_obj.browse(cr, uid, data_id, context=context).res_id
227 context.update({'partner_id': partner_ids})
229 'name': _('Create Opportunity'),
231 'view_mode': 'form,tree',
232 'res_model': 'crm.lead2opportunity',
235 'views': [(view_id, 'form')],
236 'type': 'ir.actions.act_window',
241 def action_skip(self, cr, uid, ids, context=None):
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
250 @return : Dictionary value for Opportunity form
253 data_obj = self.pool.get('ir.model.data')
254 data_id = data_obj._get_id(cr, uid, 'crm', 'view_crm_lead2opportunity_create')
257 view_id = data_obj.browse(cr, uid, data_id, context=context).res_id
259 context.update({'partner_id': False})
261 'name': _('Create Opportunity'),
263 'view_mode': 'form,tree',
264 'res_model': 'crm.lead2opportunity',
267 'views': [(view_id, 'form')],
268 'type': 'ir.actions.act_window',
274 def view_init(self, cr, uid, fields, context=None):
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
284 lead_obj = self.pool.get('crm.lead')
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.'))
295 crm_lead2opportunity_partner()
298 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: