diff --git a/app/Generator/Chart/Category/CategoryChartGeneratorInterface.php b/app/Generator/Chart/Category/CategoryChartGeneratorInterface.php index ee8ff8eb96..c311d70298 100644 --- a/app/Generator/Chart/Category/CategoryChartGeneratorInterface.php +++ b/app/Generator/Chart/Category/CategoryChartGeneratorInterface.php @@ -22,7 +22,6 @@ use Illuminate\Support\Collection; */ interface CategoryChartGeneratorInterface { - /** * @param Collection $entries * @@ -58,4 +57,11 @@ interface CategoryChartGeneratorInterface */ public function pieChart(array $data): array; + /** + * @param array $entries + * + * @return array + */ + public function reportPeriod(array $entries): array; + } diff --git a/app/Generator/Chart/Category/ChartJsCategoryChartGenerator.php b/app/Generator/Chart/Category/ChartJsCategoryChartGenerator.php index 1787125c23..b667811753 100644 --- a/app/Generator/Chart/Category/ChartJsCategoryChartGenerator.php +++ b/app/Generator/Chart/Category/ChartJsCategoryChartGenerator.php @@ -142,6 +142,41 @@ class ChartJsCategoryChartGenerator implements CategoryChartGeneratorInterface } + /** + * @param array $entries + * + * @return array + */ + public function reportPeriod(array $entries): array + { + + $data = [ + 'labels' => array_keys($entries), + 'datasets' => [ + 0 => [ + 'label' => trans('firefly.earned'), + 'data' => [], + ], + 1 => [ + 'label' => trans('firefly.spent'), + 'data' => [], + ], + ], + 'count' => 2, + ]; + + foreach ($entries as $label => $entry) { + // data set 0 is budgeted + // data set 1 is spent: + $data['datasets'][0]['data'][] = round($entry['earned'], 2); + $data['datasets'][1]['data'][] = round(bcmul($entry['spent'], '-1'), 2); + + } + + return $data; + + } + /** * @param array $entries * diff --git a/app/Http/Controllers/Chart/CategoryController.php b/app/Http/Controllers/Chart/CategoryController.php index 02645460dc..be6110534b 100644 --- a/app/Http/Controllers/Chart/CategoryController.php +++ b/app/Http/Controllers/Chart/CategoryController.php @@ -152,6 +152,43 @@ class CategoryController extends Controller } + /** + * @param CRI $repository + * @param Category $category + * @param Carbon $start + * @param Carbon $end + * @param Collection $accounts + */ + public function reportPeriod(CRI $repository, Category $category, Carbon $start, Carbon $end, Collection $accounts) + { + $cache = new CacheProperties; + $cache->addProperty($start); + $cache->addProperty($end); + $cache->addProperty('category-period-chart'); + $cache->addProperty($accounts->pluck('id')->toArray()); + $cache->addProperty($category); + if ($cache->has()) { + + return $cache->get(); + } + $report = $repository->getCategoryPeriodReport(new Collection([$category]), $accounts, $start, $end, true); + $periods = Navigation::listOfPeriods($start, $end); + + + // join them: + $result = []; + foreach (array_keys($periods) as $period) { + $nice = $periods[$period]; + $result[$nice] = [ + 'earned' => $report['income'][$category->id]['entries'][$period] ?? '0', + 'spent' => $report['expense'][$category->id]['entries'][$period] ?? '0', + ]; + } + $data = $this->generator->reportPeriod($result); + + return Response::json($data); + } + /** * @param CRI $repository * @param Category $category diff --git a/public/js/ff/reports/default/all.js b/public/js/ff/reports/default/all.js index ec3fb8c1e6..0d3f056e2b 100644 --- a/public/js/ff/reports/default/all.js +++ b/public/js/ff/reports/default/all.js @@ -89,8 +89,11 @@ function displayAjaxPartial(data, holder) { // trigger list thing listLengthInitial(); - // budget thing + // budget thing in year and multi year report: $('.budget-chart-activate').unbind('click').on('click', clickBudgetChart); + + // category thing in year and multi year report: + $('.category-chart-activate').unbind('click').on('click', clickCategoryChart); } function failAjaxPartial(uri, holder) { @@ -100,6 +103,18 @@ function failAjaxPartial(uri, holder) { } +function clickCategoryChart(e) { + "use strict"; + var link = $(e.target); + var categoryId = link.data('category'); + + // this url is different from the one below. this is something that must be fixed + var URL = 'chart/category/' + categoryId + '/report-period/' + startDate + '/' + endDate + '/' + accountIds; + var container = 'category_chart'; + columnChart(URL, container); + return false; +} + function clickBudgetChart(e) { "use strict"; var link = $(e.target); diff --git a/public/js/ff/reports/default/multi-year.js b/public/js/ff/reports/default/multi-year.js index 1bc23389ae..3988ee298b 100644 --- a/public/js/ff/reports/default/multi-year.js +++ b/public/js/ff/reports/default/multi-year.js @@ -6,6 +6,7 @@ $(function () { drawChart(); loadAjaxPartial('budgetPeriodReport', budgetPeriodReportUri); + loadAjaxPartial('categoryPeriodReport', categoryPeriodReportUri); }); function drawChart() { diff --git a/resources/views/reports/default/multi-year.twig b/resources/views/reports/default/multi-year.twig index 95f2932824..c92c8f3e91 100644 --- a/resources/views/reports/default/multi-year.twig +++ b/resources/views/reports/default/multi-year.twig @@ -114,6 +114,34 @@ + {# same thing but for categories #} +
+
+
+
+

{{ 'categories'|_ }}

+
+
+
+
+
+
+ + {# and the same chart too! #} +
+
+
+
+

{{ 'chart'|_ }}

+
+
+ + +
+
+
+
+ {% endblock %} {% block styles %} @@ -141,6 +169,7 @@ var incExpReportUri = '{{ route('reports.data.incExpReport', [start.format('Ymd'), end.format('Ymd'), accountIds]) }}'; var budgetPeriodReportUri = '{{ route('reports.data.budgetPeriodReport', [start.format('Ymd'), end.format('Ymd'), accountIds]) }}'; + var categoryPeriodReportUri = '{{ route('reports.data.categoryPeriodReport', [start.format('Ymd'), end.format('Ymd'), accountIds]) }}'; diff --git a/resources/views/reports/default/year.twig b/resources/views/reports/default/year.twig index 1824ad5f05..01dcfc304c 100644 --- a/resources/views/reports/default/year.twig +++ b/resources/views/reports/default/year.twig @@ -124,6 +124,22 @@ + + + {# and the same chart too! #} +
+
+
+
+

{{ 'chart'|_ }}

+
+
+ + +
+
+
+
{% endblock %} diff --git a/routes/web.php b/routes/web.php index 67c4135254..2d1d1d4ac4 100755 --- a/routes/web.php +++ b/routes/web.php @@ -221,6 +221,7 @@ Route::group( Route::get('/chart/category/{category}/period', ['uses' => 'Chart\CategoryController@currentPeriod']); Route::get('/chart/category/{category}/period/{date}', ['uses' => 'Chart\CategoryController@specificPeriod']); Route::get('/chart/category/{category}/all', ['uses' => 'Chart\CategoryController@all']); + Route::get('/chart/category/{category}/report-period/{start_date}/{end_date}/{accountList}', ['uses' => 'Chart\CategoryController@reportPeriod']); // these charts are used in reports (category reports): Route::get(