Skip to content

Firefly III: a personal finances manager

Jump to year report if the period is too long.

James Colea2ccbf7844d0
diff --git a/app/Http/Controllers/Chart/ReportController.php b/app/Http/Controllers/Chart/ReportController.php
index 6522f8b2a7..659be0fa76 100644
--- a/app/Http/Controllers/Chart/ReportController.php
+++ b/app/Http/Controllers/Chart/ReportController.php
@@ -9,7 +9,6 @@ use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Support\CacheProperties;
use Illuminate\Support\Collection;
use Response;
-use Log;
/**
* Class ReportController
@@ -37,23 +36,20 @@ class ReportController extends Controller
* Summarizes all income and expenses, per month, for a given year.
*
* @param ReportQueryInterface $query
- * @param $year
- * @param bool $shared
+ * @param $report_type
+ * @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:
$cache = new CacheProperties;
$cache->addProperty('yearInOut');
- $cache->addProperty($year);
- $cache->addProperty($shared);
+ $cache->addProperty($start);
+ $cache->addProperty($end);
if ($cache->has()) {
return Response::json($cache->get()); // @codeCoverageIgnore
}
@@ -63,8 +59,8 @@ class ReportController extends Controller
$month = clone $start;
$month->endOfMonth();
// total income and total expenses:
- $incomeSum = $query->incomeInPeriodCorrected($start, $month, $shared)->sum('amount_positive');
- $expenseSum = $query->expenseInPeriodCorrected($start, $month, $shared)->sum('amount_positive');
+ $incomeSum = $query->incomeInPeriodCorrectedForList($start, $month, $accounts)->sum('amount_positive');
+ $expenseSum = $query->expenseInPeriodCorrectedForList($start, $month, $accounts)->sum('amount_positive');
$entries->push([clone $start, $incomeSum, $expenseSum]);
$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.
*
* @param ReportQueryInterface $query
- * @param $year
- * @param bool $shared
+ * @param $report_type
+ * @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:
$cache = new CacheProperties;
$cache->addProperty('yearInOutSummarized');
- $cache->addProperty($year);
- $cache->addProperty($shared);
+ $cache->addProperty($start);
+ $cache->addProperty($end);
+ $cache->addProperty($accounts);
if ($cache->has()) {
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';
$expense = '0';
$count = 0;
@@ -111,18 +107,11 @@ class ReportController extends Controller
$month = clone $start;
$month->endOfMonth();
// total income and total expenses:
- $currentIncome = $query->incomeInPeriodCorrected($start, $month, $shared)->sum('amount_positive');
- $currentExpense = $query->expenseInPeriodCorrected($start, $month, $shared)->sum('amount_positive');
-
- Log::debug('Date ['.$month->format('M Y').']: income = ['.$income.' + '.$currentIncome.'], out = ['.$expense.' + '.$currentExpense.']');
-
- $income = bcadd($income, $currentIncome);
- $expense = bcadd($expense, $currentExpense);
-
-
-
-
-
+ $currentIncome = $query->incomeInPeriodCorrectedForList($start, $month, $accounts)->sum('amount_positive');
+ $currentExpense = $query->expenseInPeriodCorrectedForList($start, $month, $accounts)->sum('amount_positive');
+ $income = bcadd($income, $currentIncome);
+ $expense = bcadd($expense, $currentExpense);
+
$count++;
$start->addMonth();
}
diff --git a/app/Http/Controllers/ReportController.php b/app/Http/Controllers/ReportController.php
index a1b577e982..19e5bcbe36 100644
--- a/app/Http/Controllers/ReportController.php
+++ b/app/Http/Controllers/ReportController.php
@@ -5,8 +5,6 @@ use FireflyIII\Helpers\Report\ReportHelperInterface;
use FireflyIII\Models\Account;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use Illuminate\Support\Collection;
-use Input;
-use Redirect;
use Session;
use View;
@@ -63,145 +61,46 @@ class ReportController extends Controller
}
/**
- * 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]);
-
- }
-
- /**
- * @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
+ * @param $report_type
+ * @param Carbon $start
+ * @param Carbon $end
+ * @param Collection $accounts
*
- * @return $this
+ * @return View
*/
- public function year($year, $shared = false)
+ public function year($report_type, Carbon $start, Carbon $end, Collection $accounts)
{
- $start = new Carbon('01-01-' . $year);
- $end = clone $start;
- $subTitle = trans('firefly.reportForYear', ['year' => $year]);
+ $subTitle = trans('firefly.reportForYear', ['year' => $start->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);
+ $accountReport = $this->helper->getAccountReportForList($start, $end, $accounts);
+ $incomes = $this->helper->getIncomeReportForList($start, $end, $accounts);
+ $expenses = $this->helper->getExpenseReportForList($start, $end, $accounts);
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;
+ }
+ $accountIds = join(';', $accountIds);
+
return view(
'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 Carbon $start
@@ -210,7 +109,7 @@ class ReportController extends Controller
*
* @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:
$subTitle = trans('firefly.reportForMonth', ['month' => $start->formatLocalized($this->monthFormat)]);
@@ -250,6 +149,30 @@ class ReportController extends Controller
'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);
+
}
diff --git a/app/Http/routes.php b/app/Http/routes.php
index 82ae0a8b01..d280c50bbb 100644
--- a/app/Http/routes.php
+++ b/app/Http/routes.php
@@ -375,8 +375,8 @@ Route::group(
Route::get('/chart/piggyBank/{piggyBank}', ['uses' => 'Chart\PiggyBankController@history']);
// 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-sum/{year}/{shared?}', ['uses' => 'Chart\ReportController@yearInOutSummarized'])->where(
+ 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/{report_type}/{start_date}/{end_date}/{accountList}', ['uses' => 'Chart\ReportController@yearInOutSummarized'])->where(
['year' => '[0-9]{4}', 'shared' => 'shared']
);
diff --git a/public/js/reports.js b/public/js/reports.js
index 4552f56dcd..578d79f92c 100644
--- a/public/js/reports.js
+++ b/public/js/reports.js
@@ -104,10 +104,11 @@ function preSelectDate(e) {
function drawChart() {
"use strict";
- //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');
- //}
+ if (typeof columnChart !== 'undefined' && typeof year !== 'undefined' && typeof month === 'undefined') {
+
+ 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') {
// stackedColumnChart('chart/budget/year/' + year + shared, 'budgets');
// stackedColumnChart('chart/category/spent-in-year/' + year + shared, 'categories-spent-in-year');
diff --git a/resources/twig/reports/year.twig b/resources/twig/reports/year.twig
index 0d543bc54a..02e236b75b 100644
--- a/resources/twig/reports/year.twig
+++ b/resources/twig/reports/year.twig
@@ -122,8 +122,14 @@
<script type="text/javascript">
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 expenseTopLength = {{ expenseTopLength }};
var incomeRestShow = false; // starts hidden.