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; <?php namespace FireflyIII\Http\Controllers;
use Auth;
use Carbon\Carbon; use Carbon\Carbon;
use Config; use Config;
use FireflyIII\Repositories\Account\AccountRepositoryInterface; use FireflyIII\Repositories\Account\AccountRepositoryInterface;
@ -44,24 +43,28 @@ class HomeController extends Controller
} }
/** /**
* @param AccountRepositoryInterface $repository
*
* @return \Illuminate\View\View * @return \Illuminate\View\View
*/ */
public function index(AccountRepositoryInterface $repository) public function index(AccountRepositoryInterface $repository)
{ {
$types = Config::get('firefly.accountTypesByIdentifier.asset'); $types = Config::get('firefly.accountTypesByIdentifier.asset');
$count = $repository->countAccounts($types); $count = $repository->countAccounts($types);
$title = 'Firefly'; $title = 'Firefly';
$subTitle = 'What\'s playing?'; $subTitle = 'What\'s playing?';
$mainTitleIcon = 'fa-fire'; $mainTitleIcon = 'fa-fire';
$transactions = []; $transactions = [];
$frontPage = Preferences::get('frontPageAccounts', []); $frontPage = Preferences::get('frontPageAccounts', []);
$start = Session::get('start', Carbon::now()->startOfMonth()); $start = Session::get('start', Carbon::now()->startOfMonth());
$end = Session::get('end', Carbon::now()->endOfMonth()); $end = Session::get('end', Carbon::now()->endOfMonth());
$accounts = $repository->getFrontpageAccounts($frontPage); $accounts = $repository->getFrontpageAccounts($frontPage);
$savings = $repository->getSavingsAccounts(); $savings = $repository->getSavingsAccounts();
$piggyBankAccounts = $repository->getPiggyBankAccounts();
$savingsTotal = 0;
$savingsTotal = 0;
foreach ($savings as $savingAccount) { foreach ($savings as $savingAccount) {
$savingsTotal += Steam::balance($savingAccount, $end); $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 Auth;
use Carbon\Carbon; use Carbon\Carbon;
use Config; use Config;
use DB;
use FireflyIII\Models\Account; use FireflyIII\Models\Account;
use FireflyIII\Models\AccountMeta; use FireflyIII\Models\AccountMeta;
use FireflyIII\Models\AccountType; use FireflyIII\Models\AccountType;
@ -141,13 +142,6 @@ class AccountRepository implements AccountRepositoryInterface
->get(['transaction_journals.*', 'transaction_currencies.symbol', 'transaction_types.type']); ->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 Account $account
* @param int $page * @param int $page
@ -194,6 +188,51 @@ class AccountRepository implements AccountRepositoryInterface
return null; 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. * 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 Account $account
* @param array $data * @param array $data

View File

@ -51,6 +51,14 @@ interface AccountRepositoryInterface
*/ */
public function getCreditCards(); 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. * Get all transfers TO this account in this range.
* *

View File

@ -17,6 +17,7 @@ use Navigation;
class PiggyBankRepository implements PiggyBankRepositoryInterface class PiggyBankRepository implements PiggyBankRepositoryInterface
{ {
/** /**
* @SuppressWarnings("CyclomaticComplexity") // It's exactly 5. So I don't mind. * @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`')]); 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. * Set all piggy banks to order 0.
* *

View File

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

View File

@ -52,63 +52,104 @@
<div id="categories-chart"></div> <div id="categories-chart"></div>
</div> </div>
</div> </div>
<div class="panel panel-default">
<div class="panel-heading"> <!-- SAVINGS -->
<i class="fa fa-line-chart"></i> Savings <div class="panel panel-default">
<span class="pull-right">{!! Amount::format($savingsTotal) !!}</span> <div class="panel-heading">
</div> <i class="fa fa-line-chart"></i> Savings
<div class="panel-body"> <span class="pull-right">{!! Amount::format($savingsTotal) !!}</span>
@if(count($savings) == 0) </div>
<p class="small"><em>Mark your asset accounts as "Savings account" to fill this panel.</em></p> <div class="panel-body">
@else @if(count($savings) == 0)
@foreach($savings as $account) <p class="small"><em>Mark your asset accounts as "Savings account" to fill this panel.</em></p>
<div class="row"> @else
<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> @foreach($savings as $account)
</div> <div class="row">
<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>
<!-- start --> </div>
<div class="col-lg-2 col-md-2 col-sm-3 col-xs-4">{!! Amount::format($account->startBalance) !!}</div> <div class="row">
<!-- bar --> <!-- start -->
<div class="col-lg-8 col-md-8 col-sm-6 col-xs-4"> <div class="col-lg-2 col-md-2 col-sm-3 col-xs-4">{!! Amount::format($account->startBalance) !!}</div>
@if($account->difference < 0) <!-- bar -->
<!-- green (100-pct), then red (pct) --> <div class="col-lg-8 col-md-8 col-sm-6 col-xs-4">
<div class="progress"> @if($account->difference < 0)
<div class="progress-bar progress-bar-success progress-bar-striped" style="width: {{100 - $account->percentage}}%"> <!-- green (100-pct), then red (pct) -->
@if($account->percentage <= 50) <div class="progress">
{{Amount::format($account->difference,false)}} <div class="progress-bar progress-bar-success progress-bar-striped" style="width: {{100 - $account->percentage}}%">
@endif @if($account->percentage <= 50)
</div> {{Amount::format($account->difference,false)}}
<div class="progress-bar progress-bar-danger progress-bar-striped" style="width: {{$account->percentage}}%"> @endif
@if($account->percentage > 50) </div>
{{Amount::format($account->difference,false)}} <div class="progress-bar progress-bar-danger progress-bar-striped" style="width: {{$account->percentage}}%">
@endif @if($account->percentage > 50)
</div> {{Amount::format($account->difference,false)}}
</div> @endif
@else </div>
<!-- green (pct), then blue (100-pct) --> </div>
<div class="progress"> @else
<div class="progress-bar progress-bar-success progress-bar-striped" style="width: {{$account->percentage}}%"> <!-- green (pct), then blue (100-pct) -->
@if($account->percentage > 50) <div class="progress">
{{Amount::format($account->difference,false)}} <div class="progress-bar progress-bar-success progress-bar-striped" style="width: {{$account->percentage}}%">
@endif @if($account->percentage > 50)
</div> {{Amount::format($account->difference,false)}}
<div class="progress-bar progress-bar-info progress-bar-striped" style="width: {{100 - $account->percentage}}%"> @endif
@if($account->percentage <= 50) </div>
{{Amount::format($account->difference,false)}} <div class="progress-bar progress-bar-info progress-bar-striped" style="width: {{100 - $account->percentage}}%">
@endif @if($account->percentage <= 50)
</div> {{Amount::format($account->difference,false)}}
</div> @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 @endif
</div> </div>
<!-- end --> <div class="progress-bar progress-bar-success progress-bar-striped" style="width: {{$account->percentage}}%">
<div class="col-lg-2 col-md-2 col-sm-3 col-xs-4">{!! Amount::format($account->endBalance) !!}</div> @if($account->percentage > 50)
{{Amount::format($account->difference,false)}} left to divide
@endif
</div> </div>
@endforeach </div>
@endif </div>
</div> <!-- end -->
</div> <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> </div>

View File

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