2016-05-20 08:57:45 +02:00
|
|
|
<?php
|
2016-05-20 12:27:31 +02:00
|
|
|
/**
|
|
|
|
* CurrencyController.php
|
|
|
|
* Copyright (C) 2016 thegrumpydictator@gmail.com
|
|
|
|
*
|
2016-10-05 06:52:15 +02:00
|
|
|
* This software may be modified and distributed under the terms of the
|
|
|
|
* Creative Commons Attribution-ShareAlike 4.0 International License.
|
|
|
|
*
|
|
|
|
* See the LICENSE file for details.
|
2016-05-20 12:27:31 +02:00
|
|
|
*/
|
|
|
|
|
2016-05-20 08:57:45 +02:00
|
|
|
declare(strict_types = 1);
|
|
|
|
|
|
|
|
namespace FireflyIII\Http\Controllers;
|
2015-02-25 15:57:43 +01:00
|
|
|
|
2015-03-10 17:26:31 +01:00
|
|
|
use Cache;
|
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;
|
2015-03-10 17:26:31 +01:00
|
|
|
use Input;
|
2016-01-24 15:58:16 +01:00
|
|
|
use Log;
|
2015-02-25 15:57:43 +01:00
|
|
|
use Preferences;
|
|
|
|
use Session;
|
2015-04-01 18:59:34 +02:00
|
|
|
use URL;
|
2015-04-05 20:47:19 +02:00
|
|
|
use View;
|
2015-02-25 15:57:43 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class CurrencyController
|
|
|
|
*
|
|
|
|
* @package FireflyIII\Http\Controllers
|
|
|
|
*/
|
|
|
|
class CurrencyController extends Controller
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2016-02-04 07:27:03 +01:00
|
|
|
*
|
2015-02-25 15:57:43 +01:00
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
2015-04-28 15:26:30 +02:00
|
|
|
parent::__construct();
|
2015-05-14 13:17:53 +02:00
|
|
|
View::share('title', trans('firefly.currencies'));
|
2015-02-25 15:57:43 +01:00
|
|
|
View::share('mainTitleIcon', 'fa-usd');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-08-26 09:30:52 +02:00
|
|
|
* @return View
|
2015-02-25 15:57:43 +01:00
|
|
|
*/
|
|
|
|
public function create()
|
|
|
|
{
|
|
|
|
$subTitleIcon = 'fa-plus';
|
2015-06-09 17:56:08 +02:00
|
|
|
$subTitle = 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").
|
2016-02-04 07:27:03 +01:00
|
|
|
if (session('currency.create.fromStore') !== true) {
|
2015-04-01 18:59:34 +02:00
|
|
|
Session::put('currency.create.url', URL::previous());
|
|
|
|
}
|
|
|
|
Session::forget('currency.create.fromStore');
|
2015-05-25 08:12:31 +02:00
|
|
|
Session::flash('gaEventCategory', 'currency');
|
|
|
|
Session::flash('gaEventAction', 'create');
|
2015-04-01 18:59:34 +02:00
|
|
|
|
2015-02-25 15:57:43 +01:00
|
|
|
return view('currency.create', compact('subTitleIcon', 'subTitle'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param TransactionCurrency $currency
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
*/
|
|
|
|
public function defaultCurrency(TransactionCurrency $currency)
|
|
|
|
{
|
|
|
|
|
2015-04-05 20:47:19 +02:00
|
|
|
Preferences::set('currencyPreference', $currency->code);
|
2015-06-02 17:44:50 +02:00
|
|
|
Preferences::mark();
|
2015-02-25 15:57:43 +01:00
|
|
|
|
2016-01-23 09:10:22 +01:00
|
|
|
Session::flash('success', trans('firefly.new_default_currency', ['name' => $currency->name]));
|
2015-02-25 15:57:43 +01:00
|
|
|
Cache::forget('FFCURRENCYSYMBOL');
|
|
|
|
Cache::forget('FFCURRENCYCODE');
|
|
|
|
|
2015-07-06 16:27:21 +02:00
|
|
|
return redirect(route('currency.index'));
|
2015-02-25 15:57:43 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-09-16 12:07:45 +02:00
|
|
|
* @param TransactionCurrency $currency
|
2015-02-25 15:57:43 +01:00
|
|
|
*
|
2015-05-03 12:54:39 +02:00
|
|
|
* @return \Illuminate\Http\RedirectResponse|View
|
2015-02-25 15:57:43 +01:00
|
|
|
*/
|
2016-09-08 21:10:24 +02:00
|
|
|
public function delete(TransactionCurrency $currency)
|
2015-02-25 15:57:43 +01:00
|
|
|
{
|
2016-09-08 21:10:24 +02:00
|
|
|
if (!$this->canDeleteCurrency($currency)) {
|
2016-01-23 09:10:22 +01:00
|
|
|
Session::flash('error', trans('firefly.cannot_delete_currency', ['name' => $currency->name]));
|
2015-04-01 18:59:34 +02:00
|
|
|
|
2015-07-06 16:27:21 +02:00
|
|
|
return redirect(route('currency.index'));
|
2015-02-25 15:57:43 +01:00
|
|
|
}
|
|
|
|
|
2016-09-08 21:10:24 +02:00
|
|
|
|
2015-04-05 20:47:19 +02:00
|
|
|
// put previous url in session
|
|
|
|
Session::put('currency.delete.url', URL::previous());
|
2015-05-25 08:12:31 +02:00
|
|
|
Session::flash('gaEventCategory', 'currency');
|
|
|
|
Session::flash('gaEventAction', 'delete');
|
2015-06-21 15:09:10 +02:00
|
|
|
$subTitle = trans('form.delete_currency', ['name' => $currency->name]);
|
2015-04-05 20:47:19 +02:00
|
|
|
|
2015-02-25 15:57:43 +01:00
|
|
|
|
2015-06-21 15:09:10 +02:00
|
|
|
return view('currency.delete', compact('currency', 'subTitle'));
|
2015-02-25 15:57:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-09-16 12:07:45 +02:00
|
|
|
* @param TransactionCurrency $currency
|
2015-02-25 15:57:43 +01:00
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
2015-05-03 12:54:39 +02:00
|
|
|
* @throws \Exception
|
2015-02-25 15:57:43 +01:00
|
|
|
*/
|
2016-09-08 21:10:24 +02:00
|
|
|
public function destroy(TransactionCurrency $currency)
|
2015-02-25 15:57:43 +01:00
|
|
|
{
|
2016-09-08 21:10:24 +02:00
|
|
|
if (!$this->canDeleteCurrency($currency)) {
|
2016-01-23 09:10:22 +01:00
|
|
|
Session::flash('error', trans('firefly.cannot_delete_currency', ['name' => $currency->name]));
|
2015-02-25 15:57:43 +01:00
|
|
|
|
2015-07-06 16:27:21 +02:00
|
|
|
return redirect(route('currency.index'));
|
2015-02-25 15:57:43 +01:00
|
|
|
}
|
|
|
|
|
2016-01-23 09:10:22 +01:00
|
|
|
Session::flash('success', trans('firefly.deleted_currency', ['name' => $currency->name]));
|
2016-09-16 12:15:58 +02:00
|
|
|
if (auth()->user()->hasRole('owner')) {
|
2015-05-28 06:43:07 +02:00
|
|
|
$currency->delete();
|
|
|
|
}
|
2015-02-25 15:57:43 +01:00
|
|
|
|
2016-02-04 07:27:03 +01:00
|
|
|
return redirect(session('currency.delete.url'));
|
2015-02-25 15:57:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param TransactionCurrency $currency
|
|
|
|
*
|
2016-08-26 09:30:52 +02:00
|
|
|
* @return View
|
2015-02-25 15:57:43 +01:00
|
|
|
*/
|
|
|
|
public function edit(TransactionCurrency $currency)
|
|
|
|
{
|
|
|
|
$subTitleIcon = 'fa-pencil';
|
2016-09-09 11:19:19 +02:00
|
|
|
$subTitle = trans('breadcrumbs.edit_currency', ['name' => $currency->name]);
|
2015-02-25 15:57:43 +01:00
|
|
|
$currency->symbol = htmlentities($currency->symbol);
|
|
|
|
|
2015-04-01 18:59:34 +02:00
|
|
|
// put previous url in session if not redirect from store (not "return_to_edit").
|
2016-02-04 07:27:03 +01:00
|
|
|
if (session('currency.edit.fromUpdate') !== true) {
|
2015-04-01 18:59:34 +02:00
|
|
|
Session::put('currency.edit.url', URL::previous());
|
|
|
|
}
|
|
|
|
Session::forget('currency.edit.fromUpdate');
|
2015-05-25 08:12:31 +02:00
|
|
|
Session::flash('gaEventCategory', 'currency');
|
|
|
|
Session::flash('gaEventAction', 'edit');
|
2015-04-01 18:59:34 +02:00
|
|
|
|
2015-02-25 15:57:43 +01:00
|
|
|
return view('currency.edit', compact('currency', 'subTitle', 'subTitleIcon'));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-05-03 12:58:55 +02:00
|
|
|
* @param CurrencyRepositoryInterface $repository
|
|
|
|
*
|
2016-08-26 09:30:52 +02:00
|
|
|
* @return View
|
2015-02-25 15:57:43 +01:00
|
|
|
*/
|
2015-04-05 20:47:19 +02:00
|
|
|
public function index(CurrencyRepositoryInterface $repository)
|
2015-02-25 15:57:43 +01:00
|
|
|
{
|
2015-04-05 20:47:19 +02:00
|
|
|
$currencies = $repository->get();
|
2016-09-18 19:57:21 +02:00
|
|
|
$defaultCurrency = $repository->getCurrencyByPreference(Preferences::get('currencyPreference', config('firefly.default_currency', 'EUR')));
|
2015-02-25 15:57:43 +01:00
|
|
|
|
2015-05-28 06:43:07 +02:00
|
|
|
|
2016-09-16 12:15:58 +02:00
|
|
|
if (!auth()->user()->hasRole('owner')) {
|
2016-01-23 09:10:22 +01:00
|
|
|
Session::flash('warning', trans('firefly.ask_site_owner', ['owner' => env('SITE_OWNER')]));
|
2015-05-28 06:43:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-25 15:57:43 +01:00
|
|
|
return view('currency.index', compact('currencies', 'defaultCurrency'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
2015-05-03 12:58:55 +02:00
|
|
|
* @param CurrencyFormRequest $request
|
|
|
|
* @param CurrencyRepositoryInterface $repository
|
|
|
|
*
|
2016-08-26 09:30:52 +02:00
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
|
2015-02-25 15:57:43 +01:00
|
|
|
*/
|
2015-04-05 20:47:19 +02:00
|
|
|
public function store(CurrencyFormRequest $request, CurrencyRepositoryInterface $repository)
|
2015-02-25 15:57:43 +01:00
|
|
|
{
|
2016-09-16 12:15:58 +02:00
|
|
|
if (!auth()->user()->hasRole('owner')) {
|
|
|
|
Log::error('User ' . auth()->user()->id . ' is not admin, but tried to store a currency.');
|
2016-04-29 08:56:56 +02:00
|
|
|
|
|
|
|
return redirect(session('currency.create.url'));
|
2015-05-28 06:43:07 +02:00
|
|
|
}
|
2015-02-25 15:57:43 +01:00
|
|
|
|
2016-04-29 08:56:56 +02:00
|
|
|
$data = $request->getCurrencyData();
|
|
|
|
$currency = $repository->store($data);
|
|
|
|
Session::flash('success', trans('firefly.created_currency', ['name' => $currency->name]));
|
|
|
|
|
2015-03-02 20:05:28 +01:00
|
|
|
if (intval(Input::get('create_another')) === 1) {
|
2015-04-01 18:59:34 +02:00
|
|
|
Session::put('currency.create.fromStore', true);
|
2015-04-05 20:47:19 +02:00
|
|
|
|
2015-07-06 16:27:21 +02:00
|
|
|
return redirect(route('currency.create'))->withInput();
|
2015-03-02 20:05:28 +01:00
|
|
|
}
|
|
|
|
|
2015-04-01 18:59:34 +02:00
|
|
|
// redirect to previous URL.
|
2016-02-04 07:27:03 +01:00
|
|
|
return redirect(session('currency.create.url'));
|
2015-02-25 15:57:43 +01:00
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-05-03 12:54:39 +02:00
|
|
|
* @param CurrencyFormRequest $request
|
|
|
|
* @param CurrencyRepositoryInterface $repository
|
|
|
|
* @param TransactionCurrency $currency
|
2015-02-25 15:57:43 +01:00
|
|
|
*
|
2015-05-03 12:54:39 +02:00
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
2015-02-25 15:57:43 +01:00
|
|
|
*/
|
2015-05-03 12:54:39 +02:00
|
|
|
public function update(CurrencyFormRequest $request, CurrencyRepositoryInterface $repository, TransactionCurrency $currency)
|
2015-02-25 15:57:43 +01:00
|
|
|
{
|
2015-05-28 06:43:07 +02:00
|
|
|
$data = $request->getCurrencyData();
|
2016-09-16 12:15:58 +02:00
|
|
|
if (auth()->user()->hasRole('owner')) {
|
2015-05-28 06:43:07 +02:00
|
|
|
$currency = $repository->update($currency, $data);
|
|
|
|
}
|
2016-01-23 09:10:22 +01:00
|
|
|
Session::flash('success', trans('firefly.updated_currency', ['name' => $currency->name]));
|
2015-06-02 17:44:50 +02:00
|
|
|
Preferences::mark();
|
2015-03-02 20:05:28 +01:00
|
|
|
|
|
|
|
|
|
|
|
if (intval(Input::get('return_to_edit')) === 1) {
|
2015-04-01 18:59:34 +02:00
|
|
|
Session::put('currency.edit.fromUpdate', true);
|
2015-04-05 20:47:19 +02:00
|
|
|
|
2015-07-06 16:27:21 +02:00
|
|
|
return redirect(route('currency.edit', [$currency->id]));
|
2015-03-02 20:05:28 +01:00
|
|
|
}
|
2015-02-25 15:57:43 +01:00
|
|
|
|
2015-04-01 18:59:34 +02:00
|
|
|
// redirect to previous URL.
|
2016-02-04 07:27:03 +01:00
|
|
|
return redirect(session('currency.edit.url'));
|
2015-02-25 15:57:43 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-09-08 21:10:24 +02:00
|
|
|
/**
|
|
|
|
* @param TransactionCurrency $currency
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function canDeleteCurrency(TransactionCurrency $currency): bool
|
|
|
|
{
|
|
|
|
$repository = app(CurrencyRepositoryInterface::class);
|
|
|
|
|
|
|
|
// has transactions still
|
|
|
|
if ($repository->countJournals($currency) > 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// is the only currency left
|
|
|
|
if ($repository->get()->count() === 1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// is the default currency for the user or the system
|
2016-09-18 19:57:21 +02:00
|
|
|
$defaultCode = Preferences::get('currencyPreference', config('firefly.default_currency', 'EUR'))->data;
|
2016-09-08 21:10:24 +02:00
|
|
|
if ($currency->code === $defaultCode) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// is the default currency for the system
|
2016-09-18 19:57:21 +02:00
|
|
|
$defaultSystemCode = config('firefly.default_currency', 'EUR');
|
2016-09-08 21:10:24 +02:00
|
|
|
if ($currency->code === $defaultSystemCode) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// can be deleted
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-02-25 15:57:43 +01:00
|
|
|
}
|