Multi year report move to AJAX.

Signed-off-by: James Cole <thegrumpydictator@gmail.com>
This commit is contained in:
James Cole 2016-11-02 20:08:11 +01:00
parent 2ddd4314f1
commit e24f5ec9f3
No known key found for this signature in database
GPG Key ID: C16961E655E74B5E
15 changed files with 267 additions and 272 deletions

View File

@ -15,13 +15,17 @@ namespace FireflyIII\Helpers\Report;
use Carbon\Carbon;
use DB;
use FireflyIII\Helpers\Collection\Budget as BudgetCollection;
use FireflyIII\Helpers\Collection\BudgetLine;
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;
/**
* Class BudgetReportHelper
@ -97,6 +101,66 @@ class BudgetReportHelper implements BudgetReportHelperInterface
return $return;
}
/**
* @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
::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(
'transactions', function (JoinClause $join) {
$join->on('transaction_journals.id', '=', 'transactions.transaction_journal_id')->where('transactions.amount', '<', 0);
}
)
->whereNull('transaction_journals.deleted_at')
->whereNull('transactions.deleted_at')
->where('transaction_types.type', 'Withdrawal')
->where('transaction_journals.user_id', auth()->user()->id);
if (count($accountIds) > 0) {
$query->whereIn('transactions.account_id', $accountIds);
}
$query->groupBy(['budget_transaction_journal.budget_id', 'the_year']);
$queryResult = $query->get(
[
'budget_transaction_journal.budget_id',
DB::raw('DATE_FORMAT(transaction_journals.date,"%Y") AS the_year'),
DB::raw('SUM(transactions.amount) as sum_of_period'),
]
);
$data = [];
$budgets = $this->repository->getBudgets();
$years = $this->listOfYears($start, $end);
// do budget "zero"
$emptyBudget = new Budget;
$emptyBudget->id = 0;
$emptyBudget->name = strval(trans('firefly.no_budget'));
$budgets->push($emptyBudget);
// get all budgets and years.
foreach ($budgets as $budget) {
$data[$budget->id] = [
'name' => $budget->name,
'entries' => $this->filterAmounts($queryResult, $budget->id, $years),
'sum' => '0',
];
}
// filter out empty ones and fill sum:
$data = $this->getBudgetMultiYearMeta($data);
return $data;
}
/**
* @param Carbon $start
* @param Carbon $end
@ -183,30 +247,21 @@ class BudgetReportHelper implements BudgetReportHelperInterface
}
/**
* Take the array as returned by CategoryRepositoryInterface::spentPerDay and CategoryRepositoryInterface::earnedByDay
* and sum up everything in the array in the given range.
*
* @param Carbon $start
* @param Carbon $end
* @param array $array
*
* @return string
* @return array
*/
protected function getSumOfRange(Carbon $start, Carbon $end, array $array)
public function listOfYears(Carbon $start, Carbon $end): array
{
$sum = '0';
$currentStart = clone $start; // to not mess with the original one
$currentEnd = clone $end; // to not mess with the original one
while ($currentStart <= $currentEnd) {
$date = $currentStart->format('Y-m-d');
if (isset($array[$date])) {
$sum = bcadd($sum, $array[$date]);
}
$currentStart->addDay();
$begin = clone $start;
$years = [];
while ($begin < $end) {
$years[] = $begin->year;
$begin->addYear();
}
return $sum;
return $years;
}
/**
@ -247,6 +302,59 @@ class BudgetReportHelper implements BudgetReportHelperInterface
return $headers;
}
/**
* @param Collection $set
* @param int $budgetId
* @param array $years
*
* @return array
*/
private function filterAmounts(Collection $set, int $budgetId, array $years):array
{
$arr = [];
foreach ($years as $year) {
/** @var stdClass $object */
$result = $set->filter(
function (TransactionJournal $object) use ($budgetId, $year) {
return intval($object->the_year) === $year && $budgetId === intval($object->budget_id);
}
);
$amount = '0';
if (!is_null($result->first())) {
$amount = $result->first()->sum_of_period;
}
$arr[$year] = $amount;
}
return $arr;
}
/**
* @param array $data
*
* @return array
*/
private function getBudgetMultiYearMeta(array $data): array
{
/**
* @var int $budgetId
* @var array $set
*/
foreach ($data as $budgetId => $set) {
$sum = '0';
foreach ($set['entries'] as $amount) {
$sum = bcadd($amount, $sum);
}
$data[$budgetId]['sum'] = $sum;
if (bccomp('0', $sum) === 0) {
unset($data[$budgetId]);
}
}
return $data;
}
/**
* @param Carbon $current
* @param Carbon $end

View File

@ -34,6 +34,15 @@ interface BudgetReportHelperInterface
*/
public function budgetYearOverview(Carbon $start, Carbon $end, Collection $accounts): Collection;
/**
* @param Carbon $start
* @param Carbon $end
* @param Collection $accounts
*
* @return array
*/
public function getBudgetMultiYear(Carbon $start, Carbon $end, Collection $accounts): array;
/**
* @param Carbon $start
* @param Carbon $end
@ -52,4 +61,12 @@ interface BudgetReportHelperInterface
*/
public function getBudgetsWithExpenses(Carbon $start, Carbon $end, Collection $accounts): Collection;
/**
* @param Carbon $start
* @param Carbon $end
*
* @return array
*/
public function listOfYears(Carbon $start, Carbon $end): array;
}

