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 osv import osv, fields
36
from tools.translate import _
38
from l10n_ch_sepa.base_sepa.msg_sepa import MsgSEPAFactory
40
class WizardPain001(osv.osv_memory):
41
_name="wizard.pain001"
44
'pain_001_file':fields.binary('XML File', readonly=True)
48
def _get_country_code(self, payment):
49
''' return the coutry code or None
50
from the bank defined in a payment order'''
51
if payment.mode.bank_id.bank.country:
52
return payment.mode.bank_id.bank.country.code
53
elif payment.user_id.company_id.partner_id.country:
54
return payment.user_id.company_id.partner_id.country.code
57
def _get_pain_def(self, cc):
58
''' Get the right message definition based on country code
59
of selected company bank (via payment mode)
60
if no country is defined, take the company country
61
- Here we could add a second level for bank definitions'''
63
class_name = 'pain.001' + '.' + cc.lower()
64
if MsgSEPAFactory.has_instance(class_name):
65
return MsgSEPAFactory.get_instance(class_name)
66
return MsgSEPAFactory.get_instance('pain.001')
68
def _create_attachment(self, cursor, user, data, context=None):
69
''' Create an attachment using data provided
71
- model : type of object to attach to
72
- id : id of object model
75
context = context or {}
76
attachment_obj = self.pool.get('ir.attachment')
77
attachment_obj.create(cursor, user, {
78
'name': 'pain001_%s' %time.strftime("%Y-%m-%d_%H:%M:%S", time.gmtime()),
79
'datas': data['base64_data'],
80
'datas_fname': 'pain001_%s.xml' %time.strftime("%Y-%m-%d_%H:%M:%S", time.gmtime()),
81
'res_model': data['model'],
85
def create_pain_001(self, cursor, user, ids, context=None):
86
''' create a pain 001 file into wizard and add it as an attachment '''
88
payment_obj = self.pool.get('payment.order')
90
context = context or {}
91
if isinstance(ids, list):
95
current = self.browse(cursor, user, wiz_id, context)
97
pay_id = context.get('active_id', [])
99
payment = payment_obj.browse(cursor, user, pay_id, context=context)
101
cc = self._get_country_code(payment)
102
pain = self._get_pain_def(cc)
104
pain_001 = pain.compute_export(cursor, user, pay_id, context)
105
pain_001_file = base64.encodestring(pain_001)
107
data = {'base64_data': pain_001_file, 'id': pay_id}
108
data['model'] = 'payment.order'
110
self._create_attachment(cursor, user, data, context=context)
113
current.write({'pain_001_file': pain_001_file})
117
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: