#
##############################################################################
-from datetime import datetime
-from osv import fields,osv,orm
+from osv import fields, osv
from tools.translate import _
import crm
+
AVAILABLE_STATES = [
- ('draft','New'),
+ ('draft','Draft'),
('open','Open'),
('cancel', 'Lost'),
('done', 'Converted'),
class crm_opportunity(osv.osv):
""" Opportunity Cases """
+ _order = "priority,date_action,id desc"
+ _inherit = 'crm.lead'
+ _columns = {
+ # From crm.case
+ 'partner_address_id': fields.many2one('res.partner.address', 'Partner Contact', \
+ domain="[('partner_id','=',partner_id)]"),
- _name = "crm.opportunity"
- _description = "Opportunity Cases"
- _order = "id desc"
- _inherit = 'crm.case'
-
- def _compute_openday(self, cr, uid, ids, name, args, context={}):
+ # Opportunity fields
+ 'probability': fields.float('Probability (%)',group_operator="avg"),
+ 'planned_revenue': fields.float('Expected Revenue'),
+ 'ref': fields.reference('Reference', selection=crm._links_get, size=128),
+ 'ref2': fields.reference('Reference 2', selection=crm._links_get, size=128),
+ 'phone': fields.char("Phone", size=64),
+ 'date_deadline': fields.date('Expected Closing'),
+ 'date_action': fields.date('Next Action Date'),
+ 'title_action': fields.char('Next Action', size=64),
+ 'stage_id': fields.many2one('crm.case.stage', 'Stage', domain="[('type','=','opportunity')]"),
+ }
+
+ def _case_close_generic(self, cr, uid, ids, find_stage, *args):
+ res = super(crm_opportunity, self).case_close(cr, uid, ids, *args)
+ for case in self.browse(cr, uid, ids):
+ #if the case is not an opportunity close won't change the stage
+ if not case.type == 'opportunity':
+ return res
+
+ value = {}
+ stage_id = find_stage(cr, uid, 'opportunity', case.section_id.id or False)
+ if stage_id:
+ stage_obj = self.pool.get('crm.case.stage').browse(cr, uid, stage_id)
+ value.update({'stage_id': stage_id})
+ if stage_obj.on_change:
+ value.update({'probability': stage_obj.probability})
+
+ #Done un crm.case
+ #value.update({'date_closed': time.strftime('%Y-%m-%d %H:%M:%S')})
+
+
+ self.write(cr, uid, ids, value)
+ return res
+
+ def case_close(self, cr, uid, ids, *args):
+ """Overrides close for crm_case for setting probability and close date
+ @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 = self._case_close_generic(cr, uid, ids, self._find_won_stage, *args)
+
+ for (id, name) in self.name_get(cr, uid, ids):
+ opp = self.browse(cr, uid, id)
+ if opp.type == 'opportunity':
+ message = _("The opportunity '%s' has been won.") % name
+ self.log(cr, uid, id, message)
+ return res
+ 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
"""
+ res = self._case_close_generic(cr, uid, ids, self._find_lost_stage, *args)
+
+ for (id, name) in self.name_get(cr, uid, ids):
+ opp = self.browse(cr, uid, id)
+ if opp.type == 'opportunity':
+ message = _("The opportunity '%s' has been marked as lost.") % name
+ self.log(cr, uid, id, message)
+ return res
+
+ def case_cancel(self, cr, uid, ids, *args):
+ """Overrides cancel for crm_case for setting probability
+ @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 Openday’s IDs
- @return: difference between current date and log date
- @param context: A standard dictionary for contextual values
+ @param ids: List of case Ids
+ @param *args: Tuple Value for additional Params
"""
- 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={}):
+ res = super(crm_opportunity, self).case_cancel(cr, uid, ids, args)
+ self.write(cr, uid, ids, {'probability' : 0.0})
+ return res
+ 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, 'probability': 0.0})
+ return res
+
+
+ def case_open(self, cr, uid, ids, *args):
+ """Overrides open for crm_case for setting Open Date
+ @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 closeday’s IDs
- @return: difference between current date and closed date
- @param context: A standard dictionary for contextual values
+ @param ids: List of case's Ids
+ @param *args: Give Tuple Value
"""
- result = {}
- for r in self.browse(cr, uid, ids , context):
- result[r.id] = 0
-
- 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
-
- _columns = {
- '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')]"),
- 'priority': fields.selection(crm.AVAILABLE_PRIORITIES, 'Priority'),
- 'probability': fields.float('Probability (%)'),
- 'planned_revenue': fields.float('Expected Revenue'),
- 'ref': fields.reference('Reference', selection=crm._links_get, size=128),
- 'ref2': fields.reference('Reference 2', selection=crm._links_get, size=128),
- 'date_closed': fields.datetime('Closed', readonly=True),
- 'user_id': fields.many2one('res.users', 'Salesman'),
- 'phone': fields.char("Phone", size=64),
- '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\'.'),
-
- '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),
- }
+ res = super(crm_opportunity, self).case_open(cr, uid, ids, *args)
+
+ return res
- def onchange_stage_id(self, cr, uid, ids, stage_id, context={}):
+ def onchange_stage_id(self, cr, uid, ids, stage_id, context=None):
""" @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 """
-
if not stage_id:
return {'value':{}}
+
+ stage = self.pool.get('crm.case.stage').browse(cr, uid, stage_id, context=context)
- 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}}
-
- def stage_next(self, cr, uid, ids, context={}):
-
- """ @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 """
-
- res = super(crm_opportunity, self).stage_next(cr, uid, ids, context=context)
- for case in self.browse(cr, uid, ids, context):
- if case.stage_id and case.stage_id.on_change:
- self.write(cr, uid, [case.id], {'probability': case.stage_id.probability})
- return res
-
- def stage_previous(self, cr, uid, ids, context={}):
-
- """ @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 """
-
- res = super(crm_opportunity, self).stage_previous(cr, uid, ids, context=context)
- for case in self.browse(cr, uid, ids, context):
- if case.stage_id and case.stage_id.on_change:
- self.write(cr, uid, [case.id], {'probability': case.stage_id.probability})
- return res
+ return {'value':{'probability': stage.probability}}
_defaults = {
- 'company_id': lambda s,cr,uid,c: s.pool.get('res.company')._company_default_get(cr, uid, 'crm.opportunity', context=c),
- 'priority': lambda *a: crm.AVAILABLE_PRIORITIES[2][0],
+ 'company_id': lambda s,cr,uid,c: s.pool.get('res.company')._company_default_get(cr, uid, 'crm.lead', context=c),
+ 'priority': crm.AVAILABLE_PRIORITIES[2][0],
}
def action_makeMeeting(self, cr, uid, ids, context=None):
@return : Dictionary value for created Meeting view
"""
value = {}
- for opp in self.browse(cr, uid, ids):
+ for opp in self.browse(cr, uid, ids, context=context):
data_obj = self.pool.get('ir.model.data')
# Get meeting views
context = {
'default_opportunity_id': opp.id,
'default_partner_id': opp.partner_id and opp.partner_id.id or False,
+ 'default_user_id': uid,
'default_section_id': opp.section_id and opp.section_id.id or False,
'default_email_from': opp.email_from,
- 'default_state': 'open',
+ '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',
'search_view_id': res['res_id'],
'nodestroy': True
}
-
return value
crm_opportunity()
+# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: