fix: money in words (#27190)
This commit is contained in:
parent
b91cacdd18
commit
d3248612cb
2 changed files with 42 additions and 40 deletions
|
|
@ -221,35 +221,39 @@ class TestFilters(FrappeTestCase):
|
|||
|
||||
class TestMoney(FrappeTestCase):
|
||||
def test_money_in_words(self):
|
||||
nums_bhd = [
|
||||
(5000, "BHD Five Thousand only."),
|
||||
(5000.0, "BHD Five Thousand only."),
|
||||
(0.1, "One Hundred Fils only."),
|
||||
(0, "BHD Zero only."),
|
||||
("Fail", ""),
|
||||
]
|
||||
test_cases = {
|
||||
"BHD": [
|
||||
(5000, "BHD Five Thousand only."),
|
||||
(5000.0, "BHD Five Thousand only."),
|
||||
(0.1, "One Hundred Fils only."),
|
||||
(0, "BHD Zero only."),
|
||||
("Fail", ""),
|
||||
],
|
||||
"NGN": [
|
||||
(5000, "NGN Five Thousand only."),
|
||||
(5000.0, "NGN Five Thousand only."),
|
||||
(0.1, "Ten Kobo only."),
|
||||
(0, "NGN Zero only."),
|
||||
("Fail", ""),
|
||||
],
|
||||
"MRO": [
|
||||
(5000, "MRO Five Thousand only."),
|
||||
(5000.0, "MRO Five Thousand only."),
|
||||
(1.4, "MRO One and Two Khoums only."),
|
||||
(0.2, "One Khoums only."),
|
||||
(0, "MRO Zero only."),
|
||||
("Fail", ""),
|
||||
],
|
||||
}
|
||||
|
||||
nums_ngn = [
|
||||
(5000, "NGN Five Thousand only."),
|
||||
(5000.0, "NGN Five Thousand only."),
|
||||
(0.1, "Ten Kobo only."),
|
||||
(0, "NGN Zero only."),
|
||||
("Fail", ""),
|
||||
]
|
||||
|
||||
for num in nums_bhd:
|
||||
self.assertEqual(
|
||||
money_in_words(num[0], "BHD"),
|
||||
num[1],
|
||||
"{} is not the same as {}".format(money_in_words(num[0], "BHD"), num[1]),
|
||||
)
|
||||
|
||||
for num in nums_ngn:
|
||||
self.assertEqual(
|
||||
money_in_words(num[0], "NGN"),
|
||||
num[1],
|
||||
"{} is not the same as {}".format(money_in_words(num[0], "NGN"), num[1]),
|
||||
)
|
||||
for currency, cases in test_cases.items():
|
||||
for money, expected_words in cases:
|
||||
words = money_in_words(money, currency)
|
||||
self.assertEqual(
|
||||
words,
|
||||
expected_words,
|
||||
f"{words} is not the same as {expected_words}",
|
||||
)
|
||||
|
||||
|
||||
class TestDataManipulation(FrappeTestCase):
|
||||
|
|
|
|||
|
|
@ -1468,18 +1468,16 @@ def money_in_words(
|
|||
d = get_defaults()
|
||||
if not main_currency:
|
||||
main_currency = d.get("currency", "INR")
|
||||
|
||||
if not fraction_currency:
|
||||
fraction_currency = frappe.db.get_value("Currency", main_currency, "fraction", cache=True) or _(
|
||||
"Cent"
|
||||
)
|
||||
|
||||
currency_format_str = frappe.db.get_value("Currency", main_currency, "number_format", cache=True)
|
||||
if currency_format_str:
|
||||
number_format = NumberFormat.from_string(currency_format_str)
|
||||
else:
|
||||
number_format = get_number_format()
|
||||
number_format = get_number_format()
|
||||
|
||||
fraction_length = number_format.precision
|
||||
fraction_units = frappe.db.get_value("Currency", main_currency, "fraction_units", cache=True)
|
||||
fraction_length = math.ceil(math.log10(fraction_units)) or number_format.precision
|
||||
|
||||
n = f"%.{fraction_length}f" % number
|
||||
|
||||
|
|
@ -1494,18 +1492,18 @@ def money_in_words(
|
|||
if number_format.string == "#,##,###.##":
|
||||
in_million = False
|
||||
|
||||
def fraction_in_words() -> str:
|
||||
return in_words(float(f"0.{fraction}") * fraction_units, in_million).title()
|
||||
|
||||
# 0.00
|
||||
if main == "0" and fraction in ["00", "000"]:
|
||||
if main == "0" and fraction in ["0", "00", "000"]:
|
||||
out = _(main_currency, context="Currency") + " " + _("Zero")
|
||||
# 0.XX
|
||||
elif main == "0":
|
||||
out = in_words(fraction, in_million).title() + " " + fraction_currency
|
||||
out = f"{fraction_in_words()} {fraction_currency}"
|
||||
else:
|
||||
out = _(main_currency, context="Currency") + " " + in_words(main, in_million).title()
|
||||
if cint(fraction):
|
||||
out = (
|
||||
out + " " + _("and") + " " + in_words(fraction, in_million).title() + " " + fraction_currency
|
||||
)
|
||||
out = out + " " + _("and") + " " + fraction_in_words() + " " + fraction_currency
|
||||
|
||||
return _("{0} only.", context="Money in words").format(out)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue