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

« back to all changes in this revision

Viewing changes to l10n_ch_sepa/base_sepa/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 os
 
32
import time
 
33
 
 
34
from mako import exceptions
 
35
from mako.template import Template
 
36
from mako.lookup import TemplateLookup
 
37
 
 
38
import pooler
 
39
import addons
 
40
from osv import osv
 
41
from tools.translate import _
 
42
 
 
43
from msg_sepa import *
 
44
 
 
45
class Pain001(MsgSEPA):
 
46
 
 
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'
 
50
 
 
51
    _data = {}
 
52
 
 
53
    def __init__(self, xsd_path = _DEFAULT_XSD_PATH,
 
54
                       tmpl_dirs = [],
 
55
                       tmpl_name = _DEFAULT_TMPL_NAME):
 
56
        '''tmpl_path : path to mako template'''
 
57
 
 
58
        dirs = [addons.get_module_resource(self._BASE_TMPL_DIR)]
 
59
        for dir in tmpl_dirs:
 
60
            dirs += [addons.get_module_resource(dir)]
 
61
 
 
62
        lookup = TemplateLookup(directories=dirs, input_encoding='utf-8', output_encoding='utf-8')
 
63
        self.mako_tpl = lookup.get_template(tmpl_name)
 
64
        self._xml_data = None
 
65
 
 
66
        xsd_path = addons.get_module_resource(xsd_path)
 
67
        super(Pain001, self).__init__(xsd_path)
 
68
 
 
69
    def _check_data(self):
 
70
        '''
 
71
        Do all data check to ensure no data is missing to generate the XML file
 
72
        '''
 
73
        if not self._data:
 
74
            raise osv.except_osv(_('Error'), _('No data has been entered'))
 
75
 
 
76
        if not self._data['payment']:
 
77
            raise osv.except_osv(_('Error'), _('A payment order is missing'))
 
78
        payment = self._data['payment']
 
79
 
 
80
        if payment.state in ['draft']:
 
81
            raise osv.except_osv(_('ErrorPaymentState'), _('Payment is in draft state. Please confirm it first.'))
 
82
 
 
83
        cp_bank_acc = payment.mode.bank_id
 
84
        if not cp_bank_acc:
 
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'))
 
94
 
 
95
        #Check each invoices
 
96
        for line in payment.line_ids:
 
97
            crd_bank_acc = line.bank_id
 
98
            if not crd_bank_acc:
 
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'))
 
108
 
 
109
    def _gather_payment_data(self, cursor, user, id, context=None):
 
110
        '''
 
111
        Record the payment order data based on its id
 
112
        '''
 
113
        context = context or {}
 
114
 
 
115
        pool = pooler.get_pool(cursor.dbname)
 
116
        payment_obj = pool.get('payment.order')
 
117
 
 
118
        payment = payment_obj.browse(cursor, user, id, context=context)
 
119
        self._data['payment'] = payment
 
120
 
 
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 {}
 
124
 
 
125
        self._gather_payment_data(cursor, user, id, context)
 
126
        self._check_data()
 
127
 
 
128
        try:
 
129
            self._xml_data = self.mako_tpl.render_unicode(order=self._data['payment'],
 
130
                                                          thetime = time,
 
131
                                                          sepa_context={})
 
132
        except Exception:
 
133
            raise Exception(exceptions.text_error_template().render())
 
134
 
 
135
 
 
136
        if not self._xml_data:
 
137
            raise osv.except_osv(_('XML is Empty !'),
 
138
                                 _('An error has occured during XML generation'))
 
139
 
 
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'))
 
144
 
 
145
 
 
146
        return self._xml_data
 
147
 
 
148
MsgSEPAFactory.register_class('pain.001', Pain001)
 
149
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
 
 
b'\\ No newline at end of file'