[MERGE] merge with team1 branch
[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
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_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':
61                 return res
62                 
63             value = {}
64             stage_id = find_stage(cr, uid, 'opportunity', case.section_id.id or False)
65             if stage_id:
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})
70                 
71             #Done un crm.case
72             #value.update({'date_closed': time.strftime('%Y-%m-%d %H:%M:%S')})
73             
74
75             self.write(cr, uid, ids, value)
76         return res
77     
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
85         """
86         res = self._case_close_generic(cr, uid, ids, self._find_won_stage, *args)
87         
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)
93         return res
94
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
102         """
103         res = self._case_close_generic(cr, uid, ids, self._find_lost_stage, *args)
104         
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)
110         return res
111     
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
119         """
120         res = super(crm_opportunity, self).case_cancel(cr, uid, ids, args)
121         self.write(cr, uid, ids, {'probability' : 0.0})
122         return res
123
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
131         """
132         res = super(crm_opportunity, self).case_reset(cr, uid, ids, *args)
133         self.write(cr, uid, ids, {'stage_id': False, 'probability': 0.0})
134         return res
135    
136  
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
144         """
145         res = super(crm_opportunity, self).case_open(cr, uid, ids, *args)
146         
147         return res
148
149     def onchange_stage_id(self, cr, uid, ids, stage_id, context=None):
150
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 """
156         if not stage_id:
157             return {'value':{}}
158         
159         stage = self.pool.get('crm.case.stage').browse(cr, uid, stage_id, context=context)
160
161         if not stage.on_change:
162             return {'value':{}}
163         return {'value':{'probability': stage.probability}}
164
165     _defaults = {
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],
168     }
169
170     def action_makeMeeting(self, cr, uid, ids, context=None):
171         """
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
178
179         @return : Dictionary value for created Meeting view
180         """
181         value = {}
182         for opp in self.browse(cr, uid, ids, context=context):
183             data_obj = self.pool.get('ir.model.data')
184
185             # Get meeting views
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')
191             if id1:
192                 id1 = data_obj.browse(cr, uid, id1, context=context).res_id
193             if id2:
194                 id2 = data_obj.browse(cr, uid, id2, context=context).res_id
195             if id3:
196                 id3 = data_obj.browse(cr, uid, id3, context=context).res_id
197
198             context = {
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
206             }
207             value = {
208                 'name': _('Meetings'),
209                 'context': context,
210                 'view_type': 'form',
211                 'view_mode': 'calendar,form,tree',
212                 'res_model': 'crm.meeting',
213                 'view_id': False,
214                 'views': [(id1, 'calendar'), (id2, 'form'), (id3, 'tree')],
215                 'type': 'ir.actions.act_window',
216                 'search_view_id': res['res_id'],
217                 'nodestroy': True
218             }
219         return value
220
221 crm_opportunity()
222
223 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: