[MERGE] demo data: remove salesman on customers
[odoo/odoo.git] / openerp / tools / amount_to_text_en.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 #ENGLISH
24 #-------------------------------------------------------------
25 from translate import _
26
27 to_19 = ( 'Zero',  'One',   'Two',  'Three', 'Four',   'Five',   'Six',
28           'Seven', 'Eight', 'Nine', 'Ten',   'Eleven', 'Twelve', 'Thirteen',
29           'Fourteen', 'Fifteen', 'Sixteen', 'Seventeen', 'Eighteen', 'Nineteen' )
30 tens  = ( 'Twenty', 'Thirty', 'Forty', 'Fifty', 'Sixty', 'Seventy', 'Eighty', 'Ninety')
31 denom = ( '',
32           'Thousand',     'Million',         'Billion',       'Trillion',       'Quadrillion',
33           'Quintillion',  'Sextillion',      'Septillion',    'Octillion',      'Nonillion',
34           'Decillion',    'Undecillion',     'Duodecillion',  'Tredecillion',   'Quattuordecillion',
35           'Sexdecillion', 'Septendecillion', 'Octodecillion', 'Novemdecillion', 'Vigintillion' )
36
37 def _convert_nn(val):
38     """convert a value < 100 to English.
39     """
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 def _convert_nnn(val):
49     """
50         convert a value < 1000 to english, special cased because it is the level that kicks 
51         off the < 100 special case.  The rest are more general.  This also allows you to
52         get strings in the form of 'forty-five hundred' if called directly.
53     """
54     word = ''
55     (mod, rem) = (val % 100, val // 100)
56     if rem > 0:
57         word = to_19[rem] + ' Hundred'
58         if mod > 0:
59             word = word + ' '
60     if mod > 0:
61         word = word + _convert_nn(mod)
62     return word
63
64 def english_number(val):
65     if val < 100:
66         return _convert_nn(val)
67     if val < 1000:
68          return _convert_nnn(val)
69     for (didx, dval) in ((v - 1, 1000 ** v) for v in range(len(denom))):
70         if dval > val:
71             mod = 1000 ** didx
72             l = val // mod
73             r = val - (l * mod)
74             ret = _convert_nnn(l) + ' ' + denom[didx]
75             if r > 0:
76                 ret = ret + ', ' + english_number(r)
77             return ret
78
79 def amount_to_text(number, currency):
80     number = '%.2f' % number
81     units_name = currency
82     list = str(number).split('.')
83     start_word = english_number(int(list[0]))
84     end_word = english_number(int(list[1]))
85     cents_number = int(list[1])
86     cents_name = (cents_number > 1) and 'Cents' or 'Cent'
87     final_result = start_word +' '+units_name+' and ' + end_word +' '+cents_name
88     return final_result
89
90
91 #-------------------------------------------------------------
92 # Generic functions
93 #-------------------------------------------------------------
94
95 _translate_funcs = {'en' : amount_to_text}
96     
97 #TODO: we should use the country AND language (ex: septante VS soixante dix)
98 #TODO: we should use en by default, but the translation func is yet to be implemented
99 def amount_to_text(nbr, lang='en', currency='euro'):
100     """ Converts an integer to its textual representation, using the language set in the context if any.
101     
102         Example::
103         
104             1654: thousands six cent cinquante-quatre.
105     """
106     import openerp.loglevels as loglevels
107 #    if nbr > 10000000:
108 #        netsvc.Logger().notifyChannel('translate', netsvc.LOG_WARNING, _("Number too large '%d', can not translate it"))
109 #        return str(nbr)
110     
111     if not _translate_funcs.has_key(lang):
112         loglevels.Logger().notifyChannel('translate', loglevels.LOG_WARNING, _("no translation function found for lang: '%s'" % (lang,)))
113         #TODO: (default should be en) same as above
114         lang = 'en'
115     return _translate_funcs[lang](abs(nbr), currency)
116
117 if __name__=='__main__':
118     from sys import argv
119     
120     lang = 'nl'
121     if len(argv) < 2:
122         for i in range(1,200):
123             print i, ">>", int_to_text(i, lang)
124         for i in range(200,999999,139):
125             print i, ">>", int_to_text(i, lang)
126     else:
127         print int_to_text(int(argv[1]), lang)
128
129
130 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: