mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-09-06 04:34:00 +00:00
Currently the php function `number_format` is used to format currencies. This is problematic as we have to figure out different things for different currencies ourselves. These formats are determined based on the libc's locale functions. The issue arises where an OS doesn't have the proper locales installed, or, in some cases, it's not supported (see below on multiple issues). This addresses this issue by using the php-intl extensions to format the numbers based on the locale. The extension is already a requirement in `composer.json`. The solution does not rely on `LC_MONETARY` from the underlying libc (which in Alpine Linux's case, which uses musl, is not supported as of yet). List of issues that are related and would potentially be fixed using this PR: - #2298 - #2946 - #3070 - #3306 - #3519
172 lines
6.4 KiB
PHP
172 lines
6.4 KiB
PHP
<?php
|
|
/**
|
|
* JavascriptController.php
|
|
* Copyright (c) 2019 james@firefly-iii.org
|
|
*
|
|
* This file is part of Firefly III (https://github.com/firefly-iii).
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU Affero General Public License as
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
* License, or (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU Affero General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
*/
|
|
declare(strict_types=1);
|
|
|
|
namespace FireflyIII\Http\Controllers;
|
|
|
|
use Carbon\Carbon;
|
|
use FireflyIII\Models\Account;
|
|
use FireflyIII\Models\AccountType;
|
|
use FireflyIII\Models\TransactionCurrency;
|
|
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
|
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
|
|
use FireflyIII\Support\Http\Controllers\GetConfigurationData;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Http\Response;
|
|
|
|
/**
|
|
* Class JavascriptController.
|
|
*/
|
|
class JavascriptController extends Controller
|
|
{
|
|
use GetConfigurationData;
|
|
|
|
/**
|
|
* Show info about accounts.
|
|
*
|
|
* @param AccountRepositoryInterface $repository
|
|
* @param CurrencyRepositoryInterface $currencyRepository
|
|
*
|
|
* @return Response
|
|
*/
|
|
public function accounts(AccountRepositoryInterface $repository, CurrencyRepositoryInterface $currencyRepository): Response
|
|
{
|
|
$accounts = $repository->getAccountsByType(
|
|
[AccountType::DEFAULT, AccountType::ASSET, AccountType::DEBT, AccountType::LOAN, AccountType::MORTGAGE, AccountType::CREDITCARD]
|
|
);
|
|
$preference = app('preferences')->get('currencyPreference', config('firefly.default_currency', 'EUR'));
|
|
/** @noinspection NullPointerExceptionInspection */
|
|
$default = $currencyRepository->findByCodeNull($preference->data);
|
|
|
|
$data = ['accounts' => []];
|
|
|
|
/** @var Account $account */
|
|
foreach ($accounts as $account) {
|
|
$accountId = $account->id;
|
|
$currency = (int) $repository->getMetaValue($account, 'currency_id');
|
|
/** @noinspection NullPointerExceptionInspection */
|
|
$currency = 0 === $currency ? $default->id : $currency;
|
|
$entry = ['preferredCurrency' => $currency, 'name' => $account->name];
|
|
$data['accounts'][$accountId] = $entry;
|
|
}
|
|
|
|
return response()
|
|
->view('javascript.accounts', $data)
|
|
->header('Content-Type', 'text/javascript');
|
|
}
|
|
|
|
/**
|
|
* Get info about currencies.
|
|
*
|
|
* @param CurrencyRepositoryInterface $repository
|
|
*
|
|
* @return Response
|
|
*/
|
|
public function currencies(CurrencyRepositoryInterface $repository): Response
|
|
{
|
|
$currencies = $repository->get();
|
|
$data = ['currencies' => []];
|
|
/** @var TransactionCurrency $currency */
|
|
foreach ($currencies as $currency) {
|
|
$currencyId = $currency->id;
|
|
$entry = ['name' => $currency->name, 'code' => $currency->code, 'symbol' => $currency->symbol];
|
|
$data['currencies'][$currencyId] = $entry;
|
|
}
|
|
|
|
return response()
|
|
->view('javascript.currencies', $data)
|
|
->header('Content-Type', 'text/javascript');
|
|
}
|
|
|
|
/**
|
|
* Bit of a hack but OK.
|
|
*
|
|
* @param Request $request
|
|
*
|
|
* @return Response
|
|
*/
|
|
public function variablesV2(Request $request): Response
|
|
{
|
|
/** @var Carbon $start */
|
|
$start = clone session('start', Carbon::now()->startOfMonth());
|
|
/** @var Carbon $end */
|
|
$end = clone session('end', Carbon::now()->endOfMonth());
|
|
|
|
$data = [
|
|
'start' => $start->format('Y-m-d'),
|
|
'end' => $end->format('Y-m-d'),
|
|
];
|
|
|
|
return response()
|
|
->view('javascript.variables', $data)
|
|
->header('Content-Type', 'text/javascript');
|
|
}
|
|
|
|
/**
|
|
* Show some common variables to be used in scripts.
|
|
*
|
|
* @param Request $request
|
|
* @param AccountRepositoryInterface $repository
|
|
* @param CurrencyRepositoryInterface $currencyRepository
|
|
*
|
|
* @return Response
|
|
*/
|
|
public function variables(Request $request, AccountRepositoryInterface $repository, CurrencyRepositoryInterface $currencyRepository): Response
|
|
{
|
|
$account = $repository->findNull((int) $request->get('account'));
|
|
$currencyId = 0;
|
|
if (null !== $account) {
|
|
// TODO we can use getAccountCurrency() instead
|
|
$currencyId = (int) $repository->getMetaValue($account, 'currency_id');
|
|
}
|
|
/** @var TransactionCurrency $currency */
|
|
$currency = $currencyRepository->findNull($currencyId);
|
|
if (null === $currency) {
|
|
/** @var TransactionCurrency $currency */
|
|
$currency = app('amount')->getDefaultCurrency();
|
|
}
|
|
|
|
$accountingLocaleInfo = app('amount')->getAccountingLocaleInfo();
|
|
$accountingLocaleInfo['frac_digits'] = $currency->decimal_places;
|
|
$pref = app('preferences')->get('language', config('firefly.default_language', 'en_US'));
|
|
/** @noinspection NullPointerExceptionInspection */
|
|
$lang = $pref->data;
|
|
$dateRange = $this->getDateRangeConfig();
|
|
$uid = substr(hash('sha256', sprintf('%s-%s-%s', (string) config('app.key'), auth()->user()->id, auth()->user()->email)), 0, 12);
|
|
|
|
$data = [
|
|
'currencyCode' => $currency->code,
|
|
'currencySymbol' => $currency->symbol,
|
|
'accountingLocaleInfo' => $accountingLocaleInfo,
|
|
'language' => $lang,
|
|
'dateRangeTitle' => $dateRange['title'],
|
|
'dateRangeConfig' => $dateRange['configuration'],
|
|
'uid' => $uid,
|
|
];
|
|
$request->session()->keep(['two-factor-secret']);
|
|
|
|
return response()
|
|
->view('javascript.variables', $data)
|
|
->header('Content-Type', 'text/javascript');
|
|
}
|
|
|
|
}
|