View File

@ -113,66 +113,6 @@ class ReportHelper implements ReportHelperInterface
return $collection;
}
/**
* @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
::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(
'transactions', function (JoinClause $join) {
$join->on('transaction_journals.id', '=', 'transactions.transaction_journal_id')->where('transactions.amount', '<', 0);
}
)
->whereNull('transaction_journals.deleted_at')
->whereNull('transactions.deleted_at')
->where('transaction_types.type', 'Withdrawal')
->where('transaction_journals.user_id', auth()->user()->id);
if (count($accountIds) > 0) {
$query->whereIn('transactions.account_id', $accountIds);
}
$query->groupBy(['budget_transaction_journal.budget_id', 'the_year']);
$queryResult = $query->get(
[
'budget_transaction_journal.budget_id',
DB::raw('DATE_FORMAT(transaction_journals.date,"%Y") AS the_year'),
DB::raw('SUM(transactions.amount) as sum_of_period'),
]
);
$data = [];
$budgets = $this->budgetRepository->getBudgets();
$years = $this->listOfYears($start, $end);
// do budget "zero"
$emptyBudget = new Budget;
$emptyBudget->id = 0;
$emptyBudget->name = strval(trans('firefly.no_budget'));
$budgets->push($emptyBudget);
// get all budgets and years.
foreach ($budgets as $budget) {
$data[$budget->id] = [
'name' => $budget->name,
'entries' => $this->filterAmounts($queryResult, $budget->id, $years),
'sum' => '0',
];
}
// filter out empty ones and fill sum:
$data = $this->getBudgetMultiYearMeta($data);
return $data;
}
/**
* @param Carbon $start
* @param Carbon $end
@ -293,24 +233,6 @@ class ReportHelper implements ReportHelperInterface
return $months;
}
/**
* @param Carbon $start
* @param Carbon $end
*
* @return array
*/
public function listOfYears(Carbon $start, Carbon $end): array
{
$begin = clone $start;
$years = [];
while ($begin < $end) {
$years[] = $begin->year;
$begin->addYear();
}
return $years;
}
/**
* Returns an array of tags and their comparitive size with amounts bla bla.
*
@ -385,85 +307,4 @@ class ReportHelper implements ReportHelperInterface
return $collection;
}
/**
* @param array $data
*
* @return array
*/
protected function getBudgetMultiYearMeta(array $data): array
{
/**
* @var int $budgetId
* @var array $set
*/
foreach ($data as $budgetId => $set) {
$sum = '0';
foreach ($set['entries'] as $amount) {
$sum = bcadd($amount, $sum);
}
$data[$budgetId]['sum'] = $sum;
if (bccomp('0', $sum) === 0) {
unset($data[$budgetId]);
}
}
return $data;
}
/**
* Take the array as returned by CategoryRepositoryInterface::spentPerDay and CategoryRepositoryInterface::earnedByDay
* and sum up everything in the array in the given range.
*
* @param Carbon $start
* @param Carbon $end
* @param array $array
*
* @return string
*/
protected function getSumOfRange(Carbon $start, Carbon $end, array $array)
{
$sum = '0';
$currentStart = clone $start; // to not mess with the original one
$currentEnd = clone $end; // to not mess with the original one
while ($currentStart <= $currentEnd) {
$date = $currentStart->format('Y-m-d');
if (isset($array[$date])) {
$sum = bcadd($sum, $array[$date]);
}
$currentStart->addDay();
}
return $sum;
}
/**
* @param Collection $set
* @param int $budgetId
* @param array $years
*
* @return array
*/
private function filterAmounts(Collection $set, int $budgetId, array $years):array
{
$arr = [];
foreach ($years as $year) {
/** @var stdClass $object */
$result = $set->filter(
function (TransactionJournal $object) use ($budgetId, $year) {
return intval($object->the_year) === $year && $budgetId === intval($object->budget_id);
}
);
$amount = '0';
if (!is_null($result->first())) {
$amount = $result->first()->sum_of_period;
}
$arr[$year] = $amount;
}
return $arr;
}
}

