fix: removed df parameter and added format parameter

This commit is contained in:
Shariq Ansari 2024-12-25 18:57:15 +05:30
parent 90bf761eb3
commit eb5d362e00
2 changed files with 14 additions and 17 deletions

View File

@ -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 {

View File

@ -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]