~camptocamp/openerp-swiss-localization/better-description-jge

« back to all changes in this revision

Viewing changes to l10n_ch_sepa/wizard/wiz_pain_001.py

  • Committer: Yannick Vaucher
  • Date: 2011-11-04 10:40:59 UTC
  • Revision ID: yannick.vaucher@camptocamp.com-20111104104059-zl81j9yxsq47kcve
[ADD] l10n_ch_sepa module

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
##############################################################################
 
3
#
 
4
# Copyright (c) 2011 Camptocamp SA (http://www.camptocamp.com)
 
5
# All Right Reserved
 
6
#
 
7
# Author : Yannick Vaucher (Camptocamp)
 
8
#
 
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
 
14
# Service Company
 
15
#
 
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.
 
20
#
 
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.
 
25
#
 
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/>.
 
28
#
 
29
##############################################################################
 
30
 
 
31
import time
 
32
import base64
 
33
 
 
34
from osv import osv, fields
 
35
import pooler
 
36
from tools.translate import _
 
37
 
 
38
from l10n_ch_sepa.base_sepa.msg_sepa import MsgSEPAFactory
 
39
 
 
40
class WizardPain001(osv.osv_memory):
 
41
    _name="wizard.pain001"
 
42
 
 
43
    _columns={
 
44
        'pain_001_file':fields.binary('XML File', readonly=True)
 
45
    }
 
46
 
 
47
 
 
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
 
55
        return None
 
56
 
 
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'''
 
62
        if cc:
 
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')
 
67
 
 
68
    def _create_attachment(self, cursor, user, data, context=None):
 
69
        ''' Create an attachment using data provided
 
70
            data needed are :
 
71
                - model : type of object to attach to
 
72
                - id : id of object model
 
73
                - base64_data
 
74
        '''
 
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'],
 
82
                'res_id': data['id'],
 
83
            }, context=context)
 
84
 
 
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 '''
 
87
 
 
88
        payment_obj = self.pool.get('payment.order')
 
89
 
 
90
        context = context or {}
 
91
        if isinstance(ids, list):
 
92
            wiz_id = ids[0]
 
93
        else:
 
94
            wiz_id = ids
 
95
        current = self.browse(cursor, user, wiz_id, context)
 
96
 
 
97
        pay_id = context.get('active_id', [])
 
98
 
 
99
        payment = payment_obj.browse(cursor, user, pay_id, context=context)
 
100
 
 
101
        cc = self._get_country_code(payment)
 
102
        pain = self._get_pain_def(cc)
 
103
 
 
104
        pain_001 = pain.compute_export(cursor, user, pay_id, context)
 
105
        pain_001_file = base64.encodestring(pain_001)
 
106
        
 
107
        data = {'base64_data': pain_001_file, 'id': pay_id}
 
108
        data['model'] = 'payment.order'
 
109
        
 
110
        self._create_attachment(cursor, user, data, context=context)
 
111
        
 
112
 
 
113
        current.write({'pain_001_file': pain_001_file})
 
114
        return True
 
115
 
 
116
WizardPain001()
 
117
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: