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

244 lines
7.9 KiB
PHP
Raw Normal View History

2015-02-23 13:25:48 -06:00
<?php namespace FireflyIII\Http\Controllers;
use Carbon\Carbon;
2016-02-05 22:01:34 -06:00
use FireflyIII\Exceptions\FireflyException;
2015-02-23 13:25:48 -06:00
use FireflyIII\Helpers\Report\ReportHelperInterface;
use FireflyIII\Models\Account;
use FireflyIII\Repositories\Account\AccountRepositoryInterface as ARI;
use Illuminate\Support\Collection;
2016-02-04 00:28:39 -06:00
use Log;
2016-01-29 00:47:18 -06:00
use Preferences;
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
{
2016-01-08 11:29:47 -06:00
2016-01-27 13:45:49 -06:00
protected $accountHelper;
2016-01-27 14:35:59 -06:00
protected $balanceHelper;
2016-01-27 14:18:51 -06:00
protected $budgetHelper;
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-05-24 04:41:52 -05:00
*
2015-03-29 05:25:46 -05:00
* @param ReportHelperInterface $helper
2015-02-23 13:25:48 -06:00
*/
2015-05-16 09:04:51 -05:00
public function __construct(ReportHelperInterface $helper)
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
$this->helper = $helper;
$this->accountHelper = app('FireflyIII\Helpers\Report\AccountReportHelperInterface');
2016-01-27 14:18:51 -06:00
$this->budgetHelper = app('FireflyIII\Helpers\Report\BudgetReportHelperInterface');
2016-01-27 14:35:59 -06:00
$this->balanceHelper = app('FireflyIII\Helpers\Report\BalanceReportHelperInterface');
2015-03-29 05:25:46 -05:00
2015-05-15 14:01:24 -05:00
View::share('title', trans('firefly.reports'));
2015-02-23 13:25:48 -06:00
View::share('mainTitleIcon', 'fa-line-chart');
}
2016-02-04 00:28:39 -06:00
/**
* @param ARI $repository
*
* @return View
* @internal param ReportHelperInterface $helper
*/
public function index(ARI $repository)
{
2016-02-05 08:41:40 -06:00
/** @var Carbon $start */
2016-02-04 00:28:39 -06:00
$start = session('first');
$months = $this->helper->listOfMonths($start);
$customFiscalYear = Preferences::get('customFiscalYear', 0)->data;
// does the user have shared accounts?
$accounts = $repository->getAccounts(['Default account', 'Asset account']);
// get id's for quick links:
$accountIds = [];
/** @var Account $account */
foreach ($accounts as $account) {
$accountIds [] = $account->id;
}
$accountList = join(',', $accountIds);
return view('reports.index', compact('months', 'accounts', 'start', 'accountList', 'customFiscalYear'));
}
/**
* @param $reportType
* @param Carbon $start
* @param Carbon $end
* @param Collection $accounts
*
* @return View
*/
2016-02-05 02:25:15 -06:00
public function report(string $reportType, Carbon $start, Carbon $end, Collection $accounts)
2016-02-04 00:28:39 -06:00
{
// throw an error if necessary.
if ($end < $start) {
2016-02-05 22:04:41 -06:00
throw new FireflyException('End date cannot be before start date, silly!');
2016-02-04 00:28:39 -06:00
}
// lower threshold
if ($start < session('first')) {
Log::debug('Start is ' . $start . ' but sessionfirst is ' . session('first'));
$start = session('first');
}
switch ($reportType) {
default:
case 'default':
View::share(
'subTitle', trans(
'firefly.report_default',
[
'start' => $start->formatLocalized($this->monthFormat),
'end' => $end->formatLocalized($this->monthFormat),
]
)
);
View::share('subTitleIcon', 'fa-calendar');
// more than one year date difference means year report.
if ($start->diffInMonths($end) > 12) {
return $this->defaultMultiYear($reportType, $start, $end, $accounts);
}
// more than two months date difference means year report.
if ($start->diffInMonths($end) > 1) {
return $this->defaultYear($reportType, $start, $end, $accounts);
}
return $this->defaultMonth($reportType, $start, $end, $accounts);
}
}
/**
2015-12-28 13:04:54 -06:00
* @param $reportType
2015-12-12 10:51:07 -06:00
* @param Carbon $start
* @param Carbon $end
* @param Collection $accounts
*
* @return View
*/
2016-02-05 02:25:15 -06:00
private function defaultMonth(string $reportType, Carbon $start, Carbon $end, Collection $accounts)
{
$incomeTopLength = 8;
$expenseTopLength = 8;
// get report stuff!
2016-01-27 14:52:21 -06:00
$accountReport = $this->accountHelper->getAccountReport($start, $end, $accounts);
$incomes = $this->helper->getIncomeReport($start, $end, $accounts);
$expenses = $this->helper->getExpenseReport($start, $end, $accounts);
$budgets = $this->budgetHelper->getBudgetReport($start, $end, $accounts);
$categories = $this->helper->getCategoryReport($start, $end, $accounts);
$balance = $this->balanceHelper->getBalanceReport($start, $end, $accounts);
2016-01-01 12:52:55 -06:00
$bills = $this->helper->getBillReport($start, $end, $accounts);
2015-12-12 10:51:07 -06:00
// and some id's, joined:
2016-01-01 12:46:12 -06:00
$accountIds = join(',', $accounts->pluck('id')->toArray());
// continue!
return view(
2015-12-13 02:01:17 -06:00
'reports.default.month',
compact(
2015-12-28 13:04:54 -06:00
'start', 'end', 'reportType',
2015-12-12 10:51:07 -06:00
'accountReport',
'incomes', 'incomeTopLength',
'expenses', 'expenseTopLength',
2015-12-12 10:51:07 -06:00
'budgets', 'balance',
'categories',
2015-12-12 10:51:07 -06:00
'bills',
2015-12-28 13:04:54 -06:00
'accountIds', 'reportType'
)
);
}
/**
2015-12-28 13:04:54 -06:00
* @param $reportType
* @param $start
* @param $end
* @param $accounts
*
* @return View
*/
2016-02-05 02:25:15 -06:00
private function defaultMultiYear(string $reportType, Carbon $start, Carbon $end, Collection $accounts)
{
2015-12-14 13:45:12 -06:00
2016-01-05 14:23:58 -06:00
$incomeTopLength = 8;
$expenseTopLength = 8;
// list of users stuff:
2016-01-05 14:23:58 -06:00
$budgets = app('FireflyIII\Repositories\Budget\BudgetRepositoryInterface')->getActiveBudgets();
$categories = app('FireflyIII\Repositories\Category\CategoryRepositoryInterface')->listCategories();
2016-01-27 14:52:21 -06:00
$accountReport = $this->accountHelper->getAccountReport($start, $end, $accounts);
$incomes = $this->helper->getIncomeReport($start, $end, $accounts);
$expenses = $this->helper->getExpenseReport($start, $end, $accounts);
// and some id's, joined:
$accountIds = [];
/** @var Account $account */
foreach ($accounts as $account) {
$accountIds[] = $account->id;
}
2015-12-15 05:38:18 -06:00
$accountIds = join(',', $accountIds);
2015-12-14 13:34:08 -06:00
return view(
2016-01-05 14:23:58 -06:00
'reports.default.multi-year',
compact(
'budgets', 'accounts', 'categories', 'start', 'end', 'accountIds', 'reportType', 'accountReport', 'incomes', 'expenses',
'incomeTopLength', 'expenseTopLength'
)
2015-12-14 13:34:08 -06:00
);
}
2016-01-24 08:58:16 -06:00
/**
* @param $reportType
* @param Carbon $start
* @param Carbon $end
* @param Collection $accounts
*
* @return View
*/
2016-02-05 02:25:15 -06:00
private function defaultYear(string $reportType, Carbon $start, Carbon $end, Collection $accounts)
2016-01-24 08:58:16 -06:00
{
$incomeTopLength = 8;
$expenseTopLength = 8;
2016-01-27 13:46:38 -06:00
$accountReport = $this->accountHelper->getAccountReport($start, $end, $accounts);
2016-01-24 08:58:16 -06:00
$incomes = $this->helper->getIncomeReport($start, $end, $accounts);
$expenses = $this->helper->getExpenseReport($start, $end, $accounts);
Session::flash('gaEventCategory', 'report');
Session::flash('gaEventAction', 'year');
Session::flash('gaEventLabel', $start->format('Y'));
// and some id's, joined:
$accountIds = [];
/** @var Account $account */
foreach ($accounts as $account) {
$accountIds[] = $account->id;
}
$accountIds = join(',', $accountIds);
return view(
'reports.default.year',
compact(
'start', 'accountReport', 'incomes', 'reportType', 'accountIds', 'end',
'expenses', 'incomeTopLength', 'expenseTopLength'
)
);
}
2015-02-23 13:25:48 -06:00
}