diff --git a/app/controllers/AccountController.php b/app/controllers/AccountController.php index 71f7e32ac3..8c1e5bf966 100644 --- a/app/controllers/AccountController.php +++ b/app/controllers/AccountController.php @@ -24,9 +24,7 @@ class AccountController extends \BaseController } /** - * Show the form for creating a new resource. - * - * @return Response + * @return \Illuminate\View\View */ public function create() { @@ -44,20 +42,21 @@ class AccountController extends \BaseController } /** + * @param Account $account + * * @return \Illuminate\Http\RedirectResponse */ - public function destroy() + public function destroy(Account $account) { - $result = $this->_repository->destroy(Input::get('id')); + $result = $this->_repository->destroy($account); if ($result === true) { Session::flash('success', 'The account was deleted.'); } else { - Session::flash('error', 'Could not delete the account. Check the logs to be sure.'); + Session::flash('error', 'Could not delete the account.'); } return Redirect::route('accounts.index'); - } /** @@ -73,9 +72,7 @@ class AccountController extends \BaseController } /** - * Display a listing of the resource. - * - * @return Response + * @return \Illuminate\View\View */ public function index() { @@ -98,7 +95,7 @@ class AccountController extends \BaseController } /** - * @return \Illuminate\Http\RedirectResponse|\Illuminate\View\View + * @return \Illuminate\Http\RedirectResponse */ public function store() { @@ -109,7 +106,7 @@ class AccountController extends \BaseController // did not save, return with error: Session::flash('error', 'Could not save the new account. Please check the form.'); - return View::make('accounts.create')->withErrors($account->errors()); + return Redirect::route('accounts.create')->withErrors($account->errors())->withInput(); } else { // saved! return to wherever. Session::flash('success', 'Account "' . $account->name . '" created!'); @@ -122,16 +119,22 @@ class AccountController extends \BaseController } /** - * Update the specified resource in storage. + * @param Account $account * - * @return Response + * @return \Illuminate\Http\RedirectResponse */ - public function update() + public function update(Account $account) { - $account = $this->_repository->update(Input::all()); - Session::flash('success', 'Account "' . $account->name . '" updated.'); + $account = $this->_repository->update($account, Input::all()); + if ($account->validate()) { + Session::flash('success', 'Account "' . $account->name . '" updated.'); - return Redirect::route('accounts.index'); + return Redirect::route('accounts.index'); + } else { + Session::flash('error', 'Could not update account: ' . $account->errors()->first()); + + return Redirect::route('accounts.edit', $account->id)->withInput()->withErrors($account->errors()); + } } diff --git a/app/lib/Firefly/Storage/Account/AccountRepositoryInterface.php b/app/lib/Firefly/Storage/Account/AccountRepositoryInterface.php index 80e45c868f..21f0b9757d 100644 --- a/app/lib/Firefly/Storage/Account/AccountRepositoryInterface.php +++ b/app/lib/Firefly/Storage/Account/AccountRepositoryInterface.php @@ -32,11 +32,11 @@ interface AccountRepositoryInterface public function createOrFindBeneficiary($name); /** - * @param $accountId + * @param \Account $account * - * @return bool + * @return mixed */ - public function destroy($accountId); + public function destroy(\Account $account); /** * @param $accountId @@ -97,10 +97,11 @@ interface AccountRepositoryInterface public function store($data); /** - * @param $data + * @param \Account $account + * @param $data * - * @return \Account + * @return mixed */ - public function update($data); + public function update(\Account $account, $data); } \ No newline at end of file diff --git a/app/lib/Firefly/Storage/Account/EloquentAccountRepository.php b/app/lib/Firefly/Storage/Account/EloquentAccountRepository.php index 8f4497e4b1..22b03d285e 100644 --- a/app/lib/Firefly/Storage/Account/EloquentAccountRepository.php +++ b/app/lib/Firefly/Storage/Account/EloquentAccountRepository.php @@ -64,16 +64,17 @@ class EloquentAccountRepository implements AccountRepositoryInterface return $this->createOrFind($name, $type); } - public function destroy($accountId) + public function destroy(\Account $account) { - $account = $this->find($accountId); - if ($account) { - $account->delete(); + $account->delete(); - return true; - } + /** + * + * TODO + * Also delete: initial balance, initial balance account, and transactions + */ - return false; + return true; } /** @@ -227,24 +228,31 @@ class EloquentAccountRepository implements AccountRepositoryInterface * * @return \Account|void */ - public function update($data) + public function update(\Account $account, $data) { - $account = $this->find($data['id']); - if ($account) { - // update account accordingly: - $account->name = $data['name']; - if ($account->validate()) { - $account->save(); - } - // update initial balance if necessary: + // update account accordingly: + $account->name = $data['name']; + if ($account->validate()) { + $account->save(); + } + // update initial balance if necessary: + if (floatval($data['openingbalance']) != 0) { + + /** @var \Firefly\Helper\Controllers\AccountInterface $interface */ + $interface = \App::make('Firefly\Helper\Controllers\AccountInterface'); + if ($account->accounttype->description == 'Default account') { - $journal = $this->findOpeningBalanceTransaction($account); - $journal->date = new Carbon($data['openingbalancedate']); - $journal->transactions[0]->amount = floatval($data['openingbalance']) * -1; - $journal->transactions[1]->amount = floatval($data['openingbalance']); - $journal->transactions[0]->save(); - $journal->transactions[1]->save(); - $journal->save(); + + + $journal = $interface->openingBalanceTransaction($account); + if ($journal) { + $journal->date = new Carbon($data['openingbalancedate']); + $journal->transactions[0]->amount = floatval($data['openingbalance']) * -1; + $journal->transactions[1]->amount = floatval($data['openingbalance']); + $journal->transactions[0]->save(); + $journal->transactions[1]->save(); + $journal->save(); + } } } diff --git a/app/routes.php b/app/routes.php index ea47f032c5..77f5dd035f 100644 --- a/app/routes.php +++ b/app/routes.php @@ -67,6 +67,14 @@ Route::bind('piggybank', function($value, $route) // protected routes: Route::group(['before' => 'auth'], function () { + // account controller: + Route::get('/accounts', ['uses' => 'AccountController@index', 'as' => 'accounts.index']); + Route::get('/accounts/create', ['uses' => 'AccountController@create', 'as' => 'accounts.create']); + Route::get('/accounts/{account}', ['uses' => 'AccountController@show', 'as' => 'accounts.show']); + Route::get('/accounts/{account}/edit', ['uses' => 'AccountController@edit', 'as' => 'accounts.edit']); + Route::get('/accounts/{account}/delete', ['uses' => 'AccountController@delete', 'as' => 'accounts.delete']); + + // home controller Route::get('/', ['uses' => 'HomeController@index', 'as' => 'index']); Route::get('/flush', ['uses' => 'HomeController@flush', 'as' => 'flush']); @@ -103,12 +111,7 @@ Route::group(['before' => 'auth'], function () { Route::get('/profile', ['uses' => 'ProfileController@index', 'as' => 'profile']); Route::get('/profile/change-password',['uses' => 'ProfileController@changePassword', 'as' => 'change-password']); - // account controller: - Route::get('/accounts', ['uses' => 'AccountController@index', 'as' => 'accounts.index']); - Route::get('/accounts/create', ['uses' => 'AccountController@create', 'as' => 'accounts.create']); - Route::get('/accounts/{account}', ['uses' => 'AccountController@show', 'as' => 'accounts.show']); - Route::get('/accounts/{account}/edit', ['uses' => 'AccountController@edit', 'as' => 'accounts.edit']); - Route::get('/accounts/{account}/delete', ['uses' => 'AccountController@delete', 'as' => 'accounts.delete']); + // budget controller: Route::get('/budgets',['uses' => 'BudgetController@indexByDate','as' => 'budgets.index']); @@ -144,6 +147,12 @@ Route::group(['before' => 'auth'], function () { // protected + csrf routes (POST) Route::group(['before' => 'csrf|auth'], function () { + // account controller: + Route::post('/accounts/store', ['uses' => 'AccountController@store', 'as' => 'accounts.store']); + Route::post('/accounts/update/{account}', ['uses' => 'AccountController@update', 'as' => 'accounts.update']); + Route::post('/accounts/destroy/{account}', ['uses' => 'AccountController@destroy', 'as' => 'accounts.destroy']); + + // profile controller Route::post('/profile/change-password', ['uses' => 'ProfileController@postChangePassword']); @@ -170,10 +179,6 @@ Route::group(['before' => 'csrf|auth'], function () { // preferences controller Route::post('/preferences', ['uses' => 'PreferencesController@postIndex']); - // account controller: - Route::post('/accounts/store', ['uses' => 'AccountController@store', 'as' => 'accounts.store']); - Route::post('/accounts/update', ['uses' => 'AccountController@update', 'as' => 'accounts.update']); - Route::post('/accounts/destroy', ['uses' => 'AccountController@destroy', 'as' => 'accounts.destroy']); // limit controller: Route::post('/budgets/limits/store/{budget?}', ['uses' => 'LimitController@store', 'as' => 'budgets.limits.store']); diff --git a/app/views/accounts/delete.blade.php b/app/views/accounts/delete.blade.php index 29b4abc27f..6b939dab48 100644 --- a/app/views/accounts/delete.blade.php +++ b/app/views/accounts/delete.blade.php @@ -11,13 +11,11 @@ -{{Form::open(['class' => 'form-horizontal','url' => route('accounts.destroy')])}} -{{Form::hidden('id',$account->id)}} +{{Form::open(['class' => 'form-horizontal','url' => route('accounts.destroy',$account->id)])}}
@if($account->transactions()->count() > 0)

- Account "{{{$account->name}}}" still has {{$account->transactions()->count()}} transaction(s) associated to it. These will be deleted as well.

diff --git a/app/views/accounts/edit.blade.php b/app/views/accounts/edit.blade.php index cb5b19a877..19afe6d788 100644 --- a/app/views/accounts/edit.blade.php +++ b/app/views/accounts/edit.blade.php @@ -12,8 +12,7 @@
-{{Form::model($account, ['class' => 'form-horizontal','url' => route('accounts.update')])}} -{{Form::hidden('id',$account->id)}} +{{Form::model($account, ['class' => 'form-horizontal','url' => route('accounts.update',$account->id)])}}

Mandatory fields