diff --git a/app/Http/Controllers/CurrencyController.php b/app/Http/Controllers/CurrencyController.php new file mode 100644 index 0000000000..6bb11e4d1e --- /dev/null +++ b/app/Http/Controllers/CurrencyController.php @@ -0,0 +1,173 @@ +get('currencyPreference', 'EUR'); + $currencyPreference->data = $currency->code; + $currencyPreference->save(); + + Session::flash('success', $currency->name . ' is now the default currency.'); + Cache::forget('FFCURRENCYSYMBOL'); + Cache::forget('FFCURRENCYCODE'); + + return Redirect::route('currency.index'); + + } + + /** + * @param TransactionCurrency $currency + * + * @return \Illuminate\Http\RedirectResponse|\Illuminate\View\View + */ + public function delete(TransactionCurrency $currency) + { + if ($currency->transactionJournals()->count() > 0) { + Session::flash('error', 'Cannot delete ' . e($currency->name) . ' because there are still transactions attached to it.'); + + return Redirect::route('currency.index'); + } + + + return View::make('currency.delete', compact('currency')); + } + + /** + * @param TransactionCurrency $currency + * + * @return \Illuminate\Http\RedirectResponse + */ + public function destroy(TransactionCurrency $currency) + { + if ($currency->transactionJournals()->count() > 0) { + Session::flash('error', 'Cannot delete ' . e($currency->name) . ' because there are still transactions attached to it.'); + + return Redirect::route('currency.index'); + } + + Session::flash('success', 'Currency "' . e($currency->name) . '" deleted'); + + $currency->delete(); + + return Redirect::route('currency.index'); + } + + /** + * @param TransactionCurrency $currency + * + * @return \Illuminate\View\View + */ + public function edit(TransactionCurrency $currency) + { + $subTitleIcon = 'fa-pencil'; + $subTitle = 'Edit currency "' . e($currency->name) . '"'; + $currency->symbol = htmlentities($currency->symbol); + + return view('currency.edit', compact('currency', 'subTitle', 'subTitleIcon')); + + } + + /** + * @return \Illuminate\View\View + */ + public function index() + { + $currencies = TransactionCurrency::get(); + $currencyPreference = Preferences::get('currencyPreference', 'EUR'); + $defaultCurrency = TransactionCurrency::whereCode($currencyPreference->data)->first(); + + + return view('currency.index', compact('currencies', 'defaultCurrency')); + } + + /** + * @SuppressWarnings("CyclomaticComplexity") // It's exactly 5. So I don't mind. + * + * @return $this|\Illuminate\Http\RedirectResponse + */ + public function store(CurrencyFormRequest $request) + { + + + // no repository, because the currency controller is relatively simple. + $currency = TransactionCurrency::create( + [ + 'name' => $request->get('name'), + 'code' => $request->get('code'), + 'symbol' => $request->get('symbol'), + ] + ); + + Session::flash('success', 'Currency "' . $currency->name . '" created'); + + return Redirect::route('currency.index'); + + + } + + /** + * @param TransactionCurrency $currency + * + * @return $this|\Illuminate\Http\RedirectResponse + */ + public function update(TransactionCurrency $currency, CurrencyFormRequest $request) + { + + $currency->code = $request->get('code'); + $currency->symbol = $request->get('symbol'); + $currency->name = $request->get('name'); + $currency->save(); + + Session::flash('success', 'Currency "' . e($currency->namename) . '" updated.'); + + return Redirect::route('currency.index'); + + } + +} diff --git a/app/Http/Requests/CurrencyFormRequest.php b/app/Http/Requests/CurrencyFormRequest.php new file mode 100644 index 0000000000..75e0f7dd6f --- /dev/null +++ b/app/Http/Requests/CurrencyFormRequest.php @@ -0,0 +1,51 @@ + 'required|min:3|max:3|unique:transaction_currencies,code', + 'name' => 'required|max:48|min:1|unique:transaction_currencies,name', + 'symbol' => 'required|min:1|max:8|unique:transaction_currencies,symbol', + ]; + if (intval(Input::get('id')) > 0) { + $rules = [ + 'code' => 'required|min:3|max:3', + 'name' => 'required|max:48|min:1', + 'symbol' => 'required|min:1|max:8', + ]; + } + + return $rules; + } +} \ No newline at end of file diff --git a/app/Http/routes.php b/app/Http/routes.php index f2d544d234..1f3766d1cc 100644 --- a/app/Http/routes.php +++ b/app/Http/routes.php @@ -4,6 +4,7 @@ use FireflyIII\Models\Bill; use FireflyIII\Models\Budget; use FireflyIII\Models\Category; use FireflyIII\Models\LimitRepetition; +use FireflyIII\Models\TransactionCurrency; // models @@ -24,6 +25,12 @@ Route::bind( } ); +Route::bind( + 'currency', function ($value, $route) { + return TransactionCurrency::find($value); +} +); + Route::bind( 'bill', function ($value, $route) { if (Auth::check()) { @@ -138,10 +145,14 @@ Route::group( * Currency Controller */ Route::get('/currency', ['uses' => 'CurrencyController@index', 'as' => 'currency.index']); - //Route::get('/currency/create', ['uses' => 'CurrencyController@create', 'as' => 'currency.create']); - //Route::get('/currency/edit/{currency}', ['uses' => 'CurrencyController@edit', 'as' => 'currency.edit']); - //Route::get('/currency/delete/{currency}', ['uses' => 'CurrencyController@delete', 'as' => 'currency.delete']); - //Route::get('/currency/default/{currency}', ['uses' => 'CurrencyController@defaultCurrency', 'as' => 'currency.default']); + Route::get('/currency/create', ['uses' => 'CurrencyController@create', 'as' => 'currency.create']); + Route::get('/currency/edit/{currency}', ['uses' => 'CurrencyController@edit', 'as' => 'currency.edit']); + Route::get('/currency/delete/{currency}', ['uses' => 'CurrencyController@delete', 'as' => 'currency.delete']); + Route::get('/currency/default/{currency}', ['uses' => 'CurrencyController@defaultCurrency', 'as' => 'currency.default']); + Route::post('/currency/store', ['uses' => 'CurrencyController@store', 'as' => 'currency.store']); + Route::post('/currency/update/{currency}', ['uses' => 'CurrencyController@update', 'as' => 'currency.update']); + Route::post('/currency/destroy/{currency}', ['uses' => 'CurrencyController@destroy', 'as' => 'currency.destroy']); + /** * Google Chart Controller diff --git a/app/Models/TransactionCurrency.php b/app/Models/TransactionCurrency.php index 884e355d96..9283709f9e 100644 --- a/app/Models/TransactionCurrency.php +++ b/app/Models/TransactionCurrency.php @@ -12,6 +12,9 @@ class TransactionCurrency extends Model { use SoftDeletes; + + protected $fillable = ['name', 'code', 'symbol']; + /** * @return array */ diff --git a/resources/views/currency/create.blade.php b/resources/views/currency/create.blade.php new file mode 100644 index 0000000000..bd5249f03e --- /dev/null +++ b/resources/views/currency/create.blade.php @@ -0,0 +1,40 @@ +@extends('layouts.default') +@section('content') +{!! Breadcrumbs::renderIfExists(Route::getCurrentRoute()->getName()) !!} +{!! Form::open(['class' => 'form-horizontal','id' => 'store','route' => 'currency.store']) !!} +
+ +
++ Are you sure? +
++ + Cancel +
++ +
++ Firefly III supports various currencies which you can set and enable here. +
+