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

434 lines
16 KiB
PHP
Raw Normal View History

2016-05-20 01:57:45 -05:00
<?php
2022-12-29 12:41:57 -06:00
/**
* CurrencyController.php
2020-01-31 00:32:04 -06: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 01:40:00 -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
* GNU Affero General Public License for more details.
2017-10-21 01:40:00 -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/>.
*/
declare(strict_types=1);
2016-05-20 01:57:45 -05:00
namespace FireflyIII\Http\Controllers;
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;
use Illuminate\Contracts\View\Factory;
2022-01-02 01:01:59 -06:00
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
2017-02-17 13:14:22 -06:00
use Illuminate\Http\Request;
2017-12-28 02:53:21 -06:00
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Routing\Redirector;
2023-04-01 00:04:42 -05:00
use Illuminate\Support\Facades\Log;
2023-05-29 06:56:55 -05:00
use Illuminate\View\View;
2022-12-29 12:41:57 -06:00
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
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
{
2021-05-12 22:52:06 -05:00
protected CurrencyRepositoryInterface $repository;
protected UserRepositoryInterface $userRepository;
2015-02-25 08:57:43 -06:00
/**
2018-07-22 01:10:16 -05:00
* CurrencyController constructor.
*
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) {
2022-12-29 12:41:57 -06: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
}
2015-02-25 08:57:43 -06:00
/**
2018-07-22 01:10:16 -05:00
* Create a currency.
*
2023-06-21 05:34:58 -05:00
* @param Request $request
2017-02-24 22:57:01 -06:00
*
2021-05-24 01:50:17 -05:00
* @return Factory|RedirectResponse|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')) {
2022-12-29 12:41:57 -06: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';
2022-12-29 12:41:57 -06: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')) {
2022-04-12 11:19:30 -05:00
$this->rememberPreviousUrl('currencies.create.url');
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.');
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.
*
2023-06-21 05:34:58 -05:00
* @param Request $request
2015-02-25 08:57:43 -06:00
*
* @return RedirectResponse|Redirector
2021-09-18 03:21:29 -05:00
* @throws FireflyException
2015-02-25 08:57:43 -06:00
*/
public function defaultCurrency(Request $request)
2015-02-25 08:57:43 -06:00
{
2022-12-29 12:41:57 -06: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-12-29 12:41:57 -06:00
$request->session()->flash('success', (string)trans('firefly.new_default_currency', ['name' => $currency->name]));
return redirect(route('currencies.index'));
}
}
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.
*
2023-06-21 05:34:58 -05:00
* @param Request $request
* @param TransactionCurrency $currency
2015-02-25 08:57:43 -06:00
*
2021-05-24 01:50:17 -05:00
* @return Factory|RedirectResponse|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')) {
2022-12-29 12:41:57 -06: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'));
}
if ($this->repository->currencyInUse($currency)) {
2019-09-13 10:17:57 -05:00
$location = $this->repository->currencyInUseAt($currency);
2022-12-29 12:41:57 -06:00
$message = (string)trans(sprintf('firefly.cannot_disable_currency_%s', $location), ['name' => e($currency->name)]);
2019-09-13 10:17:57 -05:00
$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
2022-04-12 11:19:30 -05:00
$this->rememberPreviousUrl('currencies.delete.url');
2022-12-29 12:41:57 -06: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
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.
*
2023-06-21 05:34:58 -05:00
* @param Request $request
* @param TransactionCurrency $currency
2015-02-25 08:57:43 -06:00
*
* @return RedirectResponse|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')) {
2022-12-29 12:41:57 -06: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'));
}
if ($this->repository->currencyInUse($currency)) {
2022-12-29 12:41:57 -06: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
}
2020-02-13 13:09:27 -06:00
if ($this->repository->isFallbackCurrency($currency)) {
2022-12-29 12:41:57 -06:00
$request->session()->flash('error', (string)trans('firefly.cannot_delete_fallback_currency', ['name' => e($currency->name)]));
2020-02-13 13:09:27 -06:00
Log::channel('audit')->info(sprintf('Tried to delete currency %s but is FALLBACK.', $currency->code));
return redirect(route('currencies.index'));
}
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
2022-12-29 12:41:57 -06:00
$request->session()->flash('success', (string)trans('firefly.deleted_currency', ['name' => $currency->name]));
2015-02-25 08:57:43 -06:00
2022-04-12 11:19:30 -05:00
return redirect($this->getPreviousUrl('currencies.delete.url'));
2015-02-25 08:57:43 -06:00
}
/**
2023-06-21 05:34:58 -05:00
* @param Request $request
2022-03-29 08:10:05 -05:00
* @return JsonResponse
* @throws FireflyException
*/
2022-01-02 01:01:59 -06:00
public function disableCurrency(Request $request): JsonResponse
{
2022-12-29 12:41:57 -06:00
$currencyId = (int)$request->get('id');
2022-03-29 23:54:59 -05:00
$currency = $this->repository->find($currencyId);
2022-03-29 23:54:59 -05: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')) {
2022-12-29 12:41:57 -06:00
$request->session()->flash('error', (string)trans('firefly.ask_site_owner', ['owner' => e(config('firefly.site_owner'))]));
2022-03-29 23:54:59 -05:00
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);
2022-12-29 12:41:57 -06:00
$message = (string)trans(sprintf('firefly.cannot_disable_currency_%s', $location), ['name' => e($currency->name)]);
2022-03-29 23:54:59 -05:00
$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-19 23:39:10 -05:00
}
2022-03-29 23:54:59 -05: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) {
2022-12-29 12:41:57 -06:00
session()->flash('warning', (string)trans('firefly.disable_EUR_side_effects'));
2022-03-29 23:54:59 -05:00
}
2022-12-29 12:41:57 -06:00
session()->flash('success', (string)trans('firefly.currency_is_now_disabled', ['name' => $currency->name]));
return response()->json([]);
}
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.
*
2023-06-21 05:34:58 -05:00
* @param Request $request
* @param TransactionCurrency $currency
2015-02-25 08:57:43 -06:00
*
2021-05-24 01:50:17 -05:00
* @return Factory|RedirectResponse|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')) {
2022-12-29 12:41:57 -06: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'));
}
2015-02-25 08:57:43 -06:00
$subTitleIcon = 'fa-pencil';
2022-12-29 12:41:57 -06:00
$subTitle = (string)trans('breadcrumbs.edit_currency', ['name' => $currency->name]);
2015-02-25 08:57:43 -06:00
$currency->symbol = htmlentities($currency->symbol);
// code to handle active-checkboxes
$hasOldInput = null !== $request->old('_token');
$preFilled = [
2022-12-29 12:41:57 -06: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 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')) {
2022-04-12 11:19:30 -05:00
$this->rememberPreviousUrl('currencies.edit.url');
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
return view('currencies.edit', compact('currency', 'subTitle', 'subTitleIcon'));
2015-02-25 08:57:43 -06:00
}
/**
2023-06-21 05:34:58 -05:00
* @param Request $request
2022-12-30 23:57:05 -06:00
* @return JsonResponse
*/
public function enableCurrency(Request $request): JsonResponse
{
2022-12-29 12:41:57 -06:00
$currencyId = (int)$request->get('id');
2021-09-19 23:39:10 -05:00
if ($currencyId > 0) {
// valid currency?
$currency = $this->repository->find($currencyId);
if (null !== $currency) {
app('preferences')->mark();
2021-09-19 23:39:10 -05:00
$this->repository->enable($currency);
2022-12-29 12:41:57 -06:00
session()->flash('success', (string)trans('firefly.currency_is_now_enabled', ['name' => $currency->name]));
2021-09-19 23:39:10 -05:00
Log::channel('audit')->info(sprintf('Enabled currency %s.', $currency->code));
}
}
return response()->json([]);
}
2015-02-25 08:57:43 -06:00
/**
2018-07-22 01:10:16 -05:00
* Show overview of currencies.
*
2023-06-21 05:34:58 -05:00
* @param Request $request
2015-05-03 05:58:55 -05:00
*
2021-05-24 01:50:17 -05:00
* @return Factory|View
2022-12-29 12:41:57 -06:00
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
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();
2022-12-29 12:41:57 -06: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 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'));
$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')) {
2022-12-29 12:41:57 -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;
}
return view('currencies.index', compact('currencies', 'defaultCurrency', 'isOwner'));
2015-02-25 08:57:43 -06:00
}
2015-02-25 08:57:43 -06:00
/**
2018-07-22 01:10:16 -05:00
* Store new currency.
*
2023-06-21 05:34:58 -05:00
* @param CurrencyFormRequest $request
2015-02-25 08:57:43 -06:00
*
* @return $this|RedirectResponse|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')) {
2023-06-21 05:34: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
2022-04-12 11:19:30 -05:00
return redirect($this->getPreviousUrl('currencies.create.url'));
}
2015-02-25 08:57:43 -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);
2022-12-29 12:41:57 -06:00
$request->session()->flash('error', (string)trans('firefly.could_not_store_currency'));
2020-01-05 12:29:28 -06:00
$currency = null;
}
2022-04-12 11:19:30 -05:00
$redirect = redirect($this->getPreviousUrl('currencies.create.url'));
2019-02-08 00:13:59 -06:00
2018-07-09 12:24:08 -05:00
if (null !== $currency) {
2022-12-29 12:41:57 -06: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);
2022-12-29 12:41:57 -06:00
if (1 === (int)$request->get('create_another')) {
2018-07-09 12:24:08 -05:00
$request->session()->put('currencies.create.fromStore', true);
$redirect = redirect(route('currencies.create'))->withInput();
}
}
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
}
2015-02-25 08:57:43 -06:00
/**
2018-07-22 01:10:16 -05:00
* Updates a currency.
*
2023-06-21 05:34:58 -05:00
* @param CurrencyFormRequest $request
* @param TransactionCurrency $currency
2015-02-25 08:57:43 -06:00
*
* @return RedirectResponse|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')) {
2022-12-29 12:41:57 -06: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'));
}
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);
2022-12-29 12:41:57 -06: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
2022-12-29 12:41:57 -06:00
if (1 === (int)$request->get('return_to_edit')) {
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]));
2015-03-02 13:05:28 -06:00
}
2015-02-25 08:57:43 -06:00
2022-04-12 11:19:30 -05:00
return redirect($this->getPreviousUrl('currencies.edit.url'));
2015-02-25 08:57:43 -06:00
}
}