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

345 lines
10 KiB
PHP
Raw Normal View History

2016-05-20 01:57:45 -05:00
<?php
/**
* ReportController.php
* Copyright (C) 2016 thegrumpydictator@gmail.com
*
* This software may be modified and distributed under the terms of the
* Creative Commons Attribution-ShareAlike 4.0 International License.
*
* See the LICENSE file for details.
*/
2016-05-20 01:57:45 -05:00
declare(strict_types = 1);
namespace FireflyIII\Http\Controllers;
2015-02-23 13:25:48 -06:00
use Carbon\Carbon;
2016-02-05 22:01:34 -06:00
use FireflyIII\Exceptions\FireflyException;
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;
2016-10-10 00:49:39 -05:00
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Collection;
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
/**
* Class ReportController
*
* @package FireflyIII\Http\Controllers
*/
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
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
2016-10-30 12:29:26 -05:00
$this->helper = app(ReportHelperInterface::class);
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) {
return view('error')->with('message', trans('firefly.end_after_start_date'));
}
if ($start < session('first')) {
$start = session('first');
2016-02-04 00:28:39 -06:00
}
View::share(
'subTitle', trans(
'firefly.report_audit',
[
'start' => $start->formatLocalized($this->monthFormat),
'end' => $end->formatLocalized($this->monthFormat),
]
)
);
2016-02-04 00:28:39 -06:00
$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) {
return view('error')->with('message', trans('firefly.end_after_start_date'));
}
if ($start < session('first')) {
$start = session('first');
}
View::share(
'subTitle', trans(
'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) {
return view('error')->with('message', trans('firefly.end_after_start_date'));
}
if ($start < session('first')) {
$start = session('first');
2016-02-04 00:28:39 -06:00
}
View::share(
'subTitle', trans(
'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(
'subTitle', trans(
'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;
}
return Response::json(['html' => $result]);
}
/**
* @param ReportFormRequest $request
*
* @return RedirectResponse
* @throws FireflyException
*/
public function postIndex(ReportFormRequest $request): RedirectResponse
{
// 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());
2016-11-25 09:55:04 -06:00
if ($request->getAccountList()->count() === 0) {
Session::flash('error', trans('firefly.select_more_than_one_account'));
return redirect(route('reports.index'));
}
2016-11-28 12:45:36 -06:00
if ($request->getCategoryList()->count() === 0 && $reportType === 'category') {
Session::flash('error', trans('firefly.select_more_than_one_category'));
return redirect(route('reports.index'));
}
if ($request->getBudgetList()->count() === 0 && $reportType === 'budget') {
Session::flash('error', trans('firefly.select_more_than_one_budget'));
return redirect(route('reports.index'));
}
if ($end < $start) {
return view('error')->with('message', trans('firefly.end_after_start_date'));
}
// lower threshold
if ($start < session('first')) {
$start = session('first');
}
switch ($reportType) {
default:
throw new FireflyException(sprintf('Firefly does not support the "%s"-report yet.', $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;
}
return redirect($uri);
}
/**
* @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();
}
2015-02-23 13:25:48 -06:00
}