1
# -*- encoding: utf-8 -*-
2
##############################################################################
4
# OpenERP, Open Source Management Solution
5
# Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
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.
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.
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/>.
20
##############################################################################
29
email_send_form = '''<?xml version="1.0" encoding="utf-8"?>
30
<form string="Send sale order/s by Email">
31
<field name="partner_address_id"/>
32
<field name="smtp_server_id"/>
34
<field name="subject"/>
36
<separator string="Message:" colspan="4"/>
37
<field name="content" nolabel="1" colspan="4"/>
41
'smtp_server_id': {'string':"Smtp Server",
43
'relation':'email.smtpclient',
46
'subject': {'string':'Subject',
51
'content': {'string':'Content',
55
'partner_address_id': {'string':"Send Email to",
57
'relation':'res.partner.address',
64
email_done_form = '''<?xml version="1.0" encoding="utf-8"?>
65
<form string="Send sale order/s by Email">
66
<label string="Sent"/>
71
'email_sent': {'string':'Quantity of Emails sent', 'type':'integer', 'readonly': True},
74
def _get_defaults(self, cr, uid, data, context):
75
pool = pooler.get_pool(cr.dbname)
77
po_obj = pool.get('purchase.order').browse(cr, uid, data['id'], context)
79
smtp_server_id = pool.get('email.smtpclient').search(cr, uid, [('active','=',True),('state','=','confirm')], context=False)
80
smtp_server_id = smtp_server_id and smtp_server_id[0] or False
82
return {'smtp_server_id': smtp_server_id,
83
'subject':po_obj.name,
84
'content':po_obj.partner_id.supp_email_content or '',
85
'partner_address_id' : po_obj.partner_address_id.id
88
def _send_mails(self, cr, uid, data, context):
89
pool = pooler.get_pool(cr.dbname)
91
attachment_ids = pool.get('ir.attachment').search(cr, uid,
92
[('res_model', '=', 'purchase.order'),
93
('res_id', '=', po_id)])
95
for attach in pool.get('ir.attachment').browse(cr, uid, attachment_ids):
96
f_name = tempfile.gettempdir() +'/'+ attach.datas_fname
97
open(f_name,'wb').write(base64.decodestring(attach.datas))
98
attachments.append(f_name)
100
po_state = pool.get('purchase.order').browse(cr, uid, po_id).state
101
if po_state == 'cancel' :
102
raise osv.except_osv(_('Error sending email'), _('You can not send email when order is cancelled'))
103
report_name = {'draft' : 'purchase.quotation',
104
'confirmed' : 'purchase.order',
105
'approved' : 'purchase.order',}
106
service = netsvc.LocalService("report."+report_name[po_state]);
107
(result, format) = service.create(cr, uid, [], {}, context)
109
f_name = tempfile.gettempdir() +'/purchase_order.' + format
110
open(f_name,'wb').write(result)
111
attachments.append(f_name)
113
pa_obj = pool.get('res.partner.address')
114
email_to = [pa_obj.browse(cr, uid, data['form']['partner_address_id']).email]
115
state = pool.get('email.smtpclient').send_email(cr, uid,
116
data['form']['smtp_server_id'],
117
email_to, data['form']['subject'],
118
data['form']['content'], attachments)
120
msg_string = 'Please check the Server Configuration!'
122
msg_string = 'Email is send at % successsfully'%email_to[0]
125
class po_send_email(wizard.interface):
129
'actions': [_get_defaults],
130
'result': {'type': 'form', 'arch': email_send_form, 'fields': email_send_fields, 'state':[('end','Cancel'), ('send','Send Email')]}
133
'actions': [_send_mails],
134
'result': {'type': 'form', 'arch': email_done_form, 'fields': email_done_fields, 'state': [('end','Cancel'),] }
137
po_send_email('purchase.order.email_send')