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

470 lines
16 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
2017-12-17 07:41:58 -06:00
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
2018-07-08 05:08:53 -05:00
/** @noinspection CallableParameterUseCaseInTypeContextInspection */
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;
2015-02-23 13:25:48 -06:00
/**
2017-11-15 05:25:49 -06:00
* Class ReportController.
2018-07-20 07:34:56 -05:00
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2015-02-23 13:25:48 -06:00
*/
class ReportController extends Controller
{
2015-03-29 05:25:46 -05:00
/** @var ReportHelperInterface */
protected $helper;
2018-07-21 01:06:24 -05:00
/** @var BudgetRepositoryInterface The budget repository */
2018-01-23 22:17:26 -06:00
private $repository;
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
2016-10-29 00:44:46 -05:00
$this->middleware(
function ($request, $next) {
2018-07-15 02:38:49 -05:00
app('view')->share('title', (string)trans('firefly.reports'));
2017-12-16 12:46:36 -06:00
app('view')->share('mainTitleIcon', 'fa-line-chart');
app('view')->share('subTitleIcon', 'fa-calendar');
2018-01-23 22:17:26 -06:00
$this->helper = app(ReportHelperInterface::class);
$this->repository = app(BudgetRepositoryInterface::class);
2016-10-29 00:44:46 -05:00
return $next($request);
}
);
2015-02-23 13:25:48 -06:00
}
2018-07-20 07:34:56 -05:00
/** @noinspection MoreThanThreeArgumentsInspection */
2016-02-04 00:28:39 -06:00
/**
2016-12-05 23:52:17 -06:00
* @param Collection $accounts
* @param Collection $expense
* @param Carbon $start
* @param Carbon $end
2016-02-04 00:28:39 -06:00
*
2018-07-09 12:24:08 -05:00
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|string
2017-12-09 14:49:19 -06:00
* @throws \FireflyIII\Exceptions\FireflyException
*/
2018-07-09 12:24:08 -05:00
public function accountReport(Collection $accounts, Collection $expense, Carbon $start, Carbon $end)
2017-12-09 14:49:19 -06:00
{
if ($end < $start) {
2018-07-15 02:38:49 -05:00
return view('error')->with('message', (string)trans('firefly.end_after_start_date')); // @codeCoverageIgnore
2017-12-09 14:49:19 -06:00
}
2018-01-23 22:17:26 -06:00
$this->repository->cleanupBudgets();
2017-12-09 14:49:19 -06:00
app('view')->share(
2017-12-09 14:49:19 -06:00
'subTitle', trans(
2017-12-23 15:07:39 -06:00
'firefly.report_account',
2017-12-22 11:32:43 -06:00
['start' => $start->formatLocalized($this->monthFormat), 'end' => $end->formatLocalized($this->monthFormat)]
2017-12-16 12:48:31 -06:00
)
2017-12-09 14:49:19 -06:00
);
$generator = ReportGeneratorFactory::reportGenerator('Account', $start, $end);
$generator->setAccounts($accounts);
$generator->setExpense($expense);
2018-03-27 12:29:58 -05:00
return $generator->generate();
2017-12-09 14:49:19 -06:00
}
/**
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
*
2018-07-09 12:24:08 -05:00
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|string
2017-12-22 11:32:43 -06:00
*
2017-12-09 14:49:19 -06:00
* @throws \FireflyIII\Exceptions\FireflyException
2016-02-04 00:28:39 -06:00
*/
2018-07-09 12:24:08 -05:00
public function auditReport(Collection $accounts, Carbon $start, Carbon $end)
2016-02-04 00:28:39 -06:00
{
if ($end < $start) {
2018-07-15 02:38:49 -05:00
return view('error')->with('message', (string)trans('firefly.end_after_start_date')); // @codeCoverageIgnore
}
2018-01-23 22:17:26 -06:00
$this->repository->cleanupBudgets();
2016-02-04 00:28:39 -06:00
app('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);
2016-02-04 00:28:39 -06:00
2018-03-27 12:29:58 -05:00
return $generator->generate();
}
2018-07-20 07:34:56 -05:00
/** @noinspection MoreThanThreeArgumentsInspection */
/**
* @param Collection $accounts
2016-12-09 00:07:53 -06:00
* @param Collection $budgets
* @param Carbon $start
* @param Carbon $end
*
2018-07-09 12:24:08 -05:00
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|string
2017-12-22 11:32:43 -06:00
*
2017-12-09 14:49:19 -06:00
* @throws \FireflyIII\Exceptions\FireflyException
*/
2018-07-09 12:24:08 -05:00
public function budgetReport(Collection $accounts, Collection $budgets, Carbon $start, Carbon $end)
{
if ($end < $start) {
2018-07-15 02:38:49 -05:00
return view('error')->with('message', (string)trans('firefly.end_after_start_date')); // @codeCoverageIgnore
}
2018-01-23 22:17:26 -06:00
$this->repository->cleanupBudgets();
app('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);
2018-04-02 08:10:40 -05:00
return $generator->generate();
}
2018-07-20 07:34:56 -05:00
/** @noinspection MoreThanThreeArgumentsInspection */
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
*
2018-07-13 08:50:42 -05:00
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|string
2017-12-22 11:32:43 -06:00
*
2017-12-09 14:49:19 -06:00
* @throws \FireflyIII\Exceptions\FireflyException
2016-02-04 00:28:39 -06:00
*/
2018-07-09 12:24:08 -05:00
public function categoryReport(Collection $accounts, Collection $categories, Carbon $start, Carbon $end)
2016-02-04 00:28:39 -06:00
{
if ($end < $start) {
2018-07-15 02:38:49 -05:00
return view('error')->with('message', (string)trans('firefly.end_after_start_date')); // @codeCoverageIgnore
}
2018-01-23 22:17:26 -06:00
$this->repository->cleanupBudgets();
2016-02-04 00:28:39 -06:00
app('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);
2018-04-02 08:10:40 -05:00
return $generator->generate();
}
/**
2016-12-05 23:52:17 -06:00
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
*
2018-07-09 12:24:08 -05:00
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|string
2017-12-22 11:32:43 -06:00
*
2017-12-09 14:49:19 -06:00
* @throws \FireflyIII\Exceptions\FireflyException
*/
2018-07-09 12:24:08 -05:00
public function defaultReport(Collection $accounts, Carbon $start, Carbon $end)
{
if ($end < $start) {
2018-07-15 02:38:49 -05:00
return view('error')->with('message', (string)trans('firefly.end_after_start_date'));
}
2016-11-25 09:55:04 -06:00
2018-01-23 22:17:26 -06:00
$this->repository->cleanupBudgets();
2016-02-04 00:28:39 -06:00
app('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);
2016-02-23 13:23:10 -06:00
2018-04-02 08:10:40 -05:00
return $generator->generate();
}
/**
* @param AccountRepositoryInterface $repository
*
2018-07-08 05:08:53 -05:00
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function index(AccountRepositoryInterface $repository)
{
/** @var Carbon $start */
$start = clone session('first');
$months = $this->helper->listOfMonths($start);
$customFiscalYear = app('preferences')->get('customFiscalYear', 0)->data;
$accounts = $repository->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET]);
2018-04-02 08:10:40 -05:00
$accountList = implode(',', $accounts->pluck('id')->toArray());
2018-01-23 22:17:26 -06:00
$this->repository->cleanupBudgets();
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
2018-07-20 07:34:56 -05:00
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
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;
2017-12-09 14:49:19 -06:00
case 'account':
$result = $this->accountReportOptions();
break;
}
2018-03-10 13:30:09 -06:00
return response()->json(['html' => $result]);
}
/**
* @param ReportFormRequest $request
*
2017-03-24 05:07:38 -05:00
* @return RedirectResponse|\Illuminate\Routing\Redirector
2017-12-22 11:32:43 -06:00
*
2017-12-09 14:49:19 -06:00
* @throws \FireflyIII\Exceptions\FireflyException
2018-07-20 07:34:56 -05:00
*
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
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');
2018-04-02 08:10:40 -05:00
$accounts = implode(',', $request->getAccountList()->pluck('id')->toArray());
$categories = implode(',', $request->getCategoryList()->pluck('id')->toArray());
$budgets = implode(',', $request->getBudgetList()->pluck('id')->toArray());
$tags = implode(',', $request->getTagList()->pluck('tag')->toArray());
$expense = implode(',', $request->getExpenseList()->pluck('id')->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');
2018-07-15 02:38:49 -05:00
session()->flash('error', (string)trans('firefly.select_more_than_one_account'));
2016-11-25 09:55:04 -06:00
return redirect(route('reports.index'));
}
2018-07-08 05:08:53 -05:00
if ('category' === $reportType && 0 === $request->getCategoryList()->count()) {
2018-07-15 02:38:49 -05:00
session()->flash('error', (string)trans('firefly.select_more_than_one_category'));
2016-11-28 12:45:36 -06:00
return redirect(route('reports.index'));
}
2018-07-08 05:08:53 -05:00
if ('budget' === $reportType && 0 === $request->getBudgetList()->count()) {
2018-07-15 02:38:49 -05:00
session()->flash('error', (string)trans('firefly.select_more_than_one_budget'));
return redirect(route('reports.index'));
}
2018-07-08 05:08:53 -05:00
if ('tag' === $reportType && 0 === $request->getTagList()->count()) {
2018-07-15 02:38:49 -05:00
session()->flash('error', (string)trans('firefly.select_more_than_one_tag'));
2017-02-15 13:07:10 -06:00
return redirect(route('reports.index'));
}
2017-10-22 13:13:02 -05:00
if ($request->getEndDate() < $request->getStartDate()) {
2018-07-15 02:38:49 -05:00
return view('error')->with('message', (string)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;
2017-12-09 14:49:19 -06:00
case 'account':
$uri = route('reports.report.account', [$accounts, $expense, $start, $end]);
break;
}
return redirect($uri);
}
2018-07-20 07:34:56 -05:00
/** @noinspection MoreThanThreeArgumentsInspection */
2017-02-16 23:42:36 -06:00
/**
* @param Collection $accounts
* @param Collection $tags
* @param Carbon $start
* @param Carbon $end
*
2018-07-09 12:24:08 -05:00
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|string
2017-12-22 11:32:43 -06:00
*
2017-12-09 14:49:19 -06:00
* @throws \FireflyIII\Exceptions\FireflyException
2017-02-16 23:42:36 -06:00
*/
2018-07-09 12:24:08 -05:00
public function tagReport(Collection $accounts, Collection $tags, Carbon $start, Carbon $end)
2017-02-16 23:42:36 -06:00
{
if ($end < $start) {
2018-07-15 02:38:49 -05:00
return view('error')->with('message', (string)trans('firefly.end_after_start_date')); // @codeCoverageIgnore
2017-02-16 23:42:36 -06:00
}
2018-01-23 22:17:26 -06:00
$this->repository->cleanupBudgets();
2017-02-16 23:42:36 -06:00
app('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);
2018-04-02 08:10:40 -05:00
return $generator->generate();
2017-02-16 23:42:36 -06:00
}
/**
* @return string
2017-12-09 14:49:19 -06:00
*/
private function accountReportOptions(): string
{
/** @var AccountRepositoryInterface $repository */
$repository = app(AccountRepositoryInterface::class);
$expense = $repository->getActiveAccountsByType([AccountType::EXPENSE]);
$revenue = $repository->getActiveAccountsByType([AccountType::REVENUE]);
$set = new Collection;
$names = $revenue->pluck('name')->toArray();
foreach ($expense as $exp) {
2018-07-09 12:24:08 -05:00
if (\in_array($exp->name, $names, true)) {
2017-12-09 14:49:19 -06:00
$set->push($exp);
}
}
2018-04-02 08:10:40 -05:00
return view('reports.options.account', compact('set'))->render();
2017-12-09 14:49:19 -06:00
}
/**
* @return string
*/
private function budgetReportOptions(): string
{
/** @var BudgetRepositoryInterface $repository */
$repository = app(BudgetRepositoryInterface::class);
$budgets = $repository->getBudgets();
2018-04-02 08:10:40 -05:00
return view('reports.options.budget', compact('budgets'))->render();
}
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();
2018-04-02 08:10:40 -05:00
return view('reports.options.category', compact('categories'))->render();
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;
}
);
2018-04-02 08:10:40 -05:00
return view('reports.options.tag', compact('tags'))->render();
2017-02-15 13:07:10 -06:00
}
2015-02-23 13:25:48 -06:00
}