2
0
mirror of https://github.com/frappe/books.git synced 2024-12-26 04:17:36 +00:00
books/frappe/utils/format.js

114 lines
2.7 KiB
JavaScript
Raw Normal View History

import frappe from 'frappe';
import { DateTime } from 'luxon';
import { DEFAULT_DISPLAY_PRECISION, DEFAULT_LOCALE } from './consts';
2018-02-14 12:50:56 +00:00
2022-03-21 05:58:52 +00:00
export function format(value, df, doc) {
if (!df) {
return value;
}
2019-12-02 12:06:25 +00:00
2022-03-21 05:58:52 +00:00
if (typeof df === 'string') {
df = { fieldtype: df };
}
2019-12-02 12:06:25 +00:00
2022-03-21 05:58:52 +00:00
if (df.fieldtype === 'Currency') {
const currency = getCurrency(df, doc);
value = formatCurrency(value, currency);
} else if (df.fieldtype === 'Date') {
let dateFormat;
if (!frappe.SystemSettings) {
dateFormat = 'yyyy-MM-dd';
} else {
dateFormat = frappe.SystemSettings.dateFormat;
}
2022-03-21 05:58:52 +00:00
if (typeof value === 'string') {
// ISO String
value = DateTime.fromISO(value);
} else if (Object.prototype.toString.call(value) === '[object Date]') {
// JS Date
value = DateTime.fromJSDate(value);
}
2020-01-28 10:56:44 +00:00
2022-03-21 05:58:52 +00:00
value = value.toFormat(dateFormat);
if (value === 'Invalid DateTime') {
value = '';
}
} else if (df.fieldtype === 'Check') {
typeof parseInt(value) === 'number'
? (value = parseInt(value))
: (value = Boolean(value));
} else {
if (value === null || value === undefined) {
value = '';
2019-07-17 10:02:49 +00:00
} else {
2022-03-21 05:58:52 +00:00
value = value + '';
2018-02-14 12:50:56 +00:00
}
2022-03-21 05:58:52 +00:00
}
return value;
}
2019-12-02 12:06:25 +00:00
2021-12-31 08:10:17 +00:00
function formatCurrency(value, currency) {
2021-12-29 11:52:07 +00:00
let valueString;
try {
2021-12-31 08:10:17 +00:00
valueString = formatNumber(value);
2021-12-29 11:52:07 +00:00
} catch (err) {
err.message += ` value: '${value}', type: ${typeof value}`;
throw err;
}
2021-12-31 08:10:17 +00:00
const currencySymbol = frappe.currencySymbols[currency];
if (currencySymbol) {
return currencySymbol + ' ' + valueString;
}
2021-12-31 08:10:17 +00:00
return valueString;
2019-12-02 12:06:25 +00:00
}
2021-12-31 08:10:17 +00:00
function formatNumber(value) {
2022-01-18 08:13:06 +00:00
const numberFormatter = getNumberFormatter();
2021-12-31 08:10:17 +00:00
if (typeof value === 'number') {
2022-01-18 08:13:06 +00:00
return numberFormatter.format(value);
2021-12-31 08:10:17 +00:00
}
if (value.round) {
2022-01-18 08:13:06 +00:00
return numberFormatter.format(value.round());
2021-12-31 08:10:17 +00:00
}
2022-01-18 08:13:06 +00:00
const formattedNumber = numberFormatter.format(value);
if (formattedNumber === 'NaN') {
2021-12-31 08:10:17 +00:00
throw Error(
2022-01-18 08:13:06 +00:00
`invalid value passed to formatNumber: '${value}' of type ${typeof value}`
2021-12-31 08:10:17 +00:00
);
}
2022-01-18 08:13:06 +00:00
return formattedNumber;
2021-12-31 08:10:17 +00:00
}
function getNumberFormatter() {
if (frappe.currencyFormatter) {
return frappe.currencyFormatter;
}
const locale = frappe.SystemSettings.locale ?? DEFAULT_LOCALE;
const display =
frappe.SystemSettings.displayPrecision ?? DEFAULT_DISPLAY_PRECISION;
return (frappe.currencyFormatter = Intl.NumberFormat(locale, {
style: 'decimal',
minimumFractionDigits: display,
}));
}
function getCurrency(df, doc) {
if (!(doc && df.getCurrency)) {
return df.currency || frappe.AccountingSettings.currency || '';
}
if (doc.meta && doc.meta.isChild) {
return df.getCurrency(doc, doc.parentdoc);
}
return df.getCurrency(doc);
}