Merge lp:~juan-pimentel/addons-vauxoo/juan-pimentel_addons-vauxoo_amount_to_text_es into lp:addons-vauxoo

Proposed by Juan José Pimentel (Digisolution)
Status: Needs review
Proposed branch: lp:~juan-pimentel/addons-vauxoo/juan-pimentel_addons-vauxoo_amount_to_text_es
Merge into: lp:addons-vauxoo
Diff against target: 203 lines (+198/-0)
1 file modified
amount_to_text_es/amount_to_text_es.py (+198/-0)
To merge this branch: bzr merge lp:~juan-pimentel/addons-vauxoo/juan-pimentel_addons-vauxoo_amount_to_text_es
Reviewer Review Type Date Requested Status
Nhomar - Vauxoo Pending
Review via email: mp+91576@code.launchpad.net

Description of the change

These functions can be used to change amount to text for spanish language. The original functions can be found in server/bin/tools under the name amount_to_text_en.py. Will be a good idea propose this merging in the server branch, so it can be used in: vouchers, checks, invoices, etc.

To post a comment you must log in.

Unmerged revisions

120. By Juan José Pimentel (Digisolution)

[FIX] One thousand amount was written wrong

119. By Juan José Pimentel (Digisolution)

[ADD] amount to text functions for spanish language

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== added directory 'amount_to_text_es'
2=== added file 'amount_to_text_es/amount_to_text_es.py'
3--- amount_to_text_es/amount_to_text_es.py 1970-01-01 00:00:00 +0000
4+++ amount_to_text_es/amount_to_text_es.py 2012-02-05 13:05:22 +0000
5@@ -0,0 +1,198 @@
6+# -*- coding: utf-8 -*-
7+##############################################################################
8+#
9+# OpenERP, Open Source Management Solution
10+# Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
11+#
12+# This program is free software: you can redistribute it and/or modify
13+# it under the terms of the GNU Affero General Public License as
14+# published by the Free Software Foundation, either version 3 of the
15+# License, or (at your option) any later version.
16+#
17+# This program is distributed in the hope that it will be useful,
18+# but WITHOUT ANY WARRANTY; without even the implied warranty of
19+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20+# GNU Affero General Public License for more details.
21+#
22+# You should have received a copy of the GNU Affero General Public License
23+# along with this program. If not, see <http://www.gnu.org/licenses/>.
24+#
25+##############################################################################
26+
27+from tools.translate import _
28+
29+#-------------------------------------------------------------
30+#ENGLISH
31+#-------------------------------------------------------------
32+
33+to_19 = ( 'Zero', 'One', 'Two', 'Three', 'Four', 'Five', 'Six',
34+ 'Seven', 'Eight', 'Nine', 'Ten', 'Eleven', 'Twelve', 'Thirteen',
35+ 'Fourteen', 'Fifteen', 'Sixteen', 'Seventeen', 'Eighteen', 'Nineteen' )
36+tens = ( 'Twenty', 'Thirty', 'Forty', 'Fifty', 'Sixty', 'Seventy', 'Eighty', 'Ninety')
37+denom = ( '',
38+ 'Thousand', 'Million', 'Billion', 'Trillion', 'Quadrillion',
39+ 'Quintillion', 'Sextillion', 'Septillion', 'Octillion', 'Nonillion',
40+ 'Decillion', 'Undecillion', 'Duodecillion', 'Tredecillion', 'Quattuordecillion',
41+ 'Sexdecillion', 'Septendecillion', 'Octodecillion', 'Novemdecillion', 'Vigintillion' )
42+
43+# convert a value < 100 to English.
44+def _convert_nn(val):
45+ if val < 20:
46+ return to_19[val]
47+ for (dcap, dval) in ((k, 20 + (10 * v)) for (v, k) in enumerate(tens)):
48+ if dval + 10 > val:
49+ if val % 10:
50+ return dcap + '-' + to_19[val % 10]
51+ return dcap
52+
53+# convert a value < 1000 to english, special cased because it is the level that kicks
54+# off the < 100 special case. The rest are more general. This also allows you to
55+# get strings in the form of 'forty-five hundred' if called directly.
56+def _convert_nnn(val):
57+ word = ''
58+ (mod, rem) = (val % 100, val // 100)
59+ if rem > 0:
60+ word = to_19[rem] + ' Hundred'
61+ if mod > 0:
62+ word = word + ' '
63+ if mod > 0:
64+ word = word + _convert_nn(mod)
65+ return word
66+
67+def english_number(val):
68+ if val < 100:
69+ return _convert_nn(val)
70+ if val < 1000:
71+ return _convert_nnn(val)
72+ for (didx, dval) in ((v - 1, 1000 ** v) for v in range(len(denom))):
73+ if dval > val:
74+ mod = 1000 ** didx
75+ l = val // mod
76+ r = val - (l * mod)
77+ ret = _convert_nnn(l) + ' ' + denom[didx]
78+ if r > 0:
79+ ret = ret + ', ' + english_number(r)
80+ return ret
81+
82+def amount_to_text(number, currency):
83+ number = '%.2f' % number
84+ units_name = currency
85+ list = str(number).split('.')
86+ start_word = english_number(int(list[0]))
87+ end_word = english_number(int(list[1]))
88+ cents_number = int(list[1])
89+ cents_name = (cents_number > 1) and 'Cents' or 'Cent'
90+ final_result = start_word +' '+units_name+' and ' + end_word +' '+cents_name
91+ return final_result
92+
93+
94+#-------------------------------------------------------------
95+#SPANISH
96+#-------------------------------------------------------------
97+
98+to_19_es = ( 'Cero', 'Uno', 'Dos', 'Tres', 'Cuatro', 'Cinco', 'Seis',
99+ 'Siete', 'Ocho', 'Nueve', 'Diez', 'Once', 'Doce', 'Trece',
100+ 'Catorce', 'Quince', 'Dieciseis', 'Diecisiete', 'Dieciocho', 'Diecinueve' )
101+tens_es = ( 'Veinte', 'Treinta', 'Cuarenta', 'Cincuenta', 'Sesenta', 'Setenta', 'Ochenta', 'Noventa')
102+denom_es = ( '',
103+ 'Mil', 'Millones', 'Billon', 'Trillon', 'Cuatrillon',
104+ 'Quintillon' )
105+
106+# convert a value < 100 to Spanish
107+def _convert_nn_es(val):
108+ if val < 20:
109+ return to_19_es[val]
110+ for (dcap, dval) in ((k, 20 + (10 * v)) for (v, k) in enumerate(tens_es)):
111+ if dval + 10 > val:
112+ if val % 10:
113+ return dcap + ' y ' + to_19_es[val % 10]
114+ return dcap
115+
116+# convert a value < 1000 to Spanish, special cased because it is the level that kicks
117+# off the < 100 special case. The rest are more general. This also allows you to
118+
119+def _convert_nnn_es(val):
120+ word = ''
121+ (mod, rem) = (val % 100, val // 100)
122+ if rem ==1 :
123+ word='Cien'
124+ if mod > 0:
125+ word = word + 'to '
126+ if rem > 1:
127+ if rem>1 and rem < 5 or rem ==6 or rem >7 and rem < 9:
128+ word = to_19_es[rem] + 'cientos'
129+ elif rem==5:
130+ word = 'Quinientos'
131+ elif rem==7:
132+ word = 'Setecientos'
133+ else:
134+ word = 'Novecientos'
135+ if mod > 0:
136+ word = word + ' '
137+ if mod > 0:
138+ word = word + _convert_nn_es(mod)
139+ return word
140+
141+def spanish_number(val):
142+ if val < 100:
143+ return _convert_nn_es(val)
144+ if val < 1000:
145+ return _convert_nnn_es(val)
146+ for (didx, dval) in ((v - 1, 1000 ** v) for v in range(len(denom_es))):
147+ if dval > val:
148+ mod = 1000 ** didx
149+ l = val // mod
150+ r = val - (l * mod)
151+ if l==1:
152+ if didx>1:
153+ ret = 'Un ' + denom_es[didx][:-2]
154+ else:
155+ ret = 'Un ' + denom_es[didx]
156+ else:
157+ ret = _convert_nnn_es(l) + ' ' + denom_es[didx]
158+ if r==0 and didx>1:
159+ ret = ret + ' de '
160+ if r > 0:
161+ ret = ret + ' ' + spanish_number(r)
162+ return ret
163+
164+def amount_to_text_es(number, currency):
165+ number = '%.2f' % number
166+ units_name = currency
167+ list = str(number).split('.')
168+ start_word = spanish_number(int(list[0]))
169+ end_word = "%s/100"%(list[1])
170+ cents_number = int(list[1])
171+ cents_name = 'Centimos'
172+ final_result = start_word +' '+units_name+' con ' + end_word +' '+cents_name
173+ return final_result
174+
175+
176+#-------------------------------------------------------------
177+# Generic functions
178+#-------------------------------------------------------------
179+
180+_translate_funcs = {'en' : amount_to_text, 'es' : amount_to_text_es }
181+
182+#TODO: we should use the country AND language (ex: septante VS soixante dix)
183+#TODO: we should use en by default, but the translation func is yet to be implemented
184+def amount_to_text(nbr, lang='en', currency='euro'):
185+ """
186+ Converts an integer to its textual representation, using the language set in the context if any.
187+ Example:
188+ 1654: thousands six cent cinquante-quatre.
189+ """
190+ import netsvc
191+# if nbr > 10000000:
192+# netsvc.Logger().notifyChannel('translate', netsvc.LOG_WARNING, _("Number too large '%d', can not translate it"))
193+# return str(nbr)
194+
195+ if not _translate_funcs.has_key(lang):
196+ netsvc.Logger().notifyChannel('translate', netsvc.LOG_WARNING, _("no translation function found for lang: '%s'" % (lang,)))
197+ #TODO: (default should be en) same as above
198+ lang = 'en'
199+ return _translate_funcs[lang](abs(nbr), currency)
200+
201+
202+
203+