2016-05-20 08:57:45 +02:00
|
|
|
<?php
|
2016-05-20 12:27:31 +02:00
|
|
|
/**
|
|
|
|
* ReportController.php
|
|
|
|
* Copyright (C) 2016 thegrumpydictator@gmail.com
|
|
|
|
*
|
2016-10-05 06:52:15 +02:00
|
|
|
* 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 12:27:31 +02:00
|
|
|
*/
|
|
|
|
|
2016-05-20 08:57:45 +02:00
|
|
|
declare(strict_types = 1);
|
|
|
|
|
|
|
|
namespace FireflyIII\Http\Controllers;
|
2015-02-23 20:25:48 +01:00
|
|
|
|
|
|
|
use Carbon\Carbon;
|
2016-02-06 05:01:34 +01:00
|
|
|
use FireflyIII\Exceptions\FireflyException;
|
2016-11-09 21:36:54 +01:00
|
|
|
use FireflyIII\Generator\Report\ReportGeneratorFactory;
|
2015-02-23 20:25:48 +01:00
|
|
|
use FireflyIII\Helpers\Report\ReportHelperInterface;
|
2016-11-09 21:36:54 +01:00
|
|
|
use FireflyIII\Http\Requests\ReportFormRequest;
|
2016-05-20 11:02:07 +02:00
|
|
|
use FireflyIII\Models\AccountType;
|
2016-10-10 07:49:39 +02:00
|
|
|
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
2016-11-09 21:36:54 +01:00
|
|
|
use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
|
|
|
|
use Illuminate\Http\RedirectResponse;
|
2015-12-06 13:11:43 +01:00
|
|
|
use Illuminate\Support\Collection;
|
2016-01-29 07:47:18 +01:00
|
|
|
use Preferences;
|
2016-11-09 19:25:09 +01:00
|
|
|
use Response;
|
2015-02-23 20:25:48 +01:00
|
|
|
use View;
|
2015-03-04 20:47:00 +01:00
|
|
|
|
2015-02-23 20:25:48 +01:00
|
|
|
/**
|
|
|
|
* Class ReportController
|
|
|
|
*
|
|
|
|
* @package FireflyIII\Http\Controllers
|
|
|
|
*/
|
|
|
|
class ReportController extends Controller
|
|
|
|
{
|
2015-03-29 12:25:46 +02:00
|
|
|
/** @var ReportHelperInterface */
|
|
|
|
protected $helper;
|
|
|
|
|
2015-02-23 20:25:48 +01:00
|
|
|
/**
|
2016-02-04 07:28:39 +01:00
|
|
|
*
|
2015-02-23 20:25:48 +01:00
|
|
|
*/
|
2016-09-16 09:36:08 +02:00
|
|
|
public function __construct()
|
2015-02-23 20:25:48 +01:00
|
|
|
{
|
2015-05-15 22:00:00 +02:00
|
|
|
parent::__construct();
|
2016-01-27 20:45:49 +01:00
|
|
|
|
2016-10-29 07:44:46 +02:00
|
|
|
|
|
|
|
$this->middleware(
|
|
|
|
function ($request, $next) {
|
|
|
|
View::share('title', trans('firefly.reports'));
|
|
|
|
View::share('mainTitleIcon', 'fa-line-chart');
|
2016-11-10 06:23:21 +01:00
|
|
|
View::share('subTitleIcon', 'fa-calendar');
|
2016-10-29 07:44:46 +02:00
|
|
|
|
2016-10-30 18:29:26 +01:00
|
|
|
$this->helper = app(ReportHelperInterface::class);
|
2016-10-29 07:44:46 +02:00
|
|
|
|
|
|
|
return $next($request);
|
|
|
|
}
|
|
|
|
);
|
2015-02-23 20:25:48 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-02-04 07:28:39 +01:00
|
|
|
/**
|
2016-11-09 21:36:54 +01:00
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
|
|
|
* @param Collection $accounts
|
2016-02-04 07:28:39 +01:00
|
|
|
*
|
2016-11-09 21:36:54 +01:00
|
|
|
* @return string
|
|
|
|
* @throws FireflyException
|
2016-02-04 07:28:39 +01:00
|
|
|
*/
|
2016-11-09 21:36:54 +01:00
|
|
|
public function auditReport(Carbon $start, Carbon $end, Collection $accounts)
|
2016-02-04 07:28:39 +01:00
|
|
|
{
|
2016-11-09 21:36:54 +01:00
|
|
|
if ($end < $start) {
|
2016-11-10 06:23:21 +01:00
|
|
|
return view('error')->with('message', trans('firefly.end_after_start_date'));
|
2016-11-09 21:36:54 +01:00
|
|
|
}
|
|
|
|
if ($start < session('first')) {
|
|
|
|
$start = session('first');
|
2016-02-04 07:28:39 +01:00
|
|
|
}
|
|
|
|
|
2016-11-09 21:36:54 +01:00
|
|
|
View::share(
|
|
|
|
'subTitle', trans(
|
|
|
|
'firefly.report_audit',
|
|
|
|
[
|
|
|
|
'start' => $start->formatLocalized($this->monthFormat),
|
|
|
|
'end' => $end->formatLocalized($this->monthFormat),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
);
|
2016-11-10 06:23:21 +01:00
|
|
|
|
2016-02-04 07:28:39 +01:00
|
|
|
|
2016-11-09 21:36:54 +01:00
|
|
|
$generator = ReportGeneratorFactory::reportGenerator('Audit', $start, $end);
|
|
|
|
$generator->setAccounts($accounts);
|
|
|
|
$result = $generator->generate();
|
2016-02-04 07:28:39 +01:00
|
|
|
|
2016-11-09 21:36:54 +01:00
|
|
|
return $result;
|
2016-11-09 19:25:09 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-02-04 07:28:39 +01:00
|
|
|
/**
|
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
|
|
|
* @param Collection $accounts
|
|
|
|
*
|
2016-11-18 20:06:08 +01:00
|
|
|
* @param Collection $categories
|
|
|
|
*
|
2016-11-09 21:36:54 +01:00
|
|
|
* @return string
|
2016-02-04 07:28:39 +01:00
|
|
|
*/
|
2016-11-10 06:23:21 +01:00
|
|
|
public function categoryReport(Carbon $start, Carbon $end, Collection $accounts, Collection $categories)
|
2016-02-04 07:28:39 +01:00
|
|
|
{
|
|
|
|
if ($end < $start) {
|
2016-11-10 06:23:21 +01:00
|
|
|
return view('error')->with('message', trans('firefly.end_after_start_date'));
|
|
|
|
}
|
|
|
|
if ($start < session('first')) {
|
|
|
|
$start = session('first');
|
2016-02-04 07:28:39 +01:00
|
|
|
}
|
|
|
|
|
2016-11-10 06:23:21 +01: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;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
|
|
|
* @param Collection $accounts
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
* @throws FireflyException
|
|
|
|
*/
|
|
|
|
public function defaultReport(Carbon $start, Carbon $end, Collection $accounts)
|
|
|
|
{
|
|
|
|
if ($end < $start) {
|
|
|
|
return view('error')->with('message', trans('firefly.end_after_start_date'));
|
|
|
|
}
|
2016-02-04 07:28:39 +01:00
|
|
|
if ($start < session('first')) {
|
|
|
|
$start = session('first');
|
|
|
|
}
|
|
|
|
|
2016-04-08 15:10:07 +02:00
|
|
|
View::share(
|
|
|
|
'subTitle', trans(
|
2016-11-09 21:36:54 +01:00
|
|
|
'firefly.report_default',
|
2016-04-08 15:10:07 +02:00
|
|
|
[
|
|
|
|
'start' => $start->formatLocalized($this->monthFormat),
|
|
|
|
'end' => $end->formatLocalized($this->monthFormat),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2016-11-09 21:36:54 +01:00
|
|
|
$generator = ReportGeneratorFactory::reportGenerator('Standard', $start, $end);
|
|
|
|
$generator->setAccounts($accounts);
|
|
|
|
$result = $generator->generate();
|
2016-02-23 20:23:10 +01:00
|
|
|
|
2016-11-09 21:36:54 +01:00
|
|
|
return $result;
|
2016-04-08 14:50:18 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-11-09 21:36:54 +01:00
|
|
|
* @param AccountRepositoryInterface $repository
|
2016-04-08 14:50:18 +02:00
|
|
|
*
|
|
|
|
* @return View
|
|
|
|
*/
|
2016-11-09 21:36:54 +01:00
|
|
|
public function index(AccountRepositoryInterface $repository)
|
2016-04-08 14:50:18 +02:00
|
|
|
{
|
2016-11-09 21:36:54 +01:00
|
|
|
|
|
|
|
/** @var Carbon $start */
|
|
|
|
$start = clone session('first');
|
|
|
|
$months = $this->helper->listOfMonths($start);
|
|
|
|
$customFiscalYear = Preferences::get('customFiscalYear', 0)->data;
|
2016-11-10 06:23:21 +01:00
|
|
|
$accounts = $repository->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET]);
|
|
|
|
$accountList = join(',', $accounts->pluck('id')->toArray());
|
2016-02-04 07:28:39 +01:00
|
|
|
|
2016-04-08 14:50:18 +02:00
|
|
|
|
2016-11-09 21:36:54 +01:00
|
|
|
return view('reports.index', compact('months', 'accounts', 'start', 'accountList', 'customFiscalYear'));
|
2016-02-04 07:28:39 +01:00
|
|
|
}
|
|
|
|
|
2015-12-06 13:11:43 +01:00
|
|
|
/**
|
2016-11-09 21:36:54 +01:00
|
|
|
* @param string $reportType
|
2015-12-12 17:51:07 +01:00
|
|
|
*
|
2016-11-09 21:36:54 +01:00
|
|
|
* @return mixed
|
2015-12-06 13:11:43 +01:00
|
|
|
*/
|
2016-11-09 21:36:54 +01:00
|
|
|
public function options(string $reportType)
|
2015-12-06 13:11:43 +01:00
|
|
|
{
|
2016-11-09 21:36:54 +01:00
|
|
|
switch ($reportType) {
|
|
|
|
default:
|
|
|
|
$result = $this->noReportOptions();
|
|
|
|
break;
|
|
|
|
case 'category':
|
|
|
|
$result = $this->categoryReportOptions();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Response::json(['html' => $result]);
|
2015-12-12 19:04:30 +01:00
|
|
|
}
|
|
|
|
|
2015-12-28 07:55:09 +01:00
|
|
|
/**
|
2016-11-09 21:36:54 +01:00
|
|
|
* @param ReportFormRequest $request
|
2015-12-28 07:55:09 +01:00
|
|
|
*
|
2016-11-09 21:36:54 +01:00
|
|
|
* @return RedirectResponse
|
|
|
|
* @throws FireflyException
|
2015-12-28 07:55:09 +01:00
|
|
|
*/
|
2016-11-09 21:36:54 +01:00
|
|
|
public function postIndex(ReportFormRequest $request): RedirectResponse
|
2015-12-13 17:31:25 +01:00
|
|
|
{
|
2016-11-09 21:36:54 +01:00
|
|
|
// 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());
|
2016-11-02 20:08:11 +01:00
|
|
|
|
2016-11-09 21:36:54 +01:00
|
|
|
if ($end < $start) {
|
2016-11-10 06:23:21 +01:00
|
|
|
return view('error')->with('message', trans('firefly.end_after_start_date'));
|
2016-11-09 21:36:54 +01:00
|
|
|
}
|
2016-11-02 04:55:44 +01:00
|
|
|
|
2016-11-09 21:36:54 +01:00
|
|
|
// lower threshold
|
|
|
|
if ($start < session('first')) {
|
|
|
|
$start = session('first');
|
2015-12-13 17:31:25 +01:00
|
|
|
}
|
|
|
|
|
2016-11-09 21:36:54 +01:00
|
|
|
switch ($reportType) {
|
|
|
|
default:
|
|
|
|
throw new FireflyException(sprintf('Firefly does not support the "%s"-report yet.', $reportType));
|
|
|
|
case 'category':
|
|
|
|
$uri = route('reports.report.category', [$start, $end, $accounts, $categories]);
|
|
|
|
break;
|
|
|
|
case 'default':
|
|
|
|
$uri = route('reports.report.default', [$start, $end, $accounts]);
|
|
|
|
break;
|
|
|
|
case 'audit':
|
|
|
|
$uri = route('reports.report.audit', [$start, $end, $accounts]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return redirect($uri);
|
2015-12-13 17:31:25 +01:00
|
|
|
}
|
|
|
|
|
2016-01-24 15:58:16 +01:00
|
|
|
/**
|
2016-11-09 21:36:54 +01:00
|
|
|
* @return string
|
2016-01-24 15:58:16 +01:00
|
|
|
*/
|
2016-11-09 21:36:54 +01:00
|
|
|
private function categoryReportOptions(): string
|
2016-01-24 15:58:16 +01:00
|
|
|
{
|
2016-11-09 21:36:54 +01:00
|
|
|
/** @var CategoryRepositoryInterface $repository */
|
|
|
|
$repository = app(CategoryRepositoryInterface::class);
|
|
|
|
$categories = $repository->getCategories();
|
|
|
|
$result = view('reports.options.category', compact('categories'))->render();
|
|
|
|
|
|
|
|
return $result;
|
2016-01-24 15:58:16 +01:00
|
|
|
|
|
|
|
}
|
2016-11-09 19:25:09 +01:00
|
|
|
|
|
|
|
/**
|
2016-11-09 21:36:54 +01:00
|
|
|
* @return string
|
2016-11-09 19:25:09 +01:00
|
|
|
*/
|
2016-11-09 21:36:54 +01:00
|
|
|
private function noReportOptions(): string
|
2016-11-09 19:25:09 +01:00
|
|
|
{
|
2016-11-09 21:36:54 +01:00
|
|
|
return view('reports.options.no-options')->render();
|
2016-11-09 19:25:09 +01:00
|
|
|
}
|
2015-02-23 20:25:48 +01:00
|
|
|
}
|