Skip to content

Firefly III: a personal finances manager

Added a block with savings.

James Cole6a88c8634ded
diff --git a/app/Http/Controllers/HomeController.php b/app/Http/Controllers/HomeController.php
index a2926001fb..9b9d0235e7 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 Cache;
use Carbon\Carbon;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
@@ -64,6 +63,7 @@ class HomeController extends Controller
$start = Session::get('start', Carbon::now()->startOfMonth());
$end = Session::get('end', Carbon::now()->endOfMonth());
$accounts = $repository->getFrontpageAccounts($frontPage);
+ $savings = $repository->getSavingsAccounts();
foreach ($accounts as $account) {
$set = $repository->getFrontpageTransactions($account, $start, $end);
@@ -74,7 +74,7 @@ class HomeController extends Controller
// var_dump($transactions);
- return view('index', compact('count', 'title', 'subTitle', 'mainTitleIcon', 'transactions'));
+ return view('index', compact('count', 'title','savings', 'subTitle', 'mainTitleIcon', 'transactions'));
}
diff --git a/app/Models/Component.php b/app/Models/Component.php
index 0c8e57a45f..7a9bba141b 100644
--- a/app/Models/Component.php
+++ b/app/Models/Component.php
@@ -12,6 +12,8 @@ class Component extends Model
{
use SoftDeletes;
+ protected $fillable = ['user_id', 'name','class'];
+
/**
* @return array
*/
diff --git a/app/Repositories/Account/AccountRepository.php b/app/Repositories/Account/AccountRepository.php
index 104a6b6a13..25c20e0f65 100644
--- a/app/Repositories/Account/AccountRepository.php
+++ b/app/Repositories/Account/AccountRepository.php
@@ -18,6 +18,7 @@ use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Collection;
use Log;
use Session;
+use Steam;
/**
* Class AccountRepository
@@ -120,6 +121,43 @@ class AccountRepository implements AccountRepositoryInterface
}
+ /**
+ * Get savings accounts and the balance difference in the period.
+ *
+ * @return Collection
+ */
+ public function getSavingsAccounts()
+ {
+ $accounts = Auth::user()->accounts()->accountTypeIn(['Default account', 'Asset account'])->orderBy('accounts.name', 'ASC')
+ ->leftJoin('account_meta', 'account_meta.account_id', '=', 'accounts.id')
+ ->where('account_meta.name', 'accountRole')
+ ->where('account_meta.data', '"savingAsset"')
+ ->get(['accounts.*']);
+ $start = clone Session::get('start');
+ $end = clone Session::get('end');
+
+ $accounts->each(
+ function (Account $account) use ($start, $end) {
+ $account->startBalance = Steam::balance($account, $start);
+ $account->endBalance = Steam::balance($account, $end);
+
+ // diff (negative when lost, positive when gained)
+ $diff = $account->endBalance - $account->startBalance;
+
+ if ($diff < 0) {
+ // percentage lost compared to start.
+ $pct = (($diff * -1) / $account->startBalance) * 100;
+ } else {
+ $pct = ($diff / $account->startBalance) * 100;
+ }
+ $account->difference = $diff;
+ $account->percentage = round($pct);
+ }
+ );
+
+ return $accounts;
+ }
+
/**
* @param Account $account
*
diff --git a/app/Repositories/Account/AccountRepositoryInterface.php b/app/Repositories/Account/AccountRepositoryInterface.php
index 020ae41b69..41de454e63 100644
--- a/app/Repositories/Account/AccountRepositoryInterface.php
+++ b/app/Repositories/Account/AccountRepositoryInterface.php
@@ -78,4 +78,11 @@ interface AccountRepositoryInterface
* @return float
*/
public function leftOnAccount(Account $account);
+
+ /**
+ * Get savings accounts and the balance difference in the period.
+ *
+ * @return Collection
+ */
+ public function getSavingsAccounts();
}
\ No newline at end of file
diff --git a/database/seeds/TestDataSeeder.php b/database/seeds/TestDataSeeder.php
index 90b5726889..6e3d97ba3d 100644
--- a/database/seeds/TestDataSeeder.php
+++ b/database/seeds/TestDataSeeder.php
@@ -139,7 +139,7 @@ class TestDataSeeder extends Seeder
// create account meta:
$meta_a = AccountMeta::create(['account_id' => $acc_a->id, 'name' => 'accountRole', 'data' => 'defaultAsset']);
- $meta_b = AccountMeta::create(['account_id' => $acc_b->id, 'name' => 'accountRole', 'data' => 'defaultAsset']);
+ $meta_b = AccountMeta::create(['account_id' => $acc_b->id, 'name' => 'accountRole', 'data' => 'savingAsset']);
$meta_c = AccountMeta::create(['account_id' => $acc_c->id, 'name' => 'accountRole', 'data' => 'defaultAsset']);
// var_dump($meta_a->toArray());
// var_dump($meta_b->toArray());
diff --git a/resources/views/index.blade.php b/resources/views/index.blade.php
index 33e5610669..33da7dce45 100644
--- a/resources/views/index.blade.php
+++ b/resources/views/index.blade.php
@@ -57,7 +57,54 @@
<i class="fa fa-line-chart"></i> Savings
</div>
<div class="panel-body">
- (todo)
+ @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>