Jump to year report if the period is too long.

This commit is contained in:
James Cole
2015-12-12 19:04:30 +01:00
parent 61bbe8a905
commit a2ccbf7844
5 changed files with 87 additions and 168 deletions

View File

@@ -9,7 +9,6 @@ use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Support\CacheProperties; use FireflyIII\Support\CacheProperties;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Response; use Response;
use Log;
/** /**
* Class ReportController * Class ReportController
@@ -37,23 +36,20 @@ class ReportController extends Controller
* Summarizes all income and expenses, per month, for a given year. * Summarizes all income and expenses, per month, for a given year.
* *
* @param ReportQueryInterface $query * @param ReportQueryInterface $query
* @param $year * @param $report_type
* @param bool $shared * @param Carbon $start
* @param Carbon $end
* @param Collection $accounts
* *
* @return \Symfony\Component\HttpFoundation\Response * @return \Illuminate\Http\JsonResponse
*/ */
public function yearInOut(ReportQueryInterface $query, $year, $shared = false) public function yearInOut(ReportQueryInterface $query, $report_type, Carbon $start, Carbon $end, Collection $accounts)
{ {
// get start and end of year
$start = new Carbon($year . '-01-01');
$end = new Carbon($year . '-12-31');
$shared = $shared == 'shared' ? true : false;
// chart properties for cache: // chart properties for cache:
$cache = new CacheProperties; $cache = new CacheProperties;
$cache->addProperty('yearInOut'); $cache->addProperty('yearInOut');
$cache->addProperty($year); $cache->addProperty($start);
$cache->addProperty($shared); $cache->addProperty($end);
if ($cache->has()) { if ($cache->has()) {
return Response::json($cache->get()); // @codeCoverageIgnore return Response::json($cache->get()); // @codeCoverageIgnore
} }
@@ -63,8 +59,8 @@ class ReportController extends Controller
$month = clone $start; $month = clone $start;
$month->endOfMonth(); $month->endOfMonth();
// total income and total expenses: // total income and total expenses:
$incomeSum = $query->incomeInPeriodCorrected($start, $month, $shared)->sum('amount_positive'); $incomeSum = $query->incomeInPeriodCorrectedForList($start, $month, $accounts)->sum('amount_positive');
$expenseSum = $query->expenseInPeriodCorrected($start, $month, $shared)->sum('amount_positive'); $expenseSum = $query->expenseInPeriodCorrectedForList($start, $month, $accounts)->sum('amount_positive');
$entries->push([clone $start, $incomeSum, $expenseSum]); $entries->push([clone $start, $incomeSum, $expenseSum]);
$start->addMonth(); $start->addMonth();
@@ -81,26 +77,26 @@ class ReportController extends Controller
* Summarizes all income and expenses for a given year. Gives a total and an average. * Summarizes all income and expenses for a given year. Gives a total and an average.
* *
* @param ReportQueryInterface $query * @param ReportQueryInterface $query
* @param $year * @param $report_type
* @param bool $shared * @param Carbon $start
* @param Carbon $end
* @param Collection $accounts
* *
* @return \Symfony\Component\HttpFoundation\Response * @return \Illuminate\Http\JsonResponse
*/ */
public function yearInOutSummarized(ReportQueryInterface $query, $year, $shared = false) public function yearInOutSummarized(ReportQueryInterface $query, $report_type, Carbon $start, Carbon $end, Collection $accounts)
{ {
// chart properties for cache: // chart properties for cache:
$cache = new CacheProperties; $cache = new CacheProperties;
$cache->addProperty('yearInOutSummarized'); $cache->addProperty('yearInOutSummarized');
$cache->addProperty($year); $cache->addProperty($start);
$cache->addProperty($shared); $cache->addProperty($end);
$cache->addProperty($accounts);
if ($cache->has()) { if ($cache->has()) {
return Response::json($cache->get()); // @codeCoverageIgnore return Response::json($cache->get()); // @codeCoverageIgnore
} }
$start = new Carbon($year . '-01-01');
$end = new Carbon($year . '-12-31');
$shared = $shared == 'shared' ? true : false;
$income = '0'; $income = '0';
$expense = '0'; $expense = '0';
$count = 0; $count = 0;
@@ -111,18 +107,11 @@ class ReportController extends Controller
$month = clone $start; $month = clone $start;
$month->endOfMonth(); $month->endOfMonth();
// total income and total expenses: // total income and total expenses:
$currentIncome = $query->incomeInPeriodCorrected($start, $month, $shared)->sum('amount_positive'); $currentIncome = $query->incomeInPeriodCorrectedForList($start, $month, $accounts)->sum('amount_positive');
$currentExpense = $query->expenseInPeriodCorrected($start, $month, $shared)->sum('amount_positive'); $currentExpense = $query->expenseInPeriodCorrectedForList($start, $month, $accounts)->sum('amount_positive');
Log::debug('Date ['.$month->format('M Y').']: income = ['.$income.' + '.$currentIncome.'], out = ['.$expense.' + '.$currentExpense.']');
$income = bcadd($income, $currentIncome); $income = bcadd($income, $currentIncome);
$expense = bcadd($expense, $currentExpense); $expense = bcadd($expense, $currentExpense);
$count++; $count++;
$start->addMonth(); $start->addMonth();
} }

View File

@@ -5,8 +5,6 @@ use FireflyIII\Helpers\Report\ReportHelperInterface;
use FireflyIII\Models\Account; use FireflyIII\Models\Account;
use FireflyIII\Repositories\Account\AccountRepositoryInterface; use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Input;
use Redirect;
use Session; use Session;
use View; use View;
@@ -63,145 +61,46 @@ class ReportController extends Controller
} }
/** /**
* TODO needs a custom validator for ease of use. * @param $report_type
* @param Carbon $start
* @param Carbon $end
* @param Collection $accounts
* *
* @param AccountRepositoryInterface $repository * @return View
*
* @return \Illuminate\Http\RedirectResponse
*/ */
public function select(AccountRepositoryInterface $repository) public function year($report_type, Carbon $start, Carbon $end, Collection $accounts)
{ {
// process post data, give error, otherwise send redirect. $subTitle = trans('firefly.reportForYear', ['year' => $start->year]);
$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]);
}
/**
* @param string $year
* @param string $month
*
* @param bool $shared
*
* @return \Illuminate\View\View
*/
public function month($year = '2014', $month = '1', $shared = false)
{
$start = new Carbon($year . '-' . $month . '-01');
$subTitle = trans('firefly.reportForMonth', ['month' => $start->formatLocalized($this->monthFormat)]);
$subTitleIcon = 'fa-calendar';
$end = clone $start;
$incomeTopLength = 8;
$expenseTopLength = 8;
if ($shared == 'shared') {
$shared = true;
$subTitle = trans('firefly.reportForMonthShared', ['month' => $start->formatLocalized($this->monthFormat)]);
}
$end->endOfMonth();
$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);
$balance = $this->helper->getBalanceReport($start, $end, $shared);
$bills = $this->helper->getBillReport($start, $end);
Session::flash('gaEventCategory', 'report');
Session::flash('gaEventAction', 'month');
Session::flash('gaEventLabel', $start->format('F Y'));
return view(
'reports.month',
compact(
'start', 'shared',
'subTitle', 'subTitleIcon',
'accounts',
'incomes', 'incomeTopLength',
'expenses', 'expenseTopLength',
'budgets', 'balance',
'categories',
'bills'
)
);
}
/**
* @param $year
*
* @param bool $shared
*
* @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'; $subTitleIcon = 'fa-bar-chart';
$incomeTopLength = 8; $incomeTopLength = 8;
$expenseTopLength = 8; $expenseTopLength = 8;
if ($shared == 'shared') { $accountReport = $this->helper->getAccountReportForList($start, $end, $accounts);
$shared = true; $incomes = $this->helper->getIncomeReportForList($start, $end, $accounts);
$subTitle = trans('firefly.reportForYearShared', ['year' => $year]); $expenses = $this->helper->getExpenseReportForList($start, $end, $accounts);
}
$end->endOfYear();
$accounts = $this->helper->getAccountReport($start, $end, $shared);
$incomes = $this->helper->getIncomeReport($start, $end, $shared);
$expenses = $this->helper->getExpenseReport($start, $end, $shared);
Session::flash('gaEventCategory', 'report'); Session::flash('gaEventCategory', 'report');
Session::flash('gaEventAction', 'year'); Session::flash('gaEventAction', 'year');
Session::flash('gaEventLabel', $start->format('Y')); 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( return view(
'reports.year', 'reports.year',
compact('start', 'shared', 'accounts', 'incomes', 'expenses', 'subTitle', 'subTitleIcon', 'incomeTopLength', 'expenseTopLength') compact(
'start', 'accountReport', 'incomes', 'report_type', 'accountIds', 'end',
'expenses', 'subTitle', 'subTitleIcon', 'incomeTopLength', 'expenseTopLength'
)
); );
} }
/** /**
* @param $report_type * @param $report_type
* @param Carbon $start * @param Carbon $start
@@ -210,7 +109,7 @@ class ReportController extends Controller
* *
* @return View * @return View
*/ */
public function report($report_type, Carbon $start, Carbon $end, Collection $accounts) public function month($report_type, Carbon $start, Carbon $end, Collection $accounts)
{ {
// some fields for translation: // some fields for translation:
$subTitle = trans('firefly.reportForMonth', ['month' => $start->formatLocalized($this->monthFormat)]); $subTitle = trans('firefly.reportForMonth', ['month' => $start->formatLocalized($this->monthFormat)]);
@@ -250,6 +149,30 @@ class ReportController extends Controller
'accountIds', 'report_type' 'accountIds', 'report_type'
) )
); );
}
/**
* @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!');
}
// more than two months date difference means year report.
if ($start->diffInMonths($end) > 1) {
return $this->year($report_type, $start, $end, $accounts);
}
return $this->month($report_type, $start, $end, $accounts);
} }

View File

@@ -375,8 +375,8 @@ Route::group(
Route::get('/chart/piggyBank/{piggyBank}', ['uses' => 'Chart\PiggyBankController@history']); Route::get('/chart/piggyBank/{piggyBank}', ['uses' => 'Chart\PiggyBankController@history']);
// reports: // reports:
Route::get('/chart/report/in-out/{year}/{shared?}', ['uses' => 'Chart\ReportController@yearInOut'])->where(['year' => '[0-9]{4}', 'shared' => 'shared']); Route::get('/chart/report/in-out/{report_type}/{start_date}/{end_date}/{accountList}', ['uses' => 'Chart\ReportController@yearInOut'])->where(['year' => '[0-9]{4}', 'shared' => 'shared']);
Route::get('/chart/report/in-out-sum/{year}/{shared?}', ['uses' => 'Chart\ReportController@yearInOutSummarized'])->where( Route::get('/chart/report/in-out-sum/{report_type}/{start_date}/{end_date}/{accountList}', ['uses' => 'Chart\ReportController@yearInOutSummarized'])->where(
['year' => '[0-9]{4}', 'shared' => 'shared'] ['year' => '[0-9]{4}', 'shared' => 'shared']
); );

View File

@@ -104,10 +104,11 @@ function preSelectDate(e) {
function drawChart() { function drawChart() {
"use strict"; "use strict";
//if (typeof columnChart !== 'undefined' && typeof year !== 'undefined' && typeof month === 'undefined') { if (typeof columnChart !== 'undefined' && typeof year !== 'undefined' && typeof month === 'undefined') {
// columnChart('chart/report/in-out/' + year + shared, 'income-expenses-chart');
// columnChart('chart/report/in-out-sum/' + year + shared, 'income-expenses-sum-chart'); columnChart('chart/report/in-out/' + reportType + '/' + startDate + '/' + endDate + '/' + accountIds, 'income-expenses-chart');
//} columnChart('chart/report/in-out-sum/' + reportType + '/' + startDate + '/' + endDate + '/' + accountIds, 'income-expenses-sum-chart');
}
//if (typeof stackedColumnChart !== 'undefined' && typeof year !== 'undefined' && typeof month === 'undefined') { //if (typeof stackedColumnChart !== 'undefined' && typeof year !== 'undefined' && typeof month === 'undefined') {
// stackedColumnChart('chart/budget/year/' + year + shared, 'budgets'); // stackedColumnChart('chart/budget/year/' + year + shared, 'budgets');
// stackedColumnChart('chart/category/spent-in-year/' + year + shared, 'categories-spent-in-year'); // stackedColumnChart('chart/category/spent-in-year/' + year + shared, 'categories-spent-in-year');

View File

@@ -122,8 +122,14 @@
<script type="text/javascript"> <script type="text/javascript">
var year = '{{start.year}}'; var year = '{{start.year}}';
var shared = {% if shared %}'/shared'
{% else %}''{% endif %}; // to report another URL:
var startDate = '{{ start.format('Ymd') }}';
var endDate = '{{ end.format('Ymd') }}';
var reportType = '{{ report_type }}';
var accountIds = '{{ accountIds }}';
var incomeTopLength = {{ incomeTopLength }}; var incomeTopLength = {{ incomeTopLength }};
var expenseTopLength = {{ expenseTopLength }}; var expenseTopLength = {{ expenseTopLength }};
var incomeRestShow = false; // starts hidden. var incomeRestShow = false; // starts hidden.