Firefly III: a personal finances manager
PHP accountingbudgetbudgetingbudgetscash-flowcashflowcredit-carddockerexpensesfinancefinancesfinanciallinuxmoneypaycheckpersonal-financephpphp7
More extensions and views. First home view is semi-complete, time to write some tests again.
James Cole2f5afc80a378
diff --git a/app/controllers/AccountController.php b/app/controllers/AccountController.phpindex 9b50a3cae6..d4124521e6 100644--- a/app/controllers/AccountController.php+++ b/app/controllers/AccountController.php@@ -5,28 +5,31 @@ use Firefly\Storage\Account\AccountRepositoryInterface as ARI;class AccountController extends \BaseController{- public function __construct(ARI $accounts) {+ public function __construct(ARI $accounts)+ {$this->accounts = $accounts;View::share('menu', 'accounts');}- /**- * Display a listing of the resource.- *- * @return Response- */- public function index()- {++ /**+ * Display a listing of the resource.+ *+ * @return Response+ */+ public function index()+ {$all = $this->accounts->get();$list = [- 'personal' => [],+ 'personal' => [],'beneficiaries' => [],- 'initial' => [],- 'cash' => []+ 'initial' => [],+ 'cash' => []];- foreach($all as $account) {- switch($account->accounttype->description) {+ foreach ($all as $account) {+ // @codeCoverageIgnoreStart+ switch ($account->accounttype->description) {case 'Default account':$list['personal'][] = $account;break;@@ -41,10 +44,11 @@ class AccountController extends \BaseControllerbreak;}+ // @codeCoverageIgnoreEnd}- return View::make('accounts.index')->with('accounts',$list);- }+ return View::make('accounts.index')->with('accounts', $list);+ }/////**@@ -73,16 +77,17 @@ class AccountController extends \BaseController// }////- /**- * Display the specified resource.- *- * @param Account $account- * @return Response- */- public function show(Account $account)- {+ /**+ * Display the specified resource.+ *+ * @param int $id+ *+ * @return Response+ */+ public function show($id)+ {- }+ }////// /**diff --git a/app/controllers/ChartController.php b/app/controllers/ChartController.phpindex 0ac496102b..c3d412184c 100644--- a/app/controllers/ChartController.php+++ b/app/controllers/ChartController.php@@ -6,6 +6,8 @@ use Firefly\Storage\Account\AccountRepositoryInterface as ARI;class ChartController extends BaseController{+ protected $accounts;+public function __construct(ARI $accounts){$this->accounts = $accounts;@@ -14,7 +16,7 @@ class ChartController extends BaseController/*** Show home charts.*/- public function home(Account $account = null)+ public function home($account = null){// chart$chart = App::make('gchart');@@ -45,9 +47,13 @@ class ChartController extends BaseController$chart->addRowArray($row);}} else {+ $account = $this->accounts->find($account);+ if (is_null($account)) {+ return View::make('error')->with('message', 'No account found.');+ }$chart->addColumn($account->name, 'number');while ($current <= $today) {- $row = [clone $current,$account->balance(clone $current)];+ $row = [clone $current, $account->balance(clone $current)];$current->addDay();$chart->addRowArray($row);}diff --git a/app/controllers/HomeController.php b/app/controllers/HomeController.phpindex 73bfe158b7..7a2ed4a2a7 100644--- a/app/controllers/HomeController.php+++ b/app/controllers/HomeController.php@@ -1,17 +1,20 @@<?phpuse Firefly\Helper\Preferences\PreferencesHelperInterface as PHI;use Firefly\Storage\Account\AccountRepositoryInterface as ARI;+use Firefly\Storage\TransactionJournal\TransactionJournalRepositoryInterface as TJRI;class HomeController extends BaseController{protected $accounts;protected $preferences;+ protected $tj;- public function __construct(ARI $accounts, PHI $preferences)+ public function __construct(ARI $accounts, PHI $preferences, TJRI $tj){$this->accounts = $accounts;$this->preferences = $preferences;+ $this->tj = $tj;View::share('menu', 'home');}@@ -28,6 +31,11 @@ class HomeController extends BaseController$list = $this->accounts->getByIds($pref->data);}+ // get transactions for each account:+ foreach ($list as $account) {+ $account->transactionList = $this->tj->getByAccount($account,10);+ }+// build the home screen:return View::make('index')->with('count', $count)->with('accounts', $list);diff --git a/app/lib/Firefly/Storage/Account/AccountRepositoryInterface.php b/app/lib/Firefly/Storage/Account/AccountRepositoryInterface.phpindex c9a19d0c89..3976ba7cd4 100644--- a/app/lib/Firefly/Storage/Account/AccountRepositoryInterface.php+++ b/app/lib/Firefly/Storage/Account/AccountRepositoryInterface.php@@ -10,6 +10,7 @@ interface AccountRepositoryInterfacepublic function count();public function get();+ public function find($id);public function getByIds($ids);public function getDefault();public function getActiveDefault();diff --git a/app/lib/Firefly/Storage/Account/EloquentAccountRepository.php b/app/lib/Firefly/Storage/Account/EloquentAccountRepository.phpindex e95f74ed94..8731882f8f 100644--- a/app/lib/Firefly/Storage/Account/EloquentAccountRepository.php+++ b/app/lib/Firefly/Storage/Account/EloquentAccountRepository.php@@ -16,6 +16,11 @@ class EloquentAccountRepository implements AccountRepositoryInterfacereturn \Auth::user()->accounts()->with('accounttype')->get();}+ public function find($id)+ {+ return \Auth::user()->accounts()->where('id', $id)->first();+ }+public function getByIds($ids){return \Auth::user()->accounts()->with('accounttype')->whereIn('id', $ids)->get();diff --git a/app/lib/Firefly/Storage/StorageServiceProvider.php b/app/lib/Firefly/Storage/StorageServiceProvider.phpindex e2c9d326c4..b551b8eb34 100644--- a/app/lib/Firefly/Storage/StorageServiceProvider.php+++ b/app/lib/Firefly/Storage/StorageServiceProvider.php@@ -10,11 +10,16 @@ class StorageServiceProvider extends ServiceProvider// Triggered automatically by Laravelpublic function register(){- // storage:$this->app->bind('Firefly\Storage\User\UserRepositoryInterface','Firefly\Storage\User\EloquentUserRepository');+ $this->app->bind(+ 'Firefly\Storage\Transaction\TransactionRepositoryInterface',+ 'Firefly\Storage\Transaction\EloquentTransactionRepository'+ );++$this->app->bind('Firefly\Storage\Account\AccountRepositoryInterface',@@ -24,6 +29,7 @@ class StorageServiceProvider extends ServiceProvider'Firefly\Storage\TransactionJournal\TransactionJournalRepositoryInterface','Firefly\Storage\TransactionJournal\EloquentTransactionJournalRepository');+$this->app->bind('Firefly\Storage\Component\ComponentRepositoryInterface','Firefly\Storage\Component\EloquentComponentRepository'diff --git a/app/lib/Firefly/Storage/Transaction/EloquentTransactionRepository.php b/app/lib/Firefly/Storage/Transaction/EloquentTransactionRepository.phpnew file mode 100644index 0000000000..7b0ecf6796--- /dev/null+++ b/app/lib/Firefly/Storage/Transaction/EloquentTransactionRepository.php@@ -0,0 +1,8 @@+<?php++namespace Firefly\Storage\Transaction;+++class EloquentTransactionRepository implements TransactionRepositoryInterface {++}\ No newline at end of filediff --git a/app/lib/Firefly/Storage/Transaction/TransactionRepositoryInterface.php b/app/lib/Firefly/Storage/Transaction/TransactionRepositoryInterface.phpnew file mode 100644index 0000000000..a597deaf9d--- /dev/null+++ b/app/lib/Firefly/Storage/Transaction/TransactionRepositoryInterface.php@@ -0,0 +1,10 @@+<?php++namespace Firefly\Storage\Transaction;+++interface TransactionRepositoryInterface+{+++}\ No newline at end of filediff --git a/app/lib/Firefly/Storage/TransactionJournal/EloquentTransactionJournalRepository.php b/app/lib/Firefly/Storage/TransactionJournal/EloquentTransactionJournalRepository.phpindex 51f773a4f1..6beb5f255f 100644--- a/app/lib/Firefly/Storage/TransactionJournal/EloquentTransactionJournalRepository.php+++ b/app/lib/Firefly/Storage/TransactionJournal/EloquentTransactionJournalRepository.php@@ -43,24 +43,27 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito$journalType = null;switch (true) {- // is withdrawal from one of your own accounts:- case ($fromAT == 'Default account'):- $journalType = \TransactionType::where('type', 'Withdrawal')->first();+ case ($from->transactions()->count() == 0 && $to->transactions()->count() == 0):+ $journalType = \TransactionType::where('type', 'Opening balance')->first();break;+// both are yours:case ($fromAT == 'Default account' && $toAT == 'Default account'):// determin transaction type. If both accounts are new, it's an initial// balance transfer.$journalType = \TransactionType::where('type', 'Transfer')->first();break;- case ($from->transactions()->count() == 0 && $to->transactions()->count() == 0):- $journalType = \TransactionType::where('type', 'Opening balance')->first();+ case ($amount < 0):+ $journalType = \TransactionType::where('type', 'Deposit')->first();break;- default:- // is deposit into one of your own accounts:+ // is deposit into one of your own accounts:case ($toAT == 'Default account'):$journalType = \TransactionType::where('type', 'Deposit')->first();break;+ // is withdrawal from one of your own accounts:+ case ($fromAT == 'Default account'):+ $journalType = \TransactionType::where('type', 'Withdrawal')->first();+ break;}// some debug information:@@ -126,4 +129,33 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito$journal->save();return $journal;}-}\ No newline at end of file++ public function get()+ {++ }++ public function getByAccount(\Account $account, $count = 25)+ {+ $accountID = $account->id;+ $query = \TransactionJournal::+ with(+ [+ 'transactions',+ 'transactioncurrency',+ 'transactiontype'+ ]+ )+ ->take($count)+ ->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')+ ->leftJoin('accounts', 'accounts.id', '=', 'transactions.account_id')+ ->where('accounts.id', $accountID)+ ->orderBy('transaction_journals.date', 'DESC')+ ->orderBy('transaction_journals.id', 'DESC')+ ->take($count)+ ->get(['transaction_journals.*']);+ return $query;+ }+++}\ No newline at end of filediff --git a/app/lib/Firefly/Storage/TransactionJournal/TransactionJournalRepositoryInterface.php b/app/lib/Firefly/Storage/TransactionJournal/TransactionJournalRepositoryInterface.phpindex f16fb8b364..350c189388 100644--- a/app/lib/Firefly/Storage/TransactionJournal/TransactionJournalRepositoryInterface.php+++ b/app/lib/Firefly/Storage/TransactionJournal/TransactionJournalRepositoryInterface.php@@ -5,7 +5,10 @@ namespace Firefly\Storage\TransactionJournal;interface TransactionJournalRepositoryInterface{-public function createSimpleJournal(\Account $from, \Account $to, $description, $amount, \Carbon\Carbon $date);-}\ No newline at end of file+ public function get();++ public function getByAccount(\Account $account, $count = 25);++}\ No newline at end of filediff --git a/app/routes.php b/app/routes.phpindex 29a33d3755..c5fa5b13b6 100644--- a/app/routes.php+++ b/app/routes.php@@ -1,14 +1,4 @@<?php-Route::bind('account', function ($value, $route) {- if(Auth::user()) {- return Auth::user()->accounts()->find($value);- } else {- return null;- }- });---// protected routes:Route::group(['before' => 'auth'], function () {diff --git a/app/tests/controllers/AccountControllerTest.php b/app/tests/controllers/AccountControllerTest.phpindex a31803a612..e81b462161 100644--- a/app/tests/controllers/AccountControllerTest.php+++ b/app/tests/controllers/AccountControllerTest.php@@ -7,6 +7,33 @@ class AccountControllerTest extends TestCaseparent::setUp();}+ public function testIndex() {++ $list = [+ 'personal' => [],+ 'beneficiaries' => [],+ 'initial' => [],+ 'cash' => []+ ];+++ // mock:+ View::shouldReceive('share');+ View::shouldReceive('make')->with('accounts.index')->once()->andReturn(\Mockery::self())+ ->shouldReceive('with')->once()->with('accounts',$list);++ // mock account repository:+ $accounts = $this->mock('Firefly\Storage\Account\AccountRepositoryInterface');+ $accounts->shouldReceive('get')->andReturn([]);++ // call+ $this->call('GET', '/accounts');++ // test+ $this->assertResponseOk();++ }+public function testCreate(){// mock:@@ -22,8 +49,14 @@ class AccountControllerTest extends TestCasepublic function testShow(){+ // mock account repository:+ $accounts = $this->mock('Firefly\Storage\Account\AccountRepositoryInterface');+ $accounts->shouldReceive('get')->with(1)->andReturn([]);- // the route filters on accounts using Eloquent, maybe fix that instead?- $this->assertTrue(true);+ // call+ $this->call('GET', '/accounts/1');++ // test+ $this->assertResponseOk();}}\ No newline at end of filediff --git a/app/views/index.blade.php b/app/views/index.blade.phpindex 62b24b253e..a2596ab4b7 100644--- a/app/views/index.blade.php+++ b/app/views/index.blade.php@@ -32,9 +32,11 @@</p></div>@else+ <!-- ACCOUNTS --><div class="row" style="border-top:1px #eee solid;">@foreach($accounts as $index => $account)<div class="col-lg-6">+ <h4>{{{$account->name}}} chart</h4><div id="chart_{{{$account->id}}}" data-id="{{{$account->id}}}" class="homeChart" data-title="{{{$account->name}}}"></div><p>Go to <a href="#" title="Overview for {{{$account->name}}}">{{{$account->name}}}</a>@@ -42,11 +44,23 @@</div>@if($index % 2 == 1)- </div><div class="row" style="border-top:1px #eee solid;">+ </div><div class="row">@endif@endforeach</div>+ <!-- TRANSACTIONS -->+ <div class="row">+ @foreach($accounts as $index => $account)+ <div class="col-lg-6">+ <h4>{{$account->name}}</h4>+ @include('transactions.journals',['journals' => $account->transactionList])+ </div>+ @if($index % 2 == 1)+ </div><div class="row" style="border-top:1px #eee solid;">+ @endif+ @endforeach+ </div>@endifdiff --git a/app/views/transactions/journals.blade.php b/app/views/transactions/journals.blade.phpnew file mode 100644index 0000000000..95bb0d189d--- /dev/null+++ b/app/views/transactions/journals.blade.php@@ -0,0 +1,34 @@+<table class="table table-bordered table-striped table-condensed">+ <tr>+ <th> </th>+ <th>Description</th>+ <th>Date</th>+ <th>Amount</th>+ </tr>+@foreach($account->transactionList as $journal)+ <tr>++ <td>+ @if($journal->transactiontype->type == 'Withdrawal')+ <span class="glyphicon glyphicon-arrow-left" title="Withdrawal"></span>+ @endif+ @if($journal->transactiontype->type == 'Deposit')+ <span class="glyphicon glyphicon-arrow-right" title="Deposit"></span>+ @endif+ @if($journal->transactiontype->type == 'Transfer')+ <span class="glyphicon glyphicon-resize-full" title="Transfer"></span>+ @endif++ </td>+ <td><a href="#">{{{$journal->description}}}</a></td>+ <td>{{$journal->date->format('jS M Y')}}</td>+ <td>+ @foreach($journal->transactions as $t)+ @if($t->account_id == $account->id)+ {{mf($t->amount)}}+ @endif+ @endforeach+ </td>+ </tr>+@endforeach+</table>\ No newline at end of file