From d296dbbc23c5f363d7e1d8bf555e9165fc2a6099 Mon Sep 17 00:00:00 2001 From: James Cole Date: Thu, 11 Sep 2014 21:58:51 +0200 Subject: [PATCH] Cleanup account views, controllers and repositories. --- app/controllers/AccountController.php | 120 +++++-- .../Account/AccountRepositoryInterface.php | 10 + .../Account/EloquentAccountRepository.php | 299 +++++++++--------- app/views/accounts/asset.blade.php | 29 ++ app/views/accounts/create.blade.php | 19 +- app/views/accounts/delete.blade.php | 13 +- app/views/accounts/edit.blade.php | 6 +- app/views/accounts/expense.blade.php | 28 ++ app/views/accounts/revenue.blade.php | 28 ++ app/views/accounts/show.blade.php | 10 - 10 files changed, 350 insertions(+), 212 deletions(-) create mode 100644 app/views/accounts/asset.blade.php create mode 100644 app/views/accounts/expense.blade.php create mode 100644 app/views/accounts/revenue.blade.php diff --git a/app/controllers/AccountController.php b/app/controllers/AccountController.php index f63225f2cb..13bc809313 100644 --- a/app/controllers/AccountController.php +++ b/app/controllers/AccountController.php @@ -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(); } } diff --git a/app/lib/Firefly/Storage/Account/AccountRepositoryInterface.php b/app/lib/Firefly/Storage/Account/AccountRepositoryInterface.php index 296e0bd613..576b51fad1 100644 --- a/app/lib/Firefly/Storage/Account/AccountRepositoryInterface.php +++ b/app/lib/Firefly/Storage/Account/AccountRepositoryInterface.php @@ -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 * diff --git a/app/lib/Firefly/Storage/Account/EloquentAccountRepository.php b/app/lib/Firefly/Storage/Account/EloquentAccountRepository.php index f6d4fed6d4..87167e1ab5 100644 --- a/app/lib/Firefly/Storage/Account/EloquentAccountRepository.php +++ b/app/lib/Firefly/Storage/Account/EloquentAccountRepository.php @@ -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; - } - } \ No newline at end of file diff --git a/app/views/accounts/asset.blade.php b/app/views/accounts/asset.blade.php new file mode 100644 index 0000000000..6a580f2456 --- /dev/null +++ b/app/views/accounts/asset.blade.php @@ -0,0 +1,29 @@ +@extends('layouts.default') +@section('content') +
+
+

+ Accounts are your personal accounts that represent value. +

+

+ "Asset accounts" are your personal accounts that represent value. For example: bank accounts, saving + accounts, stock, etc. +

+
+
+
+
+

+ Create a new asset account +

+ @if(count($accounts) > 0) + @include('accounts.list') +

+ Create a new asset account +

+ @endif +
+ +
+ +@stop \ No newline at end of file diff --git a/app/views/accounts/create.blade.php b/app/views/accounts/create.blade.php index 1ed64521d1..5254e1b914 100644 --- a/app/views/accounts/create.blade.php +++ b/app/views/accounts/create.blade.php @@ -2,32 +2,21 @@ @section('content')
-

Firefly - Add a new personal account -

-

- Accounts are the record holders for transactions and transfers. Money moves - from one account to another. + Something about accounts.

- 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). -

- -

This form creates personal accounts only. - 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!

{{Form::open(['class' => 'form-horizontal','route' => 'accounts.store'])}} +{{Form::hidden('what',$what)}}

Mandatory fields

@@ -47,6 +36,7 @@
+ @if($what == 'asset')

Optional fields

@@ -76,6 +66,7 @@ @endif
+ @endif
diff --git a/app/views/accounts/delete.blade.php b/app/views/accounts/delete.blade.php index 6b939dab48..7174cb310f 100644 --- a/app/views/accounts/delete.blade.php +++ b/app/views/accounts/delete.blade.php @@ -2,9 +2,6 @@ @section('content')
-

Firefly - Delete "{{{$account->name}}}" -

Remember that deleting something is permanent.

@@ -33,7 +30,15 @@
- Cancel + @if($account->accountType->type == 'Asset account' || $account->accountType->type == 'Default account') + Cancel + @endif + @if($account->accountType->type == 'Expense account' || $account->accountType->type == 'Beneficiary account') + Cancel + @endif + @if($account->accountType->type == 'Revenue account') + Cancel + @endif
diff --git a/app/views/accounts/edit.blade.php b/app/views/accounts/edit.blade.php index 2c9f429eb3..54deee2c82 100644 --- a/app/views/accounts/edit.blade.php +++ b/app/views/accounts/edit.blade.php @@ -2,12 +2,8 @@ @section('content')
-

Firefly - Edit "{{{$account->name}}}" -

- Accounts are the record holders for transactions and transfers. Money moves - from one account to another. + Bla text here.

diff --git a/app/views/accounts/expense.blade.php b/app/views/accounts/expense.blade.php new file mode 100644 index 0000000000..d64ef24f63 --- /dev/null +++ b/app/views/accounts/expense.blade.php @@ -0,0 +1,28 @@ +@extends('layouts.default') +@section('content') +
+
+

+ Bla bla expense +

+

+ Bla bla bla expense +

+
+
+
+
+

+ Create a new expense account +

+ @if(count($accounts) > 0) + @include('accounts.list') +

+ Create a new expense account +

+ @endif +
+ +
+ +@stop \ No newline at end of file diff --git a/app/views/accounts/revenue.blade.php b/app/views/accounts/revenue.blade.php new file mode 100644 index 0000000000..d32e2e5655 --- /dev/null +++ b/app/views/accounts/revenue.blade.php @@ -0,0 +1,28 @@ +@extends('layouts.default') +@section('content') +
+
+

+ Bla bla revenue +

+

+ Bla bla bla revenue +

+
+
+
+
+

+ Create a new revenue account +

+ @if(count($accounts) > 0) + @include('accounts.list') +

+ Create a new revenue account +

+ @endif +
+ +
+ +@stop \ No newline at end of file diff --git a/app/views/accounts/show.blade.php b/app/views/accounts/show.blade.php index 99aad190fd..1eba856f36 100644 --- a/app/views/accounts/show.blade.php +++ b/app/views/accounts/show.blade.php @@ -1,15 +1,5 @@ @extends('layouts.default') @section('content') -
-
-

Firefly - Overview for account "{{{$account->name}}}" -

-
-
- -@include('partials.date_nav') -