2016-05-20 01:57:45 -05:00
|
|
|
<?php
|
2016-05-20 05:27:31 -05:00
|
|
|
/**
|
|
|
|
* CurrencyController.php
|
2020-01-31 00:32:04 -06:00
|
|
|
* Copyright (c) 2019 james@firefly-iii.org
|
2016-05-20 05:27:31 -05:00
|
|
|
*
|
2019-10-01 23:37:26 -05:00
|
|
|
* This file is part of Firefly III (https://github.com/firefly-iii).
|
2016-10-04 23:52:15 -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/>.
|
2016-05-20 05:27:31 -05:00
|
|
|
*/
|
2017-04-09 00:44:22 -05:00
|
|
|
declare(strict_types=1);
|
2016-05-20 01:57:45 -05:00
|
|
|
|
|
|
|
namespace FireflyIII\Http\Controllers;
|
2015-02-25 08:57:43 -06:00
|
|
|
|
2018-07-25 23:10:17 -05:00
|
|
|
|
2018-11-10 03:04:46 -06:00
|
|
|
use FireflyIII\Exceptions\FireflyException;
|
2015-02-25 08:57:43 -06:00
|
|
|
use FireflyIII\Http\Requests\CurrencyFormRequest;
|
|
|
|
use FireflyIII\Models\TransactionCurrency;
|
2015-04-05 13:47:19 -05:00
|
|
|
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
|
2017-03-19 11:54:21 -05:00
|
|
|
use FireflyIII\Repositories\User\UserRepositoryInterface;
|
2018-07-09 12:24:08 -05:00
|
|
|
use FireflyIII\User;
|
2017-02-17 13:14:22 -06:00
|
|
|
use Illuminate\Http\Request;
|
2017-12-28 02:53:21 -06:00
|
|
|
use Illuminate\Pagination\LengthAwarePaginator;
|
2016-01-24 08:58:16 -06:00
|
|
|
use Log;
|
2015-04-05 13:47:19 -05:00
|
|
|
use View;
|
2015-02-25 08:57:43 -06:00
|
|
|
|
|
|
|
/**
|
2017-11-15 05:25:49 -06:00
|
|
|
* Class CurrencyController.
|
2015-02-25 08:57:43 -06:00
|
|
|
*/
|
|
|
|
class CurrencyController extends Controller
|
|
|
|
{
|
2018-07-20 13:53:48 -05:00
|
|
|
/** @var CurrencyRepositoryInterface The currency repository */
|
2017-03-19 11:54:21 -05:00
|
|
|
protected $repository;
|
|
|
|
|
2018-07-22 01:10:16 -05:00
|
|
|
/** @var UserRepositoryInterface The user repository */
|
2017-03-19 11:54:21 -05:00
|
|
|
protected $userRepository;
|
2015-02-25 08:57:43 -06:00
|
|
|
|
|
|
|
/**
|
2018-07-22 01:10:16 -05:00
|
|
|
* CurrencyController constructor.
|
2019-07-21 10:15:06 -05:00
|
|
|
* @codeCoverageIgnore
|
2015-02-25 08:57:43 -06:00
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
2015-04-28 08:26:30 -05:00
|
|
|
parent::__construct();
|
2016-10-29 00:44:46 -05:00
|
|
|
|
|
|
|
$this->middleware(
|
|
|
|
function ($request, $next) {
|
2018-07-15 02:38:49 -05:00
|
|
|
app('view')->share('title', (string)trans('firefly.currencies'));
|
2017-12-16 12:46:36 -06:00
|
|
|
app('view')->share('mainTitleIcon', 'fa-usd');
|
2017-03-19 11:54:21 -05:00
|
|
|
$this->repository = app(CurrencyRepositoryInterface::class);
|
|
|
|
$this->userRepository = app(UserRepositoryInterface::class);
|
2016-10-29 00:44:46 -05:00
|
|
|
|
|
|
|
return $next($request);
|
|
|
|
}
|
|
|
|
);
|
2015-02-25 08:57:43 -06:00
|
|
|
}
|
|
|
|
|
2018-07-08 05:08:53 -05:00
|
|
|
|
2015-02-25 08:57:43 -06:00
|
|
|
/**
|
2018-07-22 01:10:16 -05:00
|
|
|
* Create a currency.
|
|
|
|
*
|
2017-02-24 22:57:01 -06:00
|
|
|
* @param Request $request
|
|
|
|
*
|
2017-03-19 11:54:21 -05:00
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|View
|
2015-02-25 08:57:43 -06:00
|
|
|
*/
|
2017-02-17 13:14:22 -06:00
|
|
|
public function create(Request $request)
|
2015-02-25 08:57:43 -06:00
|
|
|
{
|
2018-07-09 12:24:08 -05:00
|
|
|
/** @var User $user */
|
|
|
|
$user = auth()->user();
|
|
|
|
if (!$this->userRepository->hasRole($user, 'owner')) {
|
2019-08-02 22:08:35 -05:00
|
|
|
$request->session()->flash('error', (string)trans('firefly.ask_site_owner', ['owner' => e(config('firefly.site_owner'))]));
|
2017-03-19 11:54:21 -05:00
|
|
|
|
|
|
|
return redirect(route('currencies.index'));
|
|
|
|
}
|
|
|
|
|
2015-02-25 08:57:43 -06:00
|
|
|
$subTitleIcon = 'fa-plus';
|
2018-07-15 02:38:49 -05:00
|
|
|
$subTitle = (string)trans('firefly.create_currency');
|
2015-02-25 08:57:43 -06:00
|
|
|
|
2015-04-01 11:59:34 -05:00
|
|
|
// put previous url in session if not redirect from store (not "create another").
|
2017-11-15 05:25:49 -06:00
|
|
|
if (true !== session('currencies.create.fromStore')) {
|
2017-02-05 01:26:54 -06:00
|
|
|
$this->rememberPreviousUri('currencies.create.uri');
|
2015-04-01 11:59:34 -05:00
|
|
|
}
|
2017-02-17 13:14:22 -06:00
|
|
|
$request->session()->forget('currencies.create.fromStore');
|
2015-04-01 11:59:34 -05:00
|
|
|
|
2019-02-08 00:13:59 -06:00
|
|
|
Log::channel('audit')->info('Create new currency.');
|
|
|
|
|
2016-12-05 13:01:01 -06:00
|
|
|
return view('currencies.create', compact('subTitleIcon', 'subTitle'));
|
2015-02-25 08:57:43 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-07-22 01:10:16 -05:00
|
|
|
* Make currency the default currency.
|
|
|
|
*
|
2017-02-17 13:14:22 -06:00
|
|
|
* @param Request $request
|
2015-02-25 08:57:43 -06:00
|
|
|
* @param TransactionCurrency $currency
|
|
|
|
*
|
2017-02-17 13:14:22 -06:00
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
|
2015-02-25 08:57:43 -06:00
|
|
|
*/
|
2017-02-17 13:14:22 -06:00
|
|
|
public function defaultCurrency(Request $request, TransactionCurrency $currency)
|
2015-02-25 08:57:43 -06:00
|
|
|
{
|
2018-07-14 09:08:34 -05:00
|
|
|
app('preferences')->set('currencyPreference', $currency->code);
|
2018-07-08 05:08:53 -05:00
|
|
|
app('preferences')->mark();
|
2015-02-25 08:57:43 -06:00
|
|
|
|
2019-02-08 00:13:59 -06:00
|
|
|
Log::channel('audit')->info(sprintf('Make %s the default currency.', $currency->code));
|
|
|
|
|
2018-11-10 03:04:46 -06:00
|
|
|
$this->repository->enable($currency);
|
2018-07-15 02:38:49 -05:00
|
|
|
$request->session()->flash('success', (string)trans('firefly.new_default_currency', ['name' => $currency->name]));
|
2015-02-25 08:57:43 -06:00
|
|
|
|
2016-12-05 13:01:01 -06:00
|
|
|
return redirect(route('currencies.index'));
|
2015-02-25 08:57:43 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-07-22 01:10:16 -05:00
|
|
|
* Deletes a currency.
|
|
|
|
*
|
2017-03-19 11:54:21 -05:00
|
|
|
* @param Request $request
|
|
|
|
* @param TransactionCurrency $currency
|
2015-02-25 08:57:43 -06:00
|
|
|
*
|
2016-12-11 09:25:46 -06:00
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|View
|
2015-02-25 08:57:43 -06:00
|
|
|
*/
|
2017-03-19 11:54:21 -05:00
|
|
|
public function delete(Request $request, TransactionCurrency $currency)
|
2015-02-25 08:57:43 -06:00
|
|
|
{
|
2018-07-09 12:24:08 -05:00
|
|
|
/** @var User $user */
|
|
|
|
$user = auth()->user();
|
|
|
|
if (!$this->userRepository->hasRole($user, 'owner')) {
|
2017-03-19 11:54:21 -05:00
|
|
|
// @codeCoverageIgnoreStart
|
2019-08-02 22:08:35 -05:00
|
|
|
$request->session()->flash('error', (string)trans('firefly.ask_site_owner', ['owner' => e(config('firefly.site_owner'))]));
|
2019-02-08 00:13:59 -06:00
|
|
|
Log::channel('audit')->info(sprintf('Tried to visit page to delete currency %s but is not site owner.', $currency->code));
|
2017-03-19 11:54:21 -05:00
|
|
|
|
|
|
|
return redirect(route('currencies.index'));
|
|
|
|
// @codeCoverageIgnoreEnd
|
|
|
|
}
|
|
|
|
|
2018-11-10 03:04:46 -06:00
|
|
|
if ($this->repository->currencyInUse($currency)) {
|
2019-09-13 10:17:57 -05:00
|
|
|
$location = $this->repository->currencyInUseAt($currency);
|
|
|
|
$message = (string)trans(sprintf('firefly.cannot_disable_currency_%s', $location), ['name' => e($currency->name)]);
|
|
|
|
$request->session()->flash('error', $message);
|
2019-02-08 00:13:59 -06:00
|
|
|
Log::channel('audit')->info(sprintf('Tried to visit page to delete currency %s but currency is in use.', $currency->code));
|
2015-04-01 11:59:34 -05:00
|
|
|
|
2016-12-05 13:01:01 -06:00
|
|
|
return redirect(route('currencies.index'));
|
2015-02-25 08:57:43 -06:00
|
|
|
}
|
|
|
|
|
2015-04-05 13:47:19 -05:00
|
|
|
// put previous url in session
|
2017-02-05 01:26:54 -06:00
|
|
|
$this->rememberPreviousUri('currencies.delete.uri');
|
2018-07-15 02:38:49 -05:00
|
|
|
$subTitle = (string)trans('form.delete_currency', ['name' => $currency->name]);
|
2019-02-08 00:13:59 -06:00
|
|
|
Log::channel('audit')->info(sprintf('Visit page to delete currency %s.', $currency->code));
|
2015-04-05 13:47:19 -05:00
|
|
|
|
2016-12-05 13:01:01 -06:00
|
|
|
return view('currencies.delete', compact('currency', 'subTitle'));
|
2015-02-25 08:57:43 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-07-22 01:10:16 -05:00
|
|
|
* Destroys a currency.
|
|
|
|
*
|
2017-03-19 11:54:21 -05:00
|
|
|
* @param Request $request
|
|
|
|
* @param TransactionCurrency $currency
|
2015-02-25 08:57:43 -06:00
|
|
|
*
|
2016-12-11 09:25:46 -06:00
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
|
2015-02-25 08:57:43 -06:00
|
|
|
*/
|
2017-03-19 11:54:21 -05:00
|
|
|
public function destroy(Request $request, TransactionCurrency $currency)
|
2015-02-25 08:57:43 -06:00
|
|
|
{
|
2018-07-09 12:24:08 -05:00
|
|
|
/** @var User $user */
|
|
|
|
$user = auth()->user();
|
|
|
|
if (!$this->userRepository->hasRole($user, 'owner')) {
|
2017-03-19 11:54:21 -05:00
|
|
|
// @codeCoverageIgnoreStart
|
2019-08-02 22:08:35 -05:00
|
|
|
$request->session()->flash('error', (string)trans('firefly.ask_site_owner', ['owner' => e(config('firefly.site_owner'))]));
|
2019-02-08 00:13:59 -06:00
|
|
|
Log::channel('audit')->info(sprintf('Tried to delete currency %s but is not site owner.', $currency->code));
|
2017-03-19 11:54:21 -05:00
|
|
|
|
|
|
|
return redirect(route('currencies.index'));
|
|
|
|
// @codeCoverageIgnoreEnd
|
|
|
|
}
|
|
|
|
|
2018-11-10 03:04:46 -06:00
|
|
|
if ($this->repository->currencyInUse($currency)) {
|
2019-08-02 22:08:35 -05:00
|
|
|
$request->session()->flash('error', (string)trans('firefly.cannot_delete_currency', ['name' => e($currency->name)]));
|
2019-02-08 00:13:59 -06:00
|
|
|
Log::channel('audit')->info(sprintf('Tried to delete currency %s but is in use.', $currency->code));
|
2015-02-25 08:57:43 -06:00
|
|
|
|
2016-12-05 13:01:01 -06:00
|
|
|
return redirect(route('currencies.index'));
|
2015-02-25 08:57:43 -06:00
|
|
|
}
|
2019-02-08 00:13:59 -06:00
|
|
|
Log::channel('audit')->info(sprintf('Deleted currency %s.', $currency->code));
|
2017-03-19 11:54:21 -05:00
|
|
|
$this->repository->destroy($currency);
|
2019-02-08 00:13:59 -06:00
|
|
|
|
2018-07-15 02:38:49 -05:00
|
|
|
$request->session()->flash('success', (string)trans('firefly.deleted_currency', ['name' => $currency->name]));
|
2015-02-25 08:57:43 -06:00
|
|
|
|
2017-02-05 01:26:54 -06:00
|
|
|
return redirect($this->getPreviousUri('currencies.delete.uri'));
|
2015-02-25 08:57:43 -06:00
|
|
|
}
|
|
|
|
|
2018-11-10 03:04:46 -06:00
|
|
|
/**
|
2019-02-13 10:38:41 -06:00
|
|
|
* @param Request $request
|
2018-11-10 03:04:46 -06:00
|
|
|
* @param TransactionCurrency $currency
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
|
|
|
|
* @throws FireflyException
|
|
|
|
*/
|
|
|
|
public function disableCurrency(Request $request, TransactionCurrency $currency)
|
|
|
|
{
|
|
|
|
app('preferences')->mark();
|
|
|
|
|
|
|
|
/** @var User $user */
|
|
|
|
$user = auth()->user();
|
|
|
|
if (!$this->userRepository->hasRole($user, 'owner')) {
|
|
|
|
// @codeCoverageIgnoreStart
|
2019-08-02 22:08:35 -05:00
|
|
|
$request->session()->flash('error', (string)trans('firefly.ask_site_owner', ['owner' => e(config('firefly.site_owner'))]));
|
2019-02-08 00:13:59 -06:00
|
|
|
Log::channel('audit')->info(sprintf('Tried to disable currency %s but is not site owner.', $currency->code));
|
2018-11-10 03:04:46 -06:00
|
|
|
|
|
|
|
return redirect(route('currencies.index'));
|
|
|
|
// @codeCoverageIgnoreEnd
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->repository->currencyInUse($currency)) {
|
2019-08-18 01:46:36 -05:00
|
|
|
|
|
|
|
$location = $this->repository->currencyInUseAt($currency);
|
|
|
|
$message = (string)trans(sprintf('firefly.cannot_disable_currency_%s', $location), ['name' => e($currency->name)]);
|
|
|
|
|
|
|
|
$request->session()->flash('error', $message);
|
2019-02-08 00:13:59 -06:00
|
|
|
Log::channel('audit')->info(sprintf('Tried to disable currency %s but is in use.', $currency->code));
|
2018-11-10 03:04:46 -06:00
|
|
|
|
|
|
|
return redirect(route('currencies.index'));
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->repository->disable($currency);
|
2019-02-08 00:13:59 -06:00
|
|
|
Log::channel('audit')->info(sprintf('Disabled currency %s.', $currency->code));
|
2018-11-10 03:04:46 -06:00
|
|
|
// if no currencies are enabled, enable the first one in the DB (usually the EUR)
|
|
|
|
if (0 === $this->repository->get()->count()) {
|
2019-02-08 00:13:59 -06:00
|
|
|
/** @var TransactionCurrency $first */
|
2018-11-10 03:04:46 -06:00
|
|
|
$first = $this->repository->getAll()->first();
|
|
|
|
if (null === $first) {
|
|
|
|
throw new FireflyException('No currencies found.');
|
|
|
|
}
|
2019-02-08 00:13:59 -06:00
|
|
|
Log::channel('audit')->info(sprintf('Auto-enabled currency %s.', $first->code));
|
2018-11-10 03:04:46 -06:00
|
|
|
$this->repository->enable($first);
|
2019-02-08 00:13:59 -06:00
|
|
|
app('preferences')->set('currencyPreference', $first->code);
|
2018-11-10 03:04:46 -06:00
|
|
|
app('preferences')->mark();
|
|
|
|
}
|
|
|
|
|
2019-08-18 01:46:36 -05:00
|
|
|
if ('EUR' === $currency->code) {
|
|
|
|
session()->flash('warning', (string)trans('firefly.disable_EUR_side_effects'));
|
|
|
|
}
|
|
|
|
|
2018-11-10 03:04:46 -06:00
|
|
|
session()->flash('success', (string)trans('firefly.currency_is_now_disabled', ['name' => $currency->name]));
|
|
|
|
|
|
|
|
return redirect(route('currencies.index'));
|
|
|
|
}
|
2018-07-08 05:08:53 -05:00
|
|
|
|
2015-02-25 08:57:43 -06:00
|
|
|
/**
|
2018-07-22 01:10:16 -05:00
|
|
|
* Edit a currency.
|
|
|
|
*
|
2017-02-17 13:14:22 -06:00
|
|
|
* @param Request $request
|
2015-02-25 08:57:43 -06:00
|
|
|
* @param TransactionCurrency $currency
|
|
|
|
*
|
2017-03-19 11:54:21 -05:00
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|View
|
2015-02-25 08:57:43 -06:00
|
|
|
*/
|
2017-02-17 13:14:22 -06:00
|
|
|
public function edit(Request $request, TransactionCurrency $currency)
|
2015-02-25 08:57:43 -06:00
|
|
|
{
|
2018-07-09 12:24:08 -05:00
|
|
|
/** @var User $user */
|
|
|
|
$user = auth()->user();
|
|
|
|
if (!$this->userRepository->hasRole($user, 'owner')) {
|
2017-03-19 11:54:21 -05:00
|
|
|
// @codeCoverageIgnoreStart
|
2019-08-02 22:08:35 -05:00
|
|
|
$request->session()->flash('error', (string)trans('firefly.ask_site_owner', ['owner' => e(config('firefly.site_owner'))]));
|
2019-02-08 00:13:59 -06:00
|
|
|
Log::channel('audit')->info(sprintf('Tried to edit currency %s but is not owner.', $currency->code));
|
2017-03-19 11:54:21 -05:00
|
|
|
|
|
|
|
return redirect(route('currencies.index'));
|
|
|
|
// @codeCoverageIgnoreEnd
|
|
|
|
}
|
|
|
|
|
2015-02-25 08:57:43 -06:00
|
|
|
$subTitleIcon = 'fa-pencil';
|
2018-07-15 02:38:49 -05:00
|
|
|
$subTitle = (string)trans('breadcrumbs.edit_currency', ['name' => $currency->name]);
|
2015-02-25 08:57:43 -06:00
|
|
|
$currency->symbol = htmlentities($currency->symbol);
|
|
|
|
|
2018-11-10 03:04:46 -06:00
|
|
|
// code to handle active-checkboxes
|
|
|
|
$hasOldInput = null !== $request->old('_token');
|
|
|
|
$preFilled = [
|
|
|
|
'enabled' => $hasOldInput ? (bool)$request->old('enabled') : $currency->enabled,
|
|
|
|
];
|
|
|
|
|
|
|
|
$request->session()->flash('preFilled', $preFilled);
|
2019-02-10 01:03:09 -06:00
|
|
|
Log::channel('audit')->info('Edit currency.', $currency->toArray());
|
2018-11-10 03:04:46 -06:00
|
|
|
|
2015-04-01 11:59:34 -05:00
|
|
|
// put previous url in session if not redirect from store (not "return_to_edit").
|
2017-11-15 05:25:49 -06:00
|
|
|
if (true !== session('currencies.edit.fromUpdate')) {
|
2017-02-05 01:26:54 -06:00
|
|
|
$this->rememberPreviousUri('currencies.edit.uri');
|
2015-04-01 11:59:34 -05:00
|
|
|
}
|
2017-02-17 13:14:22 -06:00
|
|
|
$request->session()->forget('currencies.edit.fromUpdate');
|
2015-04-01 11:59:34 -05:00
|
|
|
|
2016-12-05 13:01:01 -06:00
|
|
|
return view('currencies.edit', compact('currency', 'subTitle', 'subTitleIcon'));
|
2015-02-25 08:57:43 -06:00
|
|
|
}
|
|
|
|
|
2018-11-10 03:04:46 -06:00
|
|
|
/**
|
|
|
|
* @param TransactionCurrency $currency
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
|
|
|
|
*/
|
|
|
|
public function enableCurrency(TransactionCurrency $currency)
|
|
|
|
{
|
|
|
|
app('preferences')->mark();
|
|
|
|
|
|
|
|
$this->repository->enable($currency);
|
|
|
|
session()->flash('success', (string)trans('firefly.currency_is_now_enabled', ['name' => $currency->name]));
|
2019-02-08 00:13:59 -06:00
|
|
|
Log::channel('audit')->info(sprintf('Enabled currency %s.', $currency->code));
|
2018-11-10 03:04:46 -06:00
|
|
|
|
|
|
|
return redirect(route('currencies.index'));
|
|
|
|
}
|
|
|
|
|
2015-02-25 08:57:43 -06:00
|
|
|
/**
|
2018-07-22 01:10:16 -05:00
|
|
|
* Show overview of currencies.
|
|
|
|
*
|
2017-03-19 11:54:21 -05:00
|
|
|
* @param Request $request
|
2015-05-03 05:58:55 -05:00
|
|
|
*
|
2018-07-08 05:08:53 -05:00
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
2015-02-25 08:57:43 -06:00
|
|
|
*/
|
2017-03-19 11:54:21 -05:00
|
|
|
public function index(Request $request)
|
2015-02-25 08:57:43 -06:00
|
|
|
{
|
2018-07-09 12:24:08 -05:00
|
|
|
/** @var User $user */
|
2018-07-13 08:50:42 -05:00
|
|
|
$user = auth()->user();
|
2018-04-02 08:10:40 -05:00
|
|
|
$page = 0 === (int)$request->get('page') ? 1 : (int)$request->get('page');
|
2018-07-14 09:08:34 -05:00
|
|
|
$pageSize = (int)app('preferences')->get('listPageSize', 50)->data;
|
2018-11-10 03:04:46 -06:00
|
|
|
$collection = $this->repository->getAll();
|
2017-12-28 02:53:21 -06:00
|
|
|
$total = $collection->count();
|
|
|
|
$collection = $collection->slice(($page - 1) * $pageSize, $pageSize);
|
|
|
|
$currencies = new LengthAwarePaginator($collection, $total, $pageSize, $page);
|
|
|
|
$currencies->setPath(route('currencies.index'));
|
|
|
|
|
2018-07-14 09:08:34 -05:00
|
|
|
$defaultCurrency = $this->repository->getCurrencyByPreference(app('preferences')->get('currencyPreference', config('firefly.default_currency', 'EUR')));
|
2017-11-05 12:49:20 -06:00
|
|
|
$isOwner = true;
|
2018-07-09 12:24:08 -05:00
|
|
|
if (!$this->userRepository->hasRole($user, 'owner')) {
|
2018-12-15 00:59:02 -06:00
|
|
|
$request->session()->flash('info', (string)trans('firefly.ask_site_owner', ['owner' => config('firefly.site_owner')]));
|
2017-10-20 01:00:13 -05:00
|
|
|
$isOwner = false;
|
2015-05-27 23:43:07 -05:00
|
|
|
}
|
|
|
|
|
2017-11-05 12:49:20 -06:00
|
|
|
return view('currencies.index', compact('currencies', 'defaultCurrency', 'isOwner'));
|
2015-02-25 08:57:43 -06:00
|
|
|
}
|
|
|
|
|
2018-07-08 05:08:53 -05:00
|
|
|
|
2015-02-25 08:57:43 -06:00
|
|
|
/**
|
2018-07-22 01:10:16 -05:00
|
|
|
* Store new currency.
|
|
|
|
*
|
2017-03-19 11:54:21 -05:00
|
|
|
* @param CurrencyFormRequest $request
|
2015-02-25 08:57:43 -06:00
|
|
|
*
|
2017-03-19 11:54:21 -05:00
|
|
|
* @return $this|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
|
2015-02-25 08:57:43 -06:00
|
|
|
*/
|
2017-03-19 11:54:21 -05:00
|
|
|
public function store(CurrencyFormRequest $request)
|
2015-02-25 08:57:43 -06:00
|
|
|
{
|
2018-07-09 12:24:08 -05:00
|
|
|
/** @var User $user */
|
|
|
|
$user = auth()->user();
|
2019-02-08 00:13:59 -06:00
|
|
|
$data = $request->getCurrencyData();
|
2018-07-09 12:24:08 -05:00
|
|
|
if (!$this->userRepository->hasRole($user, 'owner')) {
|
2017-03-19 11:54:21 -05:00
|
|
|
// @codeCoverageIgnoreStart
|
2016-09-16 05:15:58 -05:00
|
|
|
Log::error('User ' . auth()->user()->id . ' is not admin, but tried to store a currency.');
|
2019-02-08 00:13:59 -06:00
|
|
|
Log::channel('audit')->info('Tried to create (POST) currency without admin rights.', $data);
|
2016-04-29 01:56:56 -05:00
|
|
|
|
2017-02-05 01:26:54 -06:00
|
|
|
return redirect($this->getPreviousUri('currencies.create.uri'));
|
2017-03-19 11:54:21 -05:00
|
|
|
// @codeCoverageIgnoreEnd
|
2015-05-27 23:43:07 -05:00
|
|
|
}
|
2015-02-25 08:57:43 -06:00
|
|
|
|
2018-11-10 03:04:46 -06:00
|
|
|
$data['enabled'] = true;
|
2020-01-05 12:29:28 -06:00
|
|
|
try {
|
|
|
|
$currency = $this->repository->store($data);
|
|
|
|
} catch (FireflyException $e) {
|
|
|
|
Log::error($e->getMessage());
|
|
|
|
Log::channel('audit')->info('Could not store (POST) currency without admin rights.', $data);
|
|
|
|
$request->session()->flash('error', (string)trans('firefly.could_not_store_currency'));
|
|
|
|
$currency = null;
|
|
|
|
}
|
2018-11-10 03:04:46 -06:00
|
|
|
$redirect = redirect($this->getPreviousUri('currencies.create.uri'));
|
2019-02-08 00:13:59 -06:00
|
|
|
|
2018-07-09 12:24:08 -05:00
|
|
|
if (null !== $currency) {
|
2018-07-15 02:38:49 -05:00
|
|
|
$request->session()->flash('success', (string)trans('firefly.created_currency', ['name' => $currency->name]));
|
2019-02-08 00:13:59 -06:00
|
|
|
Log::channel('audit')->info('Created (POST) currency.', $data);
|
2018-07-09 12:24:08 -05:00
|
|
|
if (1 === (int)$request->get('create_another')) {
|
|
|
|
// @codeCoverageIgnoreStart
|
|
|
|
$request->session()->put('currencies.create.fromStore', true);
|
|
|
|
|
|
|
|
$redirect = redirect(route('currencies.create'))->withInput();
|
|
|
|
// @codeCoverageIgnoreEnd
|
|
|
|
}
|
|
|
|
}
|
2015-03-02 13:05:28 -06:00
|
|
|
|
2018-07-09 12:24:08 -05:00
|
|
|
return $redirect;
|
2015-02-25 08:57:43 -06:00
|
|
|
}
|
|
|
|
|
2018-07-08 05:08:53 -05:00
|
|
|
|
2015-02-25 08:57:43 -06:00
|
|
|
/**
|
2018-07-22 01:10:16 -05:00
|
|
|
* Updates a currency.
|
|
|
|
*
|
2017-03-19 11:54:21 -05:00
|
|
|
* @param CurrencyFormRequest $request
|
|
|
|
* @param TransactionCurrency $currency
|
2015-02-25 08:57:43 -06:00
|
|
|
*
|
2017-03-19 11:54:21 -05:00
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
|
2015-02-25 08:57:43 -06:00
|
|
|
*/
|
2017-03-19 11:54:21 -05:00
|
|
|
public function update(CurrencyFormRequest $request, TransactionCurrency $currency)
|
2015-02-25 08:57:43 -06:00
|
|
|
{
|
2018-07-09 12:24:08 -05:00
|
|
|
/** @var User $user */
|
|
|
|
$user = auth()->user();
|
2019-02-08 00:13:59 -06:00
|
|
|
$data = $request->getCurrencyData();
|
2019-08-01 22:25:24 -05:00
|
|
|
|
|
|
|
if (false === $data['enabled'] && $this->repository->currencyInUse($currency)) {
|
|
|
|
$data['enabled'] = true;
|
|
|
|
}
|
2018-07-09 12:24:08 -05:00
|
|
|
if (!$this->userRepository->hasRole($user, 'owner')) {
|
2017-03-19 11:54:21 -05:00
|
|
|
// @codeCoverageIgnoreStart
|
2019-08-02 22:08:35 -05:00
|
|
|
$request->session()->flash('error', (string)trans('firefly.ask_site_owner', ['owner' => e(config('firefly.site_owner'))]));
|
2019-02-08 00:13:59 -06:00
|
|
|
Log::channel('audit')->info('Tried to update (POST) currency without admin rights.', $data);
|
2017-03-19 11:54:21 -05:00
|
|
|
|
|
|
|
return redirect(route('currencies.index'));
|
|
|
|
// @codeCoverageIgnoreEnd
|
2015-05-27 23:43:07 -05:00
|
|
|
}
|
2017-03-19 11:54:21 -05:00
|
|
|
|
2019-02-08 00:13:59 -06:00
|
|
|
|
2017-03-19 11:54:21 -05:00
|
|
|
$currency = $this->repository->update($currency, $data);
|
2019-02-08 00:13:59 -06:00
|
|
|
Log::channel('audit')->info('Updated (POST) currency.', $data);
|
2018-07-15 02:38:49 -05:00
|
|
|
$request->session()->flash('success', (string)trans('firefly.updated_currency', ['name' => $currency->name]));
|
2018-07-08 05:08:53 -05:00
|
|
|
app('preferences')->mark();
|
2015-03-02 13:05:28 -06:00
|
|
|
|
2018-04-02 08:10:40 -05:00
|
|
|
if (1 === (int)$request->get('return_to_edit')) {
|
2017-03-19 11:54:21 -05:00
|
|
|
// @codeCoverageIgnoreStart
|
2017-02-17 13:14:22 -06:00
|
|
|
$request->session()->put('currencies.edit.fromUpdate', true);
|
2015-04-05 13:47:19 -05:00
|
|
|
|
2016-12-05 13:01:01 -06:00
|
|
|
return redirect(route('currencies.edit', [$currency->id]));
|
2017-03-19 11:54:21 -05:00
|
|
|
// @codeCoverageIgnoreEnd
|
2015-03-02 13:05:28 -06:00
|
|
|
}
|
2015-02-25 08:57:43 -06:00
|
|
|
|
2017-02-05 01:26:54 -06:00
|
|
|
return redirect($this->getPreviousUri('currencies.edit.uri'));
|
2015-02-25 08:57:43 -06:00
|
|
|
}
|
|
|
|
}
|