Skip to content

Firefly III: a personal finances manager

Work on new chart for year report.

James Cole6267930938b8
diff --git a/app/Helpers/Report/BudgetReportHelper.php b/app/Helpers/Report/BudgetReportHelper.php
index cf447ea4a6..547840f962 100644
--- a/app/Helpers/Report/BudgetReportHelper.php
+++ b/app/Helpers/Report/BudgetReportHelper.php
@@ -18,6 +18,7 @@ use FireflyIII\Helpers\Collection\BudgetLine;
use FireflyIII\Models\Budget;
use FireflyIII\Models\LimitRepetition;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
+use FireflyIII\Support\CacheProperties;
use Illuminate\Support\Collection;
/**
@@ -40,6 +41,73 @@ class BudgetReportHelper implements BudgetReportHelperInterface
$this->repository = $repository;
}
+ /**
+ * @param Carbon $start
+ * @param Carbon $end
+ * @param Collection $accounts
+ *
+ * @return Collection
+ */
+ public function budgetYearOverview(Carbon $start, Carbon $end, Collection $accounts): Collection
+ {
+ // chart properties for cache:
+ $cache = new CacheProperties;
+ $cache->addProperty($start);
+ $cache->addProperty($end);
+ $cache->addProperty('budget-year');
+ $cache->addProperty($accounts->pluck('id')->toArray());
+ if ($cache->has()) {
+ return $cache->get();
+ }
+
+ $headers = [];
+ $current = clone $start;
+ $return = new Collection;
+ $set = $this->repository->getBudgets();
+ $budgets = [];
+ $spent = [];
+ while ($current < $end) {
+ $short = $current->format('m-Y');
+ $headers[$short] = $current->formatLocalized((string)trans('config.month'));
+ $current->addMonth();
+ }
+
+
+ /** @var Budget $budget */
+ foreach ($set as $budget) {
+ $id = $budget->id;
+ $budgets[$id] = $budget->name;
+ $spent[$id] = [];
+ $current = clone $start;
+ $sum = '0';
+
+
+ while ($current < $end) {
+ $currentEnd = clone $current;
+ $currentEnd->endOfMonth();
+ $format = $current->format('m-Y');
+ $budgetSpent = $this->repository->spentInPeriod(new Collection([$budget]), $accounts, $current, $currentEnd);
+ $spent[$id][$format] = $budgetSpent;
+ $sum = bcadd($sum, $budgetSpent);
+ $current->addMonth();
+ }
+
+ if (bccomp('0', $sum) === 0) {
+ // not spent anything.
+ unset($spent[$id]);
+ unset($budgets[$id]);
+ }
+ }
+
+ $return->put('headers', $headers);
+ $return->put('budgets', $budgets);
+ $return->put('spent', $spent);
+
+ $cache->store($return);
+
+ return $return;
+ }
+
/**
* @param Carbon $start
* @param Carbon $end
diff --git a/app/Helpers/Report/BudgetReportHelperInterface.php b/app/Helpers/Report/BudgetReportHelperInterface.php
index dd0290f6ae..d776da2ead 100644
--- a/app/Helpers/Report/BudgetReportHelperInterface.php
+++ b/app/Helpers/Report/BudgetReportHelperInterface.php
@@ -23,6 +23,15 @@ use Illuminate\Support\Collection;
*/
interface BudgetReportHelperInterface
{
+ /**
+ * @param Carbon $start
+ * @param Carbon $end
+ * @param Collection $accounts
+ *
+ * @return Collection
+ */
+ public function budgetYearOverview(Carbon $start, Carbon $end, Collection $accounts): Collection;
+
/**
* @param Carbon $start
* @param Carbon $end
diff --git a/public/js/ff/reports/default/year.js b/public/js/ff/reports/default/year.js
index f342c7e1dc..79a3b50faa 100644
--- a/public/js/ff/reports/default/year.js
+++ b/public/js/ff/reports/default/year.js
@@ -19,16 +19,19 @@ function drawChart() {
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');
- // in a loop
- $.each($('.budget_year_chart'), function (i, v) {
- var holder = $(v);
- var id = holder.attr('id');
- var budgetId = holder.data('budget');
- columnChart('chart/budget/period/' + budgetId + '/' + reportType + '/' + startDate + '/' + endDate + '/' + accountIds, id);
-
- });
+ $('.budget-chart-activate').on('click', clickBudgetChart);
}
+function clickBudgetChart(e) {
+ "use strict";
+ var link = $(e.target);
+ var budgetId = link.data('budget');
+ console.log('Budget id is ' + budgetId);
+ $('#budget_chart').empty();
+ columnChart('chart/budget/period/' + budgetId + '/' + reportType + '/' + startDate + '/' + endDate + '/' + accountIds, 'budget_chart');
+
+ return false;
+}
function showIncomes() {
"use strict";
diff --git a/resources/views/reports/default/year.twig b/resources/views/reports/default/year.twig
index 89a2403ca2..9f605f9f42 100644
--- a/resources/views/reports/default/year.twig
+++ b/resources/views/reports/default/year.twig
@@ -61,21 +61,55 @@
</div>
</div>
- {% for budget in budgets %}
- <div class="row">
- <div class="col-lg-12 col-md-12 col-sm-12">
- <div class="box" id="year-budget-{{ budget.id }}">
- <div class="box-header with-border">
- <h3 class="box-title">{{ 'budget'|_ }} {{ budget.name }}</h3>
- </div>
- <div class="box-body">
- <canvas height="400" id="budgets_{{ budget.id }}" class="budget_year_chart" data-budget="{{ budget.id }}"
- style="width:100%;height:400px;"></canvas>
- </div>
+ <div class="row">
+ <div class="col-lg-12 col-md-12 col-sm-12">
+ <div class="box">
+ <div class="box-header with-border">
+ <h3 class="box-title">{{ 'budgets'|_ }}</h3>
+ </div>
+ <div class="box-body">
+ <table class="table table-condensed table-hover">
+ <thead>
+ <tr>
+ <th>&nbsp;</th>
+ {% for date, header in budgets.get('headers') %}
+ <th>{{ header }}</th>
+ {% endfor %}
+ </tr>
+ </thead>
+ <tbody>
+ {% set spentData = budgets.get('spent') %}
+ {% for budgetId, budgetName in budgets.get('budgets') %}
+ <tr>
+ <th>
+ <a title="{{ budgetName }}" href="#" data-budget="{{ budgetId }}" class="budget-chart-activate">{{ budgetName }}</a>
+ </th>
+ {% for date, header in budgets.get('headers') %}
+ <td>{{ spentData[budgetId][date]|formatAmount }}</td>
+ {% endfor %}
+
+ </tr>
+ {% endfor %}
+ </tbody>
+
+ </table>
+ </div>
+ </div>
+ </div>
+ </div>
+ <div class="row">
+ <div class="col-lg-12 col-md-12 col-sm-12">
+ <div class="box">
+ <div class="box-header with-border">
+ <h3 class="box-title">{{ 'chart'|_ }}</h3>
+ </div>
+ <div class="box-body">
+ <canvas height="400" id="budget_chart" style="width:100%;height:400px;"></canvas>
+
</div>
</div>
</div>
- {% endfor %}
+ </div>
{% endblock %}
{% block scripts %}