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