Final updates for account controller. Looks like this one is finished. [skip ci]

This commit is contained in:
James Cole 2014-08-02 07:49:48 +02:00
parent d756324432
commit 99500d6201
6 changed files with 76 additions and 62 deletions

View File

@ -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());
}
}

View File

@ -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);
}

View File

@ -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();
}
}
}

View File

@ -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']);

View File

@ -11,13 +11,11 @@
</div>
</div>
{{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)])}}
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12">
@if($account->transactions()->count() > 0)
<p class="text-info">
Account "{{{$account->name}}}" still has {{$account->transactions()->count()}} transaction(s) associated to it.
These will be deleted as well.
</p>

View File

@ -12,8 +12,7 @@
</div>
</div>
{{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)])}}
<div class="row">
<div class="col-lg-6 col-md-6 col-sm-12">
<h4>Mandatory fields</h4>