From 2f5afc80a37895dde190c03827a66b87dfdf7a99 Mon Sep 17 00:00:00 2001
From: James Cole
Date: Sun, 6 Jul 2014 21:07:52 +0200
Subject: [PATCH] More extensions and views. First home view is semi-complete,
time to write some tests again.
---
app/controllers/AccountController.php | 53 ++++++++++---------
app/controllers/ChartController.php | 10 +++-
app/controllers/HomeController.php | 10 +++-
.../Account/AccountRepositoryInterface.php | 1 +
.../Account/EloquentAccountRepository.php | 5 ++
.../Storage/StorageServiceProvider.php | 8 ++-
.../EloquentTransactionRepository.php | 8 +++
.../TransactionRepositoryInterface.php | 10 ++++
.../EloquentTransactionJournalRepository.php | 48 ++++++++++++++---
.../TransactionJournalRepositoryInterface.php | 7 ++-
app/routes.php | 10 ----
.../controllers/AccountControllerTest.php | 37 ++++++++++++-
app/views/index.blade.php | 16 +++++-
app/views/transactions/journals.blade.php | 34 ++++++++++++
14 files changed, 206 insertions(+), 51 deletions(-)
create mode 100644 app/lib/Firefly/Storage/Transaction/EloquentTransactionRepository.php
create mode 100644 app/lib/Firefly/Storage/Transaction/TransactionRepositoryInterface.php
create mode 100644 app/views/transactions/journals.blade.php
diff --git a/app/controllers/AccountController.php b/app/controllers/AccountController.php
index 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 \BaseController
break;
}
+ // @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.php
index 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.php
index 73bfe158b7..7a2ed4a2a7 100644
--- a/app/controllers/HomeController.php
+++ b/app/controllers/HomeController.php
@@ -1,17 +1,20 @@
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.php
index c9a19d0c89..3976ba7cd4 100644
--- a/app/lib/Firefly/Storage/Account/AccountRepositoryInterface.php
+++ b/app/lib/Firefly/Storage/Account/AccountRepositoryInterface.php
@@ -10,6 +10,7 @@ interface AccountRepositoryInterface
public 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.php
index 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 AccountRepositoryInterface
return \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.php
index 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 Laravel
public 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.php
new file mode 100644
index 0000000000..7b0ecf6796
--- /dev/null
+++ b/app/lib/Firefly/Storage/Transaction/EloquentTransactionRepository.php
@@ -0,0 +1,8 @@
+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 file
diff --git a/app/lib/Firefly/Storage/TransactionJournal/TransactionJournalRepositoryInterface.php b/app/lib/Firefly/Storage/TransactionJournal/TransactionJournalRepositoryInterface.php
index 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 file
diff --git a/app/routes.php b/app/routes.php
index 29a33d3755..c5fa5b13b6 100644
--- a/app/routes.php
+++ b/app/routes.php
@@ -1,14 +1,4 @@
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.php
index a31803a612..e81b462161 100644
--- a/app/tests/controllers/AccountControllerTest.php
+++ b/app/tests/controllers/AccountControllerTest.php
@@ -7,6 +7,33 @@ class AccountControllerTest extends TestCase
parent::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 TestCase
public 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 file
diff --git a/app/views/index.blade.php b/app/views/index.blade.php
index 62b24b253e..a2596ab4b7 100644
--- a/app/views/index.blade.php
+++ b/app/views/index.blade.php
@@ -32,9 +32,11 @@
@else
+
@foreach($accounts as $index => $account)
@if($index % 2 == 1)
-
+
@endif
@endforeach
+
+
+ @foreach($accounts as $index => $account)
+
+
{{$account->name}}
+ @include('transactions.journals',['journals' => $account->transactionList])
+
+ @if($index % 2 == 1)
+
+ @endif
+ @endforeach
+
@endif
diff --git a/app/views/transactions/journals.blade.php b/app/views/transactions/journals.blade.php
new file mode 100644
index 0000000000..95bb0d189d
--- /dev/null
+++ b/app/views/transactions/journals.blade.php
@@ -0,0 +1,34 @@
+
+
+ | |
+ Description |
+ Date |
+ Amount |
+
+@foreach($account->transactionList as $journal)
+
+
+ |
+ @if($journal->transactiontype->type == 'Withdrawal')
+
+ @endif
+ @if($journal->transactiontype->type == 'Deposit')
+
+ @endif
+ @if($journal->transactiontype->type == 'Transfer')
+
+ @endif
+
+ |
+ {{{$journal->description}}} |
+ {{$journal->date->format('jS M Y')}} |
+
+ @foreach($journal->transactions as $t)
+ @if($t->account_id == $account->id)
+ {{mf($t->amount)}}
+ @endif
+ @endforeach
+ |
+
+@endforeach
+
\ No newline at end of file