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.
*