firefly-iii/app/Http/Controllers/Budget/IndexController.php

147 lines
5.4 KiB
PHP
Raw Normal View History

2018-07-14 08:22:21 -05:00
<?php
/**
* IndexController.php
* Copyright (c) 2018 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Http\Controllers\Budget;
use Carbon\Carbon;
use Exception;
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use FireflyIII\Support\Http\Controllers\DateCalculation;
2018-07-14 08:22:21 -05:00
use Illuminate\Http\Request;
use Illuminate\Pagination\LengthAwarePaginator;
use Log;
/**
*
* Class IndexController
*/
class IndexController extends Controller
{
use DateCalculation;
2018-07-21 01:06:24 -05:00
/** @var BudgetRepositoryInterface The budget repository */
2018-07-14 08:22:21 -05:00
private $repository;
/**
2018-07-21 01:06:24 -05:00
* IndexController constructor.
2018-07-14 08:22:21 -05:00
*/
public function __construct()
{
parent::__construct();
app('view')->share('hideBudgets', true);
2018-07-14 08:22:21 -05:00
$this->middleware(
function ($request, $next) {
2018-07-15 02:38:49 -05:00
app('view')->share('title', (string)trans('firefly.budgets'));
2018-07-14 08:22:21 -05:00
app('view')->share('mainTitleIcon', 'fa-tasks');
$this->repository = app(BudgetRepositoryInterface::class);
return $next($request);
}
);
}
/**
2018-07-21 01:06:24 -05:00
* Show all budgets.
*
2018-07-14 08:22:21 -05:00
* @param Request $request
* @param string|null $moment
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
2018-07-14 10:23:44 -05:00
*
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
2018-07-14 08:22:21 -05:00
*/
public function index(Request $request, string $moment = null)
{
2018-07-26 21:46:21 -05:00
/** @var string $range */
2018-08-06 12:14:30 -05:00
$range = app('preferences')->get('viewRange', '1M')->data;
2018-07-15 12:17:26 -05:00
/** @var Carbon $start */
2018-08-06 12:14:30 -05:00
$start = session('start', new Carbon);
2018-07-15 12:17:26 -05:00
/** @var Carbon $end */
$end = session('end', new Carbon);
$page = 0 === (int)$request->get('page') ? 1 : (int)$request->get('page');
$pageSize = (int)app('preferences')->get('listPageSize', 50)->data;
2018-07-14 10:23:44 -05:00
$moment = $moment ?? '';
2018-07-14 08:22:21 -05:00
2018-07-14 10:23:44 -05:00
// make date if the data is given.
if ('' !== (string)$moment) {
2018-07-14 08:22:21 -05:00
try {
$start = new Carbon($moment);
2018-07-15 12:17:26 -05:00
/** @var Carbon $end */
2018-08-06 12:14:30 -05:00
$end = app('navigation')->endOfPeriod($start, $range);
2018-07-14 08:22:21 -05:00
} catch (Exception $e) {
// start and end are already defined.
Log::debug(sprintf('start and end are already defined: %s', $e->getMessage()));
}
}
// if today is between start and end, use the diff in days between end and today (days left)
// otherwise, use diff between start and end.
2018-07-14 10:23:44 -05:00
$dayDifference = $this->getDayDifference($start, $end);
2018-07-14 08:22:21 -05:00
$next = clone $end;
$next->addDay();
$prev = clone $start;
$prev->subDay();
$prev = app('navigation')->startOfPeriod($prev, $range);
$this->repository->cleanupBudgets();
$daysPassed = $this->getDaysPassedInPeriod($start, $end);
2018-07-14 08:22:21 -05:00
$allBudgets = $this->repository->getActiveBudgets();
$total = $allBudgets->count();
$budgets = $allBudgets->slice(($page - 1) * $pageSize, $pageSize);
$inactive = $this->repository->getInactiveBudgets();
$periodStart = $start->formatLocalized($this->monthAndDayFormat);
$periodEnd = $end->formatLocalized($this->monthAndDayFormat);
$budgetInformation = $this->repository->collectBudgetInformation($allBudgets, $start, $end);
$defaultCurrency = app('amount')->getDefaultCurrency();
$available = $this->repository->getAvailableBudget($defaultCurrency, $start, $end);
$spent = array_sum(array_column($budgetInformation, 'spent'));
$budgeted = array_sum(array_column($budgetInformation, 'budgeted'));
2018-07-14 10:23:44 -05:00
$previousLoop = $this->getPreviousPeriods($start, $range);
$nextLoop = $this->getNextPeriods($end, $range);
2018-07-14 08:22:21 -05:00
// paginate budgets
$budgets = new LengthAwarePaginator($budgets, $total, $pageSize, $page);
$budgets->setPath(route('budgets.index'));
// display info
$currentMonth = app('navigation')->periodShow($start, $range);
$nextText = app('navigation')->periodShow($next, $range);
$prevText = app('navigation')->periodShow($prev, $range);
return view(
'budgets.index', compact(
2018-07-14 10:23:44 -05:00
'available', 'currentMonth', 'next', 'nextText', 'prev', 'allBudgets', 'prevText', 'periodStart', 'periodEnd', 'dayDifference',
'page',
'budgetInformation', 'daysPassed',
2018-07-14 08:22:21 -05:00
'inactive', 'budgets', 'spent', 'budgeted', 'previousLoop', 'nextLoop', 'start', 'end'
)
);
}
}