Skip to content

Firefly III: a personal finances manager

Added home screen information about piggy banks.

James Cole721fa04e3c2c
diff --git a/app/Http/Controllers/HomeController.php b/app/Http/Controllers/HomeController.php
index e02c69bddb..d7728b61b1 100644
--- a/app/Http/Controllers/HomeController.php
+++ b/app/Http/Controllers/HomeController.php
@@ -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();
-
- $savingsTotal = 0;
+ $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;
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'));
}
diff --git a/app/Repositories/Account/AccountRepository.php b/app/Repositories/Account/AccountRepository.php
index ba5a88fea7..5dd2fe9772 100644
--- a/app/Repositories/Account/AccountRepository.php
+++ b/app/Repositories/Account/AccountRepository.php
@@ -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
diff --git a/app/Repositories/Account/AccountRepositoryInterface.php b/app/Repositories/Account/AccountRepositoryInterface.php
index 0d533a0970..0a80c40691 100644
--- a/app/Repositories/Account/AccountRepositoryInterface.php
+++ b/app/Repositories/Account/AccountRepositoryInterface.php
@@ -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.
*
diff --git a/app/Repositories/PiggyBank/PiggyBankRepository.php b/app/Repositories/PiggyBank/PiggyBankRepository.php
index 0c95d0ab2f..ecabb40237 100644
--- a/app/Repositories/PiggyBank/PiggyBankRepository.php
+++ b/app/Repositories/PiggyBank/PiggyBankRepository.php
@@ -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.
*
diff --git a/app/Repositories/PiggyBank/PiggyBankRepositoryInterface.php b/app/Repositories/PiggyBank/PiggyBankRepositoryInterface.php
index 4bfe3d8cab..bc445cad87 100644
--- a/app/Repositories/PiggyBank/PiggyBankRepositoryInterface.php
+++ b/app/Repositories/PiggyBank/PiggyBankRepositoryInterface.php
@@ -14,7 +14,6 @@ use Illuminate\Support\Collection;
interface PiggyBankRepositoryInterface
{
-
/**
* @SuppressWarnings("CyclomaticComplexity") // It's exactly 5. So I don't mind.
*
diff --git a/resources/views/index.blade.php b/resources/views/index.blade.php
index cc4a001e1b..013e4a4a15 100644
--- a/resources/views/index.blade.php
+++ b/resources/views/index.blade.php
@@ -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>
- @endif
+ <!-- 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>
- <!-- end -->
- <div class="col-lg-2 col-md-2 col-sm-3 col-xs-4">{!! Amount::format($account->endBalance) !!}</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>
- @endforeach
- @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>
+ <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>
+ </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>
diff --git a/tests/controllers/HomeControllerTest.php b/tests/controllers/HomeControllerTest.php
index aff74fa2e2..d9491bac8c 100644
--- a/tests/controllers/HomeControllerTest.php
+++ b/tests/controllers/HomeControllerTest.php
@@ -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);