~openerp-spain-team/openerp-spain/6.0-git

« back to all changes in this revision

Viewing changes to l10n_es_extras/l10n_ES_remesas/wizard/converter.py

  • Committer: Borja L.S.
  • Date: 2010-10-18 10:04:25 UTC
  • Revision ID: git-v1:271c47a993616dbba60585d48b8b98d603199d93
[REF] *: Refactorización para portar a 6.0 - Paso 1.

- Se han renombrado los módulos para usar la nomenclatura propuesta
  por OpenERP: l10n_es para el módulo base de localización (plan de 
  cuentas), l10n_es_* para el resto de módulos.

- Se eliminan los módulos extra_addons/* que deberían moverse a 
  los extra-addons genéricos (no son específicos de España).

- Se renombran los __terp__.py por __openerp__.py

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# -*- encoding: utf-8 -*-
2
 
##############################################################################
3
 
#
4
 
#    OpenERP, Open Source Management Solution
5
 
#    Copyright (c) 2006 ACYSOS S.L. (http://acysos.com) All Rights Reserved.
6
 
#                       Pedro Tarrafeta <pedro@acysos.com>
7
 
#    Copyright (c) 2008 Pablo Rocandio. All Rights Reserved.
8
 
#    Copyright (c) 2009 Zikzakmedia S.L. (http://zikzakmedia.com) All Rights Reserved.
9
 
#                       Jordi Esteve <jesteve@zikzakmedia.com>
10
 
#    Copyright (c) 2009 NaN (http://www.nan-tic.com) All Rights Reserved.
11
 
#                       Albert Cervera i Areny <albert@nan-tic.com>
12
 
#    $Id$
13
 
#
14
 
#    This program is free software: you can redistribute it and/or modify
15
 
#    it under the terms of the GNU General Public License as published by
16
 
#    the Free Software Foundation, either version 3 of the License, or
17
 
#    (at your option) any later version.
18
 
#
19
 
#    This program is distributed in the hope that it will be useful,
20
 
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 
#    GNU General Public License for more details.
23
 
#
24
 
#    You should have received a copy of the GNU General Public License
25
 
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
26
 
#
27
 
##############################################################################
28
 
 
29
 
from tools.translate import _
30
 
 
31
 
def digits_only(cc_in):
32
 
    """Discards non-numeric chars"""
33
 
 
34
 
    cc = ""
35
 
    for i in cc_in:
36
 
        try:
37
 
            int(i)
38
 
            cc += i
39
 
        except ValueError:
40
 
            pass
41
 
    return cc
42
 
 
43
 
def to_ascii(text):
44
 
    """Converts special characters such as those with accents to their ASCII equivalents"""
45
 
    old_chars = ['á','é','í','ó','ú','à','è','ì','ò','ù','ä','ë','ï','ö','ü','â','ê','î','ô','û','Á','É','Í','Ú','Ó','À','È','Ì','Ò','Ù','Ä','Ë','Ï','Ö','Ü','Â','Ê','Î','Ô','Û','ñ','Ñ','ç','Ç','ª','º']
46
 
    new_chars = ['a','e','i','o','u','a','e','i','o','u','a','e','i','o','u','a','e','i','o','u','A','E','I','O','U','A','E','I','O','U','A','E','I','O','U','A','E','I','O','U','n','N','c','C','a','o']
47
 
    for old, new in zip(old_chars, new_chars):
48
 
        text = text.replace(unicode(old,'UTF-8'), new)
49
 
    return text
50
 
 
51
 
 
52
 
class Log(Exception):
53
 
    def __init__(self, content = '', error = False):
54
 
        self.content = content
55
 
        self.error = error
56
 
    def add(self, s, error=True):
57
 
        self.content = self.content + s
58
 
        if error:
59
 
            self.error = error
60
 
    def __call__(self):
61
 
        return self.content
62
 
    def __str__(self):
63
 
        return self.content
64
 
 
65
 
def convert_text(text, size):
66
 
    return to_ascii(text)[:size].ljust(size)
67
 
 
68
 
def convert_float(cr, number, size, context):
69
 
    text = str( int( round( number * 100, 0 ) ) )
70
 
    if len(text) > size:
71
 
        raise Log(_('Error:\n\nCan not convert float number %(number).2f to fit in %(size)d characters.') % {
72
 
            'number': number, 
73
 
            'size': size
74
 
        })
75
 
    return text.zfill(size)
76
 
 
77
 
def convert_int(cr, number, size, context):
78
 
    text = str( number )
79
 
    if len(text) > size:
80
 
        raise Log( _('Error:\n\nCan not convert integer number %(number)d to fit in %(size)d characters.') % {
81
 
            'number': number, 
82
 
            'size': size
83
 
        })
84
 
    return text.zfill(size)
85
 
 
86
 
def convert(cr, value, size, context):
87
 
    if value == False:
88
 
        return convert_text('', size)
89
 
    elif isinstance(value, float):
90
 
        return convert_float(cr, value, size, context)
91
 
    elif isinstance(value, int):
92
 
        return convert_int(cr, value, size, context)
93
 
    else:
94
 
        return convert_text(value, size)
95
 
 
96
 
def convert_bank_account(cr, value, partner_name, context):
97
 
    if not isinstance(value, basestring):
98
 
        raise Log( _('User error:\n\nThe bank account number of %s is not defined.') % partner_name )
99
 
    ccc = digits_only(value)
100
 
    if len(ccc) != 20:
101
 
        raise Log( _('User error:\n\nThe bank account number of %s does not have 20 digits.') % partner_name )
102
 
    return ccc
103
 
 
104
 
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
105