reput rpa changes from rev:2423
[odoo/odoo.git] / addons / crm / wizard / wizard_crm_new_send_email.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution   
5 #    Copyright (C) 2004-2009 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 mx.DateTime import now
24
25 import wizard
26 import netsvc
27 import ir
28 import pooler
29 import tools
30 import base64
31 from tools.translate import _
32
33
34 email_send_form = '''<?xml version="1.0"?>
35 <form string="Mass Mailing">
36     <field name="to"/>
37     <newline/>
38     <field name="cc"/>
39     <newline/>
40     <field name="subject"/>
41     <newline/>
42     <field name="text" />
43     <newline/>
44     <field name="doc1" />
45     <newline/>
46     <field name="doc2" />
47     <newline/>
48     <field name="doc3" />
49 </form>'''
50
51 email_send_fields = {
52     'to': {'string':"To", 'type':'char', 'size':64, 'required':True},
53     'cc': {'string':"CC", 'type':'char', 'size':128,},
54     'subject': {'string':'Subject', 'type':'char', 'size':128, 'required':True},
55     'text': {'string':'Message', 'type':'text_tag', 'required':True},
56     'doc1' :  {'string':"Attachment1", 'type':'binary'},
57     'doc2' :  {'string':"Attachment2", 'type':'binary'},
58     'doc3' :  {'string':"Attachment3", 'type':'binary'},
59 }
60
61 # this sends an email to ALL the addresses of the selected partners.
62 def _mass_mail_send(self, cr, uid, data, context):
63     attach = filter(lambda x: x, [data['form']['doc1'],  data['form']['doc2'],  data['form']['doc3']])
64     attach = map(lambda x: x and ('Attachment'+str(attach.index(x)+1), base64.decodestring(x)), attach)
65
66     pool = pooler.get_pool(cr.dbname)
67     case_pool=pool.get('crm.case')
68
69     case = case_pool.browse(cr,uid,data['ids'])[0]
70     case_pool._history(cr, uid, [case], _('Send'), history=True, email=False)
71     case_pool.write(cr, uid, [case.id], {
72                 'som': False,
73                 'canal_id': False,
74                 })
75     emails = [data['form']['to']] + (data['form']['cc'] or '').split(',')
76     emails = filter(None, emails)
77     body = data['form']['text']
78     if case.user_id.signature:
79         body += '\n\n%s' % (case.user_id.signature)
80     flag = tools.email_send(
81         case.user_id.address_id.email,
82         emails,
83         data['form']['subject'],
84         body,
85         attach=attach
86         case_pool.format_body(body),
87         reply_to=case.section_id.reply_to,
88         tinycrm=str(case.id)
89     )
90     if flag:
91         raise wizard.except_wizard(_('Message!'),("Email Successfully Sent..!!"))   
92     else:
93         raise wizard.except_wizard(_('Warning!'),("Email is not sent Successfully"))
94     return {}
95
96 def _get_info(self, cr, uid, data, context):
97     if not data['id']:
98         return {}
99     pool = pooler.get_pool(cr.dbname)
100     case = pool.get('crm.case').browse(cr,uid,data['ids'])[0]
101     if not case.email_from:
102         raise wizard.except_wizard(_('Error'),_('You must put a Partner eMail to use this action!'))
103     if not case.user_id:
104         raise wizard.except_wizard(_('Error'),_('You must define a responsible user for this case in order to use this action!'))
105     return {'to': case.email_from,'subject': '['+str(case.id)+'] '+case.name,'cc': case.email_cc or ''}
106     
107 class wizard_send_mail(wizard.interface):
108     states = {
109         'init': {
110             'actions': [_get_info],
111             'result': {'type': 'form', 'arch': email_send_form, 'fields': email_send_fields, 'state':[('end','Cancel','gtk-cancel'), ('send','Send Email','gtk-go-forward')]}
112         },
113         'send': {
114             'actions': [_mass_mail_send],
115             'result': {'type': 'state', 'state':'end'}
116         }
117     }
118 wizard_send_mail('crm.new.send.mail')
119 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
120