From fe3f015171f356d977ea474a737793b447d9eda5 Mon Sep 17 00:00:00 2001 From: James Cole Date: Wed, 26 Oct 2016 16:46:43 +0200 Subject: [PATCH] Add more stuff to ajax controllers, making report controller simpler. --- .../Controllers/Report/AccountController.php | 16 ++++- .../Controllers/Report/CategoryController.php | 59 +++++++++++++++++++ .../Controllers/Report/InOutController.php | 41 +++++++++---- app/Http/Controllers/ReportController.php | 2 - public/js/ff/reports/default/all.js | 13 +++- public/js/ff/reports/default/month.js | 23 +++++++- resources/views/reports/default/month.twig | 6 +- .../views/reports/partials/categories.twig | 14 ++++- routes/web.php | 6 ++ 9 files changed, 158 insertions(+), 22 deletions(-) create mode 100644 app/Http/Controllers/Report/CategoryController.php diff --git a/app/Http/Controllers/Report/AccountController.php b/app/Http/Controllers/Report/AccountController.php index 3ea9eaab43..582ff0bf83 100644 --- a/app/Http/Controllers/Report/AccountController.php +++ b/app/Http/Controllers/Report/AccountController.php @@ -17,6 +17,7 @@ namespace FireflyIII\Http\Controllers\Report; use Carbon\Carbon; use FireflyIII\Http\Controllers\Controller; use FireflyIII\Repositories\Account\AccountTaskerInterface; +use FireflyIII\Support\CacheProperties; use Illuminate\Support\Collection; /** @@ -36,9 +37,22 @@ class AccountController extends Controller */ public function accountReport(Carbon $start, Carbon $end, Collection $accounts) { + // chart properties for cache: + $cache = new CacheProperties; + $cache->addProperty($start); + $cache->addProperty($end); + $cache->addProperty('account-report'); + $cache->addProperty($accounts->pluck('id')->toArray()); + if ($cache->has()) { + return $cache->get(); + } + + $accountTasker = app(AccountTaskerInterface::class); $accountReport = $accountTasker->getAccountReport($start, $end, $accounts); - return view('reports.partials.accounts', compact('accountReport')); + $result = view('reports.partials.accounts', compact('accountReport'))->render(); + $cache->store($result); + return $result; } } diff --git a/app/Http/Controllers/Report/CategoryController.php b/app/Http/Controllers/Report/CategoryController.php new file mode 100644 index 0000000000..48ee5d77b7 --- /dev/null +++ b/app/Http/Controllers/Report/CategoryController.php @@ -0,0 +1,59 @@ +addProperty($start); + $cache->addProperty($end); + $cache->addProperty('category-report'); + $cache->addProperty($accounts->pluck('id')->toArray()); + if ($cache->has()) { + return $cache->get(); + } + + $categories = $helper->getCategoryReport($start, $end, $accounts); + + $result = view('reports.partials.categories', compact('categories'))->render(); + $cache->store($result); + + return $result; + } + +} \ No newline at end of file diff --git a/app/Http/Controllers/Report/InOutController.php b/app/Http/Controllers/Report/InOutController.php index 7272597820..abc23c55fb 100644 --- a/app/Http/Controllers/Report/InOutController.php +++ b/app/Http/Controllers/Report/InOutController.php @@ -17,6 +17,7 @@ namespace FireflyIII\Http\Controllers\Report; use Carbon\Carbon; use FireflyIII\Helpers\Report\ReportHelperInterface; use FireflyIII\Http\Controllers\Controller; +use FireflyIII\Support\CacheProperties; use Illuminate\Support\Collection; use Response; @@ -28,22 +29,38 @@ use Response; class InOutController extends Controller { - + /** + * @param ReportHelperInterface $helper + * @param Carbon $start + * @param Carbon $end + * @param Collection $accounts + * + * @return \Illuminate\Http\JsonResponse + */ public function inOutReport(ReportHelperInterface $helper, Carbon $start, Carbon $end, Collection $accounts) { + // chart properties for cache: + $cache = new CacheProperties; + $cache->addProperty($start); + $cache->addProperty($end); + $cache->addProperty('in-out-report'); + $cache->addProperty($accounts->pluck('id')->toArray()); + if ($cache->has()) { + return Response::json($cache->get()); + } - $incomes = $helper->getIncomeReport($start, $end, $accounts); - $expenses = $helper->getExpenseReport($start, $end, $accounts); - $incomeTopLength = 8; - $expenseTopLength = 8; + $incomes = $helper->getIncomeReport($start, $end, $accounts); + $expenses = $helper->getExpenseReport($start, $end, $accounts); + + $result = [ + 'income' => view('reports.partials.income', compact('incomes'))->render(), + 'expenses' => view('reports.partials.expenses', compact('expenses'))->render(), + 'incomes_expenses' => view('reports.partials.income-vs-expenses', compact('expenses', 'incomes'))->render(), + ]; + $cache->store($result); + + return Response::json($result); - return Response::json( - [ - 'income' => view('reports.partials.income', compact('incomes', 'incomeTopLength'))->render(), - 'expenses' => view('reports.partials.expenses', compact('expenses', 'expenseTopLength'))->render(), - 'incomes_expenses' => view('reports.partials.income-vs-expenses', compact('expenses', 'incomes'))->render(), - ] - ); } } \ No newline at end of file diff --git a/app/Http/Controllers/ReportController.php b/app/Http/Controllers/ReportController.php index 7a208eafbb..352713e7fa 100644 --- a/app/Http/Controllers/ReportController.php +++ b/app/Http/Controllers/ReportController.php @@ -227,7 +227,6 @@ class ReportController extends Controller { // get report stuff! $budgets = $this->budgetHelper->getBudgetReport($start, $end, $accounts); - $categories = $this->helper->getCategoryReport($start, $end, $accounts); $balance = $this->balanceHelper->getBalanceReport($start, $end, $accounts); $bills = $this->helper->getBillReport($start, $end, $accounts); $tags = $this->helper->tagReport($start, $end, $accounts); @@ -243,7 +242,6 @@ class ReportController extends Controller 'tags', 'incomes', 'budgets', 'balance', - 'categories', 'bills', 'accountIds', 'reportType' ) diff --git a/public/js/ff/reports/default/all.js b/public/js/ff/reports/default/all.js index d3a916c725..a76d6be0ec 100644 --- a/public/js/ff/reports/default/all.js +++ b/public/js/ff/reports/default/all.js @@ -10,8 +10,7 @@ $(function () { "use strict"; - // find the little info buttons and respond to them. - $('.firefly-info-button').click(clickInfoButton); + // load the account report, which this report shows: loadAccountReport(); @@ -19,11 +18,20 @@ $(function () { // load income / expense / difference: loadInOutReport(); + // trigger info click + triggerInfoClick(); + // trigger list length things: listLengthInitial(); }); +function triggerInfoClick() { + "use strict"; + // find the little info buttons and respond to them. + $('.firefly-info-button').unbind('clicl').click(clickInfoButton); +} + function listLengthInitial() { "use strict"; $('.overListLength').hide(); @@ -62,6 +70,7 @@ function placeInOutReport(data) { $('#expenseReport').removeClass('loading').html(data.expenses); $('#incomeVsExpenseReport').removeClass('loading').html(data.incomes_expenses); listLengthInitial(); + triggerInfoClick(); } function failInOutReport() { diff --git a/public/js/ff/reports/default/month.js b/public/js/ff/reports/default/month.js index e970b554eb..8f5be8a671 100644 --- a/public/js/ff/reports/default/month.js +++ b/public/js/ff/reports/default/month.js @@ -1,11 +1,32 @@ -/* globals google, startDate ,reportURL, endDate , reportType ,accountIds , picker:true, minDate, year, month, columnChart, lineChart, stackedColumnChart */ +/* globals google, startDate ,reportURL, endDate , reportType ,accountIds, lineChart, categoryReportUrl */ $(function () { "use strict"; drawChart(); + + loadCategoryReport(); }); +function loadCategoryReport() { + "use strict"; + console.log('Going to grab ' + categoryReportUrl); + $.get(categoryReportUrl).done(placeCategoryReport).fail(failCategoryReport); +} + +function placeCategoryReport(data) { + "use strict"; + $('#categoryReport').removeClass('loading').html(data); + listLengthInitial(); + triggerInfoClick(); +} + +function failCategoryReport() { + "use strict"; + console.log('Fail category report data!'); + $('#categoryReport').removeClass('loading').addClass('general-chart-error'); +} + function drawChart() { "use strict"; diff --git a/resources/views/reports/default/month.twig b/resources/views/reports/default/month.twig index 05f70e7ab5..2f67fd62f7 100644 --- a/resources/views/reports/default/month.twig +++ b/resources/views/reports/default/month.twig @@ -39,9 +39,8 @@ {% include 'reports/partials/budgets.twig' %} -
- - {% include 'reports/partials/categories.twig' %} +
+ {# {% include 'reports/partials/categories.twig' %} #}
@@ -82,6 +81,7 @@ var accountReportUrl = '{{ route('reports.data.accountReport', [start.format('Ymd'), end.format('Ymd'), accountIds]) }}'; var inOutReportUrl = '{{ route('reports.data.inOutReport', [start.format('Ymd'), end.format('Ymd'), accountIds]) }}'; + var categoryReportUrl = '{{ route('reports.data.categoryReport', [start.format('Ymd'), end.format('Ymd'), accountIds]) }}'; diff --git a/resources/views/reports/partials/categories.twig b/resources/views/reports/partials/categories.twig index 0a2426168d..ddbe1648f2 100644 --- a/resources/views/reports/partials/categories.twig +++ b/resources/views/reports/partials/categories.twig @@ -12,7 +12,11 @@ {% for cat in categories.getCategories %} - + {% if loop.index > listLength %} + + {% else %} + + {% endif %} {{ cat.name }} @@ -26,6 +30,14 @@ {% endfor %} + {% if categories.getCategories.count > expenseTopLength %} + + + {{ trans('firefly.show_full_list',{number:incomeTopLength}) }} + + + {% endif %} + {{ 'sum'|_ }} {{ categories.getTotal|formatAmount }} diff --git a/routes/web.php b/routes/web.php index c9243d04f2..dd4bd057f1 100755 --- a/routes/web.php +++ b/routes/web.php @@ -329,6 +329,12 @@ Route::group( ['uses' => 'Report\InOutController@inOutReport', 'as' => 'reports.data.inOutReport'] ); + // category report: + Route::get( + '/reports/data/category-report/{start_date}/{end_date}/{accountList}', + ['uses' => 'Report\CategoryController@categoryReport', 'as' => 'reports.data.categoryReport'] + ); + /** * Rules Controller */