2
0
mirror of https://github.com/frappe/books.git synced 2024-12-25 20:11:15 +00:00

fix: Format currency

This commit is contained in:
Faris Ansari 2019-12-02 17:36:25 +05:30
parent 5273177768
commit 51541465e6

View File

@ -4,15 +4,20 @@ const luxon = require('luxon');
const frappe = require('frappejs'); const frappe = require('frappejs');
module.exports = { module.exports = {
format(value, field) { format(value, df, doc) {
if (typeof field === 'string') { if (!df) {
field = { fieldtype: field }; return value;
} }
if (field.fieldtype === 'Currency') {
value = numberFormat.formatNumber(value); if (typeof df === 'string') {
} else if (field.fieldtype === 'Text') { df = { fieldtype: df };
}
if (df.fieldtype === 'Currency') {
value = formatCurrency(value, df, doc);
} else if (df.fieldtype === 'Text') {
// value = markdown.makeHtml(value || ''); // value = markdown.makeHtml(value || '');
} else if (field.fieldtype === 'Date') { } else if (df.fieldtype === 'Date') {
let dateFormat; let dateFormat;
if (!frappe.SystemSettings) { if (!frappe.SystemSettings) {
dateFormat = 'yyyy-MM-dd'; dateFormat = 'yyyy-MM-dd';
@ -24,7 +29,7 @@ module.exports = {
if (value === 'Invalid DateTime') { if (value === 'Invalid DateTime') {
value = ''; value = '';
} }
} else if (field.fieldtype === 'Check') { } else if (df.fieldtype === 'Check') {
typeof parseInt(value) === 'number' typeof parseInt(value) === 'number'
? (value = parseInt(value)) ? (value = parseInt(value))
: (value = Boolean(value)); : (value = Boolean(value));
@ -38,3 +43,21 @@ module.exports = {
return value; return value;
} }
}; };
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);
}