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 osv import fields, osv
23 from tools.translate import _
31 ('done', 'Converted'),
35 class crm_opportunity(osv.osv):
36 """ Opportunity Cases """
37 _order = "priority,date_action,id desc"
41 'partner_address_id': fields.many2one('res.partner.address', 'Partner Contact', \
42 domain="[('partner_id','=',partner_id)]"),
45 'probability': fields.float('Probability (%)',group_operator="avg"),
46 'planned_revenue': fields.float('Expected Revenue'),
47 'ref': fields.reference('Reference', selection=crm._links_get, size=128),
48 'ref2': fields.reference('Reference 2', selection=crm._links_get, size=128),
49 'phone': fields.char("Phone", size=64),
50 'date_deadline': fields.date('Expected Closing'),
51 'date_action': fields.date('Next Action Date'),
52 'title_action': fields.char('Next Action', size=64),
53 'stage_id': fields.many2one('crm.case.stage', 'Stage', domain="[('type','=','opportunity')]"),
56 def _case_close_generic(self, cr, uid, ids, find_stage, *args):
57 res = super(crm_opportunity, self).case_close(cr, uid, ids, *args)
58 for case in self.browse(cr, uid, ids):
59 #if the case is not an opportunity close won't change the stage
60 if not case.type == 'opportunity':
64 stage_id = find_stage(cr, uid, 'opportunity', case.section_id.id or False)
66 stage_obj = self.pool.get('crm.case.stage').browse(cr, uid, stage_id)
67 value.update({'stage_id': stage_id})
68 if stage_obj.on_change:
69 value.update({'probability': stage_obj.probability})
72 #value.update({'date_closed': time.strftime('%Y-%m-%d %H:%M:%S')})
75 self.write(cr, uid, ids, value)
78 def case_close(self, cr, uid, ids, *args):
79 """Overrides close for crm_case for setting probability and close date
80 @param self: The object pointer
81 @param cr: the current row, from the database cursor,
82 @param uid: the current user’s ID for security checks,
83 @param ids: List of case Ids
84 @param *args: Tuple Value for additional Params
86 res = self._case_close_generic(cr, uid, ids, self._find_won_stage, *args)
88 for (id, name) in self.name_get(cr, uid, ids):
89 opp = self.browse(cr, uid, id)
90 if opp.type == 'opportunity':
91 message = _("The opportunity '%s' has been won.") % name
92 self.log(cr, uid, id, message)
95 def case_mark_lost(self, cr, uid, ids, *args):
96 """Mark the case as lost: state = done and probability = 0%
97 @param self: The object pointer
98 @param cr: the current row, from the database cursor,
99 @param uid: the current user’s ID for security checks,
100 @param ids: List of case Ids
101 @param *args: Tuple Value for additional Params
103 res = self._case_close_generic(cr, uid, ids, self._find_lost_stage, *args)
105 for (id, name) in self.name_get(cr, uid, ids):
106 opp = self.browse(cr, uid, id)
107 if opp.type == 'opportunity':
108 message = _("The opportunity '%s' has been marked as lost.") % name
109 self.log(cr, uid, id, message)
112 def case_cancel(self, cr, uid, ids, *args):
113 """Overrides cancel for crm_case for setting probability
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 ids: List of case Ids
118 @param *args: Tuple Value for additional Params
120 res = super(crm_opportunity, self).case_cancel(cr, uid, ids, args)
121 self.write(cr, uid, ids, {'probability' : 0.0})
124 def case_reset(self, cr, uid, ids, *args):
125 """Overrides reset as draft in order to set the stage field as empty
126 @param self: The object pointer
127 @param cr: the current row, from the database cursor,
128 @param uid: the current user’s ID for security checks,
129 @param ids: List of case Ids
130 @param *args: Tuple Value for additional Params
132 res = super(crm_opportunity, self).case_reset(cr, uid, ids, *args)
133 self.write(cr, uid, ids, {'stage_id': False, 'probability': 0.0})
137 def case_open(self, cr, uid, ids, *args):
138 """Overrides open for crm_case for setting Open Date
139 @param self: The object pointer
140 @param cr: the current row, from the database cursor,
141 @param uid: the current user’s ID for security checks,
142 @param ids: List of case's Ids
143 @param *args: Give Tuple Value
145 res = super(crm_opportunity, self).case_open(cr, uid, ids, *args)
149 def onchange_stage_id(self, cr, uid, ids, stage_id, context=None):
151 """ @param self: The object pointer
152 @param cr: the current row, from the database cursor,
153 @param uid: the current user’s ID for security checks,
154 @param ids: List of stage’s IDs
155 @stage_id: change state id on run time """
159 stage = self.pool.get('crm.case.stage').browse(cr, uid, stage_id, context=context)
161 if not stage.on_change:
163 return {'value':{'probability': stage.probability}}
166 'company_id': lambda s,cr,uid,c: s.pool.get('res.company')._company_default_get(cr, uid, 'crm.lead', context=c),
167 'priority': crm.AVAILABLE_PRIORITIES[2][0],
170 def action_makeMeeting(self, cr, uid, ids, context=None):
172 This opens Meeting's calendar view to schedule meeting on current Opportunity
173 @param self: The object pointer
174 @param cr: the current row, from the database cursor,
175 @param uid: the current user’s ID for security checks,
176 @param ids: List of Opportunity to Meeting IDs
177 @param context: A standard dictionary for contextual values
179 @return : Dictionary value for created Meeting view
182 for opp in self.browse(cr, uid, ids, context=context):
183 data_obj = self.pool.get('ir.model.data')
186 result = data_obj._get_id(cr, uid, 'crm', 'view_crm_case_meetings_filter')
187 res = data_obj.read(cr, uid, result, ['res_id'])
188 id1 = data_obj._get_id(cr, uid, 'crm', 'crm_case_calendar_view_meet')
189 id2 = data_obj._get_id(cr, uid, 'crm', 'crm_case_form_view_meet')
190 id3 = data_obj._get_id(cr, uid, 'crm', 'crm_case_tree_view_meet')
192 id1 = data_obj.browse(cr, uid, id1, context=context).res_id
194 id2 = data_obj.browse(cr, uid, id2, context=context).res_id
196 id3 = data_obj.browse(cr, uid, id3, context=context).res_id
199 'default_opportunity_id': opp.id,
200 'default_partner_id': opp.partner_id and opp.partner_id.id or False,
201 'default_user_id': uid,
202 'default_section_id': opp.section_id and opp.section_id.id or False,
203 'default_email_from': opp.email_from,
204 'default_state': 'open',
205 'default_name': opp.name
208 'name': _('Meetings'),
211 'view_mode': 'calendar,form,tree',
212 'res_model': 'crm.meeting',
214 'views': [(id1, 'calendar'), (id2, 'form'), (id3, 'tree')],
215 'type': 'ir.actions.act_window',
216 'search_view_id': res['res_id'],
223 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: