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 @@ 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')); } 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 @@
-Mark your asset accounts as "Savings account" to fill this panel.
- @else - @foreach($savings as $account) - -Mark your asset accounts as "Savings account" to fill this panel.
+ @else + @foreach($savings as $account) + +Create piggy banks to fill this panel.
+ @else + @foreach($piggyBankAccounts as $account) + +