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

« back to all changes in this revision

Viewing changes to auto_email_account/invoice.py

  • Committer: Jay vora
  • Date: 2008-06-09 08:43:58 UTC
  • Revision ID: jvo@tinyerp.com-c8b7029321d4f9d451745e462547a609739e1c2c

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- encoding: utf-8 -*-
 
2
##############################################################################
 
3
#
 
4
# Copyright (c) 2004-2006 TINY SPRL. (http://tiny.be) All Rights Reserved.
 
5
#
 
6
# WARNING: This program as such is intended to be used by professional
 
7
# programmers who take the whole responsability of assessing all potential
 
8
# consequences resulting from its eventual inadequacies and bugs
 
9
# End users who are looking for a ready-to-use solution with commercial
 
10
# garantees and support are strongly adviced to contract a Free Software
 
11
# Service Company
 
12
#
 
13
# This program is Free Software; you can redistribute it and/or
 
14
# modify it under the terms of the GNU General Public License
 
15
# as published by the Free Software Foundation; either version 2
 
16
# of the License, or (at your option) any later version.
 
17
#
 
18
# This program is distributed in the hope that it will be useful,
 
19
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
20
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
21
# GNU General Public License for more details.
 
22
#
 
23
# You should have received a copy of the GNU General Public License
 
24
# along with this program; if not, write to the Free Software
 
25
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
26
#
 
27
##############################################################################
 
28
 
 
29
from osv import fields,osv
 
30
import tools
 
31
import time
 
32
import pooler
 
33
import netsvc
 
34
from tools.misc import UpdateableStr, UpdateableDict
 
35
import threading
 
36
 
 
37
class Invoice(osv.osv):
 
38
    _inherit = "account.invoice"
 
39
    
 
40
    def send_mail(self, db_name, uid, ids):
 
41
        db, pool = pooler.get_db_and_pool(db_name)
 
42
        cr = db.cursor()
 
43
        
 
44
        result=None
 
45
        context=None
 
46
        
 
47
        smtp = self.pool.get('email.smtpclient')
 
48
        
 
49
        address = self.read(cr, uid, ids ,['address_invoice_id', 'number', 'name'],context=None)[0]
 
50
        email = smtp.selectAddress(cr, uid, partner=None, contact=[address['address_invoice_id'][0]])
 
51
        smtpserver_id = self.pool.get('email.smtpclient').select(cr, uid, 'account')
 
52
        
 
53
        if email:
 
54
            if smtpserver_id is False:
 
55
                raise Exception, 'Please check the Server Configuration !!!'
 
56
            
 
57
            smtpserver = self.pool.get('email.smtpclient').browse(cr, uid, smtpserver_id, context=False)
 
58
            
 
59
            body= smtpserver.body
 
60
            name = self.pool.get('res.partner.address').read(cr, uid, [address['address_invoice_id'][0]], ['name'])[0]['name'] or 'Customer'
 
61
            user = self.pool.get('res.users').read(cr, uid, [uid], ['name'])[0]['name'] or 'Tiny ERP'
 
62
            body = body.replace('__name__', name)
 
63
            body = body.replace('__user__', user)
 
64
            body = body.replace('__number__', str(address['number']))
 
65
            
 
66
            state = smtpserver.send_email(
 
67
                        cr, 
 
68
                        uid, 
 
69
                        [smtpserver_id], 
 
70
                        email, 
 
71
                        "Invoice  : " + str(address['number']),
 
72
                        ids,
 
73
                        body,
 
74
                        'account.invoice',
 
75
                        'Invoice'
 
76
                    )
 
77
            
 
78
            inv_data = self.read(cr, uid, ids ,['partner_id', 'amount_total'],context=None)
 
79
            partner_ids = inv_data[0]['partner_id']
 
80
            total = inv_data[0]['amount_total']
 
81
            
 
82
            event_obj = self.pool.get('res.partner.event')
 
83
            event_obj.create(cr, uid, {'name': 'Invoice: '+ str(address['number']),\
 
84
                    'partner_id': partner_ids[0],\
 
85
                    'date': time.strftime('%Y-%m-%d %H:%M:%S'),\
 
86
                    'user_id': uid,\
 
87
                    'partner_type': 'customer', 
 
88
                    'probability': 1.0,\
 
89
                    'planned_revenue': total,
 
90
                    'description' : body,
 
91
                    'document' : 'account.invoice,'+str(ids[0])})
 
92
            cr.commit()
 
93
            
 
94
            if not state:
 
95
                raise Exception, 'Verification Failed, Please check the Server Configuration!!!'
 
96
        else:
 
97
            model_id=self.pool.get('ir.model').search(cr, uid, [('model','=','account.invoice')], context=False)[0]
 
98
            if smtpserver_id:
 
99
                history = self.pool.get('email.smtpclient.history')
 
100
                history.create(
 
101
                    cr, 
 
102
                    uid,
 
103
                    {
 
104
                        'date_create':time.strftime('%Y-%m-%d %H:%M:%S'),
 
105
                        'server_id' : smtpserver_id,
 
106
                        'name':'The Email is not sent because the Partner have no Email',
 
107
                        'email':'',
 
108
                        'model':model_id,
 
109
                        'resource_id':ids[0]
 
110
                    }
 
111
                )
 
112
        return {}
 
113
    
 
114
    def action_number(self, cr, uid, ids):
 
115
        super(Invoice, self).action_number(cr, uid, ids)
 
116
        cr.commit()
 
117
        thread1 = threading.Thread( target=self.send_mail , args=(cr.dbname, uid, ids))
 
118
        thread1.start()
 
119
        return True
 
120
        
 
121
Invoice()
 
 
b'\\ No newline at end of file'