Fixes for account routes.

This commit is contained in:
James Cole 2014-09-15 17:03:53 +02:00
parent 9ae036f297
commit 4632142e06
5 changed files with 172 additions and 123 deletions

View File

@ -31,7 +31,18 @@ class AccountController extends \BaseController
*/ */
public function create($what) public function create($what)
{ {
View::share('subTitleIcon', 'fa-money'); switch ($what) {
case 'asset':
View::share('subTitleIcon', 'fa-money');
break;
case 'expense':
View::share('subTitleIcon', 'fa-shopping-cart');
break;
case 'revenue':
View::share('subTitleIcon', 'fa-download');
break;
}
return View::make('accounts.create')->with('subTitle', 'Create a new ' . $what . ' account')->with( return View::make('accounts.create')->with('subTitle', 'Create a new ' . $what . ' account')->with(
'what', $what 'what', $what
@ -43,7 +54,7 @@ class AccountController extends \BaseController
*/ */
public function asset() public function asset()
{ {
View::share('subTitleIcon','fa-money'); View::share('subTitleIcon', 'fa-money');
$accounts = $this->_repository->getOfTypes(['Asset account', 'Default account']); $accounts = $this->_repository->getOfTypes(['Asset account', 'Default account']);
@ -57,7 +68,7 @@ class AccountController extends \BaseController
*/ */
public function expense() public function expense()
{ {
View::share('subTitleIcon','fa-shopping-cart'); View::share('subTitleIcon', 'fa-shopping-cart');
$accounts = $this->_repository->getOfTypes(['Expense account', 'Beneficiary account']); $accounts = $this->_repository->getOfTypes(['Expense account', 'Beneficiary account']);
@ -71,7 +82,7 @@ class AccountController extends \BaseController
*/ */
public function revenue() public function revenue()
{ {
View::share('subTitleIcon','fa-download'); View::share('subTitleIcon', 'fa-download');
$accounts = $this->_repository->getOfTypes(['Revenue account']); $accounts = $this->_repository->getOfTypes(['Revenue account']);
@ -127,6 +138,21 @@ class AccountController extends \BaseController
*/ */
public function edit(Account $account) public function edit(Account $account)
{ {
switch ($account->accountType->type) {
case 'Asset account':
case 'Default account':
View::share('subTitleIcon', 'fa-money');
break;
case 'Expense account':
case 'Beneficiary account':
View::share('subTitleIcon', 'fa-shopping-cart');
break;
case 'Revenue account':
View::share('subTitleIcon', 'fa-download');
break;
}
$openingBalance = $this->_accounts->openingBalanceTransaction($account); $openingBalance = $this->_accounts->openingBalanceTransaction($account);
return View::make('accounts.edit')->with('account', $account)->with('openingBalance', $openingBalance) return View::make('accounts.edit')->with('account', $account)->with('openingBalance', $openingBalance)
@ -139,23 +165,6 @@ class AccountController extends \BaseController
public function index() public function index()
{ {
return View::make('error')->with('message', 'This view has been disabled'); return View::make('error')->with('message', 'This view has been disabled');
// $accounts = $this->_repository->get();
// $set = [
// 'personal' => [],
// 'beneficiaries' => []
// ];
// foreach ($accounts as $account) {
// switch ($account->accounttype->type) {
// case 'Default account':
// $set['personal'][] = $account;
// break;
// case 'Beneficiary account':
// $set['beneficiaries'][] = $account;
// break;
// }
// }
//
// return View::make('accounts.index')->with('accounts', $set)->with('title', 'All your accounts');
} }
/** /**
@ -165,6 +174,21 @@ class AccountController extends \BaseController
*/ */
public function show(Account $account) public function show(Account $account)
{ {
switch ($account->accountType->type) {
case 'Asset account':
case 'Default account':
View::share('subTitleIcon', 'fa-money');
break;
case 'Expense account':
case 'Beneficiary account':
View::share('subTitleIcon', 'fa-shopping-cart');
break;
case 'Revenue account':
View::share('subTitleIcon', 'fa-download');
break;
}
$data = $this->_accounts->show($account, 40); $data = $this->_accounts->show($account, 40);
return View::make('accounts.show')->with('account', $account)->with('show', $data)->with( return View::make('accounts.show')->with('account', $account)->with('show', $data)->with(
@ -204,7 +228,8 @@ class AccountController extends \BaseController
if (intval(Input::get('create')) === 1) { if (intval(Input::get('create')) === 1) {
return Redirect::route('accounts.create', $data['what'])->withInput(); return Redirect::route('accounts.create', $data['what'])->withInput();
} else { } else {
return Redirect::route('accounts.index');
return Redirect::route('accounts.' . e($data['what']));
} }
} else { } else {
// did not save, return with error: // did not save, return with error:
@ -222,11 +247,24 @@ class AccountController extends \BaseController
*/ */
public function update(Account $account) public function update(Account $account)
{ {
/** @var \Account $account */
$account = $this->_repository->update($account, Input::all()); $account = $this->_repository->update($account, Input::all());
if ($account->validate()) { if ($account->validate()) {
Session::flash('success', 'Account "' . $account->name . '" updated.'); Session::flash('success', 'Account "' . $account->name . '" updated.');
switch ($account->accountType->type) {
case 'Asset account':
case 'Default account':
return Redirect::route('accounts.asset');
break;
case 'Expense account':
case 'Beneficiary account':
return Redirect::route('accounts.expense');
break;
case 'Revenue account':
return Redirect::route('accounts.revenue');
break;
}
return Redirect::route('accounts.index');
} else { } else {
Session::flash('error', 'Could not update account: ' . $account->errors()->first()); Session::flash('error', 'Could not update account: ' . $account->errors()->first());

View File

@ -66,7 +66,7 @@ class EloquentAccountRepository implements AccountRepositoryInterface
/* /*
* Find the import map for both: * Find the import map for both:
*/ */
$accountMap = $repository->findImportEntry($importMap, 'Account', $componentId); $accountMap = $repository->findImportEntry($importMap, 'Account', $componentId);
$transactionMap = $repository->findImportEntry($importMap, 'Transaction', $transactionId); $transactionMap = $repository->findImportEntry($importMap, 'Transaction', $transactionId);
/* /*
@ -74,7 +74,7 @@ class EloquentAccountRepository implements AccountRepositoryInterface
*/ */
if (is_null($accountMap) || is_null($transactionMap)) { if (is_null($accountMap) || is_null($transactionMap)) {
\Log::notice('No map found in account/transaction mapper. Release.'); \Log::notice('No map found in account/transaction mapper. Release.');
if(\Config::get('queue.default') == 'sync') { if (\Config::get('queue.default') == 'sync') {
$importMap->jobsdone++; $importMap->jobsdone++;
$importMap->save(); $importMap->save();
$job->delete(); // count fixed $job->delete(); // count fixed
@ -96,7 +96,7 @@ class EloquentAccountRepository implements AccountRepositoryInterface
*/ */
if (is_null($account) || is_null($journal)) { if (is_null($account) || is_null($journal)) {
\Log::notice('Map is incorrect in account/transaction mapper. Release.'); \Log::notice('Map is incorrect in account/transaction mapper. Release.');
if(\Config::get('queue.default') == 'sync') { if (\Config::get('queue.default') == 'sync') {
$importMap->jobsdone++; $importMap->jobsdone++;
$importMap->save(); $importMap->save();
$job->delete(); // count fixed $job->delete(); // count fixed
@ -111,18 +111,18 @@ class EloquentAccountRepository implements AccountRepositoryInterface
*/ */
$importType = $this->findAccountType('Import account'); $importType = $this->findAccountType('Import account');
/** @var \Transaction $transaction */ /** @var \Transaction $transaction */
foreach($journal->transactions as $transaction) { foreach ($journal->transactions as $transaction) {
/* /*
* If it's of the right type, update it! * If it's of the right type, update it!
*/ */
if($transaction->account->account_type_id == $importType->id) { if ($transaction->account->account_type_id == $importType->id) {
$transaction->account()->associate($account); $transaction->account()->associate($account);
$transaction->save(); $transaction->save();
} }
} }
$journal->save(); $journal->save();
\Log::debug('Connected expense account "' . $account->name . '" to journal "' . $journal->description.'"'); \Log::debug('Connected expense account "' . $account->name . '" to journal "' . $journal->description . '"');
$importMap->jobsdone++; $importMap->jobsdone++;
$importMap->save(); $importMap->save();
@ -131,6 +131,37 @@ class EloquentAccountRepository implements AccountRepositoryInterface
} }
/**
* @param \User $user
*
* @return mixed|void
*/
public function overruleUser(\User $user)
{
$this->_user = $user;
return true;
}
/**
* @param $accountId
*
* @return mixed
*/
public function find($accountId)
{
return $this->_user->accounts()->where('id', $accountId)->first();
}
/**
* @param $type
*
* @return mixed
*/
public function findAccountType($type)
{
return \AccountType::where('type', $type)->first();
}
/** /**
* @param Job $job * @param Job $job
* @param array $payload * @param array $payload
@ -198,7 +229,7 @@ class EloquentAccountRepository implements AccountRepositoryInterface
$importMap->jobsdone++; $importMap->jobsdone++;
$importMap->save(); $importMap->save();
$job->delete();// count fixed $job->delete(); // count fixed
return; return;
} }
\Log::debug('Imported ' . $payload['account_type'] . ': ' . $payload['data']['name']); \Log::debug('Imported ' . $payload['account_type'] . ': ' . $payload['data']['name']);
@ -213,27 +244,6 @@ class EloquentAccountRepository implements AccountRepositoryInterface
return; return;
} }
/**
* @param \User $user
*
* @return mixed|void
*/
public function overruleUser(\User $user)
{
$this->_user = $user;
return true;
}
/**
* @param $type
*
* @return mixed
*/
public function findAccountType($type)
{
return \AccountType::where('type', $type)->first();
}
/** /**
* @param $data * @param $data
* *
@ -294,44 +304,6 @@ class EloquentAccountRepository implements AccountRepositoryInterface
// whatever the result, return the account. // whatever the result, return the account.
return $account; return $account;
} }
/**
* @param \Account $account
* @param int $amount
* @param Carbon $date
*
* @return bool
* @SuppressWarnings(PHPMD.CamelCaseMethodName)
*/
protected function _createInitialBalance(\Account $account, $amount = 0, Carbon $date)
{
// get account type:
$initialBalanceAT = \AccountType::where('type', 'Initial balance account')->first();
// create new account:
$initial = new \Account;
$initial->accountType()->associate($initialBalanceAT);
$initial->user()->associate($this->_user);
$initial->name = $account->name . ' initial balance';
$initial->active = 0;
if ($initial->validate()) {
$initial->save();
// create new transaction journal (and transactions):
/** @var \Firefly\Storage\TransactionJournal\TransactionJournalRepositoryInterface $transactionJournal */
$transactionJournal = \App::make(
'Firefly\Storage\TransactionJournal\TransactionJournalRepositoryInterface'
);
$transactionJournal->overruleUser($this->_user);
$transactionJournal->createSimpleJournal(
$initial, $account, 'Initial Balance for ' . $account->name, $amount, $date
);
return true;
}
return false;
}
// //
// /** // /**
// * @param $name // * @param $name
@ -383,6 +355,44 @@ class EloquentAccountRepository implements AccountRepositoryInterface
// ->first(); // ->first();
// } // }
/**
* @param \Account $account
* @param int $amount
* @param Carbon $date
*
* @return bool
* @SuppressWarnings(PHPMD.CamelCaseMethodName)
*/
protected function _createInitialBalance(\Account $account, $amount = 0, Carbon $date)
{
// get account type:
$initialBalanceAT = \AccountType::where('type', 'Initial balance account')->first();
// create new account:
$initial = new \Account;
$initial->accountType()->associate($initialBalanceAT);
$initial->user()->associate($this->_user);
$initial->name = $account->name . ' initial balance';
$initial->active = 0;
if ($initial->validate()) {
$initial->save();
// create new transaction journal (and transactions):
/** @var \Firefly\Storage\TransactionJournal\TransactionJournalRepositoryInterface $transactionJournal */
$transactionJournal = \App::make(
'Firefly\Storage\TransactionJournal\TransactionJournalRepositoryInterface'
);
$transactionJournal->overruleUser($this->_user);
$transactionJournal->createSimpleJournal(
$initial, $account, 'Initial Balance for ' . $account->name, $amount, $date
);
return true;
}
return false;
}
public function firstOrCreate(array $data) public function firstOrCreate(array $data)
{ {
return \Account::firstOrCreate($data); return \Account::firstOrCreate($data);
@ -422,6 +432,20 @@ class EloquentAccountRepository implements AccountRepositoryInterface
} }
// /**
// * Used for import
// *
// * @param $name
// *
// * @return mixed
// */
// public function findByNameAny($name)
// {
// return $this->_user->accounts()
// ->where('name', 'like', '%' . $name . '%')
// ->first();
// }
/** /**
* @param \Account $account * @param \Account $account
* *
@ -430,7 +454,7 @@ class EloquentAccountRepository implements AccountRepositoryInterface
public function destroy(\Account $account) public function destroy(\Account $account)
{ {
// find all transaction journals related to this account: // find all transaction journals related to this account:
$journals = \TransactionJournal::withRelevantData()->account($account)->get(['transaction_journals.*']); $journals = \TransactionJournal::withRelevantData()->accountIs($account)->get(['transaction_journals.*']);
$accountIDs = []; $accountIDs = [];
/** @var \TransactionJournal $journal */ /** @var \TransactionJournal $journal */
@ -463,30 +487,6 @@ class EloquentAccountRepository implements AccountRepositoryInterface
return true; return true;
} }
// /**
// * Used for import
// *
// * @param $name
// *
// * @return mixed
// */
// public function findByNameAny($name)
// {
// return $this->_user->accounts()
// ->where('name', 'like', '%' . $name . '%')
// ->first();
// }
/**
* @param $accountId
*
* @return mixed
*/
public function find($accountId)
{
return $this->_user->accounts()->where('id', $accountId)->first();
}
// /** // /**
// * @return array|mixed // * @return array|mixed
// */ // */
@ -598,7 +598,7 @@ class EloquentAccountRepository implements AccountRepositoryInterface
$account->save(); $account->save();
} }
// update initial balance if necessary: // update initial balance if necessary:
if (floatval($data['openingbalance']) != 0) { if (isset($data['openingbalance']) && floatval($data['openingbalance']) != 0) {
/** @var \Firefly\Helper\Controllers\AccountInterface $interface */ /** @var \Firefly\Helper\Controllers\AccountInterface $interface */
$interface = \App::make('Firefly\Helper\Controllers\AccountInterface'); $interface = \App::make('Firefly\Helper\Controllers\AccountInterface');

View File

@ -136,14 +136,11 @@ Route::group(['before' => 'auth'], function () {
// account controller: // account controller:
Route::get('/accounts', ['uses' => 'AccountController@index', 'as' => 'accounts.index']); Route::get('/accounts', ['uses' => 'AccountController@index', 'as' => 'accounts.index']);
// new routes for new layout:
Route::get('/accounts/asset', ['uses' => 'AccountController@asset', 'as' => 'accounts.asset']); Route::get('/accounts/asset', ['uses' => 'AccountController@asset', 'as' => 'accounts.asset']);
Route::get('/accounts/expense', ['uses' => 'AccountController@expense', 'as' => 'accounts.expense']); Route::get('/accounts/expense', ['uses' => 'AccountController@expense', 'as' => 'accounts.expense']);
Route::get('/accounts/revenue', ['uses' => 'AccountController@revenue', 'as' => 'accounts.revenue']); Route::get('/accounts/revenue', ['uses' => 'AccountController@revenue', 'as' => 'accounts.revenue']);
Route::get('/accounts/create/{what}', ['uses' => 'AccountController@create', 'as' => 'accounts.create']) Route::get('/accounts/create/{what}', ['uses' => 'AccountController@create', 'as' => 'accounts.create'])->where('what','revenue|asset|expense');
->where('what','revenue|asset|expense');
Route::get('/accounts/{account}', ['uses' => 'AccountController@show', 'as' => 'accounts.show']); 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}/edit', ['uses' => 'AccountController@edit', 'as' => 'accounts.edit']);
Route::get('/accounts/{account}/delete', ['uses' => 'AccountController@delete', 'as' => 'accounts.delete']); Route::get('/accounts/{account}/delete', ['uses' => 'AccountController@delete', 'as' => 'accounts.delete']);

View File

@ -28,7 +28,21 @@
@if($errors->has('name')) @if($errors->has('name'))
<p class="text-danger">{{$errors->first('name')}}</p> <p class="text-danger">{{$errors->first('name')}}</p>
@else @else
<span class="help-block">Use something descriptive such as "checking account" or "My Bank Main Account".</span> @if($what == 'asset')
<span class="help-block">
Use something descriptive such as "checking account" or "My Bank Main Account".
</span>
@endif
@if($what == 'expense')
<span class="help-block">
Use something descriptive such as "Albert Heijn" or "Amazon".
</span>
@endif
@if($what == 'revenue')
<span class="help-block">
Use something descriptive such as "my mom" or "my job".
</span>
@endif
@endif @endif
</div> </div>
@ -91,7 +105,7 @@
<div class="form-group"> <div class="form-group">
<div class="col-sm-offset-4 col-sm-8"> <div class="col-sm-offset-4 col-sm-8">
<button type="submit" class="btn btn-default btn-success">Create the account</button> <button type="submit" class="btn btn-default btn-success">Create the {{{$what}}} account</button>
</div> </div>
</div> </div>
</div> </div>

View File

@ -29,7 +29,7 @@
</div> </div>
<div class="col-lg-6 col-md-6 col-sm-12"> <div class="col-lg-6 col-md-6 col-sm-12">
@if($account->accounttype->type == 'Default account') @if($account->accounttype->type == 'Default account' || $account->accounttype->type == 'Asset account')
<h4>Optional fields</h4> <h4>Optional fields</h4>
<div class="form-group"> <div class="form-group">
@ -58,7 +58,7 @@
@if(!is_null($openingBalance)) @if(!is_null($openingBalance))
{{ Form::input('date','openingbalancedate', Input::old('openingbalancedate') ?: $openingBalance->date->format('Y-m-d'), ['class' => 'form-control']) }} {{ Form::input('date','openingbalancedate', Input::old('openingbalancedate') ?: $openingBalance->date->format('Y-m-d'), ['class' => 'form-control']) }}
@else @else
{{ Form::input('date','openingbalancedate', Input::old('openingbalancedate') ?: date('Y-m-d'), ['class' => 'form-control']) }} {{ Form::input('date','openingbalancedate', Input::old('openingbalancedate') ?: '', ['class' => 'form-control']) }}
@endif @endif
@if($errors->has('openingbalancedate')) @if($errors->has('openingbalancedate'))
<p class="text-danger">{{$errors->first('openingbalancedate')}}</p> <p class="text-danger">{{$errors->first('openingbalancedate')}}</p>