Files
firefly-iii/app/Http/Controllers/CurrencyController.php

449 lines
16 KiB
PHP
Raw Normal View History

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