1
# -*- coding: utf-8 -*-
2
##############################################################################
4
# Copyright (c) 2011 Camptocamp SA (http://www.camptocamp.com)
7
# Author : Yannick Vaucher (Camptocamp)
9
# WARNING: This program as such is intended to be used by professional
10
# programmers who take the whole responsability of assessing all potential
11
# consequences resulting from its eventual inadequacies and bugs
12
# End users who are looking for a ready-to-use solution with commercial
13
# garantees and support are strongly adviced to contract a Free Software
16
# This program is Free Software; you can redistribute it and/or
17
# modify it under the terms of the GNU General Public License
18
# as published by the Free Software Foundation; either version 2
19
# of the License, or (at your option) any later version.
21
# This program is distributed in the hope that it will be useful,
22
# but WITHOUT ANY WARRANTY; without even the implied warranty of
23
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24
# GNU General Public License for more details.
26
# You should have received a copy of the GNU Affero General Public License
27
# along with this program. If not, see <http://www.gnu.org/licenses/>.
29
##############################################################################
34
from mako import exceptions
35
from mako.template import Template
36
from mako.lookup import TemplateLookup
41
from tools.translate import _
43
from msg_sepa import *
45
class Pain001(MsgSEPA):
47
_DEFAULT_XSD_PATH = os.path.join('l10n_ch_sepa', 'base_sepa', 'base_xsd', 'pain.001.001.03.xsd')
48
_BASE_TMPL_DIR = os.path.join('l10n_ch_sepa', 'base_sepa', 'base_template')
49
_DEFAULT_TMPL_NAME = 'pain.001.001.03.xml.mako'
53
def __init__(self, xsd_path = _DEFAULT_XSD_PATH,
55
tmpl_name = _DEFAULT_TMPL_NAME):
56
'''tmpl_path : path to mako template'''
58
dirs = [addons.get_module_resource(self._BASE_TMPL_DIR)]
60
dirs += [addons.get_module_resource(dir)]
62
lookup = TemplateLookup(directories=dirs, input_encoding='utf-8', output_encoding='utf-8')
63
self.mako_tpl = lookup.get_template(tmpl_name)
66
xsd_path = addons.get_module_resource(xsd_path)
67
super(Pain001, self).__init__(xsd_path)
69
def _check_data(self):
71
Do all data check to ensure no data is missing to generate the XML file
74
raise osv.except_osv(_('Error'), _('No data has been entered'))
76
if not self._data['payment']:
77
raise osv.except_osv(_('Error'), _('A payment order is missing'))
78
payment = self._data['payment']
80
if payment.state in ['draft']:
81
raise osv.except_osv(_('ErrorPaymentState'), _('Payment is in draft state. Please confirm it first.'))
83
cp_bank_acc = payment.mode.bank_id
85
raise osv.except_osv(_('ErrorCompanyBank'),
86
_('No company bank is defined in payment'))
87
if not cp_bank_acc.bank.bic:
88
raise osv.except_osv(_('ErrorCompanyBankBIC'),
89
_('The selected company bank has no BIC number'))
90
if not cp_bank_acc.iban and \
91
not cp_bank_acc.acc_number:
92
raise osv.except_osv(_('ErrorCompanyBankAccNumber'),
93
_('The selected company bank has no IBAN and no Account number'))
96
for line in payment.line_ids:
97
crd_bank_acc = line.bank_id
99
raise osv.except_osv(_('ErrorCreditorBank'),
100
_('No bank selected for creditor of invoice %s') %(line.name,))
101
if not crd_bank_acc.bank.bic:
102
raise osv.except_osv(_('ErrorCreditorBankBIC'),
103
_('Creditor bank has no BIC number for invoice %s') %(line.name,))
104
if not crd_bank_acc.iban and \
105
not crd_bank_acc.acc_number:
106
raise osv.except_osv(_('ErrorCompanyBankAccNumber'),
107
_('The selected company bank has no IBAN and no Account number'))
109
def _gather_payment_data(self, cursor, user, id, context=None):
111
Record the payment order data based on its id
113
context = context or {}
115
pool = pooler.get_pool(cursor.dbname)
116
payment_obj = pool.get('payment.order')
118
payment = payment_obj.browse(cursor, user, id, context=context)
119
self._data['payment'] = payment
121
def compute_export(self, cursor, user, id, context=None):
122
'''Compute the payment order 'id' as xml data using mako template'''
123
context = context or {}
125
self._gather_payment_data(cursor, user, id, context)
129
self._xml_data = self.mako_tpl.render_unicode(order=self._data['payment'],
133
raise Exception(exceptions.text_error_template().render())
136
if not self._xml_data:
137
raise osv.except_osv(_('XML is Empty !'),
138
_('An error has occured during XML generation'))
140
# Validate the XML generation
141
if not self._is_xsd_valid():
142
raise osv.except_osv(_('XML is not Valid !'),
143
_('An error has occured during XML generation'))
146
return self._xml_data
148
MsgSEPAFactory.register_class('pain.001', Pain001)
149
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
b'\\ No newline at end of file'