[IMP]: crm, mail_gateway: Added new python class crm_case to handle all common functi...
[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 datetime import datetime
23 from osv import fields,osv,orm
24 from tools.translate import _
25 import crm
26 import time
27 import mx.DateTime
28
29 AVAILABLE_STATES = [
30     ('draft','Draft'),
31     ('open','Open'),
32     ('cancel', 'Lost'),
33     ('done', 'Converted'),
34     ('pending','Pending')
35 ]
36
37 class crm_opportunity(osv.osv):
38     """ Opportunity Cases """
39
40     _name = "crm.lead"
41     _description = "Opportunity Cases"
42     _order = "priority,date_action,id desc"
43     _inherit = 'crm.lead'
44
45     _columns = {
46         # From crm.case
47         'partner_address_id': fields.many2one('res.partner.address', 'Partner Contact', \
48                                  domain="[('partner_id','=',partner_id)]"), 
49
50         # Opportunity fields
51         'probability': fields.float('Probability (%)'),
52         'planned_revenue': fields.float('Expected Revenue'),
53         'ref': fields.reference('Reference', selection=crm._links_get, size=128),
54         'ref2': fields.reference('Reference 2', selection=crm._links_get, size=128),
55         'phone': fields.char("Phone", size=64),
56         'date_deadline': fields.date('Expected Closing'),
57         'date_action': fields.date('Next Action'),
58          }
59     
60     def case_close(self, cr, uid, ids, *args):
61         """Overrides close for crm_case for setting probability and close date
62         @param self: The object pointer
63         @param cr: the current row, from the database cursor,
64         @param uid: the current user’s ID for security checks,
65         @param ids: List of case Ids
66         @param *args: Tuple Value for additional Params
67         """
68         res = super(crm_opportunity, self).case_close(cr, uid, ids, args)
69         self.write(cr, uid, ids, {'probability' : 100.0, 'date_close': time.strftime('%Y-%m-%d %H:%M:%S')})
70         return res
71
72     def case_cancel(self, cr, uid, ids, *args):
73         """Overrides cancel for crm_case for setting probability
74         @param self: The object pointer
75         @param cr: the current row, from the database cursor,
76         @param uid: the current user’s ID for security checks,
77         @param ids: List of case Ids
78         @param *args: Tuple Value for additional Params
79         """
80         res = super(crm_opportunity, self).case_cancel(cr, uid, ids, args)
81         self.write(cr, uid, ids, {'probability' : 0.0})
82         return res
83     
84     def case_open(self, cr, uid, ids, *args):
85         """Overrides cancel for crm_case for setting Open Date
86         @param self: The object pointer
87         @param cr: the current row, from the database cursor,
88         @param uid: the current user’s ID for security checks,
89         @param ids: List of case's Ids
90         @param *args: Give Tuple Value
91         """
92         res = super(crm_opportunity, self).case_open(cr, uid, ids, *args)
93         self.write(cr, uid, ids, {'date_open': time.strftime('%Y-%m-%d %H:%M:%S')})
94         return res
95
96     def onchange_stage_id(self, cr, uid, ids, stage_id, context={}):
97
98         """ @param self: The object pointer
99             @param cr: the current row, from the database cursor,
100             @param uid: the current user’s ID for security checks,
101             @param ids: List of stage’s IDs
102             @stage_id: change state id on run time """
103
104         if not stage_id:
105             return {'value':{}}
106
107         stage = self.pool.get('crm.case.stage').browse(cr, uid, stage_id, context)
108         if not stage.on_change:
109             return {'value':{}}
110         return {'value':{'probability': stage.probability}}
111
112     _defaults = {
113         'company_id': lambda s,cr,uid,c: s.pool.get('res.company')._company_default_get(cr, uid, 'crm.lead', context=c),
114         'priority': lambda *a: crm.AVAILABLE_PRIORITIES[2][0],
115     }
116
117     def action_makeMeeting(self, cr, uid, ids, context=None):
118         """
119         This opens Meeting's calendar view to schedule meeting on current Opportunity
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 Opportunity to Meeting IDs
124         @param context: A standard dictionary for contextual values
125
126         @return : Dictionary value for created Meeting view
127         """
128         value = {}
129         for opp in self.browse(cr, uid, ids):
130             data_obj = self.pool.get('ir.model.data')
131
132             # Get meeting views
133             result = data_obj._get_id(cr, uid, 'crm', 'view_crm_case_meetings_filter')
134             res = data_obj.read(cr, uid, result, ['res_id'])
135             id1 = data_obj._get_id(cr, uid, 'crm', 'crm_case_calendar_view_meet')
136             id2 = data_obj._get_id(cr, uid, 'crm', 'crm_case_form_view_meet')
137             id3 = data_obj._get_id(cr, uid, 'crm', 'crm_case_tree_view_meet')
138             if id1:
139                 id1 = data_obj.browse(cr, uid, id1, context=context).res_id
140             if id2:
141                 id2 = data_obj.browse(cr, uid, id2, context=context).res_id
142             if id3:
143                 id3 = data_obj.browse(cr, uid, id3, context=context).res_id
144
145             context = {
146                 'default_opportunity_id': opp.id,
147                 'default_partner_id': opp.partner_id and opp.partner_id.id or False,
148                 'default_section_id': opp.section_id and opp.section_id.id or False,
149                 'default_email_from': opp.email_from,
150                 'default_state': 'open',
151                 'default_name': opp.name
152             }
153             value = {
154                 'name': _('Meetings'),
155                 'domain': "[('user_id','=',%s),('opportunity_id','=',%s)]" % (uid,opp.id),
156                 'context': context,
157                 'view_type': 'form',
158                 'view_mode': 'calendar,form,tree',
159                 'res_model': 'crm.meeting',
160                 'view_id': False,
161                 'views': [(id1, 'calendar'), (id2, 'form'), (id3, 'tree')],
162                 'type': 'ir.actions.act_window',
163                 'search_view_id': res['res_id'],
164                 'nodestroy': True
165             }
166         return value
167
168 crm_opportunity()
169
170 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: