Skip to content

Firefly III: a personal finances manager

Join two charts, simpler code.

James Colee15ea0418699
diff --git a/app/Helpers/Report/BudgetReportHelper.php b/app/Helpers/Report/BudgetReportHelper.php
index 4f370f89bb..1aa1c45711 100644
--- a/app/Helpers/Report/BudgetReportHelper.php
+++ b/app/Helpers/Report/BudgetReportHelper.php
@@ -22,7 +22,6 @@ use FireflyIII\Models\Budget;
use FireflyIII\Models\LimitRepetition;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
-use FireflyIII\Support\CacheProperties;
use Illuminate\Database\Query\JoinClause;
use Illuminate\Support\Collection;
use stdClass;
@@ -48,70 +47,31 @@ class BudgetReportHelper implements BudgetReportHelperInterface
}
/**
- * @SuppressWarnings(PHPMD.ExcessiveMethodLength) // at 43, its ok.
- * @SuppressWarnings(PHPMD.CyclomaticComplexity) // it's exactly 5.
- *
* @param Carbon $start
* @param Carbon $end
* @param Collection $accounts
*
- * @return Collection
+ * @return array
*/
- public function budgetYearOverview(Carbon $start, Carbon $end, Collection $accounts): Collection
+ public function getBudgetPeriodReport(Carbon $start, Carbon $end, Collection $accounts): array
{
- // 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();
- }
-
- $current = clone $start;
- $return = new Collection;
- $set = $this->repository->getBudgets();
- $budgets = [];
- $spent = [];
- $headers = $this->createYearHeaders($current, $end);
-
- /** @var Budget $budget */
- foreach ($set as $budget) {
- $id = $budget->id;
- $budgets[$id] = $budget->name;
- $current = clone $start;
- $budgetData = $this->getBudgetSpentData($current, $end, $budget, $accounts);
- $sum = $budgetData['sum'];
- $spent[$id] = $budgetData['spent'];
+ // get account ID's.
+ $accountIds = $accounts->pluck('id')->toArray();
- if (bccomp('0', $sum) === 0) {
- // not spent anything.
- unset($spent[$id]);
- unset($budgets[$id]);
- }
+ // define period to group on:
+ $sqlDateFormat = '%Y-%m-%d';
+ // monthly report (for year)
+ if ($start->diffInMonths($end) > 1) {
+ $sqlDateFormat = '%Y-%m';
}
- $return->put('headers', $headers);
- $return->put('budgets', $budgets);
- $return->put('spent', $spent);
-
- $cache->store($return);
-
- return $return;
- }
+ // yearly report (for multi year)
+ if ($start->diffInMonths($end) > 12) {
+ $sqlDateFormat = '%Y';
+ }
- /**
- * @param Carbon $start
- * @param Carbon $end
- * @param Collection $accounts
- *
- * @return array
- */
- public function getBudgetMultiYear(Carbon $start, Carbon $end, Collection $accounts): array
- {
- $accountIds = $accounts->pluck('id')->toArray();
- $query = TransactionJournal
+ // build query.
+ $query = TransactionJournal
::leftJoin('budget_transaction_journal', 'budget_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id')
->leftJoin('transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id')
->leftJoin(
@@ -127,18 +87,18 @@ class BudgetReportHelper implements BudgetReportHelperInterface
if (count($accountIds) > 0) {
$query->whereIn('transactions.account_id', $accountIds);
}
- $query->groupBy(['budget_transaction_journal.budget_id', 'the_year']);
+ $query->groupBy(['budget_transaction_journal.budget_id', 'period_marker']);
$queryResult = $query->get(
[
'budget_transaction_journal.budget_id',
- DB::raw('DATE_FORMAT(transaction_journals.date,"%Y") AS the_year'),
+ DB::raw('DATE_FORMAT(transaction_journals.date,"' . $sqlDateFormat . '") AS period_marker'),
DB::raw('SUM(transactions.amount) as sum_of_period'),
]
);
$data = [];
$budgets = $this->repository->getBudgets();
- $years = $this->listOfYears($start, $end);
+ $periods = $this->listOfPeriods($start, $end);
// do budget "zero"
$emptyBudget = new Budget;
@@ -151,12 +111,12 @@ class BudgetReportHelper implements BudgetReportHelperInterface
foreach ($budgets as $budget) {
$data[$budget->id] = [
'name' => $budget->name,
- 'entries' => $this->filterAmounts($queryResult, $budget->id, $years),
+ 'entries' => $this->filterAllAmounts($queryResult, $budget->id, $periods),
'sum' => '0',
];
}
// filter out empty ones and fill sum:
- $data = $this->getBudgetMultiYearMeta($data);
+ $data = $this->filterBudgetPeriodReport($data);
return $data;
}
@@ -252,16 +212,34 @@ class BudgetReportHelper implements BudgetReportHelperInterface
*
* @return array
*/
- public function listOfYears(Carbon $start, Carbon $end): array
+ public function listOfPeriods(Carbon $start, Carbon $end): array
{
- $begin = clone $start;
- $years = [];
+ // define period to increment
+ $increment = 'addDay';
+ $format = 'Y-m-d';
+ // increment by month (for year)
+ if ($start->diffInMonths($end) > 1) {
+ $increment = 'addMonth';
+ $format = 'Y-m';
+ }
+
+ // increment by year (for multi year)
+ if ($start->diffInMonths($end) > 12) {
+ $increment = 'addYear';
+ $format = 'Y';
+ }
+
+ $begin = clone $start;
+ $entries = [];
while ($begin < $end) {
- $years[] = $begin->year;
- $begin->addYear();
+ $formatted = $begin->format($format);
+ $entries[$formatted] = $formatted;
+
+ $begin->$increment();
}
- return $years;
+ return $entries;
+
}
/**
@@ -285,38 +263,22 @@ class BudgetReportHelper implements BudgetReportHelperInterface
}
/**
- * @param Carbon $current
- * @param Carbon $end
+ * Filters entries from the result set generated by getBudgetPeriodReport
*
- * @return array
- */
- private function createYearHeaders(Carbon $current, Carbon $end): array
- {
- $headers = [];
- while ($current < $end) {
- $short = $current->format('m-Y');
- $headers[$short] = $current->formatLocalized((string)trans('config.month'));
- $current->addMonth();
- }
-
- return $headers;
- }
-
- /**
* @param Collection $set
* @param int $budgetId
- * @param array $years
+ * @param array $periods
*
* @return array
*/
- private function filterAmounts(Collection $set, int $budgetId, array $years):array
+ private function filterAllAmounts(Collection $set, int $budgetId, array $periods):array
{
$arr = [];
- foreach ($years as $year) {
+ foreach ($periods as $period) {
/** @var stdClass $object */
$result = $set->filter(
- function (TransactionJournal $object) use ($budgetId, $year) {
- return intval($object->the_year) === $year && $budgetId === intval($object->budget_id);
+ function (TransactionJournal $object) use ($budgetId, $period) {
+ return strval($object->period_marker) === $period && $budgetId === intval($object->budget_id);
}
);
$amount = '0';
@@ -324,18 +286,20 @@ class BudgetReportHelper implements BudgetReportHelperInterface
$amount = $result->first()->sum_of_period;
}
- $arr[$year] = $amount;
+ $arr[$period] = $amount;
}
return $arr;
}
/**
+ * Filters empty results from getBudgetPeriodReport
+ *
* @param array $data
*
* @return array
*/
- private function getBudgetMultiYearMeta(array $data): array
+ private function filterBudgetPeriodReport(array $data): array
{
/**
* @var int $budgetId
@@ -355,31 +319,4 @@ class BudgetReportHelper implements BudgetReportHelperInterface
return $data;
}
- /**
- * @param Carbon $current
- * @param Carbon $end
- * @param Budget $budget
- * @param Collection $accounts
- *
- * @return array
- */
- private function getBudgetSpentData(Carbon $current, Carbon $end, Budget $budget, Collection $accounts): array
- {
- $sum = '0';
- $spent = [];
- while ($current < $end) {
- $currentEnd = clone $current;
- $currentEnd->endOfMonth();
- $format = $current->format('m-Y');
- $budgetSpent = $this->repository->spentInPeriod(new Collection([$budget]), $accounts, $current, $currentEnd);
- $spent[$format] = $budgetSpent;
- $sum = bcadd($sum, $budgetSpent);
- $current->addMonth();
- }
-
- return [
- 'spent' => $spent,
- 'sum' => $sum,
- ];
- }
}
diff --git a/app/Helpers/Report/BudgetReportHelperInterface.php b/app/Helpers/Report/BudgetReportHelperInterface.php
index 89c785b0a8..ee1bb2a1b6 100644
--- a/app/Helpers/Report/BudgetReportHelperInterface.php
+++ b/app/Helpers/Report/BudgetReportHelperInterface.php
@@ -25,14 +25,6 @@ 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
@@ -41,7 +33,7 @@ interface BudgetReportHelperInterface
*
* @return array
*/
- public function getBudgetMultiYear(Carbon $start, Carbon $end, Collection $accounts): array;
+ public function getBudgetPeriodReport(Carbon $start, Carbon $end, Collection $accounts): array;
/**
* @param Carbon $start
@@ -67,6 +59,6 @@ interface BudgetReportHelperInterface
*
* @return array
*/
- public function listOfYears(Carbon $start, Carbon $end): array;
+ public function listOfPeriods(Carbon $start, Carbon $end): array;
}
diff --git a/app/Http/Controllers/Chart/ReportController.php b/app/Http/Controllers/Chart/ReportController.php
index c8fecbdad2..ca02e01e37 100644
--- a/app/Http/Controllers/Chart/ReportController.php
+++ b/app/Http/Controllers/Chart/ReportController.php
@@ -49,20 +49,18 @@ class ReportController extends Controller
* 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 string $reportType
* @param Carbon $start
* @param Carbon $end
* @param Collection $accounts
*
* @return \Illuminate\Http\JsonResponse
*/
- public function netWorth(string $reportType, Carbon $start, Carbon $end, Collection $accounts)
+ public function netWorth(Carbon $start, Carbon $end, Collection $accounts)
{
// 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()) {
diff --git a/app/Http/Controllers/Report/BudgetController.php b/app/Http/Controllers/Report/BudgetController.php
index c000f86a24..aa1f844f14 100644
--- a/app/Http/Controllers/Report/BudgetController.php
+++ b/app/Http/Controllers/Report/BudgetController.php
@@ -29,6 +29,7 @@ class BudgetController extends Controller
{
/**
+ *
* @param BudgetReportHelperInterface $helper
* @param Carbon $start
* @param Carbon $end
@@ -36,22 +37,20 @@ class BudgetController extends Controller
*
* @return mixed|string
*/
- public function budgetMultiYear(BudgetReportHelperInterface $helper, Carbon $start, Carbon $end, Collection $accounts)
+ public function budgetPeriodReport(BudgetReportHelperInterface $helper, Carbon $start, Carbon $end, Collection $accounts)
{
$cache = new CacheProperties;
$cache->addProperty($start);
$cache->addProperty($end);
- $cache->addProperty('budget-mult-year-report');
+ $cache->addProperty('budget-period-report');
$cache->addProperty($accounts->pluck('id')->toArray());
if ($cache->has()) {
- return $cache->get();
+ //return $cache->get();
}
-
- $years = $helper->listOfYears($start, $end);
- $budgetMultiYear = $helper->getBudgetMultiYear($start, $end, $accounts);
-
- $result = view('reports.partials.budget-multi-year', compact('budgetMultiYear', 'years'))->render();
+ $periods = $helper->listOfPeriods($start, $end);
+ $budgets = $helper->getBudgetPeriodReport($start, $end, $accounts);
+ $result = view('reports.partials.budget-period', compact('budgets', 'periods'))->render();
$cache->store($result);
return $result;
@@ -86,35 +85,4 @@ class BudgetController extends Controller
return $result;
}
-
- /**
- * @param BudgetReportHelperInterface $helper
- * @param Carbon $start
- * @param Carbon $end
- * @param Collection $accounts
- *
- * @return string
- */
- public function budgetYearOverview(BudgetReportHelperInterface $helper, Carbon $start, Carbon $end, Collection $accounts)
- {
-
- // chart properties for cache:
- $cache = new CacheProperties;
- $cache->addProperty($start);
- $cache->addProperty($end);
- $cache->addProperty('budget-year-overview');
- $cache->addProperty($accounts->pluck('id')->toArray());
- if ($cache->has()) {
- return $cache->get();
- }
-
- $budgets = $helper->budgetYearOverview($start, $end, $accounts);
-
- $result = view('reports.partials.budget-year-overview', compact('budgets'))->render();
- $cache->store($result);
-
- return $result;
-
- }
-
}
\ No newline at end of file
diff --git a/app/Http/Controllers/Transaction/SplitController.php b/app/Http/Controllers/Transaction/SplitController.php
index d1f3504670..7c80d3f392 100644
--- a/app/Http/Controllers/Transaction/SplitController.php
+++ b/app/Http/Controllers/Transaction/SplitController.php
@@ -187,6 +187,7 @@ class SplitController extends Controller
'transactions' => $this->getTransactionDataFromRequest($request),
];
+
return $array;
}
diff --git a/public/js/ff/reports/default/multi-year.js b/public/js/ff/reports/default/multi-year.js
index 0c8213785b..1bc23389ae 100644
--- a/public/js/ff/reports/default/multi-year.js
+++ b/public/js/ff/reports/default/multi-year.js
@@ -5,7 +5,7 @@ $(function () {
"use strict";
drawChart();
- loadAjaxPartial('budgetMultiYear', budgetMultiUri);
+ loadAjaxPartial('budgetPeriodReport', budgetPeriodReportUri);
});
function drawChart() {
diff --git a/public/js/ff/reports/default/year.js b/public/js/ff/reports/default/year.js
index a28ac3f8c4..a7c5121abe 100644
--- a/public/js/ff/reports/default/year.js
+++ b/public/js/ff/reports/default/year.js
@@ -1,16 +1,16 @@
-/* globals google, accountIds, budgetYearOverviewUri */
+/* globals google, accountIds, budgetPeriodReportUri */
$(function () {
"use strict";
drawChart();
- loadAjaxPartial('budgetOverview',budgetYearOverviewUri);
+ loadAjaxPartial('budgetPeriodReport', budgetPeriodReportUri);
});
function drawChart() {
"use strict";
- lineChart('chart/report/net-worth/' + reportType + '/' + startDate + '/' + endDate + '/' + accountIds, 'net-worth');
+ lineChart('chart/report/net-worth/' + startDate + '/' + endDate + '/' + accountIds, 'net-worth');
columnChart('chart/report/in-out/' + startDate + '/' + endDate + '/' + accountIds, 'income-expenses-chart');
columnChart('chart/report/in-out-sum/' + startDate + '/' + endDate + '/' + accountIds, 'income-expenses-sum-chart');
diff --git a/resources/views/reports/default/multi-year.twig b/resources/views/reports/default/multi-year.twig
index 0470f1a6dc..95f2932824 100644
--- a/resources/views/reports/default/multi-year.twig
+++ b/resources/views/reports/default/multi-year.twig
@@ -85,13 +85,29 @@
</div>
+ {# This is the budget overview generated by budget period report #}
<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 no-padding table-responsive loading" id="budgetMultiYear">
+ <div class="box-body no-padding table-responsive loading" id="budgetPeriodReport">
+ </div>
+ </div>
+ </div>
+ </div>
+
+
+ {# This is the chart that belongs to the above overview. #}
+ <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>
@@ -124,7 +140,7 @@
var expenseReportUri = '{{ route('reports.data.expenseReport', [start.format('Ymd'), end.format('Ymd'), accountIds]) }}';
var incExpReportUri = '{{ route('reports.data.incExpReport', [start.format('Ymd'), end.format('Ymd'), accountIds]) }}';
- var budgetMultiUri = '{{ route('reports.data.budgetMultiYear', [start.format('Ymd'), end.format('Ymd'), accountIds]) }}';
+ var budgetPeriodReportUri = '{{ route('reports.data.budgetPeriodReport', [start.format('Ymd'), end.format('Ymd'), accountIds]) }}';
</script>
<script type="text/javascript" src="js/ff/reports/default/all.js"></script>
diff --git a/resources/views/reports/default/year.twig b/resources/views/reports/default/year.twig
index 0f99348969..de0a4df7bb 100644
--- a/resources/views/reports/default/year.twig
+++ b/resources/views/reports/default/year.twig
@@ -84,17 +84,21 @@
</div>
</div>
+ {# This is the budget overview generated by budget period report #}
<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 no-padding table-responsive loading" id="budgetOverview">
+ <div class="box-body no-padding table-responsive loading" id="budgetPeriodReport">
</div>
</div>
</div>
</div>
+
+
+ {# This is the chart that belongs to the above overview. #}
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12">
<div class="box">
@@ -129,7 +133,7 @@
var expenseReportUri = '{{ route('reports.data.expenseReport', [start.format('Ymd'), end.format('Ymd'), accountIds]) }}';
var incExpReportUri = '{{ route('reports.data.incExpReport', [start.format('Ymd'), end.format('Ymd'), accountIds]) }}';
- var budgetYearOverviewUri = '{{ route('reports.data.budgetYearOverview', [start.format('Ymd'), end.format('Ymd'), accountIds]) }}';
+ var budgetPeriodReportUri = '{{ route('reports.data.budgetPeriodReport', [start.format('Ymd'), end.format('Ymd'), accountIds]) }}';
</script>
<script type="text/javascript" src="js/ff/reports/default/all.js"></script>
diff --git a/resources/views/reports/partials/budget-multi-year.twig b/resources/views/reports/partials/budget-period.twig
similarity index 77%
rename from resources/views/reports/partials/budget-multi-year.twig
rename to resources/views/reports/partials/budget-period.twig
index 7fc30ea9f3..7c4b16cee4 100644
--- a/resources/views/reports/partials/budget-multi-year.twig
+++ b/resources/views/reports/partials/budget-period.twig
@@ -2,19 +2,19 @@
<thead>
<tr>
<th>{{ 'budget'|_ }}</th>
- {% for year in years %}
- <th>{{ year }}</th>
+ {% for period in periods %}
+ <th>{{ period }}</th>
{% endfor %}
<th>{{ 'sum'|_ }}</th>
</tr>
</thead>
<tbody>
- {% for id, info in budgetMultiYear %}
+ {% for id, info in budgets %}
<tr>
<td>
+ (<a title="{{ info.name }}" href="#" data-budget="{{ id }}" class="budget-chart-activate">{{ info.name }}</a>)
{% if id == 0 %}
<a href="{{ route('budgets.noBudget') }}">{{ info.name }}</a>
-
{% else %}
<a href="{{ route('budgets.show', id) }}">{{ info.name }}</a>
{% endif %}
diff --git a/resources/views/reports/partials/budget-year-overview.twig b/resources/views/reports/partials/budget-year-overview.twig
deleted file mode 100644
index 6836d0d25b..0000000000
--- a/resources/views/reports/partials/budget-year-overview.twig
+++ /dev/null
@@ -1,25 +0,0 @@
-<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>
\ No newline at end of file
diff --git a/routes/web.php b/routes/web.php
index bff941d52f..37bae1859b 100755
--- a/routes/web.php
+++ b/routes/web.php
@@ -237,7 +237,7 @@ Route::group(
// reports:
Route::get('/chart/report/in-out/{start_date}/{end_date}/{accountList}', ['uses' => 'Chart\ReportController@yearInOut']);
Route::get('/chart/report/in-out-sum/{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']);
+ Route::get('/chart/report/net-worth/{start_date}/{end_date}/{accountList}', ['uses' => 'Chart\ReportController@netWorth']);
/**
@@ -388,19 +388,12 @@ Route::group(
'/reports/data/budget-report/{start_date}/{end_date}/{accountList}',
['uses' => 'Report\BudgetController@budgetReport', 'as' => 'reports.data.budgetReport']
);
- // budget year overview
+ // budget period overview
Route::get(
- '/reports/data/budget-year-overview/{start_date}/{end_date}/{accountList}',
- ['uses' => 'Report\BudgetController@budgetYearOverview', 'as' => 'reports.data.budgetYearOverview']
+ '/reports/data/budget-period/{start_date}/{end_date}/{accountList}',
+ ['uses' => 'Report\BudgetController@budgetPeriodReport', 'as' => 'reports.data.budgetPeriodReport']
);
- // budget multi year overview
- Route::get(
- '/reports/data/budget-multi-year/{start_date}/{end_date}/{accountList}',
- ['uses' => 'Report\BudgetController@budgetMultiYear', 'as' => 'reports.data.budgetMultiYear']
- );
-
-
/**
* Rules Controller
*/