From 2e7c26c5393ba102fa44ff05e109ef2e8df5171e Mon Sep 17 00:00:00 2001 From: James Cole Date: Thu, 18 Feb 2016 10:04:53 +0100 Subject: [PATCH] New chart called "net worth". --- .../Report/ChartJsReportChartGenerator.php | 26 ++++++++ .../Report/ReportChartGeneratorInterface.php | 7 ++ .../Controllers/Chart/ReportController.php | 64 +++++++++++++++++++ app/Http/routes.php | 1 + app/Support/Steam.php | 1 + public/js/charts.js | 2 +- public/js/reports/default/multi-year.js | 1 + public/js/reports/default/year.js | 1 + .../views/reports/default/multi-year.twig | 13 ++++ resources/views/reports/default/year.twig | 14 ++++ 10 files changed, 129 insertions(+), 1 deletion(-) diff --git a/app/Generator/Chart/Report/ChartJsReportChartGenerator.php b/app/Generator/Chart/Report/ChartJsReportChartGenerator.php index 4a4f063d72..334621abca 100644 --- a/app/Generator/Chart/Report/ChartJsReportChartGenerator.php +++ b/app/Generator/Chart/Report/ChartJsReportChartGenerator.php @@ -76,6 +76,32 @@ class ChartJsReportChartGenerator implements ReportChartGeneratorInterface return $data; } + /** + * @param Collection $entries + * + * @return array + */ + public function netWorth(Collection $entries) : array + { + $format = (string)trans('config.month_and_day'); + $data = [ + 'count' => 1, + 'labels' => [], + 'datasets' => [ + [ + 'label' => trans('firefly.net-worth'), + 'data' => [], + ], + ], + ]; + foreach ($entries as $entry) { + $data['labels'][] = trim($entry['date']->formatLocalized($format)); + $data['datasets'][0]['data'][] = floatval($entry['net-worth']); + } + + return $data; + } + /** * @param Collection $entries * diff --git a/app/Generator/Chart/Report/ReportChartGeneratorInterface.php b/app/Generator/Chart/Report/ReportChartGeneratorInterface.php index 333bff112b..c1999137dc 100644 --- a/app/Generator/Chart/Report/ReportChartGeneratorInterface.php +++ b/app/Generator/Chart/Report/ReportChartGeneratorInterface.php @@ -36,6 +36,13 @@ interface ReportChartGeneratorInterface */ public function multiYearInOutSummarized(string $income, string $expense, int $count): array; + /** + * @param Collection $entries + * + * @return array + */ + public function netWorth(Collection $entries) : array; + /** * @param Collection $entries * diff --git a/app/Http/Controllers/Chart/ReportController.php b/app/Http/Controllers/Chart/ReportController.php index c80467674a..288bda6cb9 100644 --- a/app/Http/Controllers/Chart/ReportController.php +++ b/app/Http/Controllers/Chart/ReportController.php @@ -10,6 +10,7 @@ use FireflyIII\Http\Controllers\Controller; use FireflyIII\Support\CacheProperties; use Illuminate\Support\Collection; use Response; +use Steam; /** * Class ReportController @@ -32,6 +33,53 @@ class ReportController extends Controller $this->generator = app('FireflyIII\Generator\Chart\Report\ReportChartGeneratorInterface'); } + /** + * This chart, by default, is shown on the multi-year and year report pages, + * which means that giving it a 2 week "period" should be enough granularity. + * + * @param ReportQueryInterface $query + * @param string $reportType + * @param Carbon $start + * @param Carbon $end + * @param Collection $accounts + * + * @return \Illuminate\Http\JsonResponse + */ + public function netWorth(ReportQueryInterface $query, string $reportType, Carbon $start, Carbon $end, Collection $accounts) + { + bcscale(2); + // chart properties for cache: + $cache = new CacheProperties; + $cache->addProperty('netWorth'); + $cache->addProperty($start); + $cache->addProperty($reportType); + $cache->addProperty($accounts); + $cache->addProperty($end); + if ($cache->has()) { + return Response::json($cache->get()); // @codeCoverageIgnore + } + $ids = $accounts->pluck('id')->toArray(); + $current = clone $start; + $entries = new Collection; + while ($current < $end) { + $balances = Steam::balancesById($ids, $current); + $sum = $this->array_sum($balances); + $entries->push( + [ + 'date' => clone $current, + 'net-worth' => $sum, + ] + ); + + $current->addDays(7); + } + $data = $this->generator->netWorth($entries); + + //$cache->store($data); + + return Response::json($data); + } + /** * Summarizes all income and expenses, per month, for a given year. @@ -250,4 +298,20 @@ class ReportController extends Controller return $data; } + + /** + * @param $array + * + * @return string + */ + private function array_sum($array) : string + { + bcscale(2); + $sum = '0'; + foreach ($array as $entry) { + $sum = bcadd($sum, $entry); + } + + return $sum; + } } diff --git a/app/Http/routes.php b/app/Http/routes.php index c930d03514..7e66a5a452 100644 --- a/app/Http/routes.php +++ b/app/Http/routes.php @@ -190,6 +190,7 @@ Route::group( // reports: Route::get('/chart/report/in-out/{reportType}/{start_date}/{end_date}/{accountList}', ['uses' => 'Chart\ReportController@yearInOut']); Route::get('/chart/report/in-out-sum/{reportType}/{start_date}/{end_date}/{accountList}', ['uses' => 'Chart\ReportController@yearInOutSummarized']); + Route::get('/chart/report/net-worth/{reportType}/{start_date}/{end_date}/{accountList}', ['uses' => 'Chart\ReportController@netWorth']); /** diff --git a/app/Support/Steam.php b/app/Support/Steam.php index 1d523b1356..8debb4ac6b 100644 --- a/app/Support/Steam.php +++ b/app/Support/Steam.php @@ -103,6 +103,7 @@ class Steam } /** + * This method always ignores the virtual balance. * * @param array $ids * @param \Carbon\Carbon $date diff --git a/public/js/charts.js b/public/js/charts.js index 5b3ce5fcc3..7ebd3a8685 100644 --- a/public/js/charts.js +++ b/public/js/charts.js @@ -92,7 +92,7 @@ var defaultLineOptions = { datasetFill: false, scaleFontSize: 10, responsive: false, - scaleLabel: "<%= '" + currencySymbol + " ' + Number(value).toFixed(0).replace('.', ',') %>", + scaleLabel: " <%= accounting.formatMoney(value) %>", tooltipFillColor: "rgba(0,0,0,0.5)", tooltipTemplate: "<%if (label){%><%=label%>: <%}%>" + currencySymbol + " <%= value %>", multiTooltipTemplate: "<%=datasetLabel%>: <%= '" + currencySymbol + " ' + Number(value).toFixed(2).replace('.', ',') %>" diff --git a/public/js/reports/default/multi-year.js b/public/js/reports/default/multi-year.js index 61931c1d78..984d0e4a72 100644 --- a/public/js/reports/default/multi-year.js +++ b/public/js/reports/default/multi-year.js @@ -17,6 +17,7 @@ function drawChart() { "use strict"; // income and expense over multi year: + lineChart('chart/report/net-worth/' + reportType + '/' + startDate + '/' + endDate + '/' + accountIds, 'net-worth'); 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'); diff --git a/public/js/reports/default/year.js b/public/js/reports/default/year.js index 12fbfe7e13..8a39be3ef3 100644 --- a/public/js/reports/default/year.js +++ b/public/js/reports/default/year.js @@ -15,6 +15,7 @@ $(function () { function drawChart() { "use strict"; + lineChart('chart/report/net-worth/' + reportType + '/' + startDate + '/' + endDate + '/' + accountIds, 'net-worth'); 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'); stackedColumnChart('chart/budget/year/' + reportType + '/' + startDate + '/' + endDate + '/' + accountIds, 'budgets'); diff --git a/resources/views/reports/default/multi-year.twig b/resources/views/reports/default/multi-year.twig index bb18c3abb4..c44eabd91c 100644 --- a/resources/views/reports/default/multi-year.twig +++ b/resources/views/reports/default/multi-year.twig @@ -60,6 +60,19 @@ {% endfor %} +
+
+
+
+

{{ 'net_worth'|_ }}

+
+
+ +
+
+
+
+
diff --git a/resources/views/reports/default/year.twig b/resources/views/reports/default/year.twig index b54a71131d..c7551a3c56 100644 --- a/resources/views/reports/default/year.twig +++ b/resources/views/reports/default/year.twig @@ -42,6 +42,20 @@ {% include 'reports/partials/expenses.twig' %}
+ +
+
+
+
+

{{ 'net_worth'|_ }}

+
+
+ +
+
+
+
+