2018-07-14 16:12:34 +00:00
|
|
|
const numberFormat = require('./numberFormat');
|
2018-10-22 20:50:35 +00:00
|
|
|
// const markdown = new (require('showdown').Converter)();
|
2018-07-17 18:43:51 +00:00
|
|
|
const luxon = require('luxon');
|
2018-02-21 09:38:56 +00:00
|
|
|
const frappe = require('frappejs');
|
2018-02-14 12:50:56 +00:00
|
|
|
|
|
|
|
module.exports = {
|
2019-12-02 12:06:25 +00:00
|
|
|
format(value, df, doc) {
|
|
|
|
if (!df) {
|
|
|
|
return value;
|
2019-07-17 10:02:49 +00:00
|
|
|
}
|
2019-12-02 12:06:25 +00:00
|
|
|
|
|
|
|
if (typeof df === 'string') {
|
|
|
|
df = { fieldtype: df };
|
|
|
|
}
|
|
|
|
|
|
|
|
if (df.fieldtype === 'Currency') {
|
|
|
|
value = formatCurrency(value, df, doc);
|
|
|
|
} else if (df.fieldtype === 'Text') {
|
2019-07-17 10:02:49 +00:00
|
|
|
// value = markdown.makeHtml(value || '');
|
2019-12-02 12:06:25 +00:00
|
|
|
} else if (df.fieldtype === 'Date') {
|
2019-07-17 10:02:49 +00:00
|
|
|
let dateFormat;
|
|
|
|
if (!frappe.SystemSettings) {
|
|
|
|
dateFormat = 'yyyy-MM-dd';
|
|
|
|
} else {
|
|
|
|
dateFormat = frappe.SystemSettings.dateFormat;
|
|
|
|
}
|
2018-02-21 09:38:56 +00:00
|
|
|
|
2020-01-28 10:56:44 +00:00
|
|
|
if (typeof value === 'string') {
|
|
|
|
// ISO String
|
|
|
|
value = luxon.DateTime.fromISO(value);
|
|
|
|
} else if (Object.prototype.toString.call(value) === '[object Date]') {
|
|
|
|
// JS Date
|
|
|
|
value = luxon.DateTime.fromJSDate(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
value = value.toFormat(dateFormat);
|
2019-07-17 10:02:49 +00:00
|
|
|
if (value === 'Invalid DateTime') {
|
|
|
|
value = '';
|
|
|
|
}
|
2019-12-02 12:06:25 +00:00
|
|
|
} else if (df.fieldtype === 'Check') {
|
2019-08-14 07:48:04 +00:00
|
|
|
typeof parseInt(value) === 'number'
|
|
|
|
? (value = parseInt(value))
|
|
|
|
: (value = Boolean(value));
|
2019-07-17 10:02:49 +00:00
|
|
|
} else {
|
|
|
|
if (value === null || value === undefined) {
|
|
|
|
value = '';
|
|
|
|
} else {
|
|
|
|
value = value + '';
|
|
|
|
}
|
2018-02-14 12:50:56 +00:00
|
|
|
}
|
2019-07-17 10:02:49 +00:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
};
|
2019-12-02 12:06:25 +00:00
|
|
|
|
|
|
|
function formatCurrency(value, df, doc) {
|
|
|
|
let currency = df.currency || '';
|
|
|
|
if (doc && df.getCurrency) {
|
|
|
|
if (doc.meta && doc.meta.isChild) {
|
|
|
|
currency = df.getCurrency(doc, doc.parentdoc);
|
|
|
|
} else {
|
|
|
|
currency = df.getCurrency(doc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!currency) {
|
|
|
|
currency = frappe.AccountingSettings.currency;
|
|
|
|
}
|
|
|
|
|
|
|
|
let currencySymbol = frappe.currencySymbols[currency] || '';
|
|
|
|
return currencySymbol + ' ' + numberFormat.formatNumber(value);
|
|
|
|
}
|