[commit]
[odoo/odoo.git] / addons / crm / crm_opportunity.py
1 #-*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
6 #
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.
11 #
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.
16 #
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/>.
19 #
20 ##############################################################################
21
22 from osv import fields, osv
23 from tools.translate import _
24 import crm
25 import time
26
27 AVAILABLE_STATES = [
28     ('draft','Draft'),
29     ('open','Open'),
30     ('cancel', 'Lost'),
31     ('done', 'Converted'),
32     ('pending','Pending')
33 ]
34
35 class crm_opportunity(osv.osv):
36     """ Opportunity Cases """
37     _order = "priority,date_action,id desc"
38     _inherit = 'crm.lead'
39     _columns = {
40         # From crm.case
41         'partner_address_id': fields.many2one('res.partner.address', 'Partner Contact', \
42                                  domain="[('partner_id','=',partner_id)]"), 
43
44         # Opportunity fields
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')]"),
54      }
55     
56     def case_close(self, cr, uid, ids, *args):
57         """Overrides close for crm_case for setting probability and close date
58         @param self: The object pointer
59         @param cr: the current row, from the database cursor,
60         @param uid: the current user’s ID for security checks,
61         @param ids: List of case Ids
62         @param *args: Tuple Value for additional Params
63         """
64         res = super(crm_opportunity, self).case_close(cr, uid, ids, args)
65         data_obj = self.pool.get('ir.model.data')
66         data_id = data_obj._get_id(cr, uid, 'crm', 'stage_lead5')
67         stage_id = data_obj.browse(cr, uid, data_id).res_id
68         stage_obj = self.pool.get('crm.case.stage').browse(cr, uid, stage_id)
69         value = {'date_closed': time.strftime('%Y-%m-%d %H:%M:%S'), 'stage_id': stage_id}
70         if stage_obj.on_change:
71             value.update({'probability': stage_obj.probability})
72
73         self.write(cr, uid, ids, value)
74         for (id, name) in self.name_get(cr, uid, ids):
75             opp = self.browse(cr, uid, id)
76             if opp.type == 'opportunity':
77                 message = _("The opportunity '%s' has been won.") % name
78                 self.log(cr, uid, id, message)
79         return res
80
81     def case_mark_lost(self, cr, uid, ids, *args):
82         """Mark the case as lost: state = done and probability = 0%
83         @param self: The object pointer
84         @param cr: the current row, from the database cursor,
85         @param uid: the current user’s ID for security checks,
86         @param ids: List of case Ids
87         @param *args: Tuple Value for additional Params
88         """
89         res = super(crm_opportunity, self).case_close(cr, uid, ids, args)
90         data_obj = self.pool.get('ir.model.data')
91         data_id = data_obj._get_id(cr, uid, 'crm', 'stage_lead6')
92         stage_id = data_obj.browse(cr, uid, data_id).res_id
93         stage_obj = self.pool.get('crm.case.stage').browse(cr, uid, stage_id)
94         value = {'date_closed': time.strftime('%Y-%m-%d %H:%M:%S'), 'stage_id': stage_id}
95         if stage_obj.on_change:
96             value.update({'probability': stage_obj.probability})
97
98         self.write(cr, uid, ids, value)
99         for (id, name) in self.name_get(cr, uid, ids):
100             opp = self.browse(cr, uid, id)
101             if opp.type == 'opportunity':
102                 message = _("The opportunity '%s' has been marked as lost.") % name
103                 self.log(cr, uid, id, message)
104         return res
105     
106     def case_cancel(self, cr, uid, ids, *args):
107         """Overrides cancel for crm_case for setting probability
108         @param self: The object pointer
109         @param cr: the current row, from the database cursor,
110         @param uid: the current user’s ID for security checks,
111         @param ids: List of case Ids
112         @param *args: Tuple Value for additional Params
113         """
114         res = super(crm_opportunity, self).case_cancel(cr, uid, ids, args)
115         self.write(cr, uid, ids, {'probability' : 0.0})
116         return res
117
118     def case_reset(self, cr, uid, ids, *args):
119         """Overrides reset as draft in order to set the stage field as empty
120         @param self: The object pointer
121         @param cr: the current row, from the database cursor,
122         @param uid: the current user’s ID for security checks,
123         @param ids: List of case Ids
124         @param *args: Tuple Value for additional Params
125         """
126         res = super(crm_opportunity, self).case_reset(cr, uid, ids, *args)
127         self.write(cr, uid, ids, {'stage_id': False, 'probability': 0.0})
128         return res
129    
130  
131     def case_open(self, cr, uid, ids, *args):
132         """Overrides open for crm_case for setting Open Date
133         @param self: The object pointer
134         @param cr: the current row, from the database cursor,
135         @param uid: the current user’s ID for security checks,
136         @param ids: List of case's Ids
137         @param *args: Give Tuple Value
138         """
139         res = super(crm_opportunity, self).case_open(cr, uid, ids, *args)
140         self.write(cr, uid, ids, {'date_open': time.strftime('%Y-%m-%d %H:%M:%S')})
141         return res
142
143     def onchange_stage_id(self, cr, uid, ids, stage_id, context={}):
144
145         """ @param self: The object pointer
146             @param cr: the current row, from the database cursor,
147             @param uid: the current user’s ID for security checks,
148             @param ids: List of stage’s IDs
149             @stage_id: change state id on run time """
150         if not stage_id:
151             return {'value':{}}
152
153         stage = self.pool.get('crm.case.stage').browse(cr, uid, stage_id, context)
154
155         if not stage.on_change:
156             return {'value':{}}
157         return {'value':{'probability': stage.probability}}
158
159     _defaults = {
160         'company_id': lambda s,cr,uid,c: s.pool.get('res.company')._company_default_get(cr, uid, 'crm.lead', context=c),
161         'priority': crm.AVAILABLE_PRIORITIES[2][0],
162     }
163
164     def action_makeMeeting(self, cr, uid, ids, context=None):
165         """
166         This opens Meeting's calendar view to schedule meeting on current Opportunity
167         @param self: The object pointer
168         @param cr: the current row, from the database cursor,
169         @param uid: the current user’s ID for security checks,
170         @param ids: List of Opportunity to Meeting IDs
171         @param context: A standard dictionary for contextual values
172
173         @return : Dictionary value for created Meeting view
174         """
175         value = {}
176         for opp in self.browse(cr, uid, ids):
177             data_obj = self.pool.get('ir.model.data')
178
179             # Get meeting views
180             result = data_obj._get_id(cr, uid, 'crm', 'view_crm_case_meetings_filter')
181             res = data_obj.read(cr, uid, result, ['res_id'])
182             id1 = data_obj._get_id(cr, uid, 'crm', 'crm_case_calendar_view_meet')
183             id2 = data_obj._get_id(cr, uid, 'crm', 'crm_case_form_view_meet')
184             id3 = data_obj._get_id(cr, uid, 'crm', 'crm_case_tree_view_meet')
185             if id1:
186                 id1 = data_obj.browse(cr, uid, id1, context=context).res_id
187             if id2:
188                 id2 = data_obj.browse(cr, uid, id2, context=context).res_id
189             if id3:
190                 id3 = data_obj.browse(cr, uid, id3, context=context).res_id
191
192             context = {
193                 'default_opportunity_id': opp.id,
194                 'default_partner_id': opp.partner_id and opp.partner_id.id or False,
195                 'default_user_id': uid, 
196                 'default_section_id': opp.section_id and opp.section_id.id or False,
197                 'default_email_from': opp.email_from,
198                 'default_state': 'open',  
199                 'default_name': opp.name
200             }
201             value = {
202                 'name': _('Meetings'),
203                 'context': context,
204                 'view_type': 'form',
205                 'view_mode': 'calendar,form,tree',
206                 'res_model': 'crm.meeting',
207                 'view_id': False,
208                 'views': [(id1, 'calendar'), (id2, 'form'), (id3, 'tree')],
209                 'type': 'ir.actions.act_window',
210                 'search_view_id': res['res_id'],
211                 'nodestroy': True
212             }
213         return value
214
215 crm_opportunity()
216
217 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: