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 FireflyIII\Http\Controllers\Controller;
|
|
|
|
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
|
2018-07-14 09:08:34 -05:00
|
|
|
use FireflyIII\Support\Http\Controllers\DateCalculation;
|
2018-10-17 08:18:09 -05:00
|
|
|
use Illuminate\Http\JsonResponse;
|
2018-07-14 08:22:21 -05:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\Pagination\LengthAwarePaginator;
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Class IndexController
|
|
|
|
*/
|
|
|
|
class IndexController extends Controller
|
|
|
|
{
|
|
|
|
|
2018-07-14 09:08:34 -05:00
|
|
|
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();
|
|
|
|
|
2018-07-14 09:08:34 -05:00
|
|
|
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);
|
2018-08-07 10:34:43 -05:00
|
|
|
$this->repository->cleanupBudgets();
|
2018-07-14 08:22:21 -05:00
|
|
|
|
|
|
|
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
|
2018-08-08 10:53:40 -05:00
|
|
|
*
|
|
|
|
* @param Carbon|null $start
|
|
|
|
* @param Carbon|null $end
|
2018-07-14 08:22:21 -05:00
|
|
|
*
|
|
|
|
* @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
|
|
|
*/
|
2018-08-08 10:53:40 -05:00
|
|
|
public function index(Request $request, Carbon $start = null, Carbon $end = null)
|
2018-07-14 08:22:21 -05:00
|
|
|
{
|
2018-08-07 10:34:43 -05:00
|
|
|
// collect some basic vars:
|
|
|
|
$range = app('preferences')->get('viewRange', '1M')->data;
|
2018-08-08 10:53:40 -05:00
|
|
|
$start = $start ?? session('start', Carbon::now()->startOfMonth());
|
|
|
|
$end = $end ?? app('navigation')->endOfPeriod($start, $range);
|
2018-08-07 10:34:43 -05:00
|
|
|
$page = 0 === (int)$request->get('page') ? 1 : (int)$request->get('page');
|
|
|
|
$pageSize = (int)app('preferences')->get('listPageSize', 50)->data;
|
|
|
|
$defaultCurrency = app('amount')->getDefaultCurrency();
|
|
|
|
|
|
|
|
// make the next and previous period, and calculate the periods used for period navigation
|
2018-07-14 08:22:21 -05:00
|
|
|
$next = clone $end;
|
|
|
|
$next->addDay();
|
|
|
|
$prev = clone $start;
|
|
|
|
$prev->subDay();
|
2018-08-07 10:34:43 -05:00
|
|
|
$prev = app('navigation')->startOfPeriod($prev, $range);
|
|
|
|
$previousLoop = $this->getPreviousPeriods($start, $range);
|
2018-08-08 10:53:40 -05:00
|
|
|
$nextLoop = $this->getNextPeriods($start, $range);
|
2018-07-14 08:22:21 -05:00
|
|
|
$currentMonth = app('navigation')->periodShow($start, $range);
|
|
|
|
$nextText = app('navigation')->periodShow($next, $range);
|
|
|
|
$prevText = app('navigation')->periodShow($prev, $range);
|
|
|
|
|
2018-08-07 10:34:43 -05:00
|
|
|
// number of days for consistent budgeting.
|
|
|
|
$activeDaysPassed = $this->activeDaysPassed($start, $end); // see method description.
|
|
|
|
$activeDaysLeft = $this->activeDaysLeft($start, $end); // see method description.
|
|
|
|
|
|
|
|
// get all budgets, and paginate them into $budgets.
|
|
|
|
$collection = $this->repository->getActiveBudgets();
|
|
|
|
$total = $collection->count();
|
|
|
|
$budgets = $collection->slice(($page - 1) * $pageSize, $pageSize);
|
|
|
|
|
|
|
|
// get all inactive budgets, and simply list them:
|
|
|
|
$inactive = $this->repository->getInactiveBudgets();
|
|
|
|
|
|
|
|
// collect budget info to fill bars and so on.
|
|
|
|
$budgetInformation = $this->repository->collectBudgetInformation($collection, $start, $end);
|
|
|
|
|
|
|
|
// to display available budget:
|
|
|
|
$available = $this->repository->getAvailableBudget($defaultCurrency, $start, $end);
|
|
|
|
$spent = array_sum(array_column($budgetInformation, 'spent'));
|
|
|
|
$budgeted = array_sum(array_column($budgetInformation, 'budgeted'));
|
|
|
|
|
|
|
|
|
|
|
|
// paginate budgets
|
|
|
|
$paginator = new LengthAwarePaginator($budgets, $total, $pageSize, $page);
|
|
|
|
$paginator->setPath(route('budgets.index'));
|
|
|
|
|
2018-07-14 08:22:21 -05:00
|
|
|
return view(
|
|
|
|
'budgets.index', compact(
|
2018-08-07 10:34:43 -05:00
|
|
|
'available', 'currentMonth', 'next', 'nextText', 'prev', 'paginator',
|
|
|
|
'prevText',
|
|
|
|
'page', 'activeDaysPassed', 'activeDaysLeft',
|
|
|
|
'budgetInformation',
|
2018-07-14 08:22:21 -05:00
|
|
|
'inactive', 'budgets', 'spent', 'budgeted', 'previousLoop', 'nextLoop', 'start', 'end'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-10-17 08:18:09 -05:00
|
|
|
/**
|
|
|
|
* @param Request $request
|
|
|
|
*
|
|
|
|
* @return JsonResponse
|
|
|
|
*/
|
|
|
|
public function reorder(Request $request, BudgetRepositoryInterface $repository): JsonResponse
|
|
|
|
{
|
|
|
|
$budgetIds = $request->get('budgetIds');
|
|
|
|
$page = (int)$request->get('page');
|
|
|
|
$pageSize = (int)app('preferences')->get('listPageSize', 50)->data;
|
|
|
|
|
|
|
|
$currentOrder = (($page - 1) * $pageSize) + 1;
|
|
|
|
foreach ($budgetIds as $budgetId) {
|
|
|
|
$budgetId = (int)$budgetId;
|
|
|
|
$budget = $repository->findNull($budgetId);
|
|
|
|
if (null !== $budget) {
|
|
|
|
$repository->setBudgetOrder($budget, $currentOrder);
|
|
|
|
}
|
|
|
|
$currentOrder++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return response()->json(['OK']);
|
|
|
|
}
|
|
|
|
|
2018-07-14 09:08:34 -05:00
|
|
|
|
2018-07-22 13:32:02 -05:00
|
|
|
}
|