[IMP] crm:-useability improvements in Reporting/Phone Calls Analysis
[odoo/odoo.git] / addons / crm / crm_phonecall.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 crm import crm_case
23 from osv import fields, osv
24 from tools.translate import _
25 import crm
26 import time
27 from datetime import datetime, timedelta
28
29 class crm_phonecall(crm_case, osv.osv):
30     """ Phonecall Cases """
31
32     _name = "crm.phonecall"
33     _description = "Phonecall"
34     _order = "id desc"
35     _inherit = ['mailgate.thread']
36     _columns = {
37         # From crm.case
38         'id': fields.integer('ID'),
39         'name': fields.char('Name', size=64),
40         'active': fields.boolean('Active', required=False), 
41         'date_action_last': fields.datetime('Last Action', readonly=1),
42         'date_action_next': fields.datetime('Next Action', readonly=1), 
43         'create_date': fields.datetime('Creation Date' , readonly=True),
44         'section_id': fields.many2one('crm.case.section', 'Sales Team', \
45                         select=True, help='Sales team to which Case belongs to.'), 
46         'user_id': fields.many2one('res.users', 'Responsible'), 
47         'partner_id': fields.many2one('res.partner', 'Partner'), 
48         'partner_address_id': fields.many2one('res.partner.address', 'Partner Contact', \
49                                  domain="[('partner_id','=',partner_id)]"), 
50         'company_id': fields.many2one('res.company', 'Company'), 
51         'description': fields.text('Description'), 
52         'state': fields.selection([
53                                     ('draft', 'Todo'), 
54                                     ('open', 'Todo'), 
55                                     ('cancel', 'Cancelled'), 
56                                     ('done', 'Held'), 
57                                     ('pending', 'Not Held'),
58                                 ], 'State', size=16, readonly=True, 
59                                   help='The state is set to \'Draft\', when a case is created.\
60                                   \nIf the case is in progress the state is set to \'Open\'.\
61                                   \nWhen the case is over, the state is set to \'Done\'.\
62                                   \nIf the case needs to be reviewed then the state is set to \'Pending\'.'), 
63         'email_from': fields.char('Email', size=128, help="These people will receive email."), 
64         'stage_id': fields.many2one('crm.case.stage', 'Stage', \
65                             domain="[('section_id','=',section_id),\
66                             ('object_id.model', '=', 'crm.phonecall')]"), 
67         'date_open': fields.datetime('Opened', readonly=True),
68         # phonecall fields
69         'duration': fields.float('Duration', help="Duration in Minutes"), 
70         'categ_id': fields.many2one('crm.case.categ', 'Category', \
71                         domain="[('section_id','=',section_id),\
72                         ('object_id.model', '=', 'crm.phonecall')]"), 
73         'partner_phone': fields.char('Phone', size=32), 
74         'partner_contact': fields.related('partner_address_id', 'name', \
75                                  type="char", string="Contact", size=128), 
76         'partner_mobile': fields.char('Mobile', size=32), 
77         'priority': fields.selection(crm.AVAILABLE_PRIORITIES, 'Priority'), 
78         'canal_id': fields.many2one('res.partner.canal', 'Channel', \
79                         help="The channels represent the different communication\
80                          modes available with the customer." \
81                         " With each commercial opportunity, you can indicate\
82                          the canall which is this opportunity source."), 
83         'date_closed': fields.datetime('Closed', readonly=True), 
84         'date': fields.datetime('Date'), 
85         'opportunity_id': fields.many2one ('crm.lead', 'Opportunity'), 
86         'message_ids': fields.one2many('mailgate.message', 'res_id', 'Messages', domain=[('model','=',_name)]),
87     }
88
89     _defaults = {
90         'date': lambda *a: time.strftime('%Y-%m-%d %H:%M:%S'), 
91         'priority': lambda *a: crm.AVAILABLE_PRIORITIES[2][0], 
92         'state': lambda *a: 'open', 
93         'user_id': lambda self,cr,uid,ctx: uid,
94         'active': lambda *a: 1, 
95     }
96     
97     # From crm.case
98
99     def onchange_partner_address_id(self, cr, uid, ids, add, email=False):
100         res = super(crm_phonecall, self).onchange_partner_address_id(cr, uid, ids, add, email)
101         res.setdefault('value', {})
102         if add:
103             address = self.pool.get('res.partner.address').browse(cr, uid, add)
104             res['value']['partner_phone'] = address.phone
105             res['value']['partner_mobile'] = address.mobile
106         return res
107
108     def case_close(self, cr, uid, ids, *args):
109         """Overrides close for crm_case for setting close date
110         @param self: The object pointer
111         @param cr: the current row, from the database cursor,
112         @param uid: the current user’s ID for security checks,
113         @param ids: List of case Ids
114         @param *args: Tuple Value for additional Params
115         """
116         for phone in self.browse(cr, uid, ids):
117             phone_id= phone.id
118             data = {'date_closed': time.strftime('%Y-%m-%d %H:%M:%S')}
119             if phone.duration <=0:
120                 duration = datetime.now() - datetime.strptime(phone.date, '%Y-%m-%d %H:%M:%S')
121                 data.update({'duration': duration.seconds/float(60)})
122             res = super(crm_phonecall, self).case_close(cr, uid, [phone_id], args)
123             self.write(cr, uid, ids, data)
124         return res
125
126     def case_reset(self, cr, uid, ids, *args):
127         """Resets case as draft
128         @param self: The object pointer
129         @param cr: the current row, from the database cursor,
130         @param uid: the current user’s ID for security checks,
131         @param ids: List of case Ids
132         @param *args: Tuple Value for additional Params
133         """
134         res = super(crm_phonecall, self).case_reset(cr, uid, ids, args)
135         self.write(cr, uid, ids, {'duration': 0.0})
136         return res
137
138
139     def case_open(self, cr, uid, ids, *args):
140         """Overrides cancel for crm_case for setting Open Date
141         @param self: The object pointer
142         @param cr: the current row, from the database cursor,
143         @param uid: the current user’s ID for security checks,
144         @param ids: List of case's Ids
145         @param *args: Give Tuple Value
146         """
147         res = super(crm_phonecall, self).case_open(cr, uid, ids, *args)
148         self.write(cr, uid, ids, {'date_open': time.strftime('%Y-%m-%d %H:%M:%S')})
149         return res
150
151     def action_make_meeting(self, cr, uid, ids, context=None):
152         """
153         This opens Meeting's calendar view to schedule meeting on current Phonecall
154         @param self: The object pointer
155         @param cr: the current row, from the database cursor,
156         @param uid: the current user’s ID for security checks,
157         @param ids: List of Phonecall to Meeting IDs
158         @param context: A standard dictionary for contextual values
159
160         @return : Dictionary value for created Meeting view
161         """
162         value = {}
163         for phonecall in self.browse(cr, uid, ids):
164             data_obj = self.pool.get('ir.model.data')
165
166             # Get meeting views
167             result = data_obj._get_id(cr, uid, 'crm', 'view_crm_case_meetings_filter')
168             res = data_obj.read(cr, uid, result, ['res_id'])
169             id1 = data_obj._get_id(cr, uid, 'crm', 'crm_case_calendar_view_meet')
170             id2 = data_obj._get_id(cr, uid, 'crm', 'crm_case_form_view_meet')
171             id3 = data_obj._get_id(cr, uid, 'crm', 'crm_case_tree_view_meet')
172             if id1:
173                 id1 = data_obj.browse(cr, uid, id1, context=context).res_id
174             if id2:
175                 id2 = data_obj.browse(cr, uid, id2, context=context).res_id
176             if id3:
177                 id3 = data_obj.browse(cr, uid, id3, context=context).res_id
178
179             context = {
180                         'default_phonecall_id': phonecall.id, 
181                         'default_partner_id': phonecall.partner_id and phonecall.partner_id.id or False, 
182                         'default_email': phonecall.email_from , 
183                         'default_name': phonecall.name
184                     }
185
186             value = {
187                 'name': _('Meetings'), 
188                 'domain' : "[('user_id','=',%s)]" % (uid), 
189                 'context': context, 
190                 'view_type': 'form', 
191                 'view_mode': 'calendar,form,tree', 
192                 'res_model': 'crm.meeting', 
193                 'view_id': False, 
194                 'views': [(id1, 'calendar'), (id2, 'form'), (id3, 'tree')], 
195                 'type': 'ir.actions.act_window', 
196                 'search_view_id': res['res_id'], 
197                 'nodestroy': True
198                 }
199
200         return value
201
202 crm_phonecall()
203
204
205 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: