[ADD] mail_gate : new module for mail_gate and Remove the gateway stuff from crm
[odoo/odoo.git] / addons / crm / crm_phonecall.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #    
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2009 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 caldav import common
23 from dateutil.rrule import *
24 from osv import fields, osv
25 import  datetime
26 import base64
27 import re
28 import time
29 import tools
30
31 from tools.translate import _
32
33 class crm_phonecall(osv.osv):
34     _name = "crm.phonecall"
35     _description = "Phonecall Cases"
36     _order = "id desc"
37     _inherits = {'crm.case':"inherit_case_id"}    
38     _columns = {        
39         'inherit_case_id': fields.many2one('crm.case','Case',ondelete='cascade'),
40     }
41     
42     def _map_ids(self, method, cr, uid, ids, *args, **argv):
43         if isinstance(ids, (str, int, long)):
44             select = [ids]
45         else:
46             select = ids            
47         case_data = self.browse(cr, uid, select)
48         new_ids = []
49         for case in case_data:
50             if case.inherit_case_id:
51                 new_ids.append(case.inherit_case_id.id)
52         res = getattr(self.pool.get('crm.case'),method)(cr, uid, new_ids, *args, **argv)
53         if isinstance(ids, (str, int, long)) and isinstance(res, list):
54             return res and res[0] or False
55         return res
56
57
58     def onchange_case_id(self, cr, uid, ids, *args, **argv):
59         return self._map_ids('onchange_case_id',cr,uid,ids,*args,**argv)
60     def onchange_partner_id(self, cr, uid, ids, *args, **argv):
61         return self._map_ids('onchange_partner_id',cr,uid,ids,*args,**argv)
62     def onchange_partner_address_id(self, cr, uid, ids, *args, **argv):
63         return self._map_ids('onchange_partner_address_id',cr,uid,ids,*args,**argv)
64     def onchange_categ_id(self, cr, uid, ids, *args, **argv):
65         return self._map_ids('onchange_categ_id',cr,uid,ids,*args,**argv)
66     def case_close(self,cr, uid, ids, *args, **argv):
67         return self._map_ids('case_close',cr,uid,ids,*args,**argv)    
68     def case_open(self,cr, uid, ids, *args, **argv):
69         return self._map_ids('case_open',cr,uid,ids,*args,**argv)
70     def case_cancel(self,cr, uid, ids, *args, **argv):
71         return self._map_ids('case_cancel',cr,uid,ids,*args,**argv)
72     def case_reset(self,cr, uid, ids, *args, **argv):
73         return self._map_ids('case_reset',cr,uid,ids,*args,**argv)    
74     def case_escalate(self,cr, uid, ids, *args, **argv):    
75         return self._map_ids('case_escalate',cr,uid,ids,*args,**argv)    
76     def case_pending(self,cr, uid, ids, *args, **argv):    
77         return self._map_ids('case_pending',cr,uid,ids,*args,**argv)
78
79     def msg_new(self, cr, uid, msg):        
80         mailgate_obj = self.pool.get('mail.gateway')
81         msg_body = mailgate_obj.msg_body_get(msg)
82         data = {
83             'name': msg['Subject'],            
84             'email_from': msg['From'],
85             'email_cc': msg['Cc'],            
86             'user_id': False,
87             'description': msg_body['body'],
88             'history_line': [(0, 0, {'description': msg_body['body'], 'email': msg['From'] })],
89         }
90         res = mailgate_obj.partner_get(cr, uid, msg['From'])
91         if res:
92             data.update(res)
93         res = self.create(cr, uid, data)        
94         return res
95
96     def msg_update(self, cr, uid, ids, *args, **argv):
97         return self._map_ids('msg_update',cr, uid, ids, *args, **argv)
98     def emails_get(self, cr, uid, ids, *args, **argv):
99         return self._map_ids('emails_get',cr, uid, ids, *args, **argv)
100     def msg_send(self, cr, uid, ids, *args, **argv):        
101         return self._map_ids('msg_send',cr, uid, ids, *args, **argv)  
102         
103 crm_phonecall()
104
105
106 class crm_phonecall_assign_wizard(osv.osv_memory):
107     _name = 'crm.phonecall.assign_wizard'
108
109     _columns = {
110         'section_id': fields.many2one('crm.case.section', 'Section', required=True),
111         'user_id': fields.many2one('res.users', 'Responsible'),
112     }
113
114     def _get_default_section(self, cr, uid, context):
115         case_id = context.get('active_id',False)
116         if not case_id:
117             return False
118         case_obj = self.pool.get('crm.phonecall')
119         case = case_obj.read(cr, uid, case_id, ['state','section_id'])
120         if case['state'] in ('done'):
121             raise osv.except_osv(_('Error !'), _('You can not assign Closed Case.'))
122         return case['section_id']
123
124
125     _defaults = {
126         'section_id': _get_default_section
127     }
128     def action_create(self, cr, uid, ids, context=None):
129         case_obj = self.pool.get('crm.phonecall')
130         case_id = context.get('active_id',[])
131         res = self.read(cr, uid, ids)[0]
132         case = case_obj.browse(cr, uid, case_id)
133         if case.state in ('done'):
134             raise osv.except_osv(_('Error !'), _('You can not assign Closed Case.'))
135         new_case_id = case_obj.copy(cr, uid, case_id, default=
136                                             {
137                                                 'section_id':res.get('section_id',False),
138                                                 'user_id':res.get('user_id',False),
139                                                 'case_id' : case.inherit_case_id.id
140                                             }, context=context)        
141         case_obj.case_close(cr, uid, [case_id])
142         data_obj = self.pool.get('ir.model.data')
143         result = data_obj._get_id(cr, uid, 'crm', 'view_crm_case_phonecalls_filter')
144         search_view = data_obj.read(cr, uid, result, ['res_id'])
145         value = {            
146             'name': _('Phone Calls'),
147             'view_type': 'form',
148             'view_mode': 'form,tree',
149             'res_model': 'crm.phonecall',
150             'res_id': int(new_case_id),            
151             'type': 'ir.actions.act_window',
152             'search_view_id': search_view['res_id']
153         }
154         return value
155
156 crm_phonecall_assign_wizard()