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;
|
2015-05-15 13:43:50 -05:00
|
|
|
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
2015-12-06 06:11:43 -06:00
|
|
|
use Illuminate\Support\Collection;
|
2015-03-04 13:47:00 -06:00
|
|
|
use Session;
|
2015-02-23 13:25:48 -06:00
|
|
|
use View;
|
2015-03-04 13:47:00 -06:00
|
|
|
|
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
|
|
|
/**
|
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');
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-05-17 02:18:44 -05:00
|
|
|
* @param AccountRepositoryInterface $repository
|
|
|
|
*
|
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
|
|
|
*/
|
2015-05-15 13:43:50 -05:00
|
|
|
public function index(AccountRepositoryInterface $repository)
|
2015-02-23 13:25:48 -06:00
|
|
|
{
|
2015-05-16 06:06:38 -05: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?
|
|
|
|
$accounts = $repository->getAccounts(['Default account', 'Asset account']);
|
|
|
|
$hasShared = false;
|
|
|
|
|
|
|
|
/** @var Account $account */
|
|
|
|
foreach ($accounts as $account) {
|
|
|
|
if ($account->getMeta('accountRole') == 'sharedAsset') {
|
|
|
|
$hasShared = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-03 23:56:35 -06:00
|
|
|
return view('reports.index', compact('months', 'accounts', 'hasShared', 'start'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-12-12 12:04:30 -06:00
|
|
|
* @param $report_type
|
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
|
|
|
* @param Collection $accounts
|
2015-05-16 06:06:38 -05:00
|
|
|
*
|
2015-12-12 12:04:30 -06:00
|
|
|
* @return View
|
2015-05-16 06:06:38 -05:00
|
|
|
*/
|
2015-12-13 02:01:17 -06:00
|
|
|
public function defaultYear($report_type, 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'));
|
|
|
|
|
2015-12-12 12:04:30 -06:00
|
|
|
// and some id's, joined:
|
|
|
|
$accountIds = [];
|
|
|
|
/** @var Account $account */
|
|
|
|
foreach ($accounts as $account) {
|
|
|
|
$accountIds[] = $account->id;
|
|
|
|
}
|
|
|
|
$accountIds = join(';', $accountIds);
|
|
|
|
|
2015-02-24 15:53:38 -06:00
|
|
|
return view(
|
2015-12-13 02:01:17 -06:00
|
|
|
'reports.default.year',
|
2015-12-12 12:04:30 -06:00
|
|
|
compact(
|
|
|
|
'start', 'accountReport', 'incomes', 'report_type', 'accountIds', 'end',
|
2015-12-14 13:45:12 -06:00
|
|
|
'expenses', 'incomeTopLength', 'expenseTopLength'
|
2015-12-12 12:04:30 -06:00
|
|
|
)
|
2015-02-23 13:25:48 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-12-12 12:04:30 -06:00
|
|
|
|
2015-12-06 06:11:43 -06:00
|
|
|
/**
|
2015-12-12 10:51:07 -06:00
|
|
|
* @param $report_type
|
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
|
|
|
* @param Collection $accounts
|
|
|
|
*
|
|
|
|
* @return View
|
2015-12-06 06:11:43 -06:00
|
|
|
*/
|
2015-12-13 02:01:17 -06:00
|
|
|
public function defaultMonth($report_type, Carbon $start, Carbon $end, Collection $accounts)
|
2015-12-06 06:11:43 -06:00
|
|
|
{
|
|
|
|
$incomeTopLength = 8;
|
|
|
|
$expenseTopLength = 8;
|
|
|
|
|
|
|
|
// get report stuff!
|
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);
|
|
|
|
$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 10:51:07 -06:00
|
|
|
|
|
|
|
// and some id's, joined:
|
|
|
|
$accountIds = [];
|
|
|
|
/** @var Account $account */
|
|
|
|
foreach ($accounts as $account) {
|
|
|
|
$accountIds[] = $account->id;
|
|
|
|
}
|
|
|
|
$accountIds = join(';', $accountIds);
|
2015-12-06 06:11:43 -06:00
|
|
|
|
|
|
|
// continue!
|
|
|
|
return view(
|
2015-12-13 02:01:17 -06:00
|
|
|
'reports.default.month',
|
2015-12-06 06:11:43 -06:00
|
|
|
compact(
|
2015-12-12 10:51:07 -06:00
|
|
|
'start', 'end', 'report_type',
|
|
|
|
'accountReport',
|
2015-12-06 06:11:43 -06:00
|
|
|
'incomes', 'incomeTopLength',
|
|
|
|
'expenses', 'expenseTopLength',
|
2015-12-12 10:51:07 -06:00
|
|
|
'budgets', 'balance',
|
2015-12-06 06:11:43 -06:00
|
|
|
'categories',
|
2015-12-12 10:51:07 -06:00
|
|
|
'bills',
|
|
|
|
'accountIds', 'report_type'
|
2015-12-06 06:11:43 -06:00
|
|
|
)
|
|
|
|
);
|
2015-12-12 12:04:30 -06:00
|
|
|
}
|
|
|
|
|
2015-12-13 10:31:25 -06:00
|
|
|
public function defaultMultiYear($report_type, $start, $end, $accounts)
|
|
|
|
{
|
2015-12-14 13:45:12 -06:00
|
|
|
|
2015-12-13 10:31:25 -06: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;
|
|
|
|
}
|
|
|
|
$accountIds = join(';', $accountIds);
|
|
|
|
|
2015-12-14 13:34:08 -06:00
|
|
|
return view(
|
2015-12-14 13:45:12 -06:00
|
|
|
'reports.default.multi-year', compact('budgets', 'accounts', 'categories', 'start', 'end', 'accountIds', 'report_type')
|
2015-12-14 13:34:08 -06:00
|
|
|
);
|
2015-12-13 10:31:25 -06:00
|
|
|
}
|
|
|
|
|
2015-12-12 12:04:30 -06: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 13:58:23 -06:00
|
|
|
// lower threshold
|
|
|
|
if($start < Session::get('first')) {
|
|
|
|
$start = Session::get('first');
|
|
|
|
}
|
|
|
|
|
2015-12-13 03:18:25 -06:00
|
|
|
switch ($report_type) {
|
|
|
|
default:
|
|
|
|
case 'default':
|
2015-12-14 13:45:12 -06:00
|
|
|
|
|
|
|
View::share(
|
|
|
|
'subTitle', trans(
|
|
|
|
'firefly.report_default',
|
|
|
|
[
|
|
|
|
'start' => $start->formatLocalized($this->monthFormat),
|
|
|
|
'end' => $end->formatLocalized($this->monthFormat)
|
|
|
|
]
|
|
|
|
)
|
|
|
|
);
|
|
|
|
View::share('subTitleIcon', 'fa-calendar');
|
|
|
|
|
2015-12-13 03:18:25 -06:00
|
|
|
// more than one year date difference means year report.
|
|
|
|
if ($start->diffInMonths($end) > 12) {
|
2015-12-14 13:34:08 -06:00
|
|
|
// return view('error')->with('message', 'No report yet for this time period.');
|
2015-12-13 10:31:25 -06:00
|
|
|
return $this->defaultMultiYear($report_type, $start, $end, $accounts);
|
2015-12-13 03:18:25 -06:00
|
|
|
}
|
|
|
|
// 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);
|
|
|
|
break;
|
2015-12-12 12:04:30 -06:00
|
|
|
}
|
|
|
|
|
2015-12-06 06:11:43 -06:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-02-23 13:25:48 -06:00
|
|
|
|
|
|
|
}
|