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-03-10 07:14:57 +00:00
|
|
|
|
from datetime import datetime
|
2009-12-30 10:20:44 +00:00
|
|
|
|
from osv import fields,osv,orm
|
2010-04-01 11:31:30 +00:00
|
|
|
|
from tools.translate import _
|
2010-01-13 13:34:09 +00:00
|
|
|
|
import crm
|
2009-12-30 10:20:44 +00:00
|
|
|
|
|
2010-02-19 17:15:16 +00:00
|
|
|
|
AVAILABLE_STATES = [
|
|
|
|
|
('draft','New'),
|
|
|
|
|
('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 """
|
|
|
|
|
|
2009-12-30 10:20:44 +00:00
|
|
|
|
_name = "crm.opportunity"
|
|
|
|
|
_description = "Opportunity Cases"
|
|
|
|
|
_order = "id desc"
|
2010-02-19 17:15:16 +00:00
|
|
|
|
_inherit = 'crm.case'
|
2010-03-10 07:14:57 +00:00
|
|
|
|
|
|
|
|
|
def _compute_openday(self, cr, uid, ids, name, args, context={}):
|
2010-03-22 10:40:26 +00:00
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
@param cr: the current row, from the database cursor,
|
|
|
|
|
@param uid: the current user’s ID for security checks,
|
|
|
|
|
@param ids: List of Openday’s IDs
|
|
|
|
|
@return: difference between current date and log date
|
|
|
|
|
@param context: A standard dictionary for contextual values
|
|
|
|
|
"""
|
2010-03-10 07:14:57 +00:00
|
|
|
|
result = {}
|
|
|
|
|
for r in self.browse(cr, uid, ids , context):
|
|
|
|
|
result[r.id] = 0
|
|
|
|
|
model_id = self.pool.get('ir.model').search(cr, uid, [('model', '=', 'crm.opportunity')])
|
|
|
|
|
log_obj = self.pool.get('crm.case.log')
|
|
|
|
|
hist_id = log_obj.search(cr, uid, [('model_id', '=', model_id[0]), \
|
|
|
|
|
('res_id', '=', r.id), \
|
|
|
|
|
('name', '=', 'Open')])
|
|
|
|
|
|
|
|
|
|
if hist_id:
|
|
|
|
|
# Considering last log for opening case
|
|
|
|
|
log = log_obj.browse(cr, uid, hist_id[-1])
|
|
|
|
|
date_lead_open = datetime.strptime(r.create_date, "%Y-%m-%d %H:%M:%S")
|
|
|
|
|
date_log_open = datetime.strptime(log.date, "%Y-%m-%d %H:%M:%S")
|
|
|
|
|
ans = date_lead_open - date_log_open
|
|
|
|
|
duration = float(ans.days) + (float(ans.seconds) / 86400)
|
|
|
|
|
result[r.id] = abs(int(duration))
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
def _compute_closeday(self, cr, uid, ids, name, args, context={}):
|
2010-03-22 10:40:26 +00:00
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
@param cr: the current row, from the database cursor,
|
|
|
|
|
@param uid: the current user’s ID for security checks,
|
|
|
|
|
@param ids: List of closeday’s IDs
|
|
|
|
|
@return: difference between current date and closed date
|
|
|
|
|
@param context: A standard dictionary for contextual values
|
|
|
|
|
"""
|
2010-03-10 07:14:57 +00:00
|
|
|
|
result = {}
|
|
|
|
|
for r in self.browse(cr, uid, ids , context):
|
|
|
|
|
result[r.id] = 0
|
2010-03-22 10:40:26 +00:00
|
|
|
|
|
2010-03-10 07:14:57 +00:00
|
|
|
|
if r.date_closed:
|
|
|
|
|
date_create = datetime.strptime(r.create_date, "%Y-%m-%d %H:%M:%S")
|
|
|
|
|
date_close = datetime.strptime(r.date_closed, "%Y-%m-%d %H:%M:%S")
|
|
|
|
|
ans = date_close - date_create
|
|
|
|
|
duration = float(ans.days) + (float(ans.seconds) / 86400)
|
|
|
|
|
result[r.id] = abs(int(duration))
|
|
|
|
|
return result
|
|
|
|
|
|
2010-02-19 17:15:16 +00:00
|
|
|
|
_columns = {
|
2010-03-22 10:40:26 +00:00
|
|
|
|
'stage_id': fields.many2one ('crm.case.stage', 'Stage', \
|
|
|
|
|
domain="[('section_id','=',section_id),\
|
|
|
|
|
('object_id.model', '=', 'crm.opportunity')]"),
|
|
|
|
|
'categ_id': fields.many2one('crm.case.categ', 'Category', \
|
|
|
|
|
domain="[('section_id','=',section_id), \
|
|
|
|
|
('object_id.model', '=', 'crm.opportunity')]"),
|
|
|
|
|
'type_id': fields.many2one('crm.case.resource.type', 'Resource Type',\
|
|
|
|
|
domain="[('section_id','=',section_id),('object_id.model', '=', 'crm.opportunity')]"),
|
2010-01-13 13:34:09 +00:00
|
|
|
|
'priority': fields.selection(crm.AVAILABLE_PRIORITIES, 'Priority'),
|
2010-01-11 13:16:46 +00:00
|
|
|
|
'probability': fields.float('Probability (%)'),
|
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-01-11 13:16:46 +00:00
|
|
|
|
'date_closed': fields.datetime('Closed', readonly=True),
|
2010-02-25 06:55:42 +00:00
|
|
|
|
'user_id': fields.many2one('res.users', 'Salesman'),
|
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-02-19 17:15:16 +00:00
|
|
|
|
'state': fields.selection(AVAILABLE_STATES, 'State', size=16, readonly=True,
|
|
|
|
|
help='The state is set to \'Draft\', when a case is created.\
|
|
|
|
|
\nIf the case is in progress the state is set to \'Open\'.\
|
|
|
|
|
\nWhen the case is over, the state is set to \'Done\'.\
|
|
|
|
|
\nIf the case needs to be reviewed then the state is set to \'Pending\'.'),
|
2010-03-10 07:14:57 +00:00
|
|
|
|
|
|
|
|
|
'day_open': fields.function(_compute_openday, string='Days to Open', \
|
|
|
|
|
method=True, type="integer", store=True),
|
|
|
|
|
'day_close': fields.function(_compute_closeday, string='Days to Close', \
|
|
|
|
|
method=True, type="integer", store=True),
|
|
|
|
|
}
|
|
|
|
|
|
2010-02-25 06:55:42 +00:00
|
|
|
|
def onchange_stage_id(self, cr, uid, ids, stage_id, context={}):
|
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-03-22 10:40:26 +00:00
|
|
|
|
|
2010-02-25 06:55:42 +00:00
|
|
|
|
stage = self.pool.get('crm.case.stage').browse(cr, uid, stage_id, context)
|
|
|
|
|
if not stage.on_change:
|
|
|
|
|
return {'value':{}}
|
|
|
|
|
return {'value':{'probability':stage.probability}}
|
2010-01-13 10:31:48 +00:00
|
|
|
|
|
2010-03-09 13:04:06 +00:00
|
|
|
|
def stage_next(self, cr, uid, ids, context={}):
|
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 next’s IDs
|
|
|
|
|
@param context: A standard dictionary for contextual values """
|
|
|
|
|
|
2010-03-09 13:04:06 +00:00
|
|
|
|
res = super(crm_opportunity, self).stage_next(cr, uid, ids, context=context)
|
|
|
|
|
for case in self.browse(cr, uid, ids, context):
|
2010-03-10 07:14:57 +00:00
|
|
|
|
if case.stage_id and case.stage_id.on_change:
|
2010-03-09 13:04:06 +00:00
|
|
|
|
self.write(cr, uid, [case.id], {'probability': case.stage_id.probability})
|
|
|
|
|
return res
|
|
|
|
|
|
|
|
|
|
def stage_previous(self, cr, uid, ids, context={}):
|
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 previous’s IDs
|
|
|
|
|
@param context: A standard dictionary for contextual values """
|
|
|
|
|
|
2010-03-09 13:04:06 +00:00
|
|
|
|
res = super(crm_opportunity, self).stage_previous(cr, uid, ids, context=context)
|
|
|
|
|
for case in self.browse(cr, uid, ids, context):
|
2010-03-10 07:14:57 +00:00
|
|
|
|
if case.stage_id and case.stage_id.on_change:
|
2010-03-09 13:04:06 +00:00
|
|
|
|
self.write(cr, uid, [case.id], {'probability': case.stage_id.probability})
|
2010-03-10 07:14:57 +00:00
|
|
|
|
return res
|
2010-03-09 13:04:06 +00:00
|
|
|
|
|
2010-02-28 13:21:56 +00:00
|
|
|
|
_defaults = {
|
|
|
|
|
'company_id': lambda s,cr,uid,c: s.pool.get('res.company')._company_default_get(cr, uid, 'crm.opportunity', context=c),
|
2010-03-22 08:13:44 +00:00
|
|
|
|
'priority': lambda *a: 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 = {}
|
|
|
|
|
for opp in self.browse(cr, uid, ids):
|
|
|
|
|
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,
|
|
|
|
|
'default_section_id': opp.section_id and opp.section_id.id or False,
|
|
|
|
|
'default_email_from': opp.email_from,
|
|
|
|
|
'default_state': 'open',
|
|
|
|
|
'default_name': opp.name
|
|
|
|
|
}
|
|
|
|
|
value = {
|
|
|
|
|
'name': _('Meetings'),
|
|
|
|
|
'domain': "[('user_id','=',%s)]" % (uid),
|
|
|
|
|
'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()
|
|
|
|
|
|