~openerp-commiter/openobject-addons/trunk-extra-addons

« back to all changes in this revision

Viewing changes to purchase_email/wizard/wizard_send_email.py

  • Committer: DSH (Open ERP)
  • Date: 2010-02-26 13:57:31 UTC
  • Revision ID: dsh@tinyerp.com-20100226135731-n2ihoydsj7gctwbv
[ADD] new module for purchase email : not completed yet

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- encoding: 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
import wizard
 
23
import pooler
 
24
import tempfile
 
25
import netsvc
 
26
import base64
 
27
from osv import osv
 
28
 
 
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"/>
 
33
                <newline/>
 
34
                <field name="subject"/>
 
35
                <newline/>
 
36
                <separator string="Message:" colspan="4"/>
 
37
                <field name="content" nolabel="1" colspan="4"/>
 
38
        </form>'''
 
39
 
 
40
email_send_fields = {
 
41
    'smtp_server_id': {'string':"Smtp Server",
 
42
                     'type':'many2one',
 
43
                      'relation':'email.smtpclient',
 
44
                       'required':True,
 
45
                       },
 
46
    'subject': {'string':'Subject',
 
47
                 'type':'char', 
 
48
                 'size':64,
 
49
                'required':True,
 
50
                },
 
51
    'content': {'string':'Content', 
 
52
                'type':'text_tag',
 
53
                'required':True,
 
54
                },
 
55
        'partner_address_id': {'string':"Send Email to",
 
56
                                'type':'many2one',
 
57
                                'relation':'res.partner.address',
 
58
                                'required':True,
 
59
                                },
 
60
}
 
61
 
 
62
 
 
63
 
 
64
email_done_form = '''<?xml version="1.0" encoding="utf-8"?>
 
65
<form string="Send sale order/s by Email">
 
66
 <label string="Sent"/>
 
67
</form>'''
 
68
 
 
69
 
 
70
email_done_fields = {
 
71
    'email_sent': {'string':'Quantity of Emails sent', 'type':'integer', 'readonly': True},
 
72
}
 
73
 
 
74
def _get_defaults(self, cr, uid, data, context):
 
75
    pool = pooler.get_pool(cr.dbname)
 
76
    
 
77
    po_obj = pool.get('purchase.order').browse(cr, uid, data['id'], context)
 
78
    
 
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
 
81
    
 
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
 
86
                }
 
87
 
 
88
def _send_mails(self, cr, uid, data, context):
 
89
        pool = pooler.get_pool(cr.dbname)
 
90
        po_id = data['id']
 
91
        attachment_ids = pool.get('ir.attachment').search(cr, uid, 
 
92
                                                                                        [('res_model', '=', 'purchase.order'),
 
93
                                                                                        ('res_id', '=', po_id)])
 
94
        attachments = []
 
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)
 
99
                
 
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)
 
108
        
 
109
        f_name = tempfile.gettempdir() +'/purchase_order.' + format
 
110
        open(f_name,'wb').write(result) 
 
111
        attachments.append(f_name)
 
112
        
 
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)
 
119
        if not state:
 
120
                msg_string = 'Please check the Server Configuration!'
 
121
        else :
 
122
                msg_string = 'Email is send at % successsfully'%email_to[0]
 
123
        return {}
 
124
 
 
125
class po_send_email(wizard.interface):
 
126
 
 
127
    states = {
 
128
        'init': {
 
129
            'actions': [_get_defaults],
 
130
            'result': {'type': 'form', 'arch': email_send_form, 'fields': email_send_fields, 'state':[('end','Cancel'), ('send','Send Email')]}
 
131
        },
 
132
        'send': {
 
133
            'actions': [_send_mails],
 
134
            'result': {'type': 'form', 'arch': email_done_form, 'fields': email_done_fields, 'state': [('end','Cancel'),]  }
 
135
        }
 
136
    }
 
137
po_send_email('purchase.order.email_send')