passing in GPL-3
[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-2008 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
27 ones = {
28     0: '', 1:'One', 2:'Two', 3:'Three', 4:'Four', 5:'Five', 6:'Six', 7:'Seven', 8:'Eight', 9:'Nine',
29     10:'Ten', 11:'Eleven', 12:'Twelve', 13:'Thirteen', 14:'Forteen', 15:'Fifteen', 16:'Sixteen', 17:"Seventeen",18:"Eighteen",19:"Nineteen",
30 }
31
32 tens = {
33     1: 'Ten', 2: 'Twenty ', 3:'Thirty', 4:'Forty', 5:'Fifty', 6: 'Sixty', 7 : 'Seventy', 8:'Eighty' ,9: 'Ninety'}
34
35 hundred = {
36      0:'',1: 'One Hundred', 2: 'Two Hundred', 3: 'Three Hundred', 4 :'Four Hundred', 5: 'Five Hundred', 6: 'Six Hundred', 7 :'Seven Hundred', 8:' Eight Hundred ', 9:'Nine Hundred '
37 }
38
39 thousands ={
40      0:'',1: 'One Thousand'
41 }
42
43 lacs = {
44      0:'',1: 'Lac'
45 }
46
47 def _100_to_text(number):
48     if number in ones:
49         return ones[number]
50     else:
51         if number%10>0:
52             return tens[number / 10]+'-'+ones[number % 10]
53         else:
54             return tens[number / 10]
55
56 def _1000_to_text(number):
57     d = _100_to_text(number % 100)
58     d2 = number/100
59     if d2>0 and d:
60         return hundred[d2]+' '+d
61     elif d2>1 and not(d):
62         return hundred[d2]+'s'
63     else:
64         return hundred[d2] or d
65
66 def _10000_to_text(number):
67     if number==0:
68         return 'zero'
69     part1 = _1000_to_text(number % 1000)
70     part2 = thousands.get(number / 1000,  _1000_to_text(number / 1000)+' Thousands')
71     if part2 and part1:
72         part1 = ' '+part1
73     return part2+part1
74
75 def _1000000_to_text(number):
76     if number==0:
77         return 'zero'
78     part1 = _10000_to_text(number % 100000)
79     part2 = lacs.get(number / 100000,  _10000_to_text(number / 100000)+' Lacs')
80     if part2 and part1:
81         part1 = ' '+part1
82     return part2+part1
83
84
85 def amount_to_text(number, currency):
86     lacs_number = int(number)
87     units_name = currency
88     if lacs_number > 1:
89         units_name += 's'
90     
91     lacs = _1000000_to_text(lacs_number)
92     lacs = lacs_number and '%s %s' % (lacs, units_name) or ''
93     
94     units_number = int(number * 10000) % 10000
95     units = _10000_to_text(units_number)
96     units = units_number and '%s %s' % (units, units_name) or ''
97     
98     cents_number = int(number * 100) % 100
99     cents_name = (cents_number > 1) and 'cents' or 'cent'
100     cents = _100_to_text(cents_number)
101     cents = cents_number and '%s %s' % (cents, cents_name) or ''
102     return lacs
103
104
105 #-------------------------------------------------------------
106 # Generic functions
107 #-------------------------------------------------------------
108
109 _translate_funcs = {'en' : amount_to_text}
110     
111 #TODO: we should use the country AND language (ex: septante VS soixante dix)
112 #TODO: we should use en by default, but the translation func is yet to be implemented
113 def amount_to_text(nbr, lang='en', currency='euro'):
114     """
115     Converts an integer to its textual representation, using the language set in the context if any.
116     Example:
117         1654: thousands six cent cinquante-quatre.
118     """
119     if nbr > 10000000:
120 #TODO: use logger    
121         print "WARNING: number too large '%d', can't translate it!" % (nbr,)
122         return str(nbr)
123     
124     if not _translate_funcs.has_key(lang):
125 #TODO: use logger    
126         print "WARNING: no translation function found for lang: '%s'" % (lang,)
127 #TODO: (default should be en) same as above
128         lang = 'en'
129     return _translate_funcs[lang](nbr, currency)
130
131 if __name__=='__main__':
132     from sys import argv
133     
134     lang = 'nl'
135     if len(argv) < 2:
136         for i in range(1,200):
137             print i, ">>", int_to_text(i, lang)
138         for i in range(200,999999,139):
139             print i, ">>", int_to_text(i, lang)
140     else:
141         print int_to_text(int(argv[1]), lang)
142