[FIX]: crm, crm_claim, crm_helpdesk, crm_fundraising: Improvement in tooltips
[odoo/odoo.git] / addons / crm / wizard / crm_send_email.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>). All Rights Reserved
6 #    $Id$
7 #
8 #    This program is free software: you can redistribute it and/or modify
9 #    it under the terms of the GNU General Public License as published by
10 #    the Free Software Foundation, either version 3 of the License, or
11 #    (at your option) any later version.
12 #
13 #    This program is distributed in the hope that it will be useful,
14 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
15 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 #    GNU General Public License for more details.
17 #
18 #    You should have received a copy of the GNU General Public License
19 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 #
21 ##############################################################################
22
23 from crm import crm
24 from osv import osv, fields
25 from tools.translate import _
26 import base64
27 import itertools
28 import tools
29 import re
30
31 class crm_send_new_email_attachment(osv.osv_memory):
32     _name = 'crm.send.mail.attachment'
33
34     _columns = {
35         'binary' : fields.binary('Attachment', required=True),
36         'name' : fields.char('Name', size=128, required=True),
37         'wizard_id' : fields.many2one('crm.send.mail', 'Wizard', required=True),
38     }
39
40 crm_send_new_email_attachment()
41
42 class crm_send_new_email(osv.osv_memory):
43     """ Sends new email for the case"""
44     _name = "crm.send.mail"
45     _description = "Send new email"
46
47     _columns = {
48         'email_to' : fields.char('To', size=512, required=True),
49         'email_from' : fields.char('From', size=128, required=True),
50         'reply_to' : fields.char('Reply To', size=128, required=True, help="Reply-to of the Sales team defined on this case"),
51         'email_cc' : fields.char('CC', size=512, help="Carbon Copy: list of recipients that will receive a copy of this mail"),
52         'subject': fields.char('Subject', size=512, required=True),
53         'body': fields.text('Message Body', required=True),
54         'state': fields.selection(crm.AVAILABLE_STATES, string='Set New State To', required=True),
55         'attachment_ids' : fields.one2many('crm.send.mail.attachment', 'wizard_id'),
56     }
57
58     def action_send(self, cr, uid, ids, context=None):
59         """ This sends an email to ALL the addresses of the selected partners.
60         """
61         hist_obj = self.pool.get('mailgate.message')
62
63         if not context:
64             context = {}
65
66         if not context.get('active_model'):
67             raise osv.except_osv(_('Error'), _('Can not send mail!'))
68
69         model = context.get('active_model')
70         case_pool = self.pool.get(model)
71         res_id = context and context.get('active_id', False) or False
72
73         for obj in self.browse(cr, uid, ids, context=context):
74             attach = [
75                 (x.name, base64.decodestring(x.binary)) for x in obj.attachment_ids
76             ]
77
78             message_id = None
79             ref_id = None
80
81             case = case_pool.browse(cr, uid, res_id, context=context)
82             if context.get('mail', 'new') == 'new':
83                 if case.message_ids:
84                     message_id = case.message_ids[0].message_id
85             elif context.get('mail') == 'forward':
86                 # extract attachements from case and emails according to mode
87                 attachments = []
88                 attach_pool = self.pool.get('ir.attachment')
89                 direct_attachments = attach_pool.search(cr, uid, [('res_model', '=', 'crm.lead'), ('res_id', '=', res_id)], context=context)
90                 attachments += attach_pool.browse(cr, uid, direct_attachments, context=context)
91                 if obj.history in ['latest', 'whole'] and case.message_ids:
92                     msgs = case.message_ids
93                     if obj.history == 'latest':
94                         msgs = msgs[:1]
95                     attachments.extend(itertools.chain(*[m.attachment_ids for m in msgs]))
96                 attach_all = [(a.datas_fname or a.name, base64.decodestring(a.datas)) for a in attachments if a.datas]
97                 attach += attach_all
98
99             else:
100                 hist = hist_obj.browse(cr, uid, res_id, context=context)
101                 message_id = hist.message_id
102                 model = hist.model
103                 case_pool = self.pool.get(model)
104                 res_id = hist.res_id
105                 ref_id = hist.ref_id
106                 case = case_pool.browse(cr, uid, res_id, context=context)
107             emails = [obj.email_to]
108             email_cc = re.findall(r'([^ ,<@]+@[^> ,]+)', obj.email_cc)
109             emails = filter(None, emails)
110             body = obj.body
111
112             body = body and tools.ustr(body) or ''
113             email_from = getattr(obj, 'email_from', False)
114             x_headers = {}
115             if message_id:
116                 x_headers['References'] = "%s" % (message_id)
117
118             flag = tools.email_send(
119                 email_from,
120                 emails,
121                 obj.subject,
122                 body,
123                 email_cc=email_cc,
124                 attach=attach,
125                 reply_to=obj.reply_to,
126                 openobject_id=str(case.id),
127                 x_headers=x_headers
128             )
129
130             if not flag:
131                 raise osv.except_osv(_('Error!'), _('Unable to send mail. Please check SMTP is configured properly.'))
132
133             msg_dict = {'new': 'Send', 'reply': 'Reply', 'forward': 'Forward'}
134             case_pool.history(cr, uid, [case], _(msg_dict[context.get('mail', 'new')]), history=True, \
135                             email=obj.email_to, details=body, \
136                             subject=obj.subject, email_from=email_from, \
137                             email_cc=', '.join(email_cc), message_id=message_id, \
138                             references=ref_id or message_id, attach=attach)
139             if obj.state == 'unchanged':
140                 pass
141             elif obj.state == 'done':
142                 case_pool.case_close(cr, uid, [case.id])
143             elif obj.state == 'draft':
144                 case_pool.case_reset(cr, uid, [case.id])
145             elif obj.state in ['cancel', 'open', 'pending']:
146                 act = 'case_' + obj.state
147                 getattr(case_pool, act)(cr, uid, [case.id])
148
149         return {}
150
151     def default_get(self, cr, uid, fields, context=None):
152         """
153         This function gets default values
154         """
155         if not context:
156             context = {}
157
158         if not context.get('active_model'):
159             raise osv.except_osv(_('Error'), _('Can not send mail!'))
160
161         res = super(crm_send_new_email, self).default_get(cr, uid, fields, context=context)
162
163         if context.get('mail') == 'reply':
164             res.update(self.get_reply_defaults(cr, uid, fields, context=context))
165             return res
166
167         model = context.get('active_model')
168         mod_obj = self.pool.get(model)
169         res_id = context and context.get('active_ids', []) or []
170
171         user_obj = self.pool.get('res.users')
172         user_mail_from = user_obj._get_email_from(cr, uid, [uid], context=context)[uid]
173
174         for case in mod_obj.browse(cr, uid, res_id):
175             if 'email_to' in fields:
176                 res.update({'email_to': case.email_from and tools.ustr(case.email_from) or ''})
177             if 'email_from' in fields:
178                 res.update({'email_from': user_mail_from and tools.ustr(user_mail_from) or ''})
179             if 'reply_to' in fields:
180                 res.update({'reply_to': case.section_id.reply_to})
181             if 'subject' in fields:
182                 res.update({'subject': tools.ustr(context.get('subject', case.name) or '')})
183             if 'email_cc' in fields:
184                 res.update({'email_cc': tools.ustr(case.email_cc or '')})
185             if 'body' in fields:
186                 res.update({'body': u'\n'+(tools.ustr(case.user_id.signature or ''))})
187             if 'state' in fields:
188                 res.update({'state': u'pending'})
189
190         return res
191
192     def get_reply_defaults(self, cr, uid, fields, context=None):
193         """
194         This function gets default values for reply mail
195         """
196         hist_obj = self.pool.get('mailgate.message')
197         res_ids = context and context.get('active_ids', []) or []
198
199         user_obj = self.pool.get('res.users')
200         user_mail_from = user_obj._get_email_from(cr, uid, [uid], context=context)[uid]
201
202         include_original = context and context.get('include_original', False) or False
203         res = {}
204         for hist in hist_obj.browse(cr, uid, res_ids, context=context):
205             model = hist.model
206
207             # In the case where the crm.case does not exist in the database
208             if not model:
209                 return {}
210
211             model_pool = self.pool.get(model)
212             res_id = hist.res_id
213             case = model_pool.browse(cr, uid, res_id)
214             if 'email_to' in fields:
215                 res.update({'email_to': case.email_from and tools.ustr(case.email_from) or False})
216             if 'email_from' in fields:
217                 res.update({'email_from': user_mail_from and tools.ustr(user_mail_from) or False})
218
219             signature = u'\n' + (tools.ustr(case.user_id.signature or '')) + u'\n'
220             original = [signature]
221
222             if include_original == True and 'body' in fields:
223                 header = u'-------- Original Message --------'
224                 sender = u'From: %s' %(tools.ustr(hist.email_from or ''))
225                 to = u'To: %s' % (tools.ustr(hist.email_to or ''))
226                 sentdate = u'Date: %s' % (tools.ustr(hist.date))
227                 desc = u'\n%s'%(tools.ustr(hist.description))
228
229                 original = [signature, header, sender, to, sentdate, desc]
230
231             res['body']= u'\n' + u'\n'.join(original)
232
233             if 'subject' in fields:
234                 res.update({u'subject': u'Re: %s' %(tools.ustr(hist.name or ''))})
235             if 'email_cc' in fields:
236                  res.update({'email_cc': case.email_cc and tools.ustr(case.email_cc) or False})
237             if 'reply_to' in fields:
238                 res.update({'reply_to': case.section_id.reply_to})
239             if 'state' in fields:
240                 res['state'] = u'pending'
241         return res
242
243     def view_init(self, cr, uid, fields_list, context=None):
244         """
245         This function checks for precondition before wizard executes
246         @param self: The object pointer
247         @param cr: the current row, from the database cursor,
248         @param uid: the current user’s ID for security checks,
249         @param fields: List of fields for default value
250         @param context: A standard dictionary for contextual values
251
252         """
253         if not context:
254             context = {}
255
256         if not context.get('active_model'):
257             raise osv.except_osv(_('Error'), _('Can not send mail!'))
258         return True
259
260 crm_send_new_email()
261