##############################################################################
#
# OpenERP, Open Source Management Solution
-# Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
+# Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
#
##############################################################################
-import time
-import re
-import os
-
-import mx.DateTime
-import base64
-
+from datetime import datetime
+from osv import fields,osv,orm
from tools.translate import _
+import crm
+import time
-import tools
-from osv import fields,osv,orm
-from osv.orm import except_orm
-
-AVAILABLE_PRIORITIES = [
- ('5','Lowest'),
- ('4','Low'),
- ('3','Normal'),
- ('2','High'),
- ('1','Highest')
+AVAILABLE_STATES = [
+ ('draft','Draft'),
+ ('open','Open'),
+ ('cancel', 'Lost'),
+ ('done', 'Converted'),
+ ('pending','Pending')
]
-def _links_get(self, cr, uid, context={}):
- obj = self.pool.get('res.request.link')
- ids = obj.search(cr, uid, [])
- res = obj.read(cr, uid, ids, ['object', 'name'], context)
- return [(r['object'], r['name']) for r in res]
-
-class crm_opportunity_categ(osv.osv):
- _name = "crm.opportunity.categ"
- _description = "Opportunity Categories"
- _columns = {
- 'name': fields.char('Category Name', size=64, required=True),
- 'probability': fields.float('Probability (%)', required=True),
- 'section_id': fields.many2one('crm.case.section', 'Case Section'),
- }
- _defaults = {
- 'probability': lambda *args: 0.0
- }
-crm_opportunity_categ()
-
class crm_opportunity(osv.osv):
- _name = "crm.opportunity"
- _description = "Opportunity Cases"
- _order = "id desc"
- _inherit = 'crm.case'
- _columns = {
- 'stage_id': fields.many2one ('crm.case.stage', 'Stage', domain="[('section_id','=',section_id)]"),
- 'categ_id': fields.many2one('crm.opportunity.categ', 'Category', domain="[('section_id','=',section_id)]"),
- 'category2_id': fields.many2one('crm.case.category2', 'Category Name', domain="[('section_id','=',section_id)]"),
- 'priority': fields.selection(AVAILABLE_PRIORITIES, 'Priority'),
- 'probability': fields.float('Probability (%)'),
- 'planned_revenue': fields.float('Planned Revenue'),
- 'planned_cost': fields.float('Planned Costs'),
- 'canal_id': fields.many2one('res.partner.canal', 'Channel',help="The channels represent the different communication modes available with the customer." \
- " With each commercial opportunity, you can indicate the canall which is this opportunity source."),
- 'som': fields.many2one('res.partner.som', 'State of Mind', help="The minds states allow to define a value scale which represents" \
- "the partner mentality in relation to our services.The scale has" \
- "to be created with a factor for each level from 0 (Very dissatisfied) to 10 (Extremely satisfied)."),
- 'ref' : fields.reference('Reference', selection=_links_get, size=128),
- 'ref2' : fields.reference('Reference 2', selection=_links_get, size=128),
- 'date_closed': fields.datetime('Closed', readonly=True),
- }
+ """ 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)]"),
+
+ # 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),
+ }
+ 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 = 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', '=', 100)]})
+ if not stage_id:
+ raise osv.except_osv(_('Warning !'), _('There is no stage for won opportunities defined for this Sale Team.'))
+ 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)
+ for (id, name) in self.name_get(cr, uid, ids):
+ opp = self.browse(cr, uid, id)
+ if opp.type == 'opportunity':
+ message = _('The Opportunity') + " '" + name + "' "+ _("has been Won.")
+ self.log(cr, uid, id, message)
+ return res
- def msg_new(self, cr, uid, msg):
- mailgate_obj = self.pool.get('mail.gateway')
- msg_body = mailgate_obj.msg_body_get(msg)
- data = {
- 'name': msg['Subject'],
- 'email_from': msg['From'],
- 'email_cc': msg['Cc'],
- 'user_id': False,
- 'description': msg_body['body'],
- 'history_line': [(0, 0, {'description': msg_body['body'], 'email': msg['From'] })],
- }
- res = mailgate_obj.partner_get(cr, uid, msg['From'])
- if res:
- data.update(res)
- res = self.create(cr, uid, data)
+ 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 = 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)]})
+ if not stage_id:
+ raise osv.except_osv(_('Warning !'), _('There is no stage for lost opportunities defined for this Sale Team.'))
+ 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})
+
+ res = self.write(cr, uid, ids, value)
+ for (id, name) in self.name_get(cr, uid, ids):
+ opp = self.browse(cr, uid, id)
+ if opp.type == 'opportunity':
+ message = _('The Opportunity') + " '" + name + "' "+ _("has been Lost.")
+ self.log(cr, uid, id, message)
return res
-crm_opportunity()
+ 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 case Ids
+ @param *args: Tuple Value for additional Params
+ """
+ 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})
+ return True
+
+
+ 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 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
-class crm_opportunity_assign_wizard(osv.osv_memory):
- _name = 'crm.opportunity.assign_wizard'
+ def onchange_stage_id(self, cr, uid, ids, stage_id, context={}):
- _columns = {
- 'section_id': fields.many2one('crm.case.section', 'Section'),
- 'user_id': fields.many2one('res.users', 'Responsible'),
- }
+ """ @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 """
- def _get_default_section(self, cr, uid, context):
- case_id = context.get('active_id',False)
- if not case_id:
- return False
- case_obj = self.pool.get('crm.opportunity')
- case = case_obj.read(cr, uid, case_id, ['state','section_id'])
- if case['state'] in ('done'):
- raise osv.except_osv(_('Error !'), _('You can not assign Closed Case.'))
- return case['section_id']
+ if not stage_id:
+ return {'value':{}}
+ 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}}
_defaults = {
- 'section_id': _get_default_section
+ '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_create(self, cr, uid, ids, context=None):
- case_obj = self.pool.get('crm.opportunity')
- case_id = context.get('active_id',[])
- res = self.read(cr, uid, ids)[0]
- case = case_obj.browse(cr, uid, case_id)
- if case.state in ('done'):
- raise osv.except_osv(_('Error !'), _('You can not assign Closed Case.'))
- new_case_id = case_obj.copy(cr, uid, case_id, default=
- {
- 'section_id':res.get('section_id',False),
- 'user_id':res.get('user_id',False),
- 'case_id' : case.id
- }, context=context)
- case_obj.case_close(cr, uid, [case_id])
-
- data_obj = self.pool.get('ir.model.data')
- result = data_obj._get_id(cr, uid, 'crm', 'view_crm_case_opportunities_filter')
- search_view = data_obj.read(cr, uid, result, ['res_id'])
- value = {
- 'name': _('Opportunity'),
- 'view_type': 'form',
- 'view_mode': 'form,tree',
- 'res_model': 'crm.opportunity',
- 'res_id': int(new_case_id),
- 'type': 'ir.actions.act_window',
- 'search_view_id': search_view['res_id']
- }
+
+ 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_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_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
-crm_opportunity_assign_wizard()
+crm_opportunity()
+
+# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: