7f864ba7720b7fcfbba72cb7d97e2dd70f858759
[odoo/odoo.git] / bin / tools / amount_to_text_en.py
1 # -*- encoding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution   
5 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
6 #    $Id$
7 #
8 #    This program is free software: you can redistribute it and/or modify
9 #    it under the terms of the GNU General Public License as published by
10 #    the Free Software Foundation, either version 3 of the License, or
11 #    (at your option) any later version.
12 #
13 #    This program is distributed in the hope that it will be useful,
14 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
15 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 #    GNU General Public License for more details.
17 #
18 #    You should have received a copy of the GNU General Public License
19 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 #
21 ##############################################################################
22
23 #-------------------------------------------------------------
24 #ENGLISH
25 #-------------------------------------------------------------
26 from tools.translate import _
27
28 to_19 = ( 'Zero',  'One',   'Two',  'Three', 'Four',   'Five',   'Six',
29           'Seven', 'Eight', 'Nine', 'Ten',   'Eleven', 'Twelve', 'Thirteen',
30           'Fourteen', 'Fifteen', 'Sixteen', 'Seventeen', 'Eighteen', 'Nineteen' )
31 tens  = ( 'Twenty', 'Thirty', 'Forty', 'Fifty', 'Sixty', 'Seventy', 'Eighty', 'Ninety')
32 denom = ( '',
33           'Thousand',     'Million',         'Billion',       'Trillion',       'Quadrillion',
34           'Quintillion',  'Sextillion',      'Septillion',    'Octillion',      'Nonillion',
35           'Decillion',    'Undecillion',     'Duodecillion',  'Tredecillion',   'Quattuordecillion',
36           'Sexdecillion', 'Septendecillion', 'Octodecillion', 'Novemdecillion', 'Vigintillion' )
37
38 # convert a value < 100 to English.
39 def _convert_nn(val):
40     if val < 20:
41         return to_19[val]
42     for (dcap, dval) in ((k, 20 + (10 * v)) for (v, k) in enumerate(tens)):
43         if dval + 10 > val:
44             if val % 10:
45                 return dcap + '-' + to_19[val % 10]
46             return dcap
47
48 # convert a value < 1000 to english, special cased because it is the level that kicks 
49 # off the < 100 special case.  The rest are more general.  This also allows you to
50 # get strings in the form of 'forty-five hundred' if called directly.
51 def _convert_nnn(val):
52     word = ''
53     (mod, rem) = (val % 100, val // 100)
54     if rem > 0:
55         word = to_19[rem] + ' Hundred'
56         if mod > 0:
57             word = word + ' '
58     if mod > 0:
59         word = word + _convert_nn(mod)
60     return word
61
62 def english_number(val):
63     if val < 100:
64         return _convert_nn(val)
65     if val < 1000:
66          return _convert_nnn(val)
67     for (didx, dval) in ((v - 1, 1000 ** v) for v in range(len(denom))):
68         if dval > val:
69             mod = 1000 ** didx
70             l = val // mod
71             r = val - (l * mod)
72             ret = _convert_nnn(l) + ' ' + denom[didx]
73             if r > 0:
74                 ret = ret + ', ' + english_number(r)
75             return ret
76
77 def amount_to_text(number, currency):
78     number = '%.2f' % number
79     units_name = currency
80     list = str(number).split('.')
81     start_word = english_number(int(list[0]))
82     end_word = english_number(int(list[1]))
83     cents_number = int(list[1])
84     cents_name = (cents_number > 1) and 'Cents' or 'Cent'
85     final_result = start_word +' '+units_name+' and ' + end_word +' '+cents_name
86     return final_result
87
88
89 #-------------------------------------------------------------
90 # Generic functions
91 #-------------------------------------------------------------
92
93 _translate_funcs = {'en' : amount_to_text}
94     
95 #TODO: we should use the country AND language (ex: septante VS soixante dix)
96 #TODO: we should use en by default, but the translation func is yet to be implemented
97 def amount_to_text(nbr, lang='en', currency='euro'):
98     """
99     Converts an integer to its textual representation, using the language set in the context if any.
100     Example:
101         1654: thousands six cent cinquante-quatre.
102     """
103     import netsvc
104 #    if nbr > 10000000:
105 #        netsvc.Logger().notifyChannel('translate', netsvc.LOG_WARNING, _("Number too large '%d', can not translate it"))
106 #        return str(nbr)
107     
108     if not _translate_funcs.has_key(lang):
109         netsvc.Logger().notifyChannel('translate', netsvc.LOG_WARNING, _("no translation function found for lang: '%s'" % (lang,)))
110         #TODO: (default should be en) same as above
111         lang = 'en'
112     return _translate_funcs[lang](abs(nbr), currency)
113
114 if __name__=='__main__':
115     from sys import argv
116     
117     lang = 'nl'
118     if len(argv) < 2:
119         for i in range(1,200):
120             print i, ">>", int_to_text(i, lang)
121         for i in range(200,999999,139):
122             print i, ">>", int_to_text(i, lang)
123     else:
124         print int_to_text(int(argv[1]), lang)
125