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

233 lines
7.3 KiB
PHP
Raw Normal View History

2015-02-23 20:25:48 +01:00
<?php namespace FireflyIII\Http\Controllers;
use Carbon\Carbon;
use FireflyIII\Helpers\Report\ReportHelperInterface;
use FireflyIII\Models\Account;
2015-05-15 20:43:50 +02:00
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use Illuminate\Support\Collection;
use Session;
2015-02-23 20:25:48 +01:00
use View;
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
/**
2015-05-23 20:49:57 +02:00
* @codeCoverageIgnore
2015-05-24 11:41:52 +02:00
*
2015-03-29 12:25:46 +02:00
* @param ReportHelperInterface $helper
2015-02-23 20:25:48 +01:00
*/
2015-05-16 16:04:51 +02:00
public function __construct(ReportHelperInterface $helper)
2015-02-23 20:25:48 +01:00
{
2015-05-15 22:00:00 +02:00
parent::__construct();
2015-03-29 12:25:46 +02:00
$this->helper = $helper;
2015-05-15 21:01:24 +02:00
View::share('title', trans('firefly.reports'));
2015-02-23 20:25:48 +01:00
View::share('mainTitleIcon', 'fa-line-chart');
}
/**
2015-05-17 09:18:44 +02:00
* @param AccountRepositoryInterface $repository
*
2015-02-23 20:25:48 +01:00
* @return View
2015-05-03 12:58:55 +02:00
* @internal param ReportHelperInterface $helper
2015-02-23 20:25:48 +01:00
*/
2015-05-15 20:43:50 +02:00
public function index(AccountRepositoryInterface $repository)
2015-02-23 20:25:48 +01:00
{
$start = Session::get('first');
$months = $this->helper->listOfMonths($start);
$startOfMonth = clone Session::get('start');
$endOfMonth = clone Session::get('start');
$startOfYear = clone Session::get('start');
$endOfYear = clone Session::get('start');
$startOfMonth->startOfMonth();
$endOfMonth->endOfMonth();
$startOfYear->startOfYear();
$endOfYear->endOfYear();
2015-02-23 20:25:48 +01:00
2015-05-15 20:43:50 +02:00
// does the user have shared accounts?
2015-12-18 16:38:50 +01:00
$accounts = $repository->getAccounts(['Default account', 'Asset account']);
2015-12-18 08:10:41 +01:00
// get id's for quick links:
$accountIds = [];
2015-05-15 20:43:50 +02:00
/** @var Account $account */
2015-12-18 16:38:50 +01:00
foreach ($accounts as $account) {
2015-12-18 08:10:41 +01:00
$accountIds [] = $account->id;
2015-05-15 20:43:50 +02:00
}
2015-12-18 16:38:50 +01:00
$accountList = join(',', $accountIds);
2015-05-15 20:43:50 +02:00
2015-12-28 07:12:47 +01:00
return view(
'reports.index', compact(
'months', 'accounts', 'start', 'accountList',
'startOfMonth', 'endOfMonth', 'startOfYear', 'endOfYear'
)
);
2015-12-04 06:56:35 +01:00
}
/**
* @param $report_type
* @param Carbon $start
* @param Carbon $end
* @param Collection $accounts
2015-05-16 13:06:38 +02:00
*
* @return View
2015-05-16 13:06:38 +02:00
*/
2015-12-13 09:01:17 +01:00
public function defaultYear($report_type, Carbon $start, Carbon $end, Collection $accounts)
2015-05-16 13:06:38 +02:00
{
$incomeTopLength = 8;
$expenseTopLength = 8;
2015-12-13 09:41:22 +01: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 13:06:38 +02:00
2015-05-25 08:12:31 +02: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 12:38:18 +01:00
$accountIds = join(',', $accountIds);
2015-02-24 22:53:38 +01:00
return view(
2015-12-13 09:01:17 +01:00
'reports.default.year',
compact(
'start', 'accountReport', 'incomes', 'report_type', 'accountIds', 'end',
2015-12-14 20:45:12 +01:00
'expenses', 'incomeTopLength', 'expenseTopLength'
)
2015-02-23 20:25:48 +01:00
);
}
/**
2015-12-12 17:51:07 +01:00
* @param $report_type
* @param Carbon $start
* @param Carbon $end
* @param Collection $accounts
*
* @return View
*/
2015-12-13 09:01:17 +01:00
public function defaultMonth($report_type, Carbon $start, Carbon $end, Collection $accounts)
{
$incomeTopLength = 8;
$expenseTopLength = 8;
// get report stuff!
2015-12-13 09:41:22 +01:00
$accountReport = $this->helper->getAccountReport($start, $end, $accounts);
$incomes = $this->helper->getIncomeReport($start, $end, $accounts);
$expenses = $this->helper->getExpenseReport($start, $end, $accounts);
$budgets = $this->helper->getBudgetReport($start, $end, $accounts);
$categories = $this->helper->getCategoryReport($start, $end, $accounts);
$balance = $this->helper->getBalanceReport($start, $end, $accounts);
$bills = $this->helper->getBillReport($start, $end, $accounts);
2015-12-12 17:51:07 +01:00
// and some id's, joined:
$accountIds = [];
/** @var Account $account */
foreach ($accounts as $account) {
$accountIds[] = $account->id;
}
2015-12-15 12:38:18 +01:00
$accountIds = join(',', $accountIds);
// continue!
return view(
2015-12-13 09:01:17 +01:00
'reports.default.month',
compact(
2015-12-12 17:51:07 +01:00
'start', 'end', 'report_type',
'accountReport',
'incomes', 'incomeTopLength',
'expenses', 'expenseTopLength',
2015-12-12 17:51:07 +01:00
'budgets', 'balance',
'categories',
2015-12-12 17:51:07 +01:00
'bills',
'accountIds', 'report_type'
)
);
}
public function defaultMultiYear($report_type, $start, $end, $accounts)
{
2015-12-14 20:45:12 +01:00
// list of users stuff:
$budgets = app('FireflyIII\Repositories\Budget\BudgetRepositoryInterface')->getActiveBudgets();
$categories = app('FireflyIII\Repositories\Category\CategoryRepositoryInterface')->getCategories();
// and some id's, joined:
$accountIds = [];
/** @var Account $account */
foreach ($accounts as $account) {
$accountIds[] = $account->id;
}
2015-12-15 12:38:18 +01:00
$accountIds = join(',', $accountIds);
2015-12-14 20:34:08 +01:00
return view(
2015-12-14 20:45:12 +01:00
'reports.default.multi-year', compact('budgets', 'accounts', 'categories', 'start', 'end', 'accountIds', 'report_type')
2015-12-14 20:34:08 +01:00
);
}
/**
* @param $report_type
* @param Carbon $start
* @param Carbon $end
* @param Collection $accounts
*
* @return View
*/
public function report($report_type, 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 20:58:23 +01:00
// lower threshold
2015-12-18 08:10:41 +01:00
if ($start < Session::get('first')) {
2015-12-14 20:58:23 +01:00
$start = Session::get('first');
}
switch ($report_type) {
default:
case 'default':
2015-12-14 20:45:12 +01:00
View::share(
'subTitle', trans(
2015-12-18 08:10:41 +01:00
'firefly.report_default',
[
'start' => $start->formatLocalized($this->monthFormat),
'end' => $end->formatLocalized($this->monthFormat),
2015-12-18 08:10:41 +01:00
]
)
2015-12-14 20:45:12 +01:00
);
View::share('subTitleIcon', 'fa-calendar');
// more than one year date difference means year report.
if ($start->diffInMonths($end) > 12) {
return $this->defaultMultiYear($report_type, $start, $end, $accounts);
}
// more than two months date difference means year report.
if ($start->diffInMonths($end) > 1) {
return $this->defaultYear($report_type, $start, $end, $accounts);
}
return $this->defaultMonth($report_type, $start, $end, $accounts);
}
}
2015-02-23 20:25:48 +01:00
}