More extensions and views. First home view is semi-complete, time to write some tests again.

This commit is contained in:
James Cole 2014-07-06 21:07:52 +02:00
parent 4192f2bc8f
commit 2f5afc80a3
14 changed files with 206 additions and 51 deletions

View File

@ -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)
{
}
}
//
//
// /**

View File

@ -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);
}

View File

@ -1,17 +1,20 @@
<?php
use 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);

View File

@ -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();

View File

@ -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();

View File

@ -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'

View File

@ -0,0 +1,8 @@
<?php
namespace Firefly\Storage\Transaction;
class EloquentTransactionRepository implements TransactionRepositoryInterface {
}

View File

@ -0,0 +1,10 @@
<?php
namespace Firefly\Storage\Transaction;
interface TransactionRepositoryInterface
{
}

View File

@ -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;
}
}
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;
}
}

View File

@ -5,7 +5,10 @@ namespace Firefly\Storage\TransactionJournal;
interface TransactionJournalRepositoryInterface
{
public function createSimpleJournal(\Account $from, \Account $to, $description, $amount, \Carbon\Carbon $date);
}
public function get();
public function getByAccount(\Account $account, $count = 25);
}

View File

@ -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 () {

View File

@ -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();
}
}

View File

@ -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>
@endif

View File

@ -0,0 +1,34 @@
<table class="table table-bordered table-striped table-condensed">
<tr>
<th>&nbsp;</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>