~ecommerce-addons-core-editors/e-commerce-addons/github-6.1

« back to all changes in this revision

Viewing changes to sale_exceptions/sale.py

  • Committer: Maxime Chambreuil
  • Author(s): Sébastien Beau
  • Date: 2013-06-21 12:13:42 UTC
  • mfrom: (281.2.6)
  • Revision ID: git-v1:7bc62a678df102e0751b58c6a34d236af723ebd0
[MERGE] Add the posibility to send on email when an order have an exception

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
#
4
4
#    OpenERP, Open Source Management Solution
5
5
#    Copyright (C) 2011 Akretion LTDA.
6
 
#    Copyright (C) 2010-2012 Akretion Sébastien BEAU <sebastien.beau@akretion.com>
 
6
#    Copyright (C) 2010-2013 Akretion
 
7
#    @author Sébastien BEAU <sebastien.beau@akretion.com>
 
8
#            Chafique DELLI <chafique.delli@akretion.com>
7
9
#    Copyright (C) 2012 Camptocamp SA (Guewen Baconnier)
8
10
#
9
11
#    This program is free software: you can redistribute it and/or modify
21
23
#
22
24
##############################################################################
23
25
 
 
26
 
24
27
import time
25
28
import netsvc
26
29
 
29
32
from openerp.osv.osv import except_osv
30
33
from tools.safe_eval import safe_eval as eval
31
34
from tools.translate import _
 
35
import logging
 
36
 
 
37
_logger = logging.getLogger(__name__)
32
38
 
33
39
class sale_exception(Model):
34
40
    _name = "sale.exception"
47
53
        'sale_order_ids': fields.many2many('sale.order', 'sale_order_exception_rel',
48
54
                                           'exception_id', 'sale_order_id',
49
55
                                           string='Sale Orders', readonly=True),
 
56
        'send_email': fields.boolean('Notify exception by Email',
 
57
                                     help=("If true, a notification will be send "
 
58
                                     "by email at the creation of the exception")),
50
59
    }
51
60
 
52
61
    _defaults = {
136
145
        return True
137
146
 
138
147
    def detect_exceptions(self, cr, uid, ids, context=None):
 
148
        email_obj = self.pool.get('email.template')
139
149
        exception_obj = self.pool.get('sale.exception')
 
150
        model_data_obj = self.pool.get('ir.model.data')
140
151
        order_exception_ids = exception_obj.search(cr, uid,
141
152
            [('model', '=', 'sale.order')], context=context)
142
153
        line_exception_ids = exception_obj.search(cr, uid,
151
162
                continue
152
163
            exception_ids = self._detect_exceptions(cr, uid, order,
153
164
                order_exceptions, line_exceptions, context=context)
 
165
            if exception_ids:
 
166
                notify = any(exception.send_email for exception in
 
167
                             exception_obj.browse(cr, uid, exception_ids,
 
168
                             context=context))
154
169
 
 
170
                #We do not notify if the order already have exception
 
171
                if notify and not order.exception_ids:
 
172
                    model, email_tmpl_id = model_data_obj.get_object_reference(
 
173
                                                    cr, uid, 'sale_exceptions',
 
174
                                                    'email_template_sale_exceptions')
 
175
                    cr.execute('SAVEPOINT send_email')
 
176
                    try:
 
177
                        email_obj.send_mail(cr, uid, email_tmpl_id,
 
178
                            order.id, force_send=True, context=context)
 
179
                    except Exception, e:
 
180
                        _logger.exception(e)
 
181
                        cr.execute('ROLLBACK TO SAVEPOINT send_email')
 
182
                    else:
 
183
                        cr.execute('RELEASE SAVEPOINT send_email')
155
184
            self.write(cr, uid, [order.id], {'exceptions_ids': [(6, 0, exception_ids)]})
156
185
        return exception_ids
157
186