2015-12-07 14:34:55 +01:00
|
|
|
<script type="text/javascript">
|
|
|
|
|
|
|
|
var currencies = {!! \Cache::get('currencies') !!};
|
|
|
|
var currencyMap = {};
|
|
|
|
for (var i=0; i<currencies.length; i++) {
|
|
|
|
var currency = currencies[i];
|
|
|
|
currencyMap[currency.id] = currency;
|
2016-09-11 16:30:23 +02:00
|
|
|
currencyMap[currency.code] = currency;
|
2015-12-07 14:34:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var countries = {!! \Cache::get('countries') !!};
|
|
|
|
var countryMap = {};
|
|
|
|
for (var i=0; i<countries.length; i++) {
|
|
|
|
var country = countries[i];
|
|
|
|
countryMap[country.id] = country;
|
|
|
|
}
|
|
|
|
|
|
|
|
var NINJA = NINJA || {};
|
|
|
|
@if (Auth::check())
|
|
|
|
NINJA.primaryColor = "{{ Auth::user()->account->primary_color }}";
|
|
|
|
NINJA.secondaryColor = "{{ Auth::user()->account->secondary_color }}";
|
|
|
|
NINJA.fontSize = {{ Auth::user()->account->font_size ?: DEFAULT_FONT_SIZE }};
|
2016-01-07 08:08:30 +01:00
|
|
|
NINJA.headerFont = {!! json_encode(Auth::user()->account->getHeaderFontName()) !!};
|
|
|
|
NINJA.bodyFont = {!! json_encode(Auth::user()->account->getBodyFontName()) !!};
|
2015-12-07 14:34:55 +01:00
|
|
|
@else
|
|
|
|
NINJA.fontSize = {{ DEFAULT_FONT_SIZE }};
|
|
|
|
@endif
|
|
|
|
|
|
|
|
NINJA.parseFloat = function(str) {
|
|
|
|
if (!str) return '';
|
|
|
|
str = (str+'').replace(/[^0-9\.\-]/g, '');
|
2016-09-11 16:30:23 +02:00
|
|
|
|
2015-12-07 14:34:55 +01:00
|
|
|
return window.parseFloat(str);
|
|
|
|
}
|
|
|
|
|
2016-10-20 16:17:47 +02:00
|
|
|
function formatMoneyInvoice(value, invoice, decorator) {
|
2015-12-07 14:34:55 +01:00
|
|
|
var account = invoice.account;
|
|
|
|
var client = invoice.client;
|
|
|
|
|
2016-10-20 16:17:47 +02:00
|
|
|
return formatMoneyAccount(value, account, client, decorator);
|
2015-12-07 14:34:55 +01:00
|
|
|
}
|
|
|
|
|
2016-10-20 16:17:47 +02:00
|
|
|
function formatMoneyAccount(value, account, client, decorator) {
|
2015-12-07 14:34:55 +01:00
|
|
|
var currencyId = false;
|
|
|
|
var countryId = false;
|
|
|
|
|
|
|
|
if (client && client.currency_id) {
|
|
|
|
currencyId = client.currency_id;
|
|
|
|
} else if (account && account.currency_id) {
|
|
|
|
currencyId = account.currency_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (client && client.country_id) {
|
|
|
|
countryId = client.country_id;
|
|
|
|
} else if (account && account.country_id) {
|
|
|
|
countryId = account.country_id;
|
|
|
|
}
|
|
|
|
|
2016-10-20 22:17:37 +02:00
|
|
|
if (account && ! decorator) {
|
2016-10-20 16:17:47 +02:00
|
|
|
decorator = account.show_currency_code ? 'code' : 'symbol';
|
|
|
|
}
|
|
|
|
|
|
|
|
return formatMoney(value, currencyId, countryId, decorator)
|
2015-12-07 14:34:55 +01:00
|
|
|
}
|
|
|
|
|
2016-10-20 16:17:47 +02:00
|
|
|
function formatMoney(value, currencyId, countryId, decorator) {
|
2015-12-07 14:34:55 +01:00
|
|
|
value = NINJA.parseFloat(value);
|
|
|
|
|
|
|
|
if (!currencyId) {
|
|
|
|
currencyId = {{ Session::get(SESSION_CURRENCY, DEFAULT_CURRENCY) }};
|
|
|
|
}
|
|
|
|
|
2016-10-20 17:16:32 +02:00
|
|
|
if (!decorator) {
|
|
|
|
decorator = '{{ Session::get(SESSION_CURRENCY_DECORATOR, CURRENCY_DECORATOR_SYMBOL) }}';
|
|
|
|
}
|
|
|
|
|
2015-12-07 14:34:55 +01:00
|
|
|
var currency = currencyMap[currencyId];
|
2016-05-05 21:49:09 +02:00
|
|
|
var precision = currency.precision;
|
2015-12-07 14:34:55 +01:00
|
|
|
var thousand = currency.thousand_separator;
|
|
|
|
var decimal = currency.decimal_separator;
|
2016-01-18 15:35:05 +01:00
|
|
|
var code = currency.code;
|
2016-05-16 22:37:16 +02:00
|
|
|
var swapSymbol = currency.swap_currency_symbol;
|
2015-12-07 14:34:55 +01:00
|
|
|
|
|
|
|
if (countryId && currencyId == {{ CURRENCY_EURO }}) {
|
|
|
|
var country = countryMap[countryId];
|
|
|
|
swapSymbol = country.swap_currency_symbol;
|
|
|
|
if (country.thousand_separator) {
|
|
|
|
thousand = country.thousand_separator;
|
|
|
|
}
|
|
|
|
if (country.decimal_separator) {
|
|
|
|
decimal = country.decimal_separator;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-05 21:49:09 +02:00
|
|
|
value = accounting.formatMoney(value, '', precision, thousand, decimal);
|
2015-12-07 14:34:55 +01:00
|
|
|
var symbol = currency.symbol;
|
|
|
|
|
2016-10-20 16:17:47 +02:00
|
|
|
if (decorator == 'none') {
|
2015-12-07 14:34:55 +01:00
|
|
|
return value;
|
2016-10-20 16:44:31 +02:00
|
|
|
} else if (decorator == '{{ CURRENCY_DECORATOR_CODE }}' || ! symbol) {
|
2016-01-18 15:35:05 +01:00
|
|
|
return value + ' ' + code;
|
2015-12-07 14:34:55 +01:00
|
|
|
} else if (swapSymbol) {
|
|
|
|
return value + ' ' + symbol.trim();
|
|
|
|
} else {
|
|
|
|
return symbol + value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-11 16:30:23 +02:00
|
|
|
</script>
|