Added home screen information about piggy banks.

This commit is contained in:
James Cole 2015-04-13 20:43:58 +02:00
parent 9fd8d8915d
commit 721fa04e3c
7 changed files with 178 additions and 76 deletions

View File

@ -1,6 +1,5 @@
<?php namespace FireflyIII\Http\Controllers;
use Auth;
use Carbon\Carbon;
use Config;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
@ -44,24 +43,28 @@ class HomeController extends Controller
}
/**
* @param AccountRepositoryInterface $repository
*
* @return \Illuminate\View\View
*/
public function index(AccountRepositoryInterface $repository)
{
$types = Config::get('firefly.accountTypesByIdentifier.asset');
$count = $repository->countAccounts($types);
$title = 'Firefly';
$subTitle = 'What\'s playing?';
$mainTitleIcon = 'fa-fire';
$transactions = [];
$frontPage = Preferences::get('frontPageAccounts', []);
$start = Session::get('start', Carbon::now()->startOfMonth());
$end = Session::get('end', Carbon::now()->endOfMonth());
$accounts = $repository->getFrontpageAccounts($frontPage);
$savings = $repository->getSavingsAccounts();
$types = Config::get('firefly.accountTypesByIdentifier.asset');
$count = $repository->countAccounts($types);
$title = 'Firefly';
$subTitle = 'What\'s playing?';
$mainTitleIcon = 'fa-fire';
$transactions = [];
$frontPage = Preferences::get('frontPageAccounts', []);
$start = Session::get('start', Carbon::now()->startOfMonth());
$end = Session::get('end', Carbon::now()->endOfMonth());
$accounts = $repository->getFrontpageAccounts($frontPage);
$savings = $repository->getSavingsAccounts();
$piggyBankAccounts = $repository->getPiggyBankAccounts();
$savingsTotal = 0;
$savingsTotal = 0;
foreach ($savings as $savingAccount) {
$savingsTotal += Steam::balance($savingAccount, $end);
}
@ -83,7 +86,7 @@ class HomeController extends Controller
}
}
return view('index', compact('count', 'title', 'savings', 'subTitle', 'mainTitleIcon', 'transactions', 'savingsTotal'));
return view('index', compact('count', 'title', 'savings', 'subTitle', 'mainTitleIcon', 'transactions', 'savingsTotal','piggyBankAccounts'));
}

View File

