1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Business Applications
5 # Copyright (c) 2012-TODAY OpenERP S.A. <http://openerp.com>
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.
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.
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/>.
20 ##############################################################################
23 from urllib import quote as quote
25 from openerp.osv import osv, fields
26 from openerp.tools.translate import _
27 from openerp.tools import float_repr
29 _logger = logging.getLogger(__name__)
31 from mako.template import Template as MakoTemplate
33 _logger.warning("payment_acquirer: mako templates not available, payment acquirer will not work!")
36 class acquirer(osv.Model):
37 _name = 'portal.payment.acquirer'
38 _description = 'Online Payment Acquirer'
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.)"),
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)):
54 this = self.browse(cr, uid, id)
58 i18n_kind = _(object._description) # may fail to translate, but at least we try
59 result = MakoTemplate(this.form_template).render_unicode(object=object,
65 # context kw would clash with mako internals
67 format_exceptions=True)
70 _logger.exception("failed to render mako template value for payment.acquirer %s: %r", this.name, this.form_template)
73 def _wrap_payment_block(self, cr, uid, html_block, amount, currency, context=None):
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):
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>
92 </div>""" % (amount, payment_header)
93 return result % html_block
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)])
103 for this in self.browse(cr, uid, acquirer_ids):
104 content = this.render(object, reference, currency, amount, context=context, **kwargs)
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)