2
0
mirror of https://github.com/frappe/books.git synced 2024-11-10 07:40:55 +00:00

Currency formatting with symbol

This commit is contained in:
thefalconx33 2019-08-28 14:24:11 +05:30
parent d352e39082
commit ea2d0a8383
6 changed files with 96 additions and 55 deletions

View File

@ -59,13 +59,15 @@ module.exports = class BaseDocument extends Observable {
if (await this.applyFormula()) {
// multiple changes
await this.trigger('change', {
doc: this
doc: this,
changed: fieldname
});
} else {
// no other change, trigger control refresh
await this.trigger('change', {
doc: this,
fieldname: fieldname
fieldname: fieldname,
changed: fieldname
});
}
}
@ -356,7 +358,7 @@ module.exports = class BaseDocument extends Observable {
// helper functions
getSum(tablefield, childfield) {
return this[tablefield]
.map(d => d[childfield] || 0)
.map(d => frappe.parseNumber(d[childfield]) || 0)
.reduce((a, b) => a + b, 0);
}

View File

@ -5,6 +5,9 @@ export default {
methods: {
parse(value) {
return frappe.format(value, 'Currency');
},
validate(value) {
return !isNaN(frappe.parseNumber(value));
}
}
};

View File

@ -18,8 +18,9 @@ export default {
h('option', {
attrs: {
key: option,
selected: option === this.value,
value: option
value: option,
disabled: option.indexOf('...') > -1,
selected: option.indexOf('...') > -1 || option === this.value
},
domProps: {
textContent: option

View File

@ -9,7 +9,15 @@ module.exports = {
field = { fieldtype: field };
}
if (field.fieldtype === 'Currency') {
value = numberFormat.formatNumber(value);
if (field.currencyInfo) {
value = numberFormat.formatNumber(
value,
field.currencyInfo.numberFormat,
field.currencyInfo.symbol
);
} else {
value = numberFormat.formatNumber(value);
}
} else if (field.fieldtype === 'Text') {
// value = markdown.makeHtml(value || '');
} else if (field.fieldtype === 'Date') {

View File

@ -1,34 +1,52 @@
const numberFormats = {
"#,###.##": { fractionSep: ".", groupSep: ",", precision: 2 },
"#.###,##": { fractionSep: ",", groupSep: ".", precision: 2 },
"# ###.##": { fractionSep: ".", groupSep: " ", precision: 2 },
"# ###,##": { fractionSep: ",", groupSep: " ", precision: 2 },
"#'###.##": { fractionSep: ".", groupSep: "'", precision: 2 },
"#, ###.##": { fractionSep: ".", groupSep: ", ", precision: 2 },
"#,##,###.##": { fractionSep: ".", groupSep: ",", precision: 2 },
"#,###.###": { fractionSep: ".", groupSep: ",", precision: 3 },
"#.###": { fractionSep: "", groupSep: ".", precision: 0 },
"#,###": { fractionSep: "", groupSep: ",", precision: 0 },
}
'#,###.##': { fractionSep: '.', groupSep: ',', precision: 2 },
'#.###,##': { fractionSep: ',', groupSep: '.', precision: 2 },
'# ###.##': { fractionSep: '.', groupSep: ' ', precision: 2 },
'# ###,##': { fractionSep: ',', groupSep: ' ', precision: 2 },
"#'###.##": { fractionSep: '.', groupSep: "'", precision: 2 },
'#, ###.##': { fractionSep: '.', groupSep: ', ', precision: 2 },
'#,##,###.##': { fractionSep: '.', groupSep: ',', precision: 2 },
'#,###.###': { fractionSep: '.', groupSep: ',', precision: 3 },
'#.###': { fractionSep: '', groupSep: '.', precision: 0 },
'#,###': { fractionSep: '', groupSep: ',', precision: 0 }
};
module.exports = {
// parse a formatted number string
// from "4,555,000.34" -> 4555000.34
parseNumber(number, format = '#,###.##') {
parseNumber(number, format, symbol) {
if (!number) {
return 0;
}
if (!format) {
format = frappe.AccountingSettings.numberFormat || '#,###.##';
}
if (!symbol) {
symbol = frappe.AccountingSettings.symbol || '';
}
if (typeof number === 'number') {
return number;
}
if (isNaN(parseFloat(number))) {
// remove symbol
number = number.substr(2);
}
const info = this.getFormatInfo(format);
return parseFloat(this.removeSeparator(number, info.groupSep));
},
formatNumber(number, format = '#,###.##', precision = null) {
formatNumber(number, format, symbol, precision = null) {
if (!number) {
number = 0;
}
if (!format) {
format = frappe.AccountingSettings.numberFormat || '#,###.##';
}
if (!symbol) {
symbol = frappe.AccountingSettings.symbol || '';
}
let info = this.getFormatInfo(format);
if (precision) {
info.precision = precision;
@ -53,7 +71,8 @@ module.exports = {
for (var i = integer.length; i >= 0; i--) {
var l = this.removeSeparator(str, info.groupSep).length;
if (format == "#,##,###.##" && str.indexOf(",") != -1) { // INR
if (format == '#,##,###.##' && str.indexOf(',') != -1) {
// INR
group_position = 2;
l += 1;
}
@ -64,17 +83,25 @@ module.exports = {
str += info.groupSep;
}
}
parts[0] = str.split("").reverse().join("");
parts[0] = str
.split('')
.reverse()
.join('');
}
if (parts[0] + "" == "") {
parts[0] = "0";
if (parts[0] + '' == '') {
parts[0] = '0';
}
// join decimal
parts[1] = (parts[1] && info.fractionSep) ? (info.fractionSep + parts[1]) : "";
parts[1] = parts[1] && info.fractionSep ? info.fractionSep + parts[1] : '';
// join
return (is_negative ? "-" : "") + parts[0] + parts[1];
return (
(symbol.length ? `${symbol} ` : '') +
(is_negative ? '-' : '') +
parts[0] +
parts[1]
);
},
getFormatInfo(format) {
@ -92,13 +119,14 @@ module.exports = {
var d = parseInt(precision || 0);
var m = Math.pow(10, d);
var n = +(d ? Math.abs(num) * m : Math.abs(num)).toFixed(8); // Avoid rounding errors
var i = Math.floor(n), f = n - i;
var r = ((!precision && f == 0.5) ? ((i % 2 == 0) ? i : i + 1) : Math.round(n));
var i = Math.floor(n),
f = n - i;
var r = !precision && f == 0.5 ? (i % 2 == 0 ? i : i + 1) : Math.round(n);
r = d ? r / m : r;
return is_negative ? -r : r;
},
removeSeparator(text, sep) {
return text.replace(new RegExp(sep === "." ? "\\." : sep, "g"), '');
return text.replace(new RegExp(sep === '.' ? '\\.' : sep, 'g'), '');
}
};

View File

@ -1438,7 +1438,7 @@ color-string@^1.5.2:
color-name "^1.0.0"
simple-swizzle "^0.2.2"
color@^3.1.1:
color@^3.1.2:
version "3.1.2"
resolved "https://registry.yarnpkg.com/color/-/color-3.1.2.tgz#68148e7f85d41ad7649c5fa8c8106f098d229e10"
integrity sha512-vXTJhHebByxZn3lDvDJYw4lR5+uB3vuoHsuYA5AKuxRVn5wzzIfQKGLBmgdVRHKTJYeK5rvJcHnrd0Li49CFpg==
@ -2792,7 +2792,7 @@ find-up@^3.0.0:
dependencies:
locate-path "^3.0.0"
flatpickr@^4.3.2, flatpickr@^4.5.1:
flatpickr@^4.5.1, flatpickr@^4.6.2:
version "4.6.2"
resolved "https://registry.yarnpkg.com/flatpickr/-/flatpickr-4.6.2.tgz#50e1b4fc84fbf67c5b0919ba3ddc330221f126da"
integrity sha512-bRfBPyxohUQWgCTg6jPALUO1t/x+sRJ/S/RVti/NzYvHqGRpCAesKSWKLzOuLmpu+vGHfXBld4SXvOCLFgYb+g==
@ -2891,11 +2891,6 @@ fs-constants@^1.0.0:
resolved "https://registry.yarnpkg.com/fs-constants/-/fs-constants-1.0.0.tgz#6be0de9be998ce16af8afc24497b9ee9b7ccd9ad"
integrity sha512-y6OAwoSIf7FyjMIv94u+b5rdheZEjzR63GTyZJm5qh4Bi+2YgwLCcI/fPFZkL5PSixOt6ZNKm+w+Hfp/Bciwow==
fs-copy-file-sync@^1.1.1:
version "1.1.1"
resolved "https://registry.yarnpkg.com/fs-copy-file-sync/-/fs-copy-file-sync-1.1.1.tgz#11bf32c096c10d126e5f6b36d06eece776062918"
integrity sha512-2QY5eeqVv4m2PfyMiEuy9adxNP+ajf+8AR05cEi+OAzPcOj90hvFImeZhTmKLBgSd9EvG33jsD7ZRxsx9dThkQ==
fs-extra@^4.0.1:
version "4.0.3"
resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-4.0.3.tgz#0d852122e5bc5beb453fb028e9c0c9bf36340c94"
@ -4669,7 +4664,7 @@ nan@2.13.2:
resolved "https://registry.yarnpkg.com/nan/-/nan-2.13.2.tgz#f51dc7ae66ba7d5d55e1e6d4d8092e802c9aefe7"
integrity sha512-TghvYc72wlMGMVMluVo9WRJc0mB8KxxF/gZ4YYFy7V2ZQX9l7rgbPg7vjS9mt6U5HXODVFVI2bOduCzwOMv/lw==
nan@^2.12.1, nan@^2.13.2:
nan@^2.12.1, nan@^2.13.2, nan@^2.14.0:
version "2.14.0"
resolved "https://registry.yarnpkg.com/nan/-/nan-2.14.0.tgz#7818f722027b2459a86f0295d434d1fc2336c52c"
integrity sha512-INOFj37C7k3AfaNTtX8RhsTw7qRy7eLET14cROi9+5HAVbbHuIWUHEauBv5qT4Av2tWasiTY1Jw6puUNqRJXQg==
@ -5716,10 +5711,10 @@ punycode@^2.1.0:
resolved "https://registry.yarnpkg.com/punycode/-/punycode-2.1.1.tgz#b58b010ac40c22c5657616c8d2c2c02c7bf479ec"
integrity sha512-XRsRjdf+j5ml+y/6GKHPZbrF/8p2Yga0JPtdqTIY2Xe5ohJPD9saDJJLPvp9+NSBprVvevdXZybnj2cv8OEd0A==
puppeteer@^1.2.0:
version "1.18.1"
resolved "https://registry.yarnpkg.com/puppeteer/-/puppeteer-1.18.1.tgz#4a66f3bdab01115ededf70443ec904c99917a815"
integrity sha512-luUy0HPSuWPsPZ1wAp6NinE0zgetWtudf5zwZ6dHjMWfYpTQcmKveFRox7VBNhQ98OjNA9PQ9PzQyX8k/KrxTg==
puppeteer-core@^1.19.0:
version "1.19.0"
resolved "https://registry.yarnpkg.com/puppeteer-core/-/puppeteer-core-1.19.0.tgz#3c3f98edb5862583e3a9c19cbc0da57ccc63ba5c"
integrity sha512-ZPbbjUymorIJomHBvdZX5+2gciUmQtAdepCrkweHH6rMJr96xd/dXzHgmYEOBMatH44SmJrcMtWkgsLHJqT89g==
dependencies:
debug "^4.1.0"
extract-zip "^1.6.6"
@ -6188,11 +6183,16 @@ semver-diff@^2.0.0:
resolved "https://registry.yarnpkg.com/semver/-/semver-5.7.0.tgz#790a7cf6fea5459bac96110b29b60412dc8ff96b"
integrity sha512-Ya52jSX2u7QKghxeoFGpLwCtGlt7j0oY9DYb5apt9nPlJ42ID+ulTXESnt/qAQcoSERyZ5sl3LDIOw0nAn/5DA==
semver@^6.0.0, semver@^6.1.1, semver@^6.2.0:
semver@^6.1.1, semver@^6.2.0:
version "6.2.0"
resolved "https://registry.yarnpkg.com/semver/-/semver-6.2.0.tgz#4d813d9590aaf8a9192693d6c85b9344de5901db"
integrity sha512-jdFC1VdUGT/2Scgbimf7FSx9iJLXoqfglSF+gJeuNWVpiE37OIbc1jywR/GJyFdz3mnkz2/id0L0J/cr0izR5A==
semver@^6.3.0:
version "6.3.0"
resolved "https://registry.yarnpkg.com/semver/-/semver-6.3.0.tgz#ee0a64c8af5e8ceea67687b133761e1becbd1d3d"
integrity sha512-b39TBaTSfV6yBrapU89p5fKekE2m/NwnDocOVruQFS1/veMgdzuPcnOM34M6CwxW8jH/lxEa5rBoDeUwu5HHTw==
semver@~5.3.0:
version "5.3.0"
resolved "https://registry.yarnpkg.com/semver/-/semver-5.3.0.tgz#9b2ce5d3de02d17c6012ad326aa6b4d0cf54f94f"
@ -6292,20 +6292,19 @@ shallow-clone@^1.0.0:
kind-of "^5.0.0"
mixin-object "^2.0.1"
sharp@^0.22.1:
version "0.22.1"
resolved "https://registry.yarnpkg.com/sharp/-/sharp-0.22.1.tgz#a67c0e75567f03dd5a7861b901fec04072c5b0f4"
integrity sha512-lXzSk/FL5b/MpWrT1pQZneKe25stVjEbl6uhhJcTULm7PhmJgKKRbTDM/vtjyUuC/RLqL2PRyC4rpKwbv3soEw==
sharp@^0.23.0:
version "0.23.0"
resolved "https://registry.yarnpkg.com/sharp/-/sharp-0.23.0.tgz#e34c7c44af219f8fd6b780adaa2b44aa6a22cbd9"
integrity sha512-3+QlktTYDPO9CLmB3DUaBSj729ic3R9TO5Bz318F8WubUW10HR4os0Tm+GdYNcVg0layhMhP4Hf2SILwXVG2ig==
dependencies:
color "^3.1.1"
color "^3.1.2"
detect-libc "^1.0.3"
fs-copy-file-sync "^1.1.1"
nan "^2.13.2"
nan "^2.14.0"
npmlog "^4.1.2"
prebuild-install "^5.3.0"
semver "^6.0.0"
semver "^6.3.0"
simple-get "^3.0.3"
tar "^4.4.8"
tar "^4.4.10"
tunnel-agent "^0.6.0"
shebang-command@^1.2.0:
@ -6864,7 +6863,7 @@ tar@^2.0.0:
fstream "^1.0.12"
inherits "2"
tar@^4, tar@^4.4.8:
tar@^4, tar@^4.4.10:
version "4.4.10"
resolved "https://registry.yarnpkg.com/tar/-/tar-4.4.10.tgz#946b2810b9a5e0b26140cf78bea6b0b0d689eba1"
integrity sha512-g2SVs5QIxvo6OLp0GudTqEf05maawKUxXru104iaayWA09551tFCTI8f1Asb4lPfkBr91k07iL4c11XO3/b0tA==
@ -7333,10 +7332,10 @@ vm-browserify@^1.0.1:
resolved "https://registry.yarnpkg.com/vm-browserify/-/vm-browserify-1.1.0.tgz#bd76d6a23323e2ca8ffa12028dc04559c75f9019"
integrity sha512-iq+S7vZJE60yejDYM0ek6zg308+UZsdtPExWP9VZoCFCz1zkJoXFnAX7aZfd/ZwrkidzdUZL0C/ryW+JwAiIGw==
vue-flatpickr-component@^7.0.4:
version "7.0.6"
resolved "https://registry.yarnpkg.com/vue-flatpickr-component/-/vue-flatpickr-component-7.0.6.tgz#f0e750f5c43c59471eb07dd88f7e534391305815"
integrity sha512-u6bSP/2ONuUruR49V1YX/2in84j6jzVn+QVhsef5MuGv+OlF+PocrGd/YNLoQRdLc1FlcgYqqJ2UNiYI2PvhSw==
vue-flatpickr-component@^8.1.2:
version "8.1.2"
resolved "https://registry.yarnpkg.com/vue-flatpickr-component/-/vue-flatpickr-component-8.1.2.tgz#c8bcfa95a4da4e528f4850141cb0ad7b628a1d6d"
integrity sha512-z3FPLrkQgrTXWU3YZ8pcVQ9Gu4CLKUoBQWqK2i3HIhNFcXnwbfY/btf+Uk8zr9Csotrbzw1NWl/WglbW8Zn/Hg==
dependencies:
flatpickr "^4.5.1"