From 6267930938b8d585e2805f35450b673a085da723 Mon Sep 17 00:00:00 2001 From: James Cole Date: Thu, 16 Jun 2016 20:52:30 +0200 Subject: [PATCH] Work on new chart for year report. --- app/Helpers/Report/BudgetReportHelper.php | 68 +++++++++++++++++++ .../Report/BudgetReportHelperInterface.php | 9 +++ public/js/ff/reports/default/year.js | 19 +++--- resources/views/reports/default/year.twig | 58 ++++++++++++---- 4 files changed, 134 insertions(+), 20 deletions(-) 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 @@ - {% for budget in budgets %} -
-
-
-
-

{{ 'budget'|_ }} {{ budget.name }}

-
-
- -
+
+
+
+
+

{{ 'budgets'|_ }}

+
+
+ + + + + {% for date, header in budgets.get('headers') %} + + {% endfor %} + + + + {% set spentData = budgets.get('spent') %} + {% for budgetId, budgetName in budgets.get('budgets') %} + + + {% for date, header in budgets.get('headers') %} + + {% endfor %} + + + {% endfor %} + + +
 {{ header }}
+ {{ budgetName }} + {{ spentData[budgetId][date]|formatAmount }}
- {% endfor %} +
+
+
+
+
+

{{ 'chart'|_ }}

+
+
+ + +
+
+
+
{% endblock %} {% block scripts %}