@ -6,6 +6,7 @@ use App;
use Auth;
use Carbon\Carbon;
use Config;
use DB;
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountMeta;
use FireflyIII\Models\AccountType;
@ -141,13 +142,6 @@ class AccountRepository implements AccountRepositoryInterface
->get(['transaction_journals.*', 'transaction_currencies.symbol', 'transaction_types.type']);
}
/**
* @return float
*/
public function sumOfEverything() {
return floatval(Auth::user()->transactions()->sum('amount'));
}
/**
* @param Account $account
* @param int $page
@ -194,6 +188,51 @@ class AccountRepository implements AccountRepositoryInterface
return null;
}
/**
* Get the accounts of a user that have piggy banks connected to them.
*
* @return Collection
*/
public function getPiggyBankAccounts()
{
$ids = [];
$start = clone Session::get('start', new Carbon);
$end = clone Session::get('end', new Carbon);
$accountIds = DB::table('piggy_banks')->distinct()->get(['piggy_banks.account_id']);
$accounts = new Collection;
foreach ($accountIds as $id) {
$ids[] = intval($id->account_id);
}
$ids = array_unique($ids);
if (count($ids) > 0) {
$accounts = Auth::user()->accounts()->whereIn('id', $ids)->get();
}
$accounts->each(
function (Account $account) use ($start, $end) {
$account->startBalance = Steam::balance($account, $start);
$account->endBalance = Steam::balance($account, $end);
$account->piggyBalance = 0;
/** @var PiggyBank $piggyBank */
foreach ($account->piggyBanks as $piggyBank) {
$account->piggyBalance += $piggyBank->currentRelevantRep()->currentamount;
}
// sum of piggy bank amounts on this account:
// diff between endBalance and piggyBalance.
// then, percentage.
$difference = $account->endBalance - $account->piggyBalance;
$account->difference = $difference;
$account->percentage = $difference != 0 ? round((($difference / $account->endBalance) * 100)) : 100;
}
);
return $accounts;
}
/**
* Get savings accounts and the balance difference in the period.
*
@ -324,6 +363,14 @@ class AccountRepository implements AccountRepositoryInterface
}
/**
* @return float
*/
public function sumOfEverything()
{
return floatval(Auth::user()->transactions()->sum('amount'));
}
/**
* @param Account $account
* @param array $data

View File

@ -51,6 +51,14 @@ interface AccountRepositoryInterface
*/
public function getCreditCards();
/**
* Get the accounts of a user that have piggy banks connected to them.
*
* @return Collection
*/
public function getPiggyBankAccounts();
/**
* Get all transfers TO this account in this range.
*

View File

@ -17,6 +17,7 @@ use Navigation;
class PiggyBankRepository implements PiggyBankRepositoryInterface
{
/**
* @SuppressWarnings("CyclomaticComplexity") // It's exactly 5. So I don't mind.
*
@ -96,6 +97,8 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface
return DB::table('piggy_bank_events')->where('piggy_bank_id', $piggyBank->id)->groupBy('date')->get(['date', DB::Raw('SUM(`amount`) AS `sum`')]);
}
/**
* Set all piggy banks to order 0.
*

View File

@ -14,7 +14,6 @@ use Illuminate\Support\Collection;
interface PiggyBankRepositoryInterface
{
/**
* @SuppressWarnings("CyclomaticComplexity") // It's exactly 5. So I don't mind.
*

View File

@ -52,63 +52,104 @@
<div id="categories-chart"></div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">
<i class="fa fa-line-chart"></i> Savings
<span class="pull-right">{!! Amount::format($savingsTotal) !!}</span>
</div>
<div class="panel-body">
@if(count($savings) == 0)
<p class="small"><em>Mark your asset accounts as "Savings account" to fill this panel.</em></p>
@else
@foreach($savings as $account)
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12"><h5><a href="{{route('accounts.show')}}">{{$account->name}}</a></h5></div>
</div>
<div class="row">
<!-- start -->
<div class="col-lg-2 col-md-2 col-sm-3 col-xs-4">{!! Amount::format($account->startBalance) !!}</div>
<!-- bar -->
<div class="col-lg-8 col-md-8 col-sm-6 col-xs-4">
@if($account->difference < 0)
<!-- green (100-pct), then red (pct) -->
<div class="progress">
<div class="progress-bar progress-bar-success progress-bar-striped" style="width: {{100 - $account->percentage}}%">
@if($account->percentage <= 50)
{{Amount::format($account->difference,false)}}
@endif
</div>
<div class="progress-bar progress-bar-danger progress-bar-striped" style="width: {{$account->percentage}}%">
@if($account->percentage > 50)
{{Amount::format($account->difference,false)}}
@endif
</div>
</div>
@else
<!-- green (pct), then blue (100-pct) -->
<div class="progress">
<div class="progress-bar progress-bar-success progress-bar-striped" style="width: {{$account->percentage}}%">
@if($account->percentage > 50)
{{Amount::format($account->difference,false)}}
@endif
</div>
<div class="progress-bar progress-bar-info progress-bar-striped" style="width: {{100 - $account->percentage}}%">
@if($account->percentage <= 50)
{{Amount::format($account->difference,false)}}
@endif
</div>
</div>
<!-- SAVINGS -->
<div class="panel panel-default">
<div class="panel-heading">
<i class="fa fa-line-chart"></i> Savings
<span class="pull-right">{!! Amount::format($savingsTotal) !!}</span>
</div>
<div class="panel-body">
@if(count($savings) == 0)
<p class="small"><em>Mark your asset accounts as "Savings account" to fill this panel.</em></p>
@else
@foreach($savings as $account)
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12"><h5><a href="{{route('accounts.show')}}">{{$account->name}}</a></h5></div>
</div>
<div class="row">
<!-- start -->
<div class="col-lg-2 col-md-2 col-sm-3 col-xs-4">{!! Amount::format($account->startBalance) !!}</div>
<!-- bar -->
<div class="col-lg-8 col-md-8 col-sm-6 col-xs-4">
@if($account->difference < 0)
<!-- green (100-pct), then red (pct) -->
<div class="progress">
<div class="progress-bar progress-bar-success progress-bar-striped" style="width: {{100 - $account->percentage}}%">
@if($account->percentage <= 50)
{{Amount::format($account->difference,false)}}
@endif
</div>
<div class="progress-bar progress-bar-danger progress-bar-striped" style="width: {{$account->percentage}}%">
@if($account->percentage > 50)
{{Amount::format($account->difference,false)}}
@endif
</div>
</div>
@else
<!-- green (pct), then blue (100-pct) -->
<div class="progress">
<div class="progress-bar progress-bar-success progress-bar-striped" style="width: {{$account->percentage}}%">
@if($account->percentage > 50)
{{Amount::format($account->difference,false)}}
@endif
</div>
<div class="progress-bar progress-bar-info progress-bar-striped" style="width: {{100 - $account->percentage}}%">
@if($account->percentage <= 50)
{{Amount::format($account->difference,false)}}
@endif
</div>
</div>
@endif
</div>
<!-- end -->
<div class="col-lg-2 col-md-2 col-sm-3 col-xs-4">{!! Amount::format($account->endBalance) !!}</div>
</div>
@endforeach
@endif
</div>
</div>
<!-- PIGGY BANKS -->
<div class="panel panel-default">
<div class="panel-heading">
<i class="fa fa-sort-amount-asc fa-fw"></i> Piggy banks
</div>
<div class="panel-body">
@if($piggyBankAccounts->count() == 0)
<p class="small"><em>Create piggy banks to fill this panel.</em></p>
@else
@foreach($piggyBankAccounts as $account)
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12"><h5><a href="{{route('accounts.show')}}">{{$account->name}}</a></h5></div>
</div>
<div class="row">
<!-- start -->
<div class="col-lg-2 col-md-2 col-sm-3 col-xs-4">{!! Amount::format($account->startBalance) !!}</div>
<!-- bar -->
<div class="col-lg-8 col-md-8 col-sm-6 col-xs-4">
<div class="progress">
<div class="progress-bar progress-bar-info progress-bar-striped" style="width: {{100 - $account->percentage}}%">
@if($account->percentage <= 50)
{{Amount::format($account->piggyBalance,false)}} divided
@endif
</div>
<!-- end -->
<div class="col-lg-2 col-md-2 col-sm-3 col-xs-4">{!! Amount::format($account->endBalance) !!}</div>
<div class="progress-bar progress-bar-success progress-bar-striped" style="width: {{$account->percentage}}%">
@if($account->percentage > 50)
{{Amount::format($account->difference,false)}} left to divide
@endif
</div>
@endforeach
@endif
</div>
</div>
</div>
</div>
<!-- end -->
<div class="col-lg-2 col-md-2 col-sm-3 col-xs-4">{!! Amount::format($account->piggyBalance) !!}</div>
</div>
@endforeach
@endif
</div>
</div>
</div>

View File

@ -98,6 +98,7 @@ class HomeControllerTest extends TestCase
Preferences::shouldReceive('get')->once()->withArgs(['frontPageAccounts', []])->andReturn($preference);
$repository->shouldReceive('getFrontpageAccounts')->once()->with($preference)->andReturn($accounts);
$repository->shouldReceive('getSavingsAccounts')->once()->andReturn($accounts);
$repository->shouldReceive('getPiggyBankAccounts')->once()->andReturn($accounts);
$repository->shouldReceive('sumOfEverything')->once()->andReturn(1);
$repository->shouldReceive('getFrontpageTransactions')->once()->andReturn($journals);