View File

@ -28,16 +28,6 @@ use Illuminate\Support\Collection;
interface ReportHelperInterface
{
/**
* @param Carbon $start
* @param Carbon $end
* @param Collection $accounts
*
* @return array
*/
public function getBudgetMultiYear(Carbon $start, Carbon $end, Collection $accounts): array;
/**
* This method generates a full report for the given period on all
* the users bills and their payments.
@ -90,14 +80,6 @@ interface ReportHelperInterface
*/
public function listOfMonths(Carbon $date): array;
/**
* @param Carbon $start
* @param Carbon $end
*
* @return array
*/
public function listOfYears(Carbon $start, Carbon $end): array;
/**
* Returns an array of tags and their comparitive size with amounts bla bla.
*

View File

@ -61,31 +61,4 @@ class Controller extends BaseController
}
/**
* Take the array as returned by CategoryRepositoryInterface::spentPerDay and CategoryRepositoryInterface::earnedByDay
* and sum up everything in the array in the given range.
*
* @param Carbon $start
* @param Carbon $end
* @param array $array
*
* @return string
*/
protected function getSumOfRange(Carbon $start, Carbon $end, array $array)
{
$sum = '0';
$currentStart = clone $start; // to not mess with the original one
$currentEnd = clone $end; // to not mess with the original one
while ($currentStart <= $currentEnd) {
$date = $currentStart->format('Y-m-d');
if (isset($array[$date])) {
$sum = bcadd($sum, $array[$date]);
}
$currentStart->addDay();
}
return $sum;
}
}

View File

@ -28,6 +28,33 @@ use Illuminate\Support\Collection;
class BudgetController extends Controller
{
/**
* @param BudgetReportHelperInterface $helper
* @param Carbon $start
* @param Carbon $end
* @param Collection $accounts
*/
public function budgetMultiYear(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($accounts->pluck('id')->toArray());
if ($cache->has()) {
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();
$cache->store($result);
return $result;
}
/**
* @param BudgetReportHelperInterface $helper
* @param Carbon $start

View File

@ -244,8 +244,7 @@ class ReportController extends Controller
{
// need all budgets
// need all years.
$years = $this->helper->listOfYears($start, $end);
$budgetMultiYear = $this->helper->getBudgetMultiYear($start, $end, $accounts);
// and some id's, joined:
@ -259,8 +258,7 @@ class ReportController extends Controller
return view(
'reports.default.multi-year',
compact(
'accounts', 'start', 'end', 'accountIds', 'reportType',
'years', 'budgetMultiYear'
'accounts', 'start', 'end', 'accountIds', 'reportType'
)
);
}

View File

@ -294,7 +294,7 @@ class Transaction extends Twig_Extension
switch ($transaction->transaction_type_type) {
case TransactionType::WITHDRAWAL:
$txt = sprintf('<i class="fa fa-long-arrow-left fa-fw" title="%s"></i>' . trans('firefly.withdrawal'));
$txt = sprintf('<i class="fa fa-long-arrow-left fa-fw" title="%s"></i>', trans('firefly.withdrawal'));
break;
case TransactionType::DEPOSIT:
$txt = sprintf('<i class="fa fa-long-arrow-right fa-fw" title="%s"></i>', trans('firefly.deposit'));

View File

@ -128,4 +128,33 @@ function respondInfoButton(data) {
$('#defaultModal').empty().html(data.html);
$('#defaultModal').modal('show');
}
function loadAjaxPartial(holder, uri) {
"use strict";
console.log('Going to grab URI ' + uri);
$.get(uri).done(function (data) {
displayAjaxPartial(data, holder);
}).fail(function () {
failAjaxPartial(uri, holder);
});
}
function displayAjaxPartial(data, holder) {
"use strict";
console.log('Display stuff in ' + holder);
var obj = $('#' + holder);
obj.removeClass('loading').html(data);
// call some often needed recalculations and what-not:
// find a sortable table and make it sortable:
$.bootstrapSortable(true);
}
function failAjaxPartial(uri, holder) {
"use strict";
console.log('Failed to load' + uri);
$('#' + holder).removeClass('loading').addClass('general-chart-error');
}

View File

@ -1,13 +1,13 @@
/* globals google, startDate ,reportURL, endDate , reportType ,accountIds , picker:true, minDate, year, month, columnChart, lineChart, stackedColumnChart */
/* globals budgetMultiUrl, accountIds */
$(function () {
"use strict";
drawChart();
loadAjaxPartial('budgetMultiYear', budgetMultiUrl);
});
function drawChart() {
"use strict";

View File

@ -12,7 +12,7 @@
</div>
<div class="box-body table-responsive">
{% if domains|length > 0 %}
<table class="table table-condensed table-sortable">
<table class="table table-condensed sortable">
<thead>
<tr>
<th style="width:20%;">&nbsp;</th>
@ -59,7 +59,7 @@
{{ 'all_domains_is_filtered'|_ }}
</p>
<table class="table table-condensed table-sortable">
<table class="table table-condensed sortable">
<thead>
<tr>
<th style="width:20%;">&nbsp;</th>
@ -118,3 +118,9 @@
{% endblock %}
{% block styles %}
<link rel="stylesheet" href="css/bootstrap-sortable.css" type="text/css" media="all"/>
{% endblock %}
{% block scripts %}
<script type="text/javascript" src="js/lib/bootstrap-sortable.js"></script>
{% endblock %}

View File

@ -11,7 +11,7 @@
<h3 class="box-title">{{ 'all_users'|_ }}</h3>
</div>
<div class="box-body table-responsive no-padding">
<table class="table table-condensed table-sortable">
<table class="table table-condensed sortable">
<thead>
<tr>
<th colspan="2">&nbsp;</th>
@ -29,15 +29,15 @@
<tbody>
{% for user in users %}
<tr>
<td>
<td data-value="{{ user.id }}">
<div class="btn-group btn-group-xs">
<a class="btn btn-default" href="{{ route('admin.users.edit',user.id) }}"><i class="fa fa-pencil"></i></a>
</div>
</td>
<td>#{{ user.id }}</td>
<td>
<td data-value="{{ user.id }}">#{{ user.id }}</td>
<td data-value="{{ user.email }}">
<a href="{{ route('admin.users.show',user.id) }}">{{ user.email }}</a></td>
<td>
<td data-value="{{ user.created_at.format('Y-m-d-H-i-s') }}">
{{ user.created_at.formatLocalized(monthAndDayFormat) }}
{{ user.created_at.format('H:i') }}
</td>
@ -47,28 +47,28 @@
<td>
{{ Preferences.getForUser(user,"confirmation_ip_address").data }}
</td>
<td>
<td data-value="{% if user.isAdmin %}1{% else %}0{% endif %}">
{% if user.isAdmin %}
<small class="text-success"><i class="fa fa-fw fa-check"></i></small>
{% else %}
<small class="text-danger"><i class="fa fa-fw fa-times"></i></small>
{% endif %}
</td>
<td>
<td data-value="{% if user.has2FA %}1{% else %}0{% endif %}">
{% if user.has2FA %}
<small class="text-success"><i class="fa fa-fw fa-check"></i></small>
{% else %}
<small class="text-danger"><i class="fa fa-fw fa-times"></i></small>
{% endif %}
</td>
<td>
<td data-value="{% if user.activated %}1{% else %}0{% endif %}">
{% if user.activated %}
<small class="text-success"><i class="fa fa-fw fa-check"></i></small>
{% else %}
<small class="text-danger"><i class="fa fa-fw fa-times"></i></small>
{% endif %}
</td>
<td>
<td data-value="{% if user.blocked %}1{% else %}0{% endif %}">
{% if user.blocked == 1 %}
<small class="text-danger"><i class="fa fa-fw fa-check" title="{{ 'yes'|_ }}"></i></small>
{% else %}
@ -95,3 +95,9 @@
</div>
</div>
{% endblock %}
{% block styles %}
<link rel="stylesheet" href="css/bootstrap-sortable.css" type="text/css" media="all"/>
{% endblock %}
{% block scripts %}
<script type="text/javascript" src="js/lib/bootstrap-sortable.js"></script>
{% endblock %}

View File

@ -91,40 +91,8 @@
<div class="box-header with-border">
<h3 class="box-title">{{ 'budgets'|_ }}</h3>
</div>
<div class="box-body no-padding table-responsive">
<table class="table table-hover sortable">
<thead>
<tr>
<th>{{ 'budget'|_ }}</th>
{% for year in years %}
<th>{{ year }}</th>
{% endfor %}
<th>{{ 'sum'|_ }}</th>
</tr>
</thead>
<tbody>
{% for id, info in budgetMultiYear %}
<tr>
<td>
{% if id == 0 %}
<a href="{{ route('budgets.noBudget') }}">{{ info.name }}</a>
<div class="box-body no-padding table-responsive loading" id="budgetMultiYear">
{% else %}
<a href="{{ route('budgets.show', id) }}">{{ info.name }}</a>
{% endif %}
</td>
{% for amount in info.entries %}
<td data-value="{{ amount }}">
{{ amount|formatAmount }}
</td>
{% endfor %}
<td data-value="{{ info.sum }}">
{{ info.sum|formatAmount }}
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
</div>
@ -152,6 +120,7 @@
<!-- some URL's -->
var accountReportUrl = '{{ route('reports.data.accountReport', [start.format('Ymd'), end.format('Ymd'), accountIds]) }}';
var inOutReportUrl = '{{ route('reports.data.inOutReport', [start.format('Ymd'), end.format('Ymd'), accountIds]) }}';
var budgetMultiUrl = '{{ route('reports.data.budgetMultiYear', [start.format('Ymd'), end.format('Ymd'), accountIds]) }}';
</script>
<script type="text/javascript" src="js/ff/reports/default/all.js"></script>

View File

@ -0,0 +1,33 @@
<table class="table table-hover sortable">
<thead>
<tr>
<th>{{ 'budget'|_ }}</th>
{% for year in years %}
<th>{{ year }}</th>
{% endfor %}
<th>{{ 'sum'|_ }}</th>
</tr>
</thead>
<tbody>
{% for id, info in budgetMultiYear %}
<tr>
<td>
{% if id == 0 %}
<a href="{{ route('budgets.noBudget') }}">{{ info.name }}</a>
{% else %}
<a href="{{ route('budgets.show', id) }}">{{ info.name }}</a>
{% endif %}
</td>
{% for amount in info.entries %}
<td data-value="{{ amount }}">
{{ amount|formatAmount }}
</td>
{% endfor %}
<td data-value="{{ info.sum }}">
{{ info.sum|formatAmount }}
</td>
</tr>
{% endfor %}
</tbody>
</table>

View File

@ -345,6 +345,12 @@ Route::group(
['uses' => 'Report\BudgetController@budgetYearOverview', 'as' => 'reports.data.budgetYearOverview']
);
// 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