Launchpad automatic translations update.
[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         if not html_block:
75             link = '#action=account.action_account_config'
76             payment_header = _('You can finish the configuration in the <a href="%s">Bank&Cash settings</a>') % link
77             amount = _('No online payment acquirers configured')
78             group_ids = self.pool.get('res.users').browse(cr, uid, uid, context=context).groups_id
79             if any(group.is_portal for group in group_ids):
80                 return ''
81         else:
82             payment_header = _('Pay safely online')
83             amount_str = float_repr(amount, self.pool.get('decimal.precision').precision_get(cr, uid, 'Account'))
84             currency_str = currency.symbol or currency.name
85             amount = u"%s %s" % ((currency_str, amount_str) if currency.position == 'before' else (amount_str, currency_str))
86         result =  """<div class="payment_acquirers">
87                          <div class="payment_header">
88                              <div class="payment_amount">%s</div>
89                              %s
90                          </div>
91                          %%s
92                      </div>""" % (amount, payment_header)
93         return result % html_block
94
95     def render_payment_block(self, cr, uid, object, reference, currency, amount, context=None, **kwargs):
96         """ Renders all visible payment acquirer forms for the given rendering context, and
97             return them wrapped in an appropriate HTML block, ready for direct inclusion
98             in an OpenERP v7 form view """
99         acquirer_ids = self.search(cr, uid, [('visible', '=', True)])
100         if not acquirer_ids:
101             return
102         html_forms = []
103         for this in self.browse(cr, uid, acquirer_ids):
104             content = this.render(object, reference, currency, amount, context=context, **kwargs)
105             if content:
106                 html_forms.append(content)
107         html_block = '\n'.join(filter(None,html_forms))
108         return self._wrap_payment_block(cr, uid, html_block, amount, currency, context=context)