2017-01-10 11:25:03 -06:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* JavascriptController.php
|
2020-01-31 00:32:04 -06:00
|
|
|
* Copyright (c) 2019 james@firefly-iii.org
|
2017-01-10 11:25:03 -06:00
|
|
|
*
|
2019-10-01 23:37:26 -05:00
|
|
|
* This file is part of Firefly III (https://github.com/firefly-iii).
|
2017-10-21 01:40:00 -05:00
|
|
|
*
|
2019-10-01 23:37:26 -05:00
|
|
|
* 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.
|
2017-10-21 01:40:00 -05:00
|
|
|
*
|
2019-10-01 23:37:26 -05:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2017-10-21 01:40:00 -05:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2019-10-01 23:37:26 -05:00
|
|
|
* GNU Affero General Public License for more details.
|
2017-10-21 01:40:00 -05:00
|
|
|
*
|
2019-10-01 23:37:26 -05:00
|
|
|
* 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/>.
|
2017-01-10 11:25:03 -06:00
|
|
|
*/
|
2017-04-09 00:44:22 -05:00
|
|
|
declare(strict_types=1);
|
2017-01-10 11:25:03 -06:00
|
|
|
|
|
|
|
namespace FireflyIII\Http\Controllers;
|
|
|
|
|
2017-03-03 11:19:25 -06:00
|
|
|
use FireflyIII\Models\Account;
|
|
|
|
use FireflyIII\Models\AccountType;
|
2017-04-14 03:16:52 -05:00
|
|
|
use FireflyIII\Models\TransactionCurrency;
|
2017-03-03 11:19:25 -06:00
|
|
|
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
|
|
|
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
|
2018-08-09 10:50:30 -05:00
|
|
|
use FireflyIII\Support\Http\Controllers\GetConfigurationData;
|
2017-02-16 14:01:22 -06:00
|
|
|
use Illuminate\Http\Request;
|
2018-06-01 15:04:52 -05:00
|
|
|
use Illuminate\Http\Response;
|
2017-01-10 11:25:03 -06:00
|
|
|
|
|
|
|
/**
|
2017-11-15 05:25:49 -06:00
|
|
|
* Class JavascriptController.
|
2017-01-10 11:25:03 -06:00
|
|
|
*/
|
|
|
|
class JavascriptController extends Controller
|
|
|
|
{
|
2018-08-09 10:50:30 -05:00
|
|
|
use GetConfigurationData;
|
|
|
|
|
2017-03-03 11:19:25 -06:00
|
|
|
/**
|
2018-07-22 01:10:16 -05:00
|
|
|
* Show info about accounts.
|
|
|
|
*
|
2017-04-09 00:56:46 -05:00
|
|
|
* @param AccountRepositoryInterface $repository
|
|
|
|
* @param CurrencyRepositoryInterface $currencyRepository
|
2017-03-03 11:19:25 -06:00
|
|
|
*
|
2020-03-17 09:01:00 -05:00
|
|
|
* @return Response
|
2017-03-03 11:19:25 -06:00
|
|
|
*/
|
2018-07-08 05:28:42 -05:00
|
|
|
public function accounts(AccountRepositoryInterface $repository, CurrencyRepositoryInterface $currencyRepository): Response
|
2017-03-03 11:19:25 -06:00
|
|
|
{
|
2018-10-28 13:17:33 -05:00
|
|
|
$accounts = $repository->getAccountsByType(
|
|
|
|
[AccountType::DEFAULT, AccountType::ASSET, AccountType::DEBT, AccountType::LOAN, AccountType::MORTGAGE, AccountType::CREDITCARD]
|
|
|
|
);
|
2018-07-14 09:08:34 -05:00
|
|
|
$preference = app('preferences')->get('currencyPreference', config('firefly.default_currency', 'EUR'));
|
2018-07-09 12:24:08 -05:00
|
|
|
/** @noinspection NullPointerExceptionInspection */
|
|
|
|
$default = $currencyRepository->findByCodeNull($preference->data);
|
2017-03-03 11:19:25 -06:00
|
|
|
|
2017-11-15 05:25:49 -06:00
|
|
|
$data = ['accounts' => []];
|
2017-03-03 11:19:25 -06:00
|
|
|
|
|
|
|
/** @var Account $account */
|
|
|
|
foreach ($accounts as $account) {
|
2018-07-09 12:24:08 -05:00
|
|
|
$accountId = $account->id;
|
2020-03-17 09:01:00 -05:00
|
|
|
$currency = (int) $repository->getMetaValue($account, 'currency_id');
|
2018-07-09 12:24:08 -05:00
|
|
|
/** @noinspection NullPointerExceptionInspection */
|
2017-11-15 05:25:49 -06:00
|
|
|
$currency = 0 === $currency ? $default->id : $currency;
|
2017-04-14 03:16:52 -05:00
|
|
|
$entry = ['preferredCurrency' => $currency, 'name' => $account->name];
|
2017-03-03 11:19:25 -06:00
|
|
|
$data['accounts'][$accountId] = $entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
return response()
|
2019-02-13 10:38:41 -06:00
|
|
|
->view('javascript.accounts', $data)
|
2017-03-03 11:19:25 -06:00
|
|
|
->header('Content-Type', 'text/javascript');
|
|
|
|
}
|
|
|
|
|
2017-04-14 03:16:52 -05:00
|
|
|
/**
|
2018-07-22 01:10:16 -05:00
|
|
|
* Get info about currencies.
|
|
|
|
*
|
2017-04-14 03:16:52 -05:00
|
|
|
* @param CurrencyRepositoryInterface $repository
|
|
|
|
*
|
2018-06-01 15:04:52 -05:00
|
|
|
* @return Response
|
2017-04-14 03:16:52 -05:00
|
|
|
*/
|
2018-06-01 15:04:52 -05:00
|
|
|
public function currencies(CurrencyRepositoryInterface $repository): Response
|
2017-04-14 03:16:52 -05:00
|
|
|
{
|
|
|
|
$currencies = $repository->get();
|
2017-11-15 05:25:49 -06:00
|
|
|
$data = ['currencies' => []];
|
2017-04-14 03:16:52 -05:00
|
|
|
/** @var TransactionCurrency $currency */
|
|
|
|
foreach ($currencies as $currency) {
|
2017-06-05 04:12:50 -05:00
|
|
|
$currencyId = $currency->id;
|
|
|
|
$entry = ['name' => $currency->name, 'code' => $currency->code, 'symbol' => $currency->symbol];
|
2017-04-14 03:16:52 -05:00
|
|
|
$data['currencies'][$currencyId] = $entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
return response()
|
2019-02-13 10:38:41 -06:00
|
|
|
->view('javascript.currencies', $data)
|
2017-04-14 03:16:52 -05:00
|
|
|
->header('Content-Type', 'text/javascript');
|
|
|
|
}
|
|
|
|
|
2017-01-10 11:25:03 -06:00
|
|
|
/**
|
2018-07-22 01:10:16 -05:00
|
|
|
* Show some common variables to be used in scripts.
|
|
|
|
*
|
2017-12-17 07:30:53 -06:00
|
|
|
* @param Request $request
|
|
|
|
* @param AccountRepositoryInterface $repository
|
|
|
|
* @param CurrencyRepositoryInterface $currencyRepository
|
2017-01-10 11:25:03 -06:00
|
|
|
*
|
2020-03-17 09:01:00 -05:00
|
|
|
* @return Response
|
2017-01-10 11:25:03 -06:00
|
|
|
*/
|
2018-07-08 05:28:42 -05:00
|
|
|
public function variables(Request $request, AccountRepositoryInterface $repository, CurrencyRepositoryInterface $currencyRepository): Response
|
2017-01-10 11:25:03 -06:00
|
|
|
{
|
2020-03-17 09:01:00 -05:00
|
|
|
$account = $repository->findNull((int) $request->get('account'));
|
2017-11-17 23:02:15 -06:00
|
|
|
$currencyId = 0;
|
|
|
|
if (null !== $account) {
|
2019-08-15 23:20:07 -05:00
|
|
|
// TODO we can use getAccountCurrency() instead
|
2020-03-17 09:01:00 -05:00
|
|
|
$currencyId = (int) $repository->getMetaValue($account, 'currency_id');
|
2017-11-17 23:02:15 -06:00
|
|
|
}
|
|
|
|
/** @var TransactionCurrency $currency */
|
2018-02-28 13:23:45 -06:00
|
|
|
$currency = $currencyRepository->findNull($currencyId);
|
2018-06-01 15:04:52 -05:00
|
|
|
if (null === $currency) {
|
|
|
|
/** @var TransactionCurrency $currency */
|
2017-11-17 23:02:15 -06:00
|
|
|
$currency = app('amount')->getDefaultCurrency();
|
|
|
|
}
|
|
|
|
|
2020-01-31 23:32:28 -06:00
|
|
|
$localeconv = app('amount')->getLocaleInfo();
|
2017-10-05 04:49:06 -05:00
|
|
|
$accounting = app('amount')->getJsConfig($localeconv);
|
2017-11-17 23:02:15 -06:00
|
|
|
$localeconv['frac_digits'] = $currency->decimal_places;
|
2018-07-14 09:08:34 -05:00
|
|
|
$pref = app('preferences')->get('language', config('firefly.default_language', 'en_US'));
|
2018-07-09 12:24:08 -05:00
|
|
|
/** @noinspection NullPointerExceptionInspection */
|
|
|
|
$lang = $pref->data;
|
|
|
|
$dateRange = $this->getDateRangeConfig();
|
2020-04-10 23:42:21 -05:00
|
|
|
$uid = substr(hash('sha256', sprintf('%s-%s-%s', (string) config('app.key'), auth()->user()->id, auth()->user()->email)), 0, 12);
|
2017-08-10 13:41:33 -05:00
|
|
|
|
|
|
|
$data = [
|
2017-11-17 23:02:15 -06:00
|
|
|
'currencyCode' => $currency->code,
|
|
|
|
'currencySymbol' => $currency->symbol,
|
2017-08-10 13:41:33 -05:00
|
|
|
'accounting' => $accounting,
|
|
|
|
'localeconv' => $localeconv,
|
|
|
|
'language' => $lang,
|
|
|
|
'dateRangeTitle' => $dateRange['title'],
|
|
|
|
'dateRangeConfig' => $dateRange['configuration'],
|
2018-10-28 13:17:33 -05:00
|
|
|
'uid' => $uid,
|
2017-01-10 11:35:00 -06:00
|
|
|
];
|
2017-02-16 14:01:22 -06:00
|
|
|
$request->session()->keep(['two-factor-secret']);
|
2017-01-10 11:25:03 -06:00
|
|
|
|
2017-01-10 11:35:00 -06:00
|
|
|
return response()
|
2019-02-13 10:38:41 -06:00
|
|
|
->view('javascript.variables', $data)
|
2017-01-10 11:35:00 -06:00
|
|
|
->header('Content-Type', 'text/javascript');
|
|
|
|
}
|
2017-08-10 13:41:33 -05:00
|
|
|
|
2017-02-05 12:51:58 -06:00
|
|
|
}
|