firefly-iii/app/Http/Controllers/PiggyBankController.php

71 lines
2.3 KiB
PHP
Raw Normal View History

2015-02-23 14:55:52 -06:00
<?php namespace FireflyIII\Http\Controllers;
use FireflyIII\Http\Requests;
use FireflyIII\Http\Controllers\Controller;
2015-02-24 14:10:25 -06:00
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
2015-02-23 14:55:52 -06:00
use Illuminate\Http\Request;
2015-02-24 14:10:25 -06:00
use View;
use Auth;
use Illuminate\Support\Collection;
use FireflyIII\Models\PiggyBank;
use FireflyIII\Models\Account;
use Steam;
2015-02-23 14:55:52 -06:00
/**
* Class PiggyBankController
*
* @package FireflyIII\Http\Controllers
*/
class PiggyBankController extends Controller {
2015-02-24 14:10:25 -06:00
/**
*
*/
public function __construct()
{
View::share('title', 'Piggy banks');
View::share('mainTitleIcon', 'fa-sort-amount-asc');
}
/**
* @return $this
*/
public function index(AccountRepositoryInterface $repository)
{
/** @var Collection $piggyBanks */
$piggyBanks = Auth::user()->piggyBanks()->where('repeats',0)->get();
$accounts = [];
/** @var PiggyBank $piggyBank */
foreach ($piggyBanks as $piggyBank) {
$piggyBank->savedSoFar = floatval($piggyBank->currentRelevantRep()->currentamount);
$piggyBank->percentage = intval($piggyBank->savedSoFar / $piggyBank->targetamount * 100);
$piggyBank->leftToSave = $piggyBank->targetamount - $piggyBank->savedSoFar;
/*
* Fill account information:
*/
$account = $piggyBank->account;
if (!isset($accounts[$account->id])) {
$accounts[$account->id] = [
'name' => $account->name,
'balance' => Steam::balance($account),
'leftForPiggyBanks' => $repository->leftOnAccount($account),
'sumOfSaved' => $piggyBank->savedSoFar,
'sumOfTargets' => floatval($piggyBank->targetamount),
'leftToSave' => $piggyBank->leftToSave
];
} else {
$accounts[$account->id]['sumOfSaved'] += $piggyBank->savedSoFar;
$accounts[$account->id]['sumOfTargets'] += floatval($piggyBank->targetamount);
$accounts[$account->id]['leftToSave'] += $piggyBank->leftToSave;
}
}
return View::make('piggy-banks.index', compact('piggyBanks', 'accounts'));
}
2015-02-23 14:55:52 -06:00
}