Cleanup account views, controllers and repositories.

This commit is contained in:
James Cole 2014-09-11 21:58:51 +02:00
parent 9bcd27b847
commit d296dbbc23
10 changed files with 350 additions and 212 deletions

View File

@ -27,30 +27,47 @@ class AccountController extends \BaseController
/**
* @return \Illuminate\View\View
*/
public function create()
public function create($what)
{
return View::make('accounts.create')->with('title', 'Create account');
return View::make('accounts.create')->with('title', 'Accounts')->with(
'subTitle', 'Create a new ' . $what . ' account'
)->with('what', $what);
}
/**
* @return $this
*/
public function asset() {
return View::make('accounts.asset')->with('title','Accounts')->with('subTitle','Asset accounts');
public function asset()
{
$accounts = $this->_repository->getOfTypes(['Asset account', 'Default account']);
return View::make('accounts.asset')->with('title', 'Accounts')->with('subTitle', 'Asset accounts')->with(
'accounts', $accounts
);
}
/**
* @return $this
*/
public function expense() {
return View::make('accounts.expense')->with('title','Accounts')->with('subTitle','Expense accounts');
public function expense()
{
$accounts = $this->_repository->getOfTypes(['Expense account', 'Beneficiary account']);
return View::make('accounts.expense')->with('title', 'Accounts')->with('subTitle', 'Expense accounts')->with(
'accounts', $accounts
);
}
/**
* @return $this
*/
public function revenue() {
return View::make('accounts.revenue')->with('title','Accounts')->with('subTitle','Revenue accounts');
public function revenue()
{
$accounts = $this->_repository->getOfTypes(['Revenue account']);
return View::make('accounts.revenue')->with('title', 'Accounts')->with('subTitle', 'Revenue accounts')->with(
'accounts', $accounts
);
}
/**
@ -61,7 +78,9 @@ class AccountController extends \BaseController
public function delete(Account $account)
{
return View::make('accounts.delete')->with('account', $account)
->with('title', 'Delete account "' . $account->name . '"');
->with('title', 'Accounts')->with(
'subTitle', 'Delete ' . strtolower($account->accountType->type) . ' "' . $account->name . '"'
);
}
/**
@ -71,11 +90,23 @@ class AccountController extends \BaseController
*/
public function destroy(Account $account)
{
$type = $account->accountType->type;
$this->_repository->destroy($account);
Session::flash('success', 'The account was deleted.');
switch ($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');
}
@ -88,7 +119,8 @@ class AccountController extends \BaseController
{
$openingBalance = $this->_accounts->openingBalanceTransaction($account);
return View::make('accounts.edit')->with('account', $account)->with('openingBalance', $openingBalance)
->with('title', 'Edit account "' . $account->name . '"');
->with('title','Accounts')
->with('subTitle', 'Edit '.strtolower($account->accountType->type).' "' . $account->name . '"');
}
/**
@ -96,23 +128,24 @@ class AccountController extends \BaseController
*/
public function index()
{
$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');
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');
}
/**
@ -125,9 +158,9 @@ class AccountController extends \BaseController
$data = $this->_accounts->show($account, 40);
return View::make('accounts.show')->with('account', $account)->with('show', $data)->with(
'title',
'Details for account "' . $account->name . '"'
);
'subTitle',
'Details for '.strtolower($account->accountType->type).' "' . $account->name . '"'
)->with('title','Accounts');
}
/**
@ -136,13 +169,30 @@ class AccountController extends \BaseController
public function store()
{
$account = $this->_repository->store(Input::all());
$data = Input::all();
$data['what'] = isset($data['what']) && $data['what'] != '' ? $data['what'] : 'asset';
switch ($data['what']) {
default:
case 'asset':
$data['account_type'] = 'Asset account';
break;
case 'expense':
$data['account_type'] = 'Expense account';
break;
case 'revenue':
$data['account_type'] = 'Revenue account';
break;
}
$account = $this->_repository->store($data);
if ($account->validate()) {
// saved! return to wherever.
Session::flash('success', 'Account "' . $account->name . '" created!');
if (intval(Input::get('create')) === 1) {
return Redirect::route('accounts.create')->withInput();
return Redirect::route('accounts.create', $data['what'])->withInput();
} else {
return Redirect::route('accounts.index');
}
@ -150,7 +200,7 @@ class AccountController extends \BaseController
// did not save, return with error:
Session::flash('error', 'Could not save the new account: ' . $account->errors()->first());
return Redirect::route('accounts.create')->withErrors($account->errors())->withInput();
return Redirect::route('accounts.create', $data['what'])->withErrors($account->errors())->withInput();
}
}

View File

@ -24,6 +24,16 @@ interface AccountRepositoryInterface
*/
public function createOrFind($name, \AccountType $type);
/**
* Gets a list of accounts that have the mentioned type. Will automatically convert
* strings in this array to actual (model) account types.
*
* @param array $types
*
* @return Collection
*/
public function getOfTypes(array $types);
/**
* @param $name
*

View File

@ -23,6 +23,20 @@ class EloquentAccountRepository implements AccountRepositoryInterface
$this->_user = \Auth::user();
}
/**
* Gets a list of accounts that have the mentioned type. Will automatically convert
* strings in this array to actual (model) account types.
*
* @param array $types
*
* @return Collection
*/
public function getOfTypes(array $types)
{
$accounts = $this->_user->accounts()->accountTypeIn($types)->get(['accounts.*']);
return $accounts;
}
/**
* @return mixed
*/
@ -32,6 +46,20 @@ class EloquentAccountRepository implements AccountRepositoryInterface
}
/**
* @param $name
*
* @return \Account|mixed|null
*/
public function createOrFindBeneficiary($name)
{
if (is_null($name) || strlen($name) == 0) {
return null;
}
$type = \AccountType::where('type', 'Expense account')->first();
return $this->createOrFind($name, $type);
}
/**
* @param $name
* @param \AccountType $type
@ -54,17 +82,110 @@ class EloquentAccountRepository implements AccountRepositoryInterface
}
/**
* @param $name
* @param $name
* @param \AccountType $type
*
* @return \Account|mixed|null
* @return mixed
*/
public function createOrFindBeneficiary($name)
public function findByName($name, \AccountType $type = null)
{
if (is_null($name) || strlen($name) == 0) {
return null;
$type = is_null($type) ? \AccountType::where('type', 'Asset account')->first() : $type;
return $this->_user->accounts()->where('account_type_id', $type->id)
->where('name', 'like', '%' . $name . '%')
->first();
}
/**
* @param $data
*
* @return \Account
* @throws \Firefly\Exception\FireflyException
*/
public function store($data)
{
/**
* If the AccountType has been passed through, use it:
*/
if (isset($data['account_type']) && is_object($data['account_type'])
&& get_class($data['account_type']) == 'AccountType'
) {
$accountType = $data['account_type'];
} else if (isset($data['account_type']) && is_string($data['account_type'])) {
// if it isnt but set as string, find it:
$accountType = \AccountType::where('type', $data['account_type'])->first();
} else {
$accountType = \AccountType::where('type', 'Asset account')->first();
}
$type = \AccountType::where('type', 'Beneficiary account')->first();
return $this->createOrFind($name, $type);
/**
* Create new account:
*/
$account = new \Account;
$account->accountType()->associate($accountType);
$account->user()->associate($this->_user);
$account->name = $data['name'];
$account->active
=
isset($data['active']) && intval($data['active']) >= 0 && intval($data['active']) <= 1 ? intval(
$data['active']
) : 1;
// try to save it:
if ($account->save()) {
// create initial balance, if necessary:
if (isset($data['openingbalance']) && isset($data['openingbalancedate'])) {
$amount = floatval($data['openingbalance']);
$date = new Carbon($data['openingbalancedate']);
if ($amount != 0) {
$this->_createInitialBalance($account, $amount, $date);
}
}
}
// whatever the result, return the 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;
}
/**
@ -91,7 +212,7 @@ class EloquentAccountRepository implements AccountRepositoryInterface
if (count($accountIDs) > 0) {
// find the "initial balance" type accounts in this list. Should be just 1.
$query = $this->_user->accounts()->accountTypeIn(['Initial balance account'])
->whereIn('accounts.id', $accountIDs);
->whereIn('accounts.id', $accountIDs);
if ($query->count() == 1) {
$iba = $query->first(['accounts.*']);
$iba->delete();
@ -120,6 +241,7 @@ class EloquentAccountRepository implements AccountRepositoryInterface
/**
* @param $type
*
* @return mixed
*/
public function findAccountType($type)
@ -127,21 +249,6 @@ class EloquentAccountRepository implements AccountRepositoryInterface
return \AccountType::where('type', $type)->first();
}
/**
* @param $name
* @param \AccountType $type
*
* @return mixed
*/
public function findByName($name, \AccountType $type = null)
{
$type = is_null($type) ? \AccountType::where('type', 'Default account')->first() : $type;
return $this->_user->accounts()->where('account_type_id', $type->id)
->where('name', 'like', '%' . $name . '%')
->first();
}
/**
* Used for import
*
@ -152,8 +259,8 @@ class EloquentAccountRepository implements AccountRepositoryInterface
public function findByNameAny($name)
{
return $this->_user->accounts()
->where('name', 'like', '%' . $name . '%')
->first();
->where('name', 'like', '%' . $name . '%')
->first();
}
/**
@ -164,28 +271,12 @@ class EloquentAccountRepository implements AccountRepositoryInterface
return $this->_user->accounts()->with('accounttype')->orderBy('name', 'ASC')->get();
}
/**
* @return mixed
*/
public function getActiveDefault()
{
return $this->_user->accounts()->leftJoin('account_types', 'account_types.id', '=', 'accounts.account_type_id')
->where('account_types.type', 'Default account')->where('accounts.active', 1)
->get(['accounts.*']);
}
/**
* @return array|mixed
*/
public function getActiveDefaultAsSelectList()
{
$list = $this->_user->accounts()->leftJoin(
'account_types', 'account_types.id', '=', 'accounts.account_type_id'
)
->where('account_types.type', 'Default account')->where('accounts.active', 1)
->orderBy('accounts.name', 'ASC')->get(['accounts.*']);
$list = $this->getActiveDefault();
$return = [];
foreach ($list as $entry) {
$return[intval($entry->id)] = $entry->name;
@ -194,17 +285,28 @@ class EloquentAccountRepository implements AccountRepositoryInterface
return $return;
}
/**
* @return mixed
*/
public function getActiveDefault()
{
return $this->_user->accounts()->accountTypeIn(['Default account', 'Asset account'])->where(
'accounts.active', 1
)
->get(['accounts.*']);
}
/**
* @return mixed
*/
public function getBeneficiaries()
{
$list = $this->_user->accounts()->leftJoin(
'account_types', 'account_types.id', '=', 'accounts.account_type_id'
)
->where('account_types.type', 'Beneficiary account')->where('accounts.active', 1)
->orderBy('accounts.name', 'ASC')->get(['accounts.*']);
$list = $this->_user->accounts()->accountTypeIn(['Beneficiary account', 'Expense account'])->where(
'accounts.active', 1
)->orderBy(
'accounts.name', 'ASC'
)->get(['accounts.*']);
return $list;
}
@ -212,7 +314,7 @@ class EloquentAccountRepository implements AccountRepositoryInterface
public function getByAccountType(\AccountType $type)
{
return $this->_user->accounts()->with('accounttype')->orderBy('name', 'ASC')
->where('account_type_id', $type->id)->get();
->where('account_type_id', $type->id)->get();
}
/**
@ -254,14 +356,13 @@ class EloquentAccountRepository implements AccountRepositoryInterface
*/
public function getDefault()
{
return $this->_user->accounts()->leftJoin('account_types', 'account_types.id', '=', 'accounts.account_type_id')
->where('account_types.type', 'Default account')
->orderBy('accounts.name', 'ASC')->get(['accounts.*']);
return $this->_user->accounts()->accountTypeIn(['Default account', 'Asset account'])
->orderBy('accounts.name', 'ASC')->get(['accounts.*']);
}
/**
* @param \User $user
*
* @return mixed|void
*/
public function overruleUser(\User $user)
@ -270,58 +371,6 @@ class EloquentAccountRepository implements AccountRepositoryInterface
return true;
}
/**
* @param $data
*
* @return \Account
* @throws \Firefly\Exception\FireflyException
*/
public function store($data)
{
/**
* If the AccountType has been passed through, use it:
*/
if (isset($data['account_type']) && is_object($data['account_type'])
&& get_class($data['account_type']) == 'AccountType'
) {
$accountType = $data['account_type'];
} else if (isset($data['account_type']) && is_string($data['account_type'])) {
$accountType = \AccountType::where('type', $data['account_type'])->first();
} else {
$accountType = \AccountType::where('type', 'Default account')->first();
}
/**
* Create new account:
*/
$account = new \Account;
$account->accountType()->associate($accountType);
$account->user()->associate($this->_user);
$account->name = $data['name'];
$account->active
= isset($data['active']) && intval($data['active']) >= 0 && intval($data['active']) <= 1 ? intval(
$data['active']
) : 1;
// try to save it:
if ($account->save()) {
// create initial balance, if necessary:
if (isset($data['openingbalance']) && isset($data['openingbalancedate'])) {
$amount = floatval($data['openingbalance']);
$date = new Carbon($data['openingbalancedate']);
if ($amount != 0) {
$this->_createInitialBalance($account, $amount, $date);
}
}
}
// whatever the result, return the account.
return $account;
}
/**
* @param \Account $account
* @param $data
@ -341,7 +390,7 @@ class EloquentAccountRepository implements AccountRepositoryInterface
/** @var \Firefly\Helper\Controllers\AccountInterface $interface */
$interface = \App::make('Firefly\Helper\Controllers\AccountInterface');
if ($account->accounttype->type == 'Default account') {
if ($account->accounttype->type == 'Default account' || $account->accounttype->type == 'Asset account') {
$journal = $interface->openingBalanceTransaction($account);
@ -359,42 +408,4 @@ class EloquentAccountRepository implements AccountRepositoryInterface
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;
}
}

View File

@ -0,0 +1,29 @@
@extends('layouts.default')
@section('content')
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12">
<p class="lead">
Accounts are your personal accounts that represent value.
</p>
<p class="text-info">
"Asset accounts" are your personal accounts that represent value. For example: bank accounts, saving
accounts, stock, etc.
</p>
</div>
</div>
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12">
<p>
<a href="{{route('accounts.create','asset')}}" class="btn btn-success">Create a new asset account</a>
</p>
@if(count($accounts) > 0)
@include('accounts.list')
<p>
<a href="{{route('accounts.create','asset')}}" class="btn btn-success">Create a new asset account</a>
</p>
@endif
</div>
</div>
@stop

View File

@ -2,32 +2,21 @@
@section('content')
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12">
<h1>Firefly
<small>Add a new personal account</small>
</h1>
<p class="lead">
Accounts are the record holders for transactions and transfers. Money moves
from one account to another.
Something about accounts.
</p>
</div>
</div>
<div class="row">
<div class="col-lg-6 col-md-12 col-sm-12">
<p class="text-info">
In a double-entry bookkeeping system (such as this one) there is a "from"-account and a "to"-account,
even when money is created from thin air (such as interest, or when new accounts already have a
positive balance).
</p>
<p class="text-info"><span class="text-danger">This form creates personal accounts only.</span>
If this is your first account, it should be a checking or savings account. Enter its name and if relevant
the current balance. Check your bank statements for the last current balance you can find.
Something about accounts here!
</p>
</div>
</div>
{{Form::open(['class' => 'form-horizontal','route' => 'accounts.store'])}}
{{Form::hidden('what',$what)}}
<div class="row">
<div class="col-lg-6 col-md-6 col-sm-12">
<h4>Mandatory fields</h4>
@ -47,6 +36,7 @@
</div>
<div class="col-lg-6 col-md-6 col-sm-12">
@if($what == 'asset')
<h4>Optional fields</h4>
<div class="form-group">
@ -76,6 +66,7 @@
@endif
</div>
</div>
@endif
</div>

View File

@ -2,9 +2,6 @@
@section('content')
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12">
<h1>Firefly
<small>Delete "{{{$account->name}}}"</small>
</h1>
<p class="lead">
Remember that deleting something is permanent.
</p>
@ -33,7 +30,15 @@
<div class="form-group">
<div class="col-sm-8">
<button type="submit" class="btn btn-default btn-danger">Delete permanently</button>
<a href="{{route('accounts.index')}}" class="btn-default btn">Cancel</a>
@if($account->accountType->type == 'Asset account' || $account->accountType->type == 'Default account')
<a href="{{route('accounts.asset')}}" class="btn-default btn">Cancel</a >
@endif
@if($account->accountType->type == 'Expense account' || $account->accountType->type == 'Beneficiary account')
<a href="{{route('accounts.expense')}}" class="btn-default btn">Cancel</a >
@endif
@if($account->accountType->type == 'Revenue account')
<a href="{{route('accounts.revenue')}}" class="btn-default btn">Cancel</a >
@endif
</div>
</div>
</div>

View File

@ -2,12 +2,8 @@
@section('content')
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12">
<h1>Firefly
<small>Edit "{{{$account->name}}}"</small>
</h1>
<p class="lead">
Accounts are the record holders for transactions and transfers. Money moves
from one account to another.
Bla text here.
</p>
</div>
</div>

View File

@ -0,0 +1,28 @@
@extends('layouts.default')
@section('content')
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12">
<p class="lead">
Bla bla expense
</p>
<p class="text-info">
Bla bla bla expense
</p>
</div>
</div>
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12">
<p>
<a href="{{route('accounts.create','expense')}}" class="btn btn-success">Create a new expense account</a>
</p>
@if(count($accounts) > 0)
@include('accounts.list')
<p>
<a href="{{route('accounts.create','expense')}}" class="btn btn-success">Create a new expense account</a>
</p>
@endif
</div>
</div>
@stop

View File

@ -0,0 +1,28 @@
@extends('layouts.default')
@section('content')
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12">
<p class="lead">
Bla bla revenue
</p>
<p class="text-info">
Bla bla bla revenue
</p>
</div>
</div>
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12">
<p>
<a href="{{route('accounts.create','revenue')}}" class="btn btn-success">Create a new revenue account</a>
</p>
@if(count($accounts) > 0)
@include('accounts.list')
<p>
<a href="{{route('accounts.create','revenue')}}" class="btn btn-success">Create a new revenue account</a>
</p>
@endif
</div>
</div>
@stop

View File

@ -1,15 +1,5 @@
@extends('layouts.default')
@section('content')
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12">
<h1>Firefly
<small>Overview for account "{{{$account->name}}}"</small>
</h1>
</div>
</div>
@include('partials.date_nav')
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12">
<div id="chart"></div>