From eb5d362e000d0746c812cb94fb8d08c835ac94a8 Mon Sep 17 00:00:00 2001 From: Shariq Ansari Date: Wed, 25 Dec 2024 18:57:15 +0530 Subject: [PATCH] fix: removed df parameter and added format parameter --- frontend/src/stores/meta.js | 6 +++--- frontend/src/utils/numberFormat.js | 25 +++++++++++-------------- 2 files changed, 14 insertions(+), 17 deletions(-) diff --git a/frontend/src/stores/meta.js b/frontend/src/stores/meta.js index bd086d12..dfcdcb29 100644 --- a/frontend/src/stores/meta.js +++ b/frontend/src/stores/meta.js @@ -28,13 +28,13 @@ export function getMeta(doctype) { function getFormattedFloat(fieldname, doc) { let df = doctypeMeta[doctype]?.fields.find((f) => f.fieldname == fieldname) let precision = df?.precision || null - return formatNumber(doc[fieldname], "", precision) + return formatNumber(doc[fieldname], '', precision) } function getFormattedCurrency(fieldname, doc) { let currency = window.sysdefaults.currency || 'USD' - let df = doctypeMeta[doctype]?.fields.find((f) => f.fieldname == fieldname) + let precision = df?.precision || null if (df && df.options) { if (df.options.indexOf(':') != -1) { @@ -44,7 +44,7 @@ export function getMeta(doctype) { } } - return formatCurrency(doc[fieldname], df, currency) + return formatCurrency(doc[fieldname], '', currency, precision) } return { diff --git a/frontend/src/utils/numberFormat.js b/frontend/src/utils/numberFormat.js index 061e7f70..7ee2128f 100644 --- a/frontend/src/utils/numberFormat.js +++ b/frontend/src/utils/numberFormat.js @@ -1,3 +1,5 @@ +import { get } from '@vueuse/core' + const NUMBER_FORMAT_INFO = { '#,###.##': { decimalStr: '.', groupSep: ',' }, '#.###,##': { decimalStr: ',', groupSep: '.' }, @@ -159,14 +161,11 @@ export function formatNumber(v, format, decimals) { return (isNegative ? '-' : '') + part[0] + part[1] } -export function formatCurrency(value, df, currency = 'USD') { - if (!value || !df) return '' +export function formatCurrency(value, format, currency = 'USD', precision = 2) { + value = value == null || value === '' ? 0 : value - let precision - if (typeof df.precision == 'number') { - precision = df.precision - } else { - precision = cint(df.precision || window.sysdefaults.currency_precision || 2) + if (typeof precision != 'number') { + precision = cint(precision || window.sysdefaults.currency_precision || 2) } // If you change anything below, it's going to hurt a company in UAE, a bit. @@ -183,9 +182,7 @@ export function formatCurrency(value, df, currency = 'USD') { } } - value = value == null || value === '' ? '' : value - - let format = getNumberFormat() + format = getNumberFormat(format) let symbol = getCurrencySymbol(currency) if (symbol) { @@ -195,6 +192,10 @@ export function formatCurrency(value, df, currency = 'USD') { return formatNumber(value, format, precision) } +function getNumberFormat(format = null) { + return format || window.sysdefaults.number_format || '#,###.##' +} + function getCurrencySymbol(currencyCode) { try { const formatter = new Intl.NumberFormat('en-US', { @@ -213,10 +214,6 @@ function getCurrencySymbol(currencyCode) { } } -function getNumberFormat() { - return window.sysdefaults.number_format || '#,###.##' -} - function getNumberFormatInfo(format) { let info = NUMBER_FORMAT_INFO[format]