2017-01-10 11:25:03 -06:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* JavascriptController.php
|
|
|
|
* Copyright (c) 2017 thegrumpydictator@gmail.com
|
|
|
|
*
|
2017-10-21 01:40:00 -05:00
|
|
|
* This file is part of Firefly III.
|
|
|
|
*
|
|
|
|
* Firefly III is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Firefly III 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 General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2017-12-17 07:41:58 -06:00
|
|
|
* along with Firefly III. If not, see <http://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
|
|
|
*
|
2017-06-05 04:12:50 -05:00
|
|
|
* @return \Illuminate\Http\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
|
|
|
{
|
|
|
|
$accounts = $repository->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET]);
|
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;
|
|
|
|
$currency = (int)$repository->getMetaValue($account, 'currency_id');
|
|
|
|
/** @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()
|
|
|
|
->view('javascript.accounts', $data, 200)
|
|
|
|
->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()
|
|
|
|
->view('javascript.currencies', $data, 200)
|
|
|
|
->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
|
|
|
*
|
2017-02-25 10:39:50 -06:00
|
|
|
* @return \Illuminate\Http\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
|
|
|
{
|
2018-04-02 08:10:40 -05:00
|
|
|
$account = $repository->findNull((int)$request->get('account'));
|
2017-11-17 23:02:15 -06:00
|
|
|
$currencyId = 0;
|
|
|
|
if (null !== $account) {
|
2018-04-02 08:10:40 -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();
|
|
|
|
}
|
|
|
|
|
2017-01-10 11:35:00 -06:00
|
|
|
$localeconv = localeconv();
|
2017-10-05 04:49:06 -05:00
|
|
|
$accounting = app('amount')->getJsConfig($localeconv);
|
2017-01-10 11:35:00 -06:00
|
|
|
$localeconv = 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();
|
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'],
|
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()
|
|
|
|
->view('javascript.variables', $data, 200)
|
|
|
|
->header('Content-Type', 'text/javascript');
|
|
|
|
}
|
2017-08-10 13:41:33 -05:00
|
|
|
|
2017-02-05 12:51:58 -06:00
|
|
|
}
|