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

236 lines
7.4 KiB
PHP
Raw Normal View History

2015-02-23 13:25:48 -06:00
<?php namespace FireflyIII\Http\Controllers;
use Carbon\Carbon;
use FireflyIII\Helpers\Report\ReportHelperInterface;
use FireflyIII\Models\Account;
use FireflyIII\Repositories\Account\AccountRepositoryInterface as ARI;
use Illuminate\Support\Collection;
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
2015-03-29 05:25:46 -05:00
/** @var ReportHelperInterface */
protected $helper;
2015-02-23 13:25:48 -06:00
/**
2015-05-23 13:49:57 -05:00
* @codeCoverageIgnore
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();
2015-03-29 05:25:46 -05:00
$this->helper = $helper;
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');
}
/**
* @param ARI $repository
2015-05-17 02:18:44 -05:00
*
2015-02-23 13:25:48 -06:00
* @return View
2015-05-03 05:58:55 -05:00
* @internal param ReportHelperInterface $helper
2015-02-23 13:25:48 -06:00
*/
public function index(ARI $repository)
2015-02-23 13:25:48 -06:00
{
$start = Session::get('first');
$months = $this->helper->listOfMonths($start);
2015-02-23 13:25:48 -06:00
2015-05-15 13:43:50 -05:00
// does the user have shared accounts?
2015-12-18 09:38:50 -06:00
$accounts = $repository->getAccounts(['Default account', 'Asset account']);
2015-12-18 01:10:41 -06:00
// get id's for quick links:
$accountIds = [];
2015-05-15 13:43:50 -05:00
/** @var Account $account */
2015-12-18 09:38:50 -06:00
foreach ($accounts as $account) {
2015-12-18 01:10:41 -06:00
$accountIds [] = $account->id;
2015-05-15 13:43:50 -05:00
}
2015-12-18 09:38:50 -06:00
$accountList = join(',', $accountIds);
2015-05-15 13:43:50 -05:00
2015-12-28 00:12:47 -06:00
return view(
'reports.index', compact(
2016-01-22 03:10:51 -06:00
'months', 'accounts', 'start', 'accountList'
2015-12-29 15:48:55 -06:00
)
2015-12-28 00:12:47 -06:00
);
2015-12-03 23:56:35 -06:00
}
/**
2015-12-28 13:04:54 -06:00
* @param $reportType
* @param Carbon $start
* @param Carbon $end
* @param Collection $accounts
2015-05-16 06:06:38 -05:00
*
* @return View
2015-05-16 06:06:38 -05:00
*/
2015-12-28 13:04:54 -06:00
public function defaultYear($reportType, Carbon $start, Carbon $end, Collection $accounts)
2015-05-16 06:06:38 -05:00
{
$incomeTopLength = 8;
$expenseTopLength = 8;
2015-12-13 02:41:22 -06:00
$accountReport = $this->helper->getAccountReport($start, $end, $accounts);
$incomes = $this->helper->getIncomeReport($start, $end, $accounts);
$expenses = $this->helper->getExpenseReport($start, $end, $accounts);
2015-05-16 06:06:38 -05:00
2015-05-25 01:12:31 -05:00
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;
}
2015-12-15 05:38:18 -06:00
$accountIds = join(',', $accountIds);
2015-02-24 15:53:38 -06:00
return view(
2015-12-13 02:01:17 -06:00
'reports.default.year',
compact(
2015-12-28 13:04:54 -06:00
'start', 'accountReport', 'incomes', 'reportType', 'accountIds', 'end',
2015-12-14 13:45:12 -06:00
'expenses', 'incomeTopLength', 'expenseTopLength'
)
2015-02-23 13:25:48 -06:00
);
}
/**
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
*/
2015-12-28 13:04:54 -06:00
public function defaultMonth($reportType, Carbon $start, Carbon $end, Collection $accounts)
{
$incomeTopLength = 8;
$expenseTopLength = 8;
// get report stuff!
2016-01-01 12:46:12 -06:00
$accountReport = $this->helper->getAccountReport($start, $end, $accounts); // done (+2)
$incomes = $this->helper->getIncomeReport($start, $end, $accounts); // done (+3)
$expenses = $this->helper->getExpenseReport($start, $end, $accounts); // done (+1)
$budgets = $this->helper->getBudgetReport($start, $end, $accounts); // done (+5)
$categories = $this->helper->getCategoryReport($start, $end, $accounts); // done (+1) (20)
$balance = $this->helper->getBalanceReport($start, $end, $accounts); // +566
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
*/
2015-12-28 13:04:54 -06:00
public function defaultMultiYear($reportType, $start, $end, $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();
$accountReport = $this->helper->getAccountReport($start, $end, $accounts); // done (+2)
$incomes = $this->helper->getIncomeReport($start, $end, $accounts); // done (+3)
$expenses = $this->helper->getExpenseReport($start, $end, $accounts); // done (+1)
// 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
);
}
/**
2015-12-28 13:04:54 -06:00
* @param $reportType
* @param Carbon $start
* @param Carbon $end
* @param Collection $accounts
*
* @return View
*/
2015-12-28 13:04:54 -06:00
public function report($reportType, Carbon $start, Carbon $end, Collection $accounts)
{
// throw an error if necessary.
if ($end < $start) {
return view('error')->with('message', 'End date cannot be before start date, silly!');
}
2015-12-14 13:58:23 -06:00
// lower threshold
2015-12-18 01:10:41 -06:00
if ($start < Session::get('first')) {
2015-12-14 13:58:23 -06:00
$start = Session::get('first');
}
2015-12-28 13:04:54 -06:00
switch ($reportType) {
default:
case 'default':
2015-12-14 13:45:12 -06:00
View::share(
'subTitle', trans(
2015-12-18 01:10:41 -06:00
'firefly.report_default',
[
'start' => $start->formatLocalized($this->monthFormat),
'end' => $end->formatLocalized($this->monthFormat),
2015-12-18 01:10:41 -06:00
]
)
2015-12-14 13:45:12 -06:00
);
View::share('subTitleIcon', 'fa-calendar');
// more than one year date difference means year report.
if ($start->diffInMonths($end) > 12) {
2015-12-28 13:04:54 -06:00
return $this->defaultMultiYear($reportType, $start, $end, $accounts);
}
// more than two months date difference means year report.
if ($start->diffInMonths($end) > 1) {
2015-12-28 13:04:54 -06:00
return $this->defaultYear($reportType, $start, $end, $accounts);
}
2015-12-28 13:04:54 -06:00
return $this->defaultMonth($reportType, $start, $end, $accounts);
}
}
2015-02-23 13:25:48 -06:00
}