2009-12-30 10:20:44 +00:00
|
|
|
|
#-*- coding: utf-8 -*-
|
|
|
|
|
##############################################################################
|
|
|
|
|
#
|
|
|
|
|
# OpenERP, Open Source Management Solution
|
2010-01-12 09:18:39 +00:00
|
|
|
|
# Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
|
2009-12-30 10:20:44 +00:00
|
|
|
|
#
|
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
|
#
|
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
|
#
|
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
#
|
|
|
|
|
##############################################################################
|
|
|
|
|
|
2010-10-06 05:31:07 +00:00
|
|
|
|
from osv import fields, osv
|
2010-04-01 11:31:30 +00:00
|
|
|
|
from tools.translate import _
|
2010-01-13 13:34:09 +00:00
|
|
|
|
import crm
|
2010-04-05 10:15:37 +00:00
|
|
|
|
import time
|
2009-12-30 10:20:44 +00:00
|
|
|
|
|
2010-02-19 17:15:16 +00:00
|
|
|
|
AVAILABLE_STATES = [
|
2010-04-22 11:55:35 +00:00
|
|
|
|
('draft','Draft'),
|
2010-02-19 17:15:16 +00:00
|
|
|
|
('open','Open'),
|
|
|
|
|
('cancel', 'Lost'),
|
2010-03-04 06:39:52 +00:00
|
|
|
|
('done', 'Converted'),
|
2010-02-19 17:15:16 +00:00
|
|
|
|
('pending','Pending')
|
|
|
|
|
]
|
|
|
|
|
|
2009-12-30 10:20:44 +00:00
|
|
|
|
class crm_opportunity(osv.osv):
|
2010-03-22 10:40:26 +00:00
|
|
|
|
""" Opportunity Cases """
|
2010-04-18 15:32:44 +00:00
|
|
|
|
_order = "priority,date_action,id desc"
|
2010-04-29 11:35:01 +00:00
|
|
|
|
_inherit = 'crm.lead'
|
2010-02-19 17:15:16 +00:00
|
|
|
|
_columns = {
|
2010-05-03 12:30:48 +00:00
|
|
|
|
# From crm.case
|
|
|
|
|
'partner_address_id': fields.many2one('res.partner.address', 'Partner Contact', \
|
|
|
|
|
domain="[('partner_id','=',partner_id)]"),
|
|
|
|
|
|
|
|
|
|
# Opportunity fields
|
2010-07-13 21:29:14 +00:00
|
|
|
|
'probability': fields.float('Probability (%)',group_operator="avg"),
|
2010-02-19 17:15:16 +00:00
|
|
|
|
'planned_revenue': fields.float('Expected Revenue'),
|
2010-03-22 10:40:26 +00:00
|
|
|
|
'ref': fields.reference('Reference', selection=crm._links_get, size=128),
|
|
|
|
|
'ref2': fields.reference('Reference 2', selection=crm._links_get, size=128),
|
2010-03-02 07:59:52 +00:00
|
|
|
|
'phone': fields.char("Phone", size=64),
|
2010-04-02 07:51:40 +00:00
|
|
|
|
'date_deadline': fields.date('Expected Closing'),
|
2010-06-11 12:55:28 +00:00
|
|
|
|
'date_action': fields.date('Next Action Date'),
|
|
|
|
|
'title_action': fields.char('Next Action', size=64),
|
|
|
|
|
}
|
2010-05-03 12:30:48 +00:00
|
|
|
|
def case_close(self, cr, uid, ids, *args):
|
2010-05-05 11:55:13 +00:00
|
|
|
|
"""Overrides close for crm_case for setting probability and close date
|
2010-05-03 12:30:48 +00:00
|
|
|
|
@param self: The object pointer
|
|
|
|
|
@param cr: the current row, from the database cursor,
|
|
|
|
|
@param uid: the current user’s ID for security checks,
|
|
|
|
|
@param ids: List of case Ids
|
|
|
|
|
@param *args: Tuple Value for additional Params
|
|
|
|
|
"""
|
2010-05-05 11:55:13 +00:00
|
|
|
|
res = super(crm_opportunity, self).case_close(cr, uid, ids, args)
|
2010-07-12 22:10:32 +00:00
|
|
|
|
stage_id = super(crm_opportunity, self).stage_next(cr, uid, ids, context={'force_domain': [('probability', '=', 100)]})
|
|
|
|
|
if not stage_id:
|
2010-08-09 16:53:34 +00:00
|
|
|
|
raise osv.except_osv(_('Warning !'), _('There is no stage for won opportunities defined for this Sale Team.'))
|
2010-07-12 22:10:32 +00:00
|
|
|
|
value = self.onchange_stage_id(cr, uid, ids, stage_id, context={})['value']
|
|
|
|
|
value.update({'date_closed': time.strftime('%Y-%m-%d %H:%M:%S'), 'stage_id': stage_id})
|
|
|
|
|
|
|
|
|
|
self.write(cr, uid, ids, value)
|
2010-07-08 10:14:24 +00:00
|
|
|
|
for (id, name) in self.name_get(cr, uid, ids):
|
2010-07-22 11:00:56 +00:00
|
|
|
|
opp = self.browse(cr, uid, id)
|
|
|
|
|
if opp.type == 'opportunity':
|
2010-10-17 17:30:00 +00:00
|
|
|
|
message = _("The opportunity '%s' has been won.") % name
|
2010-07-22 11:00:56 +00:00
|
|
|
|
self.log(cr, uid, id, message)
|
2010-05-05 11:55:13 +00:00
|
|
|
|
return res
|
|
|
|
|
|
2010-07-12 08:53:02 +00:00
|
|
|
|
def case_mark_lost(self, cr, uid, ids, *args):
|
|
|
|
|
"""Mark the case as lost: state = done and probability = 0%
|
|
|
|
|
@param self: The object pointer
|
|
|
|
|
@param cr: the current row, from the database cursor,
|
|
|
|
|
@param uid: the current user’s ID for security checks,
|
|
|
|
|
@param ids: List of case Ids
|
|
|
|
|
@param *args: Tuple Value for additional Params
|
|
|
|
|
"""
|
2010-07-12 22:10:32 +00:00
|
|
|
|
res = super(crm_opportunity, self).case_close(cr, uid, ids, args)
|
|
|
|
|
stage_id = super(crm_opportunity, self).stage_next(cr, uid, ids, context={'force_domain': [('probability', '=', 0)]})
|
2010-10-05 07:54:18 +00:00
|
|
|
|
value = {}
|
|
|
|
|
if stage_id:
|
|
|
|
|
value = self.onchange_stage_id(cr, uid, ids, stage_id, context={}).get('value', {})
|
|
|
|
|
value['stage_id'] = stage_id
|
|
|
|
|
value.update({'date_closed': time.strftime('%Y-%m-%d %H:%M:%S')})
|
2010-07-12 22:10:32 +00:00
|
|
|
|
|
|
|
|
|
res = self.write(cr, uid, ids, value)
|
2010-07-12 08:53:02 +00:00
|
|
|
|
for (id, name) in self.name_get(cr, uid, ids):
|
2010-07-20 13:48:42 +00:00
|
|
|
|
opp = self.browse(cr, uid, id)
|
|
|
|
|
if opp.type == 'opportunity':
|
2010-10-17 17:30:00 +00:00
|
|
|
|
message = _("The opportunity '%s' has been marked as lost.") % name
|
2010-07-20 13:48:42 +00:00
|
|
|
|
self.log(cr, uid, id, message)
|
2010-07-12 08:53:02 +00:00
|
|
|
|
return res
|
|
|
|
|
|
2010-05-03 12:30:48 +00:00
|
|
|
|
def case_cancel(self, cr, uid, ids, *args):
|
2010-05-05 11:55:13 +00:00
|
|
|
|
"""Overrides cancel for crm_case for setting probability
|
2010-05-03 12:30:48 +00:00
|
|
|
|
@param self: The object pointer
|
|
|
|
|
@param cr: the current row, from the database cursor,
|
|
|
|
|
@param uid: the current user’s ID for security checks,
|
|
|
|
|
@param ids: List of case Ids
|
|
|
|
|
@param *args: Tuple Value for additional Params
|
|
|
|
|
"""
|
2010-05-05 11:55:13 +00:00
|
|
|
|
res = super(crm_opportunity, self).case_cancel(cr, uid, ids, args)
|
|
|
|
|
self.write(cr, uid, ids, {'probability' : 0.0})
|
|
|
|
|
return res
|
2010-07-12 22:10:32 +00:00
|
|
|
|
|
|
|
|
|
def case_reset(self, cr, uid, ids, *args):
|
|
|
|
|
"""Overrides reset as draft in order to set the stage field as empty
|
|
|
|
|
@param self: The object pointer
|
|
|
|
|
@param cr: the current row, from the database cursor,
|
|
|
|
|
@param uid: the current user’s ID for security checks,
|
|
|
|
|
@param ids: List of case Ids
|
|
|
|
|
@param *args: Tuple Value for additional Params
|
|
|
|
|
"""
|
|
|
|
|
res = super(crm_opportunity, self).case_reset(cr, uid, ids, *args)
|
|
|
|
|
self.write(cr, uid, ids, {'stage_id': False})
|
2010-10-06 05:31:07 +00:00
|
|
|
|
return res
|
2010-07-12 22:10:32 +00:00
|
|
|
|
|
|
|
|
|
|
2010-05-05 11:55:13 +00:00
|
|
|
|
def case_open(self, cr, uid, ids, *args):
|
2010-07-12 22:10:32 +00:00
|
|
|
|
"""Overrides open for crm_case for setting Open Date
|
2010-05-05 11:55:13 +00:00
|
|
|
|
@param self: The object pointer
|
|
|
|
|
@param cr: the current row, from the database cursor,
|
|
|
|
|
@param uid: the current user’s ID for security checks,
|
|
|
|
|
@param ids: List of case's Ids
|
|
|
|
|
@param *args: Give Tuple Value
|
|
|
|
|
"""
|
|
|
|
|
res = super(crm_opportunity, self).case_open(cr, uid, ids, *args)
|
|
|
|
|
self.write(cr, uid, ids, {'date_open': time.strftime('%Y-%m-%d %H:%M:%S')})
|
|
|
|
|
return res
|
2010-03-10 07:14:57 +00:00
|
|
|
|
|
2010-11-19 13:48:01 +00:00
|
|
|
|
def onchange_stage_id(self, cr, uid, ids, stage_id, context=None):
|
2010-03-22 10:40:26 +00:00
|
|
|
|
|
|
|
|
|
""" @param self: The object pointer
|
|
|
|
|
@param cr: the current row, from the database cursor,
|
|
|
|
|
@param uid: the current user’s ID for security checks,
|
|
|
|
|
@param ids: List of stage’s IDs
|
|
|
|
|
@stage_id: change state id on run time """
|
2010-02-25 06:55:42 +00:00
|
|
|
|
if not stage_id:
|
2010-02-23 10:35:15 +00:00
|
|
|
|
return {'value':{}}
|
2010-11-19 13:48:01 +00:00
|
|
|
|
|
|
|
|
|
stage = self.pool.get('crm.case.stage').browse(cr, uid, stage_id, context=context)
|
2010-11-01 09:46:17 +00:00
|
|
|
|
|
2010-02-25 06:55:42 +00:00
|
|
|
|
if not stage.on_change:
|
|
|
|
|
return {'value':{}}
|
2010-05-03 12:30:48 +00:00
|
|
|
|
return {'value':{'probability': stage.probability}}
|
2010-03-09 13:04:06 +00:00
|
|
|
|
|
2010-02-28 13:21:56 +00:00
|
|
|
|
_defaults = {
|
2010-04-29 11:35:01 +00:00
|
|
|
|
'company_id': lambda s,cr,uid,c: s.pool.get('res.company')._company_default_get(cr, uid, 'crm.lead', context=c),
|
2010-06-25 17:41:41 +00:00
|
|
|
|
'priority': crm.AVAILABLE_PRIORITIES[2][0],
|
2010-02-28 13:21:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2010-04-01 11:31:30 +00:00
|
|
|
|
def action_makeMeeting(self, cr, uid, ids, context=None):
|
|
|
|
|
"""
|
|
|
|
|
This opens Meeting's calendar view to schedule meeting on current Opportunity
|
|
|
|
|
@param self: The object pointer
|
|
|
|
|
@param cr: the current row, from the database cursor,
|
|
|
|
|
@param uid: the current user’s ID for security checks,
|
|
|
|
|
@param ids: List of Opportunity to Meeting IDs
|
|
|
|
|
@param context: A standard dictionary for contextual values
|
|
|
|
|
|
|
|
|
|
@return : Dictionary value for created Meeting view
|
|
|
|
|
"""
|
|
|
|
|
value = {}
|
2010-11-19 13:48:01 +00:00
|
|
|
|
for opp in self.browse(cr, uid, ids, context=context):
|
2010-04-01 11:31:30 +00:00
|
|
|
|
data_obj = self.pool.get('ir.model.data')
|
|
|
|
|
|
|
|
|
|
# Get meeting views
|
|
|
|
|
result = data_obj._get_id(cr, uid, 'crm', 'view_crm_case_meetings_filter')
|
|
|
|
|
res = data_obj.read(cr, uid, result, ['res_id'])
|
|
|
|
|
id1 = data_obj._get_id(cr, uid, 'crm', 'crm_case_calendar_view_meet')
|
|
|
|
|
id2 = data_obj._get_id(cr, uid, 'crm', 'crm_case_form_view_meet')
|
|
|
|
|
id3 = data_obj._get_id(cr, uid, 'crm', 'crm_case_tree_view_meet')
|
|
|
|
|
if id1:
|
|
|
|
|
id1 = data_obj.browse(cr, uid, id1, context=context).res_id
|
|
|
|
|
if id2:
|
|
|
|
|
id2 = data_obj.browse(cr, uid, id2, context=context).res_id
|
|
|
|
|
if id3:
|
|
|
|
|
id3 = data_obj.browse(cr, uid, id3, context=context).res_id
|
|
|
|
|
|
|
|
|
|
context = {
|
|
|
|
|
'default_opportunity_id': opp.id,
|
|
|
|
|
'default_partner_id': opp.partner_id and opp.partner_id.id or False,
|
2010-06-24 13:14:01 +00:00
|
|
|
|
'default_user_id': uid,
|
2010-04-01 11:31:30 +00:00
|
|
|
|
'default_section_id': opp.section_id and opp.section_id.id or False,
|
|
|
|
|
'default_email_from': opp.email_from,
|
2010-06-24 13:14:01 +00:00
|
|
|
|
'default_state': 'open',
|
2010-04-01 11:31:30 +00:00
|
|
|
|
'default_name': opp.name
|
|
|
|
|
}
|
|
|
|
|
value = {
|
|
|
|
|
'name': _('Meetings'),
|
|
|
|
|
'context': context,
|
|
|
|
|
'view_type': 'form',
|
|
|
|
|
'view_mode': 'calendar,form,tree',
|
|
|
|
|
'res_model': 'crm.meeting',
|
|
|
|
|
'view_id': False,
|
|
|
|
|
'views': [(id1, 'calendar'), (id2, 'form'), (id3, 'tree')],
|
|
|
|
|
'type': 'ir.actions.act_window',
|
|
|
|
|
'search_view_id': res['res_id'],
|
|
|
|
|
'nodestroy': True
|
|
|
|
|
}
|
|
|
|
|
return value
|
|
|
|
|
|
2009-12-31 12:57:20 +00:00
|
|
|
|
crm_opportunity()
|
|
|
|
|
|
2010-05-03 12:30:48 +00:00
|
|
|
|
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
|