[IMP] remove subtype canceled & stage change
[odoo/odoo.git] / addons / portal / acquirer.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Business Applications
5 #    Copyright (c) 2012-TODAY OpenERP S.A. <http://openerp.com>
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 logging
23 from urllib import quote as quote
24
25 from openerp.osv import osv, fields
26 from openerp.tools.translate import _
27 from openerp.tools import float_repr
28
29 _logger = logging.getLogger(__name__)
30 try:
31     from mako.template import Template as MakoTemplate
32 except ImportError:
33     _logger.warning("payment_acquirer: mako templates not available, payment acquirer will not work!")
34
35
36 class acquirer(osv.Model):
37     _name = 'portal.payment.acquirer'
38     _description = 'Online Payment Acquirer'
39     
40     _columns = {
41         'name': fields.char('Name', required=True),
42         'form_template': fields.text('Payment form template (HTML)', translate=True, required=True), 
43         'visible': fields.boolean('Visible', help="Make this payment acquirer available in portal forms (Customer invoices, etc.)"),
44     }
45
46     _defaults = {
47         'visible': True,
48     }
49
50     def render(self, cr, uid, id, object, reference, currency, amount, context=None, **kwargs):
51         """ Renders the form template of the given acquirer as a mako template  """
52         if not isinstance(id, (int,long)):
53             id = id[0]
54         this = self.browse(cr, uid, id)
55         if context is None:
56             context = {}
57         try:
58             i18n_kind = _(object._description) # may fail to translate, but at least we try
59             result = MakoTemplate(this.form_template).render_unicode(object=object,
60                                                            reference=reference,
61                                                            currency=currency,
62                                                            amount=amount,
63                                                            kind=i18n_kind,
64                                                            quote=quote,
65                                                            # context kw would clash with mako internals
66                                                            ctx=context,
67                                                            format_exceptions=True)
68             return result.strip()
69         except Exception:
70             _logger.exception("failed to render mako template value for payment.acquirer %s: %r", this.name, this.form_template)
71             return
72
73     def _wrap_payment_block(self, cr, uid, html_block, amount, currency, context=None):
74         payment_header = _('Pay safely online')
75         amount_str = float_repr(amount, self.pool.get('decimal.precision').precision_get(cr, uid, 'Account'))
76         currency_str = currency.symbol or currency.name
77         amount = u"%s %s" % ((currency_str, amount_str) if currency.position == 'before' else (amount_str, currency_str))  
78         result =  """<div class="payment_acquirers">
79                          <div class="payment_header">
80                              <div class="payment_amount">%s</div>
81                              %s
82                          </div>
83                          %%s
84                      </div>""" % (amount, payment_header)
85         return result % html_block
86
87     def render_payment_block(self, cr, uid, object, reference, currency, amount, context=None, **kwargs):
88         """ Renders all visible payment acquirer forms for the given rendering context, and
89             return them wrapped in an appropriate HTML block, ready for direct inclusion
90             in an OpenERP v7 form view """
91         acquirer_ids = self.search(cr, uid, [('visible', '=', True)])
92         if not acquirer_ids:
93             return
94         html_forms = []
95         for this in self.browse(cr, uid, acquirer_ids):
96             html_forms.append(this.render(object, reference, currency, amount, context=context, **kwargs))
97         html_block = '\n'.join(filter(None,html_forms))
98         return self._wrap_payment_block(cr, uid, html_block, amount, currency, context=context)