Refactor money format functionality
This commit is contained in:
parent
2ab39c4f17
commit
0a9193479a
@ -95,6 +95,10 @@ trait Charts
|
|||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function formatMoney( ){
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
public function getLineChartOptions()
|
public function getLineChartOptions()
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
@ -109,67 +113,53 @@ trait Charts
|
|||||||
'position' => 'nearest',
|
'position' => 'nearest',
|
||||||
'callbacks' => [
|
'callbacks' => [
|
||||||
'label' => new Raw("function(tooltipItem, data) {
|
'label' => new Raw("function(tooltipItem, data) {
|
||||||
const isPrefix = '" . config('money.' . setting('default.currency') . '.symbol_first') . "'
|
const moneySettings = {
|
||||||
|
|
||||||
const money = {
|
|
||||||
decimal: '" . config('money.' . setting('default.currency') . '.decimal_mark') . "',
|
decimal: '" . config('money.' . setting('default.currency') . '.decimal_mark') . "',
|
||||||
thousands: '". config('money.' . setting('default.currency') . '.thousands_separator') . "',
|
thousands: '". config('money.' . setting('default.currency') . '.thousands_separator') . "',
|
||||||
prefix: '" . config('money.' . setting('default.currency') . '.symbol') . "',
|
symbol: '" . config('money.' . setting('default.currency') . '.symbol') . "',
|
||||||
suffix: '" . config('money.' . setting('default.currency') . '.symbol') . "',
|
isPrefix: '" . config('money.' . setting('default.currency') . '.symbol_first') . "',
|
||||||
precision: '" . config('money.' . setting('default.currency') . '.precision') . "',
|
precision: '" . config('money.' . setting('default.currency') . '.precision') . "',
|
||||||
}
|
};
|
||||||
|
|
||||||
const format = function (input, opt = {
|
const formattedCurrency = function (input, opt = moneySettings) {
|
||||||
decimal: '" . config('money.' . setting('default.currency') . '.decimal_mark') . "',
|
function fixed (precision) {
|
||||||
thousands: '" . config('money.' . setting('default.currency') . '.thousands_separator') . "',
|
return Math.max(0, Math.min(precision, 20));
|
||||||
prefix: '" . config('money.' . setting('default.currency') . '.symbol') . "',
|
};
|
||||||
suffix: '" . config('money.' . setting('default.currency') . '.symbol') . "',
|
|
||||||
precision: '" . config('money.' . setting('default.currency') . '.precision') . "',
|
|
||||||
}) {
|
|
||||||
if (typeof input === 'number') {
|
|
||||||
input = input.toFixed(fixed(opt.precision));
|
|
||||||
}
|
|
||||||
|
|
||||||
var negative = input.indexOf('-') >= 0 ? '-' : '';
|
function toStr(value) {
|
||||||
var numbers = toStr(input).replace(/\D+/g, '') || '0';
|
|
||||||
var currency = numbersToCurrency(numbers, 2);
|
|
||||||
var parts = toStr(currency).split('.');
|
|
||||||
var integer = parts[0];
|
|
||||||
var decimal = parts[1];
|
|
||||||
|
|
||||||
integer.replace(/(\d)(?=(?:\d{3})+\b)/gm, `,`);
|
|
||||||
|
|
||||||
return opt.prefix + negative + joinIntegerAndDecimal(integer, decimal, opt.decimal) + opt.suffix;
|
|
||||||
}
|
|
||||||
|
|
||||||
const fixed = function (precision) {
|
|
||||||
return between(0, precision, 20)
|
|
||||||
}
|
|
||||||
|
|
||||||
const joinIntegerAndDecimal = function (integer, decimal, separator) {
|
|
||||||
return decimal ? integer + separator + decimal : integer;
|
|
||||||
}
|
|
||||||
|
|
||||||
const toStr = function (value) {
|
|
||||||
return value ? value.toString() : '';
|
return value ? value.toString() : '';
|
||||||
}
|
};
|
||||||
|
|
||||||
const numbersToCurrency = function (numbers, precision) {
|
function numbersToCurrency(numbers, precision) {
|
||||||
var exp = Math.pow(10, precision);
|
var exp = Math.pow(10, precision);
|
||||||
var float = parseFloat(numbers) / exp;
|
var float = parseFloat(numbers) / exp;
|
||||||
|
|
||||||
return float.toFixed(fixed(precision));
|
return float.toFixed(fixed(precision));
|
||||||
|
};
|
||||||
|
|
||||||
|
function joinIntegerAndDecimal (integer, decimal, separator) {
|
||||||
|
return decimal ? integer + separator + decimal : integer;
|
||||||
|
};
|
||||||
|
|
||||||
|
if (typeof input === 'number') {
|
||||||
|
input = input.toFixed(fixed(opt.precision));
|
||||||
|
};
|
||||||
|
|
||||||
|
var negative = input.indexOf('-') >= 0 ? '-' : '';
|
||||||
|
var numbers = toStr(input).replace(/\D+/g, '') || '0';
|
||||||
|
var currency = numbersToCurrency(numbers, opt.precision);
|
||||||
|
var parts = toStr(currency).split('.');
|
||||||
|
var integer = parts[0].replace(/(\d)(?=(?:\d{3})+\b)/gm, opt.thousands);;
|
||||||
|
var decimal = parts[1];
|
||||||
|
|
||||||
|
if(opt.isPrefix) {
|
||||||
|
return opt.symbol + negative + joinIntegerAndDecimal(integer, decimal, opt.decimal)
|
||||||
}
|
}
|
||||||
|
|
||||||
const between = function (min, n, max) {
|
return negative + joinIntegerAndDecimal(integer, decimal, opt.decimal) + opt.suffix;
|
||||||
return Math.max(min, Math.min(n, max));
|
};
|
||||||
}
|
|
||||||
|
|
||||||
if ('" . config('money.' . setting('default.currency') . '.symbol_first') . "' === 1) {
|
return formattedCurrency(tooltipItem.yLabel, moneySettings);
|
||||||
return format(tooltipItem.yLabel);
|
|
||||||
}
|
|
||||||
|
|
||||||
return format(tooltipItem.yLabel);
|
|
||||||
}")
|
}")
|
||||||
],
|
],
|
||||||
],
|
],
|
||||||
@ -178,6 +168,7 @@ trait Charts
|
|||||||
'ticks' => [
|
'ticks' => [
|
||||||
'beginAtZero' => true,
|
'beginAtZero' => true,
|
||||||
'callback' => new Raw("function(value, index, values) {
|
'callback' => new Raw("function(value, index, values) {
|
||||||
|
|
||||||
return '" . config('money.' . setting('default.currency') . '.symbol') . "' + value;
|
return '" . config('money.' . setting('default.currency') . '.symbol') . "' + value;
|
||||||
}"),
|
}"),
|
||||||
],
|
],
|
||||||
|
Loading…
x
Reference in New Issue
Block a user