Some refactoring.

This commit is contained in:
James Cole 2015-12-15 12:46:40 +01:00
parent 301528e2d2
commit 36d7a02994
2 changed files with 18 additions and 11 deletions

View File

@ -6,6 +6,7 @@ use Carbon\Carbon;
use FireflyIII\Http\Requests\BudgetFormRequest;
use FireflyIII\Models\Budget;
use FireflyIII\Models\LimitRepetition;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use Input;
use Navigation;
@ -135,7 +136,7 @@ class BudgetController extends Controller
*
* @return \Illuminate\View\View
*/
public function index(BudgetRepositoryInterface $repository)
public function index(BudgetRepositoryInterface $repository, AccountRepositoryInterface $accountRepository)
{
$budgets = $repository->getActiveBudgets();
$inactive = $repository->getInactiveBudgets();
@ -147,6 +148,8 @@ class BudgetController extends Controller
$key = 'budgetIncomeTotal' . $start->format('Ymd') . $end->format('Ymd');
$budgetIncomeTotal = Preferences::get($key, 1000)->data;
$period = Navigation::periodShow($start, $range);
$accounts = $accountRepository->getAccounts(['Default account', 'Asset account', 'Cash account']);
bcscale(2);
/**
* Do some cleanup:
@ -156,7 +159,7 @@ class BudgetController extends Controller
// loop the budgets:
/** @var Budget $budget */
foreach ($budgets as $budget) {
$budget->spent = $repository->balanceInPeriod($budget, $start, $end);
$budget->spent = $repository->balanceInPeriodForList($budget, $start, $end, $accounts);
$budget->currentRep = $repository->getCurrentRepetition($budget, $start, $end);
if ($budget->currentRep) {
$budgeted = bcadd($budgeted, $budget->currentRep->amount);
@ -170,7 +173,7 @@ class BudgetController extends Controller
$defaultCurrency = Amount::getDefaultCurrency();
return view(
'budgets.index', compact('budgetMaximum','period', 'range', 'budgetIncomeTotal', 'defaultCurrency', 'inactive', 'budgets', 'spent', 'budgeted')
'budgets.index', compact('budgetMaximum', 'period', 'range', 'budgetIncomeTotal', 'defaultCurrency', 'inactive', 'budgets', 'spent', 'budgeted')
);
}
@ -279,7 +282,7 @@ class BudgetController extends Controller
{
$budgetData = [
'name' => $request->input('name'),
'active' => intval($request->input('active')) == 1
'active' => intval($request->input('active')) == 1,
];
$repository->update($budget, $budgetData);

View File

@ -6,6 +6,7 @@ use Carbon\Carbon;
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Models\Budget;
use FireflyIII\Models\LimitRepetition;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use FireflyIII\Support\CacheProperties;
use Illuminate\Support\Collection;
@ -47,7 +48,7 @@ class BudgetController extends Controller
{
$currentStart = clone $start;
while($currentStart < $end) {
while ($currentStart < $end) {
$currentEnd = clone $currentStart;
$currentEnd->endOfYear();
@ -70,7 +71,7 @@ class BudgetController extends Controller
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function budget(BudgetRepositoryInterface $repository, Budget $budget)
public function budget(BudgetRepositoryInterface $repository, AccountRepositoryInterface $accountRepository, Budget $budget)
{
// dates and times
@ -79,7 +80,9 @@ class BudgetController extends Controller
$last = Session::get('end', new Carbon);
$final = clone $last;
$final->addYears(2);
$last = Navigation::endOfX($last, $range, $final);
$last = Navigation::endOfX($last, $range, $final);
$accounts = $accountRepository->getAccounts(['Default account', 'Asset account', 'Cash account']);
// chart properties for cache:
$cache = new CacheProperties();
@ -97,7 +100,7 @@ class BudgetController extends Controller
$end->subDay();
$chartDate = clone $end;
$chartDate->startOfMonth();
$spent = $repository->balanceInPeriod($budget, $first, $end) * -1;
$spent = $repository->balanceInPeriodForList($budget, $first, $end, $accounts) * -1;
$entries->push([$chartDate, $spent]);
$first = Navigation::addPeriod($first, $range, 0);
}
@ -162,12 +165,13 @@ class BudgetController extends Controller
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function frontpage(BudgetRepositoryInterface $repository)
public function frontpage(BudgetRepositoryInterface $repository, AccountRepositoryInterface $accountRepository)
{
$budgets = $repository->getBudgets();
$start = Session::get('start', Carbon::now()->startOfMonth());
$end = Session::get('end', Carbon::now()->endOfMonth());
$allEntries = new Collection;
$accounts = $accountRepository->getAccounts(['Default account', 'Asset account', 'Cash account']);
// chart properties for cache:
$cache = new CacheProperties();
@ -185,13 +189,13 @@ class BudgetController extends Controller
foreach ($budgets as $budget) {
$repetitions = $repository->getBudgetLimitRepetitions($budget, $start, $end);
if ($repetitions->count() == 0) {
$expenses = $repository->balanceInPeriod($budget, $start, $end, true) * -1;
$expenses = $repository->balanceInPeriodForList($budget, $start, $end, $accounts) * -1;
$allEntries->push([$budget->name, 0, 0, $expenses, 0, 0]);
continue;
}
/** @var LimitRepetition $repetition */
foreach ($repetitions as $repetition) {
$expenses = $repository->balanceInPeriod($budget, $repetition->startdate, $repetition->enddate, true) * -1;
$expenses = $repository->balanceInPeriodForList($budget, $repetition->startdate, $repetition->enddate, $accounts) * -1;
// $left can be less than zero.
// $overspent can be more than zero ( = overspending)