[FIX,IMP]: crm: Fixes and improvement in yml tests + security csv
[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
28 class crm_phonecall(osv.osv, crm_case):
29     """ Phonecall Cases """
30
31     _name = "crm.phonecall"
32     _description = "Phonecall Cases"
33     _order = "id desc"
34     _inherit = 'mailgate.thread'
35
36     _columns = {
37         # From crm.case
38         'section_id': fields.many2one('crm.case.section', 'Sales Team', \
39                         select=True, help='Sales team to which Case belongs to.\
40                              Define Responsible user and Email account for mail gateway.'), 
41         'user_id': fields.many2one('res.users', 'Responsible'), 
42         'partner_id': fields.many2one('res.partner', 'Partner'), 
43         'partner_address_id': fields.many2one('res.partner.address', 'Partner Contact', \
44                                  domain="[('partner_id','=',partner_id)]"), 
45         'company_id': fields.many2one('res.company', 'Company'), 
46         'description': fields.text('Description'), 
47         'state': fields.selection(crm.AVAILABLE_STATES, 'State', size=16, readonly=True, 
48                                   help='The state is set to \'Draft\', when a case is created.\
49                                   \nIf the case is in progress the state is set to \'Open\'.\
50                                   \nWhen the case is over, the state is set to \'Done\'.\
51                                   \nIf the case needs to be reviewed then the state is set to \'Pending\'.'), 
52         'email_from': fields.char('Email', size=128, help="These people will receive email."), 
53         'stage_id': fields.many2one('crm.case.stage', 'Stage', \
54                             domain="[('section_id','=',section_id),\
55                             ('object_id.model', '=', 'crm.phonecall')]"), 
56         'date_open': fields.datetime('Opened', readonly=True),
57         # phonecall fields
58         'duration': fields.float('Duration'), 
59         'categ_id': fields.many2one('crm.case.categ', 'Category', \
60                         domain="[('section_id','=',section_id),\
61                         ('object_id.model', '=', 'crm.phonecall')]"), 
62         'partner_phone': fields.char('Phone', size=32), 
63         'partner_contact': fields.related('partner_address_id', 'name', \
64                                  type="char", string="Contact", size=128), 
65         'partner_mobile': fields.char('Mobile', size=32), 
66         'priority': fields.selection(crm.AVAILABLE_PRIORITIES, 'Priority'), 
67         'canal_id': fields.many2one('res.partner.canal', 'Channel', \
68                         help="The channels represent the different communication\
69                          modes available with the customer." \
70                         " With each commercial opportunity, you can indicate\
71                          the canall which is this opportunity source."), 
72         'date_closed': fields.datetime('Closed', readonly=True), 
73         'date': fields.datetime('Date'), 
74         'opportunity_id': fields.many2one ('crm.lead', 'Opportunity'), 
75     }
76
77     _defaults = {
78         'date': lambda *a: time.strftime('%Y-%m-%d %H:%M:%S'), 
79         'priority': lambda *a: crm.AVAILABLE_PRIORITIES[2][0], 
80         'state': lambda *a: 'draft', 
81     }
82     
83     # From crm.case
84
85     def onchange_partner_address_id(self, cr, uid, ids, add, email=False):
86         res = super(crm_phonecall, self).onchange_partner_address_id(cr, uid, ids, add, email)
87         res.setdefault('value', {})
88         if add:
89             address = self.pool.get('res.partner.address').browse(cr, uid, add)
90             res['value']['partner_phone'] = address.phone
91             res['value']['partner_mobile'] = address.mobile
92         return res
93
94     def case_close(self, cr, uid, ids, *args):
95         """Overrides close for crm_case for setting close date
96         @param self: The object pointer
97         @param cr: the current row, from the database cursor,
98         @param uid: the current user’s ID for security checks,
99         @param ids: List of case Ids
100         @param *args: Tuple Value for additional Params
101         """
102         res = super(crm_phonecall, self).case_close(cr, uid, ids, args)
103         self.write(cr, uid, ids, {'date_closed': time.strftime('%Y-%m-%d %H:%M:%S')})
104         return res
105
106     def case_open(self, cr, uid, ids, *args):
107         """Overrides cancel for crm_case for setting Open Date
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's Ids
112         @param *args: Give Tuple Value
113         """
114         res = super(crm_phonecall, self).case_open(cr, uid, ids, *args)
115         self.write(cr, uid, ids, {'date_open': time.strftime('%Y-%m-%d %H:%M:%S')})
116         return res
117
118     def action_make_meeting(self, cr, uid, ids, context=None):
119         """
120         This opens Meeting's calendar view to schedule meeting on current Phonecall
121         @param self: The object pointer
122         @param cr: the current row, from the database cursor,
123         @param uid: the current user’s ID for security checks,
124         @param ids: List of Phonecall to Meeting IDs
125         @param context: A standard dictionary for contextual values
126
127         @return : Dictionary value for created Meeting view
128         """
129         value = {}
130         for phonecall in self.browse(cr, uid, ids):
131             data_obj = self.pool.get('ir.model.data')
132
133             # Get meeting views
134             result = data_obj._get_id(cr, uid, 'crm', 'view_crm_case_meetings_filter')
135             res = data_obj.read(cr, uid, result, ['res_id'])
136             id1 = data_obj._get_id(cr, uid, 'crm', 'crm_case_calendar_view_meet')
137             id2 = data_obj._get_id(cr, uid, 'crm', 'crm_case_form_view_meet')
138             id3 = data_obj._get_id(cr, uid, 'crm', 'crm_case_tree_view_meet')
139             if id1:
140                 id1 = data_obj.browse(cr, uid, id1, context=context).res_id
141             if id2:
142                 id2 = data_obj.browse(cr, uid, id2, context=context).res_id
143             if id3:
144                 id3 = data_obj.browse(cr, uid, id3, context=context).res_id
145
146             context = {
147                         'default_phonecall_id': phonecall.id, 
148                         'default_partner_id': phonecall.partner_id and phonecall.partner_id.id or False, 
149                         'default_email': phonecall.email_from , 
150                         'default_name': phonecall.name
151                     }
152
153             value = {
154                 'name': _('Meetings'), 
155                 'domain' : "[('user_id','=',%s)]" % (uid), 
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
167         return value
168
169 crm_phonecall()
170
171 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: