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

271 lines
8.3 KiB
PHP
Raw Normal View History

2015-02-23 13:25:48 -06:00
<?php namespace FireflyIII\Http\Controllers;
use Auth;
2015-02-23 13:25:48 -06:00
use Carbon\Carbon;
use Exception;
2015-02-23 13:25:48 -06:00
use FireflyIII\Helpers\Report\ReportHelperInterface;
use FireflyIII\Models\Account;
2015-05-15 13:43:50 -05:00
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use Illuminate\Support\Collection;
2015-12-03 23:56:35 -06:00
use Input;
use Log;
2015-12-03 23:56:35 -06:00
use Redirect;
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
{
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'));
}
/**
* TODO needs a custom validator for ease of use.
*
* @param AccountRepositoryInterface $repository
*
* @return \Illuminate\Http\RedirectResponse
*/
public function select(AccountRepositoryInterface $repository)
{
// process post data, give error, otherwise send redirect.
$report = Input::get('report_type');
$parts = [$report];
// date
$ranges = explode(' - ', Input::get('daterange'));
$start = clone Session::get('start');
$end = clone Session::get('end');
// kind of primitive but OK for now.
if (count($ranges) == 2 && strlen($ranges[0]) == 10 && strlen($ranges[1]) == 10) {
$start = new Carbon($ranges[0]);
$end = new Carbon($ranges[1]);
}
if ($end <= $start) {
Session::flash('error', 'Messed up the date!');
return Redirect::route('reports.index');
}
$parts[] = $start->format('Ymd');
$parts[] = $end->format('Ymd');
if (is_array(Input::get('accounts'))) {
foreach (Input::get('accounts') as $accountId) {
$account = $repository->find($accountId);
if ($account) {
$parts[] = $account->id;
}
}
}
if (count($parts) == 3) {
Session::flash('error', 'Select some accounts!');
return Redirect::route('reports.index');
}
$url = join(';', $parts);
return Redirect::route('reports.report', [$url]);
2015-02-23 13:25:48 -06:00
}
/**
* @param string $year
* @param string $month
*
2015-05-17 02:18:44 -05:00
* @param bool $shared
*
2015-02-23 13:25:48 -06:00
* @return \Illuminate\View\View
*/
2015-05-15 13:38:39 -05:00
public function month($year = '2014', $month = '1', $shared = false)
2015-02-23 13:25:48 -06:00
{
2015-05-16 06:06:38 -05:00
$start = new Carbon($year . '-' . $month . '-01');
2015-06-21 03:50:45 -05:00
$subTitle = trans('firefly.reportForMonth', ['month' => $start->formatLocalized($this->monthFormat)]);
2015-05-16 06:06:38 -05:00
$subTitleIcon = 'fa-calendar';
$end = clone $start;
2015-05-16 01:05:04 -05:00
$incomeTopLength = 8;
$expenseTopLength = 8;
2015-05-15 13:38:39 -05:00
if ($shared == 'shared') {
2015-05-15 15:00:00 -05:00
$shared = true;
2015-06-21 03:50:45 -05:00
$subTitle = trans('firefly.reportForMonthShared', ['month' => $start->formatLocalized($this->monthFormat)]);
2015-05-15 13:38:39 -05:00
}
2015-05-15 14:01:24 -05:00
2015-05-16 06:06:38 -05:00
$end->endOfMonth();
2015-05-15 15:00:00 -05:00
2015-05-16 06:53:08 -05:00
$accounts = $this->helper->getAccountReport($start, $end, $shared);
$incomes = $this->helper->getIncomeReport($start, $end, $shared);
$expenses = $this->helper->getExpenseReport($start, $end, $shared);
$budgets = $this->helper->getBudgetReport($start, $end, $shared);
$categories = $this->helper->getCategoryReport($start, $end, $shared);
2015-05-16 07:51:23 -05:00
$balance = $this->helper->getBalanceReport($start, $end, $shared);
2015-07-06 18:07:19 -05:00
$bills = $this->helper->getBillReport($start, $end);
2015-05-16 06:06:38 -05:00
2015-05-25 01:12:31 -05:00
Session::flash('gaEventCategory', 'report');
Session::flash('gaEventAction', 'month');
Session::flash('gaEventLabel', $start->format('F Y'));
2015-05-16 06:06:38 -05:00
return view(
'reports.month',
compact(
'start', 'shared',
'subTitle', 'subTitleIcon',
2015-05-16 06:53:08 -05:00
'accounts',
'incomes', 'incomeTopLength',
'expenses', 'expenseTopLength',
2015-05-17 02:18:44 -05:00
'budgets', 'balance',
'categories',
'bills'
2015-05-16 06:06:38 -05:00
)
);
}
/**
2015-05-17 02:18:44 -05:00
* @param $year
*
* @param bool $shared
2015-05-16 06:06:38 -05:00
*
* @return $this
*/
public function year($year, $shared = false)
{
$start = new Carbon('01-01-' . $year);
$end = clone $start;
$subTitle = trans('firefly.reportForYear', ['year' => $year]);
$subTitleIcon = 'fa-bar-chart';
$incomeTopLength = 8;
$expenseTopLength = 8;
if ($shared == 'shared') {
$shared = true;
$subTitle = trans('firefly.reportForYearShared', ['year' => $year]);
}
$end->endOfYear();
$accounts = $this->helper->getAccountReport($start, $end, $shared);
$incomes = $this->helper->getIncomeReport($start, $end, $shared);
$expenses = $this->helper->getExpenseReport($start, $end, $shared);
2015-05-25 01:12:31 -05:00
Session::flash('gaEventCategory', 'report');
Session::flash('gaEventAction', 'year');
Session::flash('gaEventLabel', $start->format('Y'));
2015-02-24 15:53:38 -06:00
return view(
2015-05-15 15:00:00 -05:00
'reports.year',
2015-06-04 14:35:36 -05:00
compact('start', 'shared', 'accounts', 'incomes', 'expenses', 'subTitle', 'subTitleIcon', 'incomeTopLength', 'expenseTopLength')
2015-02-23 13:25:48 -06:00
);
}
/**
* @param $url
*/
public function report($url, AccountRepositoryInterface $repository)
{
$parts = explode(';', $url);
// try to make a date out of parts 1 and 2:
try {
$start = new Carbon($parts[1]);
$end = new Carbon($parts[2]);
} catch (Exception $e) {
Log::error('Could not parse date "' . $parts[1] . '" or "' . $parts[2] . '" for user #' . Auth::user()->id);
abort(404);
}
if ($end < $start) {
abort(404);
}
// accounts:
$c = count($parts);
$list = new Collection();
for ($i = 3; $i < $c; $i++) {
$account = $repository->find($parts[$i]);
if ($account) {
$list->push($account);
}
}
// some fields:
$subTitle = trans('firefly.reportForMonth', ['month' => $start->formatLocalized($this->monthFormat)]);
$subTitleIcon = 'fa-calendar';
$incomeTopLength = 8;
$expenseTopLength = 8;
// get report stuff!
$accounts = $this->helper->getAccountReportForList($start, $end, $list);
$incomes = $this->helper->getIncomeReportForList($start, $end, $list);
$expenses = $this->helper->getExpenseReportForList($start, $end, $list);
$budgets = $this->helper->getBudgetReportForList($start, $end, $list);
// $categories = $this->helper->getCategoryReportForList($start, $end, $list);
// $balance = $this->helper->getBalanceReportForList($start, $end, $list);
// $bills = $this->helper->getBillReportForList($start, $end);
// continue!
return view(
'reports.default',
compact(
'start',
'subTitle', 'subTitleIcon',
'accounts',
'incomes', 'incomeTopLength',
'expenses', 'expenseTopLength',
'budgets', 'balance',
'categories',
'bills'
)
);
}
2015-02-23 13:25:48 -06:00
}