feat: configurable location of currency symbolversion-14
@@ -77,6 +77,8 @@ def get_bootinfo(): | |||||
# add docs | # add docs | ||||
bootinfo.docs = doclist | bootinfo.docs = doclist | ||||
load_country_doc(bootinfo) | |||||
load_currency_docs(bootinfo) | |||||
for method in hooks.boot_session or []: | for method in hooks.boot_session or []: | ||||
frappe.get_attr(method)(bootinfo) | frappe.get_attr(method)(bootinfo) | ||||
@@ -417,3 +419,34 @@ def get_translatable_doctypes(): | |||||
"Property Setter", {"property": "translate_link_fields", "value": "1"}, pluck="doc_type" | "Property Setter", {"property": "translate_link_fields", "value": "1"}, pluck="doc_type" | ||||
) | ) | ||||
return dts + custom_dts | return dts + custom_dts | ||||
def load_country_doc(bootinfo): | |||||
country = frappe.db.get_default("country") | |||||
if not country: | |||||
return | |||||
try: | |||||
bootinfo.docs.append(frappe.get_cached_doc("Country", country)) | |||||
except Exception: | |||||
pass | |||||
def load_currency_docs(bootinfo): | |||||
currency = frappe.qb.DocType("Currency") | |||||
currency_docs = ( | |||||
frappe.qb.from_(currency) | |||||
.select( | |||||
currency.name, | |||||
currency.fraction, | |||||
currency.fraction_units, | |||||
currency.number_format, | |||||
currency.smallest_currency_fraction_value, | |||||
currency.symbol, | |||||
currency.symbol_on_right, | |||||
) | |||||
.where(currency.enabled == 1) | |||||
.run(as_dict=1, update={"doctype": ":Currency"}) | |||||
) | |||||
bootinfo.docs += currency_docs |
@@ -15,6 +15,7 @@ | |||||
"fraction_units", | "fraction_units", | ||||
"smallest_currency_fraction_value", | "smallest_currency_fraction_value", | ||||
"symbol", | "symbol", | ||||
"symbol_on_right", | |||||
"number_format" | "number_format" | ||||
], | ], | ||||
"fields": [ | "fields": [ | ||||
@@ -69,16 +70,23 @@ | |||||
"in_list_view": 1, | "in_list_view": 1, | ||||
"label": "Number Format", | "label": "Number Format", | ||||
"options": "\n#,###.##\n#.###,##\n# ###.##\n# ###,##\n#'###.##\n#, ###.##\n#,##,###.##\n#,###.###\n#.###\n#,###" | "options": "\n#,###.##\n#.###,##\n# ###.##\n# ###,##\n#'###.##\n#, ###.##\n#,##,###.##\n#,###.###\n#.###\n#,###" | ||||
}, | |||||
{ | |||||
"default": "0", | |||||
"fieldname": "symbol_on_right", | |||||
"fieldtype": "Check", | |||||
"label": "Show Currency Symbol on Right Side" | |||||
} | } | ||||
], | ], | ||||
"icon": "fa fa-bitcoin", | "icon": "fa fa-bitcoin", | ||||
"idx": 1, | "idx": 1, | ||||
"index_web_pages_for_search": 1, | "index_web_pages_for_search": 1, | ||||
"links": [], | "links": [], | ||||
"modified": "2020-10-29 06:33:12.879978", | |||||
"modified": "2022-07-04 09:42:52.425440", | |||||
"modified_by": "Administrator", | "modified_by": "Administrator", | ||||
"module": "Geo", | "module": "Geo", | ||||
"name": "Currency", | "name": "Currency", | ||||
"naming_rule": "By fieldname", | |||||
"owner": "Administrator", | "owner": "Administrator", | ||||
"permissions": [ | "permissions": [ | ||||
{ | { | ||||
@@ -109,5 +117,6 @@ | |||||
], | ], | ||||
"sort_field": "modified", | "sort_field": "modified", | ||||
"sort_order": "DESC", | "sort_order": "DESC", | ||||
"states": [], | |||||
"track_changes": 1 | "track_changes": 1 | ||||
} | } |
@@ -122,16 +122,22 @@ window.format_number = function (v, format, decimals) { | |||||
}; | }; | ||||
function format_currency(v, currency, decimals) { | function format_currency(v, currency, decimals) { | ||||
var format = get_number_format(currency); | |||||
var symbol = get_currency_symbol(currency); | |||||
if(decimals === undefined) { | |||||
const format = get_number_format(currency); | |||||
const symbol = get_currency_symbol(currency); | |||||
const show_symbol_on_right = frappe.model.get_value(":Currency", currency, "symbol_on_right") ?? false; | |||||
if (decimals === undefined) { | |||||
decimals = frappe.boot.sysdefaults.currency_precision || null; | decimals = frappe.boot.sysdefaults.currency_precision || null; | ||||
} | } | ||||
if (symbol) | |||||
if (symbol) { | |||||
if (show_symbol_on_right) { | |||||
return format_number(v, format, decimals) + " " + __(symbol); | |||||
} | |||||
return __(symbol) + " " + format_number(v, format, decimals); | return __(symbol) + " " + format_number(v, format, decimals); | ||||
else | |||||
return format_number(v, format, decimals); | |||||
} | |||||
return format_number(v, format, decimals); | |||||
} | } | ||||
function get_currency_symbol(currency) { | function get_currency_symbol(currency) { | ||||
@@ -249,4 +255,4 @@ Object.assign(window, { | |||||
remainder, | remainder, | ||||
round_based_on_smallest_currency_fraction, | round_based_on_smallest_currency_fraction, | ||||
in_list | in_list | ||||
}); | |||||
}); |
@@ -97,6 +97,11 @@ class TestFmtMoney(unittest.TestCase): | |||||
self.assertEqual(fmt_money(100000, format="#,###.##"), "100,000.00") | self.assertEqual(fmt_money(100000, format="#,###.##"), "100,000.00") | ||||
self.assertEqual(fmt_money(None, format="#,###.##"), "0.00") | self.assertEqual(fmt_money(None, format="#,###.##"), "0.00") | ||||
def test_fmt_with_symbol_pos(self): | |||||
frappe.db.set_value("Currency", "JPY", "symbol_on_right", 1) | |||||
self.assertEqual(fmt_money(100.0, format="#,###.##", currency="JPY"), "100.00 ¥") | |||||
self.assertEqual(fmt_money(100.0, format="#,###.##", currency="USD"), "$ 100.00") | |||||
if __name__ == "__main__": | if __name__ == "__main__": | ||||
frappe.connect() | frappe.connect() | ||||
@@ -1187,7 +1187,12 @@ def fmt_money( | |||||
if currency and frappe.defaults.get_global_default("hide_currency_symbol") != "Yes": | if currency and frappe.defaults.get_global_default("hide_currency_symbol") != "Yes": | ||||
symbol = frappe.db.get_value("Currency", currency, "symbol", cache=True) or currency | symbol = frappe.db.get_value("Currency", currency, "symbol", cache=True) or currency | ||||
amount = frappe._(symbol) + " " + amount | |||||
symbol_on_right = frappe.db.get_value("Currency", currency, "symbol_on_right", cache=True) | |||||
if symbol_on_right: | |||||
amount = f"{amount} {frappe._(symbol)}" | |||||
else: | |||||
amount = f"{frappe._(symbol)} {amount}" | |||||
return amount | return amount | ||||