Firefly III: a personal finances manager
PHP accountingbudgetbudgetingbudgetscash-flowcashflowcredit-carddockerexpensesfinancefinancesfinanciallinuxmoneypaycheckpersonal-financephpphp7
Second attempt.
James Cole56715556ed2f
diff --git a/app/Generator/Chart/Budget/ChartJsBudgetChartGenerator.php b/app/Generator/Chart/Budget/ChartJsBudgetChartGenerator.phpindex 420501ac1a..d54c849f2c 100644--- a/app/Generator/Chart/Budget/ChartJsBudgetChartGenerator.php+++ b/app/Generator/Chart/Budget/ChartJsBudgetChartGenerator.php@@ -149,35 +149,29 @@ class ChartJsBudgetChartGenerator implements BudgetChartGenerator*/public function multiYear(Collection $entries){- //var_dump($entries);+ // dataset:$data = ['count' => 0,'labels' => [],'datasets' => [],];- // labels: for each budget.- // dataset: for each year?- foreach($entries as $entry) {- $year = $entry['date']->year;- if(!in_array($year, $data['labels'])) {- $data['labels'][] = $entry['date']->year;- }- }- // can be joined?- $set = [];- foreach($entries as $entry) {- $name = $entry['budget'];- $set[$name] = isset($set[$name]) ? $set[$name] : [];- $set[$name][] = ($entry['sum'] * -1);+ // get labels from one of the budgets (assuming there's at least one):+ $first = $entries->first();+ foreach ($first['budgeted'] as $year => $noInterest) {+ $data['labels'][] = strval($year);}- foreach($set as $name => $values) {- $data['datasets'][] = ['label' => $name, 'data' => $values];++ // then, loop all entries and create datasets:+ foreach ($entries as $entry) {+ $name = $entry['name'];+ $spent = $entry['spent'];+ $budgeted = $entry['budgeted'];+ $data['datasets'][] = ['label' => 'Spent on ' . $name, 'data' => $spent];+ $data['datasets'][] = ['label' => 'Budgeted for ' . $name, 'data' => $budgeted];}$data['count'] = count($data['datasets']);-return $data;- //var_dump($data);- //exit;+ return $data;}}diff --git a/app/Http/Controllers/Chart/BudgetController.php b/app/Http/Controllers/Chart/BudgetController.phpindex 49b0f6ab2c..c2279c923a 100644--- a/app/Http/Controllers/Chart/BudgetController.php+++ b/app/Http/Controllers/Chart/BudgetController.php@@ -56,41 +56,54 @@ class BudgetController extends Controller$cache->addProperty('multiYearBudget');if ($cache->has()) {- return Response::json($cache->get()); // @codeCoverageIgnore+ // return Response::json($cache->get()); // @codeCoverageIgnore}- $currentStart = clone $start;- $collection = new Collection;-- while ($currentStart < $end) {- $currentEnd = clone $currentStart;- $currentEnd->endOfYear();+ /**+ * budget+ * year:+ * spent: x+ * budgeted: x+ * year+ * spent: x+ * budgeted: x+ */+ $entries = new Collection;+ // go by budget, not by year.+ foreach ($budgets as $budget) {+ $entry = ['name' => '', 'spent' => [], 'budgeted' => []];- //echo 'now for ' . $currentStart->format('Ymd') . ' to ' . $currentEnd->format('Ymd') . '<br>';+ $currentStart = clone $start;+ while ($currentStart < $end) {+ // fix the date:+ $currentEnd = clone $currentStart;+ $currentEnd->endOfYear();- /** @var Budget $budget */- foreach ($budgets as $budget) {+ // get data:if (is_null($budget->id)) {- $name = trans('firefly.noBudget');- $sum = $repository->getWithoutBudgetSum($currentStart, $currentEnd);+ $name = trans('firefly.noBudget');+ $sum = $repository->getWithoutBudgetSum($currentStart, $currentEnd);+ $budgeted = 0;} else {- $name = $budget->name;- $sum = $repository->balanceInPeriodForList($budget, $currentStart, $currentEnd, $accounts);+ $name = $budget->name;+ $sum = $repository->balanceInPeriodForList($budget, $currentStart, $currentEnd, $accounts);+ $budgeted = $repository->getBudgetLimitRepetitions($budget, $currentStart, $currentEnd)->sum('amount');}- $collection->push(['budget' => $name, 'sum' => $sum,'date' => $currentStart]);- //echo $name . ': ' . $sum . '<br>';- }- // do something for all budgets.- // jump to next year.- $currentStart = clone $currentEnd;- $currentStart->addDay();+ // save to array:+ $year = $currentStart->year;+ $entry['name'] = $name;+ $entry['spent'][$year] = ($sum * -1);+ $entry['budgeted'][$year] = $budgeted;+ // jump to next year.+ $currentStart = clone $currentEnd;+ $currentStart->addDay();+ }+ $entries->push($entry);}-- $data = $this->generator->multiYear($collection);-- //$cache->store($data);+ // generate chart with data:+ $data = $this->generator->multiYear($entries);return Response::json($data);diff --git a/public/js/reports/default/multi-year.js b/public/js/reports/default/multi-year.jsindex 9b3448b489..bf18c64a91 100644--- a/public/js/reports/default/multi-year.js+++ b/public/js/reports/default/multi-year.js@@ -23,6 +23,7 @@ function drawChart() {// draw budget chart based on selected budgets:$('.budget-checkbox').on('change', updateBudgetChart);+ updateBudgetChart();}@@ -37,12 +38,24 @@ function updateBudgetChart(e) {budgets.push(current.val());}});- var budgetIds = budgets.join(',');- // remove old chart:- $('#budgets-chart').replaceWith('<canvas id="budgets-chart" class="budgets-chart" style="width:100%;height:400px;"></canvas>');+ if(budgets.length > 0) {- // draw chart. Redraw when exists? Not sure if we support that.- columnChart('chart/budget/multi-year/' + reportType + '/' + startDate + '/' + endDate + '/' + accountIds + '/' + budgetIds, 'budgets-chart');+ var budgetIds = budgets.join(',');++ // remove old chart:+ $('#budgets-chart').replaceWith('<canvas id="budgets-chart" class="budgets-chart" style="width:100%;height:400px;"></canvas>');++ // hide message:+ $('#budgets-chart-message').hide();++ // draw chart. Redraw when exists? Not sure if we support that.+ columnChart('chart/budget/multi-year/' + reportType + '/' + startDate + '/' + endDate + '/' + accountIds + '/' + budgetIds, 'budgets-chart');+ } else {+ // hide canvas, show message:+ $('#budgets-chart-message').show();+ $('#budgets-chart').hide();++ }}\ No newline at end of filediff --git a/resources/twig/reports/default/multi-year.twig b/resources/twig/reports/default/multi-year.twigindex 491713e802..5ab297f952 100644--- a/resources/twig/reports/default/multi-year.twig+++ b/resources/twig/reports/default/multi-year.twig@@ -30,7 +30,7 @@</div>{% for account in accounts %}- <div class="row">+ <div class="row" style="display:none;"><div class="col-lg-12 col-md-12 col-sm-12"><div class="box"><div class="box-header with-border">@@ -53,6 +53,9 @@<div class="box-body"><div class="row"><div class="col-lg-12">+ <p class="well" id="budgets-chart-message" style="display:none;">+ Select one or more budgets to generate this chart. Insofar possible, your selection will be saved.+ </p><canvas id="budgets-chart" class="budgets-chart" style="width:100%;height:400px;"></canvas></div></div>