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

409 lines
12 KiB
PHP
Raw Normal View History

2016-05-20 01:57:45 -05:00
<?php
/**
* ReportController.php
2017-10-21 01:40:00 -05:00
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
2017-10-21 01:40:00 -05:00
* This file is part of Firefly III.
*
2017-10-21 01:40:00 -05:00
* 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/>.
*/
2017-03-24 09:15:12 -05:00
declare(strict_types=1);
2016-05-20 01:57:45 -05:00
namespace FireflyIII\Http\Controllers;
2015-02-23 13:25:48 -06:00
use Carbon\Carbon;
use FireflyIII\Generator\Report\ReportGeneratorFactory;
2015-02-23 13:25:48 -06:00
use FireflyIII\Helpers\Report\ReportHelperInterface;
use FireflyIII\Http\Requests\ReportFormRequest;
2016-05-20 04:02:07 -05:00
use FireflyIII\Models\AccountType;
2017-02-15 13:07:10 -06:00
use FireflyIII\Models\Tag;
2016-10-10 00:49:39 -05:00
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
2017-02-15 13:07:10 -06:00
use FireflyIII\Repositories\Tag\TagRepositoryInterface;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Collection;
2017-03-24 05:07:38 -05:00
use Log;
2016-01-29 00:47:18 -06:00
use Preferences;
use Response;
2016-11-25 09:55:04 -06:00
use Session;
2015-02-23 13:25:48 -06:00
use View;
2015-02-23 13:25:48 -06:00
/**
2017-11-15 05:25:49 -06:00
* Class ReportController.
2015-02-23 13:25:48 -06:00
*/
class ReportController extends Controller
{
2015-03-29 05:25:46 -05:00
/** @var ReportHelperInterface */
protected $helper;
2015-02-23 13:25:48 -06:00
/**
2016-02-04 00:28:39 -06:00
*
2015-02-23 13:25:48 -06:00
*/
2016-09-16 02:36:08 -05:00
public function __construct()
2015-02-23 13:25:48 -06:00
{
2015-05-15 15:00:00 -05:00
parent::__construct();
2016-01-27 13:45:49 -06:00
2017-03-29 14:20:54 -05:00
$this->helper = app(ReportHelperInterface::class);
2016-10-29 00:44:46 -05:00
$this->middleware(
function ($request, $next) {
View::share('title', trans('firefly.reports'));
View::share('mainTitleIcon', 'fa-line-chart');
View::share('subTitleIcon', 'fa-calendar');
2016-10-29 00:44:46 -05:00
return $next($request);
}
);
2015-02-23 13:25:48 -06:00
}
2016-02-04 00:28:39 -06:00
/**
2016-12-05 23:52:17 -06:00
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
2016-02-04 00:28:39 -06:00
*
* @return string
2016-02-04 00:28:39 -06:00
*/
2016-12-05 23:52:17 -06:00
public function auditReport(Collection $accounts, Carbon $start, Carbon $end)
2016-02-04 00:28:39 -06:00
{
if ($end < $start) {
2017-03-24 05:07:38 -05:00
return view('error')->with('message', trans('firefly.end_after_start_date')); // @codeCoverageIgnore
}
if ($start < session('first')) {
$start = session('first');
2016-02-04 00:28:39 -06:00
}
View::share(
2017-11-15 03:52:29 -06:00
'subTitle',
trans(
2017-11-15 04:33:07 -06:00
'firefly.report_audit',
[
'start' => $start->formatLocalized($this->monthFormat),
'end' => $end->formatLocalized($this->monthFormat),
]
)
);
$generator = ReportGeneratorFactory::reportGenerator('Audit', $start, $end);
$generator->setAccounts($accounts);
$result = $generator->generate();
2016-02-04 00:28:39 -06:00
return $result;
}
/**
* @param Collection $accounts
2016-12-09 00:07:53 -06:00
* @param Collection $budgets
* @param Carbon $start
* @param Carbon $end
*
* @return string
*/
public function budgetReport(Collection $accounts, Collection $budgets, Carbon $start, Carbon $end)
{
if ($end < $start) {
2017-03-24 05:07:38 -05:00
return view('error')->with('message', trans('firefly.end_after_start_date')); // @codeCoverageIgnore
}
if ($start < session('first')) {
$start = session('first');
}
View::share(
2017-11-15 03:52:29 -06:00
'subTitle',
trans(
2017-11-15 04:33:07 -06:00
'firefly.report_budget',
[
'start' => $start->formatLocalized($this->monthFormat),
'end' => $end->formatLocalized($this->monthFormat),
]
)
);
$generator = ReportGeneratorFactory::reportGenerator('Budget', $start, $end);
$generator->setAccounts($accounts);
$generator->setBudgets($budgets);
$result = $generator->generate();
return $result;
}
2016-02-04 00:28:39 -06:00
/**
* @param Collection $accounts
2016-11-18 13:06:08 -06:00
* @param Collection $categories
2016-12-05 23:52:17 -06:00
* @param Carbon $start
* @param Carbon $end
2016-11-18 13:06:08 -06:00
*
* @return string
2016-02-04 00:28:39 -06:00
*/
2016-12-05 23:52:17 -06:00
public function categoryReport(Collection $accounts, Collection $categories, Carbon $start, Carbon $end)
2016-02-04 00:28:39 -06:00
{
if ($end < $start) {
2017-03-24 05:07:38 -05:00
return view('error')->with('message', trans('firefly.end_after_start_date')); // @codeCoverageIgnore
}
if ($start < session('first')) {
$start = session('first');
2016-02-04 00:28:39 -06:00
}
View::share(
2017-11-15 03:52:29 -06:00
'subTitle',
trans(
2017-11-15 04:33:07 -06:00
'firefly.report_category',
[
'start' => $start->formatLocalized($this->monthFormat),
'end' => $end->formatLocalized($this->monthFormat),
]
)
);
$generator = ReportGeneratorFactory::reportGenerator('Category', $start, $end);
$generator->setAccounts($accounts);
$generator->setCategories($categories);
$result = $generator->generate();
return $result;
}
/**
2016-12-05 23:52:17 -06:00
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
*
* @return string
*/
2016-12-05 23:52:17 -06:00
public function defaultReport(Collection $accounts, Carbon $start, Carbon $end)
{
if ($end < $start) {
return view('error')->with('message', trans('firefly.end_after_start_date'));
}
2016-11-25 09:55:04 -06:00
2016-02-04 00:28:39 -06:00
if ($start < session('first')) {
$start = session('first');
}
View::share(
2017-11-15 03:52:29 -06:00
'subTitle',
trans(
2017-11-15 04:33:07 -06:00
'firefly.report_default',
[
'start' => $start->formatLocalized($this->monthFormat),
'end' => $end->formatLocalized($this->monthFormat),
]
)
);
$generator = ReportGeneratorFactory::reportGenerator('Standard', $start, $end);
$generator->setAccounts($accounts);
$result = $generator->generate();
2016-02-23 13:23:10 -06:00
return $result;
}
/**
* @param AccountRepositoryInterface $repository
*
* @return View
*/
public function index(AccountRepositoryInterface $repository)
{
/** @var Carbon $start */
$start = clone session('first');
$months = $this->helper->listOfMonths($start);
$customFiscalYear = Preferences::get('customFiscalYear', 0)->data;
$accounts = $repository->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET]);
$accountList = join(',', $accounts->pluck('id')->toArray());
2016-02-04 00:28:39 -06:00
return view('reports.index', compact('months', 'accounts', 'start', 'accountList', 'customFiscalYear'));
2016-02-04 00:28:39 -06:00
}
/**
* @param string $reportType
2015-12-12 10:51:07 -06:00
*
* @return mixed
*/
public function options(string $reportType)
{
switch ($reportType) {
default:
$result = $this->noReportOptions();
break;
case 'category':
$result = $this->categoryReportOptions();
break;
case 'budget':
$result = $this->budgetReportOptions();
break;
2017-02-15 13:07:10 -06:00
case 'tag':
$result = $this->tagReportOptions();
break;
}
return Response::json(['html' => $result]);
}
/**
* @param ReportFormRequest $request
*
2017-03-24 05:07:38 -05:00
* @return RedirectResponse|\Illuminate\Routing\Redirector
2017-10-22 13:13:02 -05:00
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
2017-03-24 05:07:38 -05:00
public function postIndex(ReportFormRequest $request)
{
// report type:
$reportType = $request->get('report_type');
$start = $request->getStartDate()->format('Ymd');
$end = $request->getEndDate()->format('Ymd');
$accounts = join(',', $request->getAccountList()->pluck('id')->toArray());
$categories = join(',', $request->getCategoryList()->pluck('id')->toArray());
$budgets = join(',', $request->getBudgetList()->pluck('id')->toArray());
2017-02-15 13:07:10 -06:00
$tags = join(',', $request->getTagList()->pluck('tag')->toArray());
$uri = route('reports.index');
2017-11-15 05:25:49 -06:00
if (0 === $request->getAccountList()->count()) {
2017-03-24 05:07:38 -05:00
Log::debug('Account count is zero');
2016-11-25 09:55:04 -06:00
Session::flash('error', trans('firefly.select_more_than_one_account'));
return redirect(route('reports.index'));
}
2017-11-15 05:25:49 -06:00
if (0 === $request->getCategoryList()->count() && 'category' === $reportType) {
2016-11-28 12:45:36 -06:00
Session::flash('error', trans('firefly.select_more_than_one_category'));
return redirect(route('reports.index'));
}
2017-11-15 05:25:49 -06:00
if (0 === $request->getBudgetList()->count() && 'budget' === $reportType) {
Session::flash('error', trans('firefly.select_more_than_one_budget'));
return redirect(route('reports.index'));
}
2017-11-15 05:25:49 -06:00
if (0 === $request->getTagList()->count() && 'tag' === $reportType) {
2017-02-15 13:07:10 -06:00
Session::flash('error', trans('firefly.select_more_than_one_tag'));
return redirect(route('reports.index'));
}
2017-10-22 13:13:02 -05:00
if ($request->getEndDate() < $request->getStartDate()) {
return view('error')->with('message', trans('firefly.end_after_start_date'));
}
switch ($reportType) {
case 'category':
2016-12-05 23:52:17 -06:00
$uri = route('reports.report.category', [$accounts, $categories, $start, $end]);
break;
case 'default':
2016-12-05 23:52:17 -06:00
$uri = route('reports.report.default', [$accounts, $start, $end]);
break;
case 'audit':
2016-12-05 23:52:17 -06:00
$uri = route('reports.report.audit', [$accounts, $start, $end]);
break;
case 'budget':
$uri = route('reports.report.budget', [$accounts, $budgets, $start, $end]);
break;
2017-02-15 13:07:10 -06:00
case 'tag':
$uri = route('reports.report.tag', [$accounts, $tags, $start, $end]);
break;
}
return redirect($uri);
}
2017-02-16 23:42:36 -06:00
/**
* @param Collection $accounts
* @param Collection $tags
* @param Carbon $start
* @param Carbon $end
*
* @return string
*/
public function tagReport(Collection $accounts, Collection $tags, Carbon $start, Carbon $end)
{
if ($end < $start) {
2017-03-24 05:07:38 -05:00
return view('error')->with('message', trans('firefly.end_after_start_date')); // @codeCoverageIgnore
2017-02-16 23:42:36 -06:00
}
if ($start < session('first')) {
$start = session('first');
}
View::share(
2017-11-15 03:52:29 -06:00
'subTitle',
trans(
2017-11-15 04:33:07 -06:00
'firefly.report_tag',
[
'start' => $start->formatLocalized($this->monthFormat),
'end' => $end->formatLocalized($this->monthFormat),
]
)
2017-02-16 23:42:36 -06:00
);
$generator = ReportGeneratorFactory::reportGenerator('Tag', $start, $end);
$generator->setAccounts($accounts);
$generator->setTags($tags);
$result = $generator->generate();
return $result;
}
/**
* @return string
*/
private function budgetReportOptions(): string
{
/** @var BudgetRepositoryInterface $repository */
$repository = app(BudgetRepositoryInterface::class);
$budgets = $repository->getBudgets();
$result = view('reports.options.budget', compact('budgets'))->render();
return $result;
}
2016-01-24 08:58:16 -06:00
/**
* @return string
2016-01-24 08:58:16 -06:00
*/
private function categoryReportOptions(): string
2016-01-24 08:58:16 -06:00
{
/** @var CategoryRepositoryInterface $repository */
$repository = app(CategoryRepositoryInterface::class);
$categories = $repository->getCategories();
$result = view('reports.options.category', compact('categories'))->render();
return $result;
2016-01-24 08:58:16 -06:00
}
/**
* @return string
*/
private function noReportOptions(): string
{
return view('reports.options.no-options')->render();
}
2017-02-15 13:07:10 -06:00
/**
* @return string
*/
private function tagReportOptions(): string
{
/** @var TagRepositoryInterface $repository */
$repository = app(TagRepositoryInterface::class);
$tags = $repository->get()->sortBy(
function (Tag $tag) {
return $tag->tag;
}
);
$result = view('reports.options.tag', compact('tags'))->render();
return $result;
}
2015-02-23 13:25:48 -06:00
}