[MERGE] module kaban padding fixes (manual merge)
[odoo/odoo.git] / openerp / tools / amount_to_text.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #    
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU Affero General Public License as
9 #    published by the Free Software Foundation, either version 3 of the
10 #    License, or (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU Affero General Public License for more details.
16 #
17 #    You should have received a copy of the GNU Affero General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.     
19 #
20 ##############################################################################
21
22 #-------------------------------------------------------------
23 # French
24 #-------------------------------------------------------------
25
26
27 to_19_fr = ( 'zéro',  'un',   'deux',  'trois', 'quatre',   'cinq',   'six',
28           'sept', 'huit', 'neuf', 'dix',   'onze', 'douze', 'treize',
29           'quatorze', 'quinze', 'seize', 'dix-sept', 'dix-huit', 'dix-neuf' )
30 tens_fr  = ( 'vingt', 'trente', 'quarante', 'Cinquante', 'Soixante', 'Soixante-dix', 'Quatre-vingts', 'Quatre-vingt Dix')
31 denom_fr = ( '',
32           'Mille',     'Millions',         'Milliards',       'Billions',       'Quadrillions',
33           'Quintillion',  'Sextillion',      'Septillion',    'Octillion',      'Nonillion',
34           'Décillion',    'Undecillion',     'Duodecillion',  'Tredecillion',   'Quattuordecillion',
35           'Sexdecillion', 'Septendecillion', 'Octodecillion', 'Icosillion', 'Vigintillion' )
36
37 def _convert_nn_fr(val):
38     """ convert a value < 100 to French
39     """
40     if val < 20:
41         return to_19_fr[val]
42     for (dcap, dval) in ((k, 20 + (10 * v)) for (v, k) in enumerate(tens_fr)):
43         if dval + 10 > val:
44             if val % 10:
45                 return dcap + '-' + to_19_fr[val % 10]
46             return dcap
47
48 def _convert_nnn_fr(val):
49     """ convert a value < 1000 to french
50     
51         special cased because it is the level that kicks 
52         off the < 100 special case.  The rest are more general.  This also allows you to
53         get strings in the form of 'forty-five hundred' if called directly.
54     """
55     word = ''
56     (mod, rem) = (val % 100, val // 100)
57     if rem > 0:
58         word = to_19_fr[rem] + ' Cent'
59         if mod > 0:
60             word = word + ' '
61     if mod > 0:
62         word = word + _convert_nn_fr(mod)
63     return word
64
65 def french_number(val):
66     if val < 100:
67         return _convert_nn_fr(val)
68     if val < 1000:
69          return _convert_nnn_fr(val)
70     for (didx, dval) in ((v - 1, 1000 ** v) for v in range(len(denom_fr))):
71         if dval > val:
72             mod = 1000 ** didx
73             l = val // mod
74             r = val - (l * mod)
75             ret = _convert_nnn_fr(l) + ' ' + denom_fr[didx]
76             if r > 0:
77                 ret = ret + ', ' + french_number(r)
78             return ret
79
80 def amount_to_text_fr(number, currency):
81     number = '%.2f' % number
82     units_name = currency
83     list = str(number).split('.')
84     start_word = french_number(abs(int(list[0])))
85     end_word = french_number(int(list[1]))
86     cents_number = int(list[1])
87     cents_name = (cents_number > 1) and ' Cents' or ' Cent'
88     final_result = start_word +' '+units_name+' '+ end_word +' '+cents_name
89     return final_result
90
91 #-------------------------------------------------------------
92 # Dutch
93 #-------------------------------------------------------------
94
95 to_19_nl = ( 'Nul',  'Een',   'Twee',  'Drie', 'Vier',   'Vijf',   'Zes',
96           'Zeven', 'Acht', 'Negen', 'Tien',   'Elf', 'Twaalf', 'Dertien',
97           'Veertien', 'Vijftien', 'Zestien', 'Zeventien', 'Achttien', 'Negentien' )
98 tens_nl  = ( 'Twintig', 'Dertig', 'Veertig', 'Vijftig', 'Zestig', 'Zeventig', 'Tachtig', 'Negentig')
99 denom_nl = ( '',
100           'Duizend', 'Miljoen', 'Miljard', 'Triljoen', 'Quadriljoen',
101            'Quintillion', 'Sextiljoen', 'Septillion', 'Octillion', 'Nonillion',
102            'Decillion', 'Undecillion', 'Duodecillion', 'Tredecillion', 'Quattuordecillion',
103            'Sexdecillion', 'Septendecillion', 'Octodecillion', 'Novemdecillion', 'Vigintillion' )
104
105 def _convert_nn_nl(val):
106     """ convert a value < 100 to Dutch
107     """
108     if val < 20:
109         return to_19_nl[val]
110     for (dcap, dval) in ((k, 20 + (10 * v)) for (v, k) in enumerate(tens_nl)):
111         if dval + 10 > val:
112             if val % 10:
113                 return dcap + '-' + to_19_nl[val % 10]
114             return dcap
115
116 def _convert_nnn_nl(val):
117     """ convert a value < 1000 to Dutch
118     
119         special cased because it is the level that kicks 
120         off the < 100 special case.  The rest are more general.  This also allows you to
121         get strings in the form of 'forty-five hundred' if called directly.
122     """
123     word = ''
124     (mod, rem) = (val % 100, val // 100)
125     if rem > 0:
126         word = to_19_nl[rem] + ' Honderd'
127         if mod > 0:
128             word = word + ' '
129     if mod > 0:
130         word = word + _convert_nn_nl(mod)
131     return word
132
133 def dutch_number(val):
134     if val < 100:
135         return _convert_nn_nl(val)
136     if val < 1000:
137          return _convert_nnn_nl(val)
138     for (didx, dval) in ((v - 1, 1000 ** v) for v in range(len(denom_nl))):
139         if dval > val:
140             mod = 1000 ** didx
141             l = val // mod
142             r = val - (l * mod)
143             ret = _convert_nnn_nl(l) + ' ' + denom_nl[didx]
144             if r > 0:
145                 ret = ret + ', ' + dutch_number(r)
146             return ret
147
148 def amount_to_text_nl(number, currency):
149     number = '%.2f' % number
150     units_name = currency
151     list = str(number).split('.')
152     start_word = dutch_number(int(list[0]))
153     end_word = dutch_number(int(list[1]))
154     cents_number = int(list[1])
155     cents_name = (cents_number > 1) and 'cent' or 'cent'
156     final_result = start_word +' '+units_name+' '+ end_word +' '+cents_name
157     return final_result
158
159 #-------------------------------------------------------------
160 # Generic functions
161 #-------------------------------------------------------------
162
163 _translate_funcs = {'fr' : amount_to_text_fr, 'nl' : amount_to_text_nl}
164
165 def add_amount_to_text_function(lang, func):
166     _translate_funcs[lang] = func
167     
168 #TODO: we should use the country AND language (ex: septante VS soixante dix)
169 #TODO: we should use en by default, but the translation func is yet to be implemented
170 def amount_to_text(nbr, lang='fr', currency='euro'):
171     """ Converts an integer to its textual representation, using the language set in the context if any.
172
173         Example::
174         
175             1654: mille six cent cinquante-quatre.
176     """
177 #    if nbr > 1000000:
178 ##TODO: use logger   
179 #        print "WARNING: number too large '%d', can't translate it!" % (nbr,)
180 #        return str(nbr)
181     
182     if not _translate_funcs.has_key(lang):
183 #TODO: use logger   
184         print "WARNING: no translation function found for lang: '%s'" % (lang,)
185 #TODO: (default should be en) same as above
186         lang = 'fr'
187     return _translate_funcs[lang](abs(nbr), currency)
188
189 if __name__=='__main__':
190     from sys import argv
191     
192     lang = 'nl'
193     if len(argv) < 2:
194         for i in range(1,200):
195             print i, ">>", amount_to_text(i, lang)
196         for i in range(200,999999,139):
197             print i, ">>", amount_to_text(i, lang)
198     else:
199         print amount_to_text(int(argv[1]), lang)
200
201
202 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
203