[REVERT] r3591: causing problem to install some modules
[odoo/odoo.git] / bin / tools / amount_to_text_en.py
index c8cf4cb..66d749e 100644 (file)
-# -*- encoding: utf-8 -*-
+# -*- coding: utf-8 -*-
 ##############################################################################
-#
-#    OpenERP, Open Source Management Solution  
-#    Copyright (C) 2004-2008 Tiny SPRL (<http://tiny.be>). All Rights Reserved
-#    $Id$
+#    
+#    OpenERP, Open Source Management Solution
+#    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
 #
 #    This program is free software: you can redistribute it and/or modify
-#    it under the terms of the GNU General Public License as published by
-#    the Free Software Foundation, either version 3 of the License, or
-#    (at your option) any later version.
+#    it under the terms of the GNU Affero General Public License as
+#    published by the Free Software Foundation, either version 3 of the
+#    License, or (at your option) any later version.
 #
 #    This program is distributed in the hope that it will be useful,
 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-#    GNU General Public License for more details.
+#    GNU Affero General Public License for more details.
 #
-#    You should have received a copy of the GNU General Public License
-#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+#    You should have received a copy of the GNU Affero General Public License
+#    along with this program.  If not, see <http://www.gnu.org/licenses/>.     
 #
 ##############################################################################
 
 #-------------------------------------------------------------
 #ENGLISH
 #-------------------------------------------------------------
-
-ones = {
-    0: '', 1:'One', 2:'Two', 3:'Three', 4:'Four', 5:'Five', 6:'Six', 7:'Seven', 8:'Eight', 9:'Nine',
-    10:'Ten', 11:'Eleven', 12:'Twelve', 13:'Thirteen', 14:'Forteen', 15:'Fifteen', 16:'Sixteen', 17:"Seventeen",18:"Eighteen",19:"Nineteen",
-}
-
-tens = {
-    1: 'Ten', 2: 'Twenty ', 3:'Thirty', 4:'Forty', 5:'Fifty', 6: 'Sixty', 7 : 'Seventy', 8:'Eighty' ,9: 'Ninety'}
-
-hundred = {
-     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 '
-}
-
-thousands ={
-     0:'',1: 'One Thousand'
-}
-
-lacs = {
-     0:'',1: 'Lac'
-}
-
-def _100_to_text(number):
-    if number in ones:
-        return ones[number]
-    else:
-        if number%10>0:
-            return tens[number / 10]+'-'+ones[number % 10]
-        else:
-            return tens[number / 10]
-
-def _1000_to_text(number):
-    d = _100_to_text(number % 100)
-    d2 = number/100
-    if d2>0 and d:
-        return hundred[d2]+' '+d
-    elif d2>1 and not(d):
-        return hundred[d2]+'s'
-    else:
-        return hundred[d2] or d
-
-def _10000_to_text(number):
-    if number==0:
-        return 'zero'
-    part1 = _1000_to_text(number % 1000)
-    part2 = thousands.get(number / 1000,  _1000_to_text(number / 1000)+' Thousands')
-    if part2 and part1:
-        part1 = ' '+part1
-    return part2+part1
-
-def _1000000_to_text(number):
-    if number==0:
-        return 'zero'
-    part1 = _10000_to_text(number % 100000)
-    part2 = lacs.get(number / 100000,  _10000_to_text(number / 100000)+' Lacs')
-    if part2 and part1:
-        part1 = ' '+part1
-    return part2+part1
-
+from tools.translate import _
+
+to_19 = ( 'Zero',  'One',   'Two',  'Three', 'Four',   'Five',   'Six',
+          'Seven', 'Eight', 'Nine', 'Ten',   'Eleven', 'Twelve', 'Thirteen',
+          'Fourteen', 'Fifteen', 'Sixteen', 'Seventeen', 'Eighteen', 'Nineteen' )
+tens  = ( 'Twenty', 'Thirty', 'Forty', 'Fifty', 'Sixty', 'Seventy', 'Eighty', 'Ninety')
+denom = ( '',
+          'Thousand',     'Million',         'Billion',       'Trillion',       'Quadrillion',
+          'Quintillion',  'Sextillion',      'Septillion',    'Octillion',      'Nonillion',
+          'Decillion',    'Undecillion',     'Duodecillion',  'Tredecillion',   'Quattuordecillion',
+          'Sexdecillion', 'Septendecillion', 'Octodecillion', 'Novemdecillion', 'Vigintillion' )
+
+# convert a value < 100 to English.
+def _convert_nn(val):
+    if val < 20:
+        return to_19[val]
+    for (dcap, dval) in ((k, 20 + (10 * v)) for (v, k) in enumerate(tens)):
+        if dval + 10 > val:
+            if val % 10:
+                return dcap + '-' + to_19[val % 10]
+            return dcap
+
+# convert a value < 1000 to english, special cased because it is the level that kicks 
+# off the < 100 special case.  The rest are more general.  This also allows you to
+# get strings in the form of 'forty-five hundred' if called directly.
+def _convert_nnn(val):
+    word = ''
+    (mod, rem) = (val % 100, val // 100)
+    if rem > 0:
+        word = to_19[rem] + ' Hundred'
+        if mod > 0:
+            word = word + ' '
+    if mod > 0:
+        word = word + _convert_nn(mod)
+    return word
+
+def english_number(val):
+    if val < 100:
+        return _convert_nn(val)
+    if val < 1000:
+         return _convert_nnn(val)
+    for (didx, dval) in ((v - 1, 1000 ** v) for v in range(len(denom))):
+        if dval > val:
+            mod = 1000 ** didx
+            l = val // mod
+            r = val - (l * mod)
+            ret = _convert_nnn(l) + ' ' + denom[didx]
+            if r > 0:
+                ret = ret + ', ' + english_number(r)
+            return ret
 
 def amount_to_text(number, currency):
-    lacs_number = int(number)
+    number = '%.2f' % number
     units_name = currency
-    if lacs_number > 1:
-        units_name += 's'
-    
-    lacs = _1000000_to_text(lacs_number)
-    lacs = lacs_number and '%s %s' % (lacs, units_name) or ''
-    
-    units_number = int(number * 10000) % 10000
-    units = _10000_to_text(units_number)
-    units = units_number and '%s %s' % (units, units_name) or ''
-    
-    cents_number = int(number * 100) % 100
-    cents_name = (cents_number > 1) and 'cents' or 'cent'
-    cents = _100_to_text(cents_number)
-    cents = cents_number and '%s %s' % (cents, cents_name) or ''
-    return lacs
+    list = str(number).split('.')
+    start_word = english_number(int(list[0]))
+    end_word = english_number(int(list[1]))
+    cents_number = int(list[1])
+    cents_name = (cents_number > 1) and 'Cents' or 'Cent'
+    final_result = start_word +' '+units_name+' and ' + end_word +' '+cents_name
+    return final_result
 
 
 #-------------------------------------------------------------
@@ -117,15 +100,15 @@ def amount_to_text(nbr, lang='en', currency='euro'):
         1654: thousands six cent cinquante-quatre.
     """
     import netsvc
-    if nbr > 10000000:
-        netsvc.Logger().notifyChannel('translate', netsvc.LOG_WARNING, _("Number too large '%d', can not translate it"))
-        return str(nbr)
+#    if nbr > 10000000:
+#        netsvc.Logger().notifyChannel('translate', netsvc.LOG_WARNING, _("Number too large '%d', can not translate it"))
+#        return str(nbr)
     
     if not _translate_funcs.has_key(lang):
         netsvc.Logger().notifyChannel('translate', netsvc.LOG_WARNING, _("no translation function found for lang: '%s'" % (lang,)))
         #TODO: (default should be en) same as above
         lang = 'en'
-    return _translate_funcs[lang](nbr, currency)
+    return _translate_funcs[lang](abs(nbr), currency)
 
 if __name__=='__main__':
     from sys